Browse Source

Convert ScrollGestureRecognizer to an unstyled element

Added missing binding of IsScrollInertiaEnabled in the simple theme
pull/10803/head
Tom Edwards 4 years ago
parent
commit
e3c6ee0da3
  1. 53
      src/Avalonia.Base/Input/GestureRecognizers/ScrollGestureRecognizer.cs
  2. 2
      src/Avalonia.Themes.Fluent/Controls/ScrollViewer.xaml
  3. 3
      src/Avalonia.Themes.Simple/Controls/ScrollViewer.xaml

53
src/Avalonia.Base/Input/GestureRecognizers/ScrollGestureRecognizer.cs

@ -4,14 +4,17 @@ using Avalonia.Threading;
namespace Avalonia.Input.GestureRecognizers namespace Avalonia.Input.GestureRecognizers
{ {
public class ScrollGestureRecognizer public class ScrollGestureRecognizer : AvaloniaObject, IGestureRecognizer
: StyledElement, // It's not an "element" in any way, shape or form, but TemplateBinding refuse to work otherwise
IGestureRecognizer
{ {
// Pixels per second speed that is considered to be the stop of inertial scroll // Pixels per second speed that is considered to be the stop of inertial scroll
internal const double InertialScrollSpeedEnd = 5; internal const double InertialScrollSpeedEnd = 5;
public const double InertialResistance = 0.15; public const double InertialResistance = 0.15;
private bool _canHorizontallyScroll;
private bool _canVerticallyScroll;
private bool _isScrollInertiaEnabled;
private int _scrollStartDistance = 30;
private bool _scrolling; private bool _scrolling;
private Point _trackedRootPoint; private Point _trackedRootPoint;
private IPointer? _tracking; private IPointer? _tracking;
@ -28,34 +31,39 @@ namespace Avalonia.Input.GestureRecognizers
/// <summary> /// <summary>
/// Defines the <see cref="CanHorizontallyScroll"/> property. /// Defines the <see cref="CanHorizontallyScroll"/> property.
/// </summary> /// </summary>
public static readonly StyledProperty<bool> CanHorizontallyScrollProperty = public static readonly DirectProperty<ScrollGestureRecognizer, bool> CanHorizontallyScrollProperty =
AvaloniaProperty.Register<ScrollGestureRecognizer, bool>(nameof(CanHorizontallyScroll)); AvaloniaProperty.RegisterDirect<ScrollGestureRecognizer, bool>(nameof(CanHorizontallyScroll),
o => o.CanHorizontallyScroll, (o, v) => o.CanHorizontallyScroll = v);
/// <summary> /// <summary>
/// Defines the <see cref="CanVerticallyScroll"/> property. /// Defines the <see cref="CanVerticallyScroll"/> property.
/// </summary> /// </summary>
public static readonly StyledProperty<bool> CanVerticallyScrollProperty = public static readonly DirectProperty<ScrollGestureRecognizer, bool> CanVerticallyScrollProperty =
AvaloniaProperty.Register<ScrollGestureRecognizer, bool>(nameof(CanVerticallyScroll)); AvaloniaProperty.RegisterDirect<ScrollGestureRecognizer, bool>(nameof(CanVerticallyScroll),
o => o.CanVerticallyScroll, (o, v) => o.CanVerticallyScroll = v);
/// <summary> /// <summary>
/// Defines the <see cref="IsScrollInertiaEnabled"/> property. /// Defines the <see cref="IsScrollInertiaEnabled"/> property.
/// </summary> /// </summary>
public static readonly StyledProperty<bool> IsScrollInertiaEnabledProperty = public static readonly DirectProperty<ScrollGestureRecognizer, bool> IsScrollInertiaEnabledProperty =
AvaloniaProperty.Register<ScrollGestureRecognizer, bool>(nameof(IsScrollInertiaEnabled)); AvaloniaProperty.RegisterDirect<ScrollGestureRecognizer, bool>(nameof(IsScrollInertiaEnabled),
o => o.IsScrollInertiaEnabled, (o,v) => o.IsScrollInertiaEnabled = v);
/// <summary> /// <summary>
/// Defines the <see cref="ScrollStartDistance"/> property. /// Defines the <see cref="ScrollStartDistance"/> property.
/// </summary> /// </summary>
public static readonly StyledProperty<int> ScrollStartDistanceProperty = public static readonly DirectProperty<ScrollGestureRecognizer, int> ScrollStartDistanceProperty =
AvaloniaProperty.Register<ScrollGestureRecognizer, int>(nameof(ScrollStartDistance), 30); AvaloniaProperty.RegisterDirect<ScrollGestureRecognizer, int>(nameof(ScrollStartDistance),
o => o.ScrollStartDistance, (o, v) => o.ScrollStartDistance = v,
unsetValue: 30);
/// <summary> /// <summary>
/// Gets or sets a value indicating whether the content can be scrolled horizontally. /// Gets or sets a value indicating whether the content can be scrolled horizontally.
/// </summary> /// </summary>
public bool CanHorizontallyScroll public bool CanHorizontallyScroll
{ {
get => GetValue(CanHorizontallyScrollProperty); get => _canHorizontallyScroll;
set => SetValue(CanHorizontallyScrollProperty, value); set => SetAndRaise(CanHorizontallyScrollProperty, ref _canHorizontallyScroll, value);
} }
/// <summary> /// <summary>
@ -63,17 +71,17 @@ namespace Avalonia.Input.GestureRecognizers
/// </summary> /// </summary>
public bool CanVerticallyScroll public bool CanVerticallyScroll
{ {
get => GetValue(CanVerticallyScrollProperty); get => _canVerticallyScroll;
set => SetValue(CanVerticallyScrollProperty, value); set => SetAndRaise(CanVerticallyScrollProperty, ref _canVerticallyScroll, value);
} }
/// <summary> /// <summary>
/// Gets or sets whether the gesture should include inertia in it's behavior. /// Gets or sets whether the gesture should include inertia in it's behavior.
/// </summary> /// </summary>
public bool IsScrollInertiaEnabled public bool IsScrollInertiaEnabled
{ {
get => GetValue(IsScrollInertiaEnabledProperty); get => _isScrollInertiaEnabled;
set => SetValue(IsScrollInertiaEnabledProperty, value); set => SetAndRaise(IsScrollInertiaEnabledProperty, ref _isScrollInertiaEnabled, value);
} }
/// <summary> /// <summary>
@ -81,10 +89,9 @@ namespace Avalonia.Input.GestureRecognizers
/// </summary> /// </summary>
public int ScrollStartDistance public int ScrollStartDistance
{ {
get => GetValue(ScrollStartDistanceProperty); get => _scrollStartDistance;
set => SetValue(ScrollStartDistanceProperty, value); set => SetAndRaise(ScrollStartDistanceProperty, ref _scrollStartDistance, value);
} }
public void Initialize(IInputElement target, IGestureRecognizerActionsDispatcher actions) public void Initialize(IInputElement target, IGestureRecognizerActionsDispatcher actions)
{ {

2
src/Avalonia.Themes.Fluent/Controls/ScrollViewer.xaml

@ -37,7 +37,7 @@
<ScrollContentPresenter.GestureRecognizers> <ScrollContentPresenter.GestureRecognizers>
<ScrollGestureRecognizer CanHorizontallyScroll="{Binding CanHorizontallyScroll, ElementName=PART_ContentPresenter}" <ScrollGestureRecognizer CanHorizontallyScroll="{Binding CanHorizontallyScroll, ElementName=PART_ContentPresenter}"
CanVerticallyScroll="{Binding CanVerticallyScroll, ElementName=PART_ContentPresenter}" CanVerticallyScroll="{Binding CanVerticallyScroll, ElementName=PART_ContentPresenter}"
IsScrollInertiaEnabled="{TemplateBinding IsScrollInertiaEnabled}" /> IsScrollInertiaEnabled="{Binding IsScrollInertiaEnabled, RelativeSource={RelativeSource TemplatedParent}}" />
</ScrollContentPresenter.GestureRecognizers> </ScrollContentPresenter.GestureRecognizers>
</ScrollContentPresenter> </ScrollContentPresenter>
<ScrollBar Name="PART_HorizontalScrollBar" <ScrollBar Name="PART_HorizontalScrollBar"

3
src/Avalonia.Themes.Simple/Controls/ScrollViewer.xaml

@ -17,7 +17,8 @@
Background="{TemplateBinding Background}"> Background="{TemplateBinding Background}">
<ScrollContentPresenter.GestureRecognizers> <ScrollContentPresenter.GestureRecognizers>
<ScrollGestureRecognizer CanHorizontallyScroll="{Binding CanHorizontallyScroll, ElementName=PART_ContentPresenter}" <ScrollGestureRecognizer CanHorizontallyScroll="{Binding CanHorizontallyScroll, ElementName=PART_ContentPresenter}"
CanVerticallyScroll="{Binding CanVerticallyScroll, ElementName=PART_ContentPresenter}" /> CanVerticallyScroll="{Binding CanVerticallyScroll, ElementName=PART_ContentPresenter}"
IsScrollInertiaEnabled="{Binding IsScrollInertiaEnabled, RelativeSource={RelativeSource TemplatedParent}}" />
</ScrollContentPresenter.GestureRecognizers> </ScrollContentPresenter.GestureRecognizers>
</ScrollContentPresenter> </ScrollContentPresenter>
<ScrollBar Name="PART_HorizontalScrollBar" <ScrollBar Name="PART_HorizontalScrollBar"

Loading…
Cancel
Save