From 2dbe9e0745632f06c859e4d0940d21e40981fc19 Mon Sep 17 00:00:00 2001 From: workgroupengineering Date: Wed, 6 Mar 2024 03:36:49 +0100 Subject: [PATCH] feat(SplitButton): Add HotKey (#14638) --- .../SplitButton/SplitButton.cs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/SplitButton/SplitButton.cs b/src/Avalonia.Controls/SplitButton/SplitButton.cs index fb0c9e717f..3a5bc26b43 100644 --- a/src/Avalonia.Controls/SplitButton/SplitButton.cs +++ b/src/Avalonia.Controls/SplitButton/SplitButton.cs @@ -16,7 +16,7 @@ namespace Avalonia.Controls [TemplatePart("PART_PrimaryButton", typeof(Button))] [TemplatePart("PART_SecondaryButton", typeof(Button))] [PseudoClasses(pcFlyoutOpen, pcPressed)] - public class SplitButton : ContentControl, ICommandSource + public class SplitButton : ContentControl, ICommandSource, IClickableControl { internal const string pcChecked = ":checked"; internal const string pcPressed = ":pressed"; @@ -57,8 +57,15 @@ namespace Avalonia.Controls public static readonly StyledProperty FlyoutProperty = Button.FlyoutProperty.AddOwner(); + /// + /// Defines the property. + /// + public static readonly StyledProperty HotKeyProperty = + Button.HotKeyProperty.AddOwner(); + private Button? _primaryButton = null; private Button? _secondaryButton = null; + private KeyGesture? _hotkey = default; private bool _commandCanExecute = true; private bool _isAttachedToLogicalTree = false; @@ -101,6 +108,15 @@ namespace Avalonia.Controls set => SetValue(FlyoutProperty, value); } + /// + /// Gets or sets an associated with this control + /// + public KeyGesture? HotKey + { + get => GetValue(HotKeyProperty); + set => SetValue(HotKeyProperty, value); + } + /// /// Gets a value indicating whether the button is currently checked. /// @@ -232,6 +248,9 @@ namespace Avalonia.Controls { base.OnAttachedToLogicalTree(e); + // Control attached again, set Hotkey to create a hotkey manager for this control + SetCurrentValue(HotKeyProperty, _hotkey); + if (Command != null) { Command.CanExecuteChanged += CanExecuteChanged; @@ -246,6 +265,10 @@ namespace Avalonia.Controls { base.OnDetachedFromLogicalTree(e); + // This will cause the hotkey manager to dispose the observer and the reference to this control + _hotkey = HotKey; + SetCurrentValue(HotKeyProperty, null); + if (Command != null) { Command.CanExecuteChanged -= CanExecuteChanged; @@ -472,5 +495,8 @@ namespace Avalonia.Controls OnFlyoutClosed(); } } + + void IClickableControl.RaiseClick() => + (_primaryButton as IClickableControl)?.RaiseClick(); } }