From 78abd6d4b588a2913441114219969085c34ce01b Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Tue, 24 Jan 2023 20:03:02 +0000 Subject: [PATCH] Add IsScrollInertiaEnabled attached property to scrollviewer --- .../Pages/ScrollViewerPage.xaml | 2 ++ .../Pages/ScrollViewerPage.xaml.cs | 8 +++++++ .../ScrollGestureRecognizer.cs | 24 +++++++++++++++++-- src/Avalonia.Controls/ScrollViewer.cs | 17 +++++++++++++ .../Controls/ScrollViewer.xaml | 3 ++- 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/samples/ControlCatalog/Pages/ScrollViewerPage.xaml b/samples/ControlCatalog/Pages/ScrollViewerPage.xaml index 1903e50ed7..5042d7823b 100644 --- a/samples/ControlCatalog/Pages/ScrollViewerPage.xaml +++ b/samples/ControlCatalog/Pages/ScrollViewerPage.xaml @@ -9,6 +9,7 @@ + @@ -24,6 +25,7 @@ diff --git a/samples/ControlCatalog/Pages/ScrollViewerPage.xaml.cs b/samples/ControlCatalog/Pages/ScrollViewerPage.xaml.cs index dcd7a88a56..a097f1f951 100644 --- a/samples/ControlCatalog/Pages/ScrollViewerPage.xaml.cs +++ b/samples/ControlCatalog/Pages/ScrollViewerPage.xaml.cs @@ -9,6 +9,7 @@ namespace ControlCatalog.Pages public class ScrollViewerPageViewModel : ViewModelBase { private bool _allowAutoHide; + private bool _enableInertia; private ScrollBarVisibility _horizontalScrollVisibility; private ScrollBarVisibility _verticalScrollVisibility; @@ -25,6 +26,7 @@ namespace ControlCatalog.Pages HorizontalScrollVisibility = ScrollBarVisibility.Auto; VerticalScrollVisibility = ScrollBarVisibility.Auto; AllowAutoHide = true; + EnableInertia = true; } public bool AllowAutoHide @@ -33,6 +35,12 @@ namespace ControlCatalog.Pages set => this.RaiseAndSetIfChanged(ref _allowAutoHide, value); } + public bool EnableInertia + { + get => _enableInertia; + set => this.RaiseAndSetIfChanged(ref _enableInertia, value); + } + public ScrollBarVisibility HorizontalScrollVisibility { get => _horizontalScrollVisibility; diff --git a/src/Avalonia.Base/Input/GestureRecognizers/ScrollGestureRecognizer.cs b/src/Avalonia.Base/Input/GestureRecognizers/ScrollGestureRecognizer.cs index 790439245a..fdab2df0bf 100644 --- a/src/Avalonia.Base/Input/GestureRecognizers/ScrollGestureRecognizer.cs +++ b/src/Avalonia.Base/Input/GestureRecognizers/ScrollGestureRecognizer.cs @@ -23,7 +23,8 @@ namespace Avalonia.Input.GestureRecognizers // Movement per second private Vector _inertia; private ulong? _lastMoveTimestamp; - + private bool _isScrollInertiaEnabled; + /// /// Defines the property. /// @@ -42,6 +43,15 @@ namespace Avalonia.Input.GestureRecognizers o => o.CanVerticallyScroll, (o, v) => o.CanVerticallyScroll = v); + /// + /// Defines the property. + /// + public static readonly DirectProperty IsScrollInertiaEnabledProperty = + AvaloniaProperty.RegisterDirect( + nameof(IsScrollInertiaEnabled), + o => o.IsScrollInertiaEnabled, + (o, v) => o.IsScrollInertiaEnabled = v); + /// /// Defines the property. /// @@ -68,6 +78,15 @@ namespace Avalonia.Input.GestureRecognizers get => _canVerticallyScroll; set => SetAndRaise(CanVerticallyScrollProperty, ref _canVerticallyScroll, value); } + + /// + /// Gets or sets whether the gesture should include inertia in it's behavior. + /// + public bool IsScrollInertiaEnabled + { + get => _isScrollInertiaEnabled; + set => SetAndRaise(IsScrollInertiaEnabledProperty, ref _isScrollInertiaEnabled, value); + } /// /// Gets or sets a value indicating the distance the pointer moves before scrolling is started @@ -168,7 +187,8 @@ namespace Avalonia.Input.GestureRecognizers if (_inertia == default || e.Timestamp == 0 || _lastMoveTimestamp == 0 - || e.Timestamp - _lastMoveTimestamp > 200) + || e.Timestamp - _lastMoveTimestamp > 200 + || !IsScrollInertiaEnabled) EndGesture(); else { diff --git a/src/Avalonia.Controls/ScrollViewer.cs b/src/Avalonia.Controls/ScrollViewer.cs index 503187e2d3..d16cd209c0 100644 --- a/src/Avalonia.Controls/ScrollViewer.cs +++ b/src/Avalonia.Controls/ScrollViewer.cs @@ -193,6 +193,14 @@ namespace Avalonia.Controls nameof(IsScrollChainingEnabled), defaultValue: true); + /// + /// Defines the property. + /// + public static readonly AttachedProperty IsScrollInertiaEnabledProperty = + AvaloniaProperty.RegisterAttached( + nameof(IsScrollInertiaEnabled), + defaultValue: true); + /// /// Defines the event. /// @@ -444,6 +452,15 @@ namespace Avalonia.Controls set => SetValue(IsScrollChainingEnabledProperty, value); } + /// + /// Gets or sets whether scroll gestures should include inertia in their behavior and value. + /// + public bool IsScrollInertiaEnabled + { + get => GetValue(IsScrollInertiaEnabledProperty); + set => SetValue(IsScrollInertiaEnabledProperty, value); + } + /// /// Scrolls the content up one line. /// diff --git a/src/Avalonia.Themes.Fluent/Controls/ScrollViewer.xaml b/src/Avalonia.Themes.Fluent/Controls/ScrollViewer.xaml index 71df4d419f..15cd9428f3 100644 --- a/src/Avalonia.Themes.Fluent/Controls/ScrollViewer.xaml +++ b/src/Avalonia.Themes.Fluent/Controls/ScrollViewer.xaml @@ -39,7 +39,8 @@ IsScrollChainingEnabled="{TemplateBinding IsScrollChainingEnabled}"> + CanVerticallyScroll="{TemplateBinding CanVerticallyScroll}" + IsScrollInertiaEnabled="{TemplateBinding IsScrollInertiaEnabled}"/>