优化 PinCode 类

如果粘贴内容有效,则执行 `CompleteCommand` 并触发 `CompleteEvent` 事件。
This commit is contained in:
Soar360
2024-12-05 20:11:57 +08:00
parent 052adb5856
commit 0c24c121a8

View File

@@ -11,7 +11,7 @@ using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls; namespace Ursa.Controls;
[TemplatePart(PART_ItemsControl, typeof(ItemsControl))] [TemplatePart(PART_ItemsControl, typeof(ItemsControl))]
public class PinCode: TemplatedControl public class PinCode : TemplatedControl
{ {
public const string PART_ItemsControl = "PART_ItemsControl"; public const string PART_ItemsControl = "PART_ItemsControl";
private ItemsControl? _itemsControl; private ItemsControl? _itemsControl;
@@ -79,7 +79,7 @@ public class PinCode: TemplatedControl
{ {
CountProperty.Changed.AddClassHandler<PinCode, int>((code, args) => code.OnCountOfDigitChanged(args)); CountProperty.Changed.AddClassHandler<PinCode, int>((code, args) => code.OnCountOfDigitChanged(args));
FocusableProperty.OverrideDefaultValue<PinCode>(true); 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() public PinCode()
@@ -181,6 +181,11 @@ public class PinCode: TemplatedControl
presenter.Text = newText[i].ToString(); presenter.Text = newText[i].ToString();
} }
} }
if (newText.Length == Count)
{
CompleteCommand?.Execute(Digits);
RaiseEvent(new PinCodeCompleteEventArgs(Digits, CompleteEvent));
}
} }
return; return;
} }
@@ -210,7 +215,7 @@ public class PinCode: TemplatedControl
_currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1);
_itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); _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++;
_currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1);