fix: a general improvement of MessageBox key event handling.
This commit is contained in:
@@ -8,57 +8,60 @@ using Irihi.Avalonia.Shared.Helpers;
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// The messageBox used to display in OverlayDialogHost.
|
||||
/// The messageBox used to display in OverlayDialogHost.
|
||||
/// </summary>
|
||||
[TemplatePart(PART_NoButton, typeof(Button))]
|
||||
[TemplatePart(PART_OKButton, typeof(Button))]
|
||||
[TemplatePart(PART_CancelButton, typeof(Button))]
|
||||
[TemplatePart(PART_YesButton, typeof(Button))]
|
||||
public class MessageBoxControl: DialogControlBase
|
||||
public class MessageBoxControl : DialogControlBase
|
||||
{
|
||||
public const string PART_YesButton = "PART_YesButton";
|
||||
public const string PART_NoButton = "PART_NoButton";
|
||||
public const string PART_OKButton = "PART_OKButton";
|
||||
public const string PART_CancelButton = "PART_CancelButton";
|
||||
|
||||
private Button? _yesButton;
|
||||
private Button? _noButton;
|
||||
private Button? _okButton;
|
||||
private Button? _cancelButton;
|
||||
|
||||
|
||||
public static readonly StyledProperty<MessageBoxIcon> MessageIconProperty =
|
||||
AvaloniaProperty.Register<MessageBoxWindow, MessageBoxIcon>(
|
||||
nameof(MessageIcon));
|
||||
|
||||
public static readonly StyledProperty<MessageBoxButton> ButtonsProperty =
|
||||
AvaloniaProperty.Register<MessageBoxControl, MessageBoxButton>(
|
||||
nameof(Buttons));
|
||||
|
||||
public static readonly StyledProperty<string?> TitleProperty =
|
||||
AvaloniaProperty.Register<MessageBoxControl, string?>(
|
||||
nameof(Title));
|
||||
|
||||
private Button? _cancelButton;
|
||||
private Button? _noButton;
|
||||
private Button? _okButton;
|
||||
|
||||
private Button? _yesButton;
|
||||
|
||||
static MessageBoxControl()
|
||||
{
|
||||
ButtonsProperty.Changed.AddClassHandler<MessageBoxControl>((o, _) => { o.SetButtonVisibility(); });
|
||||
}
|
||||
|
||||
public MessageBoxIcon MessageIcon
|
||||
{
|
||||
get => GetValue(MessageIconProperty);
|
||||
set => SetValue(MessageIconProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<MessageBoxButton> ButtonsProperty = AvaloniaProperty.Register<MessageBoxControl, MessageBoxButton>(
|
||||
nameof(Buttons));
|
||||
|
||||
public MessageBoxButton Buttons
|
||||
{
|
||||
get => GetValue(ButtonsProperty);
|
||||
set => SetValue(ButtonsProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<MessageBoxControl, string?>(
|
||||
nameof(Title));
|
||||
|
||||
public string? Title
|
||||
{
|
||||
get => GetValue(TitleProperty);
|
||||
set => SetValue(TitleProperty, value);
|
||||
}
|
||||
|
||||
static MessageBoxControl()
|
||||
{
|
||||
ButtonsProperty.Changed.AddClassHandler<MessageBoxControl>((o, _) => { o.SetButtonVisibility(); });
|
||||
}
|
||||
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
@@ -71,55 +74,66 @@ public class MessageBoxControl: DialogControlBase
|
||||
SetButtonVisibility();
|
||||
}
|
||||
|
||||
protected override void OnLoaded(RoutedEventArgs e)
|
||||
{
|
||||
base.OnLoaded(e);
|
||||
var defaultButton = Buttons switch
|
||||
{
|
||||
MessageBoxButton.OK => _okButton,
|
||||
MessageBoxButton.OKCancel => _cancelButton,
|
||||
MessageBoxButton.YesNo => _yesButton,
|
||||
MessageBoxButton.YesNoCancel => _cancelButton,
|
||||
_ => null
|
||||
};
|
||||
defaultButton?.Focus();
|
||||
}
|
||||
|
||||
private void DefaultButtonsClose(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is Button button)
|
||||
if (sender is not Button button) return;
|
||||
var result = button switch
|
||||
{
|
||||
if (button == _okButton)
|
||||
{
|
||||
OnElementClosing(this, MessageBoxResult.OK);
|
||||
}
|
||||
else if (button == _cancelButton)
|
||||
{
|
||||
OnElementClosing(this, MessageBoxResult.Cancel);
|
||||
}
|
||||
else if (button == _yesButton)
|
||||
{
|
||||
OnElementClosing(this, MessageBoxResult.Yes);
|
||||
}
|
||||
else if (button == _noButton)
|
||||
{
|
||||
OnElementClosing(this, MessageBoxResult.No);
|
||||
}
|
||||
}
|
||||
_ when button == _okButton => MessageBoxResult.OK,
|
||||
_ when button == _cancelButton => MessageBoxResult.Cancel,
|
||||
_ when button == _yesButton => MessageBoxResult.Yes,
|
||||
_ when button == _noButton => MessageBoxResult.No,
|
||||
_ => MessageBoxResult.None
|
||||
};
|
||||
OnElementClosing(this, result);
|
||||
}
|
||||
|
||||
|
||||
private void SetButtonVisibility()
|
||||
{
|
||||
var closeButtonVisible = Buttons != MessageBoxButton.YesNo;
|
||||
IsVisibleProperty.SetValue(closeButtonVisible, _closeButton);
|
||||
switch (Buttons)
|
||||
{
|
||||
case MessageBoxButton.OK:
|
||||
Button.IsVisibleProperty.SetValue(true, _okButton);
|
||||
Button.IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
||||
IsVisibleProperty.SetValue(true, _okButton);
|
||||
IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
||||
Button.IsDefaultProperty.SetValue(true, _okButton);
|
||||
Button.IsDefaultProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
||||
break;
|
||||
case MessageBoxButton.OKCancel:
|
||||
Button.IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
|
||||
Button.IsVisibleProperty.SetValue(false, _yesButton, _noButton);
|
||||
IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
|
||||
IsVisibleProperty.SetValue(false, _yesButton, _noButton);
|
||||
Button.IsDefaultProperty.SetValue(true, _okButton);
|
||||
Button.IsDefaultProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
||||
break;
|
||||
case MessageBoxButton.YesNo:
|
||||
Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
|
||||
Button.IsVisibleProperty.SetValue(true, _yesButton, _noButton);
|
||||
IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
|
||||
IsVisibleProperty.SetValue(true, _yesButton, _noButton);
|
||||
break;
|
||||
case MessageBoxButton.YesNoCancel:
|
||||
Button.IsVisibleProperty.SetValue(false, _okButton);
|
||||
Button.IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
|
||||
IsVisibleProperty.SetValue(false, _okButton);
|
||||
IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
MessageBoxResult result = Buttons switch
|
||||
var result = Buttons switch
|
||||
{
|
||||
MessageBoxButton.OK => MessageBoxResult.OK,
|
||||
MessageBoxButton.OKCancel => MessageBoxResult.Cancel,
|
||||
|
||||
@@ -21,28 +21,29 @@ public class MessageBoxWindow(MessageBoxButton buttons) : Window
|
||||
public const string PART_OKButton = "PART_OKButton";
|
||||
public const string PART_CancelButton = "PART_CancelButton";
|
||||
|
||||
private Button? _closeButton;
|
||||
private Button? _yesButton;
|
||||
private Button? _noButton;
|
||||
private Button? _okButton;
|
||||
private Button? _cancelButton;
|
||||
|
||||
protected override Type StyleKeyOverride => typeof(MessageBoxWindow);
|
||||
|
||||
public static readonly StyledProperty<MessageBoxIcon> MessageIconProperty =
|
||||
AvaloniaProperty.Register<MessageBoxWindow, MessageBoxIcon>(
|
||||
nameof(MessageIcon));
|
||||
|
||||
private Button? _closeButton;
|
||||
|
||||
private Button? _cancelButton;
|
||||
private Button? _noButton;
|
||||
private Button? _okButton;
|
||||
private Button? _yesButton;
|
||||
|
||||
public MessageBoxWindow() : this(MessageBoxButton.OK)
|
||||
{
|
||||
}
|
||||
|
||||
protected override Type StyleKeyOverride => typeof(MessageBoxWindow);
|
||||
|
||||
public MessageBoxIcon MessageIcon
|
||||
{
|
||||
get => GetValue(MessageIconProperty);
|
||||
set => SetValue(MessageIconProperty, value);
|
||||
}
|
||||
|
||||
public MessageBoxWindow() : this(MessageBoxButton.OK)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
@@ -60,33 +61,32 @@ public class MessageBoxWindow(MessageBoxButton buttons) : Window
|
||||
|
||||
private void SetButtonVisibility()
|
||||
{
|
||||
var closeButtonVisible = buttons != MessageBoxButton.YesNo;
|
||||
IsVisibleProperty.SetValue(closeButtonVisible, _closeButton);
|
||||
switch (buttons)
|
||||
{
|
||||
case MessageBoxButton.OK:
|
||||
Button.IsVisibleProperty.SetValue(true, _okButton);
|
||||
Button.IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
||||
IsVisibleProperty.SetValue(true, _okButton);
|
||||
IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
||||
break;
|
||||
case MessageBoxButton.OKCancel:
|
||||
Button.IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
|
||||
Button.IsVisibleProperty.SetValue(false, _yesButton, _noButton);
|
||||
IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
|
||||
IsVisibleProperty.SetValue(false, _yesButton, _noButton);
|
||||
break;
|
||||
case MessageBoxButton.YesNo:
|
||||
Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
|
||||
Button.IsVisibleProperty.SetValue(true, _yesButton, _noButton);
|
||||
IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
|
||||
IsVisibleProperty.SetValue(true, _yesButton, _noButton);
|
||||
break;
|
||||
case MessageBoxButton.YesNoCancel:
|
||||
Button.IsVisibleProperty.SetValue(false, _okButton);
|
||||
Button.IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
|
||||
IsVisibleProperty.SetValue(false, _okButton);
|
||||
IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCloseButtonClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
if (buttons == MessageBoxButton.OK)
|
||||
{
|
||||
Close(MessageBoxResult.OK);
|
||||
}
|
||||
if (buttons == MessageBoxButton.OK) Close(MessageBoxResult.OK);
|
||||
|
||||
Close(MessageBoxResult.Cancel);
|
||||
}
|
||||
@@ -94,29 +94,29 @@ public class MessageBoxWindow(MessageBoxButton buttons) : Window
|
||||
private void OnDefaultButtonClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
if (Equals(sender, _okButton))
|
||||
{
|
||||
Close(MessageBoxResult.OK);
|
||||
}
|
||||
else if (Equals(sender, _cancelButton))
|
||||
{
|
||||
Close(MessageBoxResult.Cancel);
|
||||
}
|
||||
else if (Equals(sender, _yesButton))
|
||||
{
|
||||
Close(MessageBoxResult.Yes);
|
||||
}
|
||||
else if (Equals(sender, _noButton))
|
||||
{
|
||||
Close(MessageBoxResult.No);
|
||||
}
|
||||
else if (Equals(sender, _noButton)) Close(MessageBoxResult.No);
|
||||
}
|
||||
|
||||
protected override void OnKeyUp(KeyEventArgs e)
|
||||
{
|
||||
base.OnKeyUp(e);
|
||||
if (e.Key == Key.Escape && buttons == MessageBoxButton.OK)
|
||||
if (e.Key != Key.Escape) return;
|
||||
switch (buttons)
|
||||
{
|
||||
Close(MessageBoxResult.OK);
|
||||
case MessageBoxButton.OK:
|
||||
Close(MessageBoxResult.OK);
|
||||
break;
|
||||
case MessageBoxButton.OKCancel:
|
||||
Close(MessageBoxResult.Cancel);
|
||||
break;
|
||||
case MessageBoxButton.YesNoCancel:
|
||||
Close(MessageBoxResult.Cancel);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,4 +124,19 @@ public class MessageBoxWindow(MessageBoxButton buttons) : Window
|
||||
{
|
||||
BeginMoveDrag(e);
|
||||
}
|
||||
|
||||
protected override void OnLoaded(RoutedEventArgs e)
|
||||
{
|
||||
base.OnLoaded(e);
|
||||
var defaultButton = buttons switch
|
||||
{
|
||||
MessageBoxButton.OK => _okButton,
|
||||
MessageBoxButton.OKCancel => _cancelButton,
|
||||
MessageBoxButton.YesNo => _yesButton,
|
||||
MessageBoxButton.YesNoCancel => _cancelButton,
|
||||
_ => null
|
||||
};
|
||||
Button.IsDefaultProperty.SetValue(true, defaultButton);
|
||||
defaultButton?.Focus();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user