@native-sdk/cli 0.5.3 → 0.5.4

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 (79) hide show
  1. package/build/app.zig +41 -1
  2. package/build.zig +223 -3
  3. package/native-sdk.d.ts +17 -1
  4. package/package.json +9 -9
  5. package/packages/core/package-lock.json +2 -2
  6. package/packages/core/package.json +1 -1
  7. package/packages/core/rt/rt.zig +138 -3
  8. package/packages/core/sdk/core.ts +225 -3
  9. package/packages/core/src/emitter.ts +2586 -259
  10. package/skill-data/native-ui/SKILL.md +3 -2
  11. package/skill-data/ts-core/SKILL.md +9 -1
  12. package/src/app_runner/root.zig +81 -1
  13. package/src/app_runner/ts_core_main.zig +4 -0
  14. package/src/embed/tests.zig +119 -1
  15. package/src/platform/linux/gtk_host.c +112 -3
  16. package/src/platform/linux/root.zig +71 -0
  17. package/src/platform/macos/appkit_host.h +48 -2
  18. package/src/platform/macos/appkit_host.m +533 -34
  19. package/src/platform/macos/cef_host.mm +262 -10
  20. package/src/platform/macos/root.zig +92 -4
  21. package/src/platform/null_platform.zig +298 -5
  22. package/src/platform/root.zig +33 -0
  23. package/src/platform/types.zig +138 -4
  24. package/src/platform/windows/root.zig +134 -0
  25. package/src/platform/windows/webview2_host.cpp +152 -40
  26. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  27. package/src/primitives/app_manifest/root.zig +1 -0
  28. package/src/primitives/app_manifest/types.zig +32 -0
  29. package/src/primitives/canvas/tests.zig +1 -0
  30. package/src/primitives/canvas/ui.zig +11 -7
  31. package/src/primitives/canvas/ui_markup.zig +58 -34
  32. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  33. package/src/primitives/canvas/ui_markup_compiled.zig +23 -8
  34. package/src/primitives/canvas/ui_markup_compiled_tests.zig +55 -0
  35. package/src/primitives/canvas/ui_markup_contract.zig +1 -1
  36. package/src/primitives/canvas/ui_markup_contract_tests.zig +26 -0
  37. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  38. package/src/primitives/canvas/ui_markup_tests.zig +29 -9
  39. package/src/primitives/canvas/ui_markup_view.zig +29 -11
  40. package/src/primitives/canvas/ui_markup_view_tests.zig +150 -9
  41. package/src/primitives/canvas/ui_schema.zig +13 -0
  42. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  43. package/src/root.zig +7 -0
  44. package/src/runtime/api.zig +6 -4
  45. package/src/runtime/bridge_payload.zig +7 -0
  46. package/src/runtime/bridge_responses.zig +5 -0
  47. package/src/runtime/builtin_bridge.zig +9 -4
  48. package/src/runtime/canvas_font_tests.zig +439 -2
  49. package/src/runtime/canvas_fonts.zig +61 -6
  50. package/src/runtime/canvas_image_tests.zig +195 -0
  51. package/src/runtime/canvas_images.zig +45 -12
  52. package/src/runtime/core.zig +58 -11
  53. package/src/runtime/effects.zig +1664 -43
  54. package/src/runtime/effects_fetch_tests.zig +89 -1
  55. package/src/runtime/effects_file_tests.zig +41 -1
  56. package/src/runtime/effects_image_tests.zig +1304 -0
  57. package/src/runtime/effects_tests.zig +153 -0
  58. package/src/runtime/flow.zig +17 -0
  59. package/src/runtime/root.zig +11 -0
  60. package/src/runtime/session_blobs.zig +689 -0
  61. package/src/runtime/session_journal.zig +85 -21
  62. package/src/runtime/session_record.zig +27 -2
  63. package/src/runtime/session_replay.zig +168 -16
  64. package/src/runtime/session_tests.zig +1992 -0
  65. package/src/runtime/shell_layout.zig +7 -0
  66. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  67. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  68. package/src/runtime/tests.zig +2 -0
  69. package/src/runtime/ts_core_host.zig +439 -4
  70. package/src/runtime/ts_core_host_tests.zig +41 -0
  71. package/src/runtime/ts_ui_app.zig +7 -0
  72. package/src/runtime/ui_app.zig +111 -6
  73. package/src/runtime/ui_app_window_tests.zig +468 -0
  74. package/src/runtime/window_command_bridge_tests.zig +117 -0
  75. package/src/runtime/window_storage.zig +32 -0
  76. package/src/runtime/window_views.zig +61 -1
  77. package/src/tooling/manifest.zig +75 -0
  78. package/src/tooling/raw_manifest.zig +2 -0
  79. package/src/tooling/templates.zig +219 -1
@@ -739,6 +739,16 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
739
739
  /// register would then fail `FontIdInUse` and bury the real
740
740
  /// error).
741
741
  fonts_registered: bool = false,
742
+ /// The runtime's registered-font count the installed trees last
743
+ /// measured against. Registration is permanent with no
744
+ /// unregister, so the count IS the runtime's fonts generation:
745
+ /// a mismatch on a presented frame means a face joined the
746
+ /// registry AFTER the trees were built (late registration
747
+ /// through `runtime.registerCanvasFont` — `Options.fonts` lands
748
+ /// before the installing build), and every installed surface
749
+ /// must rebuild so layout re-measures with the new face
750
+ /// (`rebuildForRegisteredFonts`).
751
+ fonts_built_count: usize = 0,
742
752
  /// Exactly-once guard for `Options.init_fx`, independent of
