Merge pull request #773 from yangjieshao/fix_issues_590

修复 IPv4Box PinCode TimeBox  对 Delete 键的支持
This commit is contained in:
Dong Bin
2025-09-19 14:45:59 +08:00
committed by GitHub
3 changed files with 118 additions and 32 deletions

View File

@@ -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;
}
@@ -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;
var oldText = presenter.Text ?? string.Empty;
@@ -483,7 +493,40 @@ public class IPv4Box: TemplatedControl
presenter.DeleteSelection();
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();
MoveToPreviousTextPresenter(presenter);
@@ -501,6 +544,7 @@ public class IPv4Box: TemplatedControl
presenter.Text = newText;
}
}
}
private void OnPressRightKey()
{

View File

@@ -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)
{
@@ -209,6 +208,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--;

View File

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