@native-sdk/cli 0.4.4 → 0.5.0

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 (83) hide show
  1. package/build/app.zig +187 -6
  2. package/build/ts_run.mjs +63 -0
  3. package/build.zig +213 -1
  4. package/build.zig.zon +1 -0
  5. package/package.json +15 -10
  6. package/packages/core/package-lock.json +43 -0
  7. package/packages/core/package.json +37 -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 +536 -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/primitives/app_manifest/comptime_scene.zig +341 -0
  33. package/src/primitives/app_manifest/root.zig +5 -0
  34. package/src/primitives/canvas/commands.zig +14 -7
  35. package/src/primitives/canvas/lazy_tls.zig +73 -0
  36. package/src/primitives/canvas/render_generic_resources.zig +22 -15
  37. package/src/primitives/canvas/root.zig +6 -0
  38. package/src/primitives/canvas/tests.zig +1 -0
  39. package/src/primitives/canvas/text_atlas.zig +40 -32
  40. package/src/primitives/canvas/text_layout_cache.zig +29 -22
  41. package/src/primitives/canvas/text_measure_cache.zig +56 -41
  42. package/src/primitives/canvas/text_spans.zig +39 -26
  43. package/src/primitives/canvas/tokens.zig +54 -0
  44. package/src/primitives/canvas/ui.zig +125 -0
  45. package/src/primitives/canvas/ui_markup.zig +22 -3
  46. package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
  47. package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
  48. package/src/primitives/canvas/ui_markup_contract.zig +68 -14
  49. package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
  50. package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
  51. package/src/primitives/canvas/ui_markup_view.zig +105 -1
  52. package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
  53. package/src/primitives/canvas/ui_schema.zig +8 -0
  54. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  55. package/src/primitives/canvas/widget_invalidation.zig +14 -7
  56. package/src/primitives/canvas/widget_render.zig +25 -15
  57. package/src/primitives/canvas/widget_render_controls.zig +15 -9
  58. package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
  59. package/src/root.zig +7 -0
  60. package/src/runtime/canvas_frame.zig +57 -36
  61. package/src/runtime/canvas_images.zig +9 -3
  62. package/src/runtime/canvas_widget_control_tests.zig +5 -6
  63. package/src/runtime/canvas_widget_runtime.zig +7 -4
  64. package/src/runtime/effects.zig +490 -2
  65. package/src/runtime/effects_host_tests.zig +594 -0
  66. package/src/runtime/root.zig +18 -0
  67. package/src/runtime/session_replay.zig +7 -1
  68. package/src/runtime/tests.zig +2 -0
  69. package/src/runtime/ts_core_host.zig +1505 -0
  70. package/src/runtime/ts_core_host_tests.zig +1408 -0
  71. package/src/runtime/ts_ui_app.zig +469 -0
  72. package/src/runtime/ui_app.zig +58 -0
  73. package/src/runtime/view_canvas.zig +24 -17
  74. package/src/runtime/view_widget_tree.zig +2 -2
  75. package/src/tooling/codesign.zig +212 -105
  76. package/src/tooling/doctor.zig +78 -0
  77. package/src/tooling/manifest.zig +47 -0
  78. package/src/tooling/package.zig +124 -12
  79. package/src/tooling/raw_manifest.zig +15 -0
  80. package/src/tooling/root.zig +1 -0
  81. package/src/tooling/templates.zig +683 -28
  82. package/src/tooling/ts_core.zig +486 -0
  83. package/src/tooling/verbs.zig +17 -0
@@ -142,6 +142,16 @@ const term = try child.wait(io); // child.kill(io) to stop it
142
142
 
143
143
  `std.process.getEnvMap`, `argsAlloc`, and `argsWithAllocator` are gone. `main` already has both: `init.environ_map` (a `*std.process.Environ.Map`) and `init.minimal.args.toSlice(allocator)` (`tools/native-sdk/main.zig`). Building an environment from scratch — for a child process, or in tests — is `var env = std.process.Environ.Map.init(allocator); defer env.deinit(); try env.put("KEY", "value");` (`src/tooling/dev.zig`).
144
144
 
145
+ ## error: struct 'mem' has no member named 'trimRight' / 'trimLeft' — renamed `trimEnd` / `trimStart`
146
+
147
+ The directional trims renamed to match the JS/string convention; arguments are unchanged:
148
+
149
+ ```zig
150
+ const line = std.mem.trimEnd(u8, raw, "\r\n"); // was trimRight
151
+ const body = std.mem.trimStart(u8, line, " \t"); // was trimLeft
152
+ const both = std.mem.trim(u8, text, " "); // unchanged
153
+ ```
154
+
145
155
  ## error: struct 'std' has no member named 'net' — sockets live on `std.Io.net`
146
156
 
147
157
  `std.net.Address` became `std.Io.net.IpAddress`, and connect/listen take `io`: `std.Io.net.IpAddress.resolve(io, host, port)`, then `IpAddress.connect(&address, io, .{ .mode = .stream, .protocol = .tcp })`; stream readers/writers follow the buffered-writer shape (`std.Io.net.Stream.writer(stream, io, &buffer)` then `.interface`). Live reference: `src/tooling/dev.zig` (`waitUntilReady`/`httpReady`). App-level HTTP belongs in `fx.fetch`, not hand-rolled sockets.
