@native-sdk/cli 0.5.3 → 0.6.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 (172) hide show
  1. package/build/app.zig +91 -2
  2. package/build.zig +756 -5
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +19 -1
  5. package/package.json +9 -9
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/rt/rt.zig +416 -9
  9. package/packages/core/sdk/core.ts +706 -5
  10. package/packages/core/sdk/events.ts +17 -8
  11. package/packages/core/src/checker.ts +305 -2
  12. package/packages/core/src/diagnostics.ts +12 -0
  13. package/packages/core/src/emitter.ts +3187 -264
  14. package/packages/core/src/typed_ast.ts +51 -4
  15. package/packages/core/src/types.ts +34 -1
  16. package/skill-data/native-ui/SKILL.md +42 -4
  17. package/skill-data/ts-core/SKILL.md +13 -3
  18. package/src/app_runner/root.zig +112 -10
  19. package/src/app_runner/ts_core_main.zig +4 -0
  20. package/src/automation/layout_fingerprint.zig +160 -0
  21. package/src/automation/protocol.zig +82 -33
  22. package/src/automation/root.zig +1 -0
  23. package/src/automation/server.zig +1 -1
  24. package/src/automation/snapshot.zig +68 -9
  25. package/src/automation/watcher.zig +1 -1
  26. package/src/embed/host.zig +5 -0
  27. package/src/embed/tests.zig +119 -1
  28. package/src/embed/ui_host.zig +13 -0
  29. package/src/platform/linux/gtk_host.c +639 -16
  30. package/src/platform/linux/gtk_host.h +25 -0
  31. package/src/platform/linux/root.zig +351 -7
  32. package/src/platform/macos/appkit_host.h +152 -8
  33. package/src/platform/macos/appkit_host.m +1816 -100
  34. package/src/platform/macos/cef_host.mm +372 -11
  35. package/src/platform/macos/root.zig +379 -11
  36. package/src/platform/null_platform.zig +696 -9
  37. package/src/platform/null_platform_tests.zig +35 -0
  38. package/src/platform/root.zig +42 -0
  39. package/src/platform/types.zig +524 -18
  40. package/src/platform/windows/root.zig +432 -9
  41. package/src/platform/windows/webview2_host.cpp +482 -45
  42. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  43. package/src/primitives/app_manifest/root.zig +1 -0
  44. package/src/primitives/app_manifest/types.zig +32 -0
  45. package/src/primitives/app_manifest/validation.zig +18 -0
  46. package/src/primitives/canvas/a11y_audit.zig +33 -11
  47. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  48. package/src/primitives/canvas/drawing.zig +25 -0
  49. package/src/primitives/canvas/equality.zig +8 -0
  50. package/src/primitives/canvas/events.zig +103 -16
  51. package/src/primitives/canvas/frame.zig +125 -1
  52. package/src/primitives/canvas/layout_audit.zig +22 -5
  53. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  54. package/src/primitives/canvas/render_tests.zig +13 -10
  55. package/src/primitives/canvas/root.zig +48 -0
  56. package/src/primitives/canvas/terminal_box.zig +471 -0
  57. package/src/primitives/canvas/terminal_grid.zig +978 -0
  58. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  59. package/src/primitives/canvas/test_support.zig +1 -0
  60. package/src/primitives/canvas/tests.zig +2 -0
  61. package/src/primitives/canvas/text_tests.zig +39 -4
  62. package/src/primitives/canvas/tokens.zig +115 -12
  63. package/src/primitives/canvas/ui.zig +535 -12
  64. package/src/primitives/canvas/ui_markup.zig +253 -36
  65. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  66. package/src/primitives/canvas/ui_markup_compiled.zig +195 -9
  67. package/src/primitives/canvas/ui_markup_compiled_tests.zig +188 -8
  68. package/src/primitives/canvas/ui_markup_contract.zig +89 -6
  69. package/src/primitives/canvas/ui_markup_contract_tests.zig +65 -1
  70. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  71. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  72. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  73. package/src/primitives/canvas/ui_markup_tests.zig +178 -9
  74. package/src/primitives/canvas/ui_markup_view.zig +220 -11
  75. package/src/primitives/canvas/ui_markup_view_tests.zig +553 -28
  76. package/src/primitives/canvas/ui_schema.zig +110 -17
  77. package/src/primitives/canvas/ui_schema_tests.zig +18 -10
  78. package/src/primitives/canvas/ui_tests.zig +141 -9
  79. package/src/primitives/canvas/widget_access.zig +28 -2
  80. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  81. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  82. package/src/primitives/canvas/widget_layout.zig +59 -11
  83. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  84. package/src/primitives/canvas/widget_render.zig +272 -18
  85. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  86. package/src/primitives/canvas/widget_routing.zig +59 -7
  87. package/src/primitives/canvas/widget_runtime.zig +23 -0
  88. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  89. package/src/primitives/canvas/widget_semantics.zig +119 -11
  90. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  91. package/src/primitives/canvas/widget_tree.zig +17 -0
  92. package/src/primitives/canvas/widgets.zig +132 -0
  93. package/src/root.zig +23 -0
  94. package/src/runtime/api.zig +76 -5
  95. package/src/runtime/automation_commands.zig +20 -1
  96. package/src/runtime/automation_snapshot.zig +25 -1
  97. package/src/runtime/automation_widget_dispatch.zig +73 -8
  98. package/src/runtime/bridge_payload.zig +8 -0
  99. package/src/runtime/bridge_responses.zig +5 -0
  100. package/src/runtime/builtin_bridge.zig +9 -4
  101. package/src/runtime/canvas_font_tests.zig +439 -2
  102. package/src/runtime/canvas_fonts.zig +61 -6
  103. package/src/runtime/canvas_frame.zig +62 -7
  104. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  105. package/src/runtime/canvas_frame_helpers.zig +190 -1
  106. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  107. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  108. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  109. package/src/runtime/canvas_image_tests.zig +195 -0
  110. package/src/runtime/canvas_images.zig +45 -12
  111. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  112. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  113. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  114. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  115. package/src/runtime/canvas_widget_display.zig +1 -1
  116. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  117. package/src/runtime/canvas_widget_events.zig +240 -24
  118. package/src/runtime/canvas_widget_runtime.zig +213 -29
  119. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  120. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  121. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  122. package/src/runtime/canvas_widget_state.zig +6 -0
  123. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  124. package/src/runtime/core.zig +131 -13
  125. package/src/runtime/effects.zig +8136 -180
  126. package/src/runtime/effects_channel_tests.zig +3519 -0
  127. package/src/runtime/effects_fetch_tests.zig +89 -1
  128. package/src/runtime/effects_file_tests.zig +41 -1
  129. package/src/runtime/effects_image_tests.zig +1304 -0
  130. package/src/runtime/effects_pty_tests.zig +1362 -0
  131. package/src/runtime/effects_tests.zig +167 -9
  132. package/src/runtime/effects_video_tests.zig +3609 -0
  133. package/src/runtime/flow.zig +42 -4
  134. package/src/runtime/gpu_surface_events.zig +473 -13
  135. package/src/runtime/media_surface.zig +116 -11
  136. package/src/runtime/media_surface_tests.zig +61 -0
  137. package/src/runtime/pty.zig +1252 -0
  138. package/src/runtime/pty_windows.zig +1780 -0
  139. package/src/runtime/root.zig +36 -0
  140. package/src/runtime/runtime_event_tests.zig +20 -1
  141. package/src/runtime/session_blobs.zig +689 -0
  142. package/src/runtime/session_journal.zig +432 -62
  143. package/src/runtime/session_record.zig +43 -2
  144. package/src/runtime/session_replay.zig +556 -20
  145. package/src/runtime/session_tests.zig +2087 -1
  146. package/src/runtime/shell_layout.zig +7 -0
  147. package/src/runtime/terminal_session.zig +1569 -0
  148. package/src/runtime/terminal_session_tests.zig +450 -0
  149. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  150. package/src/runtime/terminal_vt_stub.zig +13 -0
  151. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  152. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  153. package/src/runtime/tests.zig +8 -0
  154. package/src/runtime/ts_core_host.zig +1320 -45
  155. package/src/runtime/ts_core_host_tests.zig +1100 -1
  156. package/src/runtime/ts_ui_app.zig +23 -5
  157. package/src/runtime/ui_app.zig +2444 -69
  158. package/src/runtime/ui_app_tests.zig +3171 -744
  159. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  160. package/src/runtime/ui_app_window_tests.zig +736 -0
  161. package/src/runtime/view.zig +190 -2
  162. package/src/runtime/view_widget_scroll.zig +218 -68
  163. package/src/runtime/view_widget_text.zig +11 -2
  164. package/src/runtime/view_widget_tree.zig +111 -3
  165. package/src/runtime/window_command_bridge_tests.zig +117 -0
  166. package/src/runtime/window_storage.zig +77 -2
  167. package/src/runtime/window_view_runtime.zig +34 -2
  168. package/src/runtime/window_views.zig +62 -1
  169. package/src/terminal_session_tests_root.zig +15 -0
  170. package/src/tooling/manifest.zig +75 -0
  171. package/src/tooling/raw_manifest.zig +2 -0
  172. package/src/tooling/templates.zig +244 -8
