feat: support paste.
This commit is contained in:
@@ -159,8 +159,30 @@ public class PinCode: TemplatedControl
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void OnPreviewKeyDown(KeyEventArgs e)
|
protected async void OnPreviewKeyDown(KeyEventArgs e)
|
||||||
{
|
{
|
||||||
|
TextBox b = new TextBox();
|
||||||
|
var pasteKeys = Application.Current?.PlatformSettings.HotkeyConfiguration.Paste;
|
||||||
|
if (pasteKeys?.Any(a => a.Matches(e)) == true)
|
||||||
|
{
|
||||||
|
var clipboard = TopLevel.GetTopLevel(this)?.Clipboard;
|
||||||
|
var text = await clipboard.GetTextAsync();
|
||||||
|
if (text is not null)
|
||||||
|
{
|
||||||
|
var newText = text.Where(c => Valid(c, Mode)).Take(Count).ToArray();
|
||||||
|
for (int i = 0; i < newText.Length; i++)
|
||||||
|
{
|
||||||
|
Digits[i] = newText[i].ToString();
|
||||||
|
var presenter = _itemsControl?.ContainerFromIndex(i) as PinCodeItem;
|
||||||
|
if (presenter is not null)
|
||||||
|
{
|
||||||
|
presenter.Focus();
|
||||||
|
presenter.Text = newText[i].ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (e.Key == Key.Tab && e.Source is PinCodeItem)
|
if (e.Key == Key.Tab && e.Source is PinCodeItem)
|
||||||
{
|
{
|
||||||
_currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1);
|
_currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1);
|
||||||
|
|||||||
89
tests/HeadlessTest.Ursa/Controls/PinCodeTests/PasteTest.cs
Normal file
89
tests/HeadlessTest.Ursa/Controls/PinCodeTests/PasteTest.cs
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Headless;
|
||||||
|
using Avalonia.Headless.XUnit;
|
||||||
|
using Avalonia.Input;
|
||||||
|
using Avalonia.Threading;
|
||||||
|
using Ursa.Controls;
|
||||||
|
|
||||||
|
namespace HeadlessTest.Ursa.Controls.PinCodeTests;
|
||||||
|
|
||||||
|
public class PasteTest
|
||||||
|
{
|
||||||
|
[AvaloniaFact]
|
||||||
|
public async void Paste_Should_Insert_Text()
|
||||||
|
{
|
||||||
|
var window = new Window();
|
||||||
|
var pinCode = new PinCode()
|
||||||
|
{
|
||||||
|
Count = 4,
|
||||||
|
};
|
||||||
|
window.Content = pinCode;
|
||||||
|
window.Show();
|
||||||
|
var clipboard = window.Clipboard;
|
||||||
|
Assert.NotNull(clipboard);
|
||||||
|
await clipboard.SetTextAsync("abcd");
|
||||||
|
pinCode.Focus();
|
||||||
|
window.KeyPressQwerty(PhysicalKey.V, RawInputModifiers.Control);
|
||||||
|
// add await for clipboard processing.
|
||||||
|
await Task.Delay(1);
|
||||||
|
Assert.Equal("abcd", string.Join("", pinCode.Digits));
|
||||||
|
}
|
||||||
|
|
||||||
|
[AvaloniaFact]
|
||||||
|
public async void Paste_Should_Insert_Text_When_Text_Is_Shorter()
|
||||||
|
{
|
||||||
|
var window = new Window();
|
||||||
|
var pinCode = new PinCode()
|
||||||
|
{
|
||||||
|
Count = 4,
|
||||||
|
};
|
||||||
|
window.Content = pinCode;
|
||||||
|
window.Show();
|
||||||
|
var clipboard = window.Clipboard;
|
||||||
|
Assert.NotNull(clipboard);
|
||||||
|
await clipboard.SetTextAsync("abc");
|
||||||
|
pinCode.Focus();
|
||||||
|
window.KeyPressQwerty(PhysicalKey.V, RawInputModifiers.Control);
|
||||||
|
await Task.Delay(1);
|
||||||
|
Assert.Equal("abc", string.Join("", pinCode.Digits));
|
||||||
|
}
|
||||||
|
|
||||||
|
[AvaloniaFact]
|
||||||
|
public async void Paste_Should_Insert_Text_When_Text_Is_Longer()
|
||||||
|
{
|
||||||
|
var window = new Window();
|
||||||
|
var pinCode = new PinCode()
|
||||||
|
{
|
||||||
|
Count = 4,
|
||||||
|
};
|
||||||
|
window.Content = pinCode;
|
||||||
|
window.Show();
|
||||||
|
var clipboard = window.Clipboard;
|
||||||
|
Assert.NotNull(clipboard);
|
||||||
|
await clipboard.SetTextAsync("abcde");
|
||||||
|
pinCode.Focus();
|
||||||
|
window.KeyPressQwerty(PhysicalKey.V, RawInputModifiers.Control);
|
||||||
|
await Task.Delay(1);
|
||||||
|
Assert.Equal("abcd", string.Join("", pinCode.Digits));
|
||||||
|
}
|
||||||
|
|
||||||
|
[AvaloniaFact]
|
||||||
|
public async void Paste_Should_Not_Insert_Text_When_Text_Is_In_Invalid_Mode()
|
||||||
|
{
|
||||||
|
var window = new Window();
|
||||||
|
var pinCode = new PinCode()
|
||||||
|
{
|
||||||
|
Count = 4,
|
||||||
|
Mode = PinCodeMode.Digit,
|
||||||
|
};
|
||||||
|
window.Content = pinCode;
|
||||||
|
window.Show();
|
||||||
|
var clipboard = window.Clipboard;
|
||||||
|
Assert.NotNull(clipboard);
|
||||||
|
await clipboard.SetTextAsync("abcde");
|
||||||
|
pinCode.Focus();
|
||||||
|
window.KeyPressQwerty(PhysicalKey.V, RawInputModifiers.Control);
|
||||||
|
await Task.Delay(1);
|
||||||
|
Assert.Equal("", string.Join("", pinCode.Digits));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user