Repository navigation
[net11.0] Add platform-neutral handler contracts for external platform backends #37855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
367520b
56aaeb6
9d4f2d3
581c8f4
669f90b
5d65992
58321e1
ece04fc
d94349c
792caa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| namespace Microsoft.Maui | ||
| { | ||
| /// <summary> | ||
| /// Defines a strongly typed, platform-neutral element handler contract. | ||
| /// </summary> | ||
| /// <typeparam name="TVirtualView">The cross-platform (virtual view) type this handler manages.</typeparam> | ||
| /// <typeparam name="TPlatformView">The native type this handler creates.</typeparam> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The per-control handler interfaces such as <see cref="Handlers.IWindowHandler"/> declare | ||
| /// <c>PlatformView</c> using a <c>using PlatformView = ...</c> alias that resolves to a different | ||
| /// native type for every target framework. That makes them impossible to implement from an | ||
| /// external platform backend whose native types are not the ones .NET MAUI ships in the box: | ||
| /// the compiler reports <c>CS0738</c> (or <c>CS9333</c> on the platform-neutral target framework) | ||
| /// because the implementing member's return type does not match the alias exactly. | ||
| /// </para> | ||
| /// <para> | ||
| /// This interface has no such alias. Both type parameters are covariant, so a handler can be | ||
| /// matched platform-neutrally by testing for <c>IElementHandler<IWindow, object></c> | ||
| /// regardless of which native type it actually creates. | ||
| /// </para> | ||
| /// <para> | ||
| /// <see cref="Handlers.ElementHandler{TVirtualView, TPlatformView}"/> implements this interface, | ||
| /// so any handler derived from it — in the box or in an external backend — satisfies the contract | ||
| /// without writing any additional members. | ||
| /// </para> | ||
| /// <para> | ||
| /// <b>Lifecycle.</b> <see cref="VirtualView"/> and <see cref="PlatformView"/> are non-nullable here, | ||
| /// matching the non-nullable properties on | ||
| /// <see cref="Handlers.ElementHandler{TVirtualView, TPlatformView}"/> that satisfy them — and those | ||
| /// properties <b>throw <see cref="System.InvalidOperationException"/></b> rather than returning | ||
| /// <see langword="null"/> once the handler is disconnected. | ||
| /// <see cref="IElementHandler.DisconnectHandler"/> clears the platform view before invoking the | ||
| /// disconnect callback, so a handler is only guaranteed to have one between | ||
| /// <see cref="IElementHandler.SetVirtualView"/> and <see cref="IElementHandler.DisconnectHandler"/>. | ||
| /// The inherited <see cref="IElementHandler.VirtualView"/> and | ||
| /// <see cref="IElementHandler.PlatformView"/> return <see langword="null"/> in exactly that state and | ||
| /// never throw, so code that can run outside a connected window — teardown paths, weak-reference | ||
| /// caches, diagnostics — should read those instead. Property and command mappers always run while | ||
| /// the handler is connected and can use the typed members freely. | ||
| /// </para> | ||
| /// </remarks> | ||
| /// <example> | ||
| /// <code lang="csharp"><![CDATA[ | ||
| /// // In an external platform backend, with a native type MAUI knows nothing about: | ||
| /// public class MyWindowHandler : ElementHandler<IWindow, MyNativeWindow> | ||
| /// { | ||
| /// public MyWindowHandler() : base(WindowHandler.Mapper) { } | ||
| /// protected override MyNativeWindow CreatePlatformElement() => new MyNativeWindow(); | ||
| /// } | ||
| /// | ||
| /// // Consumers match it platform-neutrally. The typed members below throw once the handler is | ||
| /// // disconnected, so guard on the nullable IElementHandler.PlatformView first. | ||
| /// if (window.Handler is { PlatformView: not null } handler && | ||
| /// handler is IElementHandler<IWindow, object> windowHandler) | ||
| /// { | ||
| /// IWindow virtualView = windowHandler.VirtualView; | ||
| /// object nativeWindow = windowHandler.PlatformView; | ||
| /// } | ||
| /// ]]></code> | ||
| /// </example> | ||
| public interface IElementHandler<out TVirtualView, out TPlatformView> : IElementHandler | ||
| where TVirtualView : IElement | ||
| where TPlatformView : class | ||
|
Comment on lines
+62
to
+64
|
||
| { | ||
| /// <summary> | ||
| /// Gets the cross-platform virtual view associated with the handler. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Throws <see cref="System.InvalidOperationException"/> when the handler is not connected. Read | ||
| /// the nullable <see cref="IElementHandler.VirtualView"/> instead on paths that can run while | ||
| /// disconnected. | ||
| /// </remarks> | ||
| new TVirtualView VirtualView { get; } | ||
|
Redth marked this conversation as resolved.
Resolved
|
||
|
|
||
| /// <summary> | ||
| /// Gets the native view associated with the handler. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Throws <see cref="System.InvalidOperationException"/> when the handler is not connected. Read | ||
| /// the nullable <see cref="IElementHandler.PlatformView"/> instead on paths that can run while | ||
| /// disconnected. | ||
| /// </remarks> | ||
| new TPlatformView PlatformView { get; } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| namespace Microsoft.Maui | ||
| { | ||
| /// <summary> | ||
| /// Defines a strongly typed, platform-neutral view handler contract. | ||
| /// </summary> | ||
| /// <typeparam name="TVirtualView">The cross-platform (virtual view) type this handler manages.</typeparam> | ||
| /// <typeparam name="TPlatformView">The native type this handler creates.</typeparam> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// This is the <see cref="IViewHandler"/> counterpart of | ||
| /// <see cref="IElementHandler{TVirtualView, TPlatformView}"/>. Unlike the per-control handler | ||
| /// interfaces such as <see cref="Handlers.ILabelHandler"/>, <see cref="Handlers.IContentViewHandler"/> | ||
| /// or <see cref="ILayoutHandler"/>, it does not bind <c>PlatformView</c> to a per-target-framework | ||
| /// type alias, so an external platform backend can implement it with its own native types. | ||
| /// </para> | ||
| /// <para> | ||
| /// Both type parameters are covariant. Use <c>object</c> for <typeparamref name="TPlatformView"/> | ||
| /// to match any handler for a given virtual view type, regardless of backend. | ||
| /// </para> | ||
| /// <para> | ||
| /// <see cref="Handlers.ViewHandler{TVirtualView, TPlatformView}"/> implements this interface, so | ||
| /// every handler derived from it already satisfies the contract with no extra members. | ||
| /// </para> | ||
| /// <para> | ||
| /// <b>Lifecycle.</b> As with | ||
| /// <see cref="IElementHandler{TVirtualView, TPlatformView}"/>, the typed <see cref="VirtualView"/> | ||
| /// and <c>PlatformView</c> members <b>throw <see cref="System.InvalidOperationException"/></b> rather | ||
| /// than returning <see langword="null"/> once the handler is disconnected. Use the nullable | ||
| /// <see cref="IElementHandler.VirtualView"/> and <see cref="IElementHandler.PlatformView"/> on paths | ||
| /// that can run outside a connected window. | ||
| /// </para> | ||
| /// </remarks> | ||
| /// <example> | ||
| /// <code lang="csharp"><![CDATA[ | ||
| /// // Works for the in-box LabelHandler and for an external backend's label handler alike. The typed | ||
| /// // members throw once the handler is disconnected, so guard on the nullable one first. | ||
| /// if (label.Handler is { PlatformView: not null } handler && | ||
| /// handler is IViewHandler<ILabel, object> labelHandler) | ||
| /// { | ||
| /// ILabel virtualView = labelHandler.VirtualView; | ||
| /// object nativeLabel = labelHandler.PlatformView; | ||
| /// } | ||
| /// ]]></code> | ||
| /// </example> | ||
| public interface IViewHandler<out TVirtualView, out TPlatformView> : IElementHandler<TVirtualView, TPlatformView>, IViewHandler | ||
| where TVirtualView : IView | ||
| where TPlatformView : class | ||
|
Comment on lines
+45
to
+47
|
||
| { | ||
| /// <summary> | ||
| /// Gets the cross-platform virtual view associated with the handler. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Re-declared so that lookups are unambiguous between | ||
| /// <see cref="IElementHandler{TVirtualView, TPlatformView}.VirtualView"/> and | ||
| /// <see cref="IViewHandler.VirtualView"/>. Throws | ||
| /// <see cref="System.InvalidOperationException"/> when the handler is not connected; read the | ||
| /// nullable <see cref="IViewHandler.VirtualView"/> instead on paths that can run while | ||
| /// disconnected. | ||
| /// </remarks> | ||
| new TVirtualView VirtualView { get; } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| namespace Microsoft.Maui | ||
| { | ||
| /// <summary> | ||
| /// Defines the platform-neutral behavior contract for a handler that manages an <see cref="ILayout"/>. | ||
| /// </summary> | ||
| /// <typeparam name="TPlatformView">The native type this handler creates.</typeparam> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// <see cref="ILayoutHandler"/> declares the same child-management members, but it also declares | ||
| /// <c>PlatformView</c> using a per-target-framework type alias, which prevents an external platform | ||
| /// backend from implementing it. This interface carries the behavior without the alias. | ||
| /// </para> | ||
| /// <para> | ||
| /// The two interfaces are intentionally unrelated: <see cref="ILayoutHandler"/> is untouched, so | ||
| /// existing implementations and callers keep working. A handler that implements the members as | ||
| /// ordinary public methods — as the in-box <see cref="Handlers.LayoutHandler"/> does — satisfies | ||
| /// both interfaces at once. | ||
| /// </para> | ||
| /// <para> | ||
| /// Because <typeparamref name="TPlatformView"/> is covariant, <c>ILayoutHandler<object></c> | ||
| /// matches any layout handler regardless of backend. | ||
| /// </para> | ||
| /// <para> | ||
| /// <b>How .NET MAUI Controls reaches a layout handler.</b> <c>Microsoft.Maui.Controls.Layout</c> does | ||
| /// not cast to either layout interface. It raises the child-management operations as <b>command | ||
| /// mapper keys</b> — <c>Handler.Invoke(nameof(ILayoutHandler.Add), new LayoutHandlerUpdate(index, | ||
| /// view))</c> and so on — which is type-agnostic and therefore reaches an external backend's handler | ||
| /// exactly as it reaches the in-box one. Referencing <see cref="ILayoutHandler"/> for its member | ||
| /// <em>names</em> is always allowed; only implementing it is blocked. An external backend should use | ||
| /// those same key strings so Controls interop keeps working. | ||
| /// </para> | ||
| /// <para> | ||
| /// <b>Known gap.</b> The obsolete | ||
| /// <c>Microsoft.Maui.Controls.Compatibility.Layout<T>.LayoutHandler</c> property is declared as | ||
| /// <see cref="ILayoutHandler"/> and evaluates <c>Handler as ILayoutHandler</c>, so it returns | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[moderate] Public API Surface Design / Backward Compatibility — The "Known gap" paragraph states the obsolete |
||
| /// <see langword="null"/> for a handler that implements only this generic interface. It is the sole | ||
| /// runtime cast to <see cref="ILayoutHandler"/> left in .NET MAUI, it is a public convenience | ||
| /// property that no in-box code path reads, and its declaring type is <c>[Obsolete]</c> — so it does | ||
| /// not affect layout behavior. It is called out here because this interface is, for that legacy | ||
| /// property specifically, a compile-time contract only. | ||
| /// </para> | ||
| /// </remarks> | ||
| public interface ILayoutHandler<out TPlatformView> : IViewHandler<ILayout, TPlatformView> | ||
|
Redth marked this conversation as resolved.
Resolved
|
||
| where TPlatformView : class | ||
| { | ||
| /// <summary> | ||
| /// Adds the specified child view to the end of the layout. | ||
| /// </summary> | ||
| /// <param name="view">The child view to add.</param> | ||
| void Add(IView view); | ||
|
|
||
| /// <summary> | ||
| /// Removes the specified child view from the layout. | ||
| /// </summary> | ||
| /// <param name="view">The child view to remove.</param> | ||
| void Remove(IView view); | ||
|
|
||
| /// <summary> | ||
| /// Removes all child views from the layout. | ||
| /// </summary> | ||
| void Clear(); | ||
|
|
||
| /// <summary> | ||
| /// Inserts the specified child view into the layout at the given index. | ||
| /// </summary> | ||
| /// <param name="index">The index at which to insert the child view.</param> | ||
| /// <param name="view">The child view to insert.</param> | ||
| void Insert(int index, IView view); | ||
|
|
||
| /// <summary> | ||
| /// Replaces the child view at the given index with the specified child view. | ||
| /// </summary> | ||
| /// <param name="index">The index of the child view to replace.</param> | ||
| /// <param name="view">The replacement child view.</param> | ||
| void Update(int index, IView view); | ||
|
|
||
| /// <summary> | ||
| /// Updates the z-index ordering of the specified child view. | ||
| /// </summary> | ||
| /// <param name="view">The child view whose z-index changed.</param> | ||
| void UpdateZIndex(IView view); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[major] Public API Surface Design / Logic and Correctness — The documented example for external backends passes an in-box control-specific mapper (
base(WindowHandler.Mapper)) to a handler that only implements the new generic contract.WindowHandler.MapperisIPropertyMapper<IWindow, IWindowHandler>(src/Core/src/Handlers/Window/WindowHandler.cs:17) andPropertyMapper<TVirtualView, TViewHandler>.Addinvokes each mapping via a hard castaction?.Invoke((TViewHandler)h, vv)(src/Core/src/PropertyMapper.cs:239);CommandMapper<,>does the same (src/Core/src/CommandMapper.cs:102).MyWindowHandler : ElementHandler<IWindow, MyNativeWindow>does not implementIWindowHandler— that is precisely the alias problem this PR works around — so the first property update (e.g.Title) throwsInvalidCastExceptionat runtime. Concrete scenario: an external backend follows this doc verbatim, and the window crashes on its first mapper run. No test covers it: every fake in src/Core/tests/ExternalBackend/FakeHandlers.cs declares its ownPropertyMapper<TVirtualView, FakeXHandler>chained offElementMapper/ViewMapper(whose handler type parameters —IElementHandler/IViewHandler— the fakes do satisfy), so the mapper-reuse path the doc recommends is never exercised. The example should either build a backend-owned mapper (as the fakes do) or explicitly state that in-box control-specific mappers cannot be reused by a handler that does not implement the corresponding aliased handler interface.