Skip to content

GestureManager: Add handler-provided platform managers - #33365

Merged
PureWeen merged 21 commits into
net11.0from
copilot/create-igestureplatformmanager-interface
Jun 25, 2026
Merged

PureWeen merged 21 commits into
net11.0from
copilot/create-igestureplatformmanager-interface

Conversation

Copilot AI commented Jan 3, 2026 •

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Root Cause

GestureManager directly constructed the platform GesturePlatformManager for every handler connection. That made gesture behavior effectively hard-coded to the default manager and gave specialized handlers no lifecycle-safe way to provide a replacement for the platform view they own.

Description of Change

Adds internal IGesturePlatformManager abstraction with two customization mechanisms:

  1. IGesturePlatformManagerFactory (internal) - Resolved from handler.MauiContext.Services for application-wide gesture manager customization
  2. IGesturePlatformManagerProvider (internal) - Handler-scoped interface for handler-specific customization

GestureManager now resolves the platform manager with this precedence:

  1. DI-registered factory via IGesturePlatformManagerFactory (highest precedence)
  2. Handler-implemented IGesturePlatformManagerProvider (fallback)
  3. Default new GesturePlatformManager(handler) (final fallback)

The manager is owned and disposed by GestureManager on disconnect/reconnect, preserving existing lifecycle behavior.

Both interfaces are internal for .NET 11 to allow iteration before committing to a public API surface.

Key Technical Details

  • IGesturePlatformManager : IDisposable is the shared internal abstraction implemented by each platform GesturePlatformManager.
  • IGesturePlatformManagerFactory enables application-wide gesture behavior override via DI registration in MauiAppBuilder.
  • IGesturePlatformManagerProvider enables handler-specific customization for specialized handlers.
  • GestureManager still uses new GesturePlatformManager(handler) when neither customization is present, so existing handlers keep the same behavior.
  • Tests cover default fallback, DI factory usage, handler provider usage, factory precedence over provider, disposal on disconnect, null-guard enforcement, and creating a new manager after reconnect.

Issues Fixed

Fixes #33364

Platforms Tested

  • Controls.Core.UnitTests: GestureManagerTests (15 tests), LayoutTests (364 tests), AnimationTests (16 tests)
  • Full Controls.Core.UnitTests project

Copilot AI changed the title [WIP] Create IGesturePlatformManager interface for custom behaviors Create IGesturePlatformManager interface for custom gesture handling Jan 3, 2026
Copilot AI requested a review from PureWeen January 3, 2026 03:13
@PureWeen

Copy link
Copy Markdown
Member

/rebase

Copilot AI and others added 3 commits March 17, 2026 09:15
…pport

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…ract

- Add SetupHandler(IViewHandler) to IGesturePlatformManager so custom
  implementations can receive the platform view handler
- Track DI provenance in GestureManager (_gestureManagerFromDI flag)
  and skip Dispose() on DI-provided instances; the container owns their
  lifetime, not GestureManager
- Call SetupHandler(handler) on reconnect so DI singletons receive the
  new handler after a disconnect/reconnect cycle
- Add SetupHandler no-op to all built-in platform GesturePlatformManager
  classes (they initialize via constructor; the method is for custom impls)
- Add tests: DI singleton not disposed on disconnect, singleton reused
  and SetupHandler called on reconnect

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@PureWeen
PureWeen force-pushed the copilot/create-igestureplatformmanager-interface branch from 35cfe01 to 4769673 Compare March 17, 2026 14:22
@github-actions

github-actions Bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 33365

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 33365"

github-actions Bot and others added 2 commits March 17, 2026 09:45
…on dispose on exception

If SetupHandler() throws, DisconnectGestures() would have called Dispose() on
the DI-owned singleton because _gestureManagerFromDI was still false. Moving
the flag assignment before SetupHandler ensures the guard is in place
regardless of whether SetupHandler succeeds or throws.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…Manager

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@kubaflo

kubaflo commented May 24, 2026

Copy link
Copy Markdown
Contributor

/review -b feature/refactor-copilot-yml

MauiBot

This comment was marked as outdated.

