|
| 1 | +using Microsoft.UI.Reactor; |
| 2 | +using Microsoft.UI.Reactor.Core; |
| 3 | +using Microsoft.UI.Reactor.Core.V1Protocol; |
| 4 | +using Microsoft.UI.Reactor.Core.V1Protocol.Descriptor; |
| 5 | +using static Microsoft.UI.Reactor.Factories; |
| 6 | +using Microsoft.UI.Xaml; |
| 7 | +using WinUI = Microsoft.UI.Xaml.Controls; |
| 8 | + |
| 9 | +System.AppContext.SetSwitch("Reactor.UseV1Protocol", true); |
| 10 | +ReactorApp.Run<ExtendingApp>( |
| 11 | + "Extending Reactor", width: 540, height: 360, devtools: true, |
| 12 | + configure: host => StarMeterInterop.Register(host.Reconciler)); |
| 13 | + |
| 14 | +// ════════════════════════════════════════════════════════════════════════ |
| 15 | +// Step 1 — Define the Element record |
| 16 | +// ════════════════════════════════════════════════════════════════════════ |
| 17 | + |
| 18 | +// <snippet:star-meter-element> |
| 19 | +// An Element subclass with one controlled prop (Value), three one-way |
| 20 | +// props (MaxRating, Caption, IsClearEnabled), and one callback (OnValueChanged). |
| 21 | +// Records give the reconciler value-equality for free — two StarMeterElement |
| 22 | +// instances with identical fields compare equal and Update becomes a no-op. |
| 23 | +public sealed record StarMeterElement : Element |
| 24 | +{ |
| 25 | + public double Value { get; init; } |
| 26 | + public int MaxRating { get; init; } = 5; |
| 27 | + public string? Caption { get; init; } |
| 28 | + public bool IsClearEnabled { get; init; } = true; |
| 29 | + public System.Action<double>? OnValueChanged { get; init; } |
| 30 | +} |
| 31 | +// </snippet:star-meter-element> |
| 32 | + |
| 33 | +// ════════════════════════════════════════════════════════════════════════ |
| 34 | +// Step 2 — Wire the descriptor |
| 35 | +// ════════════════════════════════════════════════════════════════════════ |
| 36 | + |
| 37 | +// <snippet:star-meter-descriptor> |
| 38 | +public static class StarMeterDescriptor |
| 39 | +{ |
| 40 | + public static readonly ControlDescriptor<StarMeterElement, WinUI.RatingControl> Descriptor = |
| 41 | + new ControlDescriptor<StarMeterElement, WinUI.RatingControl> |
| 42 | + { |
| 43 | + // Leaf control — no children. (See ChildrenStrategy survey for |
| 44 | + // the other shapes: SingleContent, Panel, NamedSlots, ItemsHost…) |
| 45 | + Children = new None<StarMeterElement, WinUI.RatingControl>(), |
| 46 | + } |
| 47 | + // OneWay props: written on Mount, diff-and-written on Update. |
| 48 | + .OneWay( |
| 49 | + get: static e => e.MaxRating, |
| 50 | + set: static (c, v) => c.MaxRating = v) |
| 51 | + .OneWay( |
| 52 | + get: static e => e.IsClearEnabled, |
| 53 | + set: static (c, v) => c.IsClearEnabled = v) |
| 54 | + // OneWayConditional skips the write when the predicate is false — |
| 55 | + // leaves Caption at the control's default for elements that didn't |
| 56 | + // supply one, rather than forcing it to null and losing a style. |
| 57 | + .OneWayConditional( |
| 58 | + get: static e => e.Caption, |
| 59 | + set: static (c, v) => c.Caption = v!, |
| 60 | + shouldWrite: static e => e.Caption is not null) |
| 61 | + // Controlled is the two-way binding shape: the framework writes the |
| 62 | + // element's value at Mount (and on diff), suppresses the echo when |
| 63 | + // the framework is the writer, and forwards user input back through |
| 64 | + // OnValueChanged. Subscription is gated on the callback being non- |
| 65 | + // null — if the caller didn't pass OnValueChanged, no trampoline |
| 66 | + // is wired and the per-fire dispatch cost stays at zero. |
| 67 | + .Controlled<double, object>( |
| 68 | + get: static e => e.Value, |
| 69 | + set: static (c, v) => c.Value = v, |
| 70 | + subscribe: static (fe, h) => ((WinUI.RatingControl)fe).ValueChanged += (s, e) => h(s, e!), |
| 71 | + unsubscribe: static (fe, h) => { /* trampoline anchored for control lifetime */ }, |
| 72 | + callback: static e => e.OnValueChanged, |
| 73 | + readBack: static c => c.Value); |
| 74 | +} |
| 75 | +// </snippet:star-meter-descriptor> |
| 76 | + |
| 77 | +// ════════════════════════════════════════════════════════════════════════ |
| 78 | +// Step 3 — Register |
| 79 | +// ════════════════════════════════════════════════════════════════════════ |
| 80 | + |
| 81 | +// <snippet:star-meter-registration> |
| 82 | +public static class StarMeterInterop |
| 83 | +{ |
| 84 | + // One call per Reactor host. RegisterHandler accepts any IElementHandler, |
| 85 | + // and DescriptorHandler<TElement,TControl> is the canonical interpreter |
| 86 | + // for a ControlDescriptor. Duplicate registration for the same element |
| 87 | + // type throws — register exactly once on each host you mount. |
| 88 | + public static void Register(Reconciler reconciler) => |
| 89 | + reconciler.RegisterHandler<StarMeterElement, WinUI.RatingControl>( |
| 90 | + new DescriptorHandler<StarMeterElement, WinUI.RatingControl>( |
| 91 | + StarMeterDescriptor.Descriptor)); |
| 92 | +} |
| 93 | +// </snippet:star-meter-registration> |
| 94 | + |
| 95 | +// ════════════════════════════════════════════════════════════════════════ |
| 96 | +// Step 4 — Use the element |
| 97 | +// ════════════════════════════════════════════════════════════════════════ |
| 98 | + |
| 99 | +// <snippet:star-meter-usage> |
| 100 | +class ExtendingApp : Component |
| 101 | +{ |
| 102 | + public override Element Render() |
| 103 | + { |
| 104 | + var (rating, setRating) = UseState(3.5); |
| 105 | + |
| 106 | + return VStack(16, |
| 107 | + TextBlock("StarMeter — custom element wrapping WinUI RatingControl") |
| 108 | + .FontSize(14).SemiBold(), |
| 109 | + |
| 110 | + new StarMeterElement |
| 111 | + { |
| 112 | + Value = rating, |
| 113 | + MaxRating = 5, |
| 114 | + Caption = "Rate this page", |
| 115 | + OnValueChanged = setRating, |
| 116 | + }, |
| 117 | + |
| 118 | + TextBlock($"current rating: {rating:0.0}"), |
| 119 | + |
| 120 | + HStack(8, |
| 121 | + Button("Reset", () => setRating(0)), |
| 122 | + Button("5 stars", () => setRating(5))) |
| 123 | + ).Padding(20); |
| 124 | + } |
| 125 | +} |
| 126 | +// </snippet:star-meter-usage> |
0 commit comments