From 26f0a3a98dd94dc536dd22be510d9568a715b636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=8A=BC?= Date: Tue, 16 Sep 2025 17:22:56 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20IPv4Box=20PinCode=20Ti?= =?UTF-8?q?meBox=20=20=E5=AF=B9=20Delete=20=E9=94=AE=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20#https://github.com/irihitech/Ursa.Avalonia/issues/?= =?UTF-8?q?590?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Ursa/Controls/IPv4Box/IPv4Box.cs | 88 +++++++++++++++++++++------- src/Ursa/Controls/PinCode/PinCode.cs | 11 ++++ src/Ursa/Controls/TimeBox.cs | 50 +++++++++++++--- 3 files changed, 118 insertions(+), 31 deletions(-) diff --git a/src/Ursa/Controls/IPv4Box/IPv4Box.cs b/src/Ursa/Controls/IPv4Box/IPv4Box.cs index 5444bf3..e5062b6 100644 --- a/src/Ursa/Controls/IPv4Box/IPv4Box.cs +++ b/src/Ursa/Controls/IPv4Box/IPv4Box.cs @@ -26,7 +26,7 @@ public enum IPv4BoxInputMode [TemplatePart(PART_SecondTextPresenter, typeof(TextPresenter))] [TemplatePart(PART_ThirdTextPresenter, typeof(TextPresenter))] [TemplatePart(PART_FourthTextPresenter, typeof(TextPresenter))] -public class IPv4Box: TemplatedControl +public class IPv4Box : TemplatedControl { public const string PART_FirstTextPresenter = "PART_FirstTextPresenter"; public const string PART_SecondTextPresenter = "PART_SecondTextPresenter"; @@ -42,7 +42,7 @@ public class IPv4Box: TemplatedControl private byte? _fourthByte; private readonly TextPresenter?[] _presenters = new TextPresenter?[4]; private TextPresenter? _currentActivePresenter; - + public static readonly StyledProperty IPAddressProperty = AvaloniaProperty.Register( nameof(IPAddress), defaultBindingMode: BindingMode.TwoWay); public IPAddress? IPAddress @@ -111,7 +111,7 @@ public class IPv4Box: TemplatedControl /// /// 是否使用小键盘输入 /// - internal bool IsTargetByNumPad + internal bool IsTargetByNumPad { set { @@ -152,7 +152,7 @@ public class IPv4Box: TemplatedControl ParseBytes(ShowLeadingZero); } } - + protected override void OnKeyDown(KeyEventArgs e) { _currentActivePresenter ??= _presenters[0]; @@ -214,7 +214,13 @@ public class IPv4Box: TemplatedControl if (e.Key == Key.Back) { - DeleteImplementation(_currentActivePresenter); + DeleteImplementation(_currentActivePresenter, isDeleteKey: false); + e.Handled = true; + return; + } + if (e.Key == Key.Delete) + { + DeleteImplementation(_currentActivePresenter, isDeleteKey: true); e.Handled = true; return; } @@ -234,7 +240,7 @@ public class IPv4Box: TemplatedControl } base.OnKeyDown(e); } - + protected override void OnTextInput(TextInputEventArgs e) { if (e.Handled) return; @@ -291,7 +297,7 @@ public class IPv4Box: TemplatedControl } } } - + protected override void OnPointerPressed(PointerPressedEventArgs e) { Point position = e.GetPosition(_firstText); @@ -413,7 +419,7 @@ public class IPv4Box: TemplatedControl if (_fourthText != null) _fourthText.Text = _fourthByte?.ToString(format); } - + private bool MoveToNextPresenter(TextPresenter? presenter, bool selectAllAfterMove) { @@ -451,7 +457,7 @@ public class IPv4Box: TemplatedControl _fourthByte = null; IPAddress = null; } - + private void SetIPAddressInternal() { if (_firstByte is null && _secondByte is null && _thirdByte is null && _fourthByte is null) @@ -474,31 +480,69 @@ public class IPv4Box: TemplatedControl } } - private void DeleteImplementation(TextPresenter? presenter) + /// + /// + /// + /// del 键 (从前往后删) + private void DeleteImplementation(TextPresenter? presenter, bool isDeleteKey) { - if(presenter is null) return; + if (presenter is null) return; var oldText = presenter.Text ?? string.Empty; if (presenter.SelectionStart != presenter.SelectionEnd) { presenter.DeleteSelection(); presenter.ClearSelection(); } - else if (string.IsNullOrWhiteSpace(oldText) || presenter.CaretIndex == 0) + else if (isDeleteKey) { - presenter.HideCaret(); - MoveToPreviousTextPresenter(presenter); - if (_currentActivePresenter != null) + if (string.IsNullOrWhiteSpace(oldText) || presenter.CaretIndex == oldText.Length) { - _currentActivePresenter.ShowCaret(); - _currentActivePresenter.MoveCaretToEnd(); + presenter.HideCaret(); + var oldActivePresenter = _currentActivePresenter; + MoveToNextPresenter(presenter, selectAllAfterMove: false); + if (_currentActivePresenter != null) + { + _currentActivePresenter.ShowCaret(); + if (!ReferenceEquals(oldActivePresenter, _currentActivePresenter)) + { + _currentActivePresenter.MoveCaretToStart(); + } + } + } + else + { + var index = presenter.CaretIndex; + var newText = string.Empty; + if (index == 0) + { + newText = oldText.Substring(1, oldText.Length - 1); + } + else + { + newText = oldText.Substring(0, index) + oldText.Substring(index + 1); + } + presenter.Text = newText; } } else { - int index = presenter.CaretIndex; - string newText = oldText.Substring(0, index - 1) + oldText.Substring(Math.Min(index, oldText.Length)); - presenter.MoveCaretHorizontal(LogicalDirection.Backward); - presenter.Text = newText; + if (string.IsNullOrWhiteSpace(oldText) || presenter.CaretIndex == 0) + { + presenter.HideCaret(); + MoveToPreviousTextPresenter(presenter); + if (_currentActivePresenter != null) + { + _currentActivePresenter.ShowCaret(); + _currentActivePresenter.MoveCaretToEnd(); + } + } + else + { + int index = presenter.CaretIndex; + string newText = oldText.Substring(0, index - 1) + oldText.Substring(Math.Min(index, oldText.Length)); + presenter.MoveCaretHorizontal(LogicalDirection.Backward); + presenter.Text = newText; + } } } @@ -565,7 +609,7 @@ public class IPv4Box: TemplatedControl if (clipboard is null) return; await clipboard.SetTextAsync(s); } - + public static KeyGesture? CopyKeyGesture { get; } = Application.Current?.PlatformSettings?.HotkeyConfiguration.Copy.FirstOrDefault(); public static KeyGesture? PasteKeyGesture { get; } = Application.Current?.PlatformSettings?.HotkeyConfiguration.Paste.FirstOrDefault(); public static KeyGesture? CutKeyGesture { get; } = Application.Current?.PlatformSettings?.HotkeyConfiguration.Cut.FirstOrDefault(); diff --git a/src/Ursa/Controls/PinCode/PinCode.cs b/src/Ursa/Controls/PinCode/PinCode.cs index a1957be..a2bdecc 100644 --- a/src/Ursa/Controls/PinCode/PinCode.cs +++ b/src/Ursa/Controls/PinCode/PinCode.cs @@ -209,6 +209,17 @@ public class PinCode : TemplatedControl _currentIndex--; _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); } + else if (e.Key == Key.Delete && _currentIndex < Digits.Count) + { + _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); + var presenter = _itemsControl?.ContainerFromIndex(_currentIndex) as PinCodeItem; + if (presenter is null) return; + Digits[_currentIndex] = string.Empty; + presenter.Text = string.Empty; + if (_currentIndex == Digits.Count-1) return; + _currentIndex++; + _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); + } else if (e.Key is Key.Left or Key.FnLeftArrow) { _currentIndex--; diff --git a/src/Ursa/Controls/TimeBox.cs b/src/Ursa/Controls/TimeBox.cs index 9eda16d..312a2db 100644 --- a/src/Ursa/Controls/TimeBox.cs +++ b/src/Ursa/Controls/TimeBox.cs @@ -251,7 +251,11 @@ public class TimeBox : TemplatedControl } else if (e.Key == Key.Back) { - DeleteImplementation(_currentActiveSectionIndex.Value); + DeleteImplementation(_currentActiveSectionIndex.Value, isDeleteKey: false); + } + else if (e.Key == Key.Delete) + { + DeleteImplementation(_currentActiveSectionIndex.Value, isDeleteKey: true); } else if (e.Key == Key.Right) { @@ -596,7 +600,11 @@ public class TimeBox : TemplatedControl } } - private void DeleteImplementation(int index) + /// + /// + /// + /// del 键 (从前往后删) + private void DeleteImplementation(int index, bool isDeleteKey) { if (index < 0 || index > 3) return; var oldText = _presenters[index].Text??string.Empty; @@ -605,17 +613,41 @@ public class TimeBox : TemplatedControl _presenters[index].DeleteSelection(); _presenters[index].ClearSelection(); } - else if (string.IsNullOrWhiteSpace(oldText) || _presenters[index].CaretIndex == 0) + else if (isDeleteKey) { - MoveToPreviousSection(index); + if (string.IsNullOrWhiteSpace(oldText) || _presenters[index].CaretIndex == oldText.Length) + { + MoveToNextSection(index); + } + else + { + int caretIndex = _presenters[index].CaretIndex; + var newText = string.Empty; + if (caretIndex == 0) + { + newText = oldText.Substring(1, oldText.Length - 1); + } + else + { + newText = oldText.Substring(0, caretIndex) + oldText.Substring(caretIndex + 1); + } + _presenters[index].Text = newText; + } } else { - int caretIndex = _presenters[index].CaretIndex; - string newText = oldText.Substring(0, caretIndex - 1) + - oldText.Substring(Math.Min(caretIndex, oldText.Length)); - _presenters[index].MoveCaretHorizontal(LogicalDirection.Backward); - _presenters[index].Text = newText; + if (string.IsNullOrWhiteSpace(oldText) || _presenters[index].CaretIndex == 0) + { + MoveToPreviousSection(index); + } + else + { + int caretIndex = _presenters[index].CaretIndex; + string newText = oldText.Substring(0, caretIndex - 1) + + oldText.Substring(Math.Min(caretIndex, oldText.Length)); + _presenters[index].MoveCaretHorizontal(LogicalDirection.Backward); + _presenters[index].Text = newText; + } } } From 9236b9f8c801058252d1a8c406e671930032755d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=8A=BC?= Date: Wed, 17 Sep 2025 14:09:19 +0800 Subject: [PATCH 2/2] =?UTF-8?q?PinCode=20=E5=8E=BB=E9=99=A4=20OnPreviewKey?= =?UTF-8?q?Down=20=E9=87=8C=E6=97=A0=E6=84=8F=E4=B9=89=E7=9A=84=20TextBox?= =?UTF-8?q?=20=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Ursa/Controls/PinCode/PinCode.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Ursa/Controls/PinCode/PinCode.cs b/src/Ursa/Controls/PinCode/PinCode.cs index a2bdecc..773c058 100644 --- a/src/Ursa/Controls/PinCode/PinCode.cs +++ b/src/Ursa/Controls/PinCode/PinCode.cs @@ -161,7 +161,6 @@ public class PinCode : TemplatedControl 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) {