@native-sdk/cli 0.4.4 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/build/app.zig +371 -5
  2. package/build/ts_run.mjs +102 -0
  3. package/build.zig +255 -1
  4. package/build.zig.zon +1 -0
  5. package/package.json +19 -10
  6. package/packages/core/package-lock.json +44 -0
  7. package/packages/core/package.json +38 -0
  8. package/packages/core/rt/rt.zig +1579 -0
  9. package/packages/core/sdk/bytes_text_methods.d.ts +88 -0
  10. package/packages/core/sdk/core.ts +694 -0
  11. package/packages/core/sdk/events.ts +137 -0
  12. package/packages/core/sdk/text.ts +519 -0
  13. package/packages/core/src/checker.ts +1869 -0
  14. package/packages/core/src/cli.ts +68 -0
  15. package/packages/core/src/devhost.ts +291 -0
  16. package/packages/core/src/devhost_resolver.mjs +22 -0
  17. package/packages/core/src/diagnostics.ts +417 -0
  18. package/packages/core/src/emitter.ts +8815 -0
  19. package/packages/core/src/infer.ts +1515 -0
  20. package/packages/core/src/modules.ts +173 -0
  21. package/packages/core/src/ownership.ts +740 -0
  22. package/packages/core/src/text_polyfill.ts +337 -0
  23. package/packages/core/src/text_tables.ts +408 -0
  24. package/packages/core/src/transpile.ts +146 -0
  25. package/packages/core/src/typed_ast.ts +546 -0
  26. package/packages/core/src/types.ts +815 -0
  27. package/skill-data/native-ui/SKILL.md +14 -0
  28. package/skill-data/ts-core/SKILL.md +493 -0
  29. package/skill-data/zig/SKILL.md +10 -0
  30. package/src/app_runner/root.zig +24 -0
  31. package/src/app_runner/ts_core_main.zig +231 -0
  32. package/src/platform/windows/webview2_host.cpp +24 -0
  33. package/src/primitives/app_manifest/comptime_scene.zig +341 -0
  34. package/src/primitives/app_manifest/root.zig +5 -0
  35. package/src/primitives/canvas/commands.zig +14 -7
  36. package/src/primitives/canvas/layout_audit.zig +5 -5
  37. package/src/primitives/canvas/lazy_tls.zig +73 -0
  38. package/src/primitives/canvas/render_generic_resources.zig +22 -15
  39. package/src/primitives/canvas/root.zig +11 -0
  40. package/src/primitives/canvas/tests.zig +1 -0
  41. package/src/primitives/canvas/text_atlas.zig +40 -32
  42. package/src/primitives/canvas/text_layout_cache.zig +29 -22
  43. package/src/primitives/canvas/text_measure_cache.zig +56 -41
  44. package/src/primitives/canvas/text_spans.zig +39 -26
  45. package/src/primitives/canvas/tokens.zig +69 -0
  46. package/src/primitives/canvas/ui.zig +125 -0
  47. package/src/primitives/canvas/ui_markup.zig +22 -3
  48. package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
  49. package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
  50. package/src/primitives/canvas/ui_markup_contract.zig +68 -14
  51. package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
  52. package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
  53. package/src/primitives/canvas/ui_markup_view.zig +105 -1
  54. package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
  55. package/src/primitives/canvas/ui_schema.zig +8 -0
  56. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  57. package/src/primitives/canvas/widget_builtin_tests.zig +309 -5
  58. package/src/primitives/canvas/widget_invalidation.zig +14 -7
  59. package/src/primitives/canvas/widget_layout.zig +138 -2
  60. package/src/primitives/canvas/widget_metrics.zig +16 -8
  61. package/src/primitives/canvas/widget_render.zig +25 -15
  62. package/src/primitives/canvas/widget_render_controls.zig +15 -9
  63. package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
  64. package/src/root.zig +7 -0
  65. package/src/runtime/canvas_frame.zig +57 -36
  66. package/src/runtime/canvas_images.zig +9 -3
  67. package/src/runtime/canvas_widget_control_tests.zig +5 -6
  68. package/src/runtime/canvas_widget_runtime.zig +7 -4
  69. package/src/runtime/core.zig +16 -0
  70. package/src/runtime/effects.zig +1525 -133
  71. package/src/runtime/effects_fetch_tests.zig +42 -0
  72. package/src/runtime/effects_file_tests.zig +220 -0
  73. package/src/runtime/effects_host_tests.zig +594 -0
  74. package/src/runtime/effects_tests.zig +287 -0
  75. package/src/runtime/root.zig +18 -0
  76. package/src/runtime/session_replay.zig +7 -1
  77. package/src/runtime/tests.zig +2 -0
  78. package/src/runtime/ts_core_host.zig +1505 -0
  79. package/src/runtime/ts_core_host_tests.zig +1408 -0
  80. package/src/runtime/ts_ui_app.zig +469 -0
  81. package/src/runtime/ui_app.zig +189 -38
  82. package/src/runtime/ui_app_tests.zig +221 -0
  83. package/src/runtime/ui_app_window_tests.zig +182 -0
  84. package/src/runtime/view_canvas.zig +24 -17
  85. package/src/runtime/view_widget_tree.zig +2 -2
  86. package/src/tooling/codesign.zig +212 -105
  87. package/src/tooling/doctor.zig +78 -0
  88. package/src/tooling/manifest.zig +47 -0
  89. package/src/tooling/package.zig +435 -20
  90. package/src/tooling/raw_manifest.zig +15 -0
  91. package/src/tooling/root.zig +1 -0
  92. package/src/tooling/templates.zig +777 -34
  93. package/src/tooling/ts_core.zig +1099 -0
  94. package/src/tooling/verbs.zig +105 -2
