Skip to content

Commit

Permalink
Merge pull request #4518 from Avid29/carousel_rtl
Browse files Browse the repository at this point in the history
Fixed left/right key input for Carousel in RTL Flow Direction
  • Loading branch information
michael-hawker authored Aug 9, 2022
2 parents 55723da + bb7c612 commit 7a9bf31
Showing 1 changed file with 35 additions and 26 deletions.
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;
}
}
}
}

0 comments on commit 7a9bf31

Please sign in to comment.