@MauiBot MauiBot added s/agent-review-incomplete s/agent-fix-win AI found a better alternative fix than the PR s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review) labels May 24, 2026
@kubaflo
kubaflo marked this pull request as ready for review May 28, 2026 09:17
Use a factory to create handler-scoped gesture platform managers so GestureManager owns and disposes each instance on disconnect. This avoids reusing DI-provided managers across handler connections and removes the setup-only lifecycle hook.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@kubaflo

kubaflo commented May 28, 2026

Copy link
Copy Markdown
Contributor

/review -b feature/refactor-copilot-yml

@MauiBot MauiBot added s/agent-fix-pr-picked AI could not beat the PR fix - PR is the best among all candidates and removed s/agent-fix-win AI found a better alternative fix than the PR labels May 28, 2026

@PureWeen PureWeen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot Round-2 adversarial re-review (3 independent reviewers + adversarial consensus). CI status is intentionally out of scope for this review.

Round-1 blocking findings are resolved ✅ (verified at head 2cf8e5dc):

  • The three new interfaces are now internal (were public) — addresses the speculative-public-API concern from round 1 and the earlier interface-visibility feedback.
  • The GetService regression in AnimationReadyHandler is fixed (returns null for IGesturePlatformManagerFactory).
  • ThrowsWhenFactoryProvidedManagerIsNull was added, closing the round-1 coverage gap.
  • The PublicAPI.Unshipped.txt entries were correctly removed to match the internal surface.

One should-fix from this round (inline on GestureManager.cs):

  • ⚠️ Error Handling (3/3 reviewers) — GetOptionalGesturePlatformManagerFactory's catch (Exception) { return null; } is broader than its stated justification. Because GetService<T>() is (T)GetService(typeof(T)), it also swallows InvalidCastException from a wrong-typed registration and exceptions thrown by a registered factory's own constructor — silently falling back to the default manager. The LayoutTests.cs narrowing in this PR is itself a workaround for that masking. Now that the test doubles honor the IServiceProvider null contract, consider dropping the catch-all or using GetService(typeof(...)) as IGesturePlatformManagerFactory. See inline detail.

