From 1cc29c7e8d24c0d326c741f0488c92b95ac6b718 Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Fri, 20 Jan 2023 09:35:37 +0000 Subject: [PATCH] set pull and pich gestures to handled when handled by handler --- samples/ControlCatalog/Pages/GesturePage.cs | 35 ++++++++++++------- .../PinchGestureRecognizer.cs | 5 ++- .../PullGestureRecognizer.cs | 6 +++- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/samples/ControlCatalog/Pages/GesturePage.cs b/samples/ControlCatalog/Pages/GesturePage.cs index ee10f21317..0bb8f38219 100644 --- a/samples/ControlCatalog/Pages/GesturePage.cs +++ b/samples/ControlCatalog/Pages/GesturePage.cs @@ -6,6 +6,7 @@ using Avalonia.Input; using Avalonia.LogicalTree; using Avalonia.Markup.Xaml; using Avalonia.Rendering.Composition; +using Avalonia.Utilities; namespace ControlCatalog.Pages { @@ -53,6 +54,7 @@ namespace ControlCatalog.Pages { _currentScale = 1; compositionVisual.Scale = new Vector3(1,1,1); + compositionVisual.Offset = default; image.InvalidateMeasure(); } }; @@ -100,13 +102,19 @@ namespace ControlCatalog.Pages { InitComposition(control!); - isZooming = true; - if(compositionVisual != null) { var scale = _currentScale * (float)e.Scale; + if (scale <= 1) + { + scale = 1; + compositionVisual.Offset = default; + } + compositionVisual.Scale = new(scale, scale, 1); + + e.Handled = true; } }); @@ -114,8 +122,6 @@ namespace ControlCatalog.Pages { InitComposition(control!); - isZooming = false; - if (compositionVisual != null) { _currentScale = compositionVisual.Scale.X; @@ -126,11 +132,19 @@ namespace ControlCatalog.Pages { InitComposition(control!); - if (compositionVisual != null && !isZooming) + if (compositionVisual != null && _currentScale != 1) { - currentOffset -= new Vector3((float)e.Delta.X, (float)e.Delta.Y, 0); + currentOffset += new Vector3((float)e.Delta.X, (float)e.Delta.Y, 0); + + var currentSize = control.Bounds.Size * _currentScale; + + currentOffset = new Vector3((float)MathUtilities.Clamp(currentOffset.X, 0, currentSize.Width - control.Bounds.Width), + (float)MathUtilities.Clamp(currentOffset.Y, 0, currentSize.Height - control.Bounds.Height), + 0); - compositionVisual.Offset = currentOffset; + compositionVisual.Offset = currentOffset * -1; + + e.Handled = true; } }); } @@ -173,6 +187,8 @@ namespace ControlCatalog.Pages if (ballCompositionVisual != null) { ballCompositionVisual.Offset = defaultOffset + new System.Numerics.Vector3((float)e.Delta.X * 0.4f, (float)e.Delta.Y * 0.4f, 0) * (inverse ? -1 : 1); + + e.Handled = true; } }); @@ -187,11 +203,6 @@ namespace ControlCatalog.Pages void InitComposition(Control control) { - if (ballCompositionVisual != null) - { - return; - } - ballCompositionVisual = ElementComposition.GetElementVisual(ball); if (ballCompositionVisual != null) diff --git a/src/Avalonia.Base/Input/GestureRecognizers/PinchGestureRecognizer.cs b/src/Avalonia.Base/Input/GestureRecognizers/PinchGestureRecognizer.cs index eea7c3b7d1..3b83d0cb87 100644 --- a/src/Avalonia.Base/Input/GestureRecognizers/PinchGestureRecognizer.cs +++ b/src/Avalonia.Base/Input/GestureRecognizers/PinchGestureRecognizer.cs @@ -57,7 +57,10 @@ namespace Avalonia.Input var scale = distance / _initialDistance; - _target?.RaiseEvent(new PinchEventArgs(scale, _origin)); + var pinchEventArgs = new PinchEventArgs(scale, _origin); + _target?.RaiseEvent(pinchEventArgs); + + e.Handled = pinchEventArgs.Handled; } } } diff --git a/src/Avalonia.Base/Input/GestureRecognizers/PullGestureRecognizer.cs b/src/Avalonia.Base/Input/GestureRecognizers/PullGestureRecognizer.cs index 23bab13fc8..991694cc60 100644 --- a/src/Avalonia.Base/Input/GestureRecognizers/PullGestureRecognizer.cs +++ b/src/Avalonia.Base/Input/GestureRecognizers/PullGestureRecognizer.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using Avalonia.Input.GestureRecognizers; namespace Avalonia.Input @@ -88,7 +89,10 @@ namespace Avalonia.Input } _pullInProgress = true; - _target?.RaiseEvent(new PullGestureEventArgs(_gestureId, delta, PullDirection)); + var pullEventArgs = new PullGestureEventArgs(_gestureId, delta, PullDirection); + _target?.RaiseEvent(pullEventArgs); + + e.Handled = pullEventArgs.Handled; } }