@native-sdk/cli 0.5.4 → 0.6.1

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 (149) hide show
  1. package/build/app.zig +50 -1
  2. package/build.zig +588 -32
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +3 -1
  5. package/package.json +9 -9
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/rt/rt.zig +280 -8
  9. package/packages/core/sdk/core.ts +481 -2
  10. package/packages/core/sdk/events.ts +17 -8
  11. package/packages/core/src/checker.ts +305 -2
  12. package/packages/core/src/diagnostics.ts +12 -0
  13. package/packages/core/src/emitter.ts +599 -3
  14. package/packages/core/src/typed_ast.ts +51 -4
  15. package/packages/core/src/types.ts +34 -1
  16. package/skill-data/native-ui/SKILL.md +39 -2
  17. package/skill-data/ts-core/SKILL.md +4 -2
  18. package/src/app_runner/root.zig +32 -10
  19. package/src/automation/layout_fingerprint.zig +160 -0
  20. package/src/automation/protocol.zig +82 -33
  21. package/src/automation/root.zig +1 -0
  22. package/src/automation/server.zig +1 -1
  23. package/src/automation/snapshot.zig +68 -9
  24. package/src/automation/watcher.zig +1 -1
  25. package/src/embed/host.zig +5 -0
  26. package/src/embed/ui_host.zig +13 -0
  27. package/src/platform/linux/gtk_host.c +527 -13
  28. package/src/platform/linux/gtk_host.h +25 -0
  29. package/src/platform/linux/root.zig +280 -7
  30. package/src/platform/macos/appkit_host.h +104 -6
  31. package/src/platform/macos/appkit_host.m +1368 -74
  32. package/src/platform/macos/cef_host.mm +110 -1
  33. package/src/platform/macos/root.zig +287 -7
  34. package/src/platform/null_platform.zig +398 -4
  35. package/src/platform/null_platform_tests.zig +35 -0
  36. package/src/platform/root.zig +9 -0
  37. package/src/platform/types.zig +386 -14
  38. package/src/platform/windows/root.zig +298 -9
  39. package/src/platform/windows/webview2_host.cpp +330 -5
  40. package/src/primitives/app_manifest/validation.zig +18 -0
  41. package/src/primitives/canvas/a11y_audit.zig +33 -11
  42. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  43. package/src/primitives/canvas/drawing.zig +25 -0
  44. package/src/primitives/canvas/equality.zig +8 -0
  45. package/src/primitives/canvas/events.zig +103 -16
  46. package/src/primitives/canvas/frame.zig +125 -1
  47. package/src/primitives/canvas/layout_audit.zig +22 -5
  48. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  49. package/src/primitives/canvas/render_tests.zig +13 -10
  50. package/src/primitives/canvas/root.zig +48 -0
  51. package/src/primitives/canvas/terminal_box.zig +471 -0
  52. package/src/primitives/canvas/terminal_grid.zig +978 -0
  53. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  54. package/src/primitives/canvas/test_support.zig +1 -0
  55. package/src/primitives/canvas/tests.zig +1 -0
  56. package/src/primitives/canvas/text_tests.zig +39 -4
  57. package/src/primitives/canvas/tokens.zig +115 -12
  58. package/src/primitives/canvas/ui.zig +524 -5
  59. package/src/primitives/canvas/ui_markup.zig +195 -2
  60. package/src/primitives/canvas/ui_markup_compiled.zig +172 -1
  61. package/src/primitives/canvas/ui_markup_compiled_tests.zig +133 -8
  62. package/src/primitives/canvas/ui_markup_contract.zig +88 -5
  63. package/src/primitives/canvas/ui_markup_contract_tests.zig +39 -1
  64. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  65. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  66. package/src/primitives/canvas/ui_markup_tests.zig +149 -0
  67. package/src/primitives/canvas/ui_markup_view.zig +191 -0
  68. package/src/primitives/canvas/ui_markup_view_tests.zig +403 -19
  69. package/src/primitives/canvas/ui_schema.zig +97 -17
  70. package/src/primitives/canvas/ui_schema_tests.zig +18 -11
  71. package/src/primitives/canvas/ui_tests.zig +141 -9
  72. package/src/primitives/canvas/widget_access.zig +28 -2
  73. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  74. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  75. package/src/primitives/canvas/widget_layout.zig +59 -11
  76. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  77. package/src/primitives/canvas/widget_render.zig +272 -18
  78. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  79. package/src/primitives/canvas/widget_routing.zig +59 -7
  80. package/src/primitives/canvas/widget_runtime.zig +23 -0
  81. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  82. package/src/primitives/canvas/widget_semantics.zig +119 -11
  83. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  84. package/src/primitives/canvas/widget_tree.zig +17 -0
  85. package/src/primitives/canvas/widgets.zig +132 -0
  86. package/src/root.zig +16 -0
  87. package/src/runtime/api.zig +76 -6
  88. package/src/runtime/automation_commands.zig +20 -1
  89. package/src/runtime/automation_snapshot.zig +25 -1
  90. package/src/runtime/automation_widget_dispatch.zig +73 -8
  91. package/src/runtime/bridge_payload.zig +1 -0
  92. package/src/runtime/canvas_frame.zig +62 -7
  93. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  94. package/src/runtime/canvas_frame_helpers.zig +190 -1
  95. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  96. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  97. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  98. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  99. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  100. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  101. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  102. package/src/runtime/canvas_widget_display.zig +1 -1
  103. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  104. package/src/runtime/canvas_widget_events.zig +268 -26
  105. package/src/runtime/canvas_widget_runtime.zig +213 -29
  106. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  107. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  108. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  109. package/src/runtime/canvas_widget_state.zig +6 -0
  110. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  111. package/src/runtime/core.zig +73 -2
  112. package/src/runtime/effects.zig +6389 -54
  113. package/src/runtime/effects_channel_tests.zig +3519 -0
  114. package/src/runtime/effects_pty_tests.zig +1362 -0
  115. package/src/runtime/effects_tests.zig +14 -9
  116. package/src/runtime/effects_video_tests.zig +3609 -0
  117. package/src/runtime/flow.zig +25 -4
  118. package/src/runtime/gpu_surface_events.zig +475 -15
  119. package/src/runtime/media_surface.zig +116 -11
  120. package/src/runtime/media_surface_tests.zig +61 -0
  121. package/src/runtime/pty.zig +1252 -0
  122. package/src/runtime/pty_windows.zig +1780 -0
  123. package/src/runtime/root.zig +25 -0
  124. package/src/runtime/runtime_event_tests.zig +20 -1
  125. package/src/runtime/session_journal.zig +367 -61
  126. package/src/runtime/session_record.zig +16 -0
  127. package/src/runtime/session_replay.zig +395 -11
  128. package/src/runtime/session_tests.zig +114 -19
  129. package/src/runtime/terminal_session.zig +1569 -0
  130. package/src/runtime/terminal_session_tests.zig +450 -0
  131. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  132. package/src/runtime/terminal_vt_stub.zig +13 -0
  133. package/src/runtime/tests.zig +6 -0
  134. package/src/runtime/ts_core_host.zig +980 -140
  135. package/src/runtime/ts_core_host_tests.zig +1059 -1
  136. package/src/runtime/ts_ui_app.zig +16 -5
  137. package/src/runtime/ui_app.zig +2338 -67
  138. package/src/runtime/ui_app_tests.zig +3171 -744
  139. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  140. package/src/runtime/ui_app_window_tests.zig +268 -0
  141. package/src/runtime/view.zig +190 -2
  142. package/src/runtime/view_widget_scroll.zig +218 -68
  143. package/src/runtime/view_widget_text.zig +11 -2
  144. package/src/runtime/view_widget_tree.zig +111 -3
  145. package/src/runtime/window_storage.zig +45 -2
  146. package/src/runtime/window_view_runtime.zig +34 -2
  147. package/src/runtime/window_views.zig +1 -0
  148. package/src/terminal_session_tests_root.zig +15 -0
  149. package/src/tooling/templates.zig +25 -7