743
753
  /// `installed` so a failed install rebuild cannot rerun it.
744
754
  init_fx_ran: bool = false,
@@ -1053,6 +1063,8 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
1053
1063
  .context = runtime,
1054
1064
  .close_fn = effectsCloseWindowByLabel,
1055
1065
  .minimize_fn = effectsMinimizeWindowByLabel,
1066
+ .show_fn = effectsShowWindowByLabel,
1067
+ .quit_fn = effectsQuitApp,
1056
1068
  });
1057
1069
  if (runtime.options.session_recorder) |recorder| {
1058
1070
  self.effects.bindJournal(recorder.effectJournal());
@@ -1090,6 +1102,23 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
1090
1102
  // envMsgs dispatch consumes the queue on the
1091
1103
  // replayed installing frame (zero env reads).
1092
1104
  .env => try self.effects.pushReplayEnv(record.stderr_tail, record.payload),
1105
+ // `.image` records deliver the RECORDED terminal
1106
+ // verbatim (byte-identical Msg stream on any host)
1107
+ // and re-register the journaled source bytes —
1108
+ // resolved from the blob store into `payload` by
1109
+ // the replayer — best-effort for presentation.
1110
+ // Loop-side validation rejections never reach here
1111
+ // (marked by `exit_reason == .rejected`, they
1112
+ // regenerate and are skipped); a worker-origin
1113
+ // `.rejected` terminal feeds like any failure class.
1114
+ .image => try self.effects.feedImageResult(
1115
+ record.key,
1116
+ record.image_outcome,
1117
+ record.image_width,
1118
+ record.image_height,
1119
+ record.status,
1120
+ record.payload,
1121
+ ),
1093
1122
  // Spectrum records feed through the band-carrying
1094
1123
  // helper so replay repaints identical bars; every
1095
1124
  // other audio kind rides the plain shape.
@@ -1149,7 +1178,16 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
1149
1178
  self.bindEffectsChannel(runtime);
1150
1179
  self.syncModel(runtime, self.canvas_window_id);
1151
1180
  var dispatched = false;
1152
- while (self.effects.takeMsg()) |msg| {
1181
+ // One pass consumes only completions that existed when it
1182
+ // began: a load started by an update handler in THIS pass
1183
+ // that finishes while the pass still runs waits for the wake
1184
+ // its producer already nudged. That keeps the session
1185
+ // journal's event boundaries causal — every result recorded
1186
+ // ahead of this wake's event record answers a request from
1187
+ // an earlier dispatch, so replay's file-order feed always
1188
+ // finds the parked request (see Effects.DrainBoundary).
1189
+ var boundary = self.effects.drainBoundary();
1190
+ while (self.effects.takeMsgWithin(&boundary)) |msg| {
1153
1191
  self.applyMsg(msg);
1154
1192
  dispatched = true;
1155
1193
  }
@@ -1250,14 +1288,20 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
1250
1288
  /// Whether a rebuild must push its tokens into the runtime's
1251
1289
  /// stored copy. Derived tokens can change with any model or
1252
1290
  /// appearance input, so they always re-emit. Static tokens are
1253
- /// fixed by the app, but the runtime stamps the surface scale
1254
- /// onto them (`effectiveTokens`), so a stored copy holding a
1255
- /// stale scale re-emits too — hairlines re-snap after a move
1256
- /// between monitors — while ordinary rebuilds keep skipping the
1257
- /// redundant emission.
1291
+ /// fixed by the app, but the runtime stamps two live values onto
1292
+ /// them: the surface scale (`effectiveTokens` — a stored copy
1293
+ /// holding a stale scale re-emits so hairlines re-snap after a
1294
+ /// move between monitors) and the text-measure provider
1295
+ /// (`tokensWithTextMeasure` — the FIRST font registration binds
1296
+ /// the runtime's font-aware provider on platforms without host
1297
+ /// measurement, and a stored copy still measuring with the
1298
+ /// estimator would keep display-list text layout on
1299
+ /// pre-registration metrics). Ordinary rebuilds keep skipping
1300
+ /// the redundant emission.
1258
1301
  fn rebuildEmitsTokens(self: *const Self, runtime: *Runtime, window_id: platform.WindowId, canvas_label: []const u8, tokens: canvas.DesignTokens) bool {
1259
1302
  if (self.derivesTokens()) return true;
1260
1303
  const stored = runtime.canvasWidgetDesignTokens(window_id, canvas_label) catch return true;
1304
+ if (!std.meta.eql(stored.text_measure, tokens.text_measure)) return true;
1261
1305
  return stored.pixel_snap.scale != tokens.pixel_snap.scale;
1262
1306
  }
1263
1307
 
@@ -2949,6 +2993,12 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
2949
2993
  _ = try runtime.emitCanvasWidgetDisplayList(frame_event.window_id, self.options.canvas_label, runtime.tokensWithTextMeasure(self.effectiveTokens()));
2950
2994
  }
2951
2995
  self.installed = true;
2996
+ // The installing rebuild measured with everything the
2997
+ // registry holds right now (declared fonts registered
2998
+ // above, embedder registrations before install): adopt
2999
+ // the count so only faces joining AFTER this build
3000
+ // trigger the late-registration rebuild.
3001
+ self.fonts_built_count = runtime.registeredCanvasFontCount();
2952
3002
  self.startMarkupWatch(runtime);
2953
3003
  self.installStatusItem(runtime);
2954
3004
  } else if (@abs(self.pixel_snap_scale - scale) > 0.001) {
@@ -2977,6 +3027,17 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
2977
3027
  // job.
2978
3028
  self.canvas_size = frame_event.size;
2979
3029
  try self.rebuild(runtime, frame_event.window_id);
3030
+ } else if (runtime.registeredCanvasFontCount() != self.fonts_built_count) {
3031
+ // A face joined the registry after install (late
3032
+ // registration through the runtime seam). The runtime
3033
+ // already invalidated measurement caches and requested
3034
+ // frames for every open surface (noteCanvasFontsChanged),
3035
+ // but repainting retained geometry only re-inks text
3036
+ // measured with the OLD seam answers: honoring the fonts
3037
+ // doc's promise — every open surface re-measures — means
3038
+ // rebuilding so layout and the re-emitted display lists
3039
+ // charge the registered face's advances.
3040
+ try self.rebuildForRegisteredFonts(runtime);
2980
3041
  } else if (self.options.web_panes != null) {
2981
3042
  // Re-snap the webview panes each presented frame: a shell
2982
3043
  // relayout that stomped a pane frame also invalidated the
@@ -3031,10 +3092,39 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
3031
3092
  // stale bounds.
3032
3093
  slot.canvas_size = frame_event.size;
3033
3094
  try self.rebuildWindowSlot(runtime, slot);
3095
+ } else if (runtime.registeredCanvasFontCount() != self.fonts_built_count) {
3096
+ // Late registration, first observed on a secondary
3097
+ // window's frame (registration requests frames for every
3098
+ // open surface, and arrival order is the platform's):
3099
+ // the same all-surfaces rebuild as the main canvas —
3100
+ // the count is app-level, so whichever surface's frame
3101
+ // lands first re-measures all of them.
3102
+ try self.rebuildForRegisteredFonts(runtime);
3034
3103
  }
