From 0d8b0c6d92b8fe648e74e9cd6a4a2563ed9bb5d1 Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Tue, 9 Jul 2024 22:40:19 +0200 Subject: [PATCH 1/2] Subscribe pointer events only when needed --- .../GesturePlatformManager.Windows.cs | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs b/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs index ba6d8a98f69c..34bc58064405 100644 --- a/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs +++ b/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs @@ -730,15 +730,7 @@ void PinchComplete(bool success) void UpdateDragAndDropGestureRecognizers() { - if (_container is null) - { - return; - } - - var view = Element as View; - IList? gestures = view?.GestureRecognizers; - - if (gestures is null) + if (_container is null || Element is not View view || view.GestureRecognizers is not IList gestures) { return; } @@ -812,16 +804,18 @@ void UpdatingGestureRecognizers() } } - _subscriptionFlags |= SubscriptionFlags.ContainerPgrPointerEventsSubscribed; - _container.PointerEntered += OnPgrPointerEntered; - _container.PointerExited += OnPgrPointerExited; - _container.PointerMoved += OnPgrPointerMoved; - _container.PointerPressed += OnPgrPointerPressed; - _container.PointerReleased += OnPgrPointerReleased; + bool hasPointerGesture = ElementGestureRecognizers.HasAnyGesturesFor(); + + if (hasPointerGesture) + { + _subscriptionFlags |= SubscriptionFlags.ContainerPgrPointerEventsSubscribed; + SubscribePointerEvents(_container); + } bool hasSwipeGesture = gestures.HasAnyGesturesFor(); bool hasPinchGesture = gestures.HasAnyGesturesFor(); bool hasPanGesture = gestures.HasAnyGesturesFor(); + if (!hasSwipeGesture && !hasPinchGesture && !hasPanGesture) { return; @@ -840,6 +834,12 @@ void UpdatingGestureRecognizers() return; } + if (!hasPointerGesture) + { + _subscriptionFlags |= SubscriptionFlags.ContainerPgrPointerEventsSubscribed; + SubscribePointerEvents(_container); + } + _subscriptionFlags |= SubscriptionFlags.ContainerManipulationAndPointerEventsSubscribed; _container.ManipulationMode = ManipulationModes.Scale | ManipulationModes.TranslateX | ManipulationModes.TranslateY; _container.ManipulationDelta += OnManipulationDelta; @@ -848,6 +848,15 @@ void UpdatingGestureRecognizers() _container.PointerCanceled += OnPointerCanceled; } + void SubscribePointerEvents(FrameworkElement container) + { + container.PointerEntered += OnPgrPointerEntered; + container.PointerExited += OnPgrPointerExited; + container.PointerMoved += OnPgrPointerMoved; + container.PointerPressed += OnPgrPointerPressed; + container.PointerReleased += OnPgrPointerReleased; + } + void HandleTapped(object sender, TappedRoutedEventArgs tappedRoutedEventArgs) { tappedRoutedEventArgs.Handled = true; From 7a4b555f643418021902bdc329cc547c91945f3c Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Fri, 19 Jul 2024 22:57:34 +0200 Subject: [PATCH 2/2] Address feedback --- .../GestureManager/GesturePlatformManager.Windows.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs b/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs index 34bc58064405..f905812c66bf 100644 --- a/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs +++ b/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs @@ -808,7 +808,6 @@ void UpdatingGestureRecognizers() if (hasPointerGesture) { - _subscriptionFlags |= SubscriptionFlags.ContainerPgrPointerEventsSubscribed; SubscribePointerEvents(_container); } @@ -834,9 +833,9 @@ void UpdatingGestureRecognizers() return; } + // Pan, pinch, and swipe gestures need pointer events if not subscribed yet. if (!hasPointerGesture) { - _subscriptionFlags |= SubscriptionFlags.ContainerPgrPointerEventsSubscribed; SubscribePointerEvents(_container); } @@ -850,6 +849,8 @@ void UpdatingGestureRecognizers() void SubscribePointerEvents(FrameworkElement container) { + _subscriptionFlags |= SubscriptionFlags.ContainerPgrPointerEventsSubscribed; + container.PointerEntered += OnPgrPointerEntered; container.PointerExited += OnPgrPointerExited; container.PointerMoved += OnPgrPointerMoved;