feat: implement default button for dialog control.
This commit is contained in:
@@ -4,22 +4,36 @@ using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Ursa.Common;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
[TemplatePart(PART_CloseButton, typeof(Button))]
|
||||
[TemplatePart(PART_OKButton, typeof(Button))]
|
||||
[TemplatePart(PART_CancelButton, typeof(Button))]
|
||||
[TemplatePart(PART_YesButton, typeof(Button))]
|
||||
[TemplatePart(PART_NoButton, typeof(Button))]
|
||||
[TemplatePart(PART_TitleArea, typeof(Panel))]
|
||||
[PseudoClasses(PC_ExtendClientArea)]
|
||||
public class DialogControl: ContentControl
|
||||
{
|
||||
public const string PART_CloseButton = "PART_CloseButton";
|
||||
public const string PART_OKButton = "PART_OKButton";
|
||||
public const string PART_CancelButton = "PART_CancelButton";
|
||||
public const string PART_YesButton = "PART_YesButton";
|
||||
public const string PART_NoButton = "PART_NoButton";
|
||||
|
||||
public const string PART_TitleArea = "PART_TitleArea";
|
||||
public const string PC_ExtendClientArea = ":extend";
|
||||
public const string PC_Modal = ":modal";
|
||||
|
||||
private Button? _closeButton;
|
||||
private Button? _okButton;
|
||||
private Button? _cancelButton;
|
||||
private Button? _yesButton;
|
||||
private Button? _noButton;
|
||||
private Panel? _titleArea;
|
||||
public event EventHandler<object?>? OnClose;
|
||||
public event EventHandler<object?>? OnDialogControlClose;
|
||||
public event EventHandler<DialogLayerChangeEventArgs>? OnLayerChange;
|
||||
|
||||
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<DialogControl, string?>(
|
||||
@@ -40,6 +54,8 @@ public class DialogControl: ContentControl
|
||||
set => SetValue(ExtendToClientAreaProperty, value);
|
||||
}
|
||||
|
||||
internal DialogButton Buttons { get; set; }
|
||||
|
||||
static DialogControl()
|
||||
{
|
||||
DataContextProperty.Changed.AddClassHandler<DialogControl, object?>((o, e) => o.OnDataContextChange(e));
|
||||
@@ -50,12 +66,12 @@ public class DialogControl: ContentControl
|
||||
{
|
||||
if (args.OldValue.Value is IDialogContext oldContext)
|
||||
{
|
||||
oldContext.Closed-= Close;
|
||||
oldContext.Closed-= CloseFromContext;
|
||||
}
|
||||
|
||||
if (args.NewValue.Value is IDialogContext newContext)
|
||||
{
|
||||
newContext.Closed += Close;
|
||||
newContext.Closed += CloseFromContext;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,24 +79,83 @@ public class DialogControl: ContentControl
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
if (_closeButton != null)
|
||||
{
|
||||
_closeButton.Click -= Close;
|
||||
}
|
||||
UnRegisterClickEvent(Close, _closeButton);
|
||||
UnRegisterClickEvent(DefaultButtonsClose, _okButton, _cancelButton, _yesButton, _noButton);
|
||||
_titleArea?.RemoveHandler(PointerMovedEvent, OnTitlePointerMove);
|
||||
_titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed);
|
||||
|
||||
_closeButton = e.NameScope.Find<Button>(PART_CloseButton);
|
||||
_titleArea = e.NameScope.Find<Panel>(PART_TitleArea);
|
||||
if (_closeButton is not null)
|
||||
{
|
||||
_closeButton.Click += Close;
|
||||
}
|
||||
_okButton = e.NameScope.Find<Button>(PART_OKButton);
|
||||
_cancelButton = e.NameScope.Find<Button>(PART_CancelButton);
|
||||
_yesButton = e.NameScope.Find<Button>(PART_YesButton);
|
||||
_noButton = e.NameScope.Find<Button>(PART_NoButton);
|
||||
|
||||
_titleArea?.AddHandler(PointerMovedEvent, OnTitlePointerMove, RoutingStrategies.Bubble);
|
||||
_titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble);
|
||||
|
||||
RegisterClickEvent(Close, _closeButton);
|
||||
RegisterClickEvent(DefaultButtonsClose, _yesButton, _noButton, _okButton, _cancelButton);
|
||||
SetButtonVisibility();
|
||||
}
|
||||
|
||||
private void UnRegisterClickEvent(EventHandler<RoutedEventArgs> action, params Button?[] buttons)
|
||||
{
|
||||
foreach (var button in buttons)
|
||||
{
|
||||
if(button is not null) button.Click -= action;
|
||||
}
|
||||
}
|
||||
|
||||
private void RegisterClickEvent(EventHandler<RoutedEventArgs> action, params Button?[] buttons)
|
||||
{
|
||||
foreach (var button in buttons)
|
||||
{
|
||||
if(button is not null) button.Click += action;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetButtonVisibility()
|
||||
{
|
||||
switch (Buttons)
|
||||
{
|
||||
case DialogButton.None:
|
||||
SetVisibility(_okButton, false);
|
||||
SetVisibility(_cancelButton, false);
|
||||
SetVisibility(_yesButton, false);
|
||||
SetVisibility(_noButton, false);
|
||||
break;
|
||||
case DialogButton.OK:
|
||||
SetVisibility(_okButton, true);
|
||||
SetVisibility(_cancelButton, false);
|
||||
SetVisibility(_yesButton, false);
|
||||
SetVisibility(_noButton, false);
|
||||
break;
|
||||
case DialogButton.OKCancel:
|
||||
SetVisibility(_okButton, true);
|
||||
SetVisibility(_cancelButton, true);
|
||||
SetVisibility(_yesButton, false);
|
||||
SetVisibility(_noButton, false);
|
||||
break;
|
||||
case DialogButton.YesNo:
|
||||
SetVisibility(_okButton, false);
|
||||
SetVisibility(_cancelButton, false);
|
||||
SetVisibility(_yesButton, true);
|
||||
SetVisibility(_noButton, true);
|
||||
break;
|
||||
case DialogButton.YesNoCancel:
|
||||
SetVisibility(_okButton, false);
|
||||
SetVisibility(_cancelButton, true);
|
||||
SetVisibility(_yesButton, true);
|
||||
SetVisibility(_noButton, true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetVisibility(Button? button, bool visible)
|
||||
{
|
||||
if (button is not null) button.IsVisible = visible;
|
||||
}
|
||||
|
||||
private void OnTitlePointerPressed(object sender, PointerPressedEventArgs e)
|
||||
{
|
||||
e.Source = this;
|
||||
@@ -101,29 +176,60 @@ public class DialogControl: ContentControl
|
||||
if (args is T result)
|
||||
{
|
||||
tcs.SetResult(result);
|
||||
OnClose-= OnCloseHandler;
|
||||
OnDialogControlClose-= OnCloseHandler;
|
||||
}
|
||||
else
|
||||
{
|
||||
tcs.SetResult(default(T));
|
||||
OnClose-= OnCloseHandler;
|
||||
OnDialogControlClose-= OnCloseHandler;
|
||||
}
|
||||
}
|
||||
|
||||
this.OnClose += OnCloseHandler;
|
||||
this.OnDialogControlClose += OnCloseHandler;
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
private void Close(object sender, object args)
|
||||
|
||||
private void Close(object sender, RoutedEventArgs args)
|
||||
{
|
||||
if (this.DataContext is IDialogContext context)
|
||||
{
|
||||
OnClose?.Invoke(this, Equals(sender, _closeButton) ? context.DefaultCloseResult : args);
|
||||
OnDialogControlClose?.Invoke(this, context.DefaultCloseResult);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
OnClose?.Invoke(this, null);
|
||||
OnDialogControlClose?.Invoke(this, null);
|
||||
}
|
||||
}
|
||||
|
||||
private void CloseFromContext(object sender, object? args)
|
||||
{
|
||||
if (this.DataContext is IDialogContext context)
|
||||
{
|
||||
OnDialogControlClose?.Invoke(this, args);
|
||||
}
|
||||
}
|
||||
|
||||
private void DefaultButtonsClose(object sender, RoutedEventArgs args)
|
||||
{
|
||||
if (sender is Button button)
|
||||
{
|
||||
if (button == _okButton)
|
||||
{
|
||||
OnDialogControlClose?.Invoke(this, DialogResult.OK);
|
||||
}
|
||||
else if (button == _cancelButton)
|
||||
{
|
||||
OnDialogControlClose?.Invoke(this, DialogResult.Cancel);
|
||||
}
|
||||
else if (button == _yesButton)
|
||||
{
|
||||
OnDialogControlClose?.Invoke(this, DialogResult.Yes);
|
||||
}
|
||||
else if (button == _noButton)
|
||||
{
|
||||
OnDialogControlClose?.Invoke(this, DialogResult.No);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user