修复 IPv4Box PinCode TimeBox 对 Delete 键的支持

#https://github.com/irihitech/Ursa.Avalonia/issues/590
This commit is contained in:
杨劼
2025-09-16 17:22:56 +08:00
parent 7f118d7b1c
commit 26f0a3a98d
3 changed files with 118 additions and 31 deletions

View File

@@ -214,7 +214,13 @@ public class IPv4Box: TemplatedControl
if (e.Key == Key.Back) 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; e.Handled = true;
return; return;
} }
@@ -474,7 +480,11 @@ public class IPv4Box: TemplatedControl
} }
} }
private void DeleteImplementation(TextPresenter? presenter) /// <summary>
/// </summary>
/// <param name="presenter"></param>
/// <param name="isDeleteKey">del 键 (从前往后删)</param>
private void DeleteImplementation(TextPresenter? presenter, bool isDeleteKey)
{ {
if (presenter is null) return; if (presenter is null) return;
var oldText = presenter.Text ?? string.Empty; var oldText = presenter.Text ?? string.Empty;
@@ -483,7 +493,40 @@ public class IPv4Box: TemplatedControl
presenter.DeleteSelection(); presenter.DeleteSelection();
presenter.ClearSelection(); presenter.ClearSelection();
} }
else if (string.IsNullOrWhiteSpace(oldText) || presenter.CaretIndex == 0) else if (isDeleteKey)
{
if (string.IsNullOrWhiteSpace(oldText) || presenter.CaretIndex == oldText.Length)
{
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
{
if (string.IsNullOrWhiteSpace(oldText) || presenter.CaretIndex == 0)
{ {
presenter.HideCaret(); presenter.HideCaret();
MoveToPreviousTextPresenter(presenter); MoveToPreviousTextPresenter(presenter);
@@ -501,6 +544,7 @@ public class IPv4Box: TemplatedControl
presenter.Text = newText; presenter.Text = newText;
} }
} }
}
private void OnPressRightKey() private void OnPressRightKey()
{ {

View File

@@ -209,6 +209,17 @@ public class PinCode : TemplatedControl
_currentIndex--; _currentIndex--;
_itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); _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) else if (e.Key is Key.Left or Key.FnLeftArrow)
{ {
_currentIndex--; _currentIndex--;

View File

@@ -251,7 +251,11 @@ public class TimeBox : TemplatedControl
} }
else if (e.Key == Key.Back) 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) else if (e.Key == Key.Right)
{ {
@@ -596,7 +600,11 @@ public class TimeBox : TemplatedControl
} }
} }
private void DeleteImplementation(int index) /// <summary>
/// </summary>
/// <param name="index"></param>
/// <param name="isDeleteKey">del 键 (从前往后删)</param>
private void DeleteImplementation(int index, bool isDeleteKey)
{ {
if (index < 0 || index > 3) return; if (index < 0 || index > 3) return;
var oldText = _presenters[index].Text??string.Empty; var oldText = _presenters[index].Text??string.Empty;
@@ -605,7 +613,30 @@ public class TimeBox : TemplatedControl
_presenters[index].DeleteSelection(); _presenters[index].DeleteSelection();
_presenters[index].ClearSelection(); _presenters[index].ClearSelection();
} }
else if (string.IsNullOrWhiteSpace(oldText) || _presenters[index].CaretIndex == 0) else if (isDeleteKey)
{
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
{
if (string.IsNullOrWhiteSpace(oldText) || _presenters[index].CaretIndex == 0)
{ {
MoveToPreviousSection(index); MoveToPreviousSection(index);
} }
@@ -618,6 +649,7 @@ public class TimeBox : TemplatedControl
_presenters[index].Text = newText; _presenters[index].Text = newText;
} }
} }
}
private bool HandlingCarry(int index, int lowerCarry = 0) private bool HandlingCarry(int index, int lowerCarry = 0)
{ {