@@ -0,0 +1,469 @@
1
+ //! `TsUiApp(core)` — the first-class UiApp adapter for transpiled app
2
+ //! cores: the committed TS model IS the app model. Where a Zig core
3
+ //! hands `UiApp` a mutable model plus `update`, a transpiled core is an
4
+ //! immutable committed graph plus a pure `update` returning the next
5
+ //! root — this adapter closes that gap with no per-app glue:
6
+ //!
7
+ //! Model = core.Model — the emitted struct itself. Markup views
8
+ //! (`canvas.CompiledMarkupView(core.Model, core.Msg, src)`)
9
+ //! and Zig builder views bind its fields directly; the
10
+ //! binding names are the TS interface's own field names
11
+ //! (`lastTickAt` binds as `{lastTickAt}` — the emitted
12
+ //! struct keeps the TS spellings), and record arrays /
13
+ //! nested records bind through markup's `*const`
14
+ //! transparency.
15
+ //! update = the bridge (`TsCoreHost(core)`): every Msg runs the
16
+ //! core's dispatch cycle — update, commit, command walk,
17
+ //! subscription reconcile — and the UiApp-held root is
18
+ //! refreshed to the committed value. Every pointer inside
19
+ //! it IS the committed graph (valid until the next
20
+ //! dispatch, exactly a view build's lifetime).
21
+ //! init = the core's `initialModel`: the boot model commits at
22
+ //! construction (so `tokens_fn`, pre-install appearance /
23
+ //! chrome dispatches, and the installing view build all
24
+ //! read real state), and the boot command + initial
25
+ //! subscriptions fire through `init_fx` on the installing
26
+ //! frame — the same init semantics Zig cores get.
27
+ //!
28
+ //! THE HOST-EVENT CHANNELS are the core's own, comptime-detected from
29
+ //! its exports (export exists -> wired; a wiring that also sets the
30
+ //! seam is a teaching panic): `frameMsg(model, frame)` -> `on_frame`,
31
+ //! `keyMsg(key)` -> `on_key`, and the arm exports `appearanceMsg` /
32
+ //! `chromeMsg` -> `on_appearance`/`on_chrome`, each host event built
33
+ //! structurally by field name from the core's declared records (the
34
+ //! effects-routing rule applied to the app shell; every shape mismatch
35
+ //! is a teaching compile error re-deriving the transpiler's NS1033).
36
+ //! `CoreOptions` carries the launch-boundary channels the wiring
37
+ //! resolves: `boot_images` (app.zon assets, registered on the
38
+ //! installing frame) and `env_values` (the core's `envMsgs` variables,
39
+ //! dispatched as journaled Msgs right after the boot command).
40
+ //!
41
+ //! Everything else on `UiApp.Options` is the wiring's, unchanged: view
42
+ //! or markup, scene, `on_command` maps command ids through the core's
43
+ //! `commandMsg`, `tokens_fn`/`windows_fn`/`status_item_fn` derive from
44
+ //! the committed model. The two seams the core owns — `update_fx` and
45
+ //! `init_fx` — are stamped by this adapter and must be left null.
46
+ //!
47
+ //! `Options.sync` is deliberately unsupported: it mutates the model in
48
+ //! place, which cannot exist for a committed graph. TS apps keep
49
+ //! continuous controls model-driven (`on-change`/`on-scroll` Msgs echo
50
+ //! the value back into the model — the pattern UiApp already supports).
51
+ //!
52
+ //! Record/replay, automation, and pixel fingerprints need nothing
53
+ //! extra: the adapter rides the ordinary UiApp dispatch path, so the
54
+ //! session journal, the automation verbs, and the screenshot marks see
55
+ //! a transpiled core exactly as they see a Zig one. The v1 process
56
+ //! contract is the bridge's: one live app per core module (two apps
57
+ //! over one emitted core would share a committed root; distinct core
58
+ //! modules coexist).
59
+
60
+ const std = @import("std");
61
+ const canvas = @import("canvas");
62
+ const platform = @import("../platform/root.zig");
63
+ const ui_app = @import("ui_app.zig");
64
+ const ts_core_host = @import("ts_core_host.zig");
65
+
66
+ pub fn TsUiApp(comptime core: type) type {
67
+ return struct {
68
+ /// The effect bridge — shared with any direct `TsCoreHost(core)`
69
+ /// instantiation (comptime memoization), so harnesses can read
70
+ /// `Host.model()` and tests can name the bridge's key bases.
71
+ pub const Host = ts_core_host.TsCoreHost(core);
72
+ pub const Model = core.Model;
73
+ pub const Msg = core.Msg;
74
+ pub const App = ui_app.UiApp(Model, Msg);
75
+ pub const Options = App.Options;
76
+ pub const Effects = App.Effects;
77
+ pub const Ui = App.Ui;
78
+
79
+ /// One boot-registered image: the wiring reads the encoded bytes
80
+ /// (app.zon's `.assets.images` paths) and the adapter registers
81
+ /// them on the installing frame — `fx.registerImageBytes`, the
82
+ /// Zig apps' `init_fx` convention. A failed decode skips the
83
+ /// entry: views keep their fallback (avatar initials), a bad
84
+ /// asset never breaks presentation.
85
+ pub const BootImage = struct {
86
+ id: u64,
87
+ bytes: []const u8,
88
+ };
89
+
90
+ /// One launch-time environment override (the core's `envMsgs`
91
+ /// channel): `msg` names the core's one-bytes-field arm, `value`
92
+ /// the variable's bytes. Dispatched as ordinary Msgs right after
93
+ /// the boot command on the installing frame — each delivery is
94
+ /// journaled (an `.env` effect record), so replay feeds the
95
+ /// recorded values and never re-reads the environment (see
96
+ /// `dispatchEnvValues`).
97
+ pub const EnvValue = struct {
98
+ msg: []const u8,
99
+ value: []const u8,
100
+ };
101
+
102
+ /// Adapter-owned configuration — the knobs that exist because
103
+ /// the core is transpiled, kept separate from `App.Options` so
104
+ /// the wiring surface reads as ordinary UiApp wiring.
105
+ pub const CoreOptions = struct {
106
+ /// Platform caches directory for URL audio playback: when a
107
+ /// core's `Cmd.audioPlay` names a URL with no cachePath, the
108
+ /// bridge derives the engine's conventional content-addressed
109
+ /// path under this directory (soundboard's convention,
110
+ /// resolved by the wiring at boot via `app_dirs` — never
111
+ /// read from the environment inside update). Empty disables
112
+ /// derivation: URL playback still works, it just re-streams.
113
+ audio_cache_dir: []const u8 = "",
114
+ /// Images registered at install, before the first view build
115
+ /// (see `BootImage`). The slices must outlive install (the
116
+ /// wiring reads them into launch-lifetime buffers).
117
+ boot_images: []const BootImage = &.{},
118
+ /// Launch-time environment overrides (see `EnvValue`), only
119
+ /// meaningful for cores exporting `envMsgs`. The slices must
120
+ /// outlive install.
121
+ env_values: []const EnvValue = &.{},
122
+ };
123
+
124
+ /// Construct the UiApp over the committed TS model. `options`
125
+ /// carries the wiring seams only — `update`, `update_fx`, and
126
+ /// `init_fx` belong to the core and must be null; `sync` cannot
127
+ /// exist for a committed model (see the module doc).
128
+ pub fn init(backing: std.mem.Allocator, core_options: CoreOptions, options: Options) App {
129
+ const stamped = stampOptions(options);
130
+ Host.boot();
131
+ applyCoreOptions(core_options);
132
+ return App.init(backing, Host.model().*, stamped);
133
+ }
134
+
135
+ /// Heap counterpart of `init`, mirroring `UiApp.create`: the app
136
+ /// struct (and any real model) is multi-MB, so construct it in
137
+ /// place on the heap — the shape generated wiring and `main`
138
+ /// functions use. Pair with `App.destroy`.
139
+ pub fn create(backing: std.mem.Allocator, core_options: CoreOptions, options: Options) error{OutOfMemory}!*App {
140
+ const stamped = stampOptions(options);
141
+ Host.boot();
142
+ applyCoreOptions(core_options);
143
+ const self = try backing.create(App);
144
+ App.initInPlace(self, backing, stamped);
145
+ self.model = Host.model().*;
146
+ return self;
147
+ }
148
+
149
+ /// Adapter-held install state (container-level like the bridge's
150
+ /// tables — one live app per core module is the v1 contract):
151
+ /// `initFx` is a plain fn pointer, so the boot images and env
152
+ /// values it performs ride here between construction and the
153
+ /// installing frame.
154
+ var boot_images_store: []const BootImage = &.{};
155
+ var env_values_store: []const EnvValue = &.{};
156
+
157
+ fn applyCoreOptions(core_options: CoreOptions) void {
158
+ Host.setAudioCacheDir(core_options.audio_cache_dir);
159
+ boot_images_store = core_options.boot_images;
160
+ env_values_store = core_options.env_values;
161
+ if (core_options.env_values.len > 0 and comptime !@hasDecl(core, "envMsgs")) {
162
+ @panic("TsUiApp received env_values but the core exports no envMsgs channel - declare `export const envMsgs = [{ env: \"NAME\", msg: \"<arm>\" }] as const` in core.ts");
163
+ }
164
+ }
165
+
166
+ fn stampOptions(options: Options) Options {
167
+ if (options.update != null or options.update_fx != null) {
168
+ @panic("TsUiApp owns update: the transpiled core is the update loop - remove the wiring's update/update_fx");
169
+ }
170
+ if (options.init_fx != null) {
171
+ @panic("TsUiApp owns init_fx: the core's initialModel boots the app - remove the wiring's init_fx");
172
+ }
173
+ if (options.sync != null) {
174
+ @panic("TsUiApp does not support Options.sync: a committed model cannot be mutated in place - echo widget state through on-change/on-scroll Msgs instead");
175
+ }
176
+ var stamped = options;
177
+ stamped.init_fx = initFx;
178
+ stamped.update_fx = updateFx;
179
+ // The core's host-event channels, comptime-detected from its
180
+ // exports (export exists -> wired; every shape mismatch is a
181
+ // teaching compile error in the adapter below). A wiring that
182
+ // also set the seam would silently shadow the core's channel,
183
+ // so that conflict is a loud teaching panic.
184
+ if (comptime @hasDecl(core, "frameMsg")) {
185
+ if (options.on_frame != null) {
186
+ @panic("TsUiApp wires on_frame from the core's frameMsg export - remove the wiring's on_frame");
187
+ }
188
+ stamped.on_frame = frameMsgAdapter;
189
+ }
190
+ if (comptime @hasDecl(core, "keyMsg")) {
191
+ if (options.on_key != null) {
192
+ @panic("TsUiApp wires on_key from the core's keyMsg export - remove the wiring's on_key");
193
+ }
194
+ stamped.on_key = keyMsgAdapter;
195
+ }
196
+ if (comptime @hasDecl(core, "appearanceMsg")) {
197
+ if (options.on_appearance != null) {
198
+ @panic("TsUiApp wires on_appearance from the core's appearanceMsg export - remove the wiring's on_appearance");
199
+ }
200
+ stamped.on_appearance = appearanceMsgAdapter;
201
+ }
202
+ if (comptime @hasDecl(core, "chromeMsg")) {
203
+ if (options.on_chrome != null) {
204
+ @panic("TsUiApp wires on_chrome from the core's chromeMsg export - remove the wiring's on_chrome");
205
+ }
206
+ stamped.on_chrome = chromeMsgAdapter;
207
+ }
208
+ return stamped;
209
+ }
210
+
211
+ /// `Options.init_fx`: register the wiring's boot images, perform
212
+ /// the boot command and initial subscriptions on the installing
213
+ /// frame (the boot model itself committed in `init`), dispatch
214
+ /// the launch environment overrides as ordinary journaled Msgs,
215
+ /// then refresh the app-held root.
216
+ fn initFx(model: *Model, fx: *Effects) void {
217
+ for (boot_images_store) |image| {
218
+ // Registration is synchronous; a failed decode leaves the
219
+ // views on their fallback (avatar initials) — a bad asset
220
+ // never breaks presentation (the Zig apps' convention).
221
+ _ = fx.registerImageBytes(image.id, image.bytes) catch continue;
222
+ }
223
+ Host.performBoot(fx);
224
+ dispatchEnvValues(fx);
225
+ model.* = Host.model().*;
226
+ }
227
+
228
+ /// The `envMsgs` channel's delivery: each launch-resolved value
229
+ /// dispatches its named one-bytes-field arm — a full core cycle
230
+ /// per value, in declaration order, right after the boot command.
231
+ ///
232
+ /// Record/replay is the channel's whole point, so the values are
233
+ /// JOURNALED at record time (one `.env` effect record per
234
+ /// delivery, written during the installing frame's dispatch) and
235
+ /// FED from the journal under replay — zero env reads, so a
236
+ /// recording replays byte-identically even when the variables
237
+ /// are unset or changed at replay launch. Backward compatibility:
238
+ /// a journal with NO `.env` records (an older recording, or a
239
+ /// launch with no variables set) re-derives from the launch
240
+ /// configuration exactly as before.
241
+ fn dispatchEnvValues(fx: *Effects) void {
242
+ if (comptime !@hasDecl(core, "envMsgs")) return;
243
+ comptime validateEnvMsgs();
244
+ if (fx.replay and fx.replay_env_len > 0) {
245
+ while (fx.takeReplayEnv()) |entry| dispatchOneEnvValue(fx, entry.msg, entry.value);
246
+ return;
247
+ }
248
+ for (env_values_store, 0..) |entry, index| {
249
+ fx.journalEnvValue(index, entry.msg, entry.value);
250
+ dispatchOneEnvValue(fx, entry.msg, entry.value);
251
+ }
252
+ }
253
+
254
+ /// One env delivery: resolve the arm by name and dispatch the
255
+ /// value through a full core cycle.
256
+ fn dispatchOneEnvValue(fx: *Effects, msg: []const u8, value: []const u8) void {
257
+ inline for (@typeInfo(Msg).@"union".fields) |arm| {
258
+ if (comptime arm.type == []const u8) {
259
+ if (std.mem.eql(u8, arm.name, msg)) {
260
+ // The value copies into the core's frame arena
261
+ // first, like every routed bytes payload: the
262
+ // commit walkers copy frame-resident bytes the
263
+ // model keeps into the heap.
264
+ const copy = core.rt.frameAlloc(u8, value.len);
265
+ @memcpy(copy, value);
266
+ Host.dispatch(fx, @unionInit(Msg, arm.name, copy));
267
+ }
268
+ }
269
+ }
270
+ }
271
+
272
+ /// Teaching re-derivation of the transpiler's NS1033 for
273
+ /// hand-assembled cores: every `envMsgs` entry must name a Msg
274
+ /// arm carrying exactly one bytes payload.
275
+ fn validateEnvMsgs() void {
276
+ for (core.envMsgs) |entry| {
277
+ var found = false;
278
+ for (@typeInfo(Msg).@"union".fields) |arm| {
279
+ if (std.mem.eql(u8, arm.name, entry.msg)) {
280
+ if (arm.type != []const u8) {
281
+ @compileError("TsUiApp: envMsgs entry '" ++ entry.env ++ "' targets Msg arm '" ++ entry.msg ++ "', whose payload is not one Uint8Array field");
282
+ }
283
+ found = true;
284
+ }
285
+ }
286
+ if (!found) {
287
+ @compileError("TsUiApp: envMsgs entry '" ++ entry.env ++ "' names '" ++ entry.msg ++ "', which is not an arm of Msg");
288
+ }
289
+ }
290
+ }
291
+
292
+ /// Widen one host number into a channel record's declared field
293
+ /// class: floats take the value exactly, integer-classed fields
294
+ /// round to the nearest whole number.
295
+ fn channelNum(comptime N: type, value: f64) N {
296
+ return if (@typeInfo(N) == .float) @floatCast(value) else @intFromFloat(@round(value));
297
+ }
298
+
299
+ /// `Options.on_frame` over the core's `frameMsg(model, frame)`
300
+ /// export: the emitted FrameEvent record — `width`/`height`
301
+ /// (canvas points) plus `timestampMs`/`intervalMs` (fractional
302
+ /// milliseconds, the timer-fire clock; emitted fields keep their
303
+ /// TS names) — built by field NAME from
304
+ /// the presented frame. The core's return gates the channel
305
+ /// exactly like a Zig `on_frame` (null while idle keeps the idle
306
+ /// law: no Msg, no rebuild, the frame channel starves on its own).
307
+ fn frameMsgAdapter(model: *const Model, frame: platform.GpuFrame) ?Msg {
308
+ const params = @typeInfo(@TypeOf(core.frameMsg)).@"fn".params;
309
+ if (comptime (params.len != 2 or params[0].type != *const Model)) {
310
+ @compileError("TsUiApp: frameMsg must take (model: Model, frame: FrameEvent) - regenerate the core");
311
+ }
312
+ const FrameArg = params[1].type.?;
313
+ comptime validateChannelRecord(FrameArg, &.{ "width", "height", "timestampMs", "intervalMs" }, "frameMsg's FrameEvent");
314
+ var arg: FrameArg = undefined;
315
+ inline for (@typeInfo(FrameArg).@"struct".fields) |field| {
316
+ const value: f64 = if (comptime std.mem.eql(u8, field.name, "width"))
317
+ frame.size.width
318
+ else if (comptime std.mem.eql(u8, field.name, "height"))
319
+ frame.size.height
320
+ else if (comptime std.mem.eql(u8, field.name, "timestampMs"))
321
+ @as(f64, @floatFromInt(frame.timestamp_ns)) / std.time.ns_per_ms
322
+ else
323
+ @as(f64, @floatFromInt(frame.frame_interval_ns)) / std.time.ns_per_ms;
324
+ @field(arg, field.name) = channelNum(field.type, value);
325
+ }
326
+ return core.frameMsg(model, arg);
327
+ }
328
+
329
+ /// `Options.on_key` over the core's `keyMsg(key)` export: the
330
+ /// emitted KeyEvent record — the key NAME (lowercased, so
331
+ /// `key.key === "space"` compares the way the Zig examples'
332
+ /// case-insensitive checks do) plus the four modifier booleans.
333
+ /// The UiApp precedence rule applies before this fires: focused
334
+ /// widgets consume their own keys, editable text keeps typing.
335
+ fn keyMsgAdapter(keyboard: canvas.WidgetKeyboardEvent) ?Msg {
336
+ const params = @typeInfo(@TypeOf(core.keyMsg)).@"fn".params;
337
+ if (comptime params.len != 1) {
338
+ @compileError("TsUiApp: keyMsg must take one KeyEvent parameter - regenerate the core");
339
+ }
340
+ const KeyArg = params[0].type.?;
341
+ comptime {
342
+ const fields = @typeInfo(KeyArg).@"struct".fields;
343
+ if (fields.len != 5 or !@hasField(KeyArg, "key") or !@hasField(KeyArg, "shift") or
344
+ !@hasField(KeyArg, "control") or !@hasField(KeyArg, "alt") or !@hasField(KeyArg, "super"))
345
+ {
346
+ @compileError("TsUiApp: keyMsg's KeyEvent must be exactly { key: string; shift: boolean; control: boolean; alt: boolean; super: boolean }");
347
+ }
348
+ }
349
+ // The key name copies lowercased into the core's frame arena:
350
+ // the arena is empty between dispatches, and a Msg the core
351
+ // builds from it commits like every routed bytes payload.
352
+ const lowered = core.rt.frameAlloc(u8, keyboard.key.len);
353
+ for (keyboard.key, 0..) |c, index| lowered[index] = std.ascii.toLower(c);
354
+ var arg: KeyArg = undefined;
355
+ arg.key = lowered;
356
+ arg.shift = keyboard.modifiers.shift;
357
+ arg.control = keyboard.modifiers.control;
358
+ arg.alt = keyboard.modifiers.alt;
359
+ arg.super = keyboard.modifiers.super;
360
+ return core.keyMsg(arg);
361
+ }
362
+
363
+ /// `Options.on_appearance` over the core's `appearanceMsg` arm
364
+ /// export: the appearance record — `colorScheme` (a declared
365
+ /// light/dark enum, matched by member name), `reduceMotion`,
366
+ /// `highContrast` — built by field NAME (emitted fields keep
367
+ /// their TS names), always dispatched (the channel exists so the
368
+ /// MODEL owns appearance state).
369
+ fn appearanceMsgAdapter(appearance: platform.Appearance) ?Msg {
370
+ const arm_index = comptime channelArmIndex(core.appearanceMsg, "appearanceMsg");
371
+ const arm = @typeInfo(Msg).@"union".fields[arm_index];
372
+ comptime validateAppearanceArm(arm.type);
373
+ var payload: arm.type = undefined;
374
+ payload.reduceMotion = appearance.reduce_motion;
375
+ payload.highContrast = appearance.high_contrast;
376
+ const Scheme = @FieldType(arm.type, "colorScheme");
377
+ payload.colorScheme = switch (appearance.color_scheme) {
378
+ inline else => |scheme| @field(Scheme, @tagName(scheme)),
379
+ };
380
+ return @unionInit(Msg, arm.name, payload);
381
+ }
382
+
383
+ /// `Options.on_chrome` over the core's `chromeMsg` arm export:
384
+ /// the chrome record — `insets` (top/right/bottom/left), `buttons`
385
+ /// (x/y/width/height), `tabsProjected` — built by field NAME from
386
+ /// the window-chrome geometry, delivered before the first view
387
+ /// build and again whenever it changes.
388
+ fn chromeMsgAdapter(chrome: platform.WindowChrome) ?Msg {
389
+ const arm_index = comptime channelArmIndex(core.chromeMsg, "chromeMsg");
390
+ const arm = @typeInfo(Msg).@"union".fields[arm_index];
391
+ comptime validateChromeArm(arm.type);
392
+ var payload: arm.type = undefined;
393
+ const Insets = @FieldType(arm.type, "insets");
394
+ payload.insets = .{
395
+ .top = channelNum(@FieldType(Insets, "top"), chrome.insets.top),
396
+ .right = channelNum(@FieldType(Insets, "right"), chrome.insets.right),
397
+ .bottom = channelNum(@FieldType(Insets, "bottom"), chrome.insets.bottom),
398
+ .left = channelNum(@FieldType(Insets, "left"), chrome.insets.left),
399
+ };
400
+ const Buttons = @FieldType(arm.type, "buttons");
401
+ payload.buttons = .{
402
+ .x = channelNum(@FieldType(Buttons, "x"), chrome.buttons.x),
403
+ .y = channelNum(@FieldType(Buttons, "y"), chrome.buttons.y),
404
+ .width = channelNum(@FieldType(Buttons, "width"), chrome.buttons.width),
405
+ .height = channelNum(@FieldType(Buttons, "height"), chrome.buttons.height),
406
+ };
407
+ payload.tabsProjected = chrome.tabs_projected;
408
+ return @unionInit(Msg, arm.name, payload);
409
+ }
410
+
411
+ /// The Msg arm index a channel export names, with the teaching
412
+ /// error the transpiler's NS1033 re-derives for hand-written
413
+ /// cores.
414
+ fn channelArmIndex(comptime tag: []const u8, comptime channel: []const u8) usize {
415
+ for (@typeInfo(Msg).@"union".fields, 0..) |arm, index| {
416
+ if (std.mem.eql(u8, arm.name, tag)) return index;
417
+ }
418
+ @compileError("TsUiApp: " ++ channel ++ " names '" ++ tag ++ "', which is not an arm of Msg");
419
+ }
420
+
421
+ fn validateChannelRecord(comptime T: type, comptime names: []const []const u8, comptime what: []const u8) void {
422
+ const info = @typeInfo(T);
423
+ if (info != .@"struct" or info.@"struct".fields.len != names.len) {
424
+ @compileError("TsUiApp: " ++ what ++ " record has the wrong field set");
425
+ }
426
+ for (names) |name| {
427
+ if (!@hasField(T, name)) {
428
+ @compileError("TsUiApp: " ++ what ++ " record is missing field '" ++ name ++ "'");
429
+ }
430
+ }
431
+ for (info.@"struct".fields) |field| {
432
+ if (field.type != i64 and field.type != f64 and field.type != f32) {
433
+ @compileError("TsUiApp: " ++ what ++ " field '" ++ field.name ++ "' must be a number");
434
+ }
435
+ }
436
+ }
437
+
438
+ fn validateAppearanceArm(comptime T: type) void {
439
+ const teaching = "TsUiApp: appearanceMsg's arm must carry exactly { colorScheme: a named light/dark alias; reduceMotion: boolean; highContrast: boolean }";
440
+ const info = @typeInfo(T);
441
+ if (info != .@"struct" or info.@"struct".fields.len != 3) @compileError(teaching);
442
+ if (!@hasField(T, "colorScheme") or !@hasField(T, "reduceMotion") or !@hasField(T, "highContrast")) @compileError(teaching);
443
+ const Scheme = @FieldType(T, "colorScheme");
444
+ const scheme_info = @typeInfo(Scheme);
445
+ if (scheme_info != .@"enum" or scheme_info.@"enum".fields.len != 2 or
446
+ !@hasField(Scheme, "light") or !@hasField(Scheme, "dark")) @compileError(teaching);
447
+ if (@FieldType(T, "reduceMotion") != bool or @FieldType(T, "highContrast") != bool) @compileError(teaching);
448
+ }
449
+
450
+ fn validateChromeArm(comptime T: type) void {
451
+ const teaching = "TsUiApp: chromeMsg's arm must carry exactly { insets: top/right/bottom/left numbers; buttons: x/y/width/height numbers; tabsProjected: boolean }";
452
+ const info = @typeInfo(T);
453
+ if (info != .@"struct" or info.@"struct".fields.len != 3) @compileError(teaching);
454
+ if (!@hasField(T, "insets") or !@hasField(T, "buttons") or !@hasField(T, "tabsProjected")) @compileError(teaching);
455
+ if (@FieldType(T, "tabsProjected") != bool) @compileError(teaching);
456
+ validateChannelRecord(@FieldType(T, "insets"), &.{ "top", "right", "bottom", "left" }, "chromeMsg's insets");
457
+ validateChannelRecord(@FieldType(T, "buttons"), &.{ "x", "y", "width", "height" }, "chromeMsg's buttons");
458
+ }
459
+
460
+ /// `Options.update_fx`: one full core dispatch cycle, then the
461
+ /// app-held root becomes the new committed value. The incoming
462
+ /// model pointer is the previous root value — the bridge holds
463
+ /// the authoritative one, so it is overwritten, never read.
464
+ fn updateFx(model: *Model, msg: Msg, fx: *Effects) void {
465
+ Host.dispatch(fx, msg);
466
+ model.* = Host.model().*;
467
+ }
468
+ };
469
+ }