@native-sdk/cli 0.5.2 → 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 (123) hide show
  1. package/build/app.zig +41 -1
  2. package/build.zig +264 -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/sdk/events.ts +39 -2
  10. package/packages/core/src/checker.ts +1 -1
  11. package/packages/core/src/emitter.ts +2625 -262
  12. package/packages/core/src/infer.ts +53 -7
  13. package/skill-data/automation/SKILL.md +7 -4
  14. package/skill-data/core/references/app-model-runtime.md +3 -0
  15. package/skill-data/native-ui/SKILL.md +4 -2
  16. package/skill-data/ts-core/SKILL.md +9 -1
  17. package/src/app_runner/root.zig +106 -1
  18. package/src/app_runner/ts_core_main.zig +4 -0
  19. package/src/automation/protocol.zig +20 -1
  20. package/src/embed/host.zig +27 -0
  21. package/src/embed/tests.zig +187 -0
  22. package/src/embed/ui_host.zig +4 -0
  23. package/src/platform/linux/gtk_host.c +112 -3
  24. package/src/platform/linux/root.zig +71 -0
  25. package/src/platform/macos/appkit_host.h +59 -2
  26. package/src/platform/macos/appkit_host.m +663 -34
  27. package/src/platform/macos/cef_host.mm +262 -10
  28. package/src/platform/macos/root.zig +126 -4
  29. package/src/platform/null_platform.zig +298 -5
  30. package/src/platform/null_platform_tests.zig +39 -0
  31. package/src/platform/root.zig +36 -0
  32. package/src/platform/types.zig +247 -8
  33. package/src/platform/windows/root.zig +134 -0
  34. package/src/platform/windows/webview2_host.cpp +152 -40
  35. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  36. package/src/primitives/app_manifest/root.zig +1 -0
  37. package/src/primitives/app_manifest/types.zig +32 -0
  38. package/src/primitives/canvas/chart.zig +7 -7
  39. package/src/primitives/canvas/chart_tests.zig +66 -0
  40. package/src/primitives/canvas/commands.zig +48 -0
  41. package/src/primitives/canvas/events.zig +161 -0
  42. package/src/primitives/canvas/font_ttf.zig +155 -8
  43. package/src/primitives/canvas/font_ttf_tests.zig +686 -0
  44. package/src/primitives/canvas/reference.zig +67 -8
  45. package/src/primitives/canvas/render_fingerprints.zig +8 -0
  46. package/src/primitives/canvas/root.zig +25 -0
  47. package/src/primitives/canvas/tests.zig +1 -0
  48. package/src/primitives/canvas/ui.zig +46 -9
  49. package/src/primitives/canvas/ui_markup.zig +82 -34
  50. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  51. package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
  52. package/src/primitives/canvas/ui_markup_compiled.zig +63 -11
  53. package/src/primitives/canvas/ui_markup_compiled_tests.zig +88 -0
  54. package/src/primitives/canvas/ui_markup_contract.zig +10 -1
  55. package/src/primitives/canvas/ui_markup_contract_tests.zig +50 -0
  56. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  57. package/src/primitives/canvas/ui_markup_tests.zig +55 -9
  58. package/src/primitives/canvas/ui_markup_view.zig +81 -14
  59. package/src/primitives/canvas/ui_markup_view_tests.zig +219 -7
  60. package/src/primitives/canvas/ui_schema.zig +29 -0
  61. package/src/primitives/canvas/ui_schema_tests.zig +11 -11
  62. package/src/primitives/canvas/ui_tests.zig +31 -0
  63. package/src/primitives/canvas/vector.zig +258 -141
  64. package/src/primitives/canvas/widget_access.zig +1 -1
  65. package/src/primitives/canvas/widget_builtin_tests.zig +129 -0
  66. package/src/primitives/canvas/widget_layout.zig +9 -4
  67. package/src/primitives/canvas/widget_render.zig +78 -40
  68. package/src/primitives/canvas/widget_render_controls.zig +15 -4
  69. package/src/primitives/canvas/widget_semantics.zig +6 -1
  70. package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
  71. package/src/primitives/canvas/widget_text_input.zig +118 -10
  72. package/src/primitives/canvas/widgets.zig +16 -0
  73. package/src/root.zig +12 -0
  74. package/src/runtime/api.zig +15 -0
  75. package/src/runtime/automation_commands.zig +33 -0
  76. package/src/runtime/automation_widget_dispatch.zig +64 -0
  77. package/src/runtime/bridge_payload.zig +7 -0
  78. package/src/runtime/bridge_responses.zig +5 -0
  79. package/src/runtime/builtin_bridge.zig +9 -4
  80. package/src/runtime/canvas_font_tests.zig +711 -5
  81. package/src/runtime/canvas_fonts.zig +120 -25
  82. package/src/runtime/canvas_frame_helpers.zig +15 -0
  83. package/src/runtime/canvas_image_tests.zig +221 -0
  84. package/src/runtime/canvas_images.zig +78 -19
  85. package/src/runtime/canvas_limits.zig +44 -8
  86. package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
  87. package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
  88. package/src/runtime/canvas_widget_events.zig +24 -4
  89. package/src/runtime/canvas_widget_runtime.zig +24 -7
  90. package/src/runtime/canvas_widget_state.zig +6 -1
  91. package/src/runtime/canvas_widget_text_tests.zig +277 -0
  92. package/src/runtime/core.zig +148 -9
  93. package/src/runtime/effects.zig +1664 -43
  94. package/src/runtime/effects_fetch_tests.zig +89 -1
  95. package/src/runtime/effects_file_tests.zig +41 -1
  96. package/src/runtime/effects_image_tests.zig +1304 -0
  97. package/src/runtime/effects_tests.zig +153 -0
  98. package/src/runtime/flow.zig +44 -0
  99. package/src/runtime/gpu_surface_events.zig +7 -0
  100. package/src/runtime/media_surface.zig +726 -0
  101. package/src/runtime/media_surface_tests.zig +1476 -0
  102. package/src/runtime/root.zig +20 -0
  103. package/src/runtime/session_blobs.zig +689 -0
  104. package/src/runtime/session_journal.zig +111 -20
  105. package/src/runtime/session_record.zig +27 -2
  106. package/src/runtime/session_replay.zig +168 -16
  107. package/src/runtime/session_tests.zig +2118 -4
  108. package/src/runtime/shell_layout.zig +7 -0
  109. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  110. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  111. package/src/runtime/tests.zig +3 -0
  112. package/src/runtime/ts_core_host.zig +439 -4
  113. package/src/runtime/ts_core_host_tests.zig +41 -0
  114. package/src/runtime/ts_ui_app.zig +63 -0
  115. package/src/runtime/ui_app.zig +169 -6
  116. package/src/runtime/ui_app_tests.zig +334 -0
  117. package/src/runtime/ui_app_window_tests.zig +468 -0
  118. package/src/runtime/window_command_bridge_tests.zig +117 -0
  119. package/src/runtime/window_storage.zig +32 -0
  120. package/src/runtime/window_views.zig +61 -1
  121. package/src/tooling/manifest.zig +75 -0
  122. package/src/tooling/raw_manifest.zig +2 -0
  123. package/src/tooling/templates.zig +219 -1