@@ -153,12 +153,88 @@ test "runtime handles built-in JavaScript window bridge commands" {
153
153
  } });
154
154
  try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"focused\":true") != null);
155
155
 
156
+ // The window JSON must expose alive-but-policy-hidden: open:true /
157
+ // focused:false alone cannot distinguish a close_policy = "hide"
158
+ // window from a visible unfocused one, so "hidden" rides every
159
+ // window response. Visible window first...
160
+ try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"hidden\":false") != null);
161
+ // ...then the host reports the palette window policy-hidden (still
162
+ // open) and the bridge list reflects it.
163
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .window_frame_changed = .{
164
+ .id = 2,
165
+ .label = "palette",
166
+ .title = "Palette",
167
+ .frame = geometry.RectF.init(0, 0, 320, 240),
168
+ .scale_factor = 1,
169
+ .open = true,
170
+ .focused = false,
171
+ .hidden = true,
172
+ } });
173
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .bridge_message = .{
174
+ .bytes = "{\"id\":\"hidden\",\"command\":\"native-sdk.window.list\",\"payload\":null}",
175
+ .origin = "zero://inline",
176
+ .window_id = 1,
177
+ } });
178
+ try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"hidden\":true") != null);
179
+
180
+ // Focusing the policy-hidden window rides the runtime's ONE
181
+ // hidden-then-focus rule: the show verb runs first (the platform
182
+ // mirror counts it — the seam that clears the hosts' policy-hidden
183
+ // bookkeeping), so the response never reports a focused window
184
+ // that is still hidden.
185
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .bridge_message = .{
186
+ .bytes = "{\"id\":\"5\",\"command\":\"native-sdk.window.focus\",\"payload\":{\"label\":\"palette\"}}",
187
+ .origin = "zero://inline",
188
+ .window_id = 1,
189
+ } });
190
+ try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"focused\":true") != null);
191
+ try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"hidden\":false") != null);
192
+ try std.testing.expectEqual(@as(u32, 1), harness.null_platform.showCountForWindow(2));
193
+
194
+ // The runtime's show verb (a tray "Open", Cmd.showWindow) is show
195
+ // AND activate: after showing a policy-hidden window, the bridge's
196
+ // window JSON — served from the same runtime table — reports it
197
+ // focused, not merely un-hidden.
198
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .window_frame_changed = .{
199
+ .id = 2,
200
+ .label = "palette",
201
+ .title = "Palette",
202
+ .frame = geometry.RectF.init(0, 0, 320, 240),
203
+ .scale_factor = 1,
204
+ .open = true,
205
+ .focused = false,
206
+ .hidden = true,
207
+ } });
208
+ try harness.runtime.showWindow(2);
209
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .bridge_message = .{
210
+ .bytes = "{\"id\":\"shown\",\"command\":\"native-sdk.window.list\",\"payload\":null}",
211
+ .origin = "zero://inline",
212
+ .window_id = 1,
213
+ } });
214
+ try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"label\":\"palette\",\"title\":\"Palette\",\"open\":true,\"focused\":true,\"hidden\":false") != null);
215
+
216
+ // Closing a POLICY-HIDDEN window through the bridge answers with
217
+ // the post-close table state: hidden clears with open (a closed
218
+ // window is gone, not "hidden"), so JS must never receive
219
+ // {open:false, hidden:true} — the shape a pre-close snapshot with
220
+ // hand-cleared fields would serialize.
221
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .window_frame_changed = .{
222
+ .id = 2,
223
+ .label = "palette",
224
+ .title = "Palette",
225
+ .frame = geometry.RectF.init(0, 0, 320, 240),
226
+ .scale_factor = 1,
227
+ .open = true,
228
+ .focused = false,
229
+ .hidden = true,
230
+ } });
156
231
  try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .bridge_message = .{
157
232
  .bytes = "{\"id\":\"4\",\"command\":\"native-sdk.window.close\",\"payload\":{\"label\":\"palette\"}}",
158
233
  .origin = "zero://inline",
159
234
  .window_id = 1,
160
235
  } });
