@native-sdk/cli 0.9.1 → 0.9.3

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 (156) hide show
  1. package/build/app.zig +26 -9
  2. package/build.zig +124 -1
  3. package/package.json +9 -9
  4. package/packages/core/package-lock.json +2 -2
  5. package/packages/core/package.json +1 -1
  6. package/packages/core/sdk/core.ts +3 -1
  7. package/packages/core/sdk/events.d.ts +7 -0
  8. package/packages/core/sdk/events.ts +26 -5
  9. package/packages/core/sdk/text.d.ts +4 -0
  10. package/packages/core/sdk/text.ts +51 -0
  11. package/packages/core/src/checker.ts +58 -1
  12. package/packages/core/src/contract.ts +5 -4
  13. package/packages/core/src/devhost_journal.mjs +2 -2
  14. package/packages/core/src/diagnostics.ts +2 -2
  15. package/packages/core/src/service_contract.ts +10 -0
  16. package/packages/core/src/typed_ast.ts +9 -7
  17. package/packages/core/src/types.ts +28 -8
  18. package/skill-data/automation/SKILL.md +4 -3
  19. package/skill-data/native-ui/SKILL.md +10 -8
  20. package/skill-data/ts-core/SKILL.md +8 -5
  21. package/src/app_runner/root.zig +301 -12
  22. package/src/app_runner/ts_core_main.zig +6 -4
  23. package/src/app_runner/window_placement.zig +63 -0
  24. package/src/automation/protocol.zig +5 -1
  25. package/src/automation/snapshot.zig +119 -0
  26. package/src/embed/app_exports.zig +2 -1
  27. package/src/embed/conversions.zig +1 -0
  28. package/src/embed/host.zig +10 -3
  29. package/src/embed/root.zig +1 -0
  30. package/src/embed/tests.zig +13 -1
  31. package/src/embed/types.zig +2 -0
  32. package/src/embed/ui_host.zig +26 -2
  33. package/src/platform/android/NativeSdkActivity.java +49 -4
  34. package/src/platform/android/android_host.c +3 -3
  35. package/src/platform/android/native_sdk_app.h +1 -1
  36. package/src/platform/ios/apple_image_fit.h +38 -0
  37. package/src/platform/ios/files.zig +1 -0
  38. package/src/platform/ios/native_sdk_app.h +1 -1
  39. package/src/platform/ios/uikit_host.m +28 -5
  40. package/src/platform/linux/cef_host.cpp +10 -2
  41. package/src/platform/linux/gtk_host.c +43 -10
  42. package/src/platform/linux/gtk_host.h +3 -3
  43. package/src/platform/linux/root.zig +22 -7
  44. package/src/platform/macos/appkit_host.h +7 -4
  45. package/src/platform/macos/appkit_host.m +271 -52
  46. package/src/platform/macos/cef_host.mm +132 -34
  47. package/src/platform/macos/image_fit_test.m +44 -0
  48. package/src/platform/macos/root.zig +222 -14
  49. package/src/platform/null_platform.zig +17 -3
  50. package/src/platform/null_platform_tests.zig +58 -4
  51. package/src/platform/root.zig +2 -0
  52. package/src/platform/types.zig +69 -19
  53. package/src/platform/windows/root.zig +22 -7
  54. package/src/platform/windows/webview2_host.cpp +31 -6
  55. package/src/primitives/app_manifest/root.zig +3 -0
  56. package/src/primitives/app_manifest/tests.zig +24 -0
  57. package/src/primitives/app_manifest/types.zig +13 -0
  58. package/src/primitives/app_manifest/validation.zig +24 -0
  59. package/src/primitives/canvas/a11y_audit.zig +9 -7
  60. package/src/primitives/canvas/a11y_audit_tests.zig +24 -0
  61. package/src/primitives/canvas/events.zig +129 -1
  62. package/src/primitives/canvas/layout_audit.zig +6 -6
  63. package/src/primitives/canvas/markdown.zig +4 -0
  64. package/src/primitives/canvas/markdown_tests.zig +1 -0
  65. package/src/primitives/canvas/png.zig +138 -0
  66. package/src/primitives/canvas/root.zig +14 -2
  67. package/src/primitives/canvas/text_interaction.zig +23 -0
  68. package/src/primitives/canvas/text_span_tests.zig +5 -3
  69. package/src/primitives/canvas/text_spans.zig +2 -0
  70. package/src/primitives/canvas/text_tests.zig +65 -0
  71. package/src/primitives/canvas/ui.zig +66 -10
  72. package/src/primitives/canvas/ui_markup.zig +40 -13
  73. package/src/primitives/canvas/ui_markup_binary.zig +8 -6
  74. package/src/primitives/canvas/ui_markup_binary_tests.zig +14 -4
  75. package/src/primitives/canvas/ui_markup_compiled.zig +5 -1
  76. package/src/primitives/canvas/ui_markup_compiled_tests.zig +55 -0
  77. package/src/primitives/canvas/ui_markup_reflect.zig +6 -5
  78. package/src/primitives/canvas/ui_markup_tests.zig +11 -2
  79. package/src/primitives/canvas/ui_markup_view.zig +8 -2
  80. package/src/primitives/canvas/ui_markup_view_tests.zig +67 -6
  81. package/src/primitives/canvas/ui_schema.zig +17 -13
  82. package/src/primitives/canvas/ui_tests.zig +84 -0
  83. package/src/primitives/canvas/widget_access.zig +3 -3
  84. package/src/primitives/canvas/widget_anchored_tests.zig +106 -0
  85. package/src/primitives/canvas/widget_builtin_tests.zig +593 -32
  86. package/src/primitives/canvas/widget_invalidation.zig +33 -6
  87. package/src/primitives/canvas/widget_layout.zig +333 -126
  88. package/src/primitives/canvas/widget_layout_tests.zig +169 -0
  89. package/src/primitives/canvas/widget_metrics.zig +7 -8
  90. package/src/primitives/canvas/widget_render.zig +76 -44
  91. package/src/primitives/canvas/widget_render_controls.zig +110 -32
  92. package/src/primitives/canvas/widget_render_style.zig +58 -18
  93. package/src/primitives/canvas/widget_render_surfaces.zig +45 -14
  94. package/src/primitives/canvas/widget_routing.zig +40 -34
  95. package/src/primitives/canvas/widget_runtime.zig +11 -2
  96. package/src/primitives/canvas/widget_runtime_tests.zig +10 -14
  97. package/src/primitives/canvas/widget_semantics.zig +8 -3
  98. package/src/primitives/canvas/widget_semantics_tests.zig +6 -2
  99. package/src/primitives/canvas/widget_tree.zig +99 -5
  100. package/src/primitives/canvas/widgets.zig +20 -7
  101. package/src/root.zig +3 -0
  102. package/src/runtime/api.zig +6 -0
  103. package/src/runtime/automation_snapshot.zig +4 -0
  104. package/src/runtime/automation_widget_dispatch.zig +48 -6
  105. package/src/runtime/builtin_bridge.zig +5 -2
  106. package/src/runtime/canvas_frame_packet_tests.zig +22 -13
  107. package/src/runtime/canvas_image_tests.zig +146 -2
  108. package/src/runtime/canvas_images.zig +35 -16
  109. package/src/runtime/canvas_limits.zig +13 -3
  110. package/src/runtime/canvas_widget_accessibility_tests.zig +279 -4
  111. package/src/runtime/canvas_widget_anchored_tests.zig +54 -0
  112. package/src/runtime/canvas_widget_control_tests.zig +33 -1
  113. package/src/runtime/canvas_widget_events.zig +151 -26
  114. package/src/runtime/canvas_widget_floating_tests.zig +48 -0
  115. package/src/runtime/canvas_widget_runtime.zig +322 -102
  116. package/src/runtime/canvas_widget_scroll_driver_tests.zig +34 -0
  117. package/src/runtime/canvas_widget_scroll_drivers.zig +12 -10
  118. package/src/runtime/canvas_widget_scroll_tests.zig +193 -2
  119. package/src/runtime/canvas_widget_semantics_tests.zig +337 -3
  120. package/src/runtime/canvas_widget_split_tree_tests.zig +55 -0
  121. package/src/runtime/canvas_widget_state.zig +33 -7
  122. package/src/runtime/canvas_widget_text_tests.zig +124 -0
  123. package/src/runtime/core.zig +16 -0
  124. package/src/runtime/effects.zig +14 -14
  125. package/src/runtime/effects_image_tests.zig +91 -0
  126. package/src/runtime/gpu_surface_events.zig +25 -2
  127. package/src/runtime/native_command_event_tests.zig +29 -0
  128. package/src/runtime/root.zig +2 -0
  129. package/src/runtime/session_journal.zig +21 -2
  130. package/src/runtime/session_replay.zig +13 -8
  131. package/src/runtime/session_tests.zig +111 -4
  132. package/src/runtime/shell_layout_tests.zig +12 -0
  133. package/src/runtime/ts_core_host_tests.zig +47 -0
  134. package/src/runtime/ts_ui_app.zig +164 -1
  135. package/src/runtime/ui_app.zig +100 -21
  136. package/src/runtime/ui_app_tests.zig +351 -32
  137. package/src/runtime/ui_app_window_tests.zig +13 -1
  138. package/src/runtime/view.zig +3 -0
  139. package/src/runtime/view_widget_control.zig +18 -0
  140. package/src/runtime/view_widget_scroll.zig +112 -18
  141. package/src/runtime/view_widget_text.zig +1 -1
  142. package/src/runtime/view_widget_tree.zig +110 -13
  143. package/src/runtime/widget_bridge.zig +2 -0
  144. package/src/runtime/window_command_bridge_tests.zig +86 -0
  145. package/src/runtime/window_storage.zig +16 -1
  146. package/src/runtime/window_views.zig +1 -0
  147. package/src/tooling/ios.zig +3 -0
  148. package/src/tooling/manifest.zig +90 -2
  149. package/src/tooling/package.zig +67 -2
  150. package/src/tooling/raw_manifest.zig +10 -0
  151. package/src/tooling/templates.zig +128 -18
  152. package/src/tooling/verbs.zig +1 -0
  153. package/src/tooling/web_engine.zig +1 -1
  154. package/tools/corewire/emit.zig +31 -12
  155. package/tools/corewire/emit_facade.zig +57 -2
  156. package/tools/corewire/shim_rt.zig +20 -0
