From 34824dde42f743d4fa022bacb2a74d7e175b7ec9 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 6 Jun 2023 20:17:30 +0200 Subject: [PATCH] HotkeyBox - Focus fixes --- .../Controls/HotkeyBox.axaml.cs | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs b/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs index d5d02c63e..1697977a8 100644 --- a/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs +++ b/src/Artemis.UI.Shared/Controls/HotkeyBox.axaml.cs @@ -41,7 +41,7 @@ public partial class HotkeyBox : UserControl { _inputService.KeyboardKeyDown += InputServiceOnKeyboardKeyDown; _inputService.KeyboardKeyUp += InputServiceOnKeyboardKeyUp; - + base.OnGotFocus(e); } @@ -50,10 +50,10 @@ public partial class HotkeyBox : UserControl { _inputService.KeyboardKeyDown -= InputServiceOnKeyboardKeyDown; _inputService.KeyboardKeyUp -= InputServiceOnKeyboardKeyUp; - + base.OnLostFocus(e); } - + private void OnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e) { if (e.Property == HotkeyProperty) @@ -68,7 +68,7 @@ public partial class HotkeyBox : UserControl Hotkey ??= new Hotkey(); Hotkey.Key = e.Key; Hotkey.Modifiers = e.Modifiers; - + Dispatcher.UIThread.Post(() => { UpdateDisplayTextBox(); @@ -79,7 +79,19 @@ public partial class HotkeyBox : UserControl private void InputServiceOnKeyboardKeyUp(object? sender, ArtemisKeyboardKeyEventArgs e) { if (e.Modifiers == KeyboardModifierKey.None) - Dispatcher.UIThread.Post(() => this.FindAncestorOfType()?.Focus()); + Dispatcher.UIThread.Post(ClearFocus); + } + + private void ClearFocus() + { + InputElement? element = this.FindAncestorOfType(); + if (element == null) + return; + + bool wasFocusable = element.Focusable; + element.Focusable = true; + element.Focus(); + element.Focusable = wasFocusable; } private void UpdateDisplayTextBox() @@ -97,7 +109,7 @@ public partial class HotkeyBox : UserControl private void Button_OnClick(object? sender, RoutedEventArgs e) { Hotkey = null; - this.FindAncestorOfType()?.Focus(); + ClearFocus(); UpdateDisplayTextBox(); }