@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
@@ -41,7 +41,16 @@
41
41
  * WebView loads report WebViewNotFound at runtime. */
42
42
  #if defined(NATIVE_SDK_ALLOW_WEBVIEW2_STUB)
43
43
  #define NATIVE_SDK_HAS_WEBVIEW2 0
44
- #pragma message("Embedded WebView layer excluded by the build configuration: building the Windows host without it (canvas apps unaffected; WebView loads will report WebViewNotFound)")
44
+ /* Deliberately NO compiler diagnostic in this branch — not even an
45
+ * informational #pragma message. The stub is the expected, configured
46
+ * state of every native-only Windows build, and zig renders every
47
+ * clang diagnostic of a failing translation unit as `error:` (its
48
+ * serialized clang diagnostics carry no severity into the error
49
+ * bundle), so an informational note here masquerades as the
50
+ * build-killing error the moment any unrelated real error appears
51
+ * anywhere in this file. The teaching lives where it is actionable
52
+ * instead: a stubbed host reports WebViewNotFound the moment an app
53
+ * actually uses a WebView. */
45
54
  #elif __has_include(<WebView2.h>) && __has_include(<wrl.h>)
46
55
  #include <WebView2.h>
47
56
  #include <wrl.h>
@@ -184,6 +193,10 @@ struct WindowsEvent {
184
193
  double y;
185
194
  int open;
186
195
  int focused;
196
+ /* kWindowFrame: nonzero while the window is alive but hidden by its
197
+ * close_policy (.hide intercepted WM_CLOSE). open stays 1 for the
198
+ * window's whole hidden stretch. */
199
+ int hidden;
187
200
  const char *label;
188
201
  size_t label_len;
189
202
  const char *title;
@@ -306,6 +319,14 @@ struct Window {
306
319
  * behind the button cluster matches the app's header). */
307
320
  COLORREF hidden_caption_color = 0;
308
321
  bool hidden_caption_color_set = false;
322
+ /* close_policy: 0 = quit (WM_CLOSE destroys; the default), 1 = hide
323
+ * (WM_CLOSE hides — SW_HIDE — and the app keeps running behind its
324
+ * tray icon, the Win32 tray-player shape). Applied right after
325
+ * create via native_sdk_windows_set_window_close_policy. */
326
+ int close_policy = 0;
327
+ /* True while the .hide policy has this window off the glass; the
328
+ * hwnd stays live and the map entry stays owned. */
329
+ bool policy_hidden = false;
309
330
  };
310
331
 
311
332
  /* One rectangle of a canvas view's window-drag mirror (runtime push,
@@ -1157,6 +1178,7 @@ static void emit(Host *host, const Window &window, EventKind kind) {
1157
1178
  event.y = window.y;
1158
1179
  event.open = window.hwnd != nullptr;
1159
1180
  event.focused = window.hwnd && GetFocus() == window.hwnd;
1181
+ event.hidden = window.policy_hidden ? 1 : 0;
1160
1182
  event.label = window.label.c_str();
1161
1183
  event.label_len = window.label.size();
1162
1184
  event.title = window.title.c_str();
@@ -1808,11 +1830,15 @@ constexpr uint64_t kGpuFrameIntervalNs = 16666667ull;
1808
1830
  * riding the frame channel (on_frame interpolation, armed tweens) and
1809
1831
  * snap it on restore; the heartbeat keeps those models gently current
1810
1832
  * while event-driven truth (audio position, input) flows at its own
1811
- * cadence. Minimize (IsIconic on the root window) is the one occlusion
1812
- * signal Win32 reports reliably for this GDI-presenting host; a window
1813
- * fully covered by other windows has no dependable signal without a
1814
- * DXGI presentation path, so covered-but-not-minimized windows keep
1815
- * full cadence deliberately rather than guess. */
1833
+ * cadence. Two occlusion facts key the throttle: minimize (IsIconic on
1834
+ * the root window — the one signal Win32 reports reliably for this
1835
+ * GDI-presenting host) and the close_policy .hide hide (host-owned
1836
+ * bookkeeping: a policy-hidden window is SW_HIDE'd off the glass, and
1837
+ * the menu-bar app shape keeps it that way for days — full-rate frame
1838
+ * completions there are pure background CPU burn). A window fully
1839
+ * covered by OTHER windows has no dependable signal without a DXGI
1840
+ * presentation path, so covered-but-not-minimized windows keep full
1841
+ * cadence deliberately rather than guess. */
1816
1842
  constexpr uint64_t kGpuOccludedHeartbeatNs = 1000000000ull;
1817
1843
  /* Placeholder pump timer (repeating, retired by the first present). */
1818
1844
  constexpr UINT_PTR kGpuFrameTimerId = 1;
@@ -2389,13 +2415,19 @@ static void gpuSurfaceAdvancePacingClock(NativeView &view) {
2389
2415
  }
2390
2416
  }
