feat: simplify name, set to center by default.

This commit is contained in:
rabbitism
2024-01-24 01:50:56 +08:00
parent 9441d7577e
commit 564a53409e
4 changed files with 31 additions and 10 deletions

View File

@@ -33,7 +33,9 @@
Command="{Binding ShowLocalOverlayModalDialogCommand}">
Show Local Overlay Dialog
</Button>
<Border CornerRadius="20" ClipToBounds="True">
<u:OverlayDialogHost HostId="LocalHost" />
</Border>
</Grid>
</Grid>
</UserControl>

View File

@@ -54,7 +54,7 @@ public static class OverlayDialog
Content = new TView(){ DataContext = vm },
DataContext = vm,
};
var host = OverlayDialogManager.GetOverlayDialogHost(hostId);
var host = OverlayDialogManager.GetHost(hostId);
host?.AddModalDialog(t);
return t.ShowAsync<TResult>();
}
@@ -69,7 +69,7 @@ public static class OverlayDialog
ExtendToClientArea = options.ExtendToClientArea,
Title = options.Title,
};
var host = OverlayDialogManager.GetOverlayDialogHost(hostId);
var host = OverlayDialogManager.GetHost(hostId);
host?.AddModalDialog(t);
return t.ShowAsync<TResult>();
}
@@ -82,7 +82,7 @@ public static class OverlayDialog
Content = new TView() { DataContext = vm },
DataContext = vm,
};
var host = OverlayDialogManager.GetOverlayDialogHost(hostId);
var host = OverlayDialogManager.GetHost(hostId);
host?.AddDialog(t);
}
@@ -96,7 +96,7 @@ public static class OverlayDialog
ExtendToClientArea = options.ExtendToClientArea,
Title = options.Title,
};
var host = OverlayDialogManager.GetOverlayDialogHost(hostId);
var host = OverlayDialogManager.GetHost(hostId);
host?.AddModalDialog(t);
}
}

View File

@@ -1,5 +1,6 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.Media;
@@ -40,7 +41,7 @@ public class OverlayDialogHost : Canvas
protected sealed override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
OverlayDialogManager.RegisterOverlayDialogHost(this, HostId);
OverlayDialogManager.RegisterHost(this, HostId);
}
protected sealed override void OnSizeChanged(SizeChangedEventArgs e)
@@ -55,7 +56,7 @@ public class OverlayDialogHost : Canvas
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
OverlayDialogManager.UnregisterOverlayDialogHost(HostId);
OverlayDialogManager.UnregisterHost(HostId);
base.OnDetachedFromVisualTree(e);
}
@@ -90,6 +91,9 @@ public class OverlayDialogHost : Canvas
{
this.Children.Add(control);
_dialogs.Add(control);
control.Measure(this.Bounds.Size);
control.Arrange(new Rect(control.DesiredSize));
SetToCenter(control);
control.OnClose += OnDialogClose;
control.OnLayerChange += OnDialogLayerChange;
ResetZIndices();
@@ -140,6 +144,9 @@ public class OverlayDialogHost : Canvas
ResetZIndices();
this.Children.Add(mask);
this.Children.Add(control);
control.Measure(this.Bounds.Size);
control.Arrange(new Rect(control.DesiredSize));
SetToCenter(control);
control.OnClose += OnDialogClose;
control.OnLayerChange += OnDialogLayerChange;
}
@@ -200,4 +207,16 @@ public class OverlayDialogHost : Canvas
index++;
}
}
private void SetToCenter(DialogControl? control)
{
// return;
if (control is null) return;
double left = (this.Bounds.Width - control.Bounds.Width) / 2;
double top = (this.Bounds.Height - control.Bounds.Height) / 2;
left = MathUtilities.Clamp(left, 0, Bounds.Width);
top = MathUtilities.Clamp(top, 0, Bounds.Height);
Canvas.SetLeft(control, left);
Canvas.SetTop(control, top);
}
}

View File

@@ -7,7 +7,7 @@ internal static class OverlayDialogManager
private static OverlayDialogHost? _defaultHost;
private static readonly ConcurrentDictionary<string, OverlayDialogHost> Hosts = new();
public static void RegisterOverlayDialogHost(OverlayDialogHost host, string? id)
public static void RegisterHost(OverlayDialogHost host, string? id)
{
if (id == null)
{
@@ -21,7 +21,7 @@ internal static class OverlayDialogManager
Hosts.TryAdd(id, host);
}
public static void UnregisterOverlayDialogHost(string? id)
public static void UnregisterHost(string? id)
{
if (id is null)
{
@@ -31,7 +31,7 @@ internal static class OverlayDialogManager
Hosts.TryRemove(id, out _);
}
public static OverlayDialogHost? GetOverlayDialogHost(string? id)
public static OverlayDialogHost? GetHost(string? id)
{
if (id is null)
{