From bde380ebfa630f1d8df216025baf49904073eef8 Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Tue, 17 Jan 2023 20:08:22 +0000 Subject: [PATCH] Add snapping sample to scroll viewer page --- .../Pages/ScrollViewerPage.xaml | 139 +++++++++++++++++- .../Pages/ScrollViewerPage.xaml.cs | 37 +++++ .../Presenters/ScrollContentPresenter.cs | 4 +- 3 files changed, 176 insertions(+), 4 deletions(-) diff --git a/samples/ControlCatalog/Pages/ScrollViewerPage.xaml b/samples/ControlCatalog/Pages/ScrollViewerPage.xaml index 1903e50ed7..0eceb9e07d 100644 --- a/samples/ControlCatalog/Pages/ScrollViewerPage.xaml +++ b/samples/ControlCatalog/Pages/ScrollViewerPage.xaml @@ -3,8 +3,8 @@ xmlns:pages="using:ControlCatalog.Pages" x:Class="ControlCatalog.Pages.ScrollViewerPage" x:DataType="pages:ScrollViewerPageViewModel"> - - Allows for horizontal and vertical content scrolling. + + Allows for horizontal and vertical content scrolling. Supports snapping on touch and pointer wheel scrolling. @@ -31,6 +31,141 @@ Source="/Assets/delicate-arch-896885_640.jpg" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/ControlCatalog/Pages/ScrollViewerPage.xaml.cs b/samples/ControlCatalog/Pages/ScrollViewerPage.xaml.cs index dcd7a88a56..10244af5f3 100644 --- a/samples/ControlCatalog/Pages/ScrollViewerPage.xaml.cs +++ b/samples/ControlCatalog/Pages/ScrollViewerPage.xaml.cs @@ -11,6 +11,9 @@ namespace ControlCatalog.Pages private bool _allowAutoHide; private ScrollBarVisibility _horizontalScrollVisibility; private ScrollBarVisibility _verticalScrollVisibility; + private SnapPointsType _verticalSnapPointsType; + private SnapPointsAlignment _verticalSnapPointsAlignment; + private bool _areSnapPointsRegular; public ScrollViewerPageViewModel() { @@ -22,6 +25,20 @@ namespace ControlCatalog.Pages ScrollBarVisibility.Disabled, }; + AvailableSnapPointsType = new List() + { + SnapPointsType.None, + SnapPointsType.Mandatory, + SnapPointsType.MandatorySingle + }; + + AvailableSnapPointsAlignment = new List() + { + SnapPointsAlignment.Near, + SnapPointsAlignment.Center, + SnapPointsAlignment.Far, + }; + HorizontalScrollVisibility = ScrollBarVisibility.Auto; VerticalScrollVisibility = ScrollBarVisibility.Auto; AllowAutoHide = true; @@ -33,6 +50,12 @@ namespace ControlCatalog.Pages set => this.RaiseAndSetIfChanged(ref _allowAutoHide, value); } + public bool AreSnapPointsRegular + { + get => _areSnapPointsRegular; + set => this.RaiseAndSetIfChanged(ref _areSnapPointsRegular, value); + } + public ScrollBarVisibility HorizontalScrollVisibility { get => _horizontalScrollVisibility; @@ -45,7 +68,21 @@ namespace ControlCatalog.Pages set => this.RaiseAndSetIfChanged(ref _verticalScrollVisibility, value); } + public SnapPointsType VerticalSnapPointsType + { + get => _verticalSnapPointsType; + set => this.RaiseAndSetIfChanged(ref _verticalSnapPointsType, value); + } + + public SnapPointsAlignment VerticalSnapPointsAlignment + { + get => _verticalSnapPointsAlignment; + set => this.RaiseAndSetIfChanged(ref _verticalSnapPointsAlignment, value); + } + public List AvailableVisibility { get; } + public List AvailableSnapPointsType { get; } + public List AvailableSnapPointsAlignment { get; } } public class ScrollViewerPage : UserControl diff --git a/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs b/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs index 2326699c36..3b291841fe 100644 --- a/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs @@ -613,7 +613,7 @@ namespace Avalonia.Controls.Presenters { height = _verticalSnapPoint; } - else + else if(_verticalSnapPoints != null) { double yOffset = Offset.Y; switch (VerticalSnapPointsAlignment) @@ -645,7 +645,7 @@ namespace Avalonia.Controls.Presenters { width = _horizontalSnapPoint; } - else + else if(_horizontalSnapPoints != null) { double xOffset = Offset.X; switch (VerticalSnapPointsAlignment)