Skip to content

Commit

Permalink
Merge pull request #6656 from AvaloniaUI/fixes/3429-scroll-gesture-vi…
Browse files Browse the repository at this point in the history
…rtualization

Improve scroll gesture with virtualization.
  • Loading branch information
Dan Walmsley committed Oct 4, 2021
2 parents 5ece272 + c4de494 commit 7ce5389
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ public class ScrollContentPresenter : ContentPresenter, IPresenter, IScrollable,
o => o.Viewport,
(o, v) => o.Viewport = v);

// Arbitrary chosen value, probably need to ask ILogicalScrollable
private const int LogicalScrollItemSize = 50;

private bool _canHorizontallyScroll;
private bool _canVerticallyScroll;
private bool _arranging;
Expand Down Expand Up @@ -351,7 +348,8 @@ private void OnScrollGesture(object sender, ScrollGestureEventArgs e)
if (Extent.Height > Viewport.Height || Extent.Width > Viewport.Width)
{
var scrollable = Child as ILogicalScrollable;
bool isLogical = scrollable?.IsLogicalScrollEnabled == true;
var isLogical = scrollable?.IsLogicalScrollEnabled == true;
var logicalScrollItemSize = new Vector(1, 1);

double x = Offset.X;
double y = Offset.Y;
Expand All @@ -361,13 +359,18 @@ private void OnScrollGesture(object sender, ScrollGestureEventArgs e)
_activeLogicalGestureScrolls?.TryGetValue(e.Id, out delta);
delta += e.Delta;

if (isLogical && scrollable is object)
{
logicalScrollItemSize = Bounds.Size / scrollable.Viewport;
}

if (Extent.Height > Viewport.Height)
{
double dy;
if (isLogical)
{
var logicalUnits = delta.Y / LogicalScrollItemSize;
delta = delta.WithY(delta.Y - logicalUnits * LogicalScrollItemSize);
var logicalUnits = delta.Y / logicalScrollItemSize.Y;
delta = delta.WithY(delta.Y - logicalUnits * logicalScrollItemSize.Y);
dy = logicalUnits * scrollable!.ScrollSize.Height;
}
else
Expand All @@ -384,8 +387,8 @@ private void OnScrollGesture(object sender, ScrollGestureEventArgs e)
double dx;
if (isLogical)
{
var logicalUnits = delta.X / LogicalScrollItemSize;
delta = delta.WithX(delta.X - logicalUnits * LogicalScrollItemSize);
var logicalUnits = delta.X / logicalScrollItemSize.X;
delta = delta.WithX(delta.X - logicalUnits * logicalScrollItemSize.X);
dx = logicalUnits * scrollable!.ScrollSize.Width;
}
else
Expand Down

0 comments on commit 7ce5389

Please sign in to comment.