Skip to content

Commit

Permalink
[Windows] Subscribe pointer events only when needed (dotnet#23515)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartyIX authored Jul 21, 2024
1 parent 8bac7ec commit 6a414ea
Showing 1 changed file with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -730,15 +730,7 @@ void PinchComplete(bool success)

void UpdateDragAndDropGestureRecognizers()
{
if (_container is null)
{
return;
}

var view = Element as View;
IList<IGestureRecognizer>? gestures = view?.GestureRecognizers;

if (gestures is null)
if (_container is null || Element is not View view || view.GestureRecognizers is not IList<IGestureRecognizer> gestures)
{
return;
}
Expand Down Expand Up @@ -812,16 +804,17 @@ void UpdatingGestureRecognizers()
}
}

_subscriptionFlags |= SubscriptionFlags.ContainerPgrPointerEventsSubscribed;
_container.PointerEntered += OnPgrPointerEntered;
_container.PointerExited += OnPgrPointerExited;
_container.PointerMoved += OnPgrPointerMoved;
_container.PointerPressed += OnPgrPointerPressed;
_container.PointerReleased += OnPgrPointerReleased;
bool hasPointerGesture = ElementGestureRecognizers.HasAnyGesturesFor<PointerGestureRecognizer>();

if (hasPointerGesture)
{
SubscribePointerEvents(_container);
}

bool hasSwipeGesture = gestures.HasAnyGesturesFor<SwipeGestureRecognizer>();
bool hasPinchGesture = gestures.HasAnyGesturesFor<PinchGestureRecognizer>();
bool hasPanGesture = gestures.HasAnyGesturesFor<PanGestureRecognizer>();

if (!hasSwipeGesture && !hasPinchGesture && !hasPanGesture)
{
return;
Expand All @@ -840,6 +833,12 @@ void UpdatingGestureRecognizers()
return;
}

// Pan, pinch, and swipe gestures need pointer events if not subscribed yet.
if (!hasPointerGesture)
{
SubscribePointerEvents(_container);
}

_subscriptionFlags |= SubscriptionFlags.ContainerManipulationAndPointerEventsSubscribed;
_container.ManipulationMode = ManipulationModes.Scale | ManipulationModes.TranslateX | ManipulationModes.TranslateY;
_container.ManipulationDelta += OnManipulationDelta;
Expand All @@ -848,6 +847,17 @@ void UpdatingGestureRecognizers()
_container.PointerCanceled += OnPointerCanceled;
}

void SubscribePointerEvents(FrameworkElement container)
{
_subscriptionFlags |= SubscriptionFlags.ContainerPgrPointerEventsSubscribed;

container.PointerEntered += OnPgrPointerEntered;
container.PointerExited += OnPgrPointerExited;
container.PointerMoved += OnPgrPointerMoved;
container.PointerPressed += OnPgrPointerPressed;
container.PointerReleased += OnPgrPointerReleased;
}

void HandleTapped(object sender, TappedRoutedEventArgs tappedRoutedEventArgs)
{
tappedRoutedEventArgs.Handled = true;
Expand Down

0 comments on commit 6a414ea

Please sign in to comment.