From 5ae499274331a2d87f797d96bbcc71fba553a260 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Fri, 29 Nov 2024 21:30:50 +0800 Subject: [PATCH 1/2] fix: fix numeric focus issue. --- demo/Sandbox/Views/MainWindow.axaml | 34 ++++++------------- .../NumericUpDown/NumericUpDownBase.cs | 31 +++++++++++++++++ 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/demo/Sandbox/Views/MainWindow.axaml b/demo/Sandbox/Views/MainWindow.axaml index ba61022e..d1f30fdc 100644 --- a/demo/Sandbox/Views/MainWindow.axaml +++ b/demo/Sandbox/Views/MainWindow.axaml @@ -17,29 +17,17 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + diff --git a/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs b/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs index e4130e77..e290c09a 100644 --- a/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs +++ b/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs @@ -26,6 +26,7 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl, IInnerCon protected ButtonSpinner? _spinner; protected TextBox? _textBox; protected internal Panel? _dragPanel; + private bool _isFocused; private Point? _point; protected internal bool _updateFromTextInput; @@ -149,6 +150,7 @@ public bool ShowButtonSpinner static NumericUpDown() { + FocusableProperty.OverrideDefaultValue(true); NumberFormatProperty.Changed.AddClassHandler((o, e) => o.OnFormatChange(e)); FormatStringProperty.Changed.AddClassHandler((o, e) => o.OnFormatChange(e)); IsReadOnlyProperty.Changed.AddClassHandler((o, args) => o.OnIsReadOnlyChanged(args)); @@ -216,6 +218,35 @@ protected override void OnLostFocus(RoutedEventArgs e) { _dragPanel.IsVisible = true; } + FocusChanged(IsKeyboardFocusWithin); + } + + protected override void OnGotFocus(GotFocusEventArgs e) + { + base.OnGotFocus(e); + FocusChanged(IsKeyboardFocusWithin); + } + + private void FocusChanged(bool hasFocus) + { + // The OnGotFocus & OnLostFocus are asynchronously and cannot + // reliably tell you that have the focus. All they do is let you + // know that the focus changed sometime in the past. To determine + // if you currently have the focus you need to do consult the + // FocusManager. + + bool wasFocused = _isFocused; + _isFocused = hasFocus; + + if (hasFocus) + { + + if (!wasFocused && _textBox != null) + { + _textBox.Focus(); + _textBox.SelectAll(); + } + } } protected override void OnKeyDown(KeyEventArgs e) From d16b55981a344c376dd0ea7532fba1ec996571a4 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Fri, 29 Nov 2024 21:59:17 +0800 Subject: [PATCH 2/2] fix: remove select all behavior. --- src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs b/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs index e290c09a..78268cf3 100644 --- a/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs +++ b/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs @@ -244,7 +244,6 @@ private void FocusChanged(bool hasFocus) if (!wasFocused && _textBox != null) { _textBox.Focus(); - _textBox.SelectAll(); } } }