3035
3104
  try self.presentFrame(runtime, frame_event, slot.canvasLabel(), installing);
3036
3105
  }
3037
3106
 
3107
+ /// A face joined the runtime's font registry after this app's
3108
+ /// trees were built: rebuild EVERY installed surface — main
3109
+ /// canvas and declared secondary windows — so widget frames and
3110
+ /// baked text runs re-measure against the registered face, then
3111
+ /// adopt the count. Rebuild re-emits each surface's display list
3112
+ /// (`rebuildEmitsTokens` treats a text-measure provider change
3113
+ /// as an emit reason), so the repaint the runtime already
3114
+ /// requested draws re-measured geometry, not re-inked stale
3115
+ /// frames.
3116
+ fn rebuildForRegisteredFonts(self: *Self, runtime: *Runtime) anyerror!void {
3117
+ try self.rebuildAllViews(runtime);
3118
+ // Adopt the count only AFTER the rebuild succeeded:
3119
+ // production dispatch degrades errors, so a failed rebuild
3120
+ // (widget budget, allocator pressure, a secondary window's
3121
+ // emit) that had already adopted the count would mark stale
3122
+ // layouts as font-current and never retry. Left unadopted,
3123
+ // the error leaves the count mismatched and the next
3124
+ // presented frame retries the rebuild.
3125
+ self.fonts_built_count = runtime.registeredCanvasFontCount();
3126
+ }
3127
+
3038
3128
  /// Present the planned canvas frame: GPU packet when the platform
3039
3129
  /// has a packet presenter (macOS/Metal — unchanged), otherwise the
3040
3130
  /// CPU reference-rendered pixel path (`presentGpuSurfacePixels`,
@@ -3570,3 +3660,18 @@ fn effectsMinimizeWindowByLabel(context: *anyopaque, window_label: []const u8) b
3570
3660
  runtime.minimizeWindow(window_id) catch return false;
3571
3661
  return true;
3572
3662
  }
3663
+
3664
+ fn effectsShowWindowByLabel(context: *anyopaque, window_label: []const u8) bool {
3665
+ const runtime: *Runtime = @ptrCast(@alignCast(context));
3666
+ // A policy-hidden window keeps `open` true, so the same live-window
3667
+ // resolution close/minimize use finds it.
3668
+ const window_id = effectsWindowIdByLabel(runtime, window_label) orelse return false;
3669
+ runtime.showWindow(window_id) catch return false;
3670
+ return true;
3671
+ }
3672
+
3673
+ fn effectsQuitApp(context: *anyopaque) bool {
3674
+ const runtime: *Runtime = @ptrCast(@alignCast(context));
3675
+ runtime.quitApp() catch return false;
3676
+ return true;
3677
+ }
@@ -594,6 +594,8 @@ const VerbMsg = union(enum) {
594
594
  minimize_settings,
595
595
  minimize_main,
596
596
  close_settings,
597
+ show_settings,
598
+ quit,
597
599
  settings_closed,
598
600
  };
599
601
 
