Skip to content

Commit

Permalink
优化 PinCode 类
Browse files Browse the repository at this point in the history
如果粘贴内容有效,则执行 `CompleteCommand` 并触发 `CompleteEvent` 事件。
  • Loading branch information
Soar360 committed Dec 5, 2024
1 parent 9001322 commit eeff2d7
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/Ursa/Controls/PinCode/PinCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICommand?> CompleteCommandProperty = AvaloniaProperty.Register<PinCode, ICommand?>(
nameof(CompleteCommand));

Expand Down Expand Up @@ -57,18 +57,18 @@ public PinCodeMode Mode

public static readonly DirectProperty<PinCode, IList<string>> DigitsProperty = AvaloniaProperty.RegisterDirect<PinCode, IList<string>>(
nameof(Digits), o => o.Digits);

private IList<string> _digits = [];
public IList<string> Digits
{
get => _digits;
private set => SetAndRaise(DigitsProperty, ref _digits, value);
}

public static readonly RoutedEvent<PinCodeCompleteEventArgs> CompleteEvent =
RoutedEvent.Register<PinCode, PinCodeCompleteEventArgs>(
nameof(Complete), RoutingStrategies.Bubble);

public event EventHandler<PinCodeCompleteEventArgs> Complete
{
add => AddHandler(CompleteEvent, value);
Expand All @@ -79,7 +79,7 @@ static PinCode()
{
CountProperty.Changed.AddClassHandler<PinCode, int>((code, args) => code.OnCountOfDigitChanged(args));
FocusableProperty.OverrideDefaultValue<PinCode>(true);
KeyDownEvent.AddClassHandler<PinCode>((o,e)=>o.OnPreviewKeyDown(e), RoutingStrategies.Tunnel);
KeyDownEvent.AddClassHandler<PinCode>((o, e) => o.OnPreviewKeyDown(e), RoutingStrategies.Tunnel);
}

public PinCode()
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit eeff2d7

Please sign in to comment.