From bb7c6120d35ce26947e7e1da5b94e4f212dd04ea Mon Sep 17 00:00:00 2001 From: Adam Dernis Date: Mon, 21 Mar 2022 20:02:02 -0400 Subject: [PATCH] Fixed left/right key input for Carousel in RTL Flow Direction --- .../Carousel/Carousel.cs | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Layout/Carousel/Carousel.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Layout/Carousel/Carousel.cs index d832a41b0f5..79bf0e6c5f1 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Layout/Carousel/Carousel.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Layout/Carousel/Carousel.cs @@ -393,11 +393,8 @@ private void Keyboard_KeyUp(object sender, KeyRoutedEventArgs e) case Windows.System.VirtualKey.GamepadLeftThumbstickDown: if (Orientation == Orientation.Vertical) { - if (SelectedIndex < Items.Count - 1) - { - SelectedIndex++; - } - else if (e.OriginalKey != Windows.System.VirtualKey.Down) + bool changed = BoundIncrement(); + if (!changed && e.OriginalKey != Windows.System.VirtualKey.Down) { FocusManager.TryMoveFocus(FocusNavigationDirection.Down); } @@ -411,11 +408,8 @@ private void Keyboard_KeyUp(object sender, KeyRoutedEventArgs e) case Windows.System.VirtualKey.GamepadLeftThumbstickUp: if (Orientation == Orientation.Vertical) { - if (SelectedIndex > 0) - { - SelectedIndex--; - } - else if (e.OriginalKey != Windows.System.VirtualKey.Up) + bool changed = BoundDecrement(); + if (!changed && e.OriginalKey != Windows.System.VirtualKey.Up) { FocusManager.TryMoveFocus(FocusNavigationDirection.Up); } @@ -429,11 +423,8 @@ private void Keyboard_KeyUp(object sender, KeyRoutedEventArgs e) case Windows.System.VirtualKey.GamepadLeftThumbstickLeft: if (Orientation == Orientation.Horizontal) { - if (SelectedIndex > 0) - { - SelectedIndex--; - } - else if (e.OriginalKey != Windows.System.VirtualKey.Left) + bool changed = FlowDirection == FlowDirection.LeftToRight ? BoundDecrement() : BoundIncrement(); + if (!changed && e.OriginalKey != Windows.System.VirtualKey.Left) { FocusManager.TryMoveFocus(FocusNavigationDirection.Left); } @@ -447,11 +438,8 @@ private void Keyboard_KeyUp(object sender, KeyRoutedEventArgs e) case Windows.System.VirtualKey.GamepadLeftThumbstickRight: if (Orientation == Orientation.Horizontal) { - if (SelectedIndex < Items.Count - 1) - { - SelectedIndex++; - } - else if (e.OriginalKey != Windows.System.VirtualKey.Right) + bool changed = FlowDirection == FlowDirection.LeftToRight ? BoundIncrement() : BoundDecrement(); + if (!changed && e.OriginalKey != Windows.System.VirtualKey.Right) { FocusManager.TryMoveFocus(FocusNavigationDirection.Right); } @@ -466,14 +454,13 @@ private void Keyboard_KeyUp(object sender, KeyRoutedEventArgs e) internal void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e) { var index = e.GetCurrentPoint(null).Properties.MouseWheelDelta > 0 ? -1 : 1; - if (index == -1 && SelectedIndex > 0) + if (index == -1) { - SelectedIndex--; + BoundDecrement(); } - - if (index == 1 && SelectedIndex < Items.Count - 1) + else if (index == 1) { - SelectedIndex++; + BoundIncrement(); } e.Handled = true; @@ -547,5 +534,27 @@ internal void SetSelectedItem(CarouselItem owner) var item = ItemFromContainer(owner); SelectedItem = item; } + + private bool BoundIncrement() + { + if (SelectedIndex < Items.Count - 1) + { + SelectedIndex++; + return true; + } + + return false; + } + + private bool BoundDecrement() + { + if (SelectedIndex > 0) + { + SelectedIndex--; + return true; + } + + return false; + } } -} \ No newline at end of file +}