Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化 PinCode 类 #503

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 13 additions & 0 deletions tests/HeadlessTest.Ursa/Controls/PinCodeTests/PasteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Avalonia.Headless.XUnit;
using Avalonia.Input;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.Input;
using Ursa.Controls;

namespace HeadlessTest.Ursa.Controls.PinCodeTests;
Expand All @@ -13,9 +14,11 @@ public class PasteTest
public async void Paste_Should_Insert_Text()
{
var window = new Window();
bool commandInvoked = false;
var pinCode = new PinCode()
{
Count = 4,
CompleteCommand = new RelayCommand(() => commandInvoked = true),
};
window.Content = pinCode;
window.Show();
Expand All @@ -27,15 +30,18 @@ public async void Paste_Should_Insert_Text()
// add await for clipboard processing.
await Task.Delay(1);
Assert.Equal("abcd", string.Join("", pinCode.Digits));
Assert.True(commandInvoked);
}

[AvaloniaFact]
public async void Paste_Should_Insert_Text_When_Text_Is_Shorter()
{
var window = new Window();
bool commandInvoked = false;
var pinCode = new PinCode()
{
Count = 4,
CompleteCommand = new RelayCommand(() => commandInvoked = true),
};
window.Content = pinCode;
window.Show();
Expand All @@ -46,15 +52,18 @@ public async void Paste_Should_Insert_Text_When_Text_Is_Shorter()
window.KeyPressQwerty(PhysicalKey.V, RawInputModifiers.Control);
await Task.Delay(1);
Assert.Equal("abc", string.Join("", pinCode.Digits));
Assert.False(commandInvoked);
}

[AvaloniaFact]
public async void Paste_Should_Insert_Text_When_Text_Is_Longer()
{
var window = new Window();
bool commandInvoked = false;
var pinCode = new PinCode()
{
Count = 4,
CompleteCommand = new RelayCommand(() => commandInvoked = true),
};
window.Content = pinCode;
window.Show();
Expand All @@ -65,16 +74,19 @@ public async void Paste_Should_Insert_Text_When_Text_Is_Longer()
window.KeyPressQwerty(PhysicalKey.V, RawInputModifiers.Control);
await Task.Delay(1);
Assert.Equal("abcd", string.Join("", pinCode.Digits));
Assert.True(commandInvoked);
}

[AvaloniaFact]
public async void Paste_Should_Not_Insert_Text_When_Text_Is_In_Invalid_Mode()
{
var window = new Window();
var commandInvoked = false;
var pinCode = new PinCode()
{
Count = 4,
Mode = PinCodeMode.Digit,
CompleteCommand = new RelayCommand(() => commandInvoked = true),
};
window.Content = pinCode;
window.Show();
Expand All @@ -85,5 +97,6 @@ public async void Paste_Should_Not_Insert_Text_When_Text_Is_In_Invalid_Mode()
window.KeyPressQwerty(PhysicalKey.V, RawInputModifiers.Control);
await Task.Delay(1);
Assert.Equal("", string.Join("", pinCode.Digits));
Assert.False(commandInvoked);
}
}
Loading