From fe52b6a9dca4ef088e51b013a96d5c63c3b9ff59 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Wed, 31 Jul 2024 20:28:12 +0800 Subject: [PATCH] feat: fix left/right navigation. --- src/Ursa/Controls/PinCode/PinCode.cs | 28 +++++++++++++++++-- .../Controls/PinCode/PinCodeCollection.cs | 9 ++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Ursa/Controls/PinCode/PinCode.cs b/src/Ursa/Controls/PinCode/PinCode.cs index 5091044..1f6d2fa 100644 --- a/src/Ursa/Controls/PinCode/PinCode.cs +++ b/src/Ursa/Controls/PinCode/PinCode.cs @@ -79,6 +79,7 @@ public class PinCode: TemplatedControl { CountProperty.Changed.AddClassHandler((code, args) => code.OnCountOfDigitChanged(args)); FocusableProperty.OverrideDefaultValue(true); + KeyDownEvent.AddClassHandler((o,e)=>o.OnPreviewKeyDown(e), RoutingStrategies.Tunnel); } public PinCode() @@ -140,6 +141,7 @@ public class PinCode: TemplatedControl { CompleteCommand?.Execute(Digits); RaiseEvent(new PinCodeCompleteEventArgs(Digits, CompleteEvent)); + _currentIndex--; } } } @@ -157,7 +159,7 @@ public class PinCode: TemplatedControl }; } - protected override void OnKeyDown(KeyEventArgs e) + protected void OnPreviewKeyDown(KeyEventArgs e) { if (e.Key == Key.Tab && e.Source is PinCodeItem) { @@ -168,8 +170,7 @@ public class PinCode: TemplatedControl _currentIndex++; _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); } - base.OnKeyDown(e); - if (e.Key == Key.Back && _currentIndex >= 0) + else if (e.Key == Key.Back && _currentIndex >= 0) { _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); var presenter = _itemsControl?.ContainerFromIndex(_currentIndex) as PinCodeItem; @@ -180,5 +181,26 @@ public class PinCode: TemplatedControl _currentIndex--; _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); } + else if (e.Key is Key.Left or Key.FnLeftArrow) + { + _currentIndex--; + _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); + _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); + } + else if(e.Key is Key.Right or Key.FnRightArrow) + { + _currentIndex++; + _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); + _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); + } + else if (e.Key is Key.Enter or Key.Return) + { + CompleteCommand?.Execute(Digits); + RaiseEvent(new PinCodeCompleteEventArgs(Digits, CompleteEvent)); + } + else + { + base.OnKeyDown(e); + } } } \ No newline at end of file diff --git a/src/Ursa/Controls/PinCode/PinCodeCollection.cs b/src/Ursa/Controls/PinCode/PinCodeCollection.cs index 24810e6..2ea4b07 100644 --- a/src/Ursa/Controls/PinCode/PinCodeCollection.cs +++ b/src/Ursa/Controls/PinCode/PinCodeCollection.cs @@ -17,4 +17,13 @@ public class PinCodeCollection: ItemsControl [InputMethod.IsInputMethodEnabledProperty] = false, }; } + + protected override void OnKeyDown(KeyEventArgs e) + { + if (e.Key is Key.Left or Key.Right or Key.FnLeftArrow or Key.FnRightArrow) + { + e.Handled = true; + } + base.OnKeyDown(e); + } } \ No newline at end of file