2391
2417
 
2392
- /* Frame completions run on the minimized heartbeat when the surface has
2393
- * presented at least once and its top-level window is iconic — the same
2394
- * first-present exemption the macOS occluded pacing keeps, so surface
2395
- * establishment (and the nonblank verdict automation reads) is never
2396
- * throttled. */
2397
- static bool gpuSurfaceOccludedPacingActive(const NativeView &view) {
2418
+ /* Frame completions run on the occluded heartbeat when the surface has
2419
+ * presented at least once and its window is off the glass — iconic, or
2420
+ * alive-but-hidden under close_policy .hide (see kGpuOccludedHeartbeatNs:
2421
+ * both facts are reliable, and a policy-hidden menu-bar app would
2422
+ * otherwise spin its frame loop full-rate for days). First-present
2423
+ * exemption as on macOS, so surface establishment (and the nonblank
2424
+ * verdict automation reads) is never throttled. */
2425
+ static bool gpuSurfaceOccludedPacingActive(Host *host, const NativeView &view) {
2398
2426
  if (!view.gpu_presented || !view.hwnd) return false;
2427
+ if (host) {
2428
+ auto owner = host->windows.find(view.window_id);
2429
+ if (owner != host->windows.end() && owner->second.policy_hidden) return true;
2430
+ }
2399
2431
  HWND root = GetAncestor(view.hwnd, GA_ROOT);
2400
2432
  return root != nullptr && IsIconic(root);
2401
2433
  }
@@ -2429,7 +2461,7 @@ static void gpuSurfaceEmitFrame(Host *host, NativeView &view, HWND hwnd) {
2429
2461
  /* Heartbeat-paced completions are not latency endpoints: their
2430
2462
  * timestamp measures the deliberate minimized cadence, not a paint
2431
2463
  * — the runtime skips input-latency stamping for them. */
2432
- event.occluded = gpuSurfaceOccludedPacingActive(view) ? 1 : 0;
2464
+ event.occluded = gpuSurfaceOccludedPacingActive(host, view) ? 1 : 0;
2433
2465
  emitGpuSurfaceEvent(host, view, event);
2434
2466
  }
2435
2467
 
@@ -2440,16 +2472,17 @@ static void gpuSurfaceEmitFrame(Host *host, NativeView &view, HWND hwnd) {
2440
2472
  * re-enter the engine — and the pacing clock's grid stamping keeps the
2441
2473
  * message hop out of the period. SetTimer clamps short delays up to its
2442
2474
  * ~10 ms floor; the clock absorbs that as jitter, not drift. */
2443
- static void gpuSurfaceScheduleFrameEmission(NativeView &view) {
2475
+ static void gpuSurfaceScheduleFrameEmission(Host *host, NativeView &view) {
2444
2476
  if (!view.hwnd || view.gpu_emission_scheduled) return;
2445
2477
  const uint64_t now = gpuTimestampNs();
2446
- /* Minimized surfaces pace on the heartbeat, not the frame grid —
2447
- * see kGpuOccludedHeartbeatNs. Exempt: an input's responding frame
2448
- * (external truth on its own cadence; it cannot sustain a spin).
2449
- * Restore re-arms the pending timer at the grid delay (the
2450
- * top-level WM_SIZE handler), so the long delay never gates the
2451
- * return to full cadence. */
2452
- const uint64_t pace_ns = (!view.gpu_prompt_frame_pending && gpuSurfaceOccludedPacingActive(view)) ? kGpuOccludedHeartbeatNs : kGpuFrameIntervalNs;
2478
+ /* Occluded surfaces (minimized or policy-hidden) pace on the
2479
+ * heartbeat, not the frame grid — see kGpuOccludedHeartbeatNs.
2480
+ * Exempt: an input's responding frame (external truth on its own
2481
+ * cadence; it cannot sustain a spin). Reveal re-arms the pending
2482
+ * timer at the grid delay (restore through the top-level WM_SIZE
2483
+ * handler, re-show through the show verb), so the long delay never
2484
+ * gates the return to full cadence. */
2485
+ const uint64_t pace_ns = (!view.gpu_prompt_frame_pending && gpuSurfaceOccludedPacingActive(host, view)) ? kGpuOccludedHeartbeatNs : kGpuFrameIntervalNs;
2453
2486
  uint64_t delay_ns = 0;
2454
2487
  if (view.gpu_last_emit_ns > 0 && now < view.gpu_last_emit_ns + pace_ns) {
2455
2488
  delay_ns = view.gpu_last_emit_ns + pace_ns - now;
@@ -2545,7 +2578,7 @@ static LRESULT CALLBACK gpuSurfaceProc(HWND hwnd, UINT message, WPARAM wparam, L
2545
2578
  KillTimer(hwnd, kGpuFrameTimerId);
2546
2579
  return 0;
2547
2580
  }
2548
- gpuSurfaceScheduleFrameEmission(*view);
2581
+ gpuSurfaceScheduleFrameEmission(host, *view);
2549
2582
  return 0;
2550
2583
  }
2551
2584
  if (wparam == kGpuEmitTimerId) {
@@ -3555,16 +3588,19 @@ static void audioSpectrumStartCapture(Host *host) {
3555
3588
  }
3556
3589
 
3557
3590
  /* Whether any of the host's top-level windows still reaches the glass.
3558
- * Minimize-keyed on purpose: IsIconic is the one occlusion fact this
3559
- * host trusts (the same decision the minimized frame heartbeat makes —
3560
- * the covered-but-not-minimized case has no reliable cheap signal on
3561
- * the DXGI presentation path), so covered windows keep full spectrum
3562
- * cadence and only an all-minimized app goes quiet. Checked across the
3563
- * whole window table because a spectrum consumer may draw its bands in
3564
- * any of the app's windows. */
3591
+ * Keyed on the two occlusion facts this host trusts (the same decision
3592
+ * the occluded frame heartbeat makes): minimize (IsIconic) and the
3593
+ * close_policy .hide hide (policy_hidden — a menu-bar app's window off
3594
+ * the glass behind its tray icon must not keep 25 Hz spectrum
3595
+ * emissions flowing for a display nobody can see). The
3596
+ * covered-but-not-minimized case has no reliable cheap signal without
3597
+ * a DXGI presentation path, so covered windows keep full spectrum
3598
+ * cadence and only an app with every window minimized or policy-hidden
3599
+ * goes quiet. Checked across the whole window table because a spectrum
3600
+ * consumer may draw its bands in any of the app's windows. */
3565
3601
  static bool audioAnyWindowReachesGlass(Host *host) {
3566
3602
  for (auto &entry : host->windows) {
3567
- if (entry.second.hwnd && !IsIconic(entry.second.hwnd)) return true;
3603
+ if (entry.second.hwnd && !IsIconic(entry.second.hwnd) && !entry.second.policy_hidden) return true;
3568
3604
  }
3569
3605
  return false;
3570
3606
  }
@@ -3575,12 +3611,13 @@ static bool audioAnyWindowReachesGlass(Host *host) {
3575
3611
  * transports emit nothing — the bars freeze honestly — while silence on
3576
3612
  * a rolling transport still emits its row of zeros. The occluded-
3577
3613
  * emission rule gates delivery too: SPECTRUM bands describe a display,
3578
- * so while every window is minimized no report is emitted — no event
3579
- * wakes the runtime's update loop for glass nobody can see, and the
3580
- * journal records the stretch as honest silence. The capture thread
3581
- * keeps its ring fresh (it must drain the loopback client regardless,
3582
- * and its FFT runs off the loop thread), so the first beat after a
3583
- * restore delivers current bands — honest within one report. */
3614
+ * so while every window is minimized or policy-hidden no report is
3615
+ * emitted — no event wakes the runtime's update loop for glass nobody
3616
+ * can see, and the journal records the stretch as honest silence. The
3617
+ * capture thread keeps its ring fresh (it must drain the loopback
3618
+ * client regardless, and its FFT runs off the loop thread), so the
3619
+ * first beat after a restore or re-show delivers current bands —
3620
+ * honest within one report. */
3584
3621
  static void audioHandleSpectrumMessage(Host *host, WPARAM generation) {
3585
3622
  AudioState &audio = host->audio;
3586
3623
  AudioSpectrumState &spectrum = host->spectrum;
@@ -4783,7 +4820,7 @@ static LRESULT CALLBACK windowProc(HWND hwnd, UINT message, WPARAM wparam, LPARA
4783
4820
  if (surface.kind != kViewGpuSurface || !surface.hwnd || !surface.gpu_emission_scheduled) continue;
4784
4821
  if (GetAncestor(surface.hwnd, GA_ROOT) != hwnd) continue;
4785
4822
  surface.gpu_emission_scheduled = false;
4786
- gpuSurfaceScheduleFrameEmission(surface);
4823
+ gpuSurfaceScheduleFrameEmission(host, surface);
4787
4824
  }
4788
4825
  }
4789
4826
  }
@@ -4896,6 +4933,38 @@ static LRESULT CALLBACK windowProc(HWND hwnd, UINT message, WPARAM wparam, LPARA
4896
4933
  if (host) emitAppearanceIfChanged(host, false);
4897
4934
  break;
4898
4935
  case WM_CLOSE:
4936
+ /* close_policy .hide: the user's close (caption X, Alt+F4)
4937
+ * hides the window instead of destroying it — the hwnd and
4938
+ * every view stay live, the app keeps running behind its
4939
+ * tray icon, and the frame event reports the hidden state.
4940
+ * Runtime-initiated closes call DestroyWindow directly
4941
+ * (native_sdk_windows_close_window) and bypass this hook. */
4942
+ if (host) {
4943
+ for (auto &entry : host->windows) {
4944
+ if (entry.second.hwnd == hwnd && entry.second.close_policy == 1) {
4945
+ /* The hide is honest only while a tray icon is
4946
+ * live: SW_HIDE removes the taskbar entry and
4947
+ * Windows has no dock-reopen path, so without a
4948
+ * tray the hidden window is a running, invisible,
4949
+ * unreachable app. tray_active is set only when
4950
+ * Shell_NotifyIcon actually added the icon, so a
4951
+ * declared tray whose creation FAILED at runtime
4952
+ * lands here too. The trade: consulting the live
4953
+ * state at close time means a close that arrives
4954
+ * before the app installs its tray (or after it
4955
+ * removes it) really closes — a loud, visible
4956
+ * close beats a silently stranded process. */
4957
+ if (!host->tray_active) {
4958
+ fprintf(stderr, "native-sdk: close_policy \"hide\" downgraded to a real close: no live tray icon exists (tray creation failed or no status item was installed), so a hidden window would have no re-show affordance\n");
4959
+ break;
4960
+ }
4961
+ entry.second.policy_hidden = true;
4962
+ ShowWindow(hwnd, SW_HIDE);
4963
+ emit(host, entry.second, kWindowFrame);
4964
+ return 0;
4965
+ }
4966
+ }
4967
+ }
4899
4968
  DestroyWindow(hwnd);
4900
4969
  return 0;
4901
4970
  case WM_DESTROY:
@@ -4905,6 +4974,8 @@ static LRESULT CALLBACK windowProc(HWND hwnd, UINT message, WPARAM wparam, LPARA
4905
4974
  destroyNativeViewsForWindow(host, entry.first);
4906
4975
  destroyChildWebViewsForWindow(host, entry.first);
4907
4976
  entry.second.hwnd = nullptr;
4977
+ /* Destroyed means closed, never hidden. */
4978
+ entry.second.policy_hidden = false;
4908
4979
  emit(host, entry.second, kWindowFrame);
4909
4980
  }
4910
4981
  }
@@ -5594,6 +5665,47 @@ int native_sdk_windows_minimize_window(Host *host, uint64_t window_id) {
5594
5665
  return 1;
5595
5666
  }
5596
5667
 
5668
+ /* The show verb: bring the window back to the glass and foreground it —
5669
+ * the counterpart to a close_policy hide (and the tray-menu "Open"
5670
+ * consequence). Restores a minimized window on the way. */
5671
+ int native_sdk_windows_show_window(Host *host, uint64_t window_id) {
5672
+ if (!host) return 0;
5673
+ auto found = host->windows.find(window_id);
5674
+ if (found == host->windows.end() || !found->second.hwnd) return 0;
5675
+ found->second.policy_hidden = false;
5676
+ ShowWindow(found->second.hwnd, IsIconic(found->second.hwnd) ? SW_RESTORE : SW_SHOW);
5677
+ /* Re-show returns full frame cadence without dropping a beat, the
5678
+ * same supersede the top-level WM_SIZE handler runs on restore:
5679
+ * SW_SHOW on a same-size window dispatches no WM_SIZE, so a
5680
+ * heartbeat-paced emission parked up to a second out on a child
5681
+ * surface's one-shot timer is re-armed here at the frame-grid
5682
+ * delay (policy_hidden just cleared, so the scheduler paces on the
5683
+ * grid again; SetTimer with the same id replaces the pending
5684
+ * timer). */
5685
+ for (auto &view_entry : host->native_views) {
5686
+ NativeView &surface = view_entry.second;
5687
+ if (surface.kind != kViewGpuSurface || !surface.hwnd || !surface.gpu_emission_scheduled) continue;
5688
+ if (surface.window_id != window_id) continue;
5689
+ surface.gpu_emission_scheduled = false;
5690
+ gpuSurfaceScheduleFrameEmission(host, surface);
5691
+ }
5692
+ SetForegroundWindow(found->second.hwnd);
5693
+ SetFocus(found->second.hwnd);
5694
+ emit(host, found->second, kWindowFrame);
5695
+ return 1;
5696
+ }
5697
+
5698
+ /* What the user's close affordance does: 0 = quit (destroy; the
5699
+ * default), 1 = hide (SW_HIDE and keep running). Applied right after
5700
+ * create — close handling is host window state for the window's life. */
5701
+ int native_sdk_windows_set_window_close_policy(Host *host, uint64_t window_id, int close_policy) {
5702
+ if (!host) return 0;
5703
+ auto found = host->windows.find(window_id);
5704
+ if (found == host->windows.end()) return 0;
5705
+ found->second.close_policy = close_policy;
5706
+ return 1;
5707
+ }
5708
+
5597
5709
  int native_sdk_windows_create_view(Host *host, uint64_t window_id, const char *label, size_t label_len, int kind, const char *parent, size_t parent_len, double x, double y, double width, double height, int layer, int visible, int enabled, const char *role, size_t role_len, const char *accessibility_label, size_t accessibility_label_len, const char *text, size_t text_len, const char *command, size_t command_len) {
5598
5710
  if (!host || label_len == 0 || !isSupportedNativeViewKind(kind) || !validNativeViewFrame(x, y, width, height)) return 0;
5599
5711
  auto window = host->windows.find(window_id);
@@ -5740,7 +5852,7 @@ int native_sdk_windows_request_gpu_surface_frame(Host *host, uint64_t window_id,
5740
5852
  * frame-event scheduler: repaint retained content and arm the next
5741
5853
  * grid-paced emission (folding into one already in flight). */
5742
5854
  InvalidateRect(found->second.hwnd, nullptr, FALSE);
5743
- gpuSurfaceScheduleFrameEmission(found->second);
5855
+ gpuSurfaceScheduleFrameEmission(host, found->second);
5744
5856
  return 1;
5745
5857
  }
5746
5858
 
@@ -5758,7 +5870,7 @@ int native_sdk_windows_note_gpu_surface_input(Host *host, uint64_t window_id, co
5758
5870
  view.gpu_prompt_frame_pending = true;
5759
5871
  if (view.gpu_emission_scheduled) {
5760
5872
  view.gpu_emission_scheduled = false;
5761
- gpuSurfaceScheduleFrameEmission(view);
5873
+ gpuSurfaceScheduleFrameEmission(host, view);
5762
5874
  }
5763
5875
  return 1;
5764
5876
  }
@@ -5826,7 +5938,7 @@ int native_sdk_windows_present_gpu_surface_pixels(Host *host, uint64_t window_id
5826
5938
  const bool first_present = !view.gpu_presented;
5827
5939
  view.gpu_presented = true;
5828
5940
  if (first_present) view.gpu_prompt_frame_pending = true;
5829
- gpuSurfaceScheduleFrameEmission(view);
5941
+ gpuSurfaceScheduleFrameEmission(host, view);
5830
5942
  return 1;
5831
5943
  }
5832
5944
 
@@ -83,6 +83,9 @@ fn shellWindowFrom(comptime window: anytype) types.ShellWindow {
83
83
  }
84
84
  if (@hasField(@TypeOf(window), "min_width")) out.min_width = window.min_width;
85
85
  if (@hasField(@TypeOf(window), "min_height")) out.min_height = window.min_height;
86
+ if (@hasField(@TypeOf(window), "close_policy")) {
87
+ out.close_policy = enumField(types.WindowClosePolicy, window.close_policy, "close_policy");
88
+ }
86
89
  if (@hasField(@TypeOf(window), "views")) {
87
90
  var views: []const types.ShellView = &.{};
88
91
  for (window.views) |view| {
@@ -210,6 +213,7 @@ test "shellConfigFrom converts a zon-shaped scene" {
210
213
  .height = 320,
211
214
  .restore_state = false,
212
215
  .restore_policy = "center_on_primary",
216
+ .close_policy = "hide",
213
217
  .views = .{
214
218
  .{ .label = "main-canvas", .kind = "gpu_surface", .fill = true, .gpu_backend = "metal", .gpu_vsync = true },
215
219
  },
@@ -223,6 +227,7 @@ test "shellConfigFrom converts a zon-shaped scene" {
223
227
  try std.testing.expectEqualStrings("Demo", scene.windows[0].title.?);
224
228
  try std.testing.expectEqual(@as(f32, 480), scene.windows[0].width);
225
229
  try std.testing.expectEqual(types.WindowRestorePolicy.center_on_primary, scene.windows[0].restore_policy);
230
+ try std.testing.expectEqual(types.WindowClosePolicy.hide, scene.windows[0].close_policy);
226
231
  try std.testing.expectEqual(@as(usize, 1), scene.windows[0].views.len);
227
232
  try std.testing.expectEqual(types.ViewKind.gpu_surface, scene.windows[0].views[0].kind);
228
233
  try std.testing.expectEqual(types.GpuSurfaceBackend.metal, scene.windows[0].views[0].gpu_backend.?);
@@ -52,6 +52,7 @@ pub const FrontendDevConfig = types.FrontendDevConfig;
52
52
  pub const FrontendConfig = types.FrontendConfig;
53
53
  pub const WindowRestorePolicy = types.WindowRestorePolicy;
54
54
  pub const WindowTitlebarStyle = types.WindowTitlebarStyle;
55
+ pub const WindowClosePolicy = types.WindowClosePolicy;
55
56
  pub const Window = types.Window;
56
57
  pub const ViewKind = types.ViewKind;
57
58
  pub const GpuSurfaceBackend = types.GpuSurfaceBackend;
@@ -310,6 +310,31 @@ pub const WindowTitlebarStyle = enum {
310
310
  chromeless,
311
311
  };
312
312
 
313
+ /// What the window's CLOSE affordance (the red traffic light, cmd+W,
314
+ /// the caption X) does. `.quit` is the classic document-app default:
315
+ /// the window really closes, and closing the last one follows the
316
+ /// host's exit semantics — behavior unchanged for every app that never
317
+ /// declares the field.
318
+ ///
319
+ /// `.hide` is the menu-bar/tray-app shape: the close affordance hides
320
+ /// the window (it stays alive with its views, `WindowState.hidden`
321
+ /// flips true) and the app keeps running behind its status item — a
322
+ /// tray action's `Effects.showWindow`, or the macOS Dock reopen,
323
+ /// brings it back. Supported where the host can re-show a hidden
324
+ /// window (macOS, Windows); platforms without that affordance refuse
325
+ /// the declaration loudly at create time rather than stranding a
326
+ /// hidden window.
327
+ ///
328
+ /// Room is deliberately left for a future `.event` tier — the model
329
+ /// receives the close request as a Msg and owns the consequence
330
+ /// (unsaved-changes prompts) — which is NOT implemented yet;
331
+ /// model-declared secondary windows already have that shape today via
332
+ /// `WindowDescriptor.on_close`.
333
+ pub const WindowClosePolicy = enum {
334
+ quit,
335
+ hide,
336
+ };
337
+
313
338
  pub const Window = struct {
314
339
  label: []const u8 = "main",
315
340
  title: ?[]const u8 = null,
@@ -329,6 +354,8 @@ pub const Window = struct {
329
354
  /// layout clamping/clipping panes below it. 0 = no floor.
330
355
  min_width: f32 = 0,
331
356
  min_height: f32 = 0,
357
+ /// What the user's close affordance does — see `WindowClosePolicy`.
358
+ close_policy: WindowClosePolicy = .quit,
332
359
  };
333
360
 
334
361
  pub const ViewKind = enum {
@@ -459,6 +486,11 @@ pub const ShellWindow = struct {
459
486
  /// runtime-created windows apply their own declaration at create.
460
487
  min_width: f32 = 0,
461
488
  min_height: f32 = 0,
489
+ /// What the user's close affordance does — see `WindowClosePolicy`.
490
+ /// Like `titlebar`, close handling is host window state: the
491
+ /// STARTUP window threads it through the host create, and
492
+ /// runtime-created windows apply their own declaration at create.
493
+ close_policy: WindowClosePolicy = .quit,
462
494
  views: []const ShellView = &.{},
463
495
  };
464
496
 
@@ -44,13 +44,13 @@ pub const max_chart_points_per_series: usize = 256;
44
44
  /// filled series — or dozens of sparkline-sized ones — per frame.
45
45
  pub const max_chart_path_elements_per_frame: usize = 2048;
46
46
 
47
- /// Per-frame byte budget for the formatted tick/tooltip label scratch
48
- /// (threadlocal frame-lifetime storage: the emitted `drawText` commands
49
- /// slice into it until the runtime copies the display list). A y axis
50
- /// labels at most a handful of ticks and a
51
- /// hover tooltip a row per series, each `max_chart_value_label_bytes`
52
- /// long, so 2 KiB holds dozens of charts per frame; overflow fails
53
- /// loudly with `ChartLabelBytesFull`.
47
+ /// Byte budget for the display-list builder's own formatted tick/tooltip
48
+ /// label store (`Builder.allocChartLabelBytes` — builder-owned like the
49
+ /// path-element store, so the emitted `drawText` commands keep their
50
+ /// label bytes for the builder's lifetime). A y axis labels at most a
51
+ /// handful of ticks and a hover tooltip a row per series, each
52
+ /// `max_chart_value_label_bytes` long, so 2 KiB holds dozens of charts
53
+ /// per frame; overflow fails loudly with `ChartLabelBytesFull`.
54
54
  pub const max_chart_label_bytes_per_frame: usize = 2048;
55
55
 
56
56
  /// Longest formatted value label: the integer digits of f32's full
@@ -436,6 +436,72 @@ test "axis labels draw muted in reserved gutters and thin to fit" {
436
436
  try testing.expect(narrow_plot.maxY() < widget.frame.maxY());
437
437
  }
438
438
 
439
+ test "emitted tick labels survive a later emit into another builder" {
440
+ // The builder contract (the `allocPathElements` rule): a display
441
+ // list may be HELD while another builder emits, and it may
442
+ // accumulate several emit calls — either way every emitted command
443
+ // stays intact. Label bytes live in the BUILDER's own store, so a
444
+ // second chart's labels can never overwrite a held list's text the
445
+ // way a per-emit-reset scratch pool would.
446
+ var widget = Widget{
447
+ .id = 93,
448
+ .kind = WidgetKind.chart,
449
+ .frame = geometry.RectF.init(0, 0, 240, 120),
450
+ .chart = .{
451
+ .series = &.{.{ .kind = .line, .values = &[_]f32{ 0.2, 0.8 } }},
452
+ .y_min = 0,
453
+ .y_max = 1,
454
+ .grid_lines = 1,
455
+ .y_labels = true,
456
+ },
457
+ };
458
+ const tokens = DesignTokens{};
459
+ var commands: [32]CanvasCommand = undefined;
460
+ var builder = Builder.init(&commands);
461
+ try emitWidgetTree(&builder, widget, tokens);
462
+
463
+ // A second chart over a DIFFERENT domain emits different label
464
+ // bytes into another builder while the first list is held.
465
+ widget.chart.y_min = 0;
466
+ widget.chart.y_max = 4;
467
+ var other_commands: [32]CanvasCommand = undefined;
468
+ var other_builder = Builder.init(&other_commands);
469
+ try emitWidgetTree(&other_builder, widget, tokens);
470
+
471
+ // The held list still reads the FIRST chart's lattice ("0", "0.5",
472
+ // "1"), not the second's ("0", "2", "4") or a byte mix.
473
+ try expectChartTickLabels(builder.displayList(), &.{ "0", "0.5", "1" });
474
+ try expectChartTickLabels(other_builder.displayList(), &.{ "0", "2", "4" });
475
+
476
+ // Accumulating both charts into ONE builder keeps both intact too.
477
+ var accumulated_commands: [64]CanvasCommand = undefined;
478
+ var accumulated = Builder.init(&accumulated_commands);
479
+ widget.chart.y_min = 0;
480
+ widget.chart.y_max = 1;
481
+ try emitWidgetTree(&accumulated, widget, tokens);
482
+ var second = widget;
483
+ second.id = 94;
484
+ second.chart.y_min = 0;
485
+ second.chart.y_max = 4;
486
+ try emitWidgetTree(&accumulated, second, tokens);
487
+ try expectChartTickLabels(accumulated.displayList(), &.{ "0", "0.5", "1", "0", "2", "4" });
488
+ }
489
+
490
+ fn expectChartTickLabels(display_list: DisplayList, expected: []const []const u8) !void {
491
+ var index: usize = 0;
492
+ for (display_list.commands) |command| {
493
+ switch (command) {
494
+ .draw_text => |text| {
495
+ try testing.expect(index < expected.len);
496
+ try testing.expectEqualStrings(expected[index], text.text);
497
+ index += 1;
498
+ },
499
+ else => {},
500
+ }
501
+ }
502
+ try testing.expectEqual(expected.len, index);
503
+ }
504
+
439
505
  test "hover-detail chrome renders only under interaction and is deterministic" {
440
506
  const values = [_]f32{ 0.2, 0.8, 0.4, 0.6 };
441
507
  const labels = [_][]const u8{ "q1", "q2", "q3", "q4" };
@@ -370,6 +370,13 @@ fn nonNegative(value: f32) f32 {
370
370
  return @max(0, value);
371
371
  }
372
372
 
373
+ /// Byte budget for the builder-owned presented-text store
374
+ /// (`Builder.text_bytes`). Mirrors the runtime's per-view
375
+ /// `max_canvas_text_bytes_per_view` draw-text budget — a lockstep test
376
+ /// keeps the two from drifting — so the store can only overflow on a
377
+ /// frame the per-view display-list copy would refuse anyway.
378
+ pub const max_display_list_text_bytes: usize = 32768;
379
+
373
380
  pub const Builder = struct {
374
381
  commands: []CanvasCommand,
375
382
  len: usize = 0,
@@ -387,6 +394,24 @@ pub const Builder = struct {
387
394
  /// anyway.
388
395
  path_elements: [chart_model.max_chart_path_elements_per_frame]drawing_model.PathElement = undefined,
389
396
  path_element_len: usize = 0,
397
+ /// Builder-owned storage for text bytes FORMATTED at emit time
398
+ /// (chart y-tick labels and hover-detail title/value rows): the
399
+ /// `path_elements` lifetime rule applied to label text, so emitted
400
+ /// `draw_text` commands survive accumulation across emit calls and
401
+ /// other builders' emissions instead of slicing per-emit-reset
402
+ /// thread scratch. Overflow fails loudly by the chart label budget's
403
+ /// name, exactly as the scratch it replaces did.
404
+ label_bytes: [chart_model.max_chart_label_bytes_per_frame]u8 = undefined,
405
+ label_byte_len: usize = 0,
406
+ /// Builder-owned storage for single-line PRESENTED values (a
407
+ /// line-broken value painted with breaks-as-spaces): same lifetime
408
+ /// rule again. Sized to the runtime's per-view draw-text budget (a
409
+ /// lockstep test keeps the two equal), so a frame that overflows
410
+ /// this store was already over the per-view copy budget; the
411
+ /// presenting emitter falls back to the raw view-owned value and
412
+ /// its forced clip contains the fallback.
413
+ text_bytes: [max_display_list_text_bytes]u8 = undefined,
414
+ text_byte_len: usize = 0,
390
415
 
391
416
  pub fn init(commands: []CanvasCommand) Builder {
392
417
  return .{ .commands = commands };
@@ -395,6 +420,8 @@ pub const Builder = struct {
395
420
  pub fn reset(self: *Builder) void {
396
421
  self.len = 0;
397
422
  self.path_element_len = 0;
423
+ self.label_byte_len = 0;
424
+ self.text_byte_len = 0;
398
425
  }
399
426
 
400
427
  /// Reserve `count` path elements in the builder-owned store. The
@@ -408,6 +435,27 @@ pub const Builder = struct {
408
435
  return self.path_elements[start..self.path_element_len];
409
436
  }
410
437
 
438
+ /// Persist a formatted chart label into the builder-owned label
439
+ /// store (same lifetime contract as `allocPathElements`).
440
+ pub fn allocChartLabelBytes(self: *Builder, text: []const u8) error{ChartLabelBytesFull}![]const u8 {
441
+ if (self.label_byte_len + text.len > self.label_bytes.len) return error.ChartLabelBytesFull;
442
+ const start = self.label_byte_len;
443
+ self.label_byte_len += text.len;
444
+ @memcpy(self.label_bytes[start..self.label_byte_len], text);
445
+ return self.label_bytes[start..self.label_byte_len];
446
+ }
447
+
448
+ /// Persist emit-built text bytes (presented single-line values) into
449
+ /// the builder-owned text store (same lifetime contract as
450
+ /// `allocPathElements`).
451
+ pub fn allocTextBytes(self: *Builder, text: []const u8) error{DisplayListTextBytesFull}![]const u8 {
452
+ if (self.text_byte_len + text.len > self.text_bytes.len) return error.DisplayListTextBytesFull;
453
+ const start = self.text_byte_len;
454
+ self.text_byte_len += text.len;
455
+ @memcpy(self.text_bytes[start..self.text_byte_len], text);
456
+ return self.text_bytes[start..self.text_byte_len];
457
+ }
458
+
411
459
  pub fn displayList(self: *const Builder) DisplayList {
412
460
  return .{ .commands = self.commands[0..self.len] };
413
461
  }