diff --git a/src/Ursa/Controls/PinCode/PinCode.cs b/src/Ursa/Controls/PinCode/PinCode.cs index a04fb6d2..a1957be5 100644 --- a/src/Ursa/Controls/PinCode/PinCode.cs +++ b/src/Ursa/Controls/PinCode/PinCode.cs @@ -11,12 +11,12 @@ namespace Ursa.Controls; [TemplatePart(PART_ItemsControl, typeof(ItemsControl))] -public class PinCode: TemplatedControl +public class PinCode : TemplatedControl { public const string PART_ItemsControl = "PART_ItemsControl"; private ItemsControl? _itemsControl; private int _currentIndex; - + public static readonly StyledProperty CompleteCommandProperty = AvaloniaProperty.Register( nameof(CompleteCommand)); @@ -57,18 +57,18 @@ public PinCodeMode Mode public static readonly DirectProperty> DigitsProperty = AvaloniaProperty.RegisterDirect>( nameof(Digits), o => o.Digits); - + private IList _digits = []; public IList Digits { get => _digits; private set => SetAndRaise(DigitsProperty, ref _digits, value); } - + public static readonly RoutedEvent CompleteEvent = RoutedEvent.Register( nameof(Complete), RoutingStrategies.Bubble); - + public event EventHandler Complete { add => AddHandler(CompleteEvent, value); @@ -79,7 +79,7 @@ static PinCode() { CountProperty.Changed.AddClassHandler((code, args) => code.OnCountOfDigitChanged(args)); FocusableProperty.OverrideDefaultValue(true); - KeyDownEvent.AddClassHandler((o,e)=>o.OnPreviewKeyDown(e), RoutingStrategies.Tunnel); + KeyDownEvent.AddClassHandler((o, e) => o.OnPreviewKeyDown(e), RoutingStrategies.Tunnel); } public PinCode() @@ -119,7 +119,7 @@ private void OnControlPressed(object? sender, PointerPressedEventArgs e) _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); } - + } e.Handled = true; } @@ -181,6 +181,11 @@ protected async void OnPreviewKeyDown(KeyEventArgs e) presenter.Text = newText[i].ToString(); } } + if (newText.Length == Count) + { + CompleteCommand?.Execute(Digits); + RaiseEvent(new PinCodeCompleteEventArgs(Digits, CompleteEvent)); + } } return; } @@ -210,7 +215,7 @@ protected async void OnPreviewKeyDown(KeyEventArgs e) _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); } - else if(e.Key is Key.Right or Key.FnRightArrow) + else if (e.Key is Key.Right or Key.FnRightArrow) { _currentIndex++; _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1);