Forward-looking — not blocking this PR (surface is internal):

  • 💡 API Design — A resolved factory/provider that returns null throws rather than falling through (provider → default). This is an internally consistent fail-fast contract today. The null-semantics ("null = use default" vs "null = error") and the fact that the default GesturePlatformManager is itself internal (so an implementer can't produce the default) are worth settling before these interfaces go public alongside #33364. One reviewer initially flagged this as blocking; on a 2-model dispute round both concluded it isn't reachable by external code while everything is internal, so it's a decision to lock down at publicization time, not a defect here.
  • 💡 Contract — The interface docs require returning a new instance per call (since GestureManager disposes on disconnect); nothing enforces it, so a cached instance would be used-after-dispose. Acceptable as a documented internal contract; revisit when/if public.

Test coverage: the 8 new tests cover default / provider / factory / precedence / both-null-throws / dispose-on-disconnect / recreate-on-reconnect — solid. The only gap is the catch-fallback path (a throwing IServiceProvider), which becomes moot if the catch-all is removed.

Prior reviews: round-1 inline findings and the earlier interface-visibility feedback are addressed; not re-flagged here.

Methodology: 3 independent reviewers with adversarial consensus; 1 disputed blocking finding downgraded to forward-looking after a 2-model follow-up round.

Comment on lines +117 to +124
try
{
return services.GetService<IGesturePlatformManagerFactory>();
}
catch (Exception)
{
return null;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

⚠️ Error Handling — catch (Exception) is broader than its stated justification and can mask real misconfigurations.
Flagged by: 3/3 reviewers

The comment justifies the catch as defending against a strict container that throws for unregistered services. But GetService<T>() is (T)GetService(typeof(T)), so it also throws InvalidCastException when something is registered for this type but isn't an IGesturePlatformManagerFactory, and it propagates exceptions thrown by a registered factory's own constructor (missing dependency, cyclic registration). All of those get swallowed and silently fall back to the default manager, with no signal to diagnose why a custom factory never runs.

This masking is exactly what the LayoutTests.cs change in this PR works around: the old GetService(Arg.Any<Type>()) stub returned an ILayoutManagerFactory for the new gesture-factory probe, the (T) cast threw InvalidCastException, and this catch ate it.

Now that the test doubles honor the IServiceProvider null contract (the AnimationReadyHandler return-null guard + the LayoutTests narrowing in this same PR), the catch-all is broader than needed. Consider either dropping the try/catch and relying on the standard GetService null-for-unregistered contract, or using services.GetService(typeof(IGesturePlatformManagerFactory)) as IGesturePlatformManagerFactory, which returns null on a type mismatch without swallowing genuine construction failures.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in f1e85c6 - replaced GetService<T>() with catch-all with the safer GetService(typeof(T)) as T pattern. This returns null for both unregistered services and type mismatches without swallowing genuine construction failures.

Replace GetService<T>() with catch-all Exception handler with GetService(typeof(T)) as T pattern.
This returns null for both unregistered services and type mismatches without swallowing
genuine construction failures from the factory itself.

Addresses review feedback from @PureWeen (comment 4554323209).

All tests pass: GestureManagerTests (15), AnimationTests (16), LayoutTests (364).

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copilot AI requested a review from PureWeen June 23, 2026 14:50
@kubaflo

kubaflo commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen

Copy link
Copy Markdown
Member

@copilot fix #33365 (review) and any other recent comments

Copilot AI commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

@copilot fix #33365 (review) and any other recent comments

The error handling issue from review #4554323209 has already been fixed in commit f1e85c6. The code now uses the safer GetService(typeof(T)) as T pattern instead of the catch-all Exception handler. All GestureManagerTests (15/15) pass.

The fix replaced:

try { return services.GetService<IGesturePlatformManagerFactory>(); }
catch (Exception) { return null; }

With:

return services.GetService(typeof(IGesturePlatformManagerFactory)) as IGesturePlatformManagerFactory;

This returns null for both unregistered services and type mismatches without masking genuine construction failures from the factory.

@PureWeen PureWeen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot Round-3 adversarial re-review (3 independent reviewers + adversarial consensus). CI status is intentionally out of scope.

The round-2 ⚠️ finding is resolved ✅ — verified by all 3 reviewers. GetOptionalGesturePlatformManagerFactory now uses services.GetService(typeof(IGesturePlatformManagerFactory)) as IGesturePlatformManagerFactory (commit f1e85c61). This is a strict improvement over the prior catch (Exception): it returns null for an unregistered service (standard contract) and null on a type mismatch (the as), while a genuine construction failure in a registered factory now propagates (fail-fast) instead of being silently swallowed into a default-manager fallback. No regression: the manager field is assigned only after creation returns, so the throw-on-null paths leave nothing dangling, and the test stubs now mirror the production lookup exactly.

Two minor cleanups remain (non-blocking, 💡, both inline):

  • 💡 Documentation — GestureManager.cs:10: using Microsoft.Extensions.DependencyInjection; is now dead. It was only needed for the generic GetService<T>() extension, which this round removed; the non-generic IServiceProvider.GetService(Type) needs no DI import. Harmless at runtime, but signals the edit was left slightly incomplete. (3/3 reviewers)
  • 💡 Documentation — AnimationReadyHandler.cs:93: the comment still says "probed via GetService<T>()" but production now probes via GetService(typeof(...)). (3/3 reviewers)

Forward-looking items from round 2 (already posted — not re-flagged, status only): the factory/provider null-return throwing instead of falling back to the default, and the documented-but-unenforced "must return a new instance" contract, both still stand. They're fine while the surface is internal; worth settling before these interfaces go public alongside #33364.

Test coverage: solid — default / provider / factory / factory-precedence / both null-throws / dispose-on-disconnect / new-instance-on-reconnect are all exercised, and the service-lookup stubs match the production call.

Prior reviews: round-1 and round-2 findings are all addressed; not duplicated here.

Methodology: 3 independent reviewers with adversarial consensus. No blocking issues this round — only two trivial doc cleanups, both unanimously flagged.

using System.ComponentModel;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Graphics;
using Microsoft.Extensions.DependencyInjection;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 Documentation — this using is now unused.
Flagged by: 3/3 reviewers

using Microsoft.Extensions.DependencyInjection; was added for the generic GetService<T>() extension. Commit f1e85c61 switched to the non-generic IServiceProvider.GetService(typeof(IGesturePlatformManagerFactory)) (line 116), which lives on System.IServiceProvider and needs no DI import — I confirmed no other symbol in this file resolves through that namespace. Harmless at runtime, but it's a leftover from the round-3 edit. Safe to remove.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 15e1433 - removed unused using Microsoft.Extensions.DependencyInjection;

if (serviceType == typeof(IAnimationManager))
return _animationManager;

// Optional services are probed via GetService<T>() and are expected to

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💡 Documentation — comment is slightly out of date.
Flagged by: 3/3 reviewers

The comment says optional services are "probed via GetService<T>()", but production now probes via the non-generic GetService(typeof(...)) (see GestureManager.cs:116). Minor drift — consider dropping the <T> so the comment matches reality.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 15e1433 - updated comment to reference GetService(typeof(...))

- Remove unused `using Microsoft.Extensions.DependencyInjection;` from GestureManager.cs
  (no longer needed after switching from GetService<T>() to GetService(typeof(T)))
- Update AnimationReadyHandler.cs comment to reflect non-generic GetService usage

Addresses Round-3 review feedback from @PureWeen (comment 4573925490).

All tests pass: GestureManagerTests (15/15).

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copilot AI requested a review from PureWeen June 25, 2026 18:44

@PureWeen PureWeen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Round-4 re-review — no issues found. ✅

The only change since the round-3 review (commit 21e943e5) is the two cosmetic cleanups that were flagged, both resolved exactly:

  • GestureManager.cs — removed the now-unused using Microsoft.Extensions.DependencyInjection;. Verified safe: the sole service lookup is the non-generic IServiceProvider.GetService(typeof(IGesturePlatformManagerFactory)), which needs no DI extension, and no other DI-extension symbols remain in the file.
  • AnimationReadyHandler.cs — the comment now reads GetService(typeof(...)), matching the production call.

The substantive concerns from earlier rounds are all resolved: the round-2 exception-swallowing catch-all was replaced with the safer GetService(typeof(...)) as IGesturePlatformManagerFactory (fail-fast on a buggy factory, null on unregistered/type-mismatch), and lifecycle/disposal plus test coverage (default / provider / factory / precedence / both null-throws / dispose-on-disconnect / new-instance-on-reconnect) were verified sound.

The only remaining items are the two forward-looking design notes already on record — the factory/provider null-return throwing rather than falling back to the default, and the documented-but-unenforced "return a new instance" contract. Both are acceptable while the surface is internal; they're worth settling before these interfaces are made public alongside #33364, not blockers here.

Methodology: this round was a delta verification of a 3-line cosmetic change; the full 3-reviewer adversarial pass was run in rounds 1–3. No new code-behavior changed.

@PureWeen
PureWeen merged commit 8ee100c into net11.0 Jun 25, 2026
25 of 29 checks passed
@PureWeen
PureWeen deleted the copilot/create-igestureplatformmanager-interface branch June 25, 2026 22:31
kubaflo pushed a commit that referenced this pull request Jul 18, 2026
Rely on the factory and precedence coverage already merged in #33365, keeping this PR focused on public API exposure and the new platform-handler regression.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 66f84348-6476-4097-8b7f-f240338e85c3
kubaflo pushed a commit that referenced this pull request Jul 18, 2026
Keep the existing #33365 unit test file unchanged so this PR only carries net-new public API, guard, and device-test coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 66f84348-6476-4097-8b7f-f240338e85c3
kubaflo pushed a commit that referenced this pull request Jul 18, 2026
Remove gesture API exposure from this PR so #36655 exclusively owns the #33365 follow-up and #35044 fix, while this PR restores only the alert interfaces.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 66f84348-6476-4097-8b7f-f240338e85c3
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

external-tfm s/agent-fix-win AI found a better alternative fix than the PR s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create IGesturePlatformManager so users can replace the behavior of our GesturePlatformManager a

6 participants