@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
@@ -153,6 +153,15 @@ pub const PlatformFeature = enum {
153
153
  /// keeps emitting; the null platform models the rule through its
154
154
  /// windows' modeled occlusion so the suites can pin it.
155
155
  audio_spectrum,
156
+ /// The `close_policy = .hide` window shape: the host can intercept
157
+ /// the user's close affordance, keep the window alive off the
158
+ /// glass, and re-show it later (`show_window_fn`, tray actions, the
159
+ /// macOS Dock reopen). macOS and Windows implement it; GTK reports
160
+ /// false — Linux has no status-item affordance in this toolkit, so
161
+ /// a hidden window would be unreachable, and window create refuses
162
+ /// the policy with a teaching error instead. The null platform
163
+ /// models full support.
164
+ window_hide_on_close,
156
165
  };
157
166
 
158
167
  pub const WebViewSourceKind = enum {
@@ -522,6 +531,24 @@ pub const WindowShowMode = enum {
522
531
  on_first_present,
523
532
  };
524
533
 
534
+ /// What the user's close affordance (the red traffic light, cmd+W, the
535
+ /// caption X) does to this window.
536
+ /// `.quit` is the default and today's behavior unchanged: the window
537
+ /// really closes, and the last close follows the host's exit semantics.
538
+ /// `.hide` is the menu-bar/tray-app shape: the close affordance hides
539
+ /// the window instead — it stays alive in the host's records (no
540
+ /// shutdown), `WindowState.hidden` flips true on the frame channel,
541
+ /// and `Effects.showWindow` / the macOS Dock reopen bring it back.
542
+ /// Hosts that cannot re-show a hidden window (GTK has no status item
543
+ /// to click) refuse `.hide` at create time with a teaching error —
544
+ /// never a silent no-op that strands the window.
545
+ /// Deliberate enum room for a future `.event` tier (model-decides
546
+ /// close, for unsaved-changes flows) — not implemented yet.
547
+ pub const WindowClosePolicy = enum {
548
+ quit,
549
+ hide,
550
+ };
551
+
525
552
  pub const WindowOptions = struct {
526
553
  id: WindowId = 1,
527
554
  label: []const u8 = "main",
@@ -539,6 +566,9 @@ pub const WindowOptions = struct {
539
566
  /// does not grow an already-smaller restored frame.
540
567
  min_width: f32 = 0,
541
568
  min_height: f32 = 0,
569
+ /// What the user's close affordance does — see `WindowClosePolicy`.
570
+ /// Fixed at create like the titlebar: it is host window state.
571
+ close_policy: WindowClosePolicy = .quit,
542
572
 
543
573
  pub fn resolvedTitle(self: WindowOptions, app_name: []const u8) []const u8 {
544
574
  return if (self.title.len > 0) self.title else app_name;
@@ -555,6 +585,16 @@ pub const WindowState = struct {
555
585
  focused: bool = true,
556
586
  maximized: bool = false,
557
587
  fullscreen: bool = false,
588
+ /// True while the window is alive but off the glass because its
589
+ /// `close_policy = .hide` intercepted a user close (the menu-bar-app
590
+ /// shape): the window keeps its views and native identity, `open`
591
+ /// stays true, and a show — `Effects.showWindow`, a tray action's
592
+ /// consequence, the macOS Dock reopen — flips it back. Distinct from
593
+ /// minimized (which keeps a Dock/taskbar affordance) and from
594
+ /// closed (`open = false`, the window is gone). Session-transient:
595
+ /// never persisted to the window-state store, so every launch
596
+ /// starts shown.
597
+ hidden: bool = false,
558
598
  };
559
599
 
560
600
  pub const WindowInfo = struct {
@@ -565,6 +605,8 @@ pub const WindowInfo = struct {
565
605
  scale_factor: f32 = 1,
566
606
  open: bool = true,
567
607
  focused: bool = false,
608
+ /// Alive but policy-hidden — see `WindowState.hidden`.
609
+ hidden: bool = false,
568
610
 
569
611
  pub fn state(self: WindowInfo) WindowState {
570
612
  return .{
@@ -575,6 +617,7 @@ pub const WindowInfo = struct {
575
617
  .scale_factor = self.scale_factor,
576
618
  .open = self.open,
577
619
  .focused = self.focused,
620
+ .hidden = self.hidden,
578
621
  };
579
622
  }
580
623
  };
@@ -593,6 +636,8 @@ pub const WindowCreateOptions = struct {
593
636
  /// `WindowOptions.min_width`/`min_height`); 0 = no floor.
594
637
  min_width: f32 = 0,
595
638
  min_height: f32 = 0,
639
+ /// See `WindowOptions.close_policy`.
640
+ close_policy: WindowClosePolicy = .quit,
596
641
  source: ?WebViewSource = null,
597
642
 
598
643
  pub fn windowOptions(self: WindowCreateOptions, id: WindowId, label: []const u8) WindowOptions {
@@ -608,6 +653,7 @@ pub const WindowCreateOptions = struct {
608
653
  .show = self.show,
609
654
  .min_width = self.min_width,
610
655
  .min_height = self.min_height,
656
+ .close_policy = self.close_policy,
611
657
  };
612
658
  }
613
659
  };
@@ -1094,6 +1140,14 @@ pub const AppInfo = struct {
1094
1140
  /// default menus: web items like Reload only exist when a webview
1095
1141
  /// can answer them.
1096
1142
  has_web_content: bool = false,
1143
+ /// Whether the manifest declares the "tray" capability (a status
1144
+ /// item). Close policy `.hide` leans on it where the OS has no
1145
+ /// built-in re-show affordance: hiding a window on windows removes
1146
+ /// its taskbar entry and windows has no dock-reopen path, so only a
1147
+ /// tray icon can bring the hidden window back. The Windows host
1148
+ /// folds this into its `window_hide_on_close` answer; macOS ignores
1149
+ /// it (the Dock reopen path always exists).
1150
+ declares_tray: bool = false,
1097
1151
  window_title: []const u8 = "",
1098
1152
  bundle_id: []const u8 = "dev.native_sdk.app",
1099
1153
  icon_path: []const u8 = "",
@@ -1783,8 +1837,11 @@ pub const GpuSurfaceImagePixels = struct {
1783
1837
  /// keyed by the canvas font id host-wide. The engine validates the bytes
1784
1838
  /// (a parseable TrueType face under the registry bounds) before this
1785
1839
  /// call, so hosts may treat a decode failure as a hard error rather than
1786
- /// a fallback. Ids are permanent for the process — the engine never
1787
- /// re-registers an id with different bytes.
1840
+ /// a fallback. Ids are permanent per RUNTIME (one runtime never
1841
+ /// re-registers an id with different bytes), but host font state is
1842
+ /// per-process and runtimes are not: a later runtime may re-register an
1843
+ /// id a still-live earlier runtime also holds, and the host resolves the
1844
+ /// most recent registration (last wins).
1788
1845
  pub const GpuSurfaceFontData = struct {
1789
1846
  id: u64,
1790
1847
  ttf: []const u8,
@@ -2047,6 +2104,30 @@ pub const PlatformServices = struct {
2047
2104
  /// controls — chromeless windows have no system button to click.
2048
2105
  /// Platforms without the concept leave this null.
2049
2106
  minimize_window_fn: ?*const fn (context: ?*anyopaque, window_id: WindowId) anyerror!void = null,
2107
+ /// The real OS show verb: unhide + activate (macOS deminiaturize +
2108
+ /// makeKeyAndOrderFront + activate, Windows SW_RESTORE/SW_SHOW +
2109
+ /// foreground, GTK `gtk_window_present`) — the counterpart to a
2110
+ /// `close_policy = .hide` hide, and the tray "Open" consequence.
2111
+ /// Platforms without the concept leave this null.
2112
+ show_window_fn: ?*const fn (context: ?*anyopaque, window_id: WindowId) anyerror!void = null,
2113
+ /// The graceful app quit: terminate through the SAME shutdown path
2114
+ /// a last-window close takes — but the host QUEUES the emit, so
2115
+ /// `app_shutdown` dispatches on its NEXT loop turn, only after the
2116
+ /// dispatch that requested the quit has returned. Never emit
2117
+ /// synchronously from inside this call: the request arrives mid
2118
+ /// dispatch (the command whose update returned it is still on the
2119
+ /// recorder's staged-event stack), and a nested emit seals the
2120
+ /// session journal before the requesting command commits — the
2121
+ /// record is lost and replay diverges. Before the run loop starts
2122
+ /// (a quit from App.start's update, or from a boot command in the
2123
+ /// synchronous first canvas frame) the host parks the request and
2124
+ /// drains it at top level after the current callback returns.
2125
+ /// Either way `app.stop` runs exactly once, and the shutdown is
2126
+ /// journaled like any platform event. The null platform's
2127
+ /// `quit_pending`/`takeQueuedQuit` seam is the reference model of
2128
+ /// the queued shape. Platforms without the concept leave this
2129
+ /// null.
2130
+ quit_app_fn: ?*const fn (context: ?*anyopaque) anyerror!void = null,
2050
2131
  /// Hand the ACTIVE pointer-down to the platform as a window-drag
2051
2132
  /// gesture (the hidden-titlebar drag-region channel): the window
2052
2133
  /// moves once the pointer actually moves — a plain click moves
@@ -2204,7 +2285,34 @@ pub const PlatformServices = struct {
2204
2285
  /// without host-side text (GTK/Win32/null default), where the engine
2205
2286
  /// measures with the parsed face and inks it through the reference
2206
2287
  /// renderer.
2207
- register_gpu_surface_font_fn: ?*const fn (context: ?*anyopaque, font: GpuSurfaceFontData) anyerror!void = null,
2288
+ ///
2289
+ /// Returns the host's ownership token for THIS registration of the
2290
+ /// id. Host font state is per-process while ids are only permanent
2291
+ /// per-runtime, so two live runtimes can register the same id (last
2292
+ /// wins); the token names which registration a later unregister may
2293
+ /// remove — the runtime stores it beside the captured unregister
2294
+ /// owner and passes it back at teardown. Platforms that retain no
2295
+ /// per-id host state (a stateless accept) return 0: there was
2296
+ /// nothing installed, so there is nothing a token could own, and an
2297
+ /// unregister carrying 0 removes nothing.
2298
+ register_gpu_surface_font_fn: ?*const fn (context: ?*anyopaque, font: GpuSurfaceFontData) anyerror!u64 = null,
2299
+ /// Teardown twin of the registration seam above: return whatever
2300
+ /// per-id state the host retained for a registered face (descriptor
2301
+ /// tables, size/measurement caches) when the runtime that registered
2302
+ /// the id deinits. Fonts are per-runtime but host font state is
2303
+ /// per-process, so without this seam an embedder cycling runtimes
2304
+ /// with fresh ids grows host state for the process lifetime.
2305
+ /// `token` is the ownership token the matching register call
2306
+ /// returned: the host removes the id's state ONLY while the id's
2307
+ /// current registration still carries that token, so an older
2308
+ /// runtime's deinit can never tear down a newer runtime's live face
2309
+ /// under a shared id. A stale token (the id was re-registered since)
2310
+ /// is a no-op accept — the registration it owned is already gone,
2311
+ /// which is exactly the state its owner asked for. Unregistering an
2312
+ /// id the host never saw is likewise a no-op, not an error. Null on
2313
+ /// platforms whose register seam is itself null (GTK/Win32 default)
2314
+ /// — with no host font state there is nothing to return.
2315
+ unregister_gpu_surface_font_fn: ?*const fn (context: ?*anyopaque, id: u64, token: u64) anyerror!void = null,
2208
2316
  update_widget_accessibility_fn: ?*const fn (context: ?*anyopaque, snapshot: WidgetAccessibilitySnapshot) anyerror!void = null,
2209
2317
  /// Reconcile the native scroll drivers for a gpu-surface view against
2210
2318
  /// the full desired set: create missing drivers, update frames /
@@ -2326,6 +2434,16 @@ pub const PlatformServices = struct {
2326
2434
  return minimize_fn(self.context, window_id);
2327
2435
  }
2328
2436
 
2437
+ pub fn showWindow(self: PlatformServices, window_id: WindowId) anyerror!void {
2438
+ const show_fn = self.show_window_fn orelse return error.UnsupportedService;
2439
+ return show_fn(self.context, window_id);
2440
+ }
2441
+
2442
+ pub fn quitApp(self: PlatformServices) anyerror!void {
2443
+ const quit_fn = self.quit_app_fn orelse return error.UnsupportedService;
2444
+ return quit_fn(self.context);
2445
+ }
2446
+
2329
2447
  pub fn startWindowDrag(self: PlatformServices, window_id: WindowId) anyerror!void {
2330
2448
  const drag_fn = self.start_window_drag_fn orelse return error.UnsupportedService;
2331
2449
  return drag_fn(self.context, window_id);
@@ -2695,13 +2813,25 @@ pub const PlatformServices = struct {
2695
2813
  return remove_fn(self.context, id);
2696
2814
  }
2697
2815
 
2698
- pub fn registerGpuSurfaceFont(self: PlatformServices, font: GpuSurfaceFontData) anyerror!void {
2816
+ /// Returns the host's ownership token for this registration (0 from
2817
+ /// hosts that retain no per-id state) — see
2818
+ /// `register_gpu_surface_font_fn`.
2819
+ pub fn registerGpuSurfaceFont(self: PlatformServices, font: GpuSurfaceFontData) anyerror!u64 {
2699
2820
  if (font.id == 0) return error.InvalidGpuSurfaceFont;
2700
2821
  if (font.ttf.len == 0 or font.ttf.len > max_gpu_surface_font_bytes) return error.InvalidGpuSurfaceFont;
2701
2822
  const register_fn = self.register_gpu_surface_font_fn orelse return error.UnsupportedService;
2702
2823
  return register_fn(self.context, font);
2703
2824
  }
2704
2825
 
2826
+ /// `token` must be the value the matching `registerGpuSurfaceFont`
2827
+ /// returned; a stale token is a no-op accept — see
2828
+ /// `unregister_gpu_surface_font_fn`.
2829
+ pub fn unregisterGpuSurfaceFont(self: PlatformServices, id: u64, token: u64) anyerror!void {
2830
+ if (id == 0) return error.InvalidGpuSurfaceFont;
2831
+ const unregister_fn = self.unregister_gpu_surface_font_fn orelse return error.UnsupportedService;
2832
+ return unregister_fn(self.context, id, token);
2833
+ }
2834
+
2705
2835
  pub fn updateWidgetAccessibility(self: PlatformServices, snapshot: WidgetAccessibilitySnapshot) anyerror!void {
2706
2836
  if (snapshot.view_label.len == 0 or snapshot.view_label.len > max_view_label_bytes) return error.InvalidViewOptions;
2707
2837
  if (snapshot.nodes.len > max_widget_accessibility_nodes) return error.InvalidViewOptions;
@@ -2780,6 +2910,10 @@ fn defaultSupportsFeature(services: PlatformServices, feature: PlatformFeature)
2780
2910
  // cannot see it; platforms that analyze answer through their own
2781
2911
  // `supports_fn` (like file_drops and gpu_surfaces above).
2782
2912
  .audio_spectrum => false,
2913
+ // Hide-on-close is host close-delegate behavior, not a service
2914
+ // verb: hosts that implement it answer through their own
2915
+ // `supports_fn`. The generic floor is honest refusal.
2916
+ .window_hide_on_close => false,
2783
2917
  };
2784
2918
  }
2785
2919
 
@@ -9,6 +9,7 @@ pub const Error = error{
9
9
  CreateFailed,
10
10
  FocusFailed,
11
11
  CloseFailed,
12
+ UnsupportedWindowClosePolicy,
12
13
  };
13
14
 
14
15
  const WindowsHost = opaque {};
@@ -45,6 +46,9 @@ const WindowsEvent = extern struct {
45
46
  y: f64,
46
47
  open: c_int,
47
48
  focused: c_int,
49
+ /// Nonzero while the window is alive but hidden by its close_policy
50
+ /// (`open` stays 1 for the whole hidden stretch).
51
+ hidden: c_int,
48
52
  label: [*]const u8,
49
53
  label_len: usize,
50
54
  title: [*]const u8,
@@ -134,6 +138,8 @@ extern fn native_sdk_windows_cancel_timer(host: *WindowsHost, timer_id: u64) voi
134
138
  extern fn native_sdk_windows_focus_window(host: *WindowsHost, window_id: u64) c_int;
135
139
  extern fn native_sdk_windows_close_window(host: *WindowsHost, window_id: u64) c_int;
136
140
  extern fn native_sdk_windows_minimize_window(host: *WindowsHost, window_id: u64) c_int;
141
+ extern fn native_sdk_windows_show_window(host: *WindowsHost, window_id: u64) c_int;
142
+ extern fn native_sdk_windows_set_window_close_policy(host: *WindowsHost, window_id: u64, close_policy: c_int) c_int;
137
143
  extern fn native_sdk_windows_create_view(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, kind: c_int, parent: [*]const u8, parent_len: usize, x: f64, y: f64, width: f64, height: f64, layer: c_int, visible: c_int, enabled: c_int, role: [*]const u8, role_len: usize, accessibility_label: [*]const u8, accessibility_label_len: usize, text: [*]const u8, text_len: usize, command: [*]const u8, command_len: usize) c_int;
138
144
  extern fn native_sdk_windows_update_view(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, has_frame: c_int, x: f64, y: f64, width: f64, height: f64, has_layer: c_int, layer: c_int, has_visible: c_int, visible: c_int, has_enabled: c_int, enabled: c_int, has_role: c_int, role: [*]const u8, role_len: usize, has_accessibility_label: c_int, accessibility_label: [*]const u8, accessibility_label_len: usize, has_text: c_int, text: [*]const u8, text_len: usize, has_command: c_int, command: [*]const u8, command_len: usize) c_int;
139
145
  extern fn native_sdk_windows_set_view_frame(host: *WindowsHost, window_id: u64, label: [*]const u8, label_len: usize, x: f64, y: f64, width: f64, height: f64) c_int;
@@ -219,6 +225,23 @@ const WindowsMessageDialogOpts = extern struct {
219
225
  tertiary_button_len: usize,
220
226
  };
221
227
 
228
+ /// The startup-window twin of the runtime's create-time `.hide` gate:
229
+ /// on windows, hide-on-close is honest only when the app declares a
230
+ /// tray (status item) — SW_HIDE removes the taskbar entry and windows
231
+ /// has no dock-reopen path, so without a tray a hidden window is a
232
+ /// running, invisible, unreachable app. The generated runner refuses
233
+ /// the declaration at comptime, the runtime refuses secondary windows
234
+ /// at create (through the conditional `window_hide_on_close` answer
235
+ /// below), and this refuses the platform-created main window at init.
236
+ /// Pure (no Win32 externs), so the refusal is unit-testable on every
237
+ /// host.
238
+ fn refuseUnsupportedMainWindowClosePolicy(app_info: platform_mod.AppInfo) Error!void {
239
+ if (app_info.resolvedMainWindow().close_policy != .hide) return;
240
+ if (app_info.declares_tray) return;
241
+ std.debug.print("window close_policy \"hide\" on windows requires the \"tray\" capability: hiding removes the taskbar entry and windows has no dock-reopen path, so only a status item (tray) could bring the hidden window back - add \"tray\" to .capabilities and install a status item, or declare \"quit\" (the default)\n", .{});
242
+ return error.UnsupportedWindowClosePolicy;
243
+ }
244
+
222
245
  pub const WindowsPlatform = struct {
223
246
  host: *WindowsHost,
224
247
  web_engine: platform_mod.WebEngine,
@@ -236,9 +259,21 @@ pub const WindowsPlatform = struct {
236
259
 
237
260
  pub fn initWithOptions(size: geometry.SizeF, web_engine: platform_mod.WebEngine, app_info: platform_mod.AppInfo) Error!WindowsPlatform {
238
261
  const window_options = app_info.resolvedMainWindow();
262
+ // The MAIN window is created right here, before any runtime
263
+ // exists, so the runtime's create-time `.hide` gate (the
264
+ // window_hide_on_close feature check) never sees it — the same
265
+ // seam the GTK host holds for its unconditional refusal. On
266
+ // windows the refusal is conditional: hiding removes the
267
+ // taskbar entry and windows has no dock-reopen path, so a
268
+ // declared tray (status item) is the ONLY re-show affordance.
269
+ try refuseUnsupportedMainWindowClosePolicy(app_info);
239
270
  const window_title = window_options.resolvedTitle(app_info.app_name);
240
271
  const frame = window_options.default_frame;
241
272
  const host = native_sdk_windows_create(app_info.app_name.ptr, app_info.app_name.len, window_title.ptr, window_title.len, app_info.bundle_id.ptr, app_info.bundle_id.len, app_info.icon_path.ptr, app_info.icon_path.len, window_options.label.ptr, window_options.label.len, frame.x, frame.y, frame.width, frame.height, if (window_options.restore_state) 1 else 0, if (window_options.resizable) 1 else 0, titlebarStyleInt(window_options.titlebar), minSizeFloor(window_options.min_width), minSizeFloor(window_options.min_height)) orelse return error.CreateFailed;
273
+ // The manifest's declared close policy rides right after the
274
+ // create, like the min-size floor: close handling is host
275
+ // window state fixed for the window's life.
276
+ applyWindowClosePolicy(host, window_options.id, window_options.close_policy);
242
277
  return .{
243
278
  .host = host,
244
279
  .web_engine = web_engine,
@@ -277,6 +312,8 @@ pub const WindowsPlatform = struct {
277
312
  .focus_window_fn = focusWindow,
278
313
  .close_window_fn = closeWindow,
279
314
  .minimize_window_fn = minimizeWindow,
315
+ .show_window_fn = showWindow,
316
+ .quit_app_fn = quitApp,
280
317
  .start_window_drag_fn = startWindowDrag,
281
318
  .set_window_drag_regions_fn = setWindowDragRegions,
282
319
  .window_chrome_fn = windowChrome,
@@ -354,6 +391,14 @@ pub const WindowsPlatform = struct {
354
391
  .audio_playback,
355
392
  .audio_streaming,
356
393
  => self.web_engine == .system,
394
+ // close_policy .hide: WM_CLOSE hides (ShowWindow SW_HIDE),
395
+ // the window stays in the host map, and the tray is the
396
+ // ONLY re-show affordance — SW_HIDE removes the taskbar
397
+ // entry and windows has no dock-reopen path. An app that
398
+ // declares no tray gets an honest false, so the runtime's
399
+ // create-time gates refuse a `.hide` declaration instead of
400
+ // stranding a hidden window nothing could re-show.
401
+ .window_hide_on_close => self.web_engine == .system and self.app_info.declares_tray,
357
402
  // Spectrum analysis captures the app's OWN audio session
358
403
  // through process-scoped WASAPI loopback, which the OS grew
359
404
  // in Windows 10 2004 — the host probes the activation
@@ -454,6 +499,7 @@ fn windowsCallback(context: ?*anyopaque, event: *const WindowsEvent) callconv(.c
454
499
  .scale_factor = @floatCast(event.scale),
455
500
  .open = event.open != 0,
456
501
  .focused = event.focused != 0,
502
+ .hidden = event.hidden != 0,
457
503
  } });
458
504
  },
459
505
  .shortcut => state.emit(.{ .shortcut = .{
@@ -666,6 +712,15 @@ fn requestFrame(context: ?*anyopaque) anyerror!void {
666
712
  native_sdk_windows_request_frame(self.host);
667
713
  }
668
714
 
715
+ /// Headless image codec for session replay: the SAME WIC decode a live
716
+ /// host serves (see `decodeImage` below — a context-free bytes-to-pixels
717
+ /// call, no window, no message loop), so journaled image bytes
718
+ /// re-register the identical pixels under a headless replay on the same
719
+ /// platform.
720
+ pub fn installHeadlessImageCodec(services: *platform_mod.PlatformServices) void {
721
+ services.decode_image_fn = decodeImage;
722
+ }
723
+
669
724
  /// WIC-backed image decoding (PNG, JPEG, ... — every codec the OS
670
725
  /// ships) into straight-alpha RGBA8.
671
726
  fn decodeImage(context: ?*anyopaque, bytes: []const u8, buffer: []u8) anyerror!platform_mod.DecodedImage {
@@ -690,6 +745,20 @@ fn titlebarStyleInt(style: platform_mod.WindowTitlebarStyle) c_int {
690
745
 
691
746
  /// Zero/negative/non-finite floors are the "no floor" sentinel (the
692
747
  /// host leaves that axis at its natural minimum).
748
+ fn closePolicyInt(policy: platform_mod.WindowClosePolicy) c_int {
749
+ return switch (policy) {
750
+ .quit => 0,
751
+ .hide => 1,
752
+ };
753
+ }
754
+
755
+ /// Register a window's declared close policy with the host right after
756
+ /// create. `.quit` skips the call — it IS the host default.
757
+ fn applyWindowClosePolicy(host: *WindowsHost, window_id: u64, policy: platform_mod.WindowClosePolicy) void {
758
+ if (policy == .quit) return;
759
+ _ = native_sdk_windows_set_window_close_policy(host, window_id, closePolicyInt(policy));
760
+ }
761
+
693
762
  fn minSizeFloor(value: f32) f64 {
694
763
  return if (std.math.isFinite(value) and value > 0) value else 0;
695
764
  }
@@ -699,6 +768,7 @@ fn createWindow(context: ?*anyopaque, options: platform_mod.WindowOptions) anyer
699
768
  const title = options.resolvedTitle(self.app_info.app_name);
700
769
  const frame = options.default_frame;
701
770
  if (native_sdk_windows_create_window(self.host, options.id, title.ptr, title.len, options.label.ptr, options.label.len, frame.x, frame.y, frame.width, frame.height, if (options.restore_state) 1 else 0, if (options.resizable) 1 else 0, titlebarStyleInt(options.titlebar), minSizeFloor(options.min_width), minSizeFloor(options.min_height)) == 0) return error.CreateFailed;
771
+ applyWindowClosePolicy(self.host, options.id, options.close_policy);
702
772
  return .{
703
773
  .id = options.id,
704
774
  .label = options.label,
@@ -725,6 +795,19 @@ fn minimizeWindow(context: ?*anyopaque, window_id: platform_mod.WindowId) anyerr
725
795
  if (native_sdk_windows_minimize_window(self.host, window_id) == 0) return error.WindowNotFound;
726
796
  }
727
797
 
798
+ fn showWindow(context: ?*anyopaque, window_id: platform_mod.WindowId) anyerror!void {
799
+ const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
800
+ if (native_sdk_windows_show_window(self.host, window_id) == 0) return error.WindowNotFound;
801
+ }
802
+
803
+ /// The graceful quit: stop the message loop the same way the last
804
+ /// window's WM_DESTROY does (PostQuitMessage); the run loop's exit
805
+ /// emits the same shutdown event.
806
+ fn quitApp(context: ?*anyopaque) anyerror!void {
807
+ const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
808
+ native_sdk_windows_stop(self.host);
809
+ }
810
+
728
811
  fn startWindowDrag(context: ?*anyopaque, window_id: platform_mod.WindowId) anyerror!void {
729
812
  const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
730
813
  if (native_sdk_windows_start_window_drag(self.host, window_id) == 0) return error.WindowNotFound;
@@ -1450,6 +1533,57 @@ test "windows chromium reports unsupported native surfaces" {
1450
1533
  try std.testing.expect(!WindowsPlatform.supportsFeature(&chromium, .audio_streaming));
1451
1534
  }
1452
1535
 
1536
+ test "windows hide-on-close support requires the declared tray (the only re-show affordance)" {
1537
+ // A system-engine host with a declared tray keeps hide-on-close.
1538
+ var with_tray = testPlatformWithEngine(.system);
1539
+ with_tray.app_info.declares_tray = true;
1540
+ try std.testing.expect(WindowsPlatform.supportsFeature(&with_tray, .window_hide_on_close));
1541
+ // Without the declaration the answer is an honest false: SW_HIDE
1542
+ // removes the taskbar entry and windows has no dock-reopen path, so
1543
+ // the runtime's create-time gates must refuse a `.hide` declaration
1544
+ // instead of stranding a hidden window nothing could re-show.
1545
+ var without_tray = testPlatformWithEngine(.system);
1546
+ try std.testing.expect(!WindowsPlatform.supportsFeature(&without_tray, .window_hide_on_close));
1547
+ // The chromium engine answers false regardless, tray or not.
1548
+ var chromium = testPlatformWithEngine(.chromium);
1549
+ chromium.app_info.declares_tray = true;
1550
+ try std.testing.expect(!WindowsPlatform.supportsFeature(&chromium, .window_hide_on_close));
1551
+ }
1552
+
1553
+ test "windows refuses a tray-less .hide main window at platform init instead of stranding it hidden" {
1554
+ // The pre-created MAIN window never passes through the runtime's
1555
+ // create gate, so the platform's init gate must hold the same line
1556
+ // (the GTK host's init refusal is the unconditional twin).
1557
+ const hide_no_tray: platform_mod.AppInfo = .{ .app_name = "player", .main_window = .{ .close_policy = .hide } };
1558
+ try std.testing.expectError(error.UnsupportedWindowClosePolicy, refuseUnsupportedMainWindowClosePolicy(hide_no_tray));
1559
+ // The declared tray is the re-show affordance: with it, the same
1560
+ // declaration passes.
1561
+ const hide_with_tray: platform_mod.AppInfo = .{ .app_name = "player", .declares_tray = true, .main_window = .{ .close_policy = .hide } };
1562
+ try refuseUnsupportedMainWindowClosePolicy(hide_with_tray);
1563
+ // The default (.quit) never consults the tray.
1564
+ const quit_app: platform_mod.AppInfo = .{ .app_name = "player" };
1565
+ try refuseUnsupportedMainWindowClosePolicy(quit_app);
1566
+ }
1567
+
1568
+ test "windows WM_CLOSE hide consults the live tray state and downgrades to a real close without it" {
1569
+ // The downgrade branch is C++ this test suite cannot execute, so
1570
+ // this pins the tray-alive consult and the loud downgrade line
1571
+ // textually: a refactor that drops either fails here. Compilation
1572
+ // is covered by the cross-target syntax checks the packaging path
1573
+ // runs; live close behavior stays windows-host territory.
1574
+ const host_source = @embedFile("webview2_host.cpp");
1575
+ const close_at = std.mem.indexOf(u8, host_source, "case WM_CLOSE:");
1576
+ try std.testing.expect(close_at != null);
1577
+ const close_hook = host_source[close_at.?..];
1578
+ const consult_at = std.mem.indexOf(u8, close_hook, "if (!host->tray_active)");
1579
+ const hide_at = std.mem.indexOf(u8, close_hook, "ShowWindow(hwnd, SW_HIDE);");
1580
+ try std.testing.expect(consult_at != null);
1581
+ try std.testing.expect(hide_at != null);
1582
+ // The consult sits INSIDE the close hook, BEFORE the hide.
1583
+ try std.testing.expect(consult_at.? < hide_at.?);
1584
+ try std.testing.expect(std.mem.indexOf(u8, close_hook, "downgraded to a real close") != null);
1585
+ }
1586
+
1453
1587
  test "windows audio event maps kinds and payload" {
1454
1588
  var event = std.mem.zeroes(WindowsEvent);
1455
1589
  event.audio_kind = 1;