Skip to content

Commit

Permalink
Subscribe pointer events only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
MartyIX committed Jul 19, 2024
1 parent 69fbd4f commit 0d8b0c6
Showing 1 changed file with 24 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,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<PointerGestureRecognizer>();

if (hasPointerGesture)
{
_subscriptionFlags |= SubscriptionFlags.ContainerPgrPointerEventsSubscribed;
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 +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;
Expand All @@ -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;
Expand Down

0 comments on commit 0d8b0c6

Please sign in to comment.