From e3c6ee0da3ee703e32c956d1521a37a7e0c86165 Mon Sep 17 00:00:00 2001 From: Tom Edwards Date: Fri, 31 Mar 2023 21:08:06 +0200 Subject: [PATCH] Convert ScrollGestureRecognizer to an unstyled element Added missing binding of IsScrollInertiaEnabled in the simple theme --- .../ScrollGestureRecognizer.cs | 53 +++++++++++-------- .../Controls/ScrollViewer.xaml | 2 +- .../Controls/ScrollViewer.xaml | 3 +- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/Avalonia.Base/Input/GestureRecognizers/ScrollGestureRecognizer.cs b/src/Avalonia.Base/Input/GestureRecognizers/ScrollGestureRecognizer.cs index 1ad2f292ca..b510d44e63 100644 --- a/src/Avalonia.Base/Input/GestureRecognizers/ScrollGestureRecognizer.cs +++ b/src/Avalonia.Base/Input/GestureRecognizers/ScrollGestureRecognizer.cs @@ -4,14 +4,17 @@ using Avalonia.Threading; namespace Avalonia.Input.GestureRecognizers { - public class ScrollGestureRecognizer - : StyledElement, // It's not an "element" in any way, shape or form, but TemplateBinding refuse to work otherwise - IGestureRecognizer + public class ScrollGestureRecognizer : AvaloniaObject, IGestureRecognizer { // Pixels per second speed that is considered to be the stop of inertial scroll internal const double InertialScrollSpeedEnd = 5; public const double InertialResistance = 0.15; + private bool _canHorizontallyScroll; + private bool _canVerticallyScroll; + private bool _isScrollInertiaEnabled; + private int _scrollStartDistance = 30; + private bool _scrolling; private Point _trackedRootPoint; private IPointer? _tracking; @@ -28,34 +31,39 @@ namespace Avalonia.Input.GestureRecognizers /// /// Defines the property. /// - public static readonly StyledProperty CanHorizontallyScrollProperty = - AvaloniaProperty.Register(nameof(CanHorizontallyScroll)); + public static readonly DirectProperty CanHorizontallyScrollProperty = + AvaloniaProperty.RegisterDirect(nameof(CanHorizontallyScroll), + o => o.CanHorizontallyScroll, (o, v) => o.CanHorizontallyScroll = v); /// /// Defines the property. /// - public static readonly StyledProperty CanVerticallyScrollProperty = - AvaloniaProperty.Register(nameof(CanVerticallyScroll)); + public static readonly DirectProperty CanVerticallyScrollProperty = + AvaloniaProperty.RegisterDirect(nameof(CanVerticallyScroll), + o => o.CanVerticallyScroll, (o, v) => o.CanVerticallyScroll = v); /// /// Defines the property. /// - public static readonly StyledProperty IsScrollInertiaEnabledProperty = - AvaloniaProperty.Register(nameof(IsScrollInertiaEnabled)); + public static readonly DirectProperty IsScrollInertiaEnabledProperty = + AvaloniaProperty.RegisterDirect(nameof(IsScrollInertiaEnabled), + o => o.IsScrollInertiaEnabled, (o,v) => o.IsScrollInertiaEnabled = v); /// /// Defines the property. /// - public static readonly StyledProperty ScrollStartDistanceProperty = - AvaloniaProperty.Register(nameof(ScrollStartDistance), 30); - + public static readonly DirectProperty ScrollStartDistanceProperty = + AvaloniaProperty.RegisterDirect(nameof(ScrollStartDistance), + o => o.ScrollStartDistance, (o, v) => o.ScrollStartDistance = v, + unsetValue: 30); + /// /// Gets or sets a value indicating whether the content can be scrolled horizontally. /// public bool CanHorizontallyScroll { - get => GetValue(CanHorizontallyScrollProperty); - set => SetValue(CanHorizontallyScrollProperty, value); + get => _canHorizontallyScroll; + set => SetAndRaise(CanHorizontallyScrollProperty, ref _canHorizontallyScroll, value); } /// @@ -63,17 +71,17 @@ namespace Avalonia.Input.GestureRecognizers /// public bool CanVerticallyScroll { - get => GetValue(CanVerticallyScrollProperty); - set => SetValue(CanVerticallyScrollProperty, value); + 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 => GetValue(IsScrollInertiaEnabledProperty); - set => SetValue(IsScrollInertiaEnabledProperty, value); + get => _isScrollInertiaEnabled; + set => SetAndRaise(IsScrollInertiaEnabledProperty, ref _isScrollInertiaEnabled, value); } /// @@ -81,10 +89,9 @@ namespace Avalonia.Input.GestureRecognizers /// public int ScrollStartDistance { - get => GetValue(ScrollStartDistanceProperty); - set => SetValue(ScrollStartDistanceProperty, value); - } - + get => _scrollStartDistance; + set => SetAndRaise(ScrollStartDistanceProperty, ref _scrollStartDistance, value); + } public void Initialize(IInputElement target, IGestureRecognizerActionsDispatcher actions) { diff --git a/src/Avalonia.Themes.Fluent/Controls/ScrollViewer.xaml b/src/Avalonia.Themes.Fluent/Controls/ScrollViewer.xaml index 04b8f80f82..1d9815713c 100644 --- a/src/Avalonia.Themes.Fluent/Controls/ScrollViewer.xaml +++ b/src/Avalonia.Themes.Fluent/Controls/ScrollViewer.xaml @@ -37,7 +37,7 @@ + IsScrollInertiaEnabled="{Binding IsScrollInertiaEnabled, RelativeSource={RelativeSource TemplatedParent}}" /> + CanVerticallyScroll="{Binding CanVerticallyScroll, ElementName=PART_ContentPresenter}" + IsScrollInertiaEnabled="{Binding IsScrollInertiaEnabled, RelativeSource={RelativeSource TemplatedParent}}" />