Browse Source

only clear held pointer reference if pointer event is raised (#14140)

pull/14153/head
Emmanuel Hansen 2 years ago
committed by GitHub
parent
commit
2c890e9b9c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      src/Avalonia.Base/Input/Gestures.cs

25
src/Avalonia.Base/Input/Gestures.cs

@ -67,7 +67,7 @@ namespace Avalonia.Input
private static readonly WeakReference<object?> s_lastPress = new WeakReference<object?>(null); private static readonly WeakReference<object?> s_lastPress = new WeakReference<object?>(null);
private static Point s_lastPressPoint; private static Point s_lastPressPoint;
private static IPointer? s_lastPointer; private static IPointer? s_lastHeldPointer;
public static readonly RoutedEvent<PinchEventArgs> PinchEvent = public static readonly RoutedEvent<PinchEventArgs> PinchEvent =
RoutedEvent.Register<PinchEventArgs>( RoutedEvent.Register<PinchEventArgs>(
@ -225,17 +225,17 @@ namespace Avalonia.Input
var e = (PointerPressedEventArgs)ev; var e = (PointerPressedEventArgs)ev;
var visual = (Visual)ev.Source; var visual = (Visual)ev.Source;
if(s_lastPointer != null) if(s_lastHeldPointer != null)
{ {
if(s_isHolding && ev.Source is Interactive i) 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?.Cancel();
s_holdCancellationToken?.Dispose(); s_holdCancellationToken?.Dispose();
s_holdCancellationToken = null; s_holdCancellationToken = null;
s_lastPointer = null; s_lastHeldPointer = null;
} }
s_isHolding = false; s_isHolding = false;
@ -244,7 +244,7 @@ namespace Avalonia.Input
{ {
s_isDoubleTapped = false; s_isDoubleTapped = false;
s_lastPress.SetTarget(ev.Source); s_lastPress.SetTarget(ev.Source);
s_lastPointer = e.Pointer; s_lastHeldPointer = e.Pointer;
s_lastPressPoint = e.GetPosition((Visual)ev.Source); s_lastPressPoint = e.GetPosition((Visual)ev.Source);
s_holdCancellationToken = new CancellationTokenSource(); s_holdCancellationToken = new CancellationTokenSource();
var token = s_holdCancellationToken.Token; var token = s_holdCancellationToken.Token;
@ -257,7 +257,7 @@ namespace Avalonia.Input
if (!token.IsCancellationRequested && e.Source is InputElement i && GetIsHoldingEnabled(i) && (e.Pointer.Type != PointerType.Mouse || GetIsHoldWithMouseEnabled(i))) if (!token.IsCancellationRequested && e.Source is InputElement i && GetIsHoldingEnabled(i) && (e.Pointer.Type != PointerType.Mouse || GetIsHoldWithMouseEnabled(i)))
{ {
s_isHolding = true; 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); }, settings.HoldWaitDuration);
} }
@ -281,7 +281,7 @@ namespace Avalonia.Input
{ {
var e = (PointerReleasedEventArgs)ev; var e = (PointerReleasedEventArgs)ev;
if (s_lastPress.TryGetTarget(out var target) && if (s_lastPress.TryGetTarget(out var target) &&
target == e.Source && target == e.Source &&
e.InitialPressMouseButton is MouseButton.Left or MouseButton.Right && e.InitialPressMouseButton is MouseButton.Left or MouseButton.Right &&
e.Source is Interactive i) e.Source is Interactive i)
@ -294,10 +294,10 @@ namespace Avalonia.Input
if (tapRect.ContainsExclusive(point.Position)) if (tapRect.ContainsExclusive(point.Position))
{ {
if(s_isHolding) if (s_isHolding)
{ {
s_isHolding = false; 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) else if (e.InitialPressMouseButton == MouseButton.Right)
{ {
@ -310,12 +310,12 @@ namespace Avalonia.Input
i.RaiseEvent(new TappedEventArgs(TappedEvent, e)); i.RaiseEvent(new TappedEventArgs(TappedEvent, e));
} }
} }
s_lastHeldPointer = null;
} }
s_holdCancellationToken?.Cancel(); s_holdCancellationToken?.Cancel();
s_holdCancellationToken?.Dispose(); s_holdCancellationToken?.Dispose();
s_holdCancellationToken = null; s_holdCancellationToken = null;
s_lastPointer = null;
} }
} }
@ -326,7 +326,7 @@ namespace Avalonia.Input
var e = (PointerEventArgs)ev; var e = (PointerEventArgs)ev;
if (s_lastPress.TryGetTarget(out var target)) 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 point = e.GetCurrentPoint((Visual)target);
var settings = ((IInputRoot?)i.GetVisualRoot())?.PlatformSettings; var settings = ((IInputRoot?)i.GetVisualRoot())?.PlatformSettings;
@ -341,7 +341,8 @@ namespace Avalonia.Input
if (s_isHolding) 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;
} }
} }
} }

Loading…
Cancel
Save