Browse Source

Merge pull request #7964 from AvaloniaUI/fixes/7840-datetimepicker-touch-scroll

Fix Date/Time picker touch scrolling
pull/7967/head
Steven Kirk 5 years ago
committed by GitHub
parent
commit
a613fc289d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      src/Avalonia.Controls/DateTimePickers/DateTimePickerPanel.cs
  2. 4
      src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs

31
src/Avalonia.Controls/DateTimePickers/DateTimePickerPanel.cs

@ -1,7 +1,9 @@
using System; using System;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Avalonia.Controls.Presenters;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Input.GestureRecognizers;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.VisualTree; using Avalonia.VisualTree;
@ -60,6 +62,7 @@ namespace Avalonia.Controls.Primitives
private Vector _offset; private Vector _offset;
private bool _hasInit; private bool _hasInit;
private bool _suppressUpdateOffset; private bool _suppressUpdateOffset;
private ScrollContentPresenter? _parentScroller;
public DateTimePickerPanel() public DateTimePickerPanel()
{ {
@ -255,6 +258,8 @@ namespace Avalonia.Controls.Primitives
_suppressUpdateOffset = true; _suppressUpdateOffset = true;
SelectedValue = (int)newSel * Increment + MinimumValue; SelectedValue = (int)newSel * Increment + MinimumValue;
_suppressUpdateOffset = false; _suppressUpdateOffset = false;
System.Diagnostics.Debug.WriteLine($"Offset: {_offset} ItemHeight: {ItemHeight}");
} }
} }
@ -270,7 +275,7 @@ namespace Avalonia.Controls.Primitives
public Size Extent => _extent; public Size Extent => _extent;
public Size Viewport => new Size(0, ItemHeight); public Size Viewport => Bounds.Size;
public event EventHandler? ScrollInvalidated; public event EventHandler? ScrollInvalidated;
@ -341,6 +346,20 @@ namespace Avalonia.Controls.Primitives
return 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) protected override void OnKeyDown(KeyEventArgs e)
{ {
switch (e.Key) switch (e.Key)
@ -554,5 +573,15 @@ namespace Avalonia.Controls.Primitives
{ {
ScrollInvalidated?.Invoke(this, 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
src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs

@ -389,7 +389,7 @@ namespace Avalonia.Controls.Presenters
{ {
var logicalUnits = delta.Y / logicalScrollItemSize.Y; var logicalUnits = delta.Y / logicalScrollItemSize.Y;
delta = delta.WithY(delta.Y - logicalUnits * logicalScrollItemSize.Y); delta = delta.WithY(delta.Y - logicalUnits * logicalScrollItemSize.Y);
dy = logicalUnits * scrollable!.ScrollSize.Height; dy = logicalUnits;
} }
else else
dy = delta.Y; dy = delta.Y;
@ -407,7 +407,7 @@ namespace Avalonia.Controls.Presenters
{ {
var logicalUnits = delta.X / logicalScrollItemSize.X; var logicalUnits = delta.X / logicalScrollItemSize.X;
delta = delta.WithX(delta.X - logicalUnits * logicalScrollItemSize.X); delta = delta.WithX(delta.X - logicalUnits * logicalScrollItemSize.X);
dx = logicalUnits * scrollable!.ScrollSize.Width; dx = logicalUnits;
} }
else else
dx = delta.X; dx = delta.X;

Loading…
Cancel
Save