161
236
  try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"open\":false") != null);
237
+ try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"hidden\":false") != null);
162
238
  }
163
239
 
164
240
  test "runtime handles built-in JavaScript command bridge commands" {
@@ -383,6 +459,47 @@ test "runtime handles built-in JavaScript platform support commands" {
383
459
  } });
384
460
  try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"result\":false") != null);
385
461
 
462
+ // Both spellings of window_hide_on_close answer the same seam: the null
463
+ // platform models full hide-on-close support by default, and flipping the
464
+ // seam off (the GTK model) flips both spellings together.
465
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .bridge_message = .{
466
+ .bytes = "{\"id\":\"hide-snake\",\"command\":\"native-sdk.platform.supports\",\"payload\":{\"feature\":\"window_hide_on_close\"}}",
467
+ .origin = "zero://inline",
468
+ .window_id = 1,
469
+ } });
470
+ try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"result\":true") != null);
471
+
472
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .bridge_message = .{
473
+ .bytes = "{\"id\":\"hide-camel\",\"command\":\"native-sdk.platform.supports\",\"payload\":{\"feature\":\"windowHideOnClose\"}}",
474
+ .origin = "zero://inline",
475
+ .window_id = 1,
476
+ } });
477
+ try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"result\":true") != null);
478
+
479
+ harness.null_platform.window_hide_on_close = false;
480
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .bridge_message = .{
481
+ .bytes = "{\"id\":\"hide-snake-off\",\"command\":\"native-sdk.platform.supports\",\"payload\":{\"feature\":\"window_hide_on_close\"}}",
482
+ .origin = "zero://inline",
483
+ .window_id = 1,
484
+ } });
485
+ try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"result\":false") != null);
486
+
487
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .bridge_message = .{
488
+ .bytes = "{\"id\":\"hide-camel-off\",\"command\":\"native-sdk.platform.supports\",\"payload\":{\"feature\":\"windowHideOnClose\"}}",
489
+ .origin = "zero://inline",
490
+ .window_id = 1,
491
+ } });
492
+ try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"result\":false") != null);
493
+
494
+ // Camel-cohort spot check: the backfilled audioSpectrum alias resolves to
495
+ // audio_spectrum, which the default null platform supports.
496
+ try harness.runtime.dispatchPlatformEvent(app_state.app(), .{ .bridge_message = .{
497
+ .bytes = "{\"id\":\"spectrum-camel\",\"command\":\"native-sdk.platform.supports\",\"payload\":{\"feature\":\"audioSpectrum\"}}",
498
+ .origin = "zero://inline",
499
+ .window_id = 1,
500
+ } });
501
+ try std.testing.expect(std.mem.indexOf(u8, harness.null_platform.lastBridgeResponse(), "\"result\":true") != null);
502
+
386
503
  var chromium_platform = platform.NullPlatform.initWithEngine(.{}, .chromium);
387
504
  harness.runtime.options.platform = chromium_platform.platform();
388
505
  try std.testing.expect(!harness.runtime.supports(.tray));
