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;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The messageBox used to display in OverlayDialogHost.
|
/// The messageBox used to display in OverlayDialogHost.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[TemplatePart(PART_NoButton, typeof(Button))]
|
[TemplatePart(PART_NoButton, typeof(Button))]
|
||||||
[TemplatePart(PART_OKButton, typeof(Button))]
|
[TemplatePart(PART_OKButton, typeof(Button))]
|
||||||
[TemplatePart(PART_CancelButton, typeof(Button))]
|
[TemplatePart(PART_CancelButton, typeof(Button))]
|
||||||
[TemplatePart(PART_YesButton, 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_YesButton = "PART_YesButton";
|
||||||
public const string PART_NoButton = "PART_NoButton";
|
public const string PART_NoButton = "PART_NoButton";
|
||||||
public const string PART_OKButton = "PART_OKButton";
|
public const string PART_OKButton = "PART_OKButton";
|
||||||
public const string PART_CancelButton = "PART_CancelButton";
|
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 =
|
public static readonly StyledProperty<MessageBoxIcon> MessageIconProperty =
|
||||||
AvaloniaProperty.Register<MessageBoxWindow, MessageBoxIcon>(
|
AvaloniaProperty.Register<MessageBoxWindow, MessageBoxIcon>(
|
||||||
nameof(MessageIcon));
|
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
|
public MessageBoxIcon MessageIcon
|
||||||
{
|
{
|
||||||
get => GetValue(MessageIconProperty);
|
get => GetValue(MessageIconProperty);
|
||||||
set => SetValue(MessageIconProperty, value);
|
set => SetValue(MessageIconProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<MessageBoxButton> ButtonsProperty = AvaloniaProperty.Register<MessageBoxControl, MessageBoxButton>(
|
|
||||||
nameof(Buttons));
|
|
||||||
|
|
||||||
public MessageBoxButton Buttons
|
public MessageBoxButton Buttons
|
||||||
{
|
{
|
||||||
get => GetValue(ButtonsProperty);
|
get => GetValue(ButtonsProperty);
|
||||||
set => SetValue(ButtonsProperty, value);
|
set => SetValue(ButtonsProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<MessageBoxControl, string?>(
|
|
||||||
nameof(Title));
|
|
||||||
|
|
||||||
public string? Title
|
public string? Title
|
||||||
{
|
{
|
||||||
get => GetValue(TitleProperty);
|
get => GetValue(TitleProperty);
|
||||||
set => SetValue(TitleProperty, value);
|
set => SetValue(TitleProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static MessageBoxControl()
|
|
||||||
{
|
|
||||||
ButtonsProperty.Changed.AddClassHandler<MessageBoxControl>((o, _) => { o.SetButtonVisibility(); });
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnApplyTemplate(e);
|
base.OnApplyTemplate(e);
|
||||||
@@ -71,55 +74,66 @@ public class MessageBoxControl: DialogControlBase
|
|||||||
SetButtonVisibility();
|
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)
|
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)
|
_ when button == _okButton => MessageBoxResult.OK,
|
||||||
{
|
_ when button == _cancelButton => MessageBoxResult.Cancel,
|
||||||
OnElementClosing(this, MessageBoxResult.OK);
|
_ when button == _yesButton => MessageBoxResult.Yes,
|
||||||
}
|
_ when button == _noButton => MessageBoxResult.No,
|
||||||
else if (button == _cancelButton)
|
_ => MessageBoxResult.None
|
||||||
{
|
};
|
||||||
OnElementClosing(this, MessageBoxResult.Cancel);
|
OnElementClosing(this, result);
|
||||||
}
|
|
||||||
else if (button == _yesButton)
|
|
||||||
{
|
|
||||||
OnElementClosing(this, MessageBoxResult.Yes);
|
|
||||||
}
|
|
||||||
else if (button == _noButton)
|
|
||||||
{
|
|
||||||
OnElementClosing(this, MessageBoxResult.No);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetButtonVisibility()
|
private void SetButtonVisibility()
|
||||||
{
|
{
|
||||||
|
var closeButtonVisible = Buttons != MessageBoxButton.YesNo;
|
||||||
|
IsVisibleProperty.SetValue(closeButtonVisible, _closeButton);
|
||||||
switch (Buttons)
|
switch (Buttons)
|
||||||
{
|
{
|
||||||
case MessageBoxButton.OK:
|
case MessageBoxButton.OK:
|
||||||
Button.IsVisibleProperty.SetValue(true, _okButton);
|
IsVisibleProperty.SetValue(true, _okButton);
|
||||||
Button.IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
||||||
|
Button.IsDefaultProperty.SetValue(true, _okButton);
|
||||||
|
Button.IsDefaultProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
||||||
break;
|
break;
|
||||||
case MessageBoxButton.OKCancel:
|
case MessageBoxButton.OKCancel:
|
||||||
Button.IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
|
IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
|
||||||
Button.IsVisibleProperty.SetValue(false, _yesButton, _noButton);
|
IsVisibleProperty.SetValue(false, _yesButton, _noButton);
|
||||||
|
Button.IsDefaultProperty.SetValue(true, _okButton);
|
||||||
|
Button.IsDefaultProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
||||||
break;
|
break;
|
||||||
case MessageBoxButton.YesNo:
|
case MessageBoxButton.YesNo:
|
||||||
Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
|
IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
|
||||||
Button.IsVisibleProperty.SetValue(true, _yesButton, _noButton);
|
IsVisibleProperty.SetValue(true, _yesButton, _noButton);
|
||||||
break;
|
break;
|
||||||
case MessageBoxButton.YesNoCancel:
|
case MessageBoxButton.YesNoCancel:
|
||||||
Button.IsVisibleProperty.SetValue(false, _okButton);
|
IsVisibleProperty.SetValue(false, _okButton);
|
||||||
Button.IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
|
IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Close()
|
public override void Close()
|
||||||
{
|
{
|
||||||
MessageBoxResult result = Buttons switch
|
var result = Buttons switch
|
||||||
{
|
{
|
||||||
MessageBoxButton.OK => MessageBoxResult.OK,
|
MessageBoxButton.OK => MessageBoxResult.OK,
|
||||||
MessageBoxButton.OKCancel => MessageBoxResult.Cancel,
|
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_OKButton = "PART_OKButton";
|
||||||
public const string PART_CancelButton = "PART_CancelButton";
|
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 =
|
public static readonly StyledProperty<MessageBoxIcon> MessageIconProperty =
|
||||||
AvaloniaProperty.Register<MessageBoxWindow, MessageBoxIcon>(
|
AvaloniaProperty.Register<MessageBoxWindow, MessageBoxIcon>(
|
||||||
nameof(MessageIcon));
|
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
|
public MessageBoxIcon MessageIcon
|
||||||
{
|
{
|
||||||
get => GetValue(MessageIconProperty);
|
get => GetValue(MessageIconProperty);
|
||||||
set => SetValue(MessageIconProperty, value);
|
set => SetValue(MessageIconProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MessageBoxWindow() : this(MessageBoxButton.OK)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnApplyTemplate(e);
|
base.OnApplyTemplate(e);
|
||||||
@@ -60,33 +61,32 @@ public class MessageBoxWindow(MessageBoxButton buttons) : Window
|
|||||||
|
|
||||||
private void SetButtonVisibility()
|
private void SetButtonVisibility()
|
||||||
{
|
{
|
||||||
|
var closeButtonVisible = buttons != MessageBoxButton.YesNo;
|
||||||
|
IsVisibleProperty.SetValue(closeButtonVisible, _closeButton);
|
||||||
switch (buttons)
|
switch (buttons)
|
||||||
{
|
{
|
||||||
case MessageBoxButton.OK:
|
case MessageBoxButton.OK:
|
||||||
Button.IsVisibleProperty.SetValue(true, _okButton);
|
IsVisibleProperty.SetValue(true, _okButton);
|
||||||
Button.IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
|
||||||
break;
|
break;
|
||||||
case MessageBoxButton.OKCancel:
|
case MessageBoxButton.OKCancel:
|
||||||
Button.IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
|
IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
|
||||||
Button.IsVisibleProperty.SetValue(false, _yesButton, _noButton);
|
IsVisibleProperty.SetValue(false, _yesButton, _noButton);
|
||||||
break;
|
break;
|
||||||
case MessageBoxButton.YesNo:
|
case MessageBoxButton.YesNo:
|
||||||
Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
|
IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
|
||||||
Button.IsVisibleProperty.SetValue(true, _yesButton, _noButton);
|
IsVisibleProperty.SetValue(true, _yesButton, _noButton);
|
||||||
break;
|
break;
|
||||||
case MessageBoxButton.YesNoCancel:
|
case MessageBoxButton.YesNoCancel:
|
||||||
Button.IsVisibleProperty.SetValue(false, _okButton);
|
IsVisibleProperty.SetValue(false, _okButton);
|
||||||
Button.IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
|
IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnCloseButtonClick(object? sender, RoutedEventArgs e)
|
private void OnCloseButtonClick(object? sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
if (buttons == MessageBoxButton.OK)
|
if (buttons == MessageBoxButton.OK) Close(MessageBoxResult.OK);
|
||||||
{
|
|
||||||
Close(MessageBoxResult.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
Close(MessageBoxResult.Cancel);
|
Close(MessageBoxResult.Cancel);
|
||||||
}
|
}
|
||||||
@@ -94,29 +94,29 @@ public class MessageBoxWindow(MessageBoxButton buttons) : Window
|
|||||||
private void OnDefaultButtonClick(object? sender, RoutedEventArgs e)
|
private void OnDefaultButtonClick(object? sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
if (Equals(sender, _okButton))
|
if (Equals(sender, _okButton))
|
||||||
{
|
|
||||||
Close(MessageBoxResult.OK);
|
Close(MessageBoxResult.OK);
|
||||||
}
|
|
||||||
else if (Equals(sender, _cancelButton))
|
else if (Equals(sender, _cancelButton))
|
||||||
{
|
|
||||||
Close(MessageBoxResult.Cancel);
|
Close(MessageBoxResult.Cancel);
|
||||||
}
|
|
||||||
else if (Equals(sender, _yesButton))
|
else if (Equals(sender, _yesButton))
|
||||||
{
|
|
||||||
Close(MessageBoxResult.Yes);
|
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)
|
protected override void OnKeyUp(KeyEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnKeyUp(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);
|
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