@@ -1,5 +1,6 @@
1
1
  const std = @import("std");
2
2
  const geometry = @import("geometry");
3
+ const canvas = @import("canvas");
3
4
  const security = @import("../security/root.zig");
4
5
 
5
6
  pub const default_gpu_frame_interval_ns: u64 = 16_666_667;
@@ -152,6 +153,15 @@ pub const PlatformFeature = enum {
152
153
  /// keeps emitting; the null platform models the rule through its
153
154
  /// windows' modeled occlusion so the suites can pin it.
154
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,
155
165
  };
156
166
 
157
167
  pub const WebViewSourceKind = enum {
@@ -256,12 +266,27 @@ pub const max_gpu_surface_packet_binary_bytes: usize = 512 * 1024;
256
266
  pub const max_gpu_present_fallback_detail_bytes: usize = 32;
257
267
  /// Per-image bound for the binary gpu-surface image upload side-channel;
258
268
  /// matches the runtime registry's per-slot bound
259
- /// (`canvas_limits.max_registered_canvas_image_pixel_bytes`).
269
+ /// (`canvas_limits.max_registered_canvas_image_pixel_bytes`). Applies to
270
+ /// ORDINARY registered-image ids only: the registry refuses anything
271
+ /// larger at registration, so an over-bound upload of a registered image
272
+ /// can only be an engine bug and is refused loudly here rather than
273
+ /// copied into host allocations.
260
274
  pub const max_gpu_surface_image_pixel_bytes: usize = 1024 * 1024;
275
+ /// Per-image bound for uploads in the media-surface texture namespace
276
+ /// (ids with `canvas.media_surface_image_id_bit` set — producer-pushed
277
+ /// dynamic textures, structurally disjoint from registered-image ids):
278
+ /// video scale, not avatar scale. Matches the producer channel's
279
+ /// per-frame budget (`canvas_limits.max_media_surface_pixel_bytes`; a
280
+ /// lockstep test in media_surface_tests.zig pins the pair), so a frame
281
+ /// the producer accepted — a full 1920x1080 RGBA8 frame included — can
282
+ /// never be refused between adoption and host presentation.
283
+ pub const max_gpu_surface_media_image_pixel_bytes: usize = 8 * 1024 * 1024;
261
284
  /// Per-font bound for the gpu-surface font registration side-channel;
262
285
  /// matches the runtime registry's per-slot bound
263
- /// (`canvas_limits.max_registered_canvas_font_bytes`).
264
- pub const max_gpu_surface_font_bytes: usize = 2 * 1024 * 1024;
286
+ /// (`canvas_limits.max_registered_canvas_font_bytes`), sized so full
287
+ /// CJK faces register (Noto Sans SC's TrueType build, the largest
288
+ /// measured, is 17.8 MB).
289
+ pub const max_gpu_surface_font_bytes: usize = 24 * 1024 * 1024;
265
290
 
266
291
  pub const ShortcutModifiers = struct {
267
292
  primary: bool = false,
@@ -506,6 +531,24 @@ pub const WindowShowMode = enum {
506
531
  on_first_present,
507
532
  };
508
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
+
509
552
  pub const WindowOptions = struct {
510
553
  id: WindowId = 1,
511
554
  label: []const u8 = "main",
@@ -523,6 +566,9 @@ pub const WindowOptions = struct {
523
566
  /// does not grow an already-smaller restored frame.
524
567
  min_width: f32 = 0,
525
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,
526
572
 
527
573
  pub fn resolvedTitle(self: WindowOptions, app_name: []const u8) []const u8 {
528
574
  return if (self.title.len > 0) self.title else app_name;
@@ -539,6 +585,16 @@ pub const WindowState = struct {
539
585
  focused: bool = true,
540
586
  maximized: bool = false,
541
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,
542
598
  };
543
599
 
544
600
  pub const WindowInfo = struct {
@@ -549,6 +605,8 @@ pub const WindowInfo = struct {
549
605
  scale_factor: f32 = 1,
550
606
  open: bool = true,
551
607
  focused: bool = false,
608
+ /// Alive but policy-hidden — see `WindowState.hidden`.
609
+ hidden: bool = false,
552
610
 
553
611
  pub fn state(self: WindowInfo) WindowState {
554
612
  return .{
@@ -559,6 +617,7 @@ pub const WindowInfo = struct {
559
617
  .scale_factor = self.scale_factor,
560
618
  .open = self.open,
561
619
  .focused = self.focused,
620
+ .hidden = self.hidden,
562
621
  };
563
622
  }
564
623
  };
@@ -577,6 +636,8 @@ pub const WindowCreateOptions = struct {
577
636
  /// `WindowOptions.min_width`/`min_height`); 0 = no floor.
578
637
  min_width: f32 = 0,
579
638
  min_height: f32 = 0,
639
+ /// See `WindowOptions.close_policy`.
640
+ close_policy: WindowClosePolicy = .quit,
580
641
  source: ?WebViewSource = null,
581
642
 
582
643
  pub fn windowOptions(self: WindowCreateOptions, id: WindowId, label: []const u8) WindowOptions {
@@ -592,6 +653,7 @@ pub const WindowCreateOptions = struct {
592
653
  .show = self.show,
593
654
  .min_width = self.min_width,
594
655
  .min_height = self.min_height,
656
+ .close_policy = self.close_policy,
595
657
  };
596
658
  }
597
659
  };
@@ -1078,6 +1140,14 @@ pub const AppInfo = struct {
1078
1140
  /// default menus: web items like Reload only exist when a webview
1079
1141
  /// can answer them.
1080
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,
1081
1151
  window_title: []const u8 = "",
1082
1152
  bundle_id: []const u8 = "dev.native_sdk.app",
1083
1153
  icon_path: []const u8 = "",
@@ -1550,6 +1620,16 @@ pub const GpuSurfaceInputKind = enum {
1550
1620
  ime_set_composition,
1551
1621
  ime_commit_composition,
1552
1622
  ime_cancel_composition,
1623
+ // Trackpad gestures are phase-explicit (every source models phases:
1624
+ // NSEvent.phase on macOS, GTK GestureZoom begin/scale-changed/end,
1625
+ // Windows precision-touchpad gesture messages), and the family stays
1626
+ // open for rotate/smart-zoom later. Only the macOS host emits these
1627
+ // today; Windows (precision touchpad) and Linux (GTK GestureZoom)
1628
+ // are staged follow-ups — on those platforms the kinds simply never
1629
+ // arrive.
1630
+ pinch_begin,
1631
+ pinch_change,
1632
+ pinch_end,
1553
1633
  };
1554
1634
 
1555
1635
  pub const GpuSurfaceInputEvent = struct {
@@ -1568,6 +1648,73 @@ pub const GpuSurfaceInputEvent = struct {
1568
1648
  text: []const u8 = "",
1569
1649
  composition_cursor: ?usize = null,
1570
1650
  modifiers: ShortcutModifiers = .{},
1651
+ /// Pinch magnification DELTA for this event: nonzero only on
1652
+ /// `pinch_change`, 0 on begin/end. The delta is MULTIPLICATIVE: the
1653
+ /// cumulative gesture scale is the running product of `(1 + scale)`
1654
+ /// across the gesture's change events. On macOS this is AppKit's
1655
+ /// raw per-event `NSEvent.magnification`, forwarded untransformed —
1656
+ /// raw magnification IS the multiplicative per-event delta, the
1657
+ /// convention every browser engine ships (see the doctrine note at
1658
+ /// `magnifyWithEvent:` in appkit_host.m), so the product matches
1659
+ /// the zoom the same gesture performs in Safari and Chrome. The
1660
+ /// host guarantees `1 + scale > 0` on every event (a per-event
1661
+ /// floor clamps the physically impossible). The pointer anchor rides
1662
+ /// `x`/`y` (view-local, same space as pointer events) — the pointer
1663
+ /// location during the gesture, NOT a midpoint between the fingers:
1664
+ /// hosts report gesture events at the pointer (AppKit's
1665
+ /// `locationInWindow`), and zoom-at-cursor is the anchoring apps
1666
+ /// want.
1667
+ scale: f32 = 0,
1668
+ };
1669
+
1670
+ /// Pinch gesture phase for the app-level pinch channel (`Options.on_pinch`
1671
+ /// / the TS core's `pinchMsg` export). Every source is phase-explicit
1672
+ /// (NSEvent phases, GTK GestureZoom, Windows precision-touchpad gestures);
1673
+ /// a host-cancelled gesture folds into `.end` — pinch delivers incremental
1674
+ /// deltas the app applies as they arrive, so there is no transient state
1675
+ /// to roll back the way `pointer_cancel` rolls back an in-flight press.
1676
+ /// A terminal host event (Ended or Cancelled) that still measured a
1677
+ /// nonzero magnification delivers that delta as a final `.change` before
1678
+ /// the `.end`, so the cumulative product always matches what the OS
1679
+ /// reported.
1680
+ pub const PinchPhase = enum {
1681
+ begin,
1682
+ change,
1683
+ end,
1684
+ };
1685
+
1686
+ /// The pinch channel's app-facing record, derived from the raw
1687
+ /// `gpu_surface_input` pinch events. Pinch bypasses the widget pipeline
1688
+ /// deliberately: it is a view-global gesture (timeline/canvas zoom), the
1689
+ /// `on_key`-fallback shape rather than a widget-targeted event.
1690
+ pub const PinchEvent = struct {
1691
+ /// Source identity: which window and gpu-surface view the gesture
1692
+ /// happened on (the `GpuFrame` identity shape, because `x`/`y` are
1693
+ /// view-local — a coordinate without its view is not a position).
1694
+ /// Multi-window and multi-view apps tell pinches apart by these.
1695
+ /// `label` is borrowed for the callback, like every event slice:
1696
+ /// a model that keeps it must copy.
1697
+ window_id: WindowId = 1,
1698
+ label: []const u8 = "",
1699
+ phase: PinchPhase,
1700
+ /// Magnification DELTA for this event: nonzero only on `.change`,
1701
+ /// 0 on begin/end. The delta is MULTIPLICATIVE — the cumulative
1702
+ /// gesture scale is the running product of `(1 + scale)` across the
1703
+ /// gesture's change events; apply it memorylessly
1704
+ /// (`zoom *= 1 + scale`), no gesture-start bookkeeping. On macOS
1705
+ /// this is AppKit's raw per-event `NSEvent.magnification`, which IS
1706
+ /// that multiplicative delta per the browser-engine convention, so
1707
+ /// the product matches the zoom the same gesture performs in Safari
1708
+ /// and Chrome. Every event keeps `1 + scale > 0`.
1709
+ scale: f32 = 0,
1710
+ /// The pointer anchor, view-local canvas points (the pointer-event
1711
+ /// space): where the zoom should anchor. This is the POINTER
1712
+ /// location during the gesture (AppKit reports gesture events at
1713
+ /// `locationInWindow`), not a midpoint between the fingers — raw
1714
+ /// touch positions are trackpad-normalized and never reach view
1715
+ /// space, and zoom-at-cursor is what apps want anyway.
1716
+ x: f32 = 0,
1717
+ y: f32 = 0,
1571
1718
  };
1572
1719
 
1573
1720
  /// Upper bound on native scroll drivers per gpu-surface view (one per
@@ -1690,8 +1837,11 @@ pub const GpuSurfaceImagePixels = struct {
1690
1837
  /// keyed by the canvas font id host-wide. The engine validates the bytes
1691
1838
  /// (a parseable TrueType face under the registry bounds) before this
1692
1839
  /// call, so hosts may treat a decode failure as a hard error rather than
1693
- /// a fallback. Ids are permanent for the process — the engine never
1694
- /// 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).
1695
1845
  pub const GpuSurfaceFontData = struct {
1696
1846
  id: u64,
1697
1847
  ttf: []const u8,
@@ -1954,6 +2104,30 @@ pub const PlatformServices = struct {
1954
2104
  /// controls — chromeless windows have no system button to click.
1955
2105
  /// Platforms without the concept leave this null.
1956
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,
1957
2131
  /// Hand the ACTIVE pointer-down to the platform as a window-drag
1958
2132
  /// gesture (the hidden-titlebar drag-region channel): the window
1959
2133
  /// moves once the pointer actually moves — a plain click moves
@@ -2111,7 +2285,34 @@ pub const PlatformServices = struct {
2111
2285
  /// without host-side text (GTK/Win32/null default), where the engine
2112
2286
  /// measures with the parsed face and inks it through the reference
2113
2287
  /// renderer.
2114
- 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,
2115
2316
  update_widget_accessibility_fn: ?*const fn (context: ?*anyopaque, snapshot: WidgetAccessibilitySnapshot) anyerror!void = null,
2116
2317
  /// Reconcile the native scroll drivers for a gpu-surface view against
2117
2318
  /// the full desired set: create missing drivers, update frames /
@@ -2233,6 +2434,16 @@ pub const PlatformServices = struct {
2233
2434
  return minimize_fn(self.context, window_id);
2234
2435
  }
2235
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
+
2236
2447
  pub fn startWindowDrag(self: PlatformServices, window_id: WindowId) anyerror!void {
2237
2448
  const drag_fn = self.start_window_drag_fn orelse return error.UnsupportedService;
2238
2449
  return drag_fn(self.context, window_id);
@@ -2579,7 +2790,19 @@ pub const PlatformServices = struct {
2579
2790
  pub fn uploadGpuSurfaceImage(self: PlatformServices, image: GpuSurfaceImagePixels) anyerror!void {
2580
2791
  const expected = image.expectedByteLen() orelse return error.InvalidGpuSurfaceImage;
2581
2792
  if (image.rgba8.len != expected) return error.InvalidGpuSurfaceImage;
2582
- if (image.rgba8.len > max_gpu_surface_image_pixel_bytes) return error.InvalidGpuSurfaceImage;
2793
+ // Two honest bounds, keyed by the id namespace: hosts copy every
2794
+ // upload into host-owned allocations (AppKit's NSData + NSImage
2795
+ // store), so each bound teaches the real cost of its id space.
2796
+ // Ordinary registered images are avatar-scale by the registry's
2797
+ // own slot bound; media-surface textures (the reserved high-bit
2798
+ // namespace) are video-scale by the producer channel's frame
2799
+ // budget — the bound the producer already enforced, so nothing a
2800
+ // producer staged can fail here.
2801
+ const bound = if ((image.id & canvas.media_surface_image_id_bit) != 0)
2802
+ max_gpu_surface_media_image_pixel_bytes
2803
+ else
2804
+ max_gpu_surface_image_pixel_bytes;
2805
+ if (image.rgba8.len > bound) return error.InvalidGpuSurfaceImage;
2583
2806
  const upload_fn = self.upload_gpu_surface_image_fn orelse return error.UnsupportedService;
2584
2807
  return upload_fn(self.context, image);
2585
2808
  }
@@ -2590,13 +2813,25 @@ pub const PlatformServices = struct {
2590
2813
  return remove_fn(self.context, id);
2591
2814
  }
2592
2815
 
2593
- 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 {
2594
2820
  if (font.id == 0) return error.InvalidGpuSurfaceFont;
2595
2821
  if (font.ttf.len == 0 or font.ttf.len > max_gpu_surface_font_bytes) return error.InvalidGpuSurfaceFont;
2596
2822
  const register_fn = self.register_gpu_surface_font_fn orelse return error.UnsupportedService;
2597
2823
  return register_fn(self.context, font);
2598
2824
  }
2599
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
+
2600
2835
  pub fn updateWidgetAccessibility(self: PlatformServices, snapshot: WidgetAccessibilitySnapshot) anyerror!void {
2601
2836
  if (snapshot.view_label.len == 0 or snapshot.view_label.len > max_view_label_bytes) return error.InvalidViewOptions;
2602
2837
  if (snapshot.nodes.len > max_widget_accessibility_nodes) return error.InvalidViewOptions;
@@ -2675,6 +2910,10 @@ fn defaultSupportsFeature(services: PlatformServices, feature: PlatformFeature)
2675
2910
  // cannot see it; platforms that analyze answer through their own
2676
2911
  // `supports_fn` (like file_drops and gpu_surfaces above).
2677
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,
2678
2917
  };
2679
2918
  }
2680
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;