Skip to content

Commit

Permalink
fix(scrollviewer): keyboard scrolling with arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ramezgerges committed Nov 5, 2023
1 parent 59ad94f commit d92fefb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ partial void InitializePartial()

// Mouse wheel support
PointerWheelChanged += PointerWheelScroll;
KeyDown += KeyDownScroll;

// Touch scroll support
ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY; // Updated in PrepareTouchScroll!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Windows.Foundation;
using Uno.UI;
using Windows.System;
using Windows.UI.Xaml.Input;
#if __ANDROID__
using View = Android.Views.View;
using Font = Android.Graphics.Typeface;
Expand Down Expand Up @@ -272,6 +273,47 @@ private void PointerWheelScroll(object sender, Input.PointerRoutedEventArgs e)
}
}

private void KeyDownScroll(object sender, KeyRoutedEventArgs e)
{
var key = e.Key;
if (key != VirtualKey.Up && key != VirtualKey.Down && key != VirtualKey.Left && key != VirtualKey.Right)
{
return;
}

if (Content is UIElement)
{
var canScrollHorizontally = CanHorizontallyScroll;
var canScrollVertically = CanVerticallyScroll;
var delta = key is VirtualKey.Down or VirtualKey.Right ? -24 : 24;

if (canScrollHorizontally && (!canScrollVertically || key == VirtualKey.Left || key == VirtualKey.Right))
{
#if __WASM__ // On wasm the scroll might be async (especially with disableAnimation: false), so we need to use the pending value to support high speed multiple wheel events
var horizontalOffset = _pendingScrollTo?.horizontal ?? HorizontalOffset;
#else
var horizontalOffset = HorizontalOffset;
#endif

Set(
horizontalOffset: horizontalOffset + delta,
disableAnimation: false);
}
else if (canScrollVertically && key is VirtualKey.Up or VirtualKey.Down)
{
#if __WASM__ // On wasm the scroll might be async (especially with disableAnimation: false), so we need to use the pending value to support high speed multiple wheel events
var verticalOffset = _pendingScrollTo?.vertical ?? VerticalOffset;
#else
var verticalOffset = VerticalOffset;
#endif

Set(
verticalOffset: verticalOffset - delta,
disableAnimation: false);
}
}
}

public void SetVerticalOffset(double offset)
=> Set(verticalOffset: offset, disableAnimation: true);

Expand Down

0 comments on commit d92fefb

Please sign in to comment.