@@ -28,10 +28,11 @@
28
28
 
29
29
  const std = @import("std");
30
30
  const canvas = @import("canvas");
31
- const canvas_limits = @import("canvas_limits.zig");
32
31
  const automation_protocol = @import("../automation/protocol.zig");
32
+ const canvas_limits = @import("canvas_limits.zig");
33
33
  const core = @import("core.zig");
34
34
  const journal = @import("session_journal.zig");
35
+ const platform = @import("../platform/root.zig");
35
36
  const runtime_effects = @import("effects.zig");
36
37
  const session_blobs = @import("session_blobs.zig");
37
38
 
@@ -58,8 +59,8 @@ pub const ReplayError = error{
58
59
  /// can never produce (an image record claiming `.loaded` with a
59
60
  /// zero-length blob: the recorder journals `.loaded` only after the
60
61
  /// bytes decoded and registered, and empty bytes cannot decode; or
61
- /// decoded dimensions the canvas registry would never have accepted
62
- /// — zero or over-budget on `.loaded`, nonzero on any other
62
+ /// decoded dimensions no conforming recording could have accepted
63
+ /// — zero or over the SDK hard ceiling on `.loaded`, nonzero on any other
63
64
  /// outcome) — the journal is damaged or hand-edited.
64
65
  /// `JournalCorrupt` is the structural sibling (payloads that fail
65
66
  /// to decode at all); this class is for records that decode fine
@@ -206,9 +207,8 @@ pub fn replaySession(
206
207
  // Decoded dimensions obey the recorder the same way: a
207
208
  // live `.loaded` journals only after the canvas registry
208
209
  // accepted the pixels (nonzero width and height, and
209
- // width * height * 4 bytes within
210
- // `max_registered_canvas_image_pixel_bytes` — the same
211
- // bound `registerCanvasImage` enforces), and every other
210
+ // width * height * 4 bytes within that recording's
211
+ // runtime-frozen app image budget), and every other
212
212
  // outcome journals 0x0. Refuse out-of-bound dims HERE:
213
213
  // the fed values flow verbatim into the app's Msg — and,
214
214
  // on the TS core host, through `@intCast` into
@@ -251,7 +251,7 @@ pub fn replaySession(
251
251
  }
252
252
  if (effect.kind == .image and imageDimsDamaged(effect)) {
253
253
  std.debug.print(
254
- "replay refused after event {d}: image record for id {d} claims .{s} with dimensions {d}x{d} - a recorded .loaded always carries nonzero decoded dimensions within the registered-image pixel budget and every other outcome records 0x0, so the journal is damaged or hand-edited; re-record the session\n",
254
+ "replay refused after event {d}: image record for id {d} claims .{s} with dimensions {d}x{d} - a recorded .loaded always carries nonzero decoded dimensions within the SDK's registered-image ceiling and every other outcome records 0x0, so the journal is damaged or hand-edited; re-record the session\n",
255
255
  .{ report.events_replayed, effect.key, @tagName(effect.image_outcome), effect.image_width, effect.image_height },
256
256
  );
257
257
  return error.ReplayDamagedRecord;
@@ -753,9 +753,14 @@ fn imageDimsDamaged(record: journal.EffectResultRecord) bool {
753
753
  return record.image_width != 0 or record.image_height != 0;
754
754
  }
755
755
  if (record.image_width == 0 or record.image_height == 0) return true;
756
+ if (record.image_width > platform.max_decoded_image_dimension or record.image_height > platform.max_decoded_image_dimension) return true;
756
757
  const pixels = std.math.mul(u64, record.image_width, record.image_height) catch return true;
757
758
  const pixel_bytes = std.math.mul(u64, pixels, 4) catch return true;
758
- return pixel_bytes > canvas_limits.max_registered_canvas_image_pixel_bytes;
759
+ // The journal does not carry the recording's manifest budget. Validate
760
+ // against the SDK-wide ceiling: a result above the replay runtime's
761
+ // current (possibly lowered) budget is still valid recorder truth. Its
762
+ // Msg feeds verbatim; best-effort pixel re-registration may fit smaller.
763
+ return pixel_bytes > canvas_limits.max_registered_canvas_image_pixel_bytes_ceiling;
759
764
  }
760
765
 
761
766
  /// Journaled results that regenerate deterministically from the
@@ -12,6 +12,7 @@ const app_manifest = @import("app_manifest");
12
12
  const core = @import("core.zig");
13
13
  const ui_app_mod = @import("ui_app.zig");
14
14
  const effects_mod = @import("effects.zig");
15
+ const canvas_limits = @import("canvas_limits.zig");
15
16
  const platform = @import("../platform/root.zig");
16
17
  const journal = @import("session_journal.zig");
17
18
  const session_record = @import("session_record.zig");
@@ -1788,6 +1789,104 @@ test "image loads record into the blob store (deduplicated) and replay byte-iden
1788
1789
  try std.testing.expect(harness.runtime.registeredCanvasImage(23) == null);
1789
1790
  }
1790
1791
 
1792
+ test "raised-budget photo load replays recorded dimensions under a smaller current budget" {
1793
+ const gpa = std.testing.allocator;
1794
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
1795
+ defer std.heap.page_allocator.destroy(buffer);
1796
+ buffer.len = 0;
1797
+ var store = session_blobs.MemoryBlobStore.init(gpa);
1798
+ defer store.deinit();
1799
+
1800
+ const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
1801
+ defer std.heap.page_allocator.destroy(recorder);
1802
+ recorder.* = session_record.SessionRecorder.init(buffer.sink());
1803
+ recorder.blob_sink = store.sink();
1804
+ recorder.begin(.{ .platform_name = "test", .app_name = "image-session-demo", .window_width = 400, .window_height = 300 });
1805
+
1806
+ const record_harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1807
+ defer record_harness.destroy(gpa);
1808
+ record_harness.null_platform.gpu_surfaces = true;
1809
+ record_harness.null_platform.image_decode = true;
1810
+ core.Runtime.initAt(&record_harness.runtime, .{
1811
+ .platform = record_harness.null_platform.platform(),
1812
+ .trace_sink = record_harness.trace_sink.sink(),
1813
+ .max_image_pixel_bytes = canvas_limits.max_registered_canvas_image_pixel_bytes_ceiling,
1814
+ .environ = std.testing.environ,
1815
+ .session_recorder = recorder,
1816
+ });
1817
+ record_harness.runtime.dispatch_error_policy = .propagate;
1818
+
1819
+ const record_app = try gpa.create(ImageSessionApp);
1820
+ defer gpa.destroy(record_app);
1821
+ record_app.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
1822
+ defer record_app.deinit();
1823
+ record_app.effects.executor = .fake;
1824
+ const record_runtime_app = record_app.app();
1825
+ try record_harness.start(record_runtime_app);
1826
+ try record_harness.runtime.dispatchPlatformEvent(record_runtime_app, .{ .gpu_surface_frame = .{
1827
+ .label = image_canvas_label,
1828
+ .size = geometry.SizeF.init(400, 300),
1829
+ .scale_factor = 1,
1830
+ .frame_index = 1,
1831
+ .timestamp_ns = 1_000_000,
1832
+ } });
1833
+
1834
+ const width: usize = 640;
1835
+ const height: usize = 480;
1836
+ const pixels = try gpa.alloc(u8, width * height * 4);
1837
+ defer gpa.free(pixels);
1838
+ var pixel_offset: usize = 0;
1839
+ while (pixel_offset < pixels.len) : (pixel_offset += 4) pixels[pixel_offset..][0..4].* = .{ 31, 97, 211, 255 };
1840
+ const encoded_len = try canvas.png.encodedRgba8ByteLen(width, height);
1841
+ const encoded = try gpa.alloc(u8, encoded_len);
1842
+ defer gpa.free(encoded);
1843
+ var writer = std.Io.Writer.fixed(encoded);
1844
+ try canvas.png.writeRgba8(&writer, width, height, pixels);
1845
+
1846
+ try record_harness.runtime.dispatchPlatformEvent(record_runtime_app, .{ .menu_command = .{ .name = "image.cover", .window_id = 1 } });
1847
+ try record_app.effects.feedImageBytes(21, writer.buffered());
1848
+ try record_harness.runtime.dispatchPlatformEvent(record_runtime_app, .wake);
1849
+ try record_harness.runtime.dispatchPlatformEvent(record_runtime_app, .frame_requested);
1850
+ const recorded_model = record_app.model;
1851
+ const recorded_fingerprint = record_harness.runtime.sessionStateFingerprint();
1852
+ const recorded_pixels = record_harness.runtime.registeredCanvasImages()[0].pixels;
1853
+ const recorded_pixel_len = recorded_pixels.len;
1854
+ try std.testing.expectEqual(width, recorded_model.last_width);
1855
+ try std.testing.expectEqual(height, recorded_model.last_height);
1856
+ recorder.finish();
1857
+ try std.testing.expect(!recorder.failed);
1858
+
1859
+ const replay_harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
1860
+ defer replay_harness.destroy(gpa);
1861
+ replay_harness.null_platform.gpu_surfaces = true;
1862
+ core.Runtime.initAt(&replay_harness.runtime, .{
1863
+ .platform = replay_harness.null_platform.platform(),
1864
+ .trace_sink = replay_harness.trace_sink.sink(),
1865
+ .environ = std.testing.environ,
1866
+ });
1867
+ replay_harness.runtime.dispatch_error_policy = .propagate;
1868
+ platform.installHeadlessImageCodec("null", &replay_harness.null_platform, &replay_harness.runtime.options.platform.services);
1869
+ const replay_app = try gpa.create(ImageSessionApp);
1870
+ defer gpa.destroy(replay_app);
1871
+ replay_app.* = ImageSessionApp.init(std.heap.page_allocator, .{}, imageSessionOptions());
1872
+ defer replay_app.deinit();
1873
+
1874
+ const report = try session_replay.replaySession(&replay_harness.runtime, replay_app.app(), buffer.journalBytes(), .{
1875
+ .verify = true,
1876
+ .require_same_platform = false,
1877
+ .blobs = store.source(),
1878
+ });
1879
+ try std.testing.expect(report.ok());
1880
+ try std.testing.expectEqualDeep(recorded_model, replay_app.model);
1881
+ try std.testing.expectEqual(recorded_fingerprint, replay_harness.runtime.sessionStateFingerprint());
1882
+ const replay_info = replay_harness.runtime.registeredCanvasImage(21).?;
1883
+ try std.testing.expectEqual(recorded_model.last_width, replay_app.model.last_width);
1884
+ try std.testing.expectEqual(recorded_model.last_height, replay_app.model.last_height);
1885
+ try std.testing.expect(replay_info.width * replay_info.height * 4 <= replay_harness.runtime.max_image_pixel_bytes);
1886
+ try std.testing.expect(replay_info.width < width or replay_info.height < height);
1887
+ try std.testing.expect(recorded_pixel_len != replay_harness.runtime.registeredCanvasImages()[0].pixels.len);
1888
+ }
1889
+
1791
1890
  /// Record the undelivered-window reference session: load id 21, feed
1792
1891
  /// its bytes (terminal queued, slot `.draining`, delivery still
1793
1892
  /// ahead), then — before any drain — an UNRELATED dispatch loads id
@@ -3080,10 +3179,18 @@ test "an image record claiming dimensions on a non-loaded outcome refuses replay
3080
3179
  try std.testing.expectError(error.ReplayDamagedRecord, result);
3081
3180
  }