@@ -606,6 +608,8 @@ fn verbUpdate(model: *VerbModel, msg: VerbMsg, fx: *VerbApp.Effects) void {
606
608
  // The imperative close, for the seam test — a real app's
607
609
  // model-declared window usually closes declaratively instead.
608
610
  .close_settings => fx.closeWindow(settings_window_label),
611
+ .show_settings => fx.showWindow(settings_window_label),
612
+ .quit => fx.quitApp(),
609
613
  .settings_closed => model.settings_open = false,
610
614
  }
611
615
  }
@@ -692,6 +696,14 @@ test "window-action effects resolve labels to live windows and drive the real ve
692
696
  if (info.id == settings_id) try std.testing.expect(info.open);
693
697
  }
694
698
 
699
+ // Show by label is the counterpart verb: it reaches the platform's
700
+ // show (the minimized window comes back with focus) and the mirror
701
+ // counts it.
702
+ try app_state.dispatch(&harness.runtime, 1, .show_settings);
703
+ try std.testing.expectEqual(@as(u32, 1), harness.null_platform.showCountForWindow(settings_id));
704
+ try std.testing.expectEqual(@as(u32, 1), app_state.effects.windowActionState().show_count);
705
+ try std.testing.expectEqualStrings(settings_window_label, app_state.effects.windowActionState().lastLabel());
706
+
695
707
  // The main window resolves by label too (the runtime adopted the
696
708
  // host-reported startup window above). The fake host never created
697
709
  // a native window for it — live hosts own the startup window — so
@@ -714,4 +726,460 @@ test "window-action effects resolve labels to live windows and drive the real ve
714
726
  // main window still answers after the secondary's churn.
715
727
  try app_state.dispatch(&harness.runtime, 1, .minimize_main);
716
728
  try std.testing.expectEqual(@as(u32, 3), app_state.effects.windowActionState().minimize_count);
