Skip to content

Commit

Permalink
Merge pull request #7964 from AvaloniaUI/fixes/7840-datetimepicker-to…
Browse files Browse the repository at this point in the history
…uch-scroll

Fix Date/Time picker touch scrolling
  • Loading branch information
grokys committed Apr 12, 2022
1 parent a888ca3 commit 0b859e8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
31 changes: 30 additions & 1 deletion src/Avalonia.Controls/DateTimePickers/DateTimePickerPanel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Globalization;
using System.Linq;
using Avalonia.Controls.Presenters;
using Avalonia.Input;
using Avalonia.Input.GestureRecognizers;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.VisualTree;
Expand Down Expand Up @@ -60,6 +62,7 @@ public class DateTimePickerPanel : Panel, ILogicalScrollable
private Vector _offset;
private bool _hasInit;
private bool _suppressUpdateOffset;
private ScrollContentPresenter? _parentScroller;

public DateTimePickerPanel()
{
Expand Down Expand Up @@ -255,6 +258,8 @@ public Vector Offset
_suppressUpdateOffset = true;
SelectedValue = (int)newSel * Increment + MinimumValue;
_suppressUpdateOffset = false;

System.Diagnostics.Debug.WriteLine($"Offset: {_offset} ItemHeight: {ItemHeight}");
}
}

Expand All @@ -270,7 +275,7 @@ public Vector Offset

public Size Extent => _extent;

public Size Viewport => new Size(0, ItemHeight);
public Size Viewport => Bounds.Size;

public event EventHandler ScrollInvalidated;

Expand Down Expand Up @@ -341,6 +346,20 @@ protected override Size ArrangeOverride(Size finalSize)
return finalSize;
}

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
_parentScroller = this.GetVisualParent() as ScrollContentPresenter;
_parentScroller?.AddHandler(Gestures.ScrollGestureEndedEvent, OnScrollGestureEnded);
}

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
_parentScroller?.RemoveHandler(Gestures.ScrollGestureEndedEvent, OnScrollGestureEnded);
_parentScroller = null;
}

protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.Key)
Expand Down Expand Up @@ -554,5 +573,15 @@ public void RaiseScrollInvalidated(EventArgs e)
{
ScrollInvalidated?.Invoke(this, e);
}

private void OnScrollGestureEnded(object? sender, ScrollGestureEndedEventArgs e)
{
var snapY = Math.Round(Offset.Y / ItemHeight) * ItemHeight;

if (snapY != Offset.Y)
{
Offset = Offset.WithY(snapY);
}
}
}
}
4 changes: 2 additions & 2 deletions src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ private void OnScrollGesture(object sender, ScrollGestureEventArgs e)
{
var logicalUnits = delta.Y / logicalScrollItemSize.Y;
delta = delta.WithY(delta.Y - logicalUnits * logicalScrollItemSize.Y);
dy = logicalUnits * scrollable!.ScrollSize.Height;
dy = logicalUnits;
}
else
dy = delta.Y;
Expand All @@ -389,7 +389,7 @@ private void OnScrollGesture(object sender, ScrollGestureEventArgs e)
{
var logicalUnits = delta.X / logicalScrollItemSize.X;
delta = delta.WithX(delta.X - logicalUnits * logicalScrollItemSize.X);
dx = logicalUnits * scrollable!.ScrollSize.Width;
dx = logicalUnits;
}
else
dx = delta.X;
Expand Down

0 comments on commit 0b859e8

Please sign in to comment.