3082
3181
 
3083
- test "an image record claiming .loaded past the pixel budget refuses replay as damage" {
3084
- // 1024x1024 RGBA8 is 4 MiB - past the registered-image slot bound
3085
- // a live .loaded can never exceed.
3086
- const result = replayWithPatchedImageDims(.loaded, 1024, 1024);
3182
+ test "an image record claiming .loaded past the SDK ceiling refuses replay as damage" {
3183
+ // 2048x2048 RGBA8 is 16 MiB - past the largest registered-image slot
3184
+ // any conforming recording can accept.
3185
+ const result = replayWithPatchedImageDims(.loaded, 2048, 2048);
3186
+ try std.testing.expectError(error.ReplayDamagedRecord, result);
3187
+ }
3188
+
3189
+ test "an image record claiming .loaded past the decoded-axis ceiling refuses replay as damage" {
3190
+ // Source metadata may exceed this ceiling, but every conforming host fits
3191
+ // it before registration and therefore before these result dimensions are
3192
+ // journaled.
3193
+ const result = replayWithPatchedImageDims(.loaded, platform.max_decoded_image_dimension + 1, 1);
3087
3194
  try std.testing.expectError(error.ReplayDamagedRecord, result);
3088
3195
  }
3089
3196
 
@@ -278,6 +278,18 @@ test "runtime materializes manifest shell windows into laid out views" {
278
278
  const window = try harness.runtime.createShellWindow(shell_window, platform.WebViewSource.html("<h1>Shell</h1>"));
279
279
  try std.testing.expectEqual(@as(platform.WindowId, 2), window.id);
280
280
  try std.testing.expectEqualStrings("shell", window.label);
281
+ try std.testing.expectEqual(platform.WindowInitialPlacement.default, harness.null_platform.window_placement[1]);
282
+ try std.testing.expectEqual(platform.WindowRestorePolicy.center_on_primary, harness.null_platform.window_restore_policy[1]);
283
+
284
+ const positioned = try harness.runtime.createShellWindow(.{
285
+ .label = "positioned",
286
+ .x = 96,
287
+ .y = 144,
288
+ .views = &.{},
289
+ }, platform.WebViewSource.html("<h1>Positioned</h1>"));
290
+ try std.testing.expectEqual(@as(f32, 96), positioned.frame.x);
291
+ try std.testing.expectEqual(@as(f32, 144), positioned.frame.y);
292
+ try std.testing.expectEqual(platform.WindowInitialPlacement.explicit, harness.null_platform.window_placement[2]);
281
293
 
282
294
  var views_buffer: [13]platform.ViewInfo = undefined;
283
295
  const views = harness.runtime.listViews(window.id, &views_buffer);
@@ -12,6 +12,7 @@ const std = @import("std");
12
12
  const effects_mod = @import("effects.zig");
13
13
  const runtime_clock = @import("clock.zig");
14
14
  const ts_core_host = @import("ts_core_host.zig");
15
+ const platform = @import("../platform/root.zig");
15
16
 
16
17
  // ------------------------------------------------------ the mini core
17
18
  //
@@ -338,6 +339,7 @@ const mini_core = struct {
338
339
  open_save_sink, // 102: write_file_stream "save" -> wrote/failed
339
340
  write_save_chunk, // 103: write_file_chunk "save" -> wrote/failed
340
341
  close_save_sink, // 104: write_file_close "save" -> wrote/failed
342
+ dock_on, // 105: dock_presence true
341
343
  };
342
344
 
343
345
  const stream_fill_keys = [_][]const u8{
@@ -686,6 +688,7 @@ const mini_core = struct {
686
688
  .open_win => return .{ .model = model, .cmd = cmdWindowShow("player") },
687
689
  .hide_win => return .{ .model = model, .cmd = cmdWindowHide("player") },
688
690
  .dock_off => return .{ .model = model, .cmd = cmdDockPresence(false) },
691
+ .dock_on => return .{ .model = model, .cmd = cmdDockPresence(true) },
689
692
  .quit_app => return .{ .model = model, .cmd = cmdQuitApp() },
690
693
  .open_chan => return .{ .model = model, .cmd = cmdChannelOpen(41, 47) },
691
694
  .close_chan => return .{ .model = model, .cmd = cmdChannelClose(41) },
@@ -2842,6 +2845,50 @@ test "window verbs bridge to the effects channel's label-addressed verbs" {
2842
2845
  try std.testing.expectEqual(boot_pending, fx.pendingHostCount());
2843
2846
  }
2844
2847
 
2848
+ test "an accessory launch composes with a TypeScript dock-presence promotion" {
2849
+ var null_platform = platform.NullPlatform.initWithOptions(.{}, .system, .{
2850
+ .app_name = "Menu Bar",
2851
+ .dock_visible = false,
2852
+ });
2853
+ try std.testing.expect(!null_platform.dock_visible);
2854
+
2855
+ const Actions = struct {
2856
+ fn window(_: *anyopaque, _: []const u8) bool {
2857
+ return true;
2858
+ }
2859
+
2860
+ fn dock(context: *anyopaque, visible: bool) bool {
2861
+ const host: *platform.NullPlatform = @ptrCast(@alignCast(context));
2862
+ host.platform().services.setDockPresence(visible) catch return false;
2863
+ return true;
2864
+ }
2865
+
2866
+ fn quit(_: *anyopaque) bool {
2867
+ return true;
2868
+ }
2869
+ };
2870
+
2871
+ const fx = freshChannel();
2872
+ defer fx.deinit();
2873
+ Host.init(fx);
2874
+ fx.bindWindowActions(.{
2875
+ .context = &null_platform,
2876
+ .close_fn = Actions.window,
2877
+ .minimize_fn = Actions.window,
2878
+ .hide_fn = Actions.window,
2879
+ .show_fn = Actions.window,
2880
+ .dock_presence_fn = Actions.dock,
2881
+ .quit_fn = Actions.quit,
2882
+ });
2883
+ fx.executor = .real;
2884
+
2885
+ Host.dispatch(fx, .dock_on);
2886
+ try std.testing.expect(fx.windowActionState().dock_visible);
2887
+ try std.testing.expectEqual(@as(u32, 1), fx.windowActionState().dock_presence_count);
2888
+ try std.testing.expect(null_platform.dock_visible);
2889
+ try std.testing.expectEqual(@as(u32, 1), null_platform.dock_presence_count);
2890
+ }
2891
+
2845
2892
  test "a channel opens, posts route the five-field arm by name, and close retires the key" {
2846
2893
  const fx = freshChannel();
2847
2894
  defer fx.deinit();
@@ -44,7 +44,8 @@
44
44
  //! the committed model. One model-helper convention joins that wiring:
45
45
  //! an exported `themePack(model): "house" | "geist"` helper selects the
46
46
  //! stock pack live through `theme_fn`, without taking ownership of the
47
- //! system appearance axes. An exported
47
+ //! system appearance axes; `themeState(model)` subsumes it with scheme and
48
+ //! accent axes through `theme_state_fn`. An exported
48
49
  //! `statusItem(model): StatusItemState` helper similarly owns one complete
49
50
  //! menu-bar item through `status_item_fn`; `statusItems(model)` owns a keyed
50
51
  //! collection through `status_items_fn`. Both keep shell, presentation, and
@@ -80,6 +81,27 @@ const ts_core_host = @import("ts_core_host.zig");
80
81
 
81
82
  const ts_ui_app_log = std.log.scoped(.zero_ts_ui_app);
82
83
 
84
+ /// Quota for a comptime scan of an app-authored type. TS `Msg` unions may
85
+ /// legally carry 256 arms; include total identifier bytes because
86
+ /// `std.mem.eql`'s comptime scalar path scales with the compared names.
87
+ fn typeScanQuota(comptime T: type) u32 {
88
+ const fields = switch (@typeInfo(T)) {
89
+ .@"struct" => |info| info.fields,
90
+ .@"union" => |info| info.fields,
91
+ .@"enum" => |info| info.fields,
92
+ else => return 2_000,
93
+ };
94
+ var name_bytes: u64 = 0;
95
+ for (fields) |field| name_bytes += field.name.len;
96
+ const quota: u64 = 100_000 + @as(u64, fields.len) * 1_024 + name_bytes * 256;
97
+ return @intCast(@min(quota, std.math.maxInt(u32)));
98
+ }
99
+
100
+ fn scaledTypeScanQuota(comptime T: type, comptime scans: usize) u32 {
101
+ const quota = @as(u64, typeScanQuota(T)) * @max(scans, 1);
102
+ return @intCast(@min(quota, std.math.maxInt(u32)));
103
+ }
104
+
83
105
  pub fn TsUiApp(comptime core: type) type {
84
106
  return struct {
85
107
  /// The effect bridge — shared with any direct `TsCoreHost(core)`
@@ -92,6 +114,9 @@ pub fn TsUiApp(comptime core: type) type {
92
114
  pub const Options = App.Options;
93
115
  pub const Effects = App.Effects;
94
116
  pub const Ui = App.Ui;
117
+ /// Shared with generated launchers that perform their own Msg scans.
118
+ pub const msg_scan_quota = typeScanQuota(Msg);
119
+ pub const persist_route_scan_quota = scaledTypeScanQuota(Msg, 3);
95
120
 
96
121
  /// Internal keyed-channel namespace for persistence write failures
97
122
  /// ("TSPR"). It never shares an app-authored TS bridge index.
@@ -223,12 +248,14 @@ pub fn TsUiApp(comptime core: type) type {
223
248
  /// a typo or payload mismatch fails during `native build`, before a
224
249
  /// first boot can reach the dynamic dispatch path below.
225
250
  pub fn validatePersistRoutes(comptime routes: PersistRoutes) void {
251
+ @setEvalBranchQuota(persist_route_scan_quota);
226
252
  validatePersistRoute(routes.ok, void, "ok");
227
253
  validatePersistRoute(routes.none, void, "none");
228
254
  validatePersistRoute(routes.err, []const u8, "err");
229
255
  }
230
256
 
231
257
  fn validatePersistRoute(comptime route: []const u8, comptime Payload: type, comptime role: []const u8) void {
258
+ @setEvalBranchQuota(msg_scan_quota);
232
259
  inline for (@typeInfo(Msg).@"union".fields) |arm| {
233
260
  if (comptime std.mem.eql(u8, arm.name, route)) {
234
261
  if (arm.type != Payload) {
@@ -379,12 +406,22 @@ pub fn TsUiApp(comptime core: type) type {
379
406
  // external-core mirror. The app owns only the pack; UiApp's
380
407
  // stock-token path keeps following the OS appearance.
381
408
  if (comptime @hasDecl(Model, "themePack")) {
409
+ if (comptime @hasDecl(Model, "themeState")) {
410
+ @compileError("TsUiApp: export either themePack or themeState, not both");
411
+ }
382
412
  if (options.theme_fn != null) {
383
413
  @panic("TsUiApp wires theme_fn from the core's themePack helper - remove the wiring's theme_fn");
384
414
  }
385
415
  comptime validateThemePackHelper();
386
416
  stamped.theme_fn = themePackAdapter;
387
417
  }
418
+ if (comptime @hasDecl(Model, "themeState")) {
419
+ if (options.theme_state_fn != null or options.theme_fn != null) {
420
+ @panic("TsUiApp wires theme_state_fn from the core's themeState helper - remove the wiring's theme_state_fn/theme_fn");
421
+ }
422
+ comptime validateThemeStateHelper();
423
+ stamped.theme_state_fn = themeStateAdapter;
424
+ }
388
425
  // A statusItem helper is the TS app's model-derived shell
389
426
  // declaration. UiApp installs it on the first frame and
390
427
  // independently patches shell/presentation/menu after each
@@ -503,6 +540,82 @@ pub fn TsUiApp(comptime core: type) type {
503
540
  }
504
541
  }
505
542
 
543
+ fn themeStateAdapter(model: *const Model) App.ThemeState {
544
+ const params = @typeInfo(@TypeOf(Model.themeState)).@"fn".params;
545
+ const raw_state = if (comptime params.len == 1)
546
+ model.themeState()
547
+ else
548
+ model.themeState(core.rt.frameAllocator());
549
+ const state = if (comptime @typeInfo(@TypeOf(raw_state)) == .pointer) raw_state.* else raw_state;
550
+ const accent = if (state.accent) |value| parseThemeAccent(value) else null;
551
+ return .{
552
+ .pack = if (state.pack) |pack| canvas.ThemePack.fromName(@tagName(pack)).? else null,
553
+ .color_scheme = if (state.colorScheme) |scheme| themeColorScheme(scheme) else .system,
554
+ .accent = accent,
555
+ .invalid_accent = if (state.accent != null and accent == null) state.accent else null,
556
+ };
557
+ }
558
+
559
+ fn themeColorScheme(value: anytype) App.ThemeColorScheme {
560
+ const name = @tagName(value);
561
+ inline for (std.meta.fields(App.ThemeColorScheme)) |field| {
562
+ if (std.mem.eql(u8, name, field.name)) return @enumFromInt(field.value);
563
+ }
564
+ unreachable;
565
+ }
566
+
567
+ fn parseThemeAccent(value: []const u8) ?canvas.Color {
568
+ if (value.len != 7 or value[0] != '#') return null;
569
+ const r = themeHexByte(value[1], value[2]) orelse return null;
570
+ const g = themeHexByte(value[3], value[4]) orelse return null;
571
+ const b = themeHexByte(value[5], value[6]) orelse return null;
572
+ return canvas.Color.rgb8(r, g, b);
573
+ }
574
+
575
+ fn themeHexByte(hi: u8, lo: u8) ?u8 {
576
+ const h = themeHexNibble(hi) orelse return null;
577
+ const l = themeHexNibble(lo) orelse return null;
578
+ return h * 16 + l;
579
+ }
580
+
581
+ fn themeHexNibble(byte: u8) ?u8 {
582
+ return switch (byte) {
583
+ '0'...'9' => byte - '0',
584
+ 'a'...'f' => byte - 'a' + 10,
585
+ 'A'...'F' => byte - 'A' + 10,
586
+ else => null,
587
+ };
588
+ }
589
+
590
+ fn validateThemeStateHelper() void {
591
+ const teaching = "TsUiApp: themeState must be exported from core.ts as themeState(model: Model): ThemeState; import ThemeState from @native-sdk/core/events";
592
+ const helper_info = @typeInfo(@TypeOf(Model.themeState));
593
+ if (helper_info != .@"fn") @compileError(teaching);
594
+ const function = helper_info.@"fn";
595
+ if ((function.params.len != 1 and function.params.len != 2) or function.params[0].type == null or function.params[0].type.? != *const Model) {
596
+ @compileError(teaching);
597
+ }
598
+ if (function.params.len == 2) {
599
+ if (function.params[1].type == null or function.params[1].type.? != std.mem.Allocator or
600
+ !@hasDecl(core, "rt") or !@hasDecl(core.rt, "frameAllocator"))
601
+ {
602
+ @compileError(teaching);
603
+ }
604
+ }
605
+ const RawState = function.return_type orelse @compileError(teaching);
606
+ const State = statusItemRecordType(RawState, teaching);
607
+ const info = @typeInfo(State).@"struct";
608
+ if (info.fields.len != 3 or !@hasField(State, "pack") or !@hasField(State, "colorScheme") or !@hasField(State, "accent")) {
609
+ @compileError(teaching);
610
+ }
611
+ if (!optionalEnumType(@FieldType(State, "pack"), &.{ "house", "geist" }) or
612
+ !optionalEnumType(@FieldType(State, "colorScheme"), &.{ "light", "dark", "system" }) or
613
+ @FieldType(State, "accent") != ?[]const u8)
614
+ {
615
+ @compileError(teaching);
616
+ }
617
+ }
618
+
506
619
  /// Convert the compiled core's canonical status-item records into
507
620
  /// the platform rows UiApp already knows how to validate, copy,
508
621
  /// hash, install, and patch. Interface records cross the core ABI
@@ -930,6 +1043,11 @@ pub fn TsUiApp(comptime core: type) type {
930
1043
  return info == .optional and statusItemNumericType(info.optional.child);
931
1044
  }
932
1045
 
1046
+ fn optionalEnumType(comptime T: type, comptime expected: []const []const u8) bool {
1047
+ const info = @typeInfo(T);
1048
+ return info == .optional and statusItemEnumType(info.optional.child, expected);
1049
+ }
1050
+
933
1051
  fn statusItemNumericType(comptime T: type) bool {
934
1052
  return @typeInfo(T) == .int or @typeInfo(T) == .float;
935
1053
  }
@@ -1029,6 +1147,7 @@ pub fn TsUiApp(comptime core: type) type {
1029
1147
  }
1030
1148
 
1031
1149
  fn persistOutcomeMsg(event: runtime_effects.EffectChannelEvent) Msg {
1150
+ @setEvalBranchQuota(msg_scan_quota);
1032
1151
  const reason: []const u8 = switch (event.kind) {
1033
1152
  .data => event.bytes,
1034
1153
  .rejected => "rejected",
@@ -1044,6 +1163,7 @@ pub fn TsUiApp(comptime core: type) type {
1044
1163
  }
1045
1164
 
1046
1165
  fn dispatchPersistVoid(fx: *Effects, route: []const u8) void {
1166
+ @setEvalBranchQuota(msg_scan_quota);
1047
1167
  inline for (@typeInfo(Msg).@"union".fields) |arm| {
1048
1168
  if (comptime arm.type == void) {
1049
1169
  if (std.mem.eql(u8, arm.name, route)) {
@@ -1056,6 +1176,7 @@ pub fn TsUiApp(comptime core: type) type {
1056
1176
  }
1057
1177
 
1058
1178
  fn dispatchPersistError(fx: *Effects, route: []const u8, reason: []const u8) void {
1179
+ @setEvalBranchQuota(msg_scan_quota);
1059
1180
  inline for (@typeInfo(Msg).@"union".fields) |arm| {
1060
1181
  if (comptime arm.type == []const u8) {
1061
1182
  if (std.mem.eql(u8, arm.name, route)) {
@@ -1098,6 +1219,7 @@ pub fn TsUiApp(comptime core: type) type {
1098
1219
  /// One env delivery: resolve the arm by name and dispatch the
1099
1220
  /// value through a full core cycle.
1100
1221
  fn dispatchOneEnvValue(fx: *Effects, msg: []const u8, value: []const u8) void {
1222
+ @setEvalBranchQuota(msg_scan_quota);
1101
1223
  inline for (@typeInfo(Msg).@"union".fields) |arm| {
1102
1224
  if (comptime arm.type == []const u8) {
1103
1225
  if (std.mem.eql(u8, arm.name, msg)) {
@@ -1117,6 +1239,7 @@ pub fn TsUiApp(comptime core: type) type {
1117
1239
  /// hand-assembled cores: every `envMsgs` entry must name a Msg
1118
1240
  /// arm carrying exactly one bytes payload.
1119
1241
  fn validateEnvMsgs() void {
1242
+ @setEvalBranchQuota(scaledTypeScanQuota(Msg, core.envMsgs.len));
1120
1243
  for (core.envMsgs) |entry| {
1121
1244
  var found = false;
1122
1245
  for (@typeInfo(Msg).@"union".fields) |arm| {
@@ -1333,6 +1456,7 @@ pub fn TsUiApp(comptime core: type) type {
1333
1456
  /// error the frontend's NS1033 re-derives for hand-written
1334
1457
  /// cores.
1335
1458
  fn channelArmIndex(comptime tag: []const u8, comptime channel: []const u8) usize {
1459
+ @setEvalBranchQuota(msg_scan_quota);
1336
1460
  for (@typeInfo(Msg).@"union".fields, 0..) |arm, index| {
1337
1461
  if (std.mem.eql(u8, arm.name, tag)) return index;
1338
1462
  }
@@ -1551,6 +1675,45 @@ const WindowsAdapterTestCore = struct {
1551
1675
  };
1552
1676
  };
1553
1677
 
1678
+ const ThemeStateAdapterTestCore = struct {
1679
+ const Pack = enum { house, geist };
1680
+ const Scheme = enum { light, dark, system };
1681
+ const State = struct {
1682
+ pack: ?Pack,
1683
+ colorScheme: ?Scheme,
1684
+ accent: ?[]const u8,
1685
+ };
1686
+
1687
+ pub const Msg = union(enum) { noop };
1688
+ pub const Model = struct {
1689
+ bad: bool = false,
1690
+
1691
+ pub fn themeState(self: *const Model) State {
1692
+ return .{
1693
+ .pack = .geist,
1694
+ .colorScheme = .dark,
1695
+ .accent = if (self.bad) "hot-pink" else "#Df2670",
1696
+ };
1697
+ }
1698
+ };
1699
+ };
1700
+
1701
+ test "TypeScript themeState adapter validates, parses hex, and preserves malformed accent teaching" {
1702
+ const Adapter = TsUiApp(ThemeStateAdapterTestCore);
1703
+ comptime Adapter.validateThemeStateHelper();
1704
+ var model = ThemeStateAdapterTestCore.Model{};
1705
+ var state = Adapter.themeStateAdapter(&model);
1706
+ try std.testing.expectEqual(canvas.ThemePack.geist, state.pack.?);
1707
+ try std.testing.expectEqual(Adapter.App.ThemeColorScheme.dark, state.color_scheme);
1708
+ try std.testing.expectEqual(canvas.Color.rgb8(0xdf, 0x26, 0x70), state.accent.?);
1709
+ try std.testing.expect(state.invalid_accent == null);
1710
+
1711
+ model.bad = true;
1712
+ state = Adapter.themeStateAdapter(&model);
1713
+ try std.testing.expect(state.accent == null);
1714
+ try std.testing.expectEqualStrings("hot-pink", state.invalid_accent.?);
1715
+ }
1716
+
1554
1717
  test "TypeScript windows adapter keeps the declared prefix on overflow and projects chromeless" {
1555
1718
  const Adapter = TsUiApp(WindowsAdapterTestCore);
1556
1719
  comptime Adapter.validateWindowsHelper();