Skip to content

Commit

Permalink
[Windows] Subscribe less pointer events in `UpdatingGestureRecognizer…
Browse files Browse the repository at this point in the history
…s` (#22781)

### Description of Change

The idea of the PR is simple. There two sets of pointer event
subscriptions:


https://github.com/dotnet/maui/blob/fcabff1fe3e551e22558cba74dd3a86e00323b69/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs#L792-L796

and


https://github.com/dotnet/maui/blob/fcabff1fe3e551e22558cba74dd3a86e00323b69/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs#L824-L827

Consequently, `_container.PointerPressed`, `_container.PointerExited`,
and `_container.PointerReleased` are subscribed twice. And this PR
removes these subscriptions.

 

### Performance impact

* MAIN:
[Maui.Controls.Sample.Sandbox.exe_20240601_222830.speedscope.MAIN.json](https://github.com/user-attachments/files/15523262/Maui.Controls.Sample.Sandbox.exe_20240601_222830.speedscope.MAIN.json)
* PR:
[Maui.Controls.Sample.Sandbox.exe_20240601_223130.speedscope.PR.json](https://github.com/user-attachments/files/15523263/Maui.Controls.Sample.Sandbox.exe_20240601_223130.speedscope.PR.json)


![image](https://github.com/dotnet/maui/assets/203266/fd77fd77-9c62-414f-b7a6-d9ceec6b3621)

-> ~25% improvement

### Issues Fixed

Contributes to #21787
  • Loading branch information
Foda authored Jun 5, 2024
2 parents df93d3a + 56a1eaa commit 4a3bc31
Showing 1 changed file with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,6 @@ void ClearContainerEventHandlers()
_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 @@ -542,24 +539,45 @@ void OnPgrPointerEntered(object sender, PointerRoutedEventArgs e)
=> GetPosition(relativeTo, e), _control is null ? null : new PlatformPointerEventArgs(_control, e)));

void OnPgrPointerExited(object sender, PointerRoutedEventArgs e)
=> HandlePgrPointerEvent(e, (view, recognizer)
=> recognizer.SendPointerExited(view, (relativeTo)
=> GetPosition(relativeTo, e), _control is null ? null : new PlatformPointerEventArgs(_control, e)));
{
HandlePgrPointerEvent(e, (view, recognizer)
=> recognizer.SendPointerExited(view, (relativeTo)
=> GetPosition(relativeTo, e), _control is null ? null : new PlatformPointerEventArgs(_control, e)));

if ((_subscriptionFlags & SubscriptionFlags.ContainerManipulationAndPointerEventsSubscribed) != 0)
{
OnPointerExited(sender, e);
}
}

void OnPgrPointerMoved(object sender, PointerRoutedEventArgs e)
=> HandlePgrPointerEvent(e, (view, recognizer)
=> recognizer.SendPointerMoved(view, (relativeTo)
=> GetPosition(relativeTo, e), _control is null ? null : new PlatformPointerEventArgs(_control, e)));

void OnPgrPointerPressed(object sender, PointerRoutedEventArgs e)
=> HandlePgrPointerEvent(e, (view, recognizer)
=> recognizer.SendPointerPressed(view, (relativeTo)
=> GetPosition(relativeTo, e), _control is null ? null : new PlatformPointerEventArgs(_control, e)));
{
HandlePgrPointerEvent(e, (view, recognizer)
=> recognizer.SendPointerPressed(view, (relativeTo)
=> GetPosition(relativeTo, e), _control is null ? null : new PlatformPointerEventArgs(_control, e)));

if ((_subscriptionFlags & SubscriptionFlags.ContainerManipulationAndPointerEventsSubscribed) != 0)
{
OnPointerPressed(sender, e);
}
}

void OnPgrPointerReleased(object sender, PointerRoutedEventArgs e)
=> HandlePgrPointerEvent(e, (view, recognizer)
=> recognizer.SendPointerReleased(view, (relativeTo)
=> GetPosition(relativeTo, e), _control is null ? null : new PlatformPointerEventArgs(_control, e)));
{
HandlePgrPointerEvent(e, (view, recognizer)
=> recognizer.SendPointerReleased(view, (relativeTo)
=> GetPosition(relativeTo, e), _control is null ? null : new PlatformPointerEventArgs(_control, e)));

if ((_subscriptionFlags & SubscriptionFlags.ContainerManipulationAndPointerEventsSubscribed) != 0)
{
OnPointerReleased(sender, e);
}
}

private void HandlePgrPointerEvent(PointerRoutedEventArgs e, Action<View, PointerGestureRecognizer> SendPointerEvent)
{
Expand Down Expand Up @@ -822,9 +840,6 @@ void UpdatingGestureRecognizers()
_container.ManipulationDelta += OnManipulationDelta;
_container.ManipulationStarted += OnManipulationStarted;
_container.ManipulationCompleted += OnManipulationCompleted;
_container.PointerPressed += OnPointerPressed;
_container.PointerExited += OnPointerExited;
_container.PointerReleased += OnPointerReleased;
_container.PointerCanceled += OnPointerCanceled;
}

Expand Down

0 comments on commit 4a3bc31

Please sign in to comment.