Skip to content

Commit 0b859e8

Browse files
committed
Merge pull request AvaloniaUI#7964 from AvaloniaUI/fixes/7840-datetimepicker-touch-scroll
Fix Date/Time picker touch scrolling
1 parent a888ca3 commit 0b859e8

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/Avalonia.Controls/DateTimePickers/DateTimePickerPanel.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Globalization;
33
using System.Linq;
4+
using Avalonia.Controls.Presenters;
45
using Avalonia.Input;
6+
using Avalonia.Input.GestureRecognizers;
57
using Avalonia.Interactivity;
68
using Avalonia.Media;
79
using Avalonia.VisualTree;
@@ -60,6 +62,7 @@ public class DateTimePickerPanel : Panel, ILogicalScrollable
6062
private Vector _offset;
6163
private bool _hasInit;
6264
private bool _suppressUpdateOffset;
65+
private ScrollContentPresenter? _parentScroller;
6366

6467
public DateTimePickerPanel()
6568
{
@@ -255,6 +258,8 @@ public Vector Offset
255258
_suppressUpdateOffset = true;
256259
SelectedValue = (int)newSel * Increment + MinimumValue;
257260
_suppressUpdateOffset = false;
261+
262+
System.Diagnostics.Debug.WriteLine($"Offset: {_offset} ItemHeight: {ItemHeight}");
258263
}
259264
}
260265

@@ -270,7 +275,7 @@ public Vector Offset
270275

271276
public Size Extent => _extent;
272277

273-
public Size Viewport => new Size(0, ItemHeight);
278+
public Size Viewport => Bounds.Size;
274279

275280
public event EventHandler ScrollInvalidated;
276281

@@ -341,6 +346,20 @@ protected override Size ArrangeOverride(Size finalSize)
341346
return finalSize;
342347
}
343348

349+
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
350+
{
351+
base.OnAttachedToVisualTree(e);
352+
_parentScroller = this.GetVisualParent() as ScrollContentPresenter;
353+
_parentScroller?.AddHandler(Gestures.ScrollGestureEndedEvent, OnScrollGestureEnded);
354+
}
355+
356+
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
357+
{
358+
base.OnDetachedFromVisualTree(e);
359+
_parentScroller?.RemoveHandler(Gestures.ScrollGestureEndedEvent, OnScrollGestureEnded);
360+
_parentScroller = null;
361+
}
362+
344363
protected override void OnKeyDown(KeyEventArgs e)
345364
{
346365
switch (e.Key)
@@ -554,5 +573,15 @@ public void RaiseScrollInvalidated(EventArgs e)
554573
{
555574
ScrollInvalidated?.Invoke(this, e);
556575
}
576+
577+
private void OnScrollGestureEnded(object? sender, ScrollGestureEndedEventArgs e)
578+
{
579+
var snapY = Math.Round(Offset.Y / ItemHeight) * ItemHeight;
580+
581+
if (snapY != Offset.Y)
582+
{
583+
Offset = Offset.WithY(snapY);
584+
}
585+
}
557586
}
558587
}

src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ private void OnScrollGesture(object sender, ScrollGestureEventArgs e)
371371
{
372372
var logicalUnits = delta.Y / logicalScrollItemSize.Y;
373373
delta = delta.WithY(delta.Y - logicalUnits * logicalScrollItemSize.Y);
374-
dy = logicalUnits * scrollable!.ScrollSize.Height;
374+
dy = logicalUnits;
375375
}
376376
else
377377
dy = delta.Y;
@@ -389,7 +389,7 @@ private void OnScrollGesture(object sender, ScrollGestureEventArgs e)
389389
{
390390
var logicalUnits = delta.X / logicalScrollItemSize.X;
391391
delta = delta.WithX(delta.X - logicalUnits * logicalScrollItemSize.X);
392-
dx = logicalUnits * scrollable!.ScrollSize.Width;
392+
dx = logicalUnits;
393393
}
394394
else
395395
dx = delta.X;

0 commit comments

Comments
 (0)