@@ -29,6 +29,8 @@ typedef enum {
29
29
  NATIVE_SDK_GTK_EVENT_TIMER = 15,
30
30
  NATIVE_SDK_GTK_EVENT_APPEARANCE = 16,
31
31
  NATIVE_SDK_GTK_EVENT_AUDIO = 17,
32
+ NATIVE_SDK_GTK_EVENT_CONTEXT_MENU_ACTION = 18,
33
+ NATIVE_SDK_GTK_EVENT_VIEW_FOCUSED = 19,
32
34
  } native_sdk_gtk_event_kind_t;
33
35
 
34
36
  typedef struct {
@@ -89,8 +91,23 @@ typedef struct {
89
91
  * 50 Hz..16 kHz buckets, each linear-in-dB from -60 dBFS at 0 to
90
92
  * full scale at 255. All zeros on every other event kind. */
91
93
  uint8_t audio_bands[32];
94
+ /* CONTEXT_MENU_ACTION payload (the macOS host's field names):
95
+ * widget_id echoes the request's correlation token, menu_item_id is
96
+ * the selected item's id (0 = dismissed without a selection). */
97
+ uint64_t widget_id;
98
+ uint32_t menu_item_id;
92
99
  } native_sdk_gtk_event_t;
93
100
 
101
+ /* One context-menu entry crossing the C ABI (the same shape the macOS
102
+ * and Windows hosts take). */
103
+ typedef struct {
104
+ uint32_t item_id;
105
+ const char *label;
106
+ size_t label_len;
107
+ int enabled;
108
+ int separator;
109
+ } native_sdk_gtk_context_menu_item_t;
110
+
94
111
  typedef void (*native_sdk_gtk_event_callback_t)(void *context, const native_sdk_gtk_event_t *event);
95
112
  typedef void (*native_sdk_gtk_bridge_callback_t)(void *context, uint64_t window_id, const char *webview_label, size_t webview_label_len, const char *message, size_t message_len, const char *origin, size_t origin_len);
96
113
 
@@ -211,6 +228,14 @@ int native_sdk_gtk_set_view_visible(native_sdk_gtk_host_t *host, uint64_t window
211
228
  int native_sdk_gtk_focus_view(native_sdk_gtk_host_t *host, uint64_t window_id, const char *label, size_t label_len);
212
229
  int native_sdk_gtk_close_view(native_sdk_gtk_host_t *host, uint64_t window_id, const char *label, size_t label_len);
213
230
  int native_sdk_gtk_request_gpu_surface_frame(native_sdk_gtk_host_t *host, uint64_t window_id, const char *label, size_t label_len);
231
+ /* Present a native context menu (GtkPopoverMenu) pointing at the given
232
+ * view-local LOGICAL x/y — the same coordinate space the gpu-surface
233
+ * input path reports pointer events in, which is the pointing-rect space
234
+ * GTK expects (widget-local logical coordinates of the popover's
235
+ * parent). The selection (or dismissal) is reported asynchronously as a
236
+ * CONTEXT_MENU_ACTION event echoing `token`. Returns 0 when the window
237
+ * is unknown or `count` is 0. */
238
+ int native_sdk_gtk_show_context_menu(native_sdk_gtk_host_t *host, uint64_t window_id, const char *label, size_t label_len, double x, double y, uint64_t token, const native_sdk_gtk_context_menu_item_t *items, size_t count);
214
239
  int native_sdk_gtk_present_gpu_surface_pixels(native_sdk_gtk_host_t *host, uint64_t window_id, const char *label, size_t label_len, size_t width, size_t height, double scale, int has_dirty_rect, double dirty_x, double dirty_y, double dirty_width, double dirty_height, const uint8_t *rgba8, size_t rgba8_len);
215
240
  int native_sdk_gtk_create_webview(native_sdk_gtk_host_t *host, uint64_t window_id, const char *label, size_t label_len, const char *url, size_t url_len, double x, double y, double width, double height, int layer, int transparent, int bridge_enabled);
216
241
  int native_sdk_gtk_set_webview_frame(native_sdk_gtk_host_t *host, uint64_t window_id, const char *label, size_t label_len, double x, double y, double width, double height);
@@ -33,6 +33,8 @@ const GtkEventKind = enum(c_int) {
33
33
  timer = 15,
34
34
  appearance = 16,
35
35
  audio = 17,
36
+ context_menu_action = 18,
37
+ view_focused = 19,
36
38
  };
37
39
 
38
40
  const GtkEvent = extern struct {
@@ -92,6 +94,12 @@ const GtkEvent = extern struct {
92
94
  /// documented scale (log-spaced 50 Hz..16 kHz buckets, linear-in-dB
93
95
  /// from -60 dBFS at 0 to full scale at 255). Zeros elsewhere.
94
96
  audio_bands: [platform_mod.audio_spectrum_band_count]u8,
97
+ /// CONTEXT_MENU_ACTION payload (same field names as the macOS and
98
+ /// Windows hosts): `widget_id` echoes the request's correlation
99
+ /// token and `menu_item_id` is the selected item's id (0 = dismissed
100
+ /// without a selection).
101
+ widget_id: u64,
102
+ menu_item_id: u32,
95
103
  };
96
104
 
97
105
  const GtkCallback = *const fn (context: ?*anyopaque, event: *const GtkEvent) callconv(.c) void;
@@ -165,6 +173,18 @@ extern fn native_sdk_gtk_clipboard_read(host: *GtkHost, buffer: [*]u8, buffer_le
165
173
  extern fn native_sdk_gtk_clipboard_write(host: *GtkHost, text: [*]const u8, text_len: usize) void;
166
174
  extern fn native_sdk_gtk_clipboard_read_data(host: *GtkHost, mime_type: [*]const u8, mime_type_len: usize, buffer: [*]u8, buffer_len: usize) usize;
167
175
  extern fn native_sdk_gtk_clipboard_write_data(host: *GtkHost, mime_type: [*]const u8, mime_type_len: usize, bytes: [*]const u8, bytes_len: usize) c_int;
176
+ extern fn native_sdk_gtk_show_context_menu(host: *GtkHost, window_id: u64, label: [*]const u8, label_len: usize, x: f64, y: f64, token: u64, items: [*]const GtkContextMenuItem, count: usize) c_int;
177
+
178
+ /// One context-menu entry crossing the C ABI (the same shape the macOS
179
+ /// and Windows hosts take): labels ride as pointer+length, flags as
180
+ /// ints.
181
+ const GtkContextMenuItem = extern struct {
182
+ item_id: u32,
183
+ label: [*]const u8,
184
+ label_len: usize,
185
+ enabled: c_int,
186
+ separator: c_int,
187
+ };
168
188
 
169
189
  const GtkOpenDialogOpts = extern struct {
170
190
  title: [*]const u8,
@@ -233,6 +253,18 @@ pub const LinuxPlatform = struct {
233
253
  app_info: platform_mod.AppInfo,
234
254
  surface_value: platform_mod.Surface,
235
255
  state: RunState = .{},
256
+ /// Latched when the runtime's effects teardown abandons an
257
+ /// in-flight channel `wake_fn` call (see
258
+ /// `PlatformServices.note_channel_wake_abandoned_fn`): the stale
259
+ /// call still holds this platform as its context and may execute
260
+ /// into it at any later time, so `deinit` must skip destruction
261
+ /// and leak the host, process-lived — and the wrapper struct the
262
+ /// context actually points at (the wake thunk casts to
263
+ /// `*LinuxPlatform` before reaching the host) must outlive the
264
+ /// call too, which is why runners allocate it through
265
+ /// `createWithOptions` and retire it through `destroy`, the
266
+ /// latch-gated free.
267
+ channel_wake_abandoned: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
236
268
 
237
269
  pub fn init(title: []const u8, size: geometry.SizeF) Error!LinuxPlatform {
238
270
  return initWithEngine(title, size, .system);
@@ -267,7 +299,44 @@ pub const LinuxPlatform = struct {
267
299
  };
268
300
  }
269
301
 
302
+ /// Heap-allocate the wrapper (process allocator) and initialize it
303
+ /// in place. Runners must use this over a stack `initWithOptions`
304
+ /// value: `platform().services.context` is this wrapper's ADDRESS,
305
+ /// worker threads dereference it inside the channel wake path, and
306
+ /// a wake call teardown abandons may do so at any later time —
307
+ /// after a runner's stack frame would have unwound. Pair with
308
+ /// `destroy`, the latch-gated free.
309
+ pub fn createWithOptions(size: geometry.SizeF, web_engine: platform_mod.WebEngine, app_info: platform_mod.AppInfo) Error!*LinuxPlatform {
310
+ const self = std.heap.page_allocator.create(LinuxPlatform) catch return error.CreateFailed;
311
+ errdefer std.heap.page_allocator.destroy(self);
312
+ self.* = try initWithOptions(size, web_engine, app_info);
313
+ return self;
314
+ }
315
+
316
+ /// `deinit` plus the wrapper's own storage, gated by the same
317
+ /// latch: an abandoned channel wake call dereferences this wrapper
318
+ /// (its context) BEFORE it reaches the native host, so on abandon
319
+ /// both are leaked, process-lived — deinit-gating extended to
320
+ /// lifetime-gating, the honest completion of the abandoned-worker
321
+ /// idiom. No cross-thread race on the gate: the latch is set
322
+ /// synchronously on the loop thread during effects teardown, which
323
+ /// runs before the runner's deferred destroy.
324
+ pub fn destroy(self: *LinuxPlatform) void {
325
+ self.deinit();
326
+ if (self.channel_wake_abandoned.load(.seq_cst)) return;
327
+ std.heap.page_allocator.destroy(self);
328
+ }
329
+
270
330
  pub fn deinit(self: *LinuxPlatform) void {
331
+ // An abandoned channel wake call may still enter this host at
332
+ // any later time (see `channel_wake_abandoned`): destroying it
333
+ // would turn that stale call into a use-after-free, so the
334
+ // host is deliberately leaked, process-lived — the
335
+ // abandoned-worker idiom, applied to the platform itself.
336
+ if (self.channel_wake_abandoned.load(.seq_cst)) {
337
+ std.debug.print("linux platform teardown: an abandoned channel wake call may still enter this host; skipping destruction and leaking it (and the wrapper it enters through), process-lived, so the stale call stays safe\n", .{});
338
+ return;
339
+ }
271
340
  native_sdk_gtk_destroy(self.host);
272
341
  }
273
342
 
@@ -306,6 +375,7 @@ pub const LinuxPlatform = struct {
306
375
  .close_view_fn = closeView,
307
376
  .request_gpu_surface_frame_fn = requestGpuSurfaceFrame,
308
377
  .present_gpu_surface_pixels_fn = presentGpuSurfacePixels,
378
+ .show_context_menu_fn = showContextMenu,
309
379
  .create_webview_fn = createWebView,
310
380
  .set_webview_frame_fn = setWebViewFrame,
311
381
  .navigate_webview_fn = navigateWebView,
@@ -330,6 +400,11 @@ pub const LinuxPlatform = struct {
330
400
  .audio_stop_fn = audioStop,
331
401
  .audio_seek_fn = audioSeek,
332
402
  .audio_set_volume_fn = audioSetVolume,
403
+ // The video load verbs are teaching refusals (see
404
+ // `videoLoad`); the transport verbs stay null — the
405
+ // channel never activates without a successful load.
406
+ .video_load_fn = videoLoad,
407
+ .video_load_url_fn = videoLoadUrl,
333
408
  .create_tray_fn = createTray,
334
409
  .update_tray_menu_fn = updateTrayMenu,
335
410
  .remove_tray_fn = removeTray,
@@ -340,6 +415,7 @@ pub const LinuxPlatform = struct {
340
415
  .start_timer_fn = startTimer,
341
416
  .cancel_timer_fn = cancelTimer,
342
417
  .wake_fn = wake,
418
+ .note_channel_wake_abandoned_fn = noteChannelWakeAbandoned,
343
419
  .request_frame_fn = requestFrame,
344
420
  .decode_image_fn = decodeImage,
345
421
  },
@@ -384,11 +460,22 @@ pub const LinuxPlatform = struct {
384
460
  // window back — reporting support would strand windows, so
385
461
  // GTK refuses `close_policy = .hide` at create instead.
386
462
  .window_hide_on_close => false,
387
- // Native scroll drivers, native context menus, and app-owned
388
- // view-surface adoption are macOS-only today; GTK keeps the
389
- // engine's wheel physics and has no popover-menu presenter
390
- // yet (documented in the skill).
391
- .gpu_surface_scroll_drivers, .context_menus, .view_surface_adoption => false,
463
+ // Native context menus present through GtkPopoverMenu (a
464
+ // GMenu pointed at the click, dispatching through a
465
+ // per-invocation action group), riding the same
466
+ // system-engine gate as the other native surfaces.
467
+ .context_menus => self.web_engine == .system,
468
+ // Native scroll drivers and app-owned view-surface adoption
469
+ // are macOS-only today; GTK keeps the engine's wheel
470
+ // physics.
471
+ .gpu_surface_scroll_drivers, .view_surface_adoption => false,
472
+ // Video decode (a GStreamer appsink feeding the
473
+ // media-surface texture channel) is not implemented yet:
474
+ // an honest false rather than a half-implemented player.
475
+ // The load verbs teach and refuse (see `videoLoad`), so an
476
+ // app that skips the capability check still learns exactly
477
+ // what is missing.
478
+ .video_playback => false,
392
479
  };
393
480
  }
394
481
 
@@ -485,6 +572,10 @@ fn gtkCallback(context: ?*anyopaque, event: *const GtkEvent) callconv(.c) void {
485
572
  .focused = event.focused != 0,
486
573
  } });
487
574
  },
575
+ .view_focused => state.emit(.{ .view_focused = .{
576
+ .window_id = event.window_id,
577
+ .label = event.view_label[0..event.view_label_len],
578
+ } }),
488
579
  .shortcut => state.emit(.{ .shortcut = .{
489
580
  .id = event.shortcut_id[0..event.shortcut_id_len],
490
581
  .key = event.shortcut_key[0..event.shortcut_key_len],
@@ -551,9 +642,24 @@ fn gtkCallback(context: ?*anyopaque, event: *const GtkEvent) callconv(.c) void {
551
642
  .buffering = event.audio_buffering != 0,
552
643
  .bands = event.audio_bands,
553
644
  } }),
645
+ .context_menu_action => state.emit(.{ .context_menu_action = contextMenuActionEventFromGtkEvent(event) }),
554
646
  }
555
647
  }
556
648
 
649
+ /// Pure event mapping (no host calls), unit-testable on every build
650
+ /// host: the C event's `widget_id` is the request's correlation token
651
+ /// and `menu_item_id` the selected item (0 = dismissed) — the same
652
+ /// payload contract as the macOS and Windows hosts, so replay is
653
+ /// shape-identical.
654
+ fn contextMenuActionEventFromGtkEvent(event: *const GtkEvent) platform_mod.ContextMenuActionEvent {
655
+ return .{
656
+ .window_id = event.window_id,
657
+ .view_label = event.view_label[0..event.view_label_len],
658
+ .token = event.widget_id,
659
+ .item_id = event.menu_item_id,
660
+ };
661
+ }
662
+
557
663
  /// Ordinals match the audio report kinds in gtk_host.c (the same set the
558
664
  /// macOS and Windows hosts use); anything unknown degrades to `.failed`
559
665
  /// so a host/SDK skew is loud in the app instead of undefined behavior
@@ -685,13 +791,23 @@ fn emitWindowEvent(context: ?*anyopaque, window_id: platform_mod.WindowId, name:
685
791
  native_sdk_gtk_emit_window_event(self.host, window_id, name.ptr, name.len, detail_json.ptr, detail_json.len);
686
792
  }
687
793
 
688
- /// Thread-safe: schedules an idle source on the GLib main loop, which
689
- /// emits `.wake` there. One of the two services worker threads may call.
794
+ /// Thread-safe: schedules an idle source on the GLib main loop
795
+ /// (`g_idle_add` — the enqueue-only shape the wake contract requires),
796
+ /// which emits `.wake` there. One of the two services worker threads
797
+ /// may call.
690
798
  fn wake(context: ?*anyopaque) anyerror!void {
691
799
  const self: *LinuxPlatform = @ptrCast(@alignCast(context.?));
692
800
  native_sdk_gtk_wake(self.host);
693
801
  }
694
802
 
803
+ /// Teardown abandoned an in-flight channel wake call: latch the flag
804
+ /// `deinit` consults so this host is leaked rather than destroyed (see
805
+ /// `LinuxPlatform.channel_wake_abandoned`).
806
+ fn noteChannelWakeAbandoned(context: ?*anyopaque) void {
807
+ const self: *LinuxPlatform = @ptrCast(@alignCast(context.?));
808
+ self.channel_wake_abandoned.store(true, .seq_cst);
809
+ }
810
+
695
811
  /// Thread-safe like `wake`: schedules an idle source on the GLib main
696
812
  /// loop, which emits one `.frame` event there. The automation arrival
697
813
  /// watcher calls this when a command lands so consumption never depends
@@ -984,6 +1100,71 @@ fn presentGpuSurfacePixels(context: ?*anyopaque, pixels: platform_mod.GpuSurface
984
1100
  ) == 0) return error.ViewNotFound;
985
1101
  }
986
1102
 
1103
+ /// GTK popover menus treat `_` in an item label as a mnemonic marker —
1104
+ /// GtkPopoverMenu eats the underscore and underlines the next character —
1105
+ /// so an app-authored label like "Save_As" must cross the ABI with the
1106
+ /// underscore doubled ("Save__As") to render literally. Labels without
1107
+ /// an underscore pass through uncopied; a label whose escaped form does
1108
+ /// not fit the remaining pool also passes through raw, because an
1109
+ /// accidental mnemonic on a pathological label beats dropping bytes.
1110
+ fn escapeMenuLabelUnderscores(label: []const u8, pool: []u8, used: *usize) []const u8 {
1111
+ const underscores = std.mem.count(u8, label, "_");
1112
+ if (underscores == 0) return label;
1113
+ if (label.len + underscores > pool.len - used.*) return label;
1114
+ const start = used.*;
1115
+ var out = start;
1116
+ for (label) |byte| {
1117
+ pool[out] = byte;
1118
+ out += 1;
1119
+ if (byte == '_') {
1120
+ pool[out] = '_';
1121
+ out += 1;
1122
+ }
1123
+ }
1124
+ used.* = out;
1125
+ return pool[start..out];
1126
+ }
1127
+
1128
+ /// Translate the request's items to the C ABI shape, escaping mnemonic
1129
+ /// underscores into `label_pool` (the host strndup-copies every label
1130
+ /// before returning, so a caller stack pool is safe). Pure (no host
1131
+ /// calls), so the separator/disabled/label mapping is unit-testable on
1132
+ /// every build host.
1133
+ fn contextMenuItemsToGtk(items: []const platform_mod.ContextMenuItem, buffer: []GtkContextMenuItem, label_pool: []u8) []const GtkContextMenuItem {
1134
+ const count = @min(items.len, buffer.len);
1135
+ var pool_used: usize = 0;
1136
+ for (items[0..count], 0..) |item, index| {
1137
+ const label = escapeMenuLabelUnderscores(item.label, label_pool, &pool_used);
1138
+ buffer[index] = .{
1139
+ .item_id = item.id,
1140
+ .label = label.ptr,
1141
+ .label_len = label.len,
1142
+ .enabled = if (item.enabled) 1 else 0,
1143
+ .separator = if (item.separator) 1 else 0,
1144
+ };
1145
+ }
1146
+ return buffer[0..count];
1147
+ }
1148
+
1149
+ fn showContextMenu(context: ?*anyopaque, request: platform_mod.ContextMenuRequest) anyerror!void {
1150
+ const self: *LinuxPlatform = @ptrCast(@alignCast(context.?));
1151
+ if (self.web_engine != .system) return error.UnsupportedService;
1152
+ var items: [platform_mod.max_context_menu_items]GtkContextMenuItem = undefined;
1153
+ var label_pool: [platform_mod.max_context_menu_items * 64]u8 = undefined;
1154
+ const translated = contextMenuItemsToGtk(request.items, &items, &label_pool);
1155
+ if (native_sdk_gtk_show_context_menu(
1156
+ self.host,
1157
+ request.window_id,
1158
+ request.view_label.ptr,
1159
+ request.view_label.len,
1160
+ request.point.x,
1161
+ request.point.y,
1162
+ request.token,
1163
+ translated.ptr,
1164
+ translated.len,
1165
+ ) == 0) return error.WindowNotFound;
1166
+ }
1167
+
987
1168
  fn createWebView(context: ?*anyopaque, options: platform_mod.WebViewOptions) anyerror!void {
988
1169
  const self: *LinuxPlatform = @ptrCast(@alignCast(context.?));
989
1170
  if (self.web_engine == .chromium) return error.UnsupportedChildWebViews;
@@ -1240,6 +1421,26 @@ fn audioSetVolume(context: ?*anyopaque, volume: f32) anyerror!void {
1240
1421
  _ = native_sdk_gtk_audio_set_volume(self.host, volume);
1241
1422
  }
1242
1423
 
1424
+ /// The video tier's teaching refusal: the load verbs exist so a video
1425
+ /// source fails with a NAMED explanation instead of a bare
1426
+ /// unsupported-service, while the transport verbs stay null — the
1427
+ /// channel never activates without a successful load. Pure (no GTK
1428
+ /// externs), so the teaching is unit-testable on every host.
1429
+ fn videoLoad(context: ?*anyopaque, path: []const u8, token: u64, sink: platform_mod.VideoFrameSink) anyerror!void {
1430
+ _ = context;
1431
+ _ = path;
1432
+ _ = token;
1433
+ _ = sink;
1434
+ std.debug.print("video playback is not implemented on linux yet: the GTK host has no GStreamer decode path into the media-surface texture channel - the app receives one failed video event (scope video sources to macos builds, or compose a media-surface with your own producer)\n", .{});
1435
+ return error.UnsupportedService;
1436
+ }
1437
+
1438
+ /// The URL twin rides the same teaching — a stream would need the same
1439
+ /// missing decode path a file does.
1440
+ fn videoLoadUrl(context: ?*anyopaque, url: []const u8, token: u64, sink: platform_mod.VideoFrameSink) anyerror!void {
1441
+ return videoLoad(context, url, token, sink);
1442
+ }
1443
+
1243
1444
  fn createTray(context: ?*anyopaque, options: platform_mod.TrayOptions) anyerror!void {
1244
1445
  _ = context;
1245
1446
  _ = options;
@@ -1539,6 +1740,68 @@ test "linux audio event maps kinds and payload" {
1539
1740
  try std.testing.expectEqual(platform_mod.AudioEventKind.failed, audioEventKindFromInt(99));
1540
1741
  }
1541
1742
 
1743
+ test "linux context menus ride the system-engine gate" {
1744
+ var system = testPlatformWithEngine(.system);
1745
+ try std.testing.expect(LinuxPlatform.supportsFeature(&system, .context_menus));
1746
+ var chromium = testPlatformWithEngine(.chromium);
1747
+ try std.testing.expect(!LinuxPlatform.supportsFeature(&chromium, .context_menus));
1748
+ }
1749
+
1750
+ test "linux context menu items translate separators, disabled flags, and labels" {
1751
+ const items = [_]platform_mod.ContextMenuItem{
1752
+ .{ .id = 1, .label = "Complete" },
1753
+ .{ .separator = true },
1754
+ .{ .id = 3, .label = "Delete", .enabled = false },
1755
+ .{ .id = 4, .label = "Move to Save_As" },
1756
+ };
1757
+ var buffer: [platform_mod.max_context_menu_items]GtkContextMenuItem = undefined;
1758
+ var label_pool: [platform_mod.max_context_menu_items * 64]u8 = undefined;
1759
+ const translated = contextMenuItemsToGtk(&items, &buffer, &label_pool);
1760
+ try std.testing.expectEqual(@as(usize, 4), translated.len);
1761
+ try std.testing.expectEqual(@as(u32, 1), translated[0].item_id);
1762
+ try std.testing.expectEqualStrings("Complete", translated[0].label[0..translated[0].label_len]);
1763
+ try std.testing.expectEqual(@as(c_int, 1), translated[0].enabled);
1764
+ try std.testing.expectEqual(@as(c_int, 0), translated[0].separator);
1765
+ try std.testing.expectEqual(@as(c_int, 1), translated[1].separator);
1766
+ try std.testing.expectEqual(@as(u32, 3), translated[2].item_id);
1767
+ try std.testing.expectEqual(@as(c_int, 0), translated[2].enabled);
1768
+ // A literal `_` doubles on the way to GtkPopoverMenu so GTK renders
1769
+ // it instead of eating it as a mnemonic marker; underscore-free
1770
+ // labels pass through pointing at the caller's bytes.
1771
+ try std.testing.expectEqualStrings("Move to Save__As", translated[3].label[0..translated[3].label_len]);
1772
+ try std.testing.expectEqual(items[0].label.ptr, translated[0].label);
1773
+ }
1774
+
1775
+ test "linux menu label escape passes a label through raw when the pool cannot hold it" {
1776
+ var pool: [8]u8 = undefined;
1777
+ var used: usize = 0;
1778
+ const label = "Save_As";
1779
+ // Escaped form needs 8 bytes and fits exactly.
1780
+ try std.testing.expectEqualStrings("Save__As", escapeMenuLabelUnderscores(label, &pool, &used));
1781
+ // The pool is spent: the next underscore label rides unescaped
1782
+ // (accidental mnemonic) rather than truncated.
1783
+ const passed_through = escapeMenuLabelUnderscores(label, &pool, &used);
1784
+ try std.testing.expectEqualStrings("Save_As", passed_through);
1785
+ try std.testing.expectEqual(label.ptr, passed_through.ptr);
1786
+ }
1787
+
1788
+ test "linux context menu action event maps token and item id" {
1789
+ const label = "canvas";
1790
+ var event = std.mem.zeroes(GtkEvent);
1791
+ event.kind = .context_menu_action;
1792
+ event.window_id = 7;
1793
+ event.view_label = label.ptr;
1794
+ event.view_label_len = label.len;
1795
+ event.widget_id = 42;
1796
+ event.menu_item_id = 3;
1797
+
1798
+ const action = contextMenuActionEventFromGtkEvent(&event);
1799
+ try std.testing.expectEqual(@as(platform_mod.WindowId, 7), action.window_id);
1800
+ try std.testing.expectEqualStrings("canvas", action.view_label);
1801
+ try std.testing.expectEqual(@as(u64, 42), action.token);
1802
+ try std.testing.expectEqual(@as(u32, 3), action.item_id);
1803
+ }
1804
+
1542
1805
  fn testPlatformWithEngine(web_engine: platform_mod.WebEngine) LinuxPlatform {
1543
1806
  return .{
1544
1807
  .host = undefined,
@@ -1576,6 +1839,16 @@ test "linux platform module exports type" {
1576
1839
  _ = LinuxPlatform;
1577
1840
  }
1578
1841
 
1842
+ test "linux webview presses report the focused child label" {
1843
+ // The capture-phase observer watches without claiming WebKit's
1844
+ // gesture; it exists solely to mirror the focus edge into runtime
1845
+ // state before the page handles the same press.
1846
+ const host_source = @embedFile("gtk_host.c");
1847
+ try std.testing.expect(std.mem.indexOf(u8, host_source, "gtk_event_controller_set_propagation_phase(GTK_EVENT_CONTROLLER(click), GTK_PHASE_CAPTURE)") != null);
1848
+ try std.testing.expect(std.mem.indexOf(u8, host_source, ".kind = NATIVE_SDK_GTK_EVENT_VIEW_FOCUSED") != null);
1849
+ try std.testing.expect(std.mem.indexOf(u8, host_source, "native_sdk_watch_webview_pointer_focus(win, web_view);") != null);
1850
+ }
1851
+
1579
1852
  test "linux refuses a .hide main window at platform init instead of a silent quit-on-close" {
1580
1853
  // The pre-created MAIN window never passes through the runtime's
1581
1854
  // create gate, so the platform's init gate must hold the same
@@ -32,6 +32,8 @@ typedef enum {
32
32
  NATIVE_SDK_APPKIT_EVENT_GPU_SURFACE_SCROLL_DRIVER = 18,
33
33
  NATIVE_SDK_APPKIT_EVENT_CONTEXT_MENU_ACTION = 19,
34
34
  NATIVE_SDK_APPKIT_EVENT_AUDIO = 20,
35
+ NATIVE_SDK_APPKIT_EVENT_VIDEO = 21,
36
+ NATIVE_SDK_APPKIT_EVENT_VIEW_FOCUSED = 22,
35
37
  } native_sdk_appkit_event_kind_t;
36
38
 
37
39
  /* Audio player reports (EVENT_AUDIO payloads). LOADED acknowledges a
@@ -53,6 +55,23 @@ typedef enum {
53
55
  NATIVE_SDK_APPKIT_AUDIO_EVENT_SPECTRUM = 4,
54
56
  } native_sdk_appkit_audio_event_kind_t;
55
57
 
58
+ /* Video player reports (EVENT_VIDEO payloads). LOADED acknowledges a
59
+ * successful native_sdk_appkit_video_load (or a ready URL stream) with
60
+ * the stream's true pixel dimensions and the decoded duration; POSITION
61
+ * ticks at a coarse honest cadence (~500ms) only while playing;
62
+ * COMPLETED fires exactly once at a non-looping video's natural end (a
63
+ * looping playback wraps and never completes); FAILED reports an
64
+ * asynchronous decode/device failure — or a network failure that killed
65
+ * a stream mid-flight. Pixels never ride events: decoded frames flow
66
+ * through the sink push handed to the load entries. Ordinals are
67
+ * mirrored by the Zig side (videoEventKindFromInt). */
68
+ typedef enum {
69
+ NATIVE_SDK_APPKIT_VIDEO_EVENT_LOADED = 0,
70
+ NATIVE_SDK_APPKIT_VIDEO_EVENT_POSITION = 1,
71
+ NATIVE_SDK_APPKIT_VIDEO_EVENT_COMPLETED = 2,
72
+ NATIVE_SDK_APPKIT_VIDEO_EVENT_FAILED = 3,
73
+ } native_sdk_appkit_video_event_kind_t;
74
+
56
75
  /* How many band magnitudes every SPECTRUM report carries: 32 buckets
57
76
  * with log-spaced center frequencies covering roughly 50 Hz..16 kHz.
58
77
  * Part of the event ABI — the Zig side binds the array by this count. */
@@ -279,9 +298,11 @@ typedef struct {
279
298
  int high_contrast;
280
299
  uint64_t timer_id;
281
300
  /* GPU_SURFACE_SCROLL_DRIVER / CONTEXT_MENU_ACTION payloads: widget_id
282
- * carries the driver id / menu token; scroll_driver_offset_y the new
283
- * content offset (canvas points, y-down, overscroll passes through);
284
- * menu_item_id the selected context-menu item (0 = dismissed). */
301
+ * carries the driver id / menu token; scroll_driver_offset_x/_y the
302
+ * new content offsets (canvas points, x-rightward, y-down,
303
+ * overscroll passes through); menu_item_id the selected
304
+ * context-menu item (0 = dismissed). */
305
+ double scroll_driver_offset_x;
285
306
  double scroll_driver_offset_y;
286
307
  uint32_t menu_item_id;
287
308
  /* GPU_SURFACE_FRAME payloads: host-stamped durations of the most
@@ -313,6 +334,26 @@ typedef struct {
313
334
  * the -60 dBFS analysis floor at 0 to full scale at 255). Zeros on
314
335
  * every other event kind. */
315
336
  uint8_t audio_bands[NATIVE_SDK_APPKIT_AUDIO_SPECTRUM_BANDS];
337
+ /* EVENT_VIDEO payloads: the report kind
338
+ * (native_sdk_appkit_video_event_kind_t) plus the player's
339
+ * position/duration readout in milliseconds at emit time.
340
+ * video_playing/video_buffering carry the audio event's exact
341
+ * semantics (transport intent vs a stalled stream). */
342
+ int video_kind;
343
+ /* The engine-minted load token this playback echoes in every event
344
+ * (see the Zig seam's VideoEvent.token): how a replaced playback's
345
+ * queued straggler is told apart from the replacement's stream. */
346
+ uint64_t video_token;
347
+ uint64_t video_position_ms;
348
+ uint64_t video_duration_ms;
349
+ int video_playing;
350
+ int video_buffering;
351
+ /* LOADED payloads: the STREAM's decoded pixel dimensions — the
352
+ * honest source geometry, even when the host downscales frames to
353
+ * fit the sink's per-frame pixel budget. Zeros on every other
354
+ * event kind. */
355
+ uint64_t video_width;
356
+ uint64_t video_height;
316
357
  } native_sdk_appkit_event_t;
317
358
 
318
359
  typedef void (*native_sdk_appkit_event_callback_t)(void *context, const native_sdk_appkit_event_t *event);
@@ -487,6 +528,41 @@ int native_sdk_appkit_audio_pause(native_sdk_appkit_host_t *host);
487
528
  int native_sdk_appkit_audio_stop(native_sdk_appkit_host_t *host);
488
529
  int native_sdk_appkit_audio_seek(native_sdk_appkit_host_t *host, uint64_t position_ms);
489
530
  int native_sdk_appkit_audio_set_volume(native_sdk_appkit_host_t *host, double volume);
531
+
532
+ /* Where the video player delivers decoded frames: one tightly packed,
533
+ * row-major, straight-alpha RGBA8 frame per call (len = width * height
534
+ * * 4), copied before return. Returns 0 when the frame was accepted, 1
535
+ * when the receiving claim has been released (the host must stop
536
+ * pushing — its frame timer has nothing left to feed), anything else a
537
+ * dropped frame (latest-wins; the host keeps decoding). Always invoked
538
+ * on the main run loop. */
539
+ typedef int (*native_sdk_appkit_video_sink_push_t)(void *context, size_t width, size_t height, const uint8_t *pixels, size_t len);
540
+
541
+ /* The app's single video player (AVPlayer + AVPlayerItemVideoOutput).
542
+ * Load replaces whatever was loaded before, paused at position zero;
543
+ * returns 0 on success, 1 when the file is missing/unreadable, 2 when
544
+ * it cannot be decoded. A successful load is followed by one
545
+ * EVENT_VIDEO/LOADED on the run loop carrying the stream's dimensions
546
+ * and the decoded duration; decoded frames flow through push_fn.
547
+ * Play/pause/stop/seek/set_volume/set_muted/set_loop return 1 when
548
+ * applied, 0 when there is no loaded player to apply to (stop, pause,
549
+ * and the setters treat that as a harmless no-op on the Zig side).
550
+ * All entries are loop-thread only. */
551
+ int native_sdk_appkit_video_load(native_sdk_appkit_host_t *host, const char *path, size_t path_len, uint64_t token, native_sdk_appkit_video_sink_push_t push_fn, void *push_context);
552
+ /* URL sources on the same single player: progressive AVPlayer streaming
553
+ * (playable as soon as enough bytes arrive, never download-then-play;
554
+ * no cache layer). Returns 2 when the URL cannot even be parsed, 0 for
555
+ * a started stream. Async failures (unreachable host, mid-stream
556
+ * network loss, undecodable payload) arrive as one EVENT_VIDEO/FAILED
557
+ * on the run loop. Loop-thread only. */
558
+ int native_sdk_appkit_video_load_url(native_sdk_appkit_host_t *host, const char *url, size_t url_len, uint64_t token, native_sdk_appkit_video_sink_push_t push_fn, void *push_context);
559
+ int native_sdk_appkit_video_play(native_sdk_appkit_host_t *host);
560
+ int native_sdk_appkit_video_pause(native_sdk_appkit_host_t *host);
561
+ int native_sdk_appkit_video_stop(native_sdk_appkit_host_t *host);
562
+ int native_sdk_appkit_video_seek(native_sdk_appkit_host_t *host, uint64_t position_ms);
563
+ int native_sdk_appkit_video_set_volume(native_sdk_appkit_host_t *host, double volume);
564
+ int native_sdk_appkit_video_set_muted(native_sdk_appkit_host_t *host, int muted);
565
+ int native_sdk_appkit_video_set_loop(native_sdk_appkit_host_t *host, int loop);
490
566
  /* Thread-safe: nudges the main run loop to emit a WAKE event. May be
491
567
  * called from any thread (worker threads streaming effect results). */
492
568
  void native_sdk_appkit_wake(native_sdk_appkit_host_t *host);
@@ -603,19 +679,41 @@ typedef void (*native_sdk_appkit_tray_callback_t)(void *context, uint32_t item_i
603
679
  * set_gpu_surface_scroll_drivers_fn). Frame coordinates are view-local
604
680
  * canvas points (top-left origin, y-down); the host flips to AppKit
605
681
  * coordinates itself. */
682
+ /* A surface that hit-blocks scroll regions beneath it (view-local
683
+ * canvas points, top-left origin): wheel routing declines a driver
684
+ * whose occluder mask includes an occluder containing the point. */
685
+ typedef struct {
686
+ double x;
687
+ double y;
688
+ double width;
689
+ double height;
690
+ } native_sdk_appkit_scroll_occluder_t;
691
+
606
692
  typedef struct {
607
693
  uint64_t driver_id;
694
+ /* The nearest ancestor driver's id (0 = none): wheel-owner
695
+ * resolution is restricted to the hit region and its ancestors. */
696
+ uint64_t parent_driver_id;
697
+ /* Bit i set = occluder i (of the sync call's occluder array) blocks
698
+ * this region at points it contains. */
699
+ uint32_t occluder_mask;
608
700
  double x;
609
701
  double y;
610
702
  double width;
611
703
  double height;
612
704
  double content_width;
613
705
  double content_height;
706
+ double offset_x;
614
707
  double offset_y;
615
- int set_offset;
708
+ int set_offset_x;
709
+ int set_offset_y;
616
710
  /* Edge behavior: 0 pins scrolling at the content edges, nonzero lets
617
- * the scroller bounce past them (vertical elasticity). */
711
+ * the scroller bounce past them (armed per axis via the grants). */
618
712
  int rubber_band;
713
+ /* Which axes the region grants: elasticity and scroller chrome arm
714
+ * only on granted axes; an ungranted axis never moves or bounces. */
715
+ int scrolls_x;
716
+ int scrolls_y;
619
717
  } native_sdk_appkit_scroll_driver_t;
620
718
 
621
719
  /* One native context-menu entry. */
@@ -632,7 +730,7 @@ typedef struct {
632
730
  * extents / (when set_offset) offsets, remove drivers absent from the
633
731
  * list. Idempotent; called every layout install and every presented
634
732
  * frame. Returns 1 on success, 0 when the view does not exist. */
635
- int native_sdk_appkit_set_gpu_surface_scroll_drivers(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, const native_sdk_appkit_scroll_driver_t *drivers, size_t count);
733
+ int native_sdk_appkit_set_gpu_surface_scroll_drivers(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, const native_sdk_appkit_scroll_driver_t *drivers, size_t count, const native_sdk_appkit_scroll_occluder_t *occluders, size_t occluder_count);
636
734
 
637
735
  /* Present a native context menu (NSMenu popUpMenuPositioningItem) at the
638
736
  * view-local point on the next main-loop turn. The selection (or