diff --git a/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml b/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml index dfd61e6..43e4ac8 100644 --- a/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml +++ b/demo/Ursa.Demo/Pages/VerificationCodeDemo.axaml @@ -4,13 +4,14 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:u="https://irihi.tech/ursa" xmlns:vm="using:Ursa.Demo.ViewModels" + xmlns:system="clr-namespace:System;assembly=System.Runtime" x:DataType="vm:VerificationCodeDemoViewModel" x:CompileBindings="True" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="Ursa.Demo.Pages.VerificationCodeDemo"> - + diff --git a/demo/Ursa.Demo/ViewModels/VerificationCodeDemoViewModel.cs b/demo/Ursa.Demo/ViewModels/VerificationCodeDemoViewModel.cs index 94e4fff..5de9705 100644 --- a/demo/Ursa.Demo/ViewModels/VerificationCodeDemoViewModel.cs +++ b/demo/Ursa.Demo/ViewModels/VerificationCodeDemoViewModel.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; using System.Windows.Input; using Avalonia.Collections; @@ -8,13 +9,15 @@ using Ursa.Controls; namespace Ursa.Demo.ViewModels; -public class VerificationCodeDemoViewModel: ObservableObject +public partial class VerificationCodeDemoViewModel: ObservableObject { public ICommand CompleteCommand { get; set; } + [ObservableProperty] private List? _error; public VerificationCodeDemoViewModel() { CompleteCommand = new AsyncRelayCommand>(OnComplete); + Error = [new Exception("Invalid verification code")]; } private async Task OnComplete(IList? obj) diff --git a/src/Ursa.Themes.Semi/Controls/VerificationCode.axaml b/src/Ursa.Themes.Semi/Controls/VerificationCode.axaml index ca051f7..ae38a54 100644 --- a/src/Ursa.Themes.Semi/Controls/VerificationCode.axaml +++ b/src/Ursa.Themes.Semi/Controls/VerificationCode.axaml @@ -10,16 +10,19 @@ - + + + + + BorderThickness="{TemplateBinding BorderThickness}" + CornerRadius="{TemplateBinding CornerRadius}"> + + - + @@ -47,21 +56,24 @@ - + - - - - - - - - - - - - + + + + + + + + + + + + + + + diff --git a/src/Ursa/Controls/VerificationCode/VerificationCode.cs b/src/Ursa/Controls/VerificationCode/VerificationCode.cs index 5e963bd..0a82576 100644 --- a/src/Ursa/Controls/VerificationCode/VerificationCode.cs +++ b/src/Ursa/Controls/VerificationCode/VerificationCode.cs @@ -8,6 +8,7 @@ using Avalonia.Controls.Primitives; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.LogicalTree; +using Avalonia.Utilities; using Irihi.Avalonia.Shared.Helpers; namespace Ursa.Controls; @@ -108,12 +109,16 @@ public class VerificationCode: TemplatedControl { if (e.Source is Control t) { - var text = t.FindLogicalAncestorOfType(); - if (text != null) + /* + var item = t.FindLogicalAncestorOfType(); + if (item != null) { - text.Focus(); - _currentIndex = _itemsControl?.IndexFromContainer(text) ?? 0; + item.Focus(); + _currentIndex = _itemsControl?.IndexFromContainer(item) ?? 0; } + */ + _currentIndex = MathUtilities.Clamp(_currentIndex, 0, Count - 1); + _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); } e.Handled = true; } @@ -157,6 +162,7 @@ public class VerificationCode: TemplatedControl base.OnKeyDown(e); if (e.Key == Key.Back && _currentIndex >= 0) { + _currentIndex = MathUtilities.Clamp(_currentIndex, 0, Count - 1); var presenter = _itemsControl?.ContainerFromIndex(_currentIndex) as VerificationCodeItem; if (presenter is null) return; Digits[_currentIndex] = string.Empty; @@ -165,15 +171,5 @@ public class VerificationCode: TemplatedControl _currentIndex--; _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); } - else if(e.Key == Key.Left && _currentIndex > 0) - { - _currentIndex--; - _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); - } - else if(e.Key == Key.Right && _currentIndex < Count) - { - _currentIndex++; - _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); - } } } \ No newline at end of file