Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only clear held pointer reference if pointer event is raised #14140

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/Avalonia.Base/Input/Gestures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static class Gestures

private static readonly WeakReference<object?> s_lastPress = new WeakReference<object?>(null);
private static Point s_lastPressPoint;
private static IPointer? s_lastPointer;
private static IPointer? s_lastHeldPointer;

public static readonly RoutedEvent<PinchEventArgs> PinchEvent =
RoutedEvent.Register<PinchEventArgs>(
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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)
Expand All @@ -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)
{
Expand All @@ -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;
}
}

Expand All @@ -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;
Expand All @@ -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;
}
}
}
Expand Down
Loading