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

[WinUI] Cache gesture event subscriptions #21959

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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Graphics;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Imaging;
Expand All @@ -23,6 +22,8 @@ class GesturePlatformManager : IDisposable
FrameworkElement? _control;
VisualElement? _element;

SubscriptionFlags _subscriptionFlags = SubscriptionFlags.None;

bool _isDisposed;
bool _isPanning;
bool _isSwiping;
Expand Down Expand Up @@ -310,24 +311,61 @@ void ClearContainerEventHandlers()
{
if (_container != null)
{
_container.DragStarting -= HandleDragStarting;
_container.DropCompleted -= HandleDropCompleted;
_container.DragOver -= HandleDragOver;
_container.Drop -= HandleDrop;
_container.Tapped -= OnTap;
_container.DoubleTapped -= OnTap;
_container.ManipulationDelta -= OnManipulationDelta;
_container.ManipulationStarted -= OnManipulationStarted;
_container.ManipulationCompleted -= OnManipulationCompleted;
_container.PointerPressed -= OnPointerPressed;
_container.PointerExited -= OnPointerExited;
_container.PointerReleased -= OnPointerReleased;
_container.PointerCanceled -= OnPointerCanceled;
_container.PointerEntered -= OnPgrPointerEntered;
_container.PointerExited -= OnPgrPointerExited;
_container.PointerMoved -= OnPgrPointerMoved;
_container.PointerPressed -= OnPgrPointerPressed;
_container.PointerReleased -= OnPgrPointerReleased;
if ((_subscriptionFlags & SubscriptionFlags.ContainerDragEventsSubscribed) != 0)
{
_subscriptionFlags &= ~SubscriptionFlags.ContainerDragEventsSubscribed;

_container.DragStarting -= HandleDragStarting;
_container.DropCompleted -= HandleDropCompleted;
}

if ((_subscriptionFlags & SubscriptionFlags.ContainerDropEventsSubscribed) != 0)
{
_subscriptionFlags &= ~SubscriptionFlags.ContainerDropEventsSubscribed;

_container.DragOver -= HandleDragOver;
_container.Drop -= HandleDrop;
_container.DragLeave -= HandleDragLeave;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this event WAS NOT unsubscribed at all.

}

if ((_subscriptionFlags & SubscriptionFlags.ContainerTapAndRightTabEventSubscribed) != 0)
{
_subscriptionFlags &= ~SubscriptionFlags.ContainerTapAndRightTabEventSubscribed;

_container.Tapped -= OnTap;
_container.RightTapped -= OnTap;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this event WAS NOT unsubscribed at all.

}

if ((_subscriptionFlags & SubscriptionFlags.ContainerDoubleTapEventSubscribed) != 0)
{
_subscriptionFlags &= ~SubscriptionFlags.ContainerDoubleTapEventSubscribed;

_container.DoubleTapped -= OnTap;
}

if ((_subscriptionFlags & SubscriptionFlags.ContainerPgrPointerEventsSubscribed) != 0)
{
_subscriptionFlags &= ~SubscriptionFlags.ContainerPgrPointerEventsSubscribed;

_container.PointerEntered -= OnPgrPointerEntered;
_container.PointerExited -= OnPgrPointerExited;
_container.PointerMoved -= OnPgrPointerMoved;
_container.PointerPressed -= OnPgrPointerPressed;
_container.PointerReleased -= OnPgrPointerReleased;
}

if ((_subscriptionFlags & SubscriptionFlags.ContainerManipulationAndPointerEventsSubscribed) != 0)
{
_subscriptionFlags &= ~SubscriptionFlags.ContainerManipulationAndPointerEventsSubscribed;

_container.ManipulationDelta -= OnManipulationDelta;
_container.ManipulationStarted -= OnManipulationStarted;
_container.ManipulationCompleted -= OnManipulationCompleted;
_container.PointerPressed -= OnPointerPressed;
_container.PointerExited -= OnPointerExited;
_container.PointerReleased -= OnPointerReleased;
_container.PointerCanceled -= OnPointerCanceled;
}
}
}

Expand Down Expand Up @@ -688,12 +726,16 @@ void UpdateDragAndDropGestureRecognizers()

if (canDrag)
{
_subscriptionFlags |= SubscriptionFlags.ContainerDragEventsSubscribed;

_container.DragStarting += HandleDragStarting;
_container.DropCompleted += HandleDropCompleted;
}

if (allowDrop)
{
_subscriptionFlags |= SubscriptionFlags.ContainerDropEventsSubscribed;

_container.DragOver += HandleDragOver;
_container.Drop += HandleDrop;
_container.DragLeave += HandleDragLeave;
Expand All @@ -718,6 +760,8 @@ void UpdatingGestureRecognizers()
if (gestures.HasAnyGesturesFor<TapGestureRecognizer>(g => g.NumberOfTapsRequired == 1)
|| children?.GetChildGesturesFor<TapGestureRecognizer>(g => g.NumberOfTapsRequired == 1).Any() == true)
{
_subscriptionFlags |= SubscriptionFlags.ContainerTapAndRightTabEventSubscribed;

_container.Tapped += OnTap;
_container.RightTapped += OnTap;
}
Expand All @@ -732,6 +776,8 @@ void UpdatingGestureRecognizers()
if (gestures.HasAnyGesturesFor<TapGestureRecognizer>(g => g.NumberOfTapsRequired == 1 || g.NumberOfTapsRequired == 2)
|| children?.GetChildGesturesFor<TapGestureRecognizer>(g => g.NumberOfTapsRequired == 1 || g.NumberOfTapsRequired == 2).Any() == true)
{
_subscriptionFlags |= SubscriptionFlags.ContainerDoubleTapEventSubscribed;

_container.DoubleTapped += OnTap;
}
else
Expand All @@ -742,6 +788,7 @@ void UpdatingGestureRecognizers()
}
}

_subscriptionFlags |= SubscriptionFlags.ContainerPgrPointerEventsSubscribed;
_container.PointerEntered += OnPgrPointerEntered;
_container.PointerExited += OnPgrPointerExited;
_container.PointerMoved += OnPgrPointerMoved;
Expand Down Expand Up @@ -769,6 +816,7 @@ void UpdatingGestureRecognizers()
return;
}

_subscriptionFlags |= SubscriptionFlags.ContainerManipulationAndPointerEventsSubscribed;
_container.ManipulationMode = ManipulationModes.Scale | ManipulationModes.TranslateX | ManipulationModes.TranslateY;
_container.ManipulationDelta += OnManipulationDelta;
_container.ManipulationStarted += OnManipulationStarted;
Expand Down Expand Up @@ -796,5 +844,17 @@ DragEventArgs ToDragEventArgs(UI.Xaml.DragEventArgs e, PlatformDragEventArgs pla

return new DragEventArgs(package!, (relativeTo) => GetPosition(relativeTo, e), platformArgs);
}

[Flags]
enum SubscriptionFlags : byte
{
None = 0,
ContainerDragEventsSubscribed = 1,
ContainerDropEventsSubscribed = 1 << 1,
ContainerPgrPointerEventsSubscribed = 1 << 2,
ContainerManipulationAndPointerEventsSubscribed = 1 << 3,
ContainerTapAndRightTabEventSubscribed = 1 << 4,
ContainerDoubleTapEventSubscribed = 1 << 5
}
}
}
Loading