@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
@@ -13,10 +13,13 @@
13
13
  //! `ElementOptions.context_menu` (app-declared, Msg-mapped items),
14
14
  //! 2. an editable text target: the standard Cut / Copy / Paste /
15
15
  //! Select All menu wired to the existing clipboard actions,
16
- //! 3. the view's live static-text selection: a Copy-only menu.
17
- //! The zero-code defaults (2 and 3) are presenter-only: without a native
18
- //! menu they degrade to the keyboard clipboard paths, never a synthesized
19
- //! surface (there are no app-declared items to mount).
16
+ //! 3. a terminal target: the standard Copy / Paste menu wired to its
17
+ //! emulator selection and committed-input channel,
18
+ //! 4. the view's live static-text selection: a Copy-only menu.
19
+ //! The zero-code defaults (2 through 4) are presenter-only: without a
20
+ //! native menu they present no synthesized surface (there are no
21
+ //! app-declared items to mount), while their available keyboard paths
22
+ //! remain unchanged.
20
23
  //!
21
24
  //! Presentation is asynchronous (macOS `popUpMenuPositioningItem` runs a
22
25
  //! nested tracking loop): the platform emits a `context_menu_action`
@@ -40,12 +43,40 @@ pub const default_item_select_all: u32 = 4;
40
43
 
41
44
  pub const PendingCanvasWidgetContextMenu = struct {
42
45
  window_id: platform.WindowId = 1,
46
+ /// Per-request generation, minted when the request is armed and
47
+ /// echoed back by the platform on the action event. Menus can be
48
+ /// superseded while a dismissal is still in flight (GTK tears the
49
+ /// old popover down one loop turn after the successor presents), so
50
+ /// the token must name the REQUEST, not the widget: a stale token's
51
+ /// event must never resolve — or clear — a successor's pending
52
+ /// request, even when both menus target the same widget.
43
53
  token: u64 = 0,
54
+ /// The widget the menu was presented for (selections dispatch
55
+ /// against it; the token above no longer doubles as its id).
56
+ target_id: canvas.ObjectId = 0,
44
57
  kind: Kind = .app,
58
+ /// The presented view's label, copied into the request so a
59
+ /// superseded menu's dismissal notice can still name its canvas —
60
+ /// the view may be gone (window closed) by the time the request
61
+ /// resolves, and per-canvas menu state in a raw app needs the
62
+ /// correlation.
63
+ view_label_storage: [platform.max_view_label_bytes]u8 = undefined,
64
+ view_label_len: usize = 0,
65
+
66
+ pub fn viewLabel(self: *const PendingCanvasWidgetContextMenu) []const u8 {
67
+ return self.view_label_storage[0..self.view_label_len];
68
+ }
69
+
70
+ pub fn setViewLabel(self: *PendingCanvasWidgetContextMenu, label: []const u8) void {
71
+ const len = @min(label.len, self.view_label_storage.len);
72
+ @memcpy(self.view_label_storage[0..len], label[0..len]);
73
+ self.view_label_len = len;
74
+ }
45
75
 
46
76
  pub const Kind = enum {
47
77
  app,
48
78
  edit_text,
79
+ terminal,
49
80
  static_copy,
50
81
  };
51
82
  };
@@ -106,23 +137,57 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
106
137
  .separator = item.separator,
107
138
  };
108
139
  }
109
- if (try showMenu(self, index, .{
140
+ switch (try showMenu(self, app, index, .{
110
141
  .window_id = input_event.window_id,
111
- .token = widget.id,
142
+ .target_id = widget.id,
112
143
  .kind = .app,
113
- }, point, items[0..count])) return;
144
+ }, point, items[0..count])) {
145
+ .shown => |shown| {
146
+ // Presentation is asynchronous on GTK (and the
147
+ // snapshot is harmless where the presenter blocks):
148
+ // tell the app WHAT is on the glass, keyed by the
149
+ // request's token, so the eventual selection
150
+ // resolves against the shown items' dispatch
151
+ // payloads — never a tree that rebuilt (reordering
152
+ // or re-mapping items) while the menu was open.
153
+ // The event's fields come from the returned request
154
+ // itself, never `self.views[index]` re-read here:
155
+ // `showMenu` ran the superseded menu's dismissal
156
+ // notice — arbitrary app code that may have closed
157
+ // views and compacted their indices.
158
+ try self.dispatchEvent(app, .{ .canvas_widget_context_menu_shown = .{
159
+ .window_id = shown.window_id,
160
+ .view_label = shown.viewLabel(),
161
+ .target_id = widget.id,
162
+ .token = shown.token,
163
+ .item_count = count,
164
+ } });
165
+ return;
166
+ },
167
+ // The dismissal notice's app code presented a
168
+ // successor that replaced this request (the
169
+ // successor already announced itself), or closed
170
+ // the presenting view outright: announce NOTHING
171
+ // here — and never the anchored fallback, which
172
+ // would mount a surface under the successor's
173
+ // native menu or on a view that no longer exists.
174
+ .superseded, .view_closed => return,
175
+ .refused => {},
176
+ }
114
177
  }
