From 7e7a70afb1ea7f1fa7ca3fccb8e27dcd60e4bb34 Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Mon, 8 Jan 2024 08:51:23 +0000 Subject: [PATCH] only clear held pointer reference if pointer event is raised --- src/Avalonia.Base/Input/Gestures.cs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Avalonia.Base/Input/Gestures.cs b/src/Avalonia.Base/Input/Gestures.cs index 53562d97c64..40981711a3c 100644 --- a/src/Avalonia.Base/Input/Gestures.cs +++ b/src/Avalonia.Base/Input/Gestures.cs @@ -67,7 +67,7 @@ public static class Gestures private static readonly WeakReference s_lastPress = new WeakReference(null); private static Point s_lastPressPoint; - private static IPointer? s_lastPointer; + private static IPointer? s_lastHeldPointer; public static readonly RoutedEvent PinchEvent = RoutedEvent.Register( @@ -225,17 +225,17 @@ private static void PointerPressed(RoutedEventArgs ev) var e = (PointerPressedEventArgs)ev; var visual = (Visual)ev.Source; - if(s_lastPointer != null) + if(s_lastHeldPointer != null) { if(s_isHolding && ev.Source is Interactive i) { - i.RaiseEvent(new HoldingRoutedEventArgs(HoldingState.Cancelled, s_lastPressPoint, s_lastPointer.Type, e)); + i.RaiseEvent(new HoldingRoutedEventArgs(HoldingState.Cancelled, s_lastPressPoint, s_lastHeldPointer.Type, e)); } s_holdCancellationToken?.Cancel(); s_holdCancellationToken?.Dispose(); s_holdCancellationToken = null; - s_lastPointer = null; + s_lastHeldPointer = null; } s_isHolding = false; @@ -244,7 +244,7 @@ private static void PointerPressed(RoutedEventArgs ev) { s_isDoubleTapped = false; s_lastPress.SetTarget(ev.Source); - s_lastPointer = e.Pointer; + s_lastHeldPointer = e.Pointer; s_lastPressPoint = e.GetPosition((Visual)ev.Source); s_holdCancellationToken = new CancellationTokenSource(); var token = s_holdCancellationToken.Token; @@ -257,7 +257,7 @@ private static void PointerPressed(RoutedEventArgs ev) if (!token.IsCancellationRequested && e.Source is InputElement i && GetIsHoldingEnabled(i) && (e.Pointer.Type != PointerType.Mouse || GetIsHoldWithMouseEnabled(i))) { s_isHolding = true; - i.RaiseEvent(new HoldingRoutedEventArgs(HoldingState.Started, s_lastPressPoint, s_lastPointer.Type, e)); + i.RaiseEvent(new HoldingRoutedEventArgs(HoldingState.Started, s_lastPressPoint, s_lastHeldPointer.Type, e)); } }, settings.HoldWaitDuration); } @@ -281,7 +281,7 @@ private static void PointerReleased(RoutedEventArgs ev) { var e = (PointerReleasedEventArgs)ev; - if (s_lastPress.TryGetTarget(out var target) && + if (s_lastPress.TryGetTarget(out var target) && target == e.Source && e.InitialPressMouseButton is MouseButton.Left or MouseButton.Right && e.Source is Interactive i) @@ -294,10 +294,10 @@ e.InitialPressMouseButton is MouseButton.Left or MouseButton.Right && if (tapRect.ContainsExclusive(point.Position)) { - if(s_isHolding) + if (s_isHolding) { s_isHolding = false; - i.RaiseEvent(new HoldingRoutedEventArgs(HoldingState.Completed, s_lastPressPoint, s_lastPointer!.Type, e)); + i.RaiseEvent(new HoldingRoutedEventArgs(HoldingState.Completed, s_lastPressPoint, s_lastHeldPointer!.Type, e)); } else if (e.InitialPressMouseButton == MouseButton.Right) { @@ -310,12 +310,12 @@ e.InitialPressMouseButton is MouseButton.Left or MouseButton.Right && i.RaiseEvent(new TappedEventArgs(TappedEvent, e)); } } + s_lastHeldPointer = null; } s_holdCancellationToken?.Cancel(); s_holdCancellationToken?.Dispose(); s_holdCancellationToken = null; - s_lastPointer = null; } } @@ -326,7 +326,7 @@ private static void PointerMoved(RoutedEventArgs ev) var e = (PointerEventArgs)ev; if (s_lastPress.TryGetTarget(out var target)) { - if (e.Pointer == s_lastPointer && ev.Source is Interactive i) + if (e.Pointer == s_lastHeldPointer && ev.Source is Interactive i) { var point = e.GetCurrentPoint((Visual)target); var settings = ((IInputRoot?)i.GetVisualRoot())?.PlatformSettings; @@ -341,7 +341,8 @@ private static void PointerMoved(RoutedEventArgs ev) if (s_isHolding) { - i.RaiseEvent(new HoldingRoutedEventArgs(HoldingState.Cancelled, s_lastPressPoint, s_lastPointer!.Type, e)); + i.RaiseEvent(new HoldingRoutedEventArgs(HoldingState.Cancelled, s_lastPressPoint, s_lastHeldPointer!.Type, e)); + s_lastHeldPointer = null; } } }