Skip to content

Fixed left/right key input for Carousel in RTL Flow Direction #4518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions Microsoft.Toolkit.Uwp.UI.Controls.Layout/Carousel/Carousel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
}