Repository navigation
GestureManager: Add handler-provided platform managers - #33365
Conversation
|
/rebase |
30d352f to
35cfe01
Compare
…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>
35cfe01 to
4769673
Compare
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 33365Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 33365" |
…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>
|
/review -b feature/refactor-copilot-yml |
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>
|
/review -b feature/refactor-copilot-yml |
PureWeen
left a comment
There was a problem hiding this comment.
@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
GetServiceregression inAnimationReadyHandleris fixed (returnsnullforIGesturePlatformManagerFactory). ThrowsWhenFactoryProvidedManagerIsNullwas added, closing the round-1 coverage gap.- The
PublicAPI.Unshipped.txtentries were correctly removed to match the internal surface.
One should-fix from this round (inline on GestureManager.cs):
⚠️ Error Handling (3/3 reviewers) —GetOptionalGesturePlatformManagerFactory'scatch (Exception) { return null; }is broader than its stated justification. BecauseGetService<T>()is(T)GetService(typeof(T)), it also swallowsInvalidCastExceptionfrom a wrong-typed registration and exceptions thrown by a registered factory's own constructor — silently falling back to the default manager. TheLayoutTests.csnarrowing in this PR is itself a workaround for that masking. Now that the test doubles honor theIServiceProvidernull contract, consider dropping the catch-all or usingGetService(typeof(...)) as IGesturePlatformManagerFactory. See inline detail.
Forward-looking — not blocking this PR (surface is internal):
- 💡 API Design — A resolved factory/provider that returns
nullthrows 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 defaultGesturePlatformManageris itselfinternal(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
GestureManagerdisposes 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.
| try | ||
| { | ||
| return services.GetService<IGesturePlatformManagerFactory>(); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
|
@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 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
left a comment
There was a problem hiding this comment.
@copilot Round-3 adversarial re-review (3 independent reviewers + adversarial consensus). CI status is intentionally out of scope.
The round-2 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 genericGetService<T>()extension, which this round removed; the non-genericIServiceProvider.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 viaGetService<T>()" but production now probes viaGetService(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; |
There was a problem hiding this comment.
💡 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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
💡 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.
There was a problem hiding this comment.
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>
PureWeen
left a comment
There was a problem hiding this comment.
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-unusedusing Microsoft.Extensions.DependencyInjection;. Verified safe: the sole service lookup is the non-genericIServiceProvider.GetService(typeof(IGesturePlatformManagerFactory)), which needs no DI extension, and no other DI-extension symbols remain in the file.AnimationReadyHandler.cs— the comment now readsGetService(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.
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
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
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
GestureManagerdirectly constructed the platformGesturePlatformManagerfor 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
IGesturePlatformManagerabstraction with two customization mechanisms:IGesturePlatformManagerFactory(internal) - Resolved fromhandler.MauiContext.Servicesfor application-wide gesture manager customizationIGesturePlatformManagerProvider(internal) - Handler-scoped interface for handler-specific customizationGestureManagernow resolves the platform manager with this precedence:IGesturePlatformManagerFactory(highest precedence)IGesturePlatformManagerProvider(fallback)new GesturePlatformManager(handler)(final fallback)The manager is owned and disposed by
GestureManageron 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 : IDisposableis the shared internal abstraction implemented by each platformGesturePlatformManager.IGesturePlatformManagerFactoryenables application-wide gesture behavior override via DI registration inMauiAppBuilder.IGesturePlatformManagerProviderenables handler-specific customization for specialized handlers.GestureManagerstill usesnew GesturePlatformManager(handler)when neither customization is present, so existing handlers keep the same behavior.Issues Fixed
Fixes #33364
Platforms Tested
GestureManagerTests(15 tests),LayoutTests(364 tests),AnimationTests(16 tests)Controls.Core.UnitTestsproject