@@ -37,6 +37,29 @@ pub fn RuntimeWindowStorage(comptime Runtime: type) type {
37
37
 
38
38
  pub fn focusWindow(self: *Runtime, window_id: platform.WindowId) anyerror!void {
39
39
  const index = Self.findWindowIndexById(self, window_id) orelse return error.WindowNotFound;
40
+ // Liveness before the platform call, like showWindow: a
41
+ // closed window keeps its table slot until the id or label
42
+ // is re-created, and close clears `hidden` with `open`, so
43
+ // a dead slot would skip the hidden-routing below and reach
44
+ // the platform's focus verb directly — the CEF host retains
45
+ // browser-bearing windows past their close and would order
46
+ // the closed window back front. Dead slots answer
47
+ // WindowNotFound, the runtime's one answer for them.
48
+ if (!self.windows[index].info.open) return error.WindowNotFound;
49
+ // Focus implies visibility: a window hidden by its .hide
50
+ // close policy must leave the hidden state through the REAL
51
+ // show verb before it takes key. The hosts' focus paths
52
+ // order a window forward without touching their
53
+ // policy-hidden bookkeeping (macOS would report hidden=true
54
+ // on a window standing on the glass; Windows never shows an
55
+ // SW_HIDE'd window at all, leaving focused=true on an
56
+ // invisible one), while their show paths clear that
57
+ // bookkeeping and emit the state — and the runtime's
58
+ // showWindow flips its own hidden flag with rollback on
59
+ // platform failure. One rule, at this seam, so every focus
60
+ // ingress (the app verb, the JS bridge's window.focus)
61
+ // resolves hidden-then-focus the same way.
62
+ if (self.windows[index].info.hidden) try self.showWindow(window_id);
40
63
  try self.options.platform.services.focusWindow(window_id);
41
64
  try Self.setFocusedIndex(self, index);
42
65
  self.invalidated = true;
@@ -64,6 +87,14 @@ pub fn RuntimeWindowStorage(comptime Runtime: type) type {
64
87
  }
65
88
  const id = if (options.id != 0) options.id else Self.allocateWindowId(self);
66
89
  try validateWindowFrame(options.default_frame);
90
+ // `.hide` needs a host that can keep a closed-by-the-user
91
+ // window alive and re-show it; refusing here is the loud
92
+ // teaching (GTK: no status item exists to bring the window
93
+ // back, declare .quit — the default — instead). NEVER a
94
+ // silent no-op that strands a hidden window.
95
+ if (options.close_policy == .hide and !self.options.platform.supports(.window_hide_on_close)) {
96
+ return error.UnsupportedWindowClosePolicy;
97
+ }
67
98
  if (Self.findWindowIndexById(self, id) != null) return error.DuplicateWindowId;
68
99
  if (Self.findWindowIndexByLabel(self, label) != null) return error.DuplicateWindowLabel;
69
100
  const index = try Self.reserveWindow(self, id, label, options.title, source, source_reloads_from_app);
@@ -151,6 +182,7 @@ pub fn RuntimeWindowStorage(comptime Runtime: type) type {
151
182
  self.windows[index].info.frame = state.frame;
152
183
  self.windows[index].info.scale_factor = state.scale_factor;
153
184
  self.windows[index].info.open = state.open;
185
+ self.windows[index].info.hidden = state.hidden;
154
186
  if (!self.windows[index].main_frame_set) {
155
187
  self.windows[index].main_frame = geometry.RectF.init(0, 0, state.frame.width, state.frame.height);
156
188
  }
@@ -240,9 +272,13 @@ pub fn RuntimeWindowStorage(comptime Runtime: type) type {
240
272
  /// and the re-key restores focus where it was without revealing
241
273
  /// anything.
242
274
  pub fn setFocusedIndex(self: *Runtime, focused_index: usize) anyerror!void {
275
+ var first_error: ?anyerror = null;
243
276
  for (0..self.window_count) |index| {
244
- try Self.setWindowFocused(self, index, index == focused_index);
277
+ Self.setWindowFocused(self, index, index == focused_index) catch |err| {
278
+ if (first_error == null) first_error = err;
279
+ };
245
280
  }
281
+ if (first_error) |err| return err;
246
282
  }
247
283
 
248
284
  /// The ONE writer of a tracked window's `focused` flag: the
@@ -271,8 +307,47 @@ pub fn RuntimeWindowStorage(comptime Runtime: type) type {
271
307
  const window = &self.windows[index];
272
308
  const was_focused = window.info.focused;
273
309
  window.info.focused = focused;
310
+ var first_error: ?anyerror = null;
311
+ for (self.views[0..self.view_count], 0..) |*view, view_index| {
312
+ if (view.window_id != window.info.id) continue;
313
+ Self.setViewKeyboardActive(self, view_index, self.app_active and focused) catch |err| {
314
+ if (first_error == null) first_error = err;
315
+ };
316
+ }
274
317
  if (was_focused and !focused) {
275
- try CanvasWidgetEventMethods.resetCanvasTooltipIntentForWindowKeyLoss(self, window.info.id);
318
+ CanvasWidgetEventMethods.resetCanvasTooltipIntentForWindowKeyLoss(self, window.info.id) catch |err| {
319
+ if (first_error == null) first_error = err;
320
+ };
321
+ }
322
+ if (first_error) |err| return err;
323
+ }
324
+
325
+ /// Move the application-active register and project it into every
326
+ /// view's keyboard gate without disturbing per-window focus
327
+ /// memory. A deactivated app owns no keyboard focus; activation
328
+ /// reopens the gate only in the key window.
329
+ pub fn setAppActive(self: *Runtime, active: bool) anyerror!void {
330
+ self.app_active = active;
331
+ var first_error: ?anyerror = null;
332
+ for (self.views[0..self.view_count], 0..) |*view, view_index| {
333
+ const window_index = Self.findWindowIndexById(self, view.window_id);
334
+ const window_focused = if (window_index) |index| self.windows[index].info.focused else false;
335
+ Self.setViewKeyboardActive(self, view_index, active and window_focused) catch |err| {
336
+ if (first_error == null) first_error = err;
337
+ };
338
+ }
339
+ if (first_error) |err| return err;
340
+ }
341
+
342
+ fn setViewKeyboardActive(self: *Runtime, view_index: usize, active: bool) anyerror!void {
343
+ const CanvasWidgetEventMethods = runtime_canvas_widget_events.RuntimeCanvasWidgetEvents(Runtime);
344
+ const view = &self.views[view_index];
345
+ if (view.keyboard_active == active) return;
346
+ const previous_state = view.canvasWidgetRenderState();
347
+ view.keyboard_active = active;
348
+ const next_state = view.canvasWidgetRenderState();
349
+ if (!CanvasWidgetEventMethods.canvasWidgetRenderStatesEqual(previous_state, next_state)) {
350
+ try CanvasWidgetEventMethods.invalidateForCanvasWidgetRenderStateChange(self, view_index, previous_state, next_state);
276
351
  }
277
352
  }
278
353
 
@@ -7,6 +7,7 @@ const bridge_responses = @import("bridge_responses.zig");
7
7
  const runtime_canvas_widget_events = @import("canvas_widget_events.zig");
8
8
  const runtime_clock = @import("clock.zig");
9
9
  const runtime_state = @import("state.zig");
10
+ const runtime_view = @import("view.zig");
10
11
  const runtime_window_storage = @import("window_storage.zig");
11
12
  const platform = @import("../platform/root.zig");
12
13
  const security = @import("../security/root.zig");
@@ -492,6 +493,7 @@ pub fn RuntimeWindowViewRuntime(comptime Runtime: type) type {
492
493
  .gpu_status = if (options.kind == .gpu_surface) .ready else .unavailable,
493
494
  .gpu_surface_created_timestamp_ns = if (options.kind == .gpu_surface) timestampToU64(nowNanoseconds()) else 0,
494
495
  .focused = false,
496
+ .keyboard_active = windowKeyboardActive(self, options.window_id),
495
497
  .open = true,
496
498
  };
497
499
  self.views[index].label = try copyInto(&self.views[index].label_storage, options.label);
@@ -503,6 +505,12 @@ pub fn RuntimeWindowViewRuntime(comptime Runtime: type) type {
503
505
  self.view_count += 1;
504
506
  }
505
507
 
508
+ fn windowKeyboardActive(self: *const Runtime, window_id: platform.WindowId) bool {
509
+ if (!self.app_active) return false;
510
+ const window_index = WindowStorageMethods.findWindowIndexById(self, window_id) orelse return false;
511
+ return self.windows[window_index].info.focused;
512
+ }
513
+
506
514
  pub fn findViewIndex(self: *const Runtime, window_id: platform.WindowId, label: []const u8) ?usize {
507
515
  for (self.views[0..self.view_count], 0..) |*view, index| {
508
516
  if (view.open and view.window_id == window_id and std.mem.eql(u8, view.label, label)) return index;
@@ -542,9 +550,12 @@ pub fn RuntimeWindowViewRuntime(comptime Runtime: type) type {
542
550
  }
543
551
  // A view losing focus drops its tooltip state and
544
552
  // re-stamps hidden — no stale tooltip may keep floating
545
- // in a view the keyboard just left.
553
+ // in a view the keyboard just left — and its IME grace
554
+ // dies with the blur (the shared hygiene every
555
+ // focus-mutation path applies).
546
556
  if (was_focused and !view.focused) {
547
557
  try CanvasWidgetEventMethods.resetCanvasTooltipIntentForViewBlur(self, view_index);
558
+ runtime_view.clearImeGraceOnViewBlur(self, view);
548
559
  }
549
560
  }
550
561
  for (self.webviews[0..self.webview_count]) |*webview| {
@@ -566,9 +577,10 @@ pub fn RuntimeWindowViewRuntime(comptime Runtime: type) type {
566
577
  try CanvasWidgetEventMethods.invalidateForCanvasWidgetRenderStateChange(self, view_index, previous_state, next_state);
567
578
  }
568
579
  // Window-level focus loss blurs every view: same tooltip
569
- // reset as a per-view focus move.
580
+ // reset and IME-grace hygiene as a per-view focus move.
570
581
  if (was_focused) {
571
582
  try CanvasWidgetEventMethods.resetCanvasTooltipIntentForViewBlur(self, view_index);
583
+ runtime_view.clearImeGraceOnViewBlur(self, view);
572
584
  }
573
585
  }
574
586
  for (self.webviews[0..self.webview_count]) |*webview| {
@@ -629,6 +641,26 @@ pub fn RuntimeWindowViewRuntime(comptime Runtime: type) type {
629
641
 
630
642
  pub fn removeViewAt(self: *Runtime, index: usize) void {
631
643
  if (index >= self.view_count) return;
644
+ // A target-less IME composition owned by this view dies with
645
+ // it: a later view REUSING the same window+label must not
646
+ // inherit the stale sequence (its first composition update
647
+ // would be mistaken for a continuation and bypass its own
648
+ // focused editor) NOR the cancel-to-commit grace (whose
649
+ // trailing text would leak target-less into the new
650
+ // surface — cancellation zeroes the preedit length before
651
+ // arming the grace, so both states gate the clear). The
652
+ // buffer stays allocated; only the ownership state clears.
653
+ if ((self.targetless_ime_preedit_len > 0 or self.targetless_ime_commit_grace) and
654
+ self.targetless_ime_preedit_window == self.views[index].window_id and
655
+ std.mem.eql(
656
+ u8,
657
+ self.targetless_ime_preedit_label[0..self.targetless_ime_preedit_label_len],
658
+ self.views[index].label,
659
+ ))
660
+ {
661
+ self.targetless_ime_preedit_len = 0;
662
+ self.targetless_ime_commit_grace = false;
663
+ }
632
664
  var cursor = index;
633
665
  while (cursor + 1 < self.view_count) : (cursor += 1) {
634
666
  const next = &self.views[cursor + 1];
@@ -47,6 +47,7 @@ pub fn RuntimeWindowViews(comptime Runtime: type) type {
47
47
  pub const findShellLayoutIndex = WindowStorageMethods.findShellLayoutIndex;
48
48
  pub const removeShellLayoutForWindow = WindowStorageMethods.removeShellLayoutForWindow;
49
49
  pub const setFocusedIndex = WindowStorageMethods.setFocusedIndex;
50
+ pub const setAppActive = WindowStorageMethods.setAppActive;
50
51
  pub const findWindowIndexById = WindowStorageMethods.findWindowIndexById;
51
52
  pub const findWindowIndexByLabel = WindowStorageMethods.findWindowIndexByLabel;
52
53
  pub const allocateWindowId = WindowStorageMethods.allocateWindowId;
@@ -113,11 +114,19 @@ pub fn RuntimeWindowViews(comptime Runtime: type) type {
113
114
  // a window that never lost key.
114
115
  const was_open = self.windows[index].info.open;
115
116
  const was_focused = self.windows[index].info.focused;
117
+ // `hidden` clears with `open`: an app-driven close of a
118
+ // policy-hidden window (a menu-bar app tearing down its
119
+ // hidden panel) must not leave {open=false, hidden=true}
120
+ // in the runtime table — the JS bridge exposes hidden, and
121
+ // a closed window is not "hidden", it is gone.
122
+ const was_hidden = self.windows[index].info.hidden;
116
123
  self.windows[index].info.open = false;
117
124
  self.windows[index].info.focused = false;
125
+ self.windows[index].info.hidden = false;
118
126
  self.options.platform.services.closeWindow(window_id) catch |err| {
119
127
  self.windows[index].info.open = was_open;
120
128
  self.windows[index].info.focused = was_focused;
129
+ self.windows[index].info.hidden = was_hidden;
121
130
  return err;
122
131
  };
123
132
  Self.removeWindowRuntimeViews(self, window_id);
@@ -129,10 +138,61 @@ pub fn RuntimeWindowViews(comptime Runtime: type) type {
129
138
  /// bookkeeping moves: a minimized window stays open and keeps
130
139
  /// its views — it comes back from the Dock/taskbar.
131
140
  pub fn minimizeWindow(self: *Runtime, window_id: platform.WindowId) anyerror!void {
132
- if (Self.findWindowIndexById(self, window_id) == null) return error.WindowNotFound;
141
+ const index = Self.findWindowIndexById(self, window_id) orelse return error.WindowNotFound;
142
+ // The same liveness gate as showWindow below: a retained
143
+ // closed slot must not reach the platform (the CEF host
144
+ // would genie the retained closed window into the Dock).
145
+ if (!self.windows[index].info.open) return error.WindowNotFound;
133
146
  try self.options.platform.services.minimizeWindow(window_id);
134
147
  }
135
148
 
149
+ /// The real OS show verb: unhide + activate — the counterpart
150
+ /// to a `close_policy = .hide` hide, and what a tray "Open"
151
+ /// action resolves to. Like `closeWindow`, the runtime flag
152
+ /// flips BEFORE the platform call (hosts that emit the frame
153
+ /// event synchronously would echo the same state) and rolls
154
+ /// back on platform failure. A window that was never hidden
155
+ /// just comes to the front.
156
+ pub fn showWindow(self: *Runtime, window_id: platform.WindowId) anyerror!void {
157
+ const index = Self.findWindowIndexById(self, window_id) orelse return error.WindowNotFound;
158
+ // A closed window keeps its table slot (the id and label
159
+ // release lazily, at the next create), so resolving the id
160
+ // is not liveness: a dead slot must answer WindowNotFound —
161
+ // the same answer every not-open gate in the runtime gives —
162
+ // BEFORE the platform call. The null platform would accept
163
+ // the show and report {open:false, focused:true}; the CEF
164
+ // host retains browser-bearing windows past their close and
165
+ // would visibly re-order one onto the glass.
166
+ if (!self.windows[index].info.open) return error.WindowNotFound;
167
+ const was_hidden = self.windows[index].info.hidden;
168
+ self.windows[index].info.hidden = false;
169
+ self.options.platform.services.showWindow(window_id) catch |err| {
170
+ self.windows[index].info.hidden = was_hidden;
171
+ return err;
172
+ };
173
+ // The contract is show AND activate: every host's show verb
174
+ // makes the window key (makeKeyAndOrderFront / activate), so
175
+ // the runtime table must move focus with it or listWindows
176
+ // and the JS bridge report the shown window unfocused while
177
+ // it stands frontmost on the glass. Same post-success flow
178
+ // as focusWindow — through the setFocusedIndex seam, so the
179
+ // dethroned window's key-loss consequence fires — and only
180
+ // AFTER the platform accepted: a refused show rolls back
181
+ // hidden above and moves no focus.
182
+ try Self.setFocusedIndex(self, index);
183
+ self.invalidated = true;
184
+ }
185
+
186
+ /// The graceful app quit: ask the platform to terminate through
187
+ /// the SAME shutdown path a last-window close takes — the host
188
+ /// emits `app_shutdown` (journaled like any platform event),
189
+ /// `app.stop` runs exactly once, and a recording session seals
190
+ /// its journal. No runtime bookkeeping here: the shutdown event
191
+ /// owns the teardown.
192
+ pub fn quitApp(self: *Runtime) anyerror!void {
193
+ try self.options.platform.services.quitApp();
194
+ }
195
+
136
196
  pub fn updateWindowState(self: *Runtime, state: platform.WindowState) !void {
137
197
  try WindowStorageMethods.updateWindowState(self, state);
138
198
  if (!state.open) Self.removeWindowRuntimeViews(self, state.id);
@@ -173,6 +233,7 @@ pub fn RuntimeWindowViews(comptime Runtime: type) type {
173
233
  .show = shell_layout.shellWindowShowMode(shell_window),
174
234
  .min_width = shell_window.min_width,
175
235
  .min_height = shell_window.min_height,
236
+ .close_policy = shell_layout.shellClosePolicy(shell_window.close_policy),
176
237
  .source = source,
177
238
  }, source_reloads_from_app, source_policy);
178
239
  errdefer Self.closeWindow(self, info.id) catch {};
@@ -0,0 +1,15 @@
1
+ //! Test root for the terminal-session store tests, which only have
2
+ //! anything to assert in a build that WIRES the emulator behind
3
+ //! `<terminal pty={key}>`. This package pins no emulator (apps that want
4
+ //! live sessions pin it themselves and ask with
5
+ //! `addAppArtifacts(.{ .terminal_sessions = true })`), so under this
6
+ //! repository's own `zig build test` every case skips; an app build that
7
+ //! opts in points a test artifact at this file and runs them for real.
8
+ //! It lives beside `root.zig` because a test root sets the module path,
9
+ //! and the runtime's files import across `src/`.
10
+ //!
11
+ //! See `examples/workbench/build.zig` for the consumer-side wiring.
12
+
13
+ test {
14
+ _ = @import("runtime/terminal_session_tests.zig");
15
+ }
@@ -102,6 +102,7 @@ pub const Metadata = struct {
102
102
  allocator.free(window.label);
103
103
  if (window.title) |title| allocator.free(title);
104
104
  allocator.free(window.titlebar);
105
+ allocator.free(window.close_policy);
105
106
  }
106
107
  if (self.windows.len > 0) allocator.free(self.windows);
107
108
  for (self.shell.windows) |window| {
@@ -109,6 +110,7 @@ pub const Metadata = struct {
109
110
  if (window.title) |title| allocator.free(title);
110
111
  allocator.free(window.restore_policy);
111
112
  allocator.free(window.titlebar);
113
+ allocator.free(window.close_policy);
112
114
  for (window.views) |view| {
113
115
  allocator.free(view.label);
114
116
  allocator.free(view.kind);
@@ -200,6 +202,7 @@ pub const WindowMetadata = struct {
200
202
  titlebar: []const u8 = "standard",
201
203
  min_width: f32 = 0,
202
204
  min_height: f32 = 0,
205
+ close_policy: []const u8 = "quit",
203
206
  };
204
207
 
205
208
  pub const ShellMetadata = struct {
@@ -237,6 +240,7 @@ pub const ShellWindowMetadata = struct {
237
240
  titlebar: []const u8 = "standard",
238
241
  min_width: f32 = 0,
239
242
  min_height: f32 = 0,
243
+ close_policy: []const u8 = "quit",
240
244
  views: []const ShellViewMetadata = &.{},
241
245
  };
242
246
 
@@ -592,6 +596,7 @@ fn convertRawWindows(allocator: std.mem.Allocator, windows: []const RawWindow) !
592
596
  .titlebar = try allocator.dupe(u8, window.titlebar),
593
597
  .min_width = window.min_width,
594
598
  .min_height = window.min_height,
599
+ .close_policy = try allocator.dupe(u8, window.close_policy),
595
600
  };
596
601
  }
597
602
  return converted;
@@ -644,6 +649,7 @@ fn convertRawShellWindows(allocator: std.mem.Allocator, windows: []const RawShel
644
649
  .titlebar = try allocator.dupe(u8, window.titlebar),
645
650
  .min_width = window.min_width,
646
651
  .min_height = window.min_height,
652
+ .close_policy = try allocator.dupe(u8, window.close_policy),
647
653
  .views = try convertRawShellViews(allocator, window.views),
648
654
  };
649
655
  }
@@ -843,6 +849,7 @@ fn convertWindows(allocator: std.mem.Allocator, windows: []const WindowMetadata)
843
849
  .titlebar = try parseTitlebarStyle(window.titlebar),
844
850
  .min_width = try parseWindowMinSize(window.min_width),
845
851
  .min_height = try parseWindowMinSize(window.min_height),
852
+ .close_policy = try parseClosePolicy(window.close_policy),
846
853
  };
847
854
  }
848
855
  return converted;
@@ -867,6 +874,7 @@ fn parseShell(allocator: std.mem.Allocator, shell: ShellMetadata) !app_manifest.
867
874
  const titlebar = try parseTitlebarStyle(window.titlebar);
868
875
  const min_width = try parseWindowMinSize(window.min_width);
869
876
  const min_height = try parseWindowMinSize(window.min_height);
877
+ const close_policy = try parseClosePolicy(window.close_policy);
870
878
  const views = try parseShellViews(allocator, window.views);
871
879
  windows[index] = .{
872
880
  .label = window.label,
@@ -881,6 +889,7 @@ fn parseShell(allocator: std.mem.Allocator, shell: ShellMetadata) !app_manifest.
881
889
  .titlebar = titlebar,
882
890
  .min_width = min_width,
883
891
  .min_height = min_height,
892
+ .close_policy = close_policy,
884
893
  .views = views,
885
894
  };
886
895
  initialized += 1;
@@ -1273,6 +1282,12 @@ fn parseTitlebarStyle(value: []const u8) !app_manifest.WindowTitlebarStyle {
1273
1282
  return error.InvalidWindowTitlebarStyle;
1274
1283
  }
1275
1284
 
1285
+ fn parseClosePolicy(value: []const u8) !app_manifest.WindowClosePolicy {
1286
+ if (std.mem.eql(u8, value, "quit")) return .quit;
1287
+ if (std.mem.eql(u8, value, "hide")) return .hide;
1288
+ return error.InvalidWindowClosePolicy;
1289
+ }
1290
+
1276
1291
  /// Same validation posture as the titlebar style: a min-size floor the
1277
1292
  /// host cannot honor (negative or non-finite) is a manifest error, not
1278
1293
  /// a silent clamp. 0 is the "no floor" sentinel.
@@ -1860,6 +1875,66 @@ test "manifest parser rejects negative window min sizes" {
1860
1875
  try std.testing.expectError(error.InvalidWindowMinSize, parseShell(std.testing.allocator, metadata.shell));
1861
1876
  }
1862
1877
 
1878
+ test "manifest parser reads window close policies" {
1879
+ const metadata = try parseText(std.testing.allocator,
1880
+ \\.{
1881
+ \\ .id = "com.example.app",
1882
+ \\ .name = "example",
1883
+ \\ .version = "1.2.3",
1884
+ \\ .windows = .{
1885
+ \\ .{ .label = "main", .close_policy = "hide" },
1886
+ \\ .{ .label = "doc" },
1887
+ \\ },
1888
+ \\ .shell = .{
1889
+ \\ .windows = .{
1890
+ \\ .{ .label = "scene", .close_policy = "hide", .views = .{ .{ .label = "content", .kind = "webview", .url = "zero://app/index.html" } } },
1891
+ \\ },
1892
+ \\ },
1893
+ \\}
1894
+ );
1895
+ defer metadata.deinit(std.testing.allocator);
1896
+
1897
+ try std.testing.expectEqualStrings("hide", metadata.windows[0].close_policy);
1898
+ try std.testing.expectEqualStrings("quit", metadata.windows[1].close_policy);
1899
+ try std.testing.expectEqualStrings("hide", metadata.shell.windows[0].close_policy);
1900
+
1901
+ const windows = try convertWindows(std.testing.allocator, metadata.windows);
1902
+ defer std.testing.allocator.free(windows);
1903
+ try std.testing.expectEqual(app_manifest.WindowClosePolicy.hide, windows[0].close_policy);
1904
+ // Undeclared stays the .quit default — behavior unchanged for
1905
+ // every existing app.
1906
+ try std.testing.expectEqual(app_manifest.WindowClosePolicy.quit, windows[1].close_policy);
1907
+
1908
+ const shell = try parseShell(std.testing.allocator, metadata.shell);
1909
+ defer deinitParsedShell(std.testing.allocator, shell);
1910
+ try std.testing.expectEqual(app_manifest.WindowClosePolicy.hide, shell.windows[0].close_policy);
1911
+ }
1912
+
1913
+ test "manifest parser rejects unknown window close policy" {
1914
+ const metadata = try parseText(std.testing.allocator,
1915
+ \\.{
1916
+ \\ .id = "com.example.app",
1917
+ \\ .name = "example",
1918
+ \\ .version = "1.2.3",
1919
+ \\ .windows = .{
1920
+ \\ .{ .label = "main", .close_policy = "minimize" },
1921
+ \\ },
1922
+ \\ .shell = .{
1923
+ \\ .windows = .{
1924
+ \\ .{ .label = "scene", .close_policy = "event", .views = .{ .{ .label = "content", .kind = "webview", .url = "zero://app/index.html" } } },
1925
+ \\ },
1926
+ \\ },
1927
+ \\}
1928
+ );
1929
+ defer metadata.deinit(std.testing.allocator);
1930
+
1931
+ // "event" is the deliberately reserved future tier (model-decides
1932
+ // close); it parses as unknown until it ships — staged honestly,
1933
+ // never accepted early.
1934
+ try std.testing.expectError(error.InvalidWindowClosePolicy, convertWindows(std.testing.allocator, metadata.windows));
1935
+ try std.testing.expectError(error.InvalidWindowClosePolicy, parseShell(std.testing.allocator, metadata.shell));
1936
+ }
1937
+
1863
1938
  test "manifest parser rejects unknown window titlebar style" {
1864
1939
  const metadata = try parseText(std.testing.allocator,
1865
1940
  \\.{
@@ -96,6 +96,7 @@ pub const RawWindow = struct {
96
96
  titlebar: []const u8 = "standard",
97
97
  min_width: f32 = 0,
98
98
  min_height: f32 = 0,
99
+ close_policy: []const u8 = "quit",
99
100
  };
100
101
 
101
102
  pub const RawShell = struct {
@@ -133,6 +134,7 @@ pub const RawShellWindow = struct {
133
134
  titlebar: []const u8 = "standard",
134
135
  min_width: f32 = 0,
135
136
  min_height: f32 = 0,
137
+ close_policy: []const u8 = "quit",
136
138
  views: []const RawShellView = &.{},
137
139
  };
138
140