@@ -254,6 +254,30 @@ pub fn manifestThemePack() native_sdk.canvas.ThemePack {
254
254
  @compileError("unknown app.zon theme \"" ++ name ++ "\" — expected one of: house, geist");
255
255
  }
256
256
 
257
+ /// The manifest's ONE-accent brand override (`theme_accent = "#df2670"`),
258
+ /// resolved at comptime so a malformed value is a build error naming the
259
+ /// field — never a silent fallback. Absent means the pack's own accent.
260
+ /// Apps hand this to their `UiApp` options' `theme_accent` field; the
261
+ /// runtime layers `canvas.accentOverrides` over the resolved pack (and
262
+ /// skips it under high contrast — accessibility beats brand).
263
+ pub fn manifestThemeAccent() ?native_sdk.canvas.Color {
264
+ if (comptime !@hasField(@TypeOf(app_manifest), "theme_accent")) return null;
265
+ const value: []const u8 = app_manifest.theme_accent;
266
+ return comptime parseHexColor(value) orelse
267
+ @compileError("invalid app.zon theme_accent \"" ++ value ++ "\" — expected a #rrggbb hex color");
268
+ }
269
+
270
+ fn parseHexColor(comptime value: []const u8) ?native_sdk.canvas.Color {
271
+ comptime {
272
+ if (value.len != 7 or value[0] != '#') return null;
273
+ var channels: [3]u8 = undefined;
274
+ for (&channels, 0..) |*channel, index| {
275
+ channel.* = std.fmt.parseInt(u8, value[1 + index * 2 .. 3 + index * 2], 16) catch return null;
276
+ }
277
+ return native_sdk.canvas.Color.rgb8(channels[0], channels[1], channels[2]);
278
+ }
279
+ }
280
+
257
281
  /// Whether app.zon declares web content — the shared declare-to-use
258
282
  /// contract (`native_sdk.app_manifest.web_layer`) over the comptime
259
283
  /// manifest import: a `.frontend` block, the `"webview"` capability, a
