feat: refactor dialog host to accept drawer.
This commit is contained in:
@@ -7,10 +7,10 @@
|
||||
<ControlTheme x:Key="{x:Type u:OverlayDialogHost}" TargetType="u:OverlayDialogHost">
|
||||
<Setter Property="OverlayMaskBrush" Value="{DynamicResource OverlayDialogMaskBrush}" />
|
||||
</ControlTheme>
|
||||
<ControlTheme x:Key="{x:Type u:DialogControl}" TargetType="u:DialogControl">
|
||||
<ControlTheme x:Key="{x:Type u:CustomDialogControl}" TargetType="u:CustomDialogControl">
|
||||
<Setter Property="CornerRadius" Value="12" />
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate TargetType="u:DialogControl">
|
||||
<ControlTemplate TargetType="u:CustomDialogControl">
|
||||
<Border
|
||||
Margin="8"
|
||||
Padding="0"
|
||||
@@ -30,7 +30,7 @@
|
||||
Content="{TemplateBinding Content}" />
|
||||
<Grid Grid.Row="0" ColumnDefinitions="*, Auto">
|
||||
<Panel
|
||||
Name="{x:Static u:DialogControl.PART_TitleArea}"
|
||||
Name="{x:Static u:DialogControlBase.PART_TitleArea}"
|
||||
Grid.Column="0"
|
||||
Grid.ColumnSpan="2"
|
||||
Background="Transparent" />
|
||||
@@ -50,7 +50,7 @@
|
||||
<Setter Property="ContextFlyout">
|
||||
<MenuFlyout>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].CloseDialog}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].CloseDialog}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.BringForward}"
|
||||
Header="{DynamicResource STRING_MENU_DIALOG_CLOSE}">
|
||||
<MenuItem.Icon>
|
||||
@@ -67,7 +67,7 @@
|
||||
<Setter Property="ContextFlyout">
|
||||
<MenuFlyout>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].UpdateLayer}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.BringForward}"
|
||||
Header="{DynamicResource STRING_MENU_BRING_FORWARD}">
|
||||
<MenuItem.Icon>
|
||||
@@ -78,7 +78,7 @@
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].UpdateLayer}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.BringToFront}"
|
||||
Header="{DynamicResource STRING_MENU_BRING_TO_FRONT}">
|
||||
<MenuItem.Icon>
|
||||
@@ -89,7 +89,7 @@
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].UpdateLayer}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.SendBackward}"
|
||||
Header="{DynamicResource STRING_MENU_SEND_BACKWARD}">
|
||||
<MenuItem.Icon>
|
||||
@@ -100,7 +100,7 @@
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].UpdateLayer}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.SendToBack}"
|
||||
Header="{DynamicResource STRING_MENU_SEND_TO_BACK}">
|
||||
<MenuItem.Icon>
|
||||
@@ -111,7 +111,7 @@
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].CloseDialog}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].CloseDialog}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.BringForward}"
|
||||
Header="{DynamicResource STRING_MENU_DIALOG_CLOSE}">
|
||||
<MenuItem.Icon>
|
||||
@@ -152,7 +152,7 @@
|
||||
</ScrollViewer>
|
||||
<Grid Grid.Row="0" ColumnDefinitions="Auto, *, Auto">
|
||||
<Panel
|
||||
Name="{x:Static u:DialogControl.PART_TitleArea}"
|
||||
Name="{x:Static u:DialogControlBase.PART_TitleArea}"
|
||||
Grid.Column="0"
|
||||
Grid.ColumnSpan="3"
|
||||
Background="Transparent" />
|
||||
@@ -338,7 +338,7 @@
|
||||
<Setter Property="ContextFlyout">
|
||||
<MenuFlyout>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].CloseDialog}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].CloseDialog}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.BringForward}"
|
||||
Header="{DynamicResource STRING_MENU_DIALOG_CLOSE}">
|
||||
<MenuItem.Icon>
|
||||
@@ -355,7 +355,7 @@
|
||||
<Setter Property="ContextFlyout">
|
||||
<MenuFlyout>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].UpdateLayer}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.BringForward}"
|
||||
Header="{DynamicResource STRING_MENU_BRING_FORWARD}">
|
||||
<MenuItem.Icon>
|
||||
@@ -366,7 +366,7 @@
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].UpdateLayer}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.BringToFront}"
|
||||
Header="{DynamicResource STRING_MENU_BRING_TO_FRONT}">
|
||||
<MenuItem.Icon>
|
||||
@@ -377,7 +377,7 @@
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].UpdateLayer}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.SendBackward}"
|
||||
Header="{DynamicResource STRING_MENU_SEND_BACKWARD}">
|
||||
<MenuItem.Icon>
|
||||
@@ -388,7 +388,7 @@
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].UpdateLayer}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.SendToBack}"
|
||||
Header="{DynamicResource STRING_MENU_SEND_TO_BACK}">
|
||||
<MenuItem.Icon>
|
||||
@@ -399,7 +399,7 @@
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
<MenuItem
|
||||
Command="{Binding $parent[u:DialogControl].CloseDialog}"
|
||||
Command="{Binding $parent[u:CustomDialogControl].CloseDialog}"
|
||||
CommandParameter="{x:Static u:DialogLayerChangeType.BringForward}"
|
||||
Header="{DynamicResource STRING_MENU_DIALOG_CLOSE}">
|
||||
<MenuItem.Icon>
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
Content="{TemplateBinding Content}" />
|
||||
<Grid Grid.Row="0" ColumnDefinitions="*, Auto">
|
||||
<Panel
|
||||
Name="{x:Static u:DialogControl.PART_TitleArea}"
|
||||
Name="{x:Static u:DialogControlBase.PART_TitleArea}"
|
||||
Grid.Column="0"
|
||||
Grid.ColumnSpan="2"
|
||||
Background="Transparent" />
|
||||
|
||||
@@ -165,7 +165,7 @@
|
||||
<Grid RowDefinitions="Auto, *, Auto">
|
||||
<Grid Grid.Row="0" ColumnDefinitions="Auto, *, Auto">
|
||||
<Panel
|
||||
Name="{x:Static u:DialogControl.PART_TitleArea}"
|
||||
Name="{x:Static u:DialogControlBase.PART_TitleArea}"
|
||||
Grid.Column="0"
|
||||
Grid.ColumnSpan="3"
|
||||
Background="Transparent" />
|
||||
@@ -247,7 +247,7 @@
|
||||
<Style Selector="^ /template/ Panel#PART_TitleArea">
|
||||
<Setter Property="ContextFlyout">
|
||||
<MenuFlyout>
|
||||
<MenuItem Command="{Binding $parent[u:DialogControl].CloseDialog}" Header="{DynamicResource STRING_MENU_DIALOG_CLOSE}">
|
||||
<MenuItem Command="{Binding $parent[u:CustomDialogControl].CloseDialog}" Header="{DynamicResource STRING_MENU_DIALOG_CLOSE}">
|
||||
<MenuItem.Icon>
|
||||
<PathIcon
|
||||
Width="12"
|
||||
|
||||
39
src/Ursa/Controls/Dialog/CustomDialogControl.cs
Normal file
39
src/Ursa/Controls/Dialog/CustomDialogControl.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Input.GestureRecognizers;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Threading;
|
||||
using Ursa.Common;
|
||||
using Ursa.Controls.OverlayShared;
|
||||
using Ursa.EventArgs;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class CustomDialogControl: DialogControlBase
|
||||
{
|
||||
internal bool IsCloseButtonVisible { get; set; }
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
if (_closeButton is not null)
|
||||
{
|
||||
_closeButton.IsVisible = IsCloseButtonVisible;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (DataContext is IDialogContext context)
|
||||
{
|
||||
context.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
OnElementClosing(this, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,7 @@ namespace Ursa.Controls;
|
||||
[TemplatePart(PART_CancelButton, typeof(Button))]
|
||||
[TemplatePart(PART_YesButton, typeof(Button))]
|
||||
[TemplatePart(PART_NoButton, typeof(Button))]
|
||||
[TemplatePart(PART_TitleArea, typeof(Panel))]
|
||||
public class DefaultDialogControl: DialogControl
|
||||
public class DefaultDialogControl: DialogControlBase
|
||||
{
|
||||
public const string PART_OKButton = "PART_OKButton";
|
||||
public const string PART_CancelButton = "PART_CancelButton";
|
||||
@@ -105,35 +104,30 @@ public class DefaultDialogControl: DialogControl
|
||||
}
|
||||
}
|
||||
|
||||
private void SetVisibility(Button? button, bool visible)
|
||||
{
|
||||
if (button is not null) button.IsVisible = visible;
|
||||
}
|
||||
|
||||
private void DefaultButtonsClose(object sender, RoutedEventArgs args)
|
||||
{
|
||||
if (sender is Button button)
|
||||
{
|
||||
if (button == _okButton)
|
||||
{
|
||||
OnDialogControlClosing(this, DialogResult.OK);
|
||||
OnElementClosing(this, DialogResult.OK);
|
||||
}
|
||||
else if (button == _cancelButton)
|
||||
{
|
||||
OnDialogControlClosing(this, DialogResult.Cancel);
|
||||
OnElementClosing(this, DialogResult.Cancel);
|
||||
}
|
||||
else if (button == _yesButton)
|
||||
{
|
||||
OnDialogControlClosing(this, DialogResult.Yes);
|
||||
OnElementClosing(this, DialogResult.Yes);
|
||||
}
|
||||
else if (button == _noButton)
|
||||
{
|
||||
OnDialogControlClosing(this, DialogResult.No);
|
||||
OnElementClosing(this, DialogResult.No);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal override void CloseDialog()
|
||||
public override void Close()
|
||||
{
|
||||
if (DataContext is IDialogContext context)
|
||||
{
|
||||
@@ -150,7 +144,7 @@ public class DefaultDialogControl: DialogControl
|
||||
DialogButton.YesNoCancel => DialogResult.Cancel,
|
||||
_ => DialogResult.None
|
||||
};
|
||||
OnDialogControlClosing(this, result);
|
||||
OnElementClosing(this, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,10 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Input.GestureRecognizers;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Threading;
|
||||
using Ursa.Common;
|
||||
using Ursa.Controls.OverlayShared;
|
||||
using Ursa.EventArgs;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
@@ -14,15 +12,12 @@ namespace Ursa.Controls;
|
||||
[TemplatePart(PART_CloseButton, typeof(Button))]
|
||||
[TemplatePart(PART_TitleArea, typeof(Panel))]
|
||||
[PseudoClasses(PC_Modal)]
|
||||
public class DialogControl: ContentControl
|
||||
public abstract class DialogControlBase: OverlayFeedbackElement
|
||||
{
|
||||
public const string PART_CloseButton = "PART_CloseButton";
|
||||
public const string PART_TitleArea = "PART_TitleArea";
|
||||
public const string PC_Modal = ":modal";
|
||||
|
||||
protected internal Button? _closeButton;
|
||||
private Panel? _titleArea;
|
||||
|
||||
internal HorizontalPosition HorizontalAnchor { get; set; } = HorizontalPosition.Center;
|
||||
internal VerticalPosition VerticalAnchor { get; set; } = VerticalPosition.Center;
|
||||
internal HorizontalPosition ActualHorizontalAnchor { get; set; }
|
||||
@@ -32,9 +27,16 @@ public class DialogControl: ContentControl
|
||||
internal double? HorizontalOffsetRatio { get; set; }
|
||||
internal double? VerticalOffsetRatio { get; set; }
|
||||
internal bool CanClickOnMaskToClose { get; set; }
|
||||
internal bool IsCloseButtonVisible { get; set; }
|
||||
|
||||
public static readonly RoutedEvent<DialogLayerChangeEventArgs> LayerChangedEvent = RoutedEvent.Register<DialogControl, DialogLayerChangeEventArgs>(
|
||||
protected internal Button? _closeButton;
|
||||
private Panel? _titleArea;
|
||||
|
||||
internal void SetAsModal(bool modal)
|
||||
{
|
||||
PseudoClasses.Set(PC_Modal, modal);
|
||||
}
|
||||
|
||||
public static readonly RoutedEvent<DialogLayerChangeEventArgs> LayerChangedEvent = RoutedEvent.Register<CustomDialogControl, DialogLayerChangeEventArgs>(
|
||||
nameof(LayerChanged), RoutingStrategies.Bubble);
|
||||
public event EventHandler<DialogLayerChangeEventArgs> LayerChanged
|
||||
{
|
||||
@@ -42,51 +44,26 @@ public class DialogControl: ContentControl
|
||||
remove => RemoveHandler(LayerChangedEvent, value);
|
||||
}
|
||||
|
||||
public static readonly RoutedEvent<ResultEventArgs> ClosedEvent = RoutedEvent.Register<DrawerControlBase, ResultEventArgs>(
|
||||
nameof(Closed), RoutingStrategies.Bubble);
|
||||
|
||||
public event EventHandler<ResultEventArgs> Closed
|
||||
public void UpdateLayer(object? o)
|
||||
{
|
||||
add => AddHandler(ClosedEvent, value);
|
||||
remove => RemoveHandler(ClosedEvent, value);
|
||||
}
|
||||
|
||||
static DialogControl()
|
||||
if (o is DialogLayerChangeType t)
|
||||
{
|
||||
DataContextProperty.Changed.AddClassHandler<DialogControl, object?>((o, e) => o.OnDataContextChange(e));
|
||||
}
|
||||
|
||||
private void OnDataContextChange(AvaloniaPropertyChangedEventArgs<object?> args)
|
||||
{
|
||||
if (args.OldValue.Value is IDialogContext oldContext)
|
||||
{
|
||||
oldContext.RequestClose -= OnContextRequestClose;
|
||||
}
|
||||
if (args.NewValue.Value is IDialogContext newContext)
|
||||
{
|
||||
newContext.RequestClose += OnContextRequestClose;
|
||||
RaiseEvent(new DialogLayerChangeEventArgs(LayerChangedEvent, t));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
EventHelper.UnregisterClickEvent(OnCloseButtonClick, _closeButton);
|
||||
|
||||
_titleArea?.RemoveHandler(PointerMovedEvent, OnTitlePointerMove);
|
||||
_titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed);
|
||||
_titleArea?.RemoveHandler(PointerReleasedEvent, OnTitlePointerRelease);
|
||||
|
||||
_closeButton = e.NameScope.Find<Button>(PART_CloseButton);
|
||||
_titleArea = e.NameScope.Find<Panel>(PART_TitleArea);
|
||||
if (_closeButton is not null)
|
||||
{
|
||||
_closeButton.IsVisible = IsCloseButtonVisible;
|
||||
}
|
||||
_titleArea?.AddHandler(PointerMovedEvent, OnTitlePointerMove, RoutingStrategies.Bubble);
|
||||
_titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble);
|
||||
_titleArea?.AddHandler(PointerReleasedEvent, OnTitlePointerRelease, RoutingStrategies.Bubble);
|
||||
EventHelper.UnregisterClickEvent(OnCloseButtonClick, _closeButton);
|
||||
_closeButton = e.NameScope.Find<Button>(PART_CloseButton);
|
||||
EventHelper.RegisterClickEvent(OnCloseButtonClick, _closeButton);
|
||||
}
|
||||
|
||||
@@ -105,77 +82,5 @@ public class DialogControl: ContentControl
|
||||
e.Source = this;
|
||||
}
|
||||
|
||||
|
||||
public Task<T?> ShowAsync<T>(CancellationToken? token = default)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<T?>();
|
||||
token?.Register(() =>
|
||||
{
|
||||
Dispatcher.UIThread.Invoke(CloseDialog);
|
||||
});
|
||||
|
||||
void OnCloseHandler(object sender, ResultEventArgs? args)
|
||||
{
|
||||
if (args?.Result is T result)
|
||||
{
|
||||
tcs.SetResult(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
tcs.SetResult(default);
|
||||
}
|
||||
RemoveHandler(ClosedEvent, OnCloseHandler);
|
||||
}
|
||||
|
||||
AddHandler(ClosedEvent, OnCloseHandler);
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
private void OnCloseButtonClick(object sender, RoutedEventArgs args) => CloseDialog();
|
||||
|
||||
private void OnContextRequestClose(object sender, object? args)
|
||||
{
|
||||
RaiseEvent(new ResultEventArgs(ClosedEvent, args));
|
||||
}
|
||||
|
||||
|
||||
public void UpdateLayer(object? o)
|
||||
{
|
||||
if (o is DialogLayerChangeType t)
|
||||
{
|
||||
RaiseEvent(new DialogLayerChangeEventArgs(LayerChangedEvent, t));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used for inherited classes to invoke the DialogControlClosing event.
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="args"></param>
|
||||
protected virtual void OnDialogControlClosing(object sender, object? args)
|
||||
{
|
||||
RaiseEvent(new ResultEventArgs(ClosedEvent, args));
|
||||
}
|
||||
|
||||
internal void SetAsModal(bool modal)
|
||||
{
|
||||
PseudoClasses.Set(PC_Modal, modal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is exposed internally for closing the dialog from neither context nor closing by clicking on the close button.
|
||||
/// It is also exposed to be bound to context flyout.
|
||||
/// It is virtual because inherited classes may return a different result by default.
|
||||
/// </summary>
|
||||
internal virtual void CloseDialog()
|
||||
{
|
||||
if (DataContext is IDialogContext context)
|
||||
{
|
||||
context.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
OnDialogControlClosing(this, null);
|
||||
}
|
||||
}
|
||||
private void OnCloseButtonClick(object sender, RoutedEventArgs args) => Close();
|
||||
}
|
||||
@@ -58,7 +58,7 @@ public static class OverlayDialog
|
||||
{
|
||||
var host = OverlayDialogManager.GetHost(hostId);
|
||||
if (host is null) return;
|
||||
var t = new DialogControl()
|
||||
var t = new CustomDialogControl()
|
||||
{
|
||||
Content = new TView(),
|
||||
DataContext = vm,
|
||||
@@ -72,7 +72,7 @@ public static class OverlayDialog
|
||||
{
|
||||
var host = OverlayDialogManager.GetHost(hostId);
|
||||
if (host is null) return;
|
||||
var t = new DialogControl()
|
||||
var t = new CustomDialogControl()
|
||||
{
|
||||
Content = control,
|
||||
DataContext = vm,
|
||||
@@ -89,7 +89,7 @@ public static class OverlayDialog
|
||||
var view = host.GetDataTemplate(vm)?.Build(vm);
|
||||
if (view is null) view = new ContentControl() { Padding = new Thickness(24) };
|
||||
view.DataContext = vm;
|
||||
var t = new DialogControl()
|
||||
var t = new CustomDialogControl()
|
||||
{
|
||||
Content = view,
|
||||
DataContext = vm,
|
||||
@@ -135,7 +135,7 @@ public static class OverlayDialog
|
||||
{
|
||||
var host = OverlayDialogManager.GetHost(hostId);
|
||||
if (host is null) return Task.FromResult(default(TResult));
|
||||
var t = new DialogControl()
|
||||
var t = new CustomDialogControl()
|
||||
{
|
||||
Content = new TView(),
|
||||
DataContext = vm,
|
||||
@@ -150,7 +150,7 @@ public static class OverlayDialog
|
||||
{
|
||||
var host = OverlayDialogManager.GetHost(hostId);
|
||||
if (host is null) return Task.FromResult(default(TResult));
|
||||
var t = new DialogControl()
|
||||
var t = new CustomDialogControl()
|
||||
{
|
||||
Content = control,
|
||||
DataContext = vm,
|
||||
@@ -168,7 +168,7 @@ public static class OverlayDialog
|
||||
var view = host.GetDataTemplate(vm)?.Build(vm);
|
||||
if (view is null) view = new ContentControl() { Padding = new Thickness(24) };
|
||||
view.DataContext = vm;
|
||||
var t = new DialogControl()
|
||||
var t = new CustomDialogControl()
|
||||
{
|
||||
Content = view,
|
||||
DataContext = vm,
|
||||
@@ -178,7 +178,7 @@ public static class OverlayDialog
|
||||
return t.ShowAsync<TResult?>(token);
|
||||
}
|
||||
|
||||
private static void ConfigureDialogControl(DialogControl control, OverlayDialogOptions? options)
|
||||
private static void ConfigureDialogControl(CustomDialogControl control, OverlayDialogOptions? options)
|
||||
{
|
||||
options ??= OverlayDialogOptions.Default;
|
||||
control.HorizontalAnchor = options.HorizontalAnchor;
|
||||
|
||||
@@ -9,103 +9,19 @@ using Avalonia.Layout;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Styling;
|
||||
using Avalonia.Utilities;
|
||||
using Ursa.Controls.OverlayShared;
|
||||
using Ursa.EventArgs;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class OverlayDialogHost : Canvas
|
||||
public partial class OverlayDialogHost
|
||||
{
|
||||
private readonly List<DialogControl> _dialogs = new();
|
||||
private readonly List<Control> _modalDialogs = new();
|
||||
private readonly List<Border> _masks = new();
|
||||
|
||||
public string? HostId { get; set; }
|
||||
|
||||
private Point _lastPoint;
|
||||
|
||||
|
||||
public DataTemplates DialogDataTemplates { get; set; } = new DataTemplates();
|
||||
public Thickness SnapThickness { get; set; } = new Thickness(0);
|
||||
|
||||
public static readonly StyledProperty<IBrush?> OverlayMaskBrushProperty =
|
||||
AvaloniaProperty.Register<OverlayDialogHost, IBrush?>(
|
||||
nameof(OverlayMaskBrush));
|
||||
|
||||
public IBrush? OverlayMaskBrush
|
||||
{
|
||||
get => GetValue(OverlayMaskBrushProperty);
|
||||
set => SetValue(OverlayMaskBrushProperty, value);
|
||||
}
|
||||
|
||||
private Border CreateOverlayMask(bool canCloseOnClick)
|
||||
{
|
||||
Border border = new()
|
||||
{
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch,
|
||||
VerticalAlignment = VerticalAlignment.Stretch,
|
||||
Width = this.Bounds.Width,
|
||||
Height = this.Bounds.Height,
|
||||
[!BackgroundProperty] = this[!OverlayMaskBrushProperty],
|
||||
IsVisible = true,
|
||||
};
|
||||
if (canCloseOnClick)
|
||||
{
|
||||
border.AddHandler(PointerReleasedEvent, ClickBorderToCloseDialog);
|
||||
}
|
||||
return border;
|
||||
}
|
||||
|
||||
private void ClickBorderToCloseDialog(object sender, PointerReleasedEventArgs e)
|
||||
{
|
||||
if (sender is Border border)
|
||||
{
|
||||
int i = _masks.IndexOf(border);
|
||||
if (_modalDialogs[i] is DialogControl dialog)
|
||||
{
|
||||
dialog?.CloseDialog();
|
||||
border.RemoveHandler(PointerReleasedEvent, ClickBorderToCloseDialog);
|
||||
}
|
||||
else if(_modalDialogs[i] is DrawerControlBase drawer)
|
||||
{
|
||||
drawer.CloseDrawer();
|
||||
border.RemoveHandler(PointerReleasedEvent, ClickBorderToCloseDialog);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected sealed override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
OverlayDialogManager.RegisterHost(this, HostId);
|
||||
}
|
||||
|
||||
protected sealed override void OnSizeChanged(SizeChangedEventArgs e)
|
||||
{
|
||||
base.OnSizeChanged(e);
|
||||
for (int i = 0; i < _masks.Count; i++)
|
||||
{
|
||||
_masks[i].Width = this.Bounds.Width;
|
||||
_masks[i].Height = this.Bounds.Height;
|
||||
}
|
||||
|
||||
var oldSize = e.PreviousSize;
|
||||
var newSize = e.NewSize;
|
||||
foreach (var dialog in _dialogs)
|
||||
{
|
||||
ResetDialogPosition(dialog, oldSize, newSize);
|
||||
}
|
||||
|
||||
foreach (var modalDialog in _modalDialogs)
|
||||
{
|
||||
if (modalDialog is DialogControl c)
|
||||
{
|
||||
ResetDialogPosition(c, oldSize, newSize);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetDialogPosition(DialogControl control, Size oldSize, Size newSize)
|
||||
private void ResetDialogPosition(DialogControlBase control, Size oldSize, Size newSize)
|
||||
{
|
||||
var width = newSize.Width - control.Bounds.Width;
|
||||
var height = newSize.Height - control.Bounds.Height;
|
||||
@@ -131,15 +47,9 @@ public class OverlayDialogHost : Canvas
|
||||
SetTop(control, Math.Max(0.0, newTop));
|
||||
}
|
||||
|
||||
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
OverlayDialogManager.UnregisterHost(HostId);
|
||||
base.OnDetachedFromVisualTree(e);
|
||||
}
|
||||
|
||||
protected override void OnPointerMoved(PointerEventArgs e)
|
||||
{
|
||||
if (e.Source is DialogControl item)
|
||||
if (e.Source is DialogControlBase item)
|
||||
{
|
||||
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
|
||||
{
|
||||
@@ -156,7 +66,7 @@ public class OverlayDialogHost : Canvas
|
||||
|
||||
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
||||
{
|
||||
if (e.Source is DialogControl item)
|
||||
if (e.Source is DialogControlBase item)
|
||||
{
|
||||
_lastPoint = e.GetPosition(item);
|
||||
}
|
||||
@@ -164,33 +74,31 @@ public class OverlayDialogHost : Canvas
|
||||
|
||||
protected override void OnPointerReleased(PointerReleasedEventArgs e)
|
||||
{
|
||||
if (e.Source is DialogControl item)
|
||||
if (e.Source is DialogControlBase item)
|
||||
{
|
||||
AnchorDialog(item);
|
||||
}
|
||||
}
|
||||
|
||||
internal void AddDialog(DialogControl control)
|
||||
internal void AddDialog(DialogControlBase control)
|
||||
{
|
||||
this.Children.Add(control);
|
||||
_dialogs.Add(control);
|
||||
control.Measure(this.Bounds.Size);
|
||||
control.Arrange(new Rect(control.DesiredSize));
|
||||
SetToPosition(control);
|
||||
control.AddHandler(DialogControl.ClosedEvent, OnDialogControlClosing);
|
||||
control.AddHandler(DialogControl.LayerChangedEvent, OnDialogLayerChanged);
|
||||
control.AddHandler(OverlayFeedbackElement.ClosedEvent, OnDialogControlClosing);
|
||||
control.AddHandler(DialogControlBase.LayerChangedEvent, OnDialogLayerChanged);
|
||||
ResetZIndices();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void OnDialogControlClosing(object sender, object? e)
|
||||
{
|
||||
if (sender is DialogControl control)
|
||||
if (sender is DialogControlBase control)
|
||||
{
|
||||
Children.Remove(control);
|
||||
control.RemoveHandler(DialogControl.ClosedEvent, OnDialogControlClosing);
|
||||
control.RemoveHandler(DialogControl.LayerChangedEvent, OnDialogLayerChanged);
|
||||
control.RemoveHandler(OverlayFeedbackElement.ClosedEvent, OnDialogControlClosing);
|
||||
control.RemoveHandler(DialogControlBase.LayerChangedEvent, OnDialogLayerChanged);
|
||||
if (_dialogs.Contains(control))
|
||||
{
|
||||
_dialogs.Remove(control);
|
||||
@@ -217,7 +125,7 @@ public class OverlayDialogHost : Canvas
|
||||
/// Add a dialog as a modal dialog to the host
|
||||
/// </summary>
|
||||
/// <param name="control"></param>
|
||||
internal void AddModalDialog(DialogControl control)
|
||||
internal void AddModalDialog(DialogControlBase control)
|
||||
{
|
||||
var mask = CreateOverlayMask(control.CanClickOnMaskToClose);
|
||||
_masks.Add(mask);
|
||||
@@ -233,70 +141,19 @@ public class OverlayDialogHost : Canvas
|
||||
control.Measure(this.Bounds.Size);
|
||||
control.Arrange(new Rect(control.DesiredSize));
|
||||
SetToPosition(control);
|
||||
control.AddHandler(DialogControl.ClosedEvent, OnDialogControlClosing);
|
||||
control.AddHandler(DialogControl.LayerChangedEvent, OnDialogLayerChanged);
|
||||
control.AddHandler(OverlayFeedbackElement.ClosedEvent, OnDialogControlClosing);
|
||||
control.AddHandler(DialogControlBase.LayerChangedEvent, OnDialogLayerChanged);
|
||||
}
|
||||
|
||||
internal async void AddDrawer(DrawerControlBase control)
|
||||
{
|
||||
var mask = CreateOverlayMask(false);
|
||||
mask.Opacity = 0;
|
||||
_masks.Add(mask);
|
||||
_modalDialogs.Add(control);
|
||||
// control.SetAsModal(true);
|
||||
for (int i = 0; i < _masks.Count-1; i++)
|
||||
{
|
||||
_masks[i].Opacity = 0.5;
|
||||
}
|
||||
ResetZIndices();
|
||||
this.Children.Add(mask);
|
||||
this.Children.Add(control);
|
||||
control.Measure(this.Bounds.Size);
|
||||
control.Arrange(new Rect(control.DesiredSize));
|
||||
control.Height = this.Bounds.Height;
|
||||
control.AddHandler(DrawerControlBase.ClosedEvent, OnDrawerControlClosing);
|
||||
// SetLeft(control, this.Bounds.Width - control.Bounds.Width);
|
||||
var animation = CreateAnimation(control.Bounds.Width);
|
||||
var animation2 = CreateOpacityAnimation();
|
||||
await Task.WhenAll(animation.RunAsync(control), animation2.RunAsync(mask));
|
||||
}
|
||||
|
||||
private Animation CreateAnimation(double width)
|
||||
{
|
||||
var animation = new Animation();
|
||||
animation.Easing = new CubicEaseOut();
|
||||
animation.FillMode = FillMode.Forward;
|
||||
var keyFrame1 = new KeyFrame(){ Cue = new Cue(0.0) };
|
||||
keyFrame1.Setters.Add(new Setter() { Property = Canvas.LeftProperty, Value = Bounds.Width });
|
||||
var keyFrame2 = new KeyFrame() { Cue = new Cue(1.0) };
|
||||
keyFrame2.Setters.Add(new Setter() { Property = Canvas.LeftProperty, Value = Bounds.Width - width });
|
||||
animation.Children.Add(keyFrame1);
|
||||
animation.Children.Add(keyFrame2);
|
||||
animation.Duration = TimeSpan.FromSeconds(0.3);
|
||||
return animation;
|
||||
}
|
||||
|
||||
private Animation CreateOpacityAnimation()
|
||||
{
|
||||
var animation = new Animation();
|
||||
animation.FillMode = FillMode.Forward;
|
||||
var keyFrame1 = new KeyFrame(){ Cue = new Cue(0.0) };
|
||||
keyFrame1.Setters.Add(new Setter(){ Property = OpacityProperty, Value = 0.0});
|
||||
var keyFrame2 = new KeyFrame() { Cue = new Cue(1.0) };
|
||||
keyFrame2.Setters.Add(new Setter() { Property = OpacityProperty, Value = 1.0 });
|
||||
animation.Children.Add(keyFrame1);
|
||||
animation.Children.Add(keyFrame2);
|
||||
animation.Duration = TimeSpan.FromSeconds(0.3);
|
||||
return animation;
|
||||
}
|
||||
|
||||
private void OnDrawerControlClosing(object sender, ResultEventArgs e)
|
||||
{
|
||||
if (sender is DrawerControlBase control)
|
||||
{
|
||||
Children.Remove(control);
|
||||
control.RemoveHandler(DialogControl.ClosedEvent, OnDialogControlClosing);
|
||||
control.RemoveHandler(DialogControl.LayerChangedEvent, OnDialogLayerChanged);
|
||||
control.RemoveHandler(CustomDialogControl.ClosedEvent, OnDialogControlClosing);
|
||||
control.RemoveHandler(CustomDialogControl.LayerChangedEvent, OnDialogLayerChanged);
|
||||
if (_modalDialogs.Contains(control))
|
||||
{
|
||||
_modalDialogs.Remove(control);
|
||||
@@ -318,7 +175,7 @@ public class OverlayDialogHost : Canvas
|
||||
// Handle dialog layer change event
|
||||
private void OnDialogLayerChanged(object sender, DialogLayerChangeEventArgs e)
|
||||
{
|
||||
if (sender is not DialogControl control)
|
||||
if (sender is not CustomDialogControl control)
|
||||
return;
|
||||
if (!_dialogs.Contains(control))
|
||||
return;
|
||||
@@ -350,29 +207,12 @@ public class OverlayDialogHost : Canvas
|
||||
for (int i = 0; i < _masks.Count * 2; i += 2)
|
||||
{
|
||||
_masks[i].ZIndex = _dialogs.Count + i;
|
||||
_modalDialogs[i].ZIndex = _dialogs.Count + i + 1;
|
||||
(_modalDialogs[i] as Control)!.ZIndex = _dialogs.Count + i + 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ResetZIndices()
|
||||
{
|
||||
int index = 0;
|
||||
for ( int i = 0; i< _dialogs.Count; i++)
|
||||
{
|
||||
_dialogs[i].ZIndex = index;
|
||||
index++;
|
||||
}
|
||||
for(int i = 0; i< _masks.Count; i++)
|
||||
{
|
||||
_masks[i].ZIndex = index;
|
||||
index++;
|
||||
_modalDialogs[i].ZIndex = index;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetToPosition(DialogControl? control)
|
||||
private void SetToPosition(DialogControlBase? control)
|
||||
{
|
||||
if (control is null) return;
|
||||
double left = GetLeftPosition(control);
|
||||
@@ -382,7 +222,7 @@ public class OverlayDialogHost : Canvas
|
||||
AnchorDialog(control);
|
||||
}
|
||||
|
||||
private void AnchorDialog(DialogControl control)
|
||||
private void AnchorDialog(DialogControlBase control)
|
||||
{
|
||||
control.ActualHorizontalAnchor = HorizontalPosition.Center;
|
||||
control.ActualVerticalAnchor = VerticalPosition.Center;
|
||||
@@ -423,7 +263,7 @@ public class OverlayDialogHost : Canvas
|
||||
control.VerticalOffsetRatio = (top + bottom) == 0 ? 0 : top / (top + bottom);
|
||||
}
|
||||
|
||||
private double GetLeftPosition(DialogControl control)
|
||||
private double GetLeftPosition(DialogControlBase control)
|
||||
{
|
||||
double left = 0;
|
||||
double offset = Math.Max(0, control.HorizontalOffset ?? 0);
|
||||
@@ -451,7 +291,7 @@ public class OverlayDialogHost : Canvas
|
||||
return left;
|
||||
}
|
||||
|
||||
private double GetTopPosition(DialogControl control)
|
||||
private double GetTopPosition(DialogControlBase control)
|
||||
{
|
||||
double top = 0;
|
||||
double offset = Math.Max(0, control.VerticalOffset ?? 0);
|
||||
@@ -474,22 +314,5 @@ public class OverlayDialogHost : Canvas
|
||||
return top;
|
||||
}
|
||||
|
||||
internal IDataTemplate? GetDataTemplate(object? o)
|
||||
{
|
||||
if (o is null) return null;
|
||||
IDataTemplate? result = null;
|
||||
var templates = this.DialogDataTemplates;
|
||||
result = templates.FirstOrDefault(a => a.Match(o));
|
||||
if (result != null) return result;
|
||||
var keys = this.Resources.Keys;
|
||||
foreach (var key in keys)
|
||||
{
|
||||
if (Resources.TryGetValue(key, out var value) && value is IDataTemplate t)
|
||||
{
|
||||
result = t;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
64
src/Ursa/Controls/Dialog/OverlayDialogHost.Drawer.cs
Normal file
64
src/Ursa/Controls/Dialog/OverlayDialogHost.Drawer.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Animation;
|
||||
using Avalonia.Animation.Easings;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Styling;
|
||||
using Ursa.Controls.OverlayShared;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public partial class OverlayDialogHost
|
||||
{
|
||||
internal async void AddDrawer(DrawerControlBase control)
|
||||
{
|
||||
var mask = CreateOverlayMask(false);
|
||||
mask.Opacity = 0;
|
||||
_masks.Add(mask);
|
||||
_modalDialogs.Add(control);
|
||||
// control.SetAsModal(true);
|
||||
for (int i = 0; i < _masks.Count-1; i++)
|
||||
{
|
||||
_masks[i].Opacity = 0.5;
|
||||
}
|
||||
ResetZIndices();
|
||||
this.Children.Add(mask);
|
||||
this.Children.Add(control);
|
||||
control.Measure(this.Bounds.Size);
|
||||
control.Arrange(new Rect(control.DesiredSize));
|
||||
control.Height = this.Bounds.Height;
|
||||
control.AddHandler(OverlayFeedbackElement.ClosedEvent, OnDrawerControlClosing);
|
||||
// SetLeft(control, this.Bounds.Width - control.Bounds.Width);
|
||||
var animation = CreateAnimation(control.Bounds.Width);
|
||||
var animation2 = CreateOpacityAnimation();
|
||||
await Task.WhenAll(animation.RunAsync(control), animation2.RunAsync(mask));
|
||||
}
|
||||
|
||||
private Animation CreateAnimation(double width)
|
||||
{
|
||||
var animation = new Animation();
|
||||
animation.Easing = new CubicEaseOut();
|
||||
animation.FillMode = FillMode.Forward;
|
||||
var keyFrame1 = new KeyFrame(){ Cue = new Cue(0.0) };
|
||||
keyFrame1.Setters.Add(new Setter() { Property = Canvas.LeftProperty, Value = Bounds.Width });
|
||||
var keyFrame2 = new KeyFrame() { Cue = new Cue(1.0) };
|
||||
keyFrame2.Setters.Add(new Setter() { Property = Canvas.LeftProperty, Value = Bounds.Width - width });
|
||||
animation.Children.Add(keyFrame1);
|
||||
animation.Children.Add(keyFrame2);
|
||||
animation.Duration = TimeSpan.FromSeconds(0.3);
|
||||
return animation;
|
||||
}
|
||||
|
||||
private Animation CreateOpacityAnimation()
|
||||
{
|
||||
var animation = new Animation();
|
||||
animation.FillMode = FillMode.Forward;
|
||||
var keyFrame1 = new KeyFrame(){ Cue = new Cue(0.0) };
|
||||
keyFrame1.Setters.Add(new Setter(){ Property = OpacityProperty, Value = 0.0});
|
||||
var keyFrame2 = new KeyFrame() { Cue = new Cue(1.0) };
|
||||
keyFrame2.Setters.Add(new Setter() { Property = OpacityProperty, Value = 1.0 });
|
||||
animation.Children.Add(keyFrame1);
|
||||
animation.Children.Add(keyFrame2);
|
||||
animation.Duration = TimeSpan.FromSeconds(0.3);
|
||||
return animation;
|
||||
}
|
||||
}
|
||||
137
src/Ursa/Controls/Dialog/OverlayDialogHost.Shared.cs
Normal file
137
src/Ursa/Controls/Dialog/OverlayDialogHost.Shared.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Media;
|
||||
using Ursa.Controls.OverlayShared;
|
||||
using Avalonia.Layout;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public partial class OverlayDialogHost: Canvas
|
||||
{
|
||||
private readonly List<OverlayFeedbackElement> _dialogs = new();
|
||||
private readonly List<OverlayFeedbackElement> _modalDialogs = new();
|
||||
private readonly List<Border> _masks = new();
|
||||
|
||||
public string? HostId { get; set; }
|
||||
|
||||
public DataTemplates DialogDataTemplates { get; set; } = new DataTemplates();
|
||||
|
||||
public static readonly StyledProperty<IBrush?> OverlayMaskBrushProperty =
|
||||
AvaloniaProperty.Register<OverlayDialogHost, IBrush?>(
|
||||
nameof(OverlayMaskBrush));
|
||||
|
||||
public IBrush? OverlayMaskBrush
|
||||
{
|
||||
get => GetValue(OverlayMaskBrushProperty);
|
||||
set => SetValue(OverlayMaskBrushProperty, value);
|
||||
}
|
||||
|
||||
private Border CreateOverlayMask(bool canCloseOnClick)
|
||||
{
|
||||
Border border = new()
|
||||
{
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch,
|
||||
VerticalAlignment = VerticalAlignment.Stretch,
|
||||
Width = this.Bounds.Width,
|
||||
Height = this.Bounds.Height,
|
||||
[!BackgroundProperty] = this[!OverlayMaskBrushProperty],
|
||||
IsVisible = true,
|
||||
};
|
||||
if (canCloseOnClick)
|
||||
{
|
||||
border.AddHandler(PointerReleasedEvent, ClickBorderToCloseDialog);
|
||||
}
|
||||
return border;
|
||||
}
|
||||
|
||||
private void ClickBorderToCloseDialog(object sender, PointerReleasedEventArgs e)
|
||||
{
|
||||
if (sender is Border border)
|
||||
{
|
||||
int i = _masks.IndexOf(border);
|
||||
if (_modalDialogs[i] is { } dialog)
|
||||
{
|
||||
dialog.Close();
|
||||
border.RemoveHandler(PointerReleasedEvent, ClickBorderToCloseDialog);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected sealed override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
OverlayDialogManager.RegisterHost(this, HostId);
|
||||
}
|
||||
|
||||
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
OverlayDialogManager.UnregisterHost(HostId);
|
||||
base.OnDetachedFromVisualTree(e);
|
||||
}
|
||||
|
||||
|
||||
protected sealed override void OnSizeChanged(SizeChangedEventArgs e)
|
||||
{
|
||||
base.OnSizeChanged(e);
|
||||
for (int i = 0; i < _masks.Count; i++)
|
||||
{
|
||||
_masks[i].Width = this.Bounds.Width;
|
||||
_masks[i].Height = this.Bounds.Height;
|
||||
}
|
||||
|
||||
var oldSize = e.PreviousSize;
|
||||
var newSize = e.NewSize;
|
||||
foreach (var dialog in _dialogs)
|
||||
{
|
||||
if (dialog is DialogControlBase c)
|
||||
{
|
||||
ResetDialogPosition(c, oldSize, newSize);
|
||||
}
|
||||
}
|
||||
foreach (var modalDialog in _modalDialogs)
|
||||
{
|
||||
if (modalDialog is DialogControlBase c)
|
||||
{
|
||||
ResetDialogPosition(c, oldSize, newSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetZIndices()
|
||||
{
|
||||
int index = 0;
|
||||
for ( int i = 0; i< _dialogs.Count; i++)
|
||||
{
|
||||
_dialogs[i].ZIndex = index;
|
||||
index++;
|
||||
}
|
||||
for(int i = 0; i< _masks.Count; i++)
|
||||
{
|
||||
_masks[i].ZIndex = index;
|
||||
index++;
|
||||
(_modalDialogs[i] as Control)!.ZIndex = index;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
internal IDataTemplate? GetDataTemplate(object? o)
|
||||
{
|
||||
if (o is null) return null;
|
||||
IDataTemplate? result = null;
|
||||
var templates = this.DialogDataTemplates;
|
||||
result = templates.FirstOrDefault(a => a.Match(o));
|
||||
if (result != null) return result;
|
||||
var keys = this.Resources.Keys;
|
||||
foreach (var key in keys)
|
||||
{
|
||||
if (Resources.TryGetValue(key, out var value) && value is IDataTemplate t)
|
||||
{
|
||||
result = t;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,29 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class CustomDrawerControl: DrawerControlBase
|
||||
{
|
||||
internal bool IsCloseButtonVisible { get; set; }
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
if (_closeButton is not null)
|
||||
{
|
||||
_closeButton.IsVisible = IsCloseButtonVisible;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (DataContext is IDialogContext context)
|
||||
{
|
||||
context.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
OnElementClosing(this, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,13 @@ using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Threading;
|
||||
using Ursa.Common;
|
||||
using Ursa.Controls.OverlayShared;
|
||||
using Ursa.EventArgs;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
[TemplatePart(PART_CloseButton, typeof(Button))]
|
||||
public abstract class DrawerControlBase: ContentControl
|
||||
public abstract class DrawerControlBase: OverlayFeedbackElement
|
||||
{
|
||||
public const string PART_CloseButton = "PART_CloseButton";
|
||||
|
||||
@@ -47,15 +48,6 @@ public const string PART_CloseButton = "PART_CloseButton";
|
||||
set => SetValue(IsCloseButtonVisibleProperty, value);
|
||||
}
|
||||
|
||||
public static readonly RoutedEvent<ResultEventArgs> ClosedEvent = RoutedEvent.Register<DrawerControlBase, ResultEventArgs>(
|
||||
nameof(Closed), RoutingStrategies.Bubble);
|
||||
|
||||
public event EventHandler<ResultEventArgs>? Closed
|
||||
{
|
||||
add => AddHandler(ClosedEvent, value);
|
||||
remove => RemoveHandler(ClosedEvent, value);
|
||||
}
|
||||
|
||||
static DrawerControlBase()
|
||||
{
|
||||
DataContextProperty.Changed.AddClassHandler<DrawerControlBase, object?>((o, e) => o.OnDataContextChange(e));
|
||||
@@ -86,33 +78,9 @@ public const string PART_CloseButton = "PART_CloseButton";
|
||||
RaiseEvent(new ResultEventArgs(ClosedEvent, e));
|
||||
}
|
||||
|
||||
private void OnCloseButtonClick(object sender, RoutedEventArgs e) => CloseDrawer();
|
||||
private void OnCloseButtonClick(object sender, RoutedEventArgs e) => Close();
|
||||
|
||||
public Task<T?> ShowAsync<T>(CancellationToken? token = default)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<T?>();
|
||||
token?.Register(() =>
|
||||
{
|
||||
Dispatcher.UIThread.Invoke(CloseDrawer);
|
||||
});
|
||||
|
||||
void OnCloseHandler(object sender, ResultEventArgs args)
|
||||
{
|
||||
if (args.Result is T result)
|
||||
{
|
||||
tcs.SetResult(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
tcs.SetResult(default(T));
|
||||
}
|
||||
RemoveHandler(ClosedEvent, OnCloseHandler);
|
||||
}
|
||||
AddHandler(ClosedEvent, OnCloseHandler);
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
internal virtual void CloseDrawer()
|
||||
public override void Close()
|
||||
{
|
||||
if (DataContext is IDialogContext context)
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@ using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Styling;
|
||||
@@ -12,12 +13,11 @@ namespace Ursa.Controls;
|
||||
/// <summary>
|
||||
/// The messageBox used to display in OverlayDialogHost.
|
||||
/// </summary>
|
||||
[TemplatePart(PART_CloseButton, typeof(Button))]
|
||||
[TemplatePart(PART_NoButton, typeof(Button))]
|
||||
[TemplatePart(PART_OKButton, typeof(Button))]
|
||||
[TemplatePart(PART_CancelButton, typeof(Button))]
|
||||
[TemplatePart(PART_YesButton, typeof(Button))]
|
||||
public class MessageBoxControl: DialogControl
|
||||
public class MessageBoxControl: DialogControlBase
|
||||
{
|
||||
public const string PART_YesButton = "PART_YesButton";
|
||||
public const string PART_NoButton = "PART_NoButton";
|
||||
@@ -80,19 +80,19 @@ public class MessageBoxControl: DialogControl
|
||||
{
|
||||
if (button == _okButton)
|
||||
{
|
||||
OnDialogControlClosing(this, MessageBoxResult.OK);
|
||||
OnElementClosing(this, MessageBoxResult.OK);
|
||||
}
|
||||
else if (button == _cancelButton)
|
||||
{
|
||||
OnDialogControlClosing(this, MessageBoxResult.Cancel);
|
||||
OnElementClosing(this, MessageBoxResult.Cancel);
|
||||
}
|
||||
else if (button == _yesButton)
|
||||
{
|
||||
OnDialogControlClosing(this, MessageBoxResult.Yes);
|
||||
OnElementClosing(this, MessageBoxResult.Yes);
|
||||
}
|
||||
else if (button == _noButton)
|
||||
{
|
||||
OnDialogControlClosing(this, MessageBoxResult.No);
|
||||
OnElementClosing(this, MessageBoxResult.No);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -128,12 +128,7 @@ public class MessageBoxControl: DialogControl
|
||||
}
|
||||
}
|
||||
|
||||
private void SetVisibility(Button? button, bool visible)
|
||||
{
|
||||
if (button is not null) button.IsVisible = visible;
|
||||
}
|
||||
|
||||
internal override void CloseDialog()
|
||||
public override void Close()
|
||||
{
|
||||
MessageBoxResult result = Buttons switch
|
||||
{
|
||||
@@ -143,6 +138,6 @@ public class MessageBoxControl: DialogControl
|
||||
MessageBoxButton.YesNoCancel => MessageBoxResult.Cancel,
|
||||
_ => MessageBoxResult.None
|
||||
};
|
||||
OnDialogControlClosing(this, result);
|
||||
OnElementClosing(this, result);
|
||||
}
|
||||
}
|
||||
78
src/Ursa/Controls/OverlayShared/OverlayFeedbackElement.cs
Normal file
78
src/Ursa/Controls/OverlayShared/OverlayFeedbackElement.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Threading;
|
||||
using Ursa.EventArgs;
|
||||
|
||||
namespace Ursa.Controls.OverlayShared;
|
||||
|
||||
public abstract class OverlayFeedbackElement: ContentControl
|
||||
{
|
||||
static OverlayFeedbackElement()
|
||||
{
|
||||
DataContextProperty.Changed.AddClassHandler<CustomDialogControl, object?>((o, e) => o.OnDataContextChange(e));
|
||||
}
|
||||
|
||||
public static readonly RoutedEvent<ResultEventArgs> ClosedEvent = RoutedEvent.Register<DrawerControlBase, ResultEventArgs>(
|
||||
nameof(Closed), RoutingStrategies.Bubble);
|
||||
|
||||
public event EventHandler<ResultEventArgs> Closed
|
||||
{
|
||||
add => AddHandler(ClosedEvent, value);
|
||||
remove => RemoveHandler(ClosedEvent, value);
|
||||
}
|
||||
|
||||
private void OnDataContextChange(AvaloniaPropertyChangedEventArgs<object?> args)
|
||||
{
|
||||
if (args.OldValue.Value is IDialogContext oldContext)
|
||||
{
|
||||
oldContext.RequestClose -= OnContextRequestClose;
|
||||
}
|
||||
if (args.NewValue.Value is IDialogContext newContext)
|
||||
{
|
||||
newContext.RequestClose += OnContextRequestClose;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnElementClosing(object sender, object? args)
|
||||
{
|
||||
RaiseEvent(new ResultEventArgs(ClosedEvent, args));
|
||||
}
|
||||
|
||||
private void OnContextRequestClose(object sender, object? args)
|
||||
{
|
||||
RaiseEvent(new ResultEventArgs(ClosedEvent, args));
|
||||
}
|
||||
|
||||
public Task<T?> ShowAsync<T>(CancellationToken? token = default)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<T?>();
|
||||
token?.Register(() =>
|
||||
{
|
||||
Dispatcher.UIThread.Invoke(Close);
|
||||
});
|
||||
|
||||
void OnCloseHandler(object sender, ResultEventArgs? args)
|
||||
{
|
||||
if (args?.Result is T result)
|
||||
{
|
||||
tcs.SetResult(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
tcs.SetResult(default);
|
||||
}
|
||||
RemoveHandler(ClosedEvent, OnCloseHandler);
|
||||
}
|
||||
|
||||
AddHandler(ClosedEvent, OnCloseHandler);
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
protected static void SetVisibility(Button? button, bool visible)
|
||||
{
|
||||
if (button is not null) button.IsVisible = visible;
|
||||
}
|
||||
|
||||
public abstract void Close();
|
||||
}
|
||||
Reference in New Issue
Block a user