feat: fix left/right navigation.

This commit is contained in:
rabbitism
2024-07-31 20:28:12 +08:00
parent 8941ddfce4
commit fe52b6a9dc
2 changed files with 34 additions and 3 deletions

View File

@@ -79,6 +79,7 @@ public class PinCode: TemplatedControl
{ {
CountProperty.Changed.AddClassHandler<PinCode, int>((code, args) => code.OnCountOfDigitChanged(args)); CountProperty.Changed.AddClassHandler<PinCode, int>((code, args) => code.OnCountOfDigitChanged(args));
FocusableProperty.OverrideDefaultValue<PinCode>(true); FocusableProperty.OverrideDefaultValue<PinCode>(true);
KeyDownEvent.AddClassHandler<PinCode>((o,e)=>o.OnPreviewKeyDown(e), RoutingStrategies.Tunnel);
} }
public PinCode() public PinCode()
@@ -140,6 +141,7 @@ public class PinCode: TemplatedControl
{ {
CompleteCommand?.Execute(Digits); CompleteCommand?.Execute(Digits);
RaiseEvent(new PinCodeCompleteEventArgs(Digits, CompleteEvent)); RaiseEvent(new PinCodeCompleteEventArgs(Digits, CompleteEvent));
_currentIndex--;
} }
} }
} }
@@ -157,7 +159,7 @@ public class PinCode: TemplatedControl
}; };
} }
protected override void OnKeyDown(KeyEventArgs e) protected void OnPreviewKeyDown(KeyEventArgs e)
{ {
if (e.Key == Key.Tab && e.Source is PinCodeItem) if (e.Key == Key.Tab && e.Source is PinCodeItem)
{ {
@@ -168,8 +170,7 @@ public class PinCode: TemplatedControl
_currentIndex++; _currentIndex++;
_currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1);
} }
base.OnKeyDown(e); else if (e.Key == Key.Back && _currentIndex >= 0)
if (e.Key == Key.Back && _currentIndex >= 0)
{ {
_currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1); _currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1);
var presenter = _itemsControl?.ContainerFromIndex(_currentIndex) as PinCodeItem; var presenter = _itemsControl?.ContainerFromIndex(_currentIndex) as PinCodeItem;
@@ -180,5 +181,26 @@ public class PinCode: TemplatedControl
_currentIndex--; _currentIndex--;
_itemsControl?.ContainerFromIndex(_currentIndex)?.Focus(); _itemsControl?.ContainerFromIndex(_currentIndex)?.Focus();
} }
else if (e.Key is Key.Left or Key.FnLeftArrow)
{
_currentIndex--;
_currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1);
_itemsControl?.ContainerFromIndex(_currentIndex)?.Focus();
}
else if(e.Key is Key.Right or Key.FnRightArrow)
{
_currentIndex++;
_currentIndex = MathHelpers.SafeClamp(_currentIndex, 0, Count - 1);
_itemsControl?.ContainerFromIndex(_currentIndex)?.Focus();
}
else if (e.Key is Key.Enter or Key.Return)
{
CompleteCommand?.Execute(Digits);
RaiseEvent(new PinCodeCompleteEventArgs(Digits, CompleteEvent));
}
else
{
base.OnKeyDown(e);
}
} }
} }

View File

@@ -17,4 +17,13 @@ public class PinCodeCollection: ItemsControl
[InputMethod.IsInputMethodEnabledProperty] = false, [InputMethod.IsInputMethodEnabledProperty] = false,
}; };
} }
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key is Key.Left or Key.Right or Key.FnLeftArrow or Key.FnRightArrow)
{
e.Handled = true;
}
base.OnKeyDown(e);
}
} }