@@ -0,0 +1,231 @@
1
+ //! The generated wiring for a TypeScript app core — staged (never written
2
+ //! into the app) by the framework build when the tree carries src/core.ts:
3
+ //! the build transpiles the core beside this file as core.zig, copies the
4
+ //! app's src/app.native beside it too, and roots the app module here. The
5
+ //! app tree stays three files of truth — core.ts (logic), app.native
6
+ //! (view), app.zon (manifest) — and every value below derives from them at
7
+ //! comptime:
8
+ //!
9
+ //! scene / canvas app.zon's `.shell` through `shellConfigFrom`; the
10
+ //! canvas is the scene's first gpu_surface view.
11
+ //! identity app.zon's `.id`/`.name`/`.display_name`.
12
+ //! security app.zon's `.permissions` and navigation origins.
13
+ //! theme app.zon's `.theme` pack; the stock tokens compose
14
+ //! it with the live system appearance. `.theme_accent`
15
+ //! layers the manifest's one-accent brand override
16
+ //! (`canvas.accentOverrides`) over the pack.
17
+ //! audio cache the platform caches directory (app_dirs `.cache`),
18
+ //! resolved at launch so a core's URL audio playback
19
+ //! caches under the conventional content-addressed
20
+ //! path with no `cachePath` in the core.
21
+ //! update loop the transpiled core through `TsUiApp(core)` — the
22
+ //! committed TS model IS the app model.
23
+ //! view app.native over the model's own field names (the
24
+ //! emitted Zig keeps the TS spellings), hot-reloaded
25
+ //! from src/app.native in Debug.
26
+ //! menus/shortcuts a core exporting `commandMsg(name): Msg | null`
27
+ //! receives command events (menus, shortcuts, chrome
28
+ //! tabs) as ordinary Msgs; without the export command
29
+ //! events stay host-handled only.
30
+ //! host events the adapter wires the core's opt-in channels from
31
+ //! its exports (export exists -> wired): `frameMsg`
32
+ //! (presented frames), `keyMsg` (the app-level key
33
+ //! fallback), `appearanceMsg`/`chromeMsg` (Msg arms
34
+ //! the host fills structurally).
35
+ //! images app.zon's `.assets.images` entries (`.{ .id = 1,
36
+ //! .path = "assets/..." }`) are read once at launch
37
+ //! and registered on the installing frame — the ids
38
+ //! are the `ImageId`s markup avatar bindings use. A
39
+ //! missing file or failed decode skips the entry
40
+ //! (views keep the initials fallback).
41
+ //! environment a core exporting `envMsgs` receives each named
42
+ //! variable present at launch as one journaled Msg on
43
+ //! its bytes arm, right after the boot command — the
44
+ //! core itself never reads the environment (NS1005),
45
+ //! and replay carries the recorded values.
46
+ //!
47
+ //! Editing this file is never core-level work: it carries no app logic and
48
+ //! regenerates from the SDK on every build.
49
+
50
+ const std = @import("std");
51
+ const runner = @import("runner");
52
+ const native_sdk = @import("native_sdk");
53
+ const manifest = @import("app_manifest_zon");
54
+ pub const core = @import("core.zig");
55
+
56
+ pub const panic = std.debug.FullPanic(native_sdk.debug.capturePanic);
57
+
58
+ /// Re-exported so the model-contract step (and any test) reflects the
59
+ /// core's real surface: `native check` verifies app.native against it.
60
+ pub const Model = core.Model;
61
+ pub const Msg = core.Msg;
62
+
63
+ const Adapter = native_sdk.TsUiApp(core);
64
+ const App = Adapter.App;
65
+
66
+ const shell_scene = native_sdk.app_manifest.shellConfigFrom(manifest);
67
+ const canvas_label = native_sdk.app_manifest.firstGpuSurfaceLabel(shell_scene);
68
+ pub const app_markup = @embedFile("app.native");
69
+
70
+ const app_permissions = manifestStringList(manifest, "permissions");
71
+ const allowed_origins = manifestAllowedOrigins();
72
+
73
+ pub fn main(init: std.process.Init) !void {
74
+ var options: Adapter.Options = .{
75
+ .name = manifest.name,
76
+ .scene = shell_scene,
77
+ .canvas_label = canvas_label,
78
+ .markup = .{ .source = app_markup, .watch_path = "src/app.native", .io = init.io },
79
+ // app.zon's theme pack; unthemed manifests get the house register.
80
+ // The stock tokens compose the pack with the LIVE system
81
+ // appearance, so TS apps follow the OS light/dark flip with no
82
+ // core code. `theme_accent` is the manifest's one-accent brand
83
+ // override, layered over the pack by the runtime (skipped under
84
+ // high contrast — accessibility beats brand).
85
+ .theme = comptime runner.manifestThemePack(),
86
+ .theme_accent = comptime runner.manifestThemeAccent(),
87
+ };
88
+ if (comptime @hasDecl(core, "commandMsg")) {
89
+ // Menus, shortcuts, and chrome tabs dispatch through the core's
90
+ // exported command mapper.
91
+ options.on_command = core.commandMsg;
92
+ }
93
+ // The platform caches directory for this app: when the core's
94
+ // `Cmd.audioPlay` names a URL with no cachePath, the bridge derives
95
+ // the conventional content-addressed path under this directory —
96
+ // resolved once at launch (never inside update), so replay's
97
+ // deterministic-init contract holds. Resolution failure just disables
98
+ // the cache: streams still play, they re-download.
99
+ var cache_dir_buffer: [512]u8 = undefined;
100
+ const audio_cache_dir = native_sdk.app_dirs.resolveOne(
101
+ .{ .name = manifest.name },
102
+ native_sdk.app_dirs.currentPlatform(),
103
+ native_sdk.debug.envFromMap(init.environ_map),
104
+ .cache,
105
+ &cache_dir_buffer,
106
+ ) catch "";
107
+ // app.zon-declared images, read once at launch (bounded; a missing or
108
+ // over-bound file skips its entry and the views keep their fallback)
109
+ // and registered by the adapter on the installing frame.
110
+ const manifest_images = comptime manifestImages();
111
+ var boot_images_buffer: [manifest_images.len]Adapter.BootImage = undefined;
112
+ var boot_image_count: usize = 0;
113
+ inline for (manifest_images) |asset| {
114
+ if (std.Io.Dir.cwd().readFileAlloc(init.io, asset.path, std.heap.page_allocator, .limited(max_boot_image_bytes))) |bytes| {
115
+ boot_images_buffer[boot_image_count] = .{ .id = asset.id, .bytes = bytes };
116
+ boot_image_count += 1;
117
+ } else |_| {}
118
+ }
119
+
120
+ // The core's launch-time environment channel (`envMsgs`): read each
121
+ // named variable once, here at the boundary — never inside update —
122
+ // and hand the present values to the adapter, which dispatches them
123
+ // as ordinary journaled Msgs right after the boot command.
124
+ var env_values_buffer: [envMsgsLen()]Adapter.EnvValue = undefined;
125
+ var env_value_count: usize = 0;
126
+ if (comptime @hasDecl(core, "envMsgs")) {
127
+ inline for (core.envMsgs) |entry| {
128
+ if (init.environ_map.get(entry.env)) |value| {
129
+ env_values_buffer[env_value_count] = .{ .msg = entry.msg, .value = value };
130
+ env_value_count += 1;
131
+ }
132
+ }
133
+ }
134
+
135
+ // The app struct (and any real model) is multi-MB: `create`
136
+ // heap-allocates and constructs in place, so neither rides the stack.
137
+ const app_state = try Adapter.create(std.heap.page_allocator, .{
138
+ .audio_cache_dir = audio_cache_dir,
139
+ .boot_images = boot_images_buffer[0..boot_image_count],
140
+ .env_values = env_values_buffer[0..env_value_count],
141
+ }, options);
142
+ defer app_state.destroy();
143
+
144
+ try runner.runWithOptions(app_state.app(), .{
145
+ .app_name = manifest.name,
146
+ .window_title = comptime windowTitle(),
147
+ .bundle_id = manifest.id,
148
+ .icon_path = "assets/icon.png",
149
+ .default_frame = comptime defaultFrame(),
150
+ .restore_state = comptime startupRestoreState(),
151
+ .js_window_api = false,
152
+ .security = .{
153
+ .permissions = app_permissions,
154
+ .navigation = .{ .allowed_origins = allowed_origins },
155
+ },
156
+ }, init);
157
+ }
158
+
159
+ /// The startup window title: the scene's first window title, else the
160
+ /// manifest display name, else the app name.
161
+ fn windowTitle() []const u8 {
162
+ if (shell_scene.windows.len > 0) {
163
+ if (shell_scene.windows[0].title) |title| return title;
164
+ }
165
+ if (@hasField(@TypeOf(manifest), "display_name")) return manifest.display_name;
166
+ return manifest.name;
167
+ }
168
+
169
+ fn defaultFrame() native_sdk.geometry.RectF {
170
+ if (shell_scene.windows.len > 0) {
171
+ const window = shell_scene.windows[0];
172
+ return native_sdk.geometry.RectF.init(window.x orelse 0, window.y orelse 0, window.width, window.height);
173
+ }
174
+ return native_sdk.geometry.RectF.init(0, 0, 720, 480);
175
+ }
176
+
177
+ fn startupRestoreState() bool {
178
+ if (shell_scene.windows.len > 0) return shell_scene.windows[0].restore_state;
179
+ return true;
180
+ }
181
+
182
+ /// One app.zon `.assets.images` entry: the encoded file the wiring reads
183
+ /// at launch and the `ImageId` markup avatar bindings reference.
184
+ const ImageAsset = struct {
185
+ id: u64,
186
+ path: []const u8,
187
+ };
188
+
189
+ /// Encoded-size bound for one boot image: over-bound files skip their
190
+ /// entry (the views keep the initials fallback) instead of holding the
191
+ /// launch path hostage to a mis-sized asset.
192
+ const max_boot_image_bytes: usize = 4 * 1024 * 1024;
193
+
194
+ fn manifestImages() []const ImageAsset {
195
+ comptime {
196
+ if (!@hasField(@TypeOf(manifest), "assets")) return &.{};
197
+ if (!@hasField(@TypeOf(manifest.assets), "images")) return &.{};
198
+ var out: []const ImageAsset = &.{};
199
+ for (manifest.assets.images) |entry| {
200
+ out = out ++ &[_]ImageAsset{.{ .id = entry.id, .path = entry.path }};
201
+ }
202
+ return out;
203
+ }
204
+ }
205
+
206
+ fn envMsgsLen() usize {
207
+ comptime {
208
+ if (!@hasDecl(core, "envMsgs")) return 0;
209
+ return core.envMsgs.len;
210
+ }
211
+ }
212
+
213
+ fn manifestStringList(comptime m: anytype, comptime field: []const u8) []const []const u8 {
214
+ comptime {
215
+ if (!@hasField(@TypeOf(m), field)) return &.{};
216
+ var out: []const []const u8 = &.{};
217
+ for (@field(m, field)) |entry| {
218
+ const name: []const u8 = entry;
219
+ out = out ++ &[_][]const u8{name};
220
+ }
221
+ return out;
222
+ }
223
+ }
224
+
225
+ fn manifestAllowedOrigins() []const []const u8 {
226
+ comptime {
227
+ if (!@hasField(@TypeOf(manifest), "security")) return &.{};
228
+ if (!@hasField(@TypeOf(manifest.security), "navigation")) return &.{};
229
+ return manifestStringList(manifest.security.navigation, "allowed_origins");
230
+ }
231
+ }
@@ -0,0 +1,341 @@
1
+ //! Comptime app.zon -> scene conversion: turn the manifest module's
2
+ //! `.shell` declaration (the anonymous ZON tuple a build imports as
3
+ //! `app_manifest_zon`) into a typed `ShellConfig`, so a generated wiring
4
+ //! module can run an app whose ONLY window/scene declaration is app.zon —
5
+ //! nothing repeated in Zig. The Zig app template keeps declaring its scene
6
+ //! in main.zig (both declarations there are user-owned); this converter is
7
+ //! for build-generated wiring where app.zon must be the single source of
8
+ //! truth. Unknown enum spellings are teaching @compileErrors naming the
9
+ //! field — the manifest validator accepts the same vocabularies at
10
+ //! `native check` time (validation.zig), so the two surfaces agree.
11
+
12
+ const std = @import("std");
13
+ const types = @import("types.zig");
14
+
15
+ /// The `.shell` scene from an imported app.zon module, as a comptime
16
+ /// `ShellConfig`. Absent shell (or absent windows) yields the empty
17
+ /// config — the runtime then keeps the host's default startup window.
18
+ pub fn shellConfigFrom(comptime manifest: anytype) types.ShellConfig {
19
+ comptime {
20
+ if (!@hasField(@TypeOf(manifest), "shell")) return .{};
21
+ const shell = manifest.shell;
22
+ // The whole conversion is ONE comptime evaluation sharing one
23
+ // backwards-branch budget, and the default (1000) does not survive
24
+ // a real manifest: every window and view costs field probes, slice
25
+ // concatenation, and enum-name scans, so a scene one field richer
26
+ // than the smallest demo dies mid-sort deep in std. Budget
27
+ // proportional to the declared scene instead — see
28
+ // sceneEvalBranchQuota for the per-node ceiling.
29
+ @setEvalBranchQuota(sceneEvalBranchQuota(shell));
30
+ var windows: []const types.ShellWindow = &.{};
31
+ if (@hasField(@TypeOf(shell), "windows")) {
32
+ for (shell.windows) |window| {
33
+ windows = windows ++ &[_]types.ShellWindow{shellWindowFrom(window)};
34
+ }
35
+ }
36
+ var chrome: types.ShellChrome = .{};
37
+ if (@hasField(@TypeOf(shell), "chrome")) {
38
+ chrome = shellChromeFrom(shell.chrome);
39
+ }
40
+ return .{ .windows = windows, .chrome = chrome };
41
+ }
42
+ }
43
+
44
+ /// The first `gpu_surface` view label in the scene — the canvas a
45
+ /// markup-viewed app renders into. A teaching @compileError when the
46
+ /// manifest declares none: a canvas app cannot render without one.
47
+ pub fn firstGpuSurfaceLabel(comptime scene: types.ShellConfig) []const u8 {
48
+ comptime {
49
+ // A separate top-level evaluation with its own default branch
50
+ // budget: walking a large converted scene must not die at 1000
51
+ // either. The walk is a compare per view, so a small per-view
52
+ // grant is plenty.
53
+ var views: comptime_int = 1;
54
+ for (scene.windows) |window| views += 1 + window.views.len;
55
+ @setEvalBranchQuota(1_000 + 16 * views);
56
+ for (scene.windows) |window| {
57
+ for (window.views) |view| {
58
+ if (view.kind == .gpu_surface) return view.label;
59
+ }
60
+ }
61
+ @compileError("app.zon declares no gpu_surface view - a canvas app needs one: add" ++
62
+ " .views = .{ .{ .label = \"main-canvas\", .kind = \"gpu_surface\", .fill = true } } to the shell window");
63
+ }
64
+ }
65
+
66
+ fn shellWindowFrom(comptime window: anytype) types.ShellWindow {
67
+ var out: types.ShellWindow = .{};
68
+ if (@hasField(@TypeOf(window), "label")) out.label = window.label;
69
+ if (@hasField(@TypeOf(window), "title")) {
70
+ if (@TypeOf(window.title) != @TypeOf(null)) out.title = window.title;
71
+ }
72
+ if (@hasField(@TypeOf(window), "width")) out.width = window.width;
73
+ if (@hasField(@TypeOf(window), "height")) out.height = window.height;
74
+ if (@hasField(@TypeOf(window), "x")) out.x = window.x;
75
+ if (@hasField(@TypeOf(window), "y")) out.y = window.y;
76
+ if (@hasField(@TypeOf(window), "resizable")) out.resizable = window.resizable;
77
+ if (@hasField(@TypeOf(window), "restore_state")) out.restore_state = window.restore_state;
78
+ if (@hasField(@TypeOf(window), "restore_policy")) {
79
+ out.restore_policy = enumField(types.WindowRestorePolicy, window.restore_policy, "restore_policy");
80
+ }
81
+ if (@hasField(@TypeOf(window), "titlebar")) {
82
+ out.titlebar = enumField(types.WindowTitlebarStyle, window.titlebar, "titlebar");
83
+ }
84
+ if (@hasField(@TypeOf(window), "min_width")) out.min_width = window.min_width;
85
+ if (@hasField(@TypeOf(window), "min_height")) out.min_height = window.min_height;
86
+ if (@hasField(@TypeOf(window), "views")) {
87
+ var views: []const types.ShellView = &.{};
88
+ for (window.views) |view| {
89
+ views = views ++ &[_]types.ShellView{shellViewFrom(view)};
90
+ }
91
+ out.views = views;
92
+ }
93
+ return out;
94
+ }
95
+
96
+ fn shellViewFrom(comptime view: anytype) types.ShellView {
97
+ if (!@hasField(@TypeOf(view), "label")) @compileError("app.zon shell view needs a .label");
98
+ if (!@hasField(@TypeOf(view), "kind")) @compileError("app.zon shell view needs a .kind");
99
+ var out: types.ShellView = .{
100
+ .label = view.label,
101
+ .kind = enumField(types.ViewKind, view.kind, "view kind"),
102
+ };
103
+ if (@hasField(@TypeOf(view), "parent")) out.parent = view.parent;
104
+ if (@hasField(@TypeOf(view), "edge")) out.edge = enumField(types.ShellEdge, view.edge, "edge");
105
+ if (@hasField(@TypeOf(view), "axis")) out.axis = enumField(types.ShellAxis, view.axis, "axis");
106
+ if (@hasField(@TypeOf(view), "x")) out.x = view.x;
107
+ if (@hasField(@TypeOf(view), "y")) out.y = view.y;
108
+ if (@hasField(@TypeOf(view), "width")) out.width = view.width;
109
+ if (@hasField(@TypeOf(view), "height")) out.height = view.height;
110
+ if (@hasField(@TypeOf(view), "min_width")) out.min_width = view.min_width;
111
+ if (@hasField(@TypeOf(view), "min_height")) out.min_height = view.min_height;
112
+ if (@hasField(@TypeOf(view), "max_width")) out.max_width = view.max_width;
113
+ if (@hasField(@TypeOf(view), "max_height")) out.max_height = view.max_height;
114
+ if (@hasField(@TypeOf(view), "fill")) out.fill = view.fill;
115
+ if (@hasField(@TypeOf(view), "layer")) out.layer = view.layer;
116
+ if (@hasField(@TypeOf(view), "visible")) out.visible = view.visible;
117
+ if (@hasField(@TypeOf(view), "enabled")) out.enabled = view.enabled;
118
+ if (@hasField(@TypeOf(view), "role")) out.role = view.role;
119
+ if (@hasField(@TypeOf(view), "accessibility_label")) out.accessibility_label = view.accessibility_label;
120
+ if (@hasField(@TypeOf(view), "url")) out.url = view.url;
121
+ if (@hasField(@TypeOf(view), "text")) out.text = view.text;
122
+ if (@hasField(@TypeOf(view), "command")) out.command = view.command;
123
+ if (@hasField(@TypeOf(view), "gpu_backend")) out.gpu_backend = enumField(types.GpuSurfaceBackend, view.gpu_backend, "gpu_backend");
124
+ if (@hasField(@TypeOf(view), "gpu_pixel_format")) out.gpu_pixel_format = enumField(types.GpuSurfacePixelFormat, view.gpu_pixel_format, "gpu_pixel_format");
125
+ if (@hasField(@TypeOf(view), "gpu_present_mode")) out.gpu_present_mode = enumField(types.GpuSurfacePresentMode, view.gpu_present_mode, "gpu_present_mode");
126
+ if (@hasField(@TypeOf(view), "gpu_alpha_mode")) out.gpu_alpha_mode = enumField(types.GpuSurfaceAlphaMode, view.gpu_alpha_mode, "gpu_alpha_mode");
127
+ if (@hasField(@TypeOf(view), "gpu_color_space")) out.gpu_color_space = enumField(types.GpuSurfaceColorSpace, view.gpu_color_space, "gpu_color_space");
128
+ if (@hasField(@TypeOf(view), "gpu_vsync")) out.gpu_vsync = view.gpu_vsync;
129
+ return out;
130
+ }
131
+
132
+ fn shellChromeFrom(comptime chrome: anytype) types.ShellChrome {
133
+ var out: types.ShellChrome = .{};
134
+ if (@hasField(@TypeOf(chrome), "tabs")) {
135
+ var tabs: []const types.ShellTab = &.{};
136
+ for (chrome.tabs) |tab| {
137
+ var entry: types.ShellTab = .{ .id = tab.id, .label = tab.label };
138
+ if (@hasField(@TypeOf(tab), "icon")) entry.icon = tab.icon;
139
+ tabs = tabs ++ &[_]types.ShellTab{entry};
140
+ }
141
+ out.tabs = tabs;
142
+ }
143
+ if (@hasField(@TypeOf(chrome), "primary_action")) {
144
+ const action = chrome.primary_action;
145
+ var entry: types.ShellPrimaryAction = .{ .id = action.id, .label = action.label };
146
+ if (@hasField(@TypeOf(action), "icon")) entry.icon = action.icon;
147
+ out.primary_action = entry;
148
+ }
149
+ return out;
150
+ }
151
+
152
+ /// The branch budget for converting `shell`, proportional to its size.
153
+ /// Counting first is safe: windows.len is comptime-known (no branches),
154
+ /// so an interim grant covers the counting walk itself, and the walk is
155
+ /// one pass over the windows. The 2000-per-node ceiling is deliberately
156
+ /// fat — a view costs ~25 field probes, a slice concatenation, and up to
157
+ /// six linear enum-name scans (see enumField), each of which
158
+ /// short-circuits on length, so real cost sits far below it.
159
+ fn sceneEvalBranchQuota(comptime shell: anytype) comptime_int {
160
+ comptime {
161
+ var nodes: comptime_int = 4; // the shell itself, chrome, slack
162
+ if (@hasField(@TypeOf(shell), "windows")) {
163
+ @setEvalBranchQuota(1_000 + 16 * shell.windows.len);
164
+ for (shell.windows) |window| {
165
+ nodes += 1;
166
+ if (@hasField(@TypeOf(window), "views")) nodes += window.views.len;
167
+ }
168
+ }
169
+ if (@hasField(@TypeOf(shell), "chrome")) {
170
+ if (@hasField(@TypeOf(shell.chrome), "tabs")) nodes += shell.chrome.tabs.len;
171
+ }
172
+ return 2_000 * nodes;
173
+ }
174
+ }
175
+
176
+ fn enumField(comptime E: type, comptime value: []const u8, comptime what: []const u8) E {
177
+ comptime {
178
+ // NOT std.meta.stringToEnum: that builds a sorted StaticStringMap
179
+ // at comptime on EVERY call, and its own quota grant is a @max
180
+ // that never rises above the default for small enums — so each
181
+ // enum-mapped manifest field pays a full comptime pdq sort out of
182
+ // the shared budget. A linear name scan is a handful of branches
183
+ // per member (length short-circuit first) and needs no map.
184
+ for (@typeInfo(E).@"enum".fields) |field| {
185
+ if (std.mem.eql(u8, field.name, value)) return @field(E, field.name);
186
+ }
187
+ @compileError("unknown app.zon " ++ what ++ " \"" ++ value ++ "\" - expected one of: " ++ memberList(E));
188
+ }
189
+ }
190
+
191
+ fn memberList(comptime E: type) []const u8 {
192
+ comptime {
193
+ var out: []const u8 = "";
194
+ for (@typeInfo(E).@"enum".fields, 0..) |field, index| {
195
+ out = out ++ (if (index == 0) "" else ", ") ++ field.name;
196
+ }
197
+ return out;
198
+ }
199
+ }
200
+
201
+ test "shellConfigFrom converts a zon-shaped scene" {
202
+ const manifest = .{
203
+ .name = "demo",
204
+ .shell = .{
205
+ .windows = .{
206
+ .{
207
+ .label = "main",
208
+ .title = "Demo",
209
+ .width = 480,
210
+ .height = 320,
211
+ .restore_state = false,
212
+ .restore_policy = "center_on_primary",
213
+ .views = .{
214
+ .{ .label = "main-canvas", .kind = "gpu_surface", .fill = true, .gpu_backend = "metal", .gpu_vsync = true },
215
+ },
216
+ },
217
+ },
218
+ },
219
+ };
220
+ const scene = comptime shellConfigFrom(manifest);
221
+ try std.testing.expectEqual(@as(usize, 1), scene.windows.len);
222
+ try std.testing.expectEqualStrings("main", scene.windows[0].label);
223
+ try std.testing.expectEqualStrings("Demo", scene.windows[0].title.?);
224
+ try std.testing.expectEqual(@as(f32, 480), scene.windows[0].width);
225
+ try std.testing.expectEqual(types.WindowRestorePolicy.center_on_primary, scene.windows[0].restore_policy);
226
+ try std.testing.expectEqual(@as(usize, 1), scene.windows[0].views.len);
227
+ try std.testing.expectEqual(types.ViewKind.gpu_surface, scene.windows[0].views[0].kind);
228
+ try std.testing.expectEqual(types.GpuSurfaceBackend.metal, scene.windows[0].views[0].gpu_backend.?);
229
+ try std.testing.expect(scene.windows[0].views[0].fill);
230
+ try std.testing.expectEqualStrings("main-canvas", comptime firstGpuSurfaceLabel(scene));
231
+ }
232
+
233
+ test "shellConfigFrom yields the empty scene when app.zon declares none" {
234
+ const scene = comptime shellConfigFrom(.{ .name = "bare" });
235
+ try std.testing.expectEqual(@as(usize, 0), scene.windows.len);
236
+ }
237
+
238
+ // Branch-budget regression guard: a scene substantially richer than any
239
+ // shipped example — several windows, every enum-mapped shell field spelled
240
+ // as a string, chrome — must CONVERT, not die on "evaluation exceeded N
241
+ // backwards branches" in a comptime sort deep in std (the system-monitor-ts
242
+ // macOS build regression: one field richer than the smallest demo was
243
+ // already over the default quota).
244
+ test "shellConfigFrom converts a rich scene without exhausting the branch quota" {
245
+ const rich_view = .{
246
+ .label = "surface",
247
+ .kind = "gpu_surface",
248
+ .parent = "root",
249
+ .edge = "left",
250
+ .axis = "column",
251
+ .x = 0,
252
+ .y = 0,
253
+ .width = 400,
254
+ .height = 300,
255
+ .min_width = 200,
256
+ .min_height = 150,
257
+ .max_width = 800,
258
+ .max_height = 600,
259
+ .fill = true,
260
+ .layer = 1,
261
+ .visible = true,
262
+ .enabled = true,
263
+ .role = "Canvas",
264
+ .accessibility_label = "Main canvas",
265
+ .gpu_backend = "metal",
266
+ .gpu_pixel_format = "bgra8_unorm",
267
+ .gpu_present_mode = "timer",
268
+ .gpu_alpha_mode = "opaque",
269
+ .gpu_color_space = "display_p3",
270
+ .gpu_vsync = true,
271
+ };
272
+ const rich_window = .{
273
+ .label = "main",
274
+ .title = "Rich",
275
+ .width = 1144,
276
+ .height = 720,
277
+ .x = 10,
278
+ .y = 20,
279
+ .resizable = true,
280
+ .restore_state = false,
281
+ .restore_policy = "center_on_primary",
282
+ .titlebar = "hidden_inset_tall",
283
+ .min_width = 1144,
284
+ .min_height = 720,
285
+ .views = .{
286
+ rich_view,
287
+ .{ .label = "side", .kind = "sidebar", .edge = "right", .axis = "row", .width = 240 },
288
+ .{ .label = "status", .kind = "statusbar", .edge = "bottom", .visible = true },
289
+ .{ .label = "tools", .kind = "toolbar", .edge = "top", .enabled = true },
290
+ },
291
+ };
292
+ const manifest = .{
293
+ .name = "rich",
294
+ .shell = .{
295
+ .windows = .{
296
+ rich_window,
297
+ .{
298
+ .label = "aux",
299
+ .titlebar = "hidden_inset",
300
+ .restore_policy = "clamp_to_visible_screen",
301
+ .views = .{
302
+ .{ .label = "aux-canvas", .kind = "gpu_surface", .fill = true, .gpu_backend = "software", .gpu_alpha_mode = "premultiplied", .gpu_color_space = "srgb" },
303
+ },
304
+ },
305
+ .{
306
+ .label = "chromeless",
307
+ .titlebar = "chromeless",
308
+ .views = .{
309
+ .{ .label = "web", .kind = "webview", .url = "zero://app" },
310
+ .{ .label = "split", .kind = "split", .axis = "row" },
311
+ },
312
+ },
313
+ },
314
+ .chrome = .{
315
+ .tabs = .{
316
+ .{ .id = "one", .label = "One", .icon = "gauge" },
317
+ .{ .id = "two", .label = "Two" },
318
+ .{ .id = "three", .label = "Three" },
319
+ },
320
+ .primary_action = .{ .id = "go", .label = "Go", .icon = "play" },
321
+ },
322
+ },
323
+ };
324
+ const scene = comptime shellConfigFrom(manifest);
325
+ try std.testing.expectEqual(@as(usize, 3), scene.windows.len);
326
+ try std.testing.expectEqual(types.WindowTitlebarStyle.hidden_inset_tall, scene.windows[0].titlebar);
327
+ try std.testing.expectEqual(types.WindowRestorePolicy.center_on_primary, scene.windows[0].restore_policy);
328
+ try std.testing.expectEqual(@as(usize, 4), scene.windows[0].views.len);
329
+ try std.testing.expectEqual(types.ShellEdge.left, scene.windows[0].views[0].edge.?);
330
+ try std.testing.expectEqual(types.ShellAxis.column, scene.windows[0].views[0].axis.?);
331
+ try std.testing.expectEqual(types.GpuSurfacePixelFormat.bgra8_unorm, scene.windows[0].views[0].gpu_pixel_format.?);
332
+ try std.testing.expectEqual(types.GpuSurfacePresentMode.timer, scene.windows[0].views[0].gpu_present_mode.?);
333
+ try std.testing.expectEqual(types.GpuSurfaceAlphaMode.@"opaque", scene.windows[0].views[0].gpu_alpha_mode.?);
334
+ try std.testing.expectEqual(types.GpuSurfaceColorSpace.display_p3, scene.windows[0].views[0].gpu_color_space.?);
335
+ try std.testing.expectEqual(types.GpuSurfaceBackend.software, scene.windows[1].views[0].gpu_backend.?);
336
+ try std.testing.expectEqual(types.GpuSurfaceAlphaMode.premultiplied, scene.windows[1].views[0].gpu_alpha_mode.?);
337
+ try std.testing.expectEqual(types.WindowTitlebarStyle.chromeless, scene.windows[2].titlebar);
338
+ try std.testing.expectEqual(@as(usize, 3), scene.chrome.tabs.len);
339
+ try std.testing.expectEqualStrings("go", scene.chrome.primary_action.?.id);
340
+ try std.testing.expectEqualStrings("surface", comptime firstGpuSurfaceLabel(scene));
341
+ }
@@ -109,9 +109,14 @@ pub const validateUpdates = validation.validateUpdates;
109
109
  pub const validatePlatforms = validation.validatePlatforms;
110
110
  pub const validatePackageMetadata = validation.validatePackageMetadata;
111
111
  pub const versionString = validation.versionString;
112
+ pub const shellConfigFrom = comptime_scene.shellConfigFrom;
113
+ pub const firstGpuSurfaceLabel = comptime_scene.firstGpuSurfaceLabel;
114
+
115
+ const comptime_scene = @import("comptime_scene.zig");
112
116
 
113
117
  test {
114
118
  std.testing.refAllDecls(@This());
115
119
  _ = @import("tests.zig");
120
+ _ = comptime_scene;
116
121
  _ = @import("web_layer.zig");
117
122
  }