729
+
730
+ // quitApp asks the platform for the graceful terminate (the modeled
731
+ // host records the request; a real host QUEUES the stop so
732
+ // app_shutdown emits on the next loop turn, after this dispatch
733
+ // has returned) and the mirror counts it.
734
+ try app_state.dispatch(&harness.runtime, 1, .quit);
735
+ try std.testing.expectEqual(@as(u32, 1), app_state.effects.windowActionState().quit_count);
736
+ try std.testing.expectEqual(@as(u32, 1), harness.null_platform.quit_request_count);
737
+ // The host's queued shutdown echo delivers the exactly-once stop
738
+ // hook — the same path a last-window close takes. Draining it here
739
+ // IS the next loop turn, and it drains exactly once.
740
+ const shutdown_event = harness.null_platform.takeQueuedQuit() orelse return error.TestUnexpectedResult;
741
+ try std.testing.expect(harness.null_platform.takeQueuedQuit() == null);
742
+ try harness.runtime.dispatchPlatformEvent(app, shutdown_event);
743
+ }
744
+
745
+ test "the .hide close then showWindow round-trip: tray Open brings the hidden window back" {
746
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
747
+ defer harness.destroy(std.testing.allocator);
748
+ harness.null_platform.gpu_surfaces = true;
749
+ const app_state = try VerbApp.create(std.heap.page_allocator, .{
750
+ .name = "ui-app-hide-show-loop",
751
+ .scene = panel_scene,
752
+ .canvas_label = canvas_label,
753
+ .update_fx = verbUpdate,
754
+ .view = verbView,
755
+ .windows_fn = verbWindows,
756
+ .window_view = verbWindowView,
757
+ });
758
+ defer app_state.destroy();
759
+ const app = app_state.app();
760
+ try harness.start(app);
761
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
762
+ .label = canvas_label,
763
+ .size = geometry.SizeF.init(400, 300),
764
+ .scale_factor = 2,
765
+ .frame_index = 1,
766
+ .timestamp_ns = 1_000_000,
767
+ .nonblank = true,
768
+ } });
769
+ var buffer: [support.platform.max_windows]support.platform.WindowInfo = undefined;
770
+ var settings_id: support.platform.WindowId = 0;
771
+ for (harness.runtime.listWindows(&buffer)) |info| {
772
+ if (std.mem.eql(u8, info.label, settings_window_label)) settings_id = info.id;
773
+ }
774
+ try std.testing.expect(settings_id != 0);
775
+
776
+ // Give the live window the .hide policy at the platform (the seam a
777
+ // manifest declaration rides) and close it as the user: hidden, not
778
+ // gone.
779
+ for (harness.null_platform.windows[0..harness.null_platform.window_count], 0..) |window, index| {
780
+ if (window.id == settings_id) harness.null_platform.window_close_policy[index] = .hide;
781
+ }
782
+ const hide_event = harness.null_platform.userCloseWindow(settings_id).?;
783
+ try harness.runtime.dispatchPlatformEvent(app, hide_event);
784
+ for (harness.runtime.listWindows(&buffer)) |info| {
785
+ if (info.id == settings_id) try std.testing.expect(info.hidden);
786
+ }
787
+
788
+ // The tray "Open" consequence: the model returns the show verb, the
789
+ // label resolves against the still-open hidden window, and the
790
+ // platform + runtime state both flip back.
791
+ try app_state.dispatch(&harness.runtime, 1, .show_settings);
792
+ try std.testing.expectEqual(@as(u32, 1), harness.null_platform.showCountForWindow(settings_id));
793
+ for (harness.runtime.listWindows(&buffer)) |info| {
794
+ if (info.id == settings_id) try std.testing.expect(!info.hidden);
795
+ }
796
+ for (harness.null_platform.windows[0..harness.null_platform.window_count]) |window| {
797
+ if (window.id == settings_id) {
798
+ try std.testing.expect(!window.hidden);
799
+ try std.testing.expect(window.focused);
800
+ }
801
+ }
802
+ }
803
+
804
+ // ------------------------------------- close_policy (.quit | .hide)
805
+
806
+ test "close_policy .hide: the user close hides the window, nothing tears down, and the reopen re-shows it" {
807
+ const fixture = try Fixture.create();
808
+ defer fixture.destroy();
809
+
810
+ // A menu-bar-shaped window through the REAL create seam (the same
811
+ // `ShellWindow` path a manifest-declared window rides): the
812
+ // declared policy must survive to the platform.
813
+ const info = try fixture.harness.runtime.createSourcelessShellWindow(.{
814
+ .label = "player",
815
+ .title = "Player",
816
+ .width = 400,
817
+ .height = 300,
818
+ .close_policy = .hide,
819
+ });
820
+ try std.testing.expectEqual(support.platform.WindowClosePolicy.hide, fixture.harness.null_platform.closePolicyForWindow(info.id).?);
821
+
822
+ // The user clicks the red button: the modeled close delegate hides
823
+ // instead of closing — the window stays in the host's records
824
+ // (open = true), only the hidden flag flips, and because open never
825
+ // transitioned, NO window_closed app event (and so no on_close Msg)
826
+ // fires.
827
+ const hide_event = fixture.harness.null_platform.userCloseWindow(info.id).?;
828
+ try std.testing.expect(hide_event.window_frame_changed.open);
829
+ try std.testing.expect(hide_event.window_frame_changed.hidden);
830
+ try fixture.harness.runtime.dispatchPlatformEvent(fixture.app, hide_event);
831
+ try std.testing.expectEqual(@as(u32, 0), fixture.app_state.model.user_closes);
832
+ var buffer: [support.platform.max_windows]support.platform.WindowInfo = undefined;
833
+ for (fixture.harness.runtime.listWindows(&buffer)) |window| {
834
+ if (window.id != info.id) continue;
835
+ try std.testing.expect(window.open);
836
+ try std.testing.expect(window.hidden);
837
+ }
838
+
839
+ // The Dock-icon reopen: every policy-hidden window re-shows and the
840
+ // hidden flag clears through the same journaled frame channel.
841
+ var reopen_buffer: [support.platform.max_windows]support.platform.Event = undefined;
842
+ const reopen_events = fixture.harness.null_platform.userReopenApp(&reopen_buffer);
843
+ try std.testing.expectEqual(@as(usize, 1), reopen_events.len);
844
+ try std.testing.expect(!reopen_events[0].window_frame_changed.hidden);
845
+ for (reopen_events) |event| try fixture.harness.runtime.dispatchPlatformEvent(fixture.app, event);
846
+ for (fixture.harness.runtime.listWindows(&buffer)) |window| {
847
+ if (window.id != info.id) continue;
848
+ try std.testing.expect(window.open);
849
+ try std.testing.expect(!window.hidden);
850
+ }
851
+
852
+ // A reopen with nothing hidden is honestly empty — the host's
853
+ // default reopen behavior stays in charge.
854
+ try std.testing.expectEqual(@as(usize, 0), fixture.harness.null_platform.userReopenApp(&reopen_buffer).len);
855
+ }
856
+
857
+ test "an app-driven close of a policy-hidden window clears hidden with open, and rolls it back on platform failure" {
858
+ const fixture = try Fixture.create();
859
+ defer fixture.destroy();
860
+
861
+ // A menu-bar-shaped window through the REAL create seam, hidden by
862
+ // its .hide close policy (the user clicked the red button).
863
+ const info = try fixture.harness.runtime.createSourcelessShellWindow(.{
864
+ .label = "player",
865
+ .title = "Player",
866
+ .width = 400,
867
+ .height = 300,
868
+ .close_policy = .hide,
869
+ });
870
+ const hide_event = fixture.harness.null_platform.userCloseWindow(info.id).?;
871
+ try fixture.harness.runtime.dispatchPlatformEvent(fixture.app, hide_event);
872
+ var buffer: [support.platform.max_windows]support.platform.WindowInfo = undefined;
873
+ for (fixture.harness.runtime.listWindows(&buffer)) |window| {
874
+ if (window.id != info.id) continue;
875
+ try std.testing.expect(window.open);
876
+ try std.testing.expect(window.hidden);
877
+ }
878
+
879
+ // The platform refuses the close: EVERY optimistically flipped flag
880
+ // rolls back — the window is still open, still policy-hidden.
881
+ fixture.harness.null_platform.fail_next_close_window = true;
882
+ try std.testing.expectError(error.CloseFailed, fixture.harness.runtime.closeWindow(info.id));
883
+ for (fixture.harness.runtime.listWindows(&buffer)) |window| {
884
+ if (window.id != info.id) continue;
885
+ try std.testing.expect(window.open);
886
+ try std.testing.expect(window.hidden);
887
+ }
888
+
889
+ // The app closes its hidden window for real: a closed window is not
890
+ // "hidden", it is gone — the runtime table (the JS bridge's window
891
+ // list) must read {open=false, hidden=false}, never a closed window
892
+ // still flying the hidden flag.
893
+ try fixture.harness.runtime.closeWindow(info.id);
894
+ var found = false;
895
+ for (fixture.harness.runtime.listWindows(&buffer)) |window| {
896
+ if (window.id != info.id) continue;
897
+ found = true;
898
+ try std.testing.expect(!window.open);
899
+ try std.testing.expect(!window.hidden);
900
+ try std.testing.expect(!window.focused);
901
+ }
902
+ try std.testing.expect(found);
903
+ }
904
+
905
+ test "showWindow activates: the shown window takes key in the runtime table, and a refused show moves nothing" {
906
+ const fixture = try Fixture.create();
907
+ defer fixture.destroy();
908
+
909
+ // A policy-hidden window (the user's close under .hide).
910
+ const info = try fixture.harness.runtime.createSourcelessShellWindow(.{
911
+ .label = "player",
912
+ .title = "Player",
913
+ .width = 400,
914
+ .height = 300,
915
+ .close_policy = .hide,
916
+ });
917
+ const hide_event = fixture.harness.null_platform.userCloseWindow(info.id).?;
918
+ try fixture.harness.runtime.dispatchPlatformEvent(fixture.app, hide_event);
919
+
920
+ // The show verb's contract is show AND activate — every host's show
921
+ // path makes the window key. After a successful platform show, the
922
+ // runtime table (what listWindows and the JS bridge serve) must
923
+ // read the shown window focused and every other window unfocused,
924
+ // not a frontmost window still reported keyless.
925
+ try fixture.harness.runtime.showWindow(info.id);
926
+ var buffer: [support.platform.max_windows]support.platform.WindowInfo = undefined;
927
+ var found = false;
928
+ for (fixture.harness.runtime.listWindows(&buffer)) |window| {
929
+ if (window.id == info.id) {
930
+ found = true;
931
+ try std.testing.expect(!window.hidden);
932
+ try std.testing.expect(window.focused);
933
+ } else {
934
+ try std.testing.expect(!window.focused);
935
+ }
936
+ }
937
+ try std.testing.expect(found);
938
+
939
+ // Hide again, then refuse the show at the platform: hidden rolls
940
+ // back and focus does not move — a refused show must never report
941
+ // a focused window that never came back to the glass.
942
+ const rehide_event = fixture.harness.null_platform.userCloseWindow(info.id).?;
943
+ try fixture.harness.runtime.dispatchPlatformEvent(fixture.app, rehide_event);
944
+ fixture.harness.null_platform.fail_next_show_window = true;
945
+ try std.testing.expectError(error.ShowFailed, fixture.harness.runtime.showWindow(info.id));
946
+ for (fixture.harness.runtime.listWindows(&buffer)) |window| {
947
+ if (window.id != info.id) continue;
948
+ try std.testing.expect(window.hidden);
949
+ try std.testing.expect(!window.focused);
950
+ }
951
+ }
952
+
953
+ test "the window verbs refuse a retained closed slot: show, focus, and minimize answer WindowNotFound with no platform call" {
954
+ const fixture = try Fixture.create();
955
+ defer fixture.destroy();
956
+
957
+ // A closed window keeps its table slot (the id and label release
958
+ // lazily, at the next create), so resolving the id is not liveness.
959
+ // Without the open gate the null platform accepts the verbs and
960
+ // reports {open:false, focused:true}, and the CEF host — which
961
+ // retains browser-bearing windows past their close — would visibly
962
+ // re-order the closed window onto the glass. The runtime gate holds
963
+ // independent of host timing.
964
+ const info = try fixture.harness.runtime.createSourcelessShellWindow(.{
965
+ .label = "player",
966
+ .title = "Player",
967
+ .width = 400,
968
+ .height = 300,
969
+ .close_policy = .hide,
970
+ });
971
+ try fixture.harness.runtime.closeWindow(info.id);
972
+
973
+ // show: the dead-slot answer, and the platform recorder never moves.
974
+ try std.testing.expectError(error.WindowNotFound, fixture.harness.runtime.showWindow(info.id));
975
+ try std.testing.expectEqual(@as(u32, 0), fixture.harness.null_platform.showCountForWindow(info.id));
976
+
977
+ // focus resolves by id too, and close cleared `hidden` with `open`,
978
+ // so without its own gate it skips the hidden-routing and reaches
979
+ // the platform's focus verb directly.
980
+ try std.testing.expectError(error.WindowNotFound, fixture.harness.runtime.focusWindow(info.id));
981
+ try std.testing.expectEqual(@as(u32, 0), fixture.harness.null_platform.showCountForWindow(info.id));
982
+
983
+ // The minimize twin (the CEF host would genie the retained closed
984
+ // window into the Dock).
985
+ try std.testing.expectError(error.WindowNotFound, fixture.harness.runtime.minimizeWindow(info.id));
986
+ try std.testing.expectEqual(@as(u32, 0), fixture.harness.null_platform.minimizeCountForWindow(info.id));
987
+
988
+ // Nothing resurrected: the runtime table and the platform mirror
989
+ // both still read the window closed, unfocused, un-hidden.
990
+ var buffer: [support.platform.max_windows]support.platform.WindowInfo = undefined;
991
+ var found = false;
992
+ for (fixture.harness.runtime.listWindows(&buffer)) |window| {
993
+ if (window.id != info.id) continue;
994
+ found = true;
995
+ try std.testing.expect(!window.open);
996
+ try std.testing.expect(!window.focused);
997
+ try std.testing.expect(!window.hidden);
998
+ }
999
+ try std.testing.expect(found);
1000
+ for (fixture.harness.null_platform.windows[0..fixture.harness.null_platform.window_count]) |window| {
1001
+ if (window.id != info.id) continue;
1002
+ try std.testing.expect(!window.open);
1003
+ try std.testing.expect(!window.focused);
1004
+ }
1005
+ }
1006
+
1007
+ test "the modeled host mirrors the app close of a hidden window, and the reopen never resurrects a closed one" {
1008
+ const fixture = try Fixture.create();
1009
+ defer fixture.destroy();
1010
+
1011
+ // A menu-bar-shaped window, policy-hidden by the user's close.
1012
+ const info = try fixture.harness.runtime.createSourcelessShellWindow(.{
1013
+ .label = "player",
1014
+ .title = "Player",
1015
+ .width = 400,
1016
+ .height = 300,
1017
+ .close_policy = .hide,
1018
+ });
1019
+ const hide_event = fixture.harness.null_platform.userCloseWindow(info.id).?;
1020
+ try fixture.harness.runtime.dispatchPlatformEvent(fixture.app, hide_event);
1021
+
1022
+ // The app closes the hidden window through the runtime. The modeled
1023
+ // host's mirror must read closed AND un-hidden — the same
1024
+ // hidden-clears-with-open rule the real hosts' close paths carry
1025
+ // (set cleanup before the open=false emit) and the runtime table
1026
+ // already pins for its own flags.
1027
+ try fixture.harness.runtime.closeWindow(info.id);
1028
+ for (fixture.harness.null_platform.windows[0..fixture.harness.null_platform.window_count]) |window| {
1029
+ if (window.id != info.id) continue;
1030
+ try std.testing.expect(!window.open);
1031
+ try std.testing.expect(!window.hidden);
1032
+ try std.testing.expect(!window.focused);
1033
+ }
1034
+
1035
+ // The Dock reopen after the close: NO frame event for the closed
1036
+ // window — nothing hidden-and-open remains to re-show.
1037
+ var reopen_buffer: [support.platform.max_windows]support.platform.Event = undefined;
1038
+ try std.testing.expectEqual(@as(usize, 0), fixture.harness.null_platform.userReopenApp(&reopen_buffer).len);
1039
+
1040
+ // The reopen's liveness check must hold on its own, not lean on the
1041
+ // close mirror's hidden clear: model a host whose close path forgot
1042
+ // that cleanup by forcing the stale flag onto the closed slot — the
1043
+ // reopen still refuses to resurrect (hidden alone is not liveness).
1044
+ for (fixture.harness.null_platform.windows[0..fixture.harness.null_platform.window_count]) |*window| {
1045
+ if (window.id == info.id) window.hidden = true;
1046
+ }
1047
+ try std.testing.expectEqual(@as(usize, 0), fixture.harness.null_platform.userReopenApp(&reopen_buffer).len);
1048
+
1049
+ // No events dispatched, so the runtime table is untouched: the
1050
+ // window stays closed, un-hidden, never re-focused.
1051
+ var buffer: [support.platform.max_windows]support.platform.WindowInfo = undefined;
1052
+ var found = false;
1053
+ for (fixture.harness.runtime.listWindows(&buffer)) |window| {
1054
+ if (window.id != info.id) continue;
1055
+ found = true;
1056
+ try std.testing.expect(!window.open);
1057
+ try std.testing.expect(!window.hidden);
1058
+ try std.testing.expect(!window.focused);
1059
+ }
1060
+ try std.testing.expect(found);
1061
+ }
1062
+
1063
+ test "focus-while-hidden routes through the show verb: hidden clears before the window takes key" {
1064
+ const fixture = try Fixture.create();
1065
+ defer fixture.destroy();
1066
+
1067
+ const info = try fixture.harness.runtime.createSourcelessShellWindow(.{
1068
+ .label = "player",
1069
+ .title = "Player",
1070
+ .width = 400,
1071
+ .height = 300,
1072
+ .close_policy = .hide,
1073
+ });
1074
+
1075
+ // The user close hides the window (policy .hide) and the runtime
1076
+ // adopts the hidden state off the journaled frame channel.
1077
+ const hide_event = fixture.harness.null_platform.userCloseWindow(info.id).?;
1078
+ try fixture.harness.runtime.dispatchPlatformEvent(fixture.app, hide_event);
1079
+ var buffer: [support.platform.max_windows]support.platform.WindowInfo = undefined;
1080
+ for (fixture.harness.runtime.listWindows(&buffer)) |window| {
1081
+ if (window.id == info.id) try std.testing.expect(window.hidden);
1082
+ }
1083
+
1084
+ // Focusing the hidden window must drive the platform's SHOW verb
1085
+ // (the path that clears the hosts' policy-hidden bookkeeping and
1086
+ // emits consistent state), not just its focus verb: the hidden
1087
+ // flag clears, focus lands, and the show count pins the routing —
1088
+ // a focusWindow that skips the show seam leaves this count at 0
1089
+ // and the window reported hidden while standing on the glass.
1090
+ try fixture.harness.runtime.focusWindow(info.id);
1091
+ try std.testing.expectEqual(@as(u32, 1), fixture.harness.null_platform.showCountForWindow(info.id));
1092
+ for (fixture.harness.runtime.listWindows(&buffer)) |window| {
1093
+ if (window.id != info.id) continue;
1094
+ try std.testing.expect(window.open);
1095
+ try std.testing.expect(!window.hidden);
1096
+ try std.testing.expect(window.focused);
1097
+ }
1098
+ // The platform mirror agrees on both flags.
1099
+ for (fixture.harness.null_platform.windows[0..fixture.harness.null_platform.window_count]) |window| {
1100
+ if (window.id != info.id) continue;
1101
+ try std.testing.expect(!window.hidden);
1102
+ try std.testing.expect(window.focused);
1103
+ }
1104
+
1105
+ // A visible window's focus stays a plain focus: no second show.
1106
+ try fixture.harness.runtime.focusWindow(info.id);
1107
+ try std.testing.expectEqual(@as(u32, 1), fixture.harness.null_platform.showCountForWindow(info.id));
1108
+ }
1109
+
1110
+ test "close_policy default stays .quit: the user close still tears the window down" {
1111
+ const fixture = try Fixture.create();
1112
+ defer fixture.destroy();
1113
+
1114
+ try fixture.clickSettingsButton();
1115
+ const info = fixture.settingsWindowInfo() orelse return error.TestUnexpectedResult;
1116
+ // No declaration means .quit: the capture proves the default rode
1117
+ // the create seam unchanged.
1118
+ try std.testing.expectEqual(support.platform.WindowClosePolicy.quit, fixture.harness.null_platform.closePolicyForWindow(info.id).?);
1119
+ const close_event = fixture.harness.null_platform.userCloseWindow(info.id).?;
1120
+ try std.testing.expect(!close_event.window_frame_changed.open);
1121
+ try std.testing.expect(!close_event.window_frame_changed.hidden);
1122
+ }
1123
+
1124
+ test "close_policy .hide is refused loudly at create on hosts without the affordance" {
1125
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
1126
+ defer harness.destroy(std.testing.allocator);
1127
+ // Model a GTK-shaped host: no status item, no way to bring a
1128
+ // hidden window back — the feature reports false and window create
1129
+ // refuses the declaration instead of stranding the window.
1130
+ harness.null_platform.window_hide_on_close = false;
1131
+ try std.testing.expectError(error.UnsupportedWindowClosePolicy, harness.runtime.createSourcelessShellWindow(.{
1132
+ .label = "tools",
1133
+ .title = "Tools",
1134
+ .width = 320,
1135
+ .height = 240,
1136
+ .close_policy = .hide,
1137
+ }));
1138
+ // The same shape under .quit (the default) creates fine.
1139
+ const info = try harness.runtime.createSourcelessShellWindow(.{
1140
+ .label = "tools",
1141
+ .title = "Tools",
1142
+ .width = 320,
1143
+ .height = 240,
1144
+ });
1145
+ try std.testing.expect(info.open);
1146
+ }
1147
+
1148
+ test "close_policy .hide on a secondary startup window is refused loudly at startup load on hosts without the affordance" {
1149
+ const SourceApp = struct {
1150
+ fn app(self: *@This()) support.App {
1151
+ return .{ .context = self, .name = "startup-hide", .source = support.platform.WebViewSource.html("<p>Startup</p>") };
1152
+ }
1153
+ };
1154
+ const startup_windows = [_]support.platform.WindowOptions{
1155
+ .{ .id = 1, .label = "main", .title = "Main" },
1156
+ .{ .id = 2, .label = "panel", .title = "Panel", .close_policy = .hide },
1157
+ };
1158
+
1159
+ // Model a GTK-shaped host: the feature reports false, and the
1160
+ // startup-window load — which creates secondary windows through
1161
+ // the platform services directly, not the runtime's createWindow —
1162
+ // must refuse the secondary window's `.hide` declaration with the
1163
+ // same loud error as the create seam. A silent accept here means
1164
+ // the host ignores the policy and the user's close really closes.
1165
+ {
1166
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
1167
+ defer harness.destroy(std.testing.allocator);
1168
+ harness.null_platform.window_hide_on_close = false;
1169
+ harness.runtime.options.platform.app_info.windows = &startup_windows;
1170
+ var app_state: SourceApp = .{};
1171
+ try std.testing.expectError(error.UnsupportedWindowClosePolicy, harness.start(app_state.app()));
1172
+ }
1173
+
1174
+ // A host WITH the affordance loads the same declaration: both
1175
+ // startup windows come up.
1176
+ {
1177
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
1178
+ defer harness.destroy(std.testing.allocator);
1179
+ harness.runtime.options.platform.app_info.windows = &startup_windows;
1180
+ var app_state: SourceApp = .{};
1181
+ try harness.start(app_state.app());
1182
+ var buffer: [support.platform.max_windows]support.platform.WindowInfo = undefined;
1183
+ try std.testing.expectEqual(@as(usize, 2), harness.runtime.listWindows(&buffer).len);
1184
+ }
717
1185
  }