115
178
  try self.dispatchEvent(app, .{ .canvas_widget_context_menu_request = .{
116
179
  .window_id = input_event.window_id,
117
180
  .view_label = self.views[index].label,
118
181
  .target_id = widget.id,
182
+ .point = point,
119
183
  } });
120
184
  return;
121
185
  }
122
186
 
123
- // 2. Editable text target: the standard edit menu, wired to
124
- // the existing clipboard actions. Focus the field first so
125
- // paste lands where the user clicked (macOS behavior).
187
+ // 2/3. Editable text and terminal targets: standard menus
188
+ // wired to the existing clipboard and committed-input
189
+ // paths. Focus the target first so paste lands where the
190
+ // user clicked (macOS behavior).
126
191
  if (pointer_event.target) |target| {
127
192
  const node_index = self.views[index].canvasWidgetNodeIndexById(target.id) orelse return;
128
193
  const widget = self.views[index].widget_layout_nodes[node_index].widget;
@@ -135,29 +200,44 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
135
200
  items[2] = .{ .id = default_item_paste, .label = "Paste" };
136
201
  items[3] = .{ .separator = true };
137
202
  items[4] = .{ .id = default_item_select_all, .label = "Select All", .enabled = widget.text.len > 0 };
138
- _ = try showMenu(self, index, .{
203
+ _ = try showMenu(self, app, index, .{
139
204
  .window_id = input_event.window_id,
140
- .token = target.id,
205
+ .target_id = target.id,
141
206
  .kind = .edit_text,
142
207
  }, point, items[0..5]);
143
208
  return;
144
209
  }
145
210
 
146
- // 3. Static text with a live selection: Copy only.
211
+ if (widget.kind == .terminal and !widget.state.disabled) {
212
+ if (!has_presenter) return;
213
+ try CanvasWidgetEventMethods().updateCanvasWidgetFocusFromPointer(self, pointer_event);
214
+ const has_selection = if (widget.terminal.grid) |grid| grid.selection_active else false;
215
+ const can_paste = if (widget.terminal.grid) |grid| widget.terminal.pty != 0 and grid.running else false;
216
+ items[0] = .{ .id = default_item_copy, .label = "Copy", .enabled = has_selection };
217
+ items[1] = .{ .id = default_item_paste, .label = "Paste", .enabled = can_paste };
218
+ _ = try showMenu(self, app, index, .{
219
+ .window_id = input_event.window_id,
220
+ .target_id = target.id,
221
+ .kind = .terminal,
222
+ }, point, items[0..2]);
223
+ return;
224
+ }
225
+
226
+ // 4. Static text with a live selection: Copy only.
147
227
  const selected_id = self.views[index].canvas_widget_selected_text_id;
148
228
  if (selected_id != 0 and selected_id == target.id) {
149
229
  if (!has_presenter) return;
150
230
  items[0] = .{ .id = default_item_copy, .label = "Copy" };
151
- _ = try showMenu(self, index, .{
231
+ _ = try showMenu(self, app, index, .{
152
232
  .window_id = input_event.window_id,
153
- .token = target.id,
233
+ .target_id = target.id,
154
234
  .kind = .static_copy,
155
235
  }, point, items[0..1]);
156
236
  return;
157
237
  }
158
238
  }
159
239
 
160
- // 4. No menu anywhere on the route: the press-and-hold
240
+ // 5. No menu anywhere on the route: the press-and-hold
161
241
  // alternative. Deliver the context press so `UiApp` can
162
242
  // dispatch the press target's `on_hold` Msg.
163
243
  try self.dispatchEvent(app, .{ .canvas_widget_context_press = .{
@@ -167,11 +247,66 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
167
247
  } });
168
248
  }
169
249
 
170
- /// Returns whether the platform accepted the presentation; a
171
- /// refusal is not fatal (app-declared menus fall back to the
172
- /// anchored canvas surface, the zero-code defaults degrade to
173
- /// their keyboard paths).
174
- fn showMenu(self: *Runtime, view_index: usize, pending: PendingCanvasWidgetContextMenu, point: geometry.PointF, items: []const platform.ContextMenuItem) anyerror!bool {
250
+ /// A NEW presentation or dispatch replaced the pending request.
251
+ /// An app menu's pending has state beyond the runtime's gate —
252
+ /// UiApp holds a token-keyed snapshot and a pinned build
253
+ /// generation for it, and a raw app may track per-canvas menu
254
+ /// state — and the superseded token can never arrive (the gate
255
+ /// would swallow it), so tell the app the old menu is gone,
256
+ /// named by the view it was presented on. This runs for EVERY
257
+ /// superseding kind: app over app, a default edit/copy menu
258
+ /// over an app menu, and the automation verb's direct dispatch.
259
+ /// Call it AFTER committing the replacement pending: the notice
260
+ /// dispatch is fallible, and the runtime's bookkeeping must
261
+ /// already match the menu the platform accepted.
262
+ pub fn notifySupersededPending(self: *Runtime, app: runtime_api.App(Runtime), superseded: ?PendingCanvasWidgetContextMenu) anyerror!void {
263
+ const pending = superseded orelse return;
264
+ if (pending.kind != .app) return;
265
+ try self.dispatchEvent(app, .{ .canvas_widget_context_menu_dismissed = .{
266
+ .window_id = pending.window_id,
267
+ .view_label = pending.viewLabel(),
268
+ .token = pending.token,
269
+ } });
270
+ }
271
+
272
+ const ShowMenuOutcome = union(enum) {
273
+ /// The platform accepted and the request is still the
274
+ /// pending truth: announce it
275
+ /// (`canvas_widget_context_menu_shown`).
276
+ shown: PendingCanvasWidgetContextMenu,
277
+ /// The platform refused — not fatal (app-declared menus
278
+ /// fall back to the anchored canvas surface, the zero-code
279
+ /// defaults degrade to their keyboard paths). The old
280
+ /// pending (and its popover, if any) is still the truth.
281
+ refused,
282
+ /// The platform accepted, but the superseded menu's
283
+ /// dismissal notice synchronously presented a SUCCESSOR
284
+ /// that replaced this request. The successor is the truth
285
+ /// and already announced itself: announcing this request
286
+ /// late would overwrite the successor's snapshot with a
287
+ /// menu whose token the action gate no longer accepts,
288
+ /// stranding the successor on live-tree resolution and the
289
+ /// stale pin unreleased.
290
+ superseded,
291
+ /// The platform accepted, but the superseded menu's
292
+ /// dismissal notice CLOSED the presenting view. The request
293
+ /// can never resolve (its action would clear the token and
294
+ /// silently drop on the view lookup — or never arrive at
295
+ /// all if the window died with it), so it is disarmed here:
296
+ /// announce nothing, mount nothing.
297
+ view_closed,
298
+ };
299
+
300
+ /// Present `request` natively. Callers must describe the
301
+ /// presented menu from the returned `.shown` request, not from
302
+ /// `self.views[view_index]`: the superseded menu's dismissal
303
+ /// notice below runs arbitrary app code that may close views
304
+ /// and compact their indices — or present a successor menu
305
+ /// (see `ShowMenuOutcome.superseded`).
306
+ fn showMenu(self: *Runtime, app: runtime_api.App(Runtime), view_index: usize, request: PendingCanvasWidgetContextMenu, point: geometry.PointF, items: []const platform.ContextMenuItem) anyerror!ShowMenuOutcome {
307
+ var pending = request;
308
+ pending.token = nextContextMenuToken(self);
309
+ pending.setViewLabel(self.views[view_index].label);
175
310
  self.options.platform.services.showContextMenu(.{
176
311
  .window_id = pending.window_id,
177
312
  .view_label = self.views[view_index].label,
@@ -182,10 +317,25 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
182
317
  if (err != error.UnsupportedService) {
183
318
  context_menu_log.warn("context menu presentation failed: {s}", .{@errorName(err)});
184
319
  }
185
- return false;
320
+ // Presentation refused: nothing superseded — the old
321
+ // pending (and its popover, if any) is still the truth.
322
+ return .refused;
186
323
  };
324
+ // The platform accepted: commit the replacement BEFORE the
325
+ // fallible superseded-notice dispatch, so the runtime's
326
+ // expected token always matches the menu on the glass.
327
+ const superseded = self.canvas_widget_context_menu_pending;
187
328
  self.canvas_widget_context_menu_pending = pending;
188
- return true;
329
+ try notifySupersededPending(self, app, superseded);
330
+ const still_armed = if (self.canvas_widget_context_menu_pending) |current| current.token == pending.token else false;
331
+ if (!still_armed) return .superseded;
332
+ // Still armed, but the notice may have closed the view the
333
+ // menu presented on: disarm the unresolvable request.
334
+ if (runtimeFindViewIndex(self, pending.window_id, pending.viewLabel()) == null) {
335
+ self.canvas_widget_context_menu_pending = null;
336
+ return .view_closed;
337
+ }
338
+ return .{ .shown = pending };
189
339
  }
190
340
 
191
341
  /// The platform reported the menu outcome: resolve the pending
@@ -195,19 +345,48 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
195
345
  /// directly through the same paths the keyboard shortcuts use.
196
346
  pub fn dispatchContextMenuAction(self: *Runtime, app: runtime_api.App(Runtime), event: platform.ContextMenuActionEvent) anyerror!void {
197
347
  const pending = self.canvas_widget_context_menu_pending orelse return;
198
- self.canvas_widget_context_menu_pending = null;
348
+ // Token gate BEFORE the clear: an event carrying a stale
349
+ // token belongs to a superseded request (its deferred
350
+ // dismissal outlived a re-click's replacement menu) and is
351
+ // swallowed — it must never clear, let alone resolve, the
352
+ // successor's pending request. Windows cannot produce a
353
+ // stale event (TrackPopupMenu blocks the loop thread and
354
+ // emits inline from the moved-out request, so a second
355
+ // request never dispatches mid-menu); macOS cannot either
356
+ // (each presentation block captures its own token and
357
+ // popUpMenuPositioningItem's nested tracking loop blocks
358
+ // the main queue); GTK popovers are asynchronous and CAN.
199
359
  if (pending.window_id != event.window_id or pending.token != event.token) return;
200
- if (event.item_id == 0) return; // dismissed
360
+ self.canvas_widget_context_menu_pending = null;
361
+ if (event.item_id == 0) {
362
+ // Dismissed without a selection. App menus tell the app:
363
+ // UiApp disarms the token's presented-items snapshot and
364
+ // releases the build storage pinned under it.
365
+ if (pending.kind == .app) {
366
+ try self.dispatchEvent(app, .{ .canvas_widget_context_menu_dismissed = .{
367
+ .window_id = event.window_id,
368
+ .view_label = event.view_label,
369
+ .token = pending.token,
370
+ } });
371
+ }
372
+ return;
373
+ }
201
374
  const index = runtimeFindViewIndex(self, event.window_id, event.view_label) orelse return;
202
375
 
203
376
  switch (pending.kind) {
204
- .app => try self.dispatchEvent(app, .{ .canvas_widget_context_menu = .{
205
- .window_id = event.window_id,
206
- .view_label = self.views[index].label,
207
- .target_id = pending.token,
208
- .item_index = event.item_id - 1,
209
- } }),
210
- .edit_text => try applyDefaultEditAction(self, app, index, pending.token, event.item_id),
377
+ .app => try self.dispatchEvent(app, .{
378
+ .canvas_widget_context_menu = .{
379
+ .window_id = event.window_id,
380
+ .view_label = self.views[index].label,
381
+ .target_id = pending.target_id,
382
+ .item_index = event.item_id - 1,
383
+ // The shown snapshot's key: UiApp resolves the
384
+ // selection from what was presented under this token.
385
+ .token = pending.token,
386
+ },
387
+ }),
388
+ .edit_text => try applyDefaultEditAction(self, app, index, pending.target_id, event.item_id),
389
+ .terminal => try applyDefaultTerminalAction(self, app, index, pending.target_id, event.item_id),
211
390
  .static_copy => {
212
391
  if (event.item_id != default_item_copy) return;
213
392
  const text = self.views[index].canvasWidgetCopyText() orelse return;
@@ -216,6 +395,14 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
216
395
  }
217
396
  }
218
397
 
398
+ /// Mint the next per-request correlation token. Never zero, so a
399
+ /// zero-token event can never match an armed request.
400
+ pub fn nextContextMenuToken(self: *Runtime) u64 {
401
+ self.canvas_widget_context_menu_token +%= 1;
402
+ if (self.canvas_widget_context_menu_token == 0) self.canvas_widget_context_menu_token = 1;
403
+ return self.canvas_widget_context_menu_token;
404
+ }
405
+
219
406
  fn applyDefaultEditAction(self: *Runtime, app: runtime_api.App(Runtime), view_index: usize, target_id: canvas.ObjectId, item_id: u32) anyerror!void {
220
407
  switch (item_id) {
221
408
  default_item_copy => {
@@ -257,6 +444,53 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
257
444
  }
258
445
  }
259
446
 
447
+ fn applyDefaultTerminalAction(self: *Runtime, app: runtime_api.App(Runtime), view_index: usize, target_id: canvas.ObjectId, item_id: u32) anyerror!void {
448
+ const node_index = self.views[view_index].canvasWidgetNodeIndexById(target_id) orelse return;
449
+ const widget = self.views[view_index].widget_layout_nodes[node_index].widget;
450
+ if (widget.kind != .terminal or widget.state.disabled) return;
451
+
452
+ switch (item_id) {
453
+ default_item_copy => {
454
+ const grid = widget.terminal.grid orelse return;
455
+ if (!grid.selection_active) return;
456
+ self.writeClipboard(grid.selection_text) catch return;
457
+ },
458
+ default_item_paste => {
459
+ if (widget.terminal.pty == 0) return;
460
+ const grid = widget.terminal.grid orelse return;
461
+ // The native menu resolves asynchronously on GTK:
462
+ // a session that was live when the menu opened may
463
+ // have exited before Paste is chosen. Revalidate at
464
+ // action time so dead-session input cannot fall
465
+ // through to UiApp's app-level `on_text` seam.
466
+ if (!grid.running) return;
467
+ var paste_buffer: [platform.max_clipboard_data_bytes]u8 = undefined;
468
+ const text = self.readClipboard(&paste_buffer) catch return;
469
+ if (text.len == 0) return;
470
+ // Terminals deliberately stay outside the TextBuffer
471
+ // editor pipeline. Mark this committed payload as a
472
+ // PASTE so UiApp routes it through the emulator's
473
+ // dedicated paste encoder, not the ordinary typing
474
+ // path: bracketed-paste framing, newline conversion,
475
+ // and unsafe-control stripping are all provenance-
476
+ // sensitive terminal behavior.
477
+ try self.dispatchEvent(app, .{ .canvas_widget_keyboard = .{
478
+ .window_id = self.views[view_index].window_id,
479
+ .view_label = self.views[view_index].label,
480
+ .keyboard = .{
481
+ .phase = .text_input,
482
+ .focused_id = target_id,
483
+ .text = text,
484
+ .edit = .{ .insert_text = text },
485
+ },
486
+ .terminal_paste = true,
487
+ .standalone = true,
488
+ } });
489
+ },
490
+ else => {},
491
+ }
492
+ }
493
+
260
494
  /// A synthesized key-down keyboard event addressed at the editable
261
495
  /// widget, shaped like the routed events the clipboard shortcuts
262
496
  /// stamp their edits onto.
@@ -275,6 +509,9 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
275
509
  /// edits perform, so runtime widget and app model stay in sync.
276
510
  fn applyEditKeyboardEvent(self: *Runtime, app: runtime_api.App(Runtime), keyboard_event: runtime_api.CanvasWidgetKeyboardEvent) anyerror!void {
277
511
  var event = keyboard_event;
512
+ // No gpu-surface input cycle wraps a context-menu edit: mark
513
+ // it standalone so the ui-app hover drain runs at its tail.
514
+ event.standalone = true;
278
515
  try CanvasWidgetEventMethods().updateCanvasWidgetTextFromKeyboard(self, &event);
279
516
  try self.dispatchEvent(app, .{ .canvas_widget_keyboard = event });
280
517
  }