@native-sdk/cli 0.5.4 → 0.6.0

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 +534 -3
  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 +1283 -66
  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 +70 -1
  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 +240 -24
  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 +473 -13
  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 +109 -15
  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 +2333 -63
  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
@@ -242,7 +242,7 @@ pub fn RuntimeFlow(comptime Runtime: type) type {
242
242
  log(self, "app.start", "app started", &.{trace.string("app", app.name)});
243
243
  },
244
244
  .app_activated => {
245
- self.app_active = true;
245
+ try WindowViewMethods().setAppActive(self, true);
246
246
  try dispatchEvent(self, app, .{ .lifecycle = .activate });
247
247
  emitAppLifecycleEvent(self, "app:activate") catch |err| log(self, "app.activate.emit_failed", @errorName(err), &.{});
248
248
  },
@@ -254,7 +254,7 @@ pub fn RuntimeFlow(comptime Runtime: type) type {
254
254
  // that already knows the app is inactive: the
255
255
  // adoption arm's reveal/arm paths are gated on this
256
256
  // register and stay silent.
257
- self.app_active = false;
257
+ try WindowViewMethods().setAppActive(self, false);
258
258
  // Deactivation drops every tooltip conversation in
259
259
  // every window (see the seam's own rationale) BEFORE
260
260
  // the app hears the lifecycle event, so a model that
@@ -342,6 +342,17 @@ pub fn RuntimeFlow(comptime Runtime: type) type {
342
342
  if (WindowViewMethods().findWindowIndexById(self, window_id)) |index| try WindowViewMethods().setFocusedIndex(self, index);
343
343
  self.invalidated = true;
344
344
  },
345
+ .view_focused => |focus| {
346
+ // Native children (notably embedded webviews) own
347
+ // their pointer stream, so the host reports the
348
+ // focus edge explicitly. Keep the canvas widget id
349
+ // as per-view focus memory while its view blurs:
350
+ // inactive terminal/text carets paint honestly, and
351
+ // clicking back into the canvas resumes its normal
352
+ // pointer-driven widget focus path.
353
+ try WindowViewMethods().setFocusedView(self, focus.window_id, focus.label);
354
+ self.invalidated = true;
355
+ },
345
356
  .frame_requested => try frame(self, app),
346
357
  .bridge_message => |message| try handleBridgeMessage(self, app, message),
347
358
  .tray_action => |item_id| {
@@ -433,6 +444,9 @@ pub fn RuntimeFlow(comptime Runtime: type) type {
433
444
  .audio => |audio_event| {
434
445
  try dispatchEvent(self, app, .{ .audio = audio_event });
435
446
  },
447
+ .video => |video_event| {
448
+ try dispatchEvent(self, app, .{ .video = video_event });
449
+ },
436
450
  .wake => {
437
451
  try dispatchEvent(self, app, .effects_wake);
438
452
  },
@@ -498,6 +512,7 @@ pub fn RuntimeFlow(comptime Runtime: type) type {
498
512
  .timer => {},
499
513
  .effects_wake => {},
500
514
  .audio => {},
515
+ .video => {},
501
516
  .files_dropped => {},
502
517
  .gpu_surface_frame => {},
503
518
  .gpu_surface_resized => {},
@@ -510,6 +525,12 @@ pub fn RuntimeFlow(comptime Runtime: type) type {
510
525
  .canvas_widget_context_menu => {
511
526
  self.invalidateFor(.command, null);
512
527
  },
528
+ // The shown notice only arms UiApp's selection snapshot:
529
+ // no visual change (the popover is an OS surface).
530
+ .canvas_widget_context_menu_shown => {},
531
+ // The dismissal notice only disarms UiApp's snapshot and
532
+ // pin: no visual change either.
533
+ .canvas_widget_context_menu_dismissed => {},
513
534
  .canvas_widget_context_menu_request => {
514
535
  // The app loop answers by mounting the anchored
515
536
  // fallback surface: a visual change.
@@ -1198,7 +1219,7 @@ pub fn RuntimeFlow(comptime Runtime: type) type {
1198
1219
  /// app: record it in the bounded ring (queryable through
1199
1220
  /// `Runtime.dispatchErrors` and the automation snapshot), trace
1200
1221
  /// it at `.err`, and republish observable state.
1201
- fn recordDispatchError(self: *Runtime, event_name: []const u8, err: anyerror) void {
1222
+ pub fn recordDispatchError(self: *Runtime, event_name: []const u8, err: anyerror) void {
1202
1223
  recordDispatchErrorDetail(self, event_name, err, "");
1203
1224
  }
1204
1225
 
@@ -1210,9 +1231,9 @@ pub fn RuntimeFlow(comptime Runtime: type) type {
1210
1231
  self.dispatch_error_total +%= 1;
1211
1232
  var record: automation.snapshot.DispatchError = .{
1212
1233
  .timestamp_ns = runtime_clock.timestampToU64(nowNanoseconds()),
1213
- .event = event_name,
1214
1234
  .error_name = @errorName(err),
1215
1235
  };
1236
+ record.setEvent(event_name);
1216
1237
  record.setDetail(detail);
1217
1238
  if (self.dispatch_error_len == self.dispatch_errors.len) {
1218
1239
  std.mem.copyForwards(automation.snapshot.DispatchError, self.dispatch_errors[0 .. self.dispatch_errors.len - 1], self.dispatch_errors[1..]);
@@ -1,5 +1,6 @@
1
1
  const std = @import("std");
2
2
  const canvas = @import("canvas");
3
+ const runtime_view = @import("view.zig");
3
4
  const geometry = @import("geometry");
4
5
  const platform = @import("../platform/root.zig");
5
6
  const runtime_api = @import("api.zig");
@@ -190,6 +191,40 @@ pub fn RuntimeGpuSurfaceEvents(comptime Runtime: type) type {
190
191
  if (ContextMenuMethods().canvasWidgetContextPointerInput(input_event)) {
191
192
  if (runtimeFindViewIndex(self, input_event.window_id, input_event.label)) |index| {
192
193
  self.views[index].recordGpuSurfaceInputTimestamp(input_event.timestamp_ns);
194
+ // A consumed cancel is still the pointer leaving the
195
+ // view (the tooltip machine's exact reading below):
196
+ // the proven pointer's departure retires hover-Msg
197
+ // containment too, so a window exit mid-secondary
198
+ // stream never strands an entered element. Honesty
199
+ // about scope: only the secondary DOWN/UP/CANCEL
200
+ // stream is consumed here — hosts report drag
201
+ // MOTION without a button, so it rides the primary
202
+ // path and containment follows the pointer through
203
+ // a right-drag (the mouseenter/mouseleave
204
+ // convention), exactly as the wash does for the
205
+ // same journaled moves.
206
+ if (input_event.kind == .pointer_cancel and
207
+ self.views[index].canvas_widget_hover_pointer_live and
208
+ self.views[index].canvas_widget_hover_pointer_id == input_event.pointer_id)
209
+ {
210
+ self.views[index].canvas_widget_hover_msg_chain_len = 0;
211
+ self.views[index].canvas_widget_hover_pointer_live = false;
212
+ }
213
+ // A consumed secondary RELEASE outside the view is
214
+ // the gesture ending with the pointer already gone —
215
+ // hosts hold an implicit grab through a right-drag,
216
+ // so no motion-leave fired and none will until
217
+ // re-entry: retire containment like the cancel
218
+ // above, matching the primary path whose outside
219
+ // release re-hit-tests to nothing.
220
+ if (input_event.kind == .pointer_up and
221
+ self.views[index].canvas_widget_hover_pointer_live and
222
+ self.views[index].canvas_widget_hover_pointer_id == input_event.pointer_id and
223
+ !gpuViewContainsPoint(&self.views[index], input_event.x, input_event.y))
224
+ {
225
+ self.views[index].canvas_widget_hover_msg_chain_len = 0;
226
+ self.views[index].canvas_widget_hover_pointer_live = false;
227
+ }
193
228
  }
194
229
  // The whole consumed stream still feeds the tooltip
195
230
  // intent choke point: every pointer-carrying event
@@ -292,14 +327,49 @@ pub fn RuntimeGpuSurfaceEvents(comptime Runtime: type) type {
292
327
  => null,
293
328
  else => return err,
294
329
  };
295
- const keyboard_dismissed_id = try CanvasWidgetEventMethods().dismissCanvasWidgetSurfaceFromKeyboardInput(self, input_event);
330
+ // A live target-less composition owns its surface's
331
+ // UNCHORDED keys wholesale, and it owns them BEFORE any
332
+ // widget pass runs: on hosts that surface the key ahead of
333
+ // the input method's result, the confirming Enter (or a
334
+ // candidate-navigation arrow, or the cancelling Escape)
335
+ // would otherwise dismiss a popup, move widget focus, or
336
+ // activate a freshly focused button while the composition
337
+ // it belongs to is still open. Chorded keys stay live —
338
+ // input methods never consume command chords, so those are
339
+ // genuine shortcuts even mid-composition.
340
+ const targetless_composition_owns_keys = (input_event.kind == .key_down or input_event.kind == .key_up) and
341
+ !canvas_frame_helpers.gpuInputHasTextCommandModifier(input_event) and
342
+ self.targetless_ime_preedit_len > 0 and
343
+ self.targetless_ime_preedit_window == input_event.window_id and
344
+ std.mem.eql(
345
+ u8,
346
+ self.targetless_ime_preedit_label[0..self.targetless_ime_preedit_label_len],
347
+ input_event.label,
348
+ );
349
+ // A Tab key-down that moved focus INTO a live terminal is a
350
+ // focus gesture for its whole physical lifetime. Suppress
351
+ // its auto-repeats and release before any widget pass can
352
+ // reinterpret them against the newly focused terminal.
353
+ const terminal_focus_entry_tab_suppressed =
354
+ CanvasWidgetEventMethods().consumeCanvasWidgetTerminalFocusEntryTab(self, input_event);
355
+ // Terminal Paste owns its matching physical V release even
356
+ // when Command/Ctrl came up first. Classify it before focus,
357
+ // routing, or app fallbacks can reinterpret the orphan.
358
+ const terminal_paste_release_suppressed =
359
+ CanvasWidgetEventMethods().consumeCanvasWidgetTerminalPasteKeyLifetime(self, input_event);
360
+ const terminal_key_lifetime_suppressed =
361
+ terminal_focus_entry_tab_suppressed or terminal_paste_release_suppressed;
362
+ const keyboard_dismissed_id = if (targetless_composition_owns_keys or terminal_key_lifetime_suppressed)
363
+ 0
364
+ else
365
+ try CanvasWidgetEventMethods().dismissCanvasWidgetSurfaceFromKeyboardInput(self, input_event);
296
366
  if (keyboard_dismissed_id != 0) dismissed_surface_id = keyboard_dismissed_id;
297
367
  const widget_surface_dismissed = keyboard_dismissed_id != 0;
298
- const widget_focus_moved = if (widget_surface_dismissed)
368
+ const widget_focus_moved = if (widget_surface_dismissed or targetless_composition_owns_keys or terminal_key_lifetime_suppressed)
299
369
  false
300
370
  else
301
371
  try CanvasWidgetEventMethods().updateCanvasWidgetFocusFromKeyboardInput(self, input_event);
302
- var widget_keyboard_event = if (widget_surface_dismissed)
372
+ var widget_keyboard_event = if (widget_surface_dismissed or targetless_composition_owns_keys or terminal_key_lifetime_suppressed)
303
373
  null
304
374
  else
305
375
  CanvasWidgetEventMethods().routeCanvasWidgetKeyboardInput(self, input_event, &self.widget_event_route_entries) catch |err| switch (err) {
@@ -340,7 +410,114 @@ pub fn RuntimeGpuSurfaceEvents(comptime Runtime: type) type {
340
410
  try CanvasWidgetEventMethods().updateCanvasWidgetControlFromKeyboard(self, keyboard_event.*);
341
411
  try CanvasWidgetEventMethods().updateCanvasWidgetTextFromKeyboard(self, keyboard_event);
342
412
  }
343
- var widget_text_input_event = if (widget_surface_dismissed)
413
+ // An IME sequence belongs to whoever it STARTED over: a
414
+ // composition buffered by the target-less consumer (this
415
+ // surface owns a live target-less preedit) continues
416
+ // target-less even if a text widget has since taken focus —
417
+ // routing its commit or cancel to the newly focused editor
418
+ // would resolve a composition that editor never saw, and the
419
+ // target-less consumer's composed text would be lost.
420
+ const ime_continuation_owned_targetless = switch (input_event.kind) {
421
+ .ime_set_composition, .ime_commit_composition, .ime_cancel_composition => self.targetless_ime_preedit_len > 0 and
422
+ self.targetless_ime_preedit_window == input_event.window_id and
423
+ std.mem.eql(
424
+ u8,
425
+ self.targetless_ime_preedit_label[0..self.targetless_ime_preedit_label_len],
426
+ input_event.label,
427
+ ),
428
+ else => false,
429
+ };
430
+ // Resolve the one-shot cancel grace FIRST (see
431
+ // `ImeCommitGrace`): a text_input arriving right after a
432
+ // cancel is a converted commit's result and still belongs
433
+ // to the cancelled sequence; any other event disarms the
434
+ // grace (a plain Escape cancel is chased by its own
435
+ // key_down, so ordinary typing never inherits it).
436
+ const ime_grace: runtime_view.ImeCommitGrace = grace: {
437
+ const index = runtimeFindViewIndex(self, input_event.window_id, input_event.label) orelse break :grace .none;
438
+ if (self.views[index].kind != .gpu_surface) break :grace .none;
439
+ const armed = self.views[index].canvas_widget_ime_commit_grace;
440
+ if (armed == .none) break :grace .none;
441
+ self.views[index].canvas_widget_ime_commit_grace = .none;
442
+ if (input_event.kind == .text_input) {
443
+ if (armed == .route_to_owner) {
444
+ // The owner may have vanished between the cancel
445
+ // and this trailing commit (the cancel's own app
446
+ // dispatch can rebuild the tree): a dead owner
447
+ // converts the grace to a SWALLOW — the composed
448
+ // result resolves nowhere, never in whichever
449
+ // editor holds focus now.
450
+ const owner = self.views[index].canvas_widget_ime_owner_id;
451
+ const alive = alive: {
452
+ if (owner == 0) break :alive false;
453
+ if (self.views[index].widgetLayoutTree().findById(owner)) |node| {
454
+ break :alive canvas.isWidgetTextEntry(node.widget);
455
+ }
456
+ break :alive false;
457
+ };
458
+ if (!alive) {
459
+ self.views[index].canvas_widget_ime_owner_id = 0;
460
+ break :grace .swallow;
461
+ }
462
+ }
463
+ break :grace armed;
464
+ }
465
+ // Disarmed: release the owner a route_to_owner grace
466
+ // held through the cancel.
467
+ if (armed == .route_to_owner) self.views[index].canvas_widget_ime_owner_id = 0;
468
+ break :grace .none;
469
+ };
470
+ // A WIDGET-owned sequence whose owning editor vanished (a
471
+ // rebuild removed it mid-composition) can resolve NOWHERE:
472
+ // every continuation is swallowed — routing to the newly
473
+ // focused editor would resolve (or preedit-update) a
474
+ // composition that editor never saw, and the target-less
475
+ // fallback would type it into a consumer that never composed
476
+ // it. The pin HOLDS through set updates (the sequence is
477
+ // still open) and clears only when its commit or cancel
478
+ // closes it; an orphaned cancel arms the swallow grace so a
479
+ // converted commit's trailing text_input is swallowed too.
480
+ const ime_widget_owner_orphaned = switch (input_event.kind) {
481
+ .ime_set_composition, .ime_commit_composition, .ime_cancel_composition => blk: {
482
+ const index = runtimeFindViewIndex(self, input_event.window_id, input_event.label) orelse break :blk false;
483
+ if (self.views[index].kind != .gpu_surface) break :blk false;
484
+ const owner = self.views[index].canvas_widget_ime_owner_id;
485
+ if (owner == 0) break :blk false;
486
+ const alive = alive: {
487
+ if (self.views[index].widgetLayoutTree().findById(owner)) |node| {
488
+ break :alive canvas.isWidgetTextEntry(node.widget);
489
+ }
490
+ break :alive false;
491
+ };
492
+ if (alive) break :blk false;
493
+ if (input_event.kind != .ime_set_composition) {
494
+ self.views[index].canvas_widget_ime_owner_id = 0;
495
+ if (input_event.kind == .ime_cancel_composition) {
496
+ self.views[index].canvas_widget_ime_commit_grace = .swallow;
497
+ }
498
+ }
499
+ break :blk true;
500
+ },
501
+ else => false,
502
+ };
503
+ const ime_grace_swallow = ime_grace == .swallow;
504
+ // The target-less twin of the cancel grace: the text_input
505
+ // trailing a target-less composition's cancel is that
506
+ // composition's converted commit — it delivers TARGET-LESS
507
+ // on the owning surface, never into whichever text widget
508
+ // holds focus by now. One-shot: any other event disarms.
509
+ const targetless_commit_grace = grace: {
510
+ if (!self.targetless_ime_commit_grace) break :grace false;
511
+ self.targetless_ime_commit_grace = false;
512
+ break :grace input_event.kind == .text_input and
513
+ self.targetless_ime_preedit_window == input_event.window_id and
514
+ std.mem.eql(
515
+ u8,
516
+ self.targetless_ime_preedit_label[0..self.targetless_ime_preedit_label_len],
517
+ input_event.label,
518
+ );
519
+ };
520
+ var widget_text_input_event = if (widget_surface_dismissed or ime_continuation_owned_targetless or ime_widget_owner_orphaned or ime_grace_swallow or targetless_commit_grace)
344
521
  null
345
522
  else
346
523
  CanvasWidgetEventMethods().routeCanvasWidgetTextInput(self, input_event, &self.widget_event_route_entries) catch |err| switch (err) {
@@ -352,7 +529,77 @@ pub fn RuntimeGpuSurfaceEvents(comptime Runtime: type) type {
352
529
  };
353
530
  if (widget_text_input_event) |*text_input_event| {
354
531
  try CanvasWidgetEventMethods().updateCanvasWidgetTextFromKeyboard(self, text_input_event);
532
+ // Composition ownership follows the ROUTED editor: a
533
+ // set_composition opens (or continues) the sequence in
534
+ // its target; a commit — or a direct insertion — closes
535
+ // it. A CANCEL holds the pin one more event and arms the
536
+ // route grace: the hosts encode a converted commit as
537
+ // cancel-then-text_input, so the trailing text_input
538
+ // must still find the owner (the grace probe above
539
+ // releases the pin instead when anything else follows a
540
+ // plain cancel).
541
+ if (runtimeFindViewIndex(self, input_event.window_id, input_event.label)) |index| {
542
+ switch (input_event.kind) {
543
+ .ime_set_composition => self.views[index].canvas_widget_ime_owner_id =
544
+ text_input_event.keyboard.focused_id orelse 0,
545
+ // Arm ONLY while an owner is pinned: a cancel of
546
+ // a live composition holds it. A DUPLICATE
547
+ // cancel — hosts emit a synthetic one to disarm
548
+ // the grace when the resolving key's own
549
+ // key_down was consumed — arrives after the
550
+ // probe above already released the owner, and
551
+ // re-arming ownerless would convert the next
552
+ // ordinary character into a dead-owner swallow.
553
+ .ime_cancel_composition => if (self.views[index].canvas_widget_ime_owner_id != 0) {
554
+ self.views[index].canvas_widget_ime_commit_grace = .route_to_owner;
555
+ },
556
+ .ime_commit_composition, .text_input => self.views[index].canvas_widget_ime_owner_id = 0,
557
+ else => {},
558
+ }
559
+ }
355
560
  }
561
+ // The target-less committed-text claim is decided NOW —
562
+ // against the tree the input actually routed through —
563
+ // because the app dispatches below may rebuild it: a Space
564
+ // that pressed a focused button whose command removes that
565
+ // button must still count as claimed, or the same physical
566
+ // keystroke would double into a command AND a literal space
567
+ // through `on_text`.
568
+ const committed_text_claimed = blk: {
569
+ const index = runtimeFindViewIndex(self, input_event.window_id, input_event.label) orelse break :blk false;
570
+ // Consume the split-event claim carry first: hosts that
571
+ // deliver the claimed key_down and its committed text as
572
+ // SEPARATE events would otherwise recompute the claim
573
+ // against a tree the activation already rebuilt (see
574
+ // `canvas_widget_claimed_key_grace`).
575
+ const split_claim = self.views[index].canvas_widget_claimed_key_grace;
576
+ self.views[index].canvas_widget_claimed_key_grace = .none;
577
+ // KEYED: the carry swallows only the armed key's OWN
578
+ // committed literal. A different key's text arriving
579
+ // first (a second key pressed while the claimed one is
580
+ // held, on hosts that emit input-method text before its
581
+ // key_down) must flow, not feed a stale latch.
582
+ if (input_event.kind == .text_input and split_claim.coversText(input_event.text)) break :blk true;
583
+ // An input-method-owned key claims nothing (it reached
584
+ // no widget above) and must not arm the carry either.
585
+ if (targetless_composition_owns_keys) break :blk false;
586
+ const claimed = targetlessCommittedTextClaimedByFocusedWidget(self, index, input_event);
587
+ // Arm the carry when this claimed key_down brought no
588
+ // text of its own — its committed character may follow
589
+ // as a separate event. Only the text-producing
590
+ // activation keys arm one; other claimed keys commit
591
+ // nothing, so there is nothing to carry.
592
+ if (claimed and input_event.kind == .key_down and input_event.text.len == 0) {
593
+ self.views[index].canvas_widget_claimed_key_grace =
594
+ if (std.ascii.eqlIgnoreCase(input_event.key, "space"))
595
+ .space
596
+ else if (std.ascii.eqlIgnoreCase(input_event.key, "enter"))
597
+ .enter
598
+ else
599
+ .none;
600
+ }
601
+ break :blk claimed;
602
+ };
356
603
  // The refresh batch stays open across the app dispatches
357
604
  // below: a click's pointer-up used to emit once for the
358
605
  // widget-state change and once more for the Msg-driven
@@ -383,8 +630,10 @@ pub fn RuntimeGpuSurfaceEvents(comptime Runtime: type) type {
383
630
  if (widget_keyboard_event) |keyboard_event| {
384
631
  try CanvasWidgetEventMethods().dispatchCanvasWidgetCommandFromKeyboard(self, app, keyboard_event);
385
632
  try self.dispatchEvent(app, .{ .canvas_widget_keyboard = keyboard_event });
386
- } else if (input_event.kind == .key_down and !widget_surface_dismissed) {
387
- // No focused widget routed this key_down (nothing is
633
+ } else if ((input_event.kind == .key_down or input_event.kind == .key_up) and
634
+ !widget_surface_dismissed and !terminal_key_lifetime_suppressed)
635
+ {
636
+ // No focused widget routed this key (nothing is
388
637
  // focused, or the focused id is gone from the tree): the
389
638
  // key still reaches the app, as a TARGET-LESS keyboard
390
639
  // event. This is the app-level key-fallback seam — the
@@ -392,18 +641,40 @@ pub fn RuntimeGpuSurfaceEvents(comptime Runtime: type) type {
392
641
  // transport toggle), which chrome shortcuts deliberately
393
642
  // refuse (`validateShortcut` demands a modifier so global
394
643
  // registration can never steal typing). The ui-app layer
395
- // maps it through `Options.on_key`; with a target present
396
- // the routed event above carries the same fallback duty
397
- // once widget dispatch declines the key. A key that just
398
- // dismissed a surface was consumed by the dismissal and
399
- // never falls through.
644
+ // maps it through `Options.on_key` (releases only for
645
+ // apps that opt into `key_release_events` — a terminal
646
+ // forwarding the kitty protocol's event reporting); with
647
+ // a target present the routed event above carries the
648
+ // same fallback duty once widget dispatch declines the
649
+ // key. A key that just dismissed a surface was consumed
650
+ // by the dismissal and never falls through.
400
651
  if (runtimeFindViewIndex(self, input_event.window_id, input_event.label)) |index| {
401
- if (self.views[index].kind == .gpu_surface and self.views[index].focused) {
652
+ // The composition-ownership gate again (see
653
+ // `targetless_composition_owns_keys` above): while
654
+ // this surface holds a target-less preedit, an
655
+ // unchorded key_down is input-method machinery —
656
+ // candidate navigation, the confirming Enter on
657
+ // hosts that surface the key before the commit —
658
+ // never an app-level key (a terminal mapping Enter
659
+ // to CR would submit the half-composed command).
660
+ // Hosts that run the input-method filter BEFORE
661
+ // surfacing the key (GTK) suppress consumed
662
+ // composition keys at the source, so a key_down
663
+ // arriving after the resolution is a genuine
664
+ // keystroke on every host and always flows.
665
+ if (self.views[index].kind == .gpu_surface and self.views[index].focused and !targetless_composition_owns_keys) {
666
+ // Target-less, but the FOCUSED widget's id still
667
+ // rides along (0 = nothing focused): consumers
668
+ // that own focused input without editor state —
669
+ // the terminal element — resolve their widget
670
+ // from it.
671
+ const focused_id = self.views[index].canvas_widget_focused_id;
402
672
  try self.dispatchEvent(app, .{ .canvas_widget_keyboard = .{
403
673
  .window_id = input_event.window_id,
404
674
  .view_label = self.views[index].label,
405
675
  .keyboard = .{
406
- .phase = .key_down,
676
+ .phase = if (input_event.kind == .key_up) .key_up else .key_down,
677
+ .focused_id = if (focused_id != 0) focused_id else null,
407
678
  .key = input_event.key,
408
679
  .text = input_event.text,
409
680
  .modifiers = canvas_frame_helpers.canvasWidgetKeyboardModifiers(input_event.modifiers),
@@ -414,6 +685,154 @@ pub fn RuntimeGpuSurfaceEvents(comptime Runtime: type) type {
414
685
  }
415
686
  if (widget_text_input_event) |text_input_event| {
416
687
  try self.dispatchEvent(app, .{ .canvas_widget_keyboard = text_input_event });
688
+ } else if (!widget_surface_dismissed and !ime_widget_owner_orphaned and !ime_grace_swallow) {
689
+ // No focused text widget consumed this text: committed
690
+ // text still reaches the app as a TARGET-LESS text event
691
+ // — the key_down fallback's typing twin, for apps that
692
+ // consume typing with no text-entry widget focused (a
693
+ // terminal grid). IME is handled here too, since no
694
+ // focused editor tracks the composition: a preedit
695
+ // (`ime_set_composition`) is buffered but NOT delivered
696
+ // (provisional), and the commit delivers the composed
697
+ // text — the host emits an EMPTY commit when the marked
698
+ // text is committed unchanged, so the bytes come from
699
+ // the buffered preedit. Only committed UTF-8 ever
700
+ // reaches `on_text`; key names never reconstruct text.
701
+ // Preedit state is scoped to its ORIGINATING surface (the
702
+ // way a focused widget's editor scopes composition to the
703
+ // widget): only the owning surface's events consume or
704
+ // clear the buffer, so surface B's empty commit can never
705
+ // insert a composition typed into surface A.
706
+ const owns_preedit = self.targetless_ime_preedit_len > 0 and
707
+ self.targetless_ime_preedit_window == input_event.window_id and
708
+ std.mem.eql(
709
+ u8,
710
+ self.targetless_ime_preedit_label[0..self.targetless_ime_preedit_label_len],
711
+ input_event.label,
712
+ );
713
+ const committed: ?[]const u8 = switch (input_event.kind) {
714
+ .text_input => blk: {
715
+ // A command-chorded text event is a SHORTCUT, not
716
+ // typing — the same gate the focused-widget text
717
+ // path applies (`canvasWidgetTextEditEventFromGpuInput`),
718
+ // so Ctrl/Cmd+C never delivers both the chord and
719
+ // a literal "c". It neither commits nor disturbs
720
+ // a composition in progress.
721
+ if (canvas_frame_helpers.gpuInputHasTextCommandModifier(input_event)) break :blk null;
722
+ if (owns_preedit) self.targetless_ime_preedit_len = 0;
723
+ break :blk if (input_event.text.len > 0) input_event.text else null;
724
+ },
725
+ // A key_down CARRYING text is the other committed-text
726
+ // shape: hosts without a separate text event for plain
727
+ // typing (the GTK path when no input method consumes
728
+ // the key) deliver the printable on the key event
729
+ // itself — the focused-widget rule
730
+ // (`canvasWidgetTextEditEventFromGpuInput`) inserts
731
+ // from it, and the target-less fallback must too or
732
+ // ordinary typing never reaches `on_text` there. Same
733
+ // chord gate; specials (enter, backspace) carry no
734
+ // text on any host, so nothing doubles with the
735
+ // key_down fallback dispatched above.
736
+ .key_down => blk: {
737
+ if (canvas_frame_helpers.gpuInputHasTextCommandModifier(input_event)) break :blk null;
738
+ if (input_event.text.len == 0) break :blk null;
739
+ if (owns_preedit) self.targetless_ime_preedit_len = 0;
740
+ break :blk input_event.text;
741
+ },
742
+ .ime_set_composition => blk: {
743
+ // Buffer the FULL composition (grow to fit, never
744
+ // truncate): each set_composition REPLACES the
745
+ // preedit, so this holds one composition's bytes
746
+ // — and takes ownership for this surface (at most
747
+ // one system composition exists at a time). If
748
+ // growth fails, CLEAR the preedit rather than
749
+ // leave the prior (now superseded) composition
750
+ // active — a later empty commit must never insert
751
+ // stale text the user has since replaced.
752
+ if (input_event.text.len > self.targetless_ime_preedit.len) {
753
+ if (self.owned_allocator.realloc(self.targetless_ime_preedit, input_event.text.len)) |grown| {
754
+ self.targetless_ime_preedit = grown;
755
+ } else |_| {
756
+ self.targetless_ime_preedit_len = 0;
757
+ break :blk null;
758
+ }
759
+ }
760
+ @memcpy(self.targetless_ime_preedit[0..input_event.text.len], input_event.text);
761
+ self.targetless_ime_preedit_len = input_event.text.len;
762
+ self.targetless_ime_preedit_window = input_event.window_id;
763
+ const label_len = @min(input_event.label.len, self.targetless_ime_preedit_label.len);
764
+ @memcpy(self.targetless_ime_preedit_label[0..label_len], input_event.label[0..label_len]);
765
+ self.targetless_ime_preedit_label_len = label_len;
766
+ break :blk null; // preedit is provisional
767
+ },
768
+ .ime_commit_composition => blk: {
769
+ // An EMPTY commit means "commit the marked text
770
+ // unchanged" — buffered bytes stand in ONLY when
771
+ // this surface owns them; another surface's empty
772
+ // commit commits nothing of ours.
773
+ const text = if (input_event.text.len > 0)
774
+ input_event.text
775
+ else if (owns_preedit)
776
+ self.targetless_ime_preedit[0..self.targetless_ime_preedit_len]
777
+ else
778
+ "";
779
+ if (owns_preedit) self.targetless_ime_preedit_len = 0;
780
+ break :blk if (text.len > 0) text else null;
781
+ },
782
+ .ime_cancel_composition => blk: {
783
+ if (owns_preedit) {
784
+ self.targetless_ime_preedit_len = 0;
785
+ // Hold one event for the converted-commit
786
+ // shape (cancel-then-text_input): the
787
+ // trailing text still belongs to this
788
+ // surface's composition. A plain cancel's
789
+ // own key_down disarms it.
790
+ self.targetless_ime_commit_grace = true;
791
+ }
792
+ break :blk null;
793
+ },
794
+ else => null,
795
+ };
796
+ // An IME RESOLUTION is never a widget's claimed key: the
797
+ // structural-claim gate exists so one keystroke cannot
798
+ // both activate a focused control and type its literal
799
+ // character, but a composition's committed result (an
800
+ // owned empty commit, or a converted commit's trailing
801
+ // text riding the grace) is the END of a sequence the
802
+ // widget never participated in — a focused button's
803
+ // Space claim must not eat it.
804
+ const ime_resolution = targetless_commit_grace or
805
+ input_event.kind == .ime_commit_composition;
806
+ if (committed) |text| {
807
+ if (runtimeFindViewIndex(self, input_event.window_id, input_event.label)) |index| {
808
+ if (self.views[index].kind == .gpu_surface and self.views[index].focused and
809
+ (!committed_text_claimed or ime_resolution))
810
+ {
811
+ // The focused widget's id rides the
812
+ // target-less event (see the key fallback
813
+ // above): a focused terminal resolves its
814
+ // typing channel from it.
815
+ const focused_id = self.views[index].canvas_widget_focused_id;
816
+ try self.dispatchEvent(app, .{
817
+ .canvas_widget_keyboard = .{
818
+ .window_id = input_event.window_id,
819
+ .view_label = self.views[index].label,
820
+ .keyboard = .{
821
+ .phase = .text_input,
822
+ .focused_id = if (focused_id != 0) focused_id else null,
823
+ .key = input_event.key,
824
+ .text = text,
825
+ // Mark it committed so the ui-app
826
+ // `on_text` gate (insert_text only)
827
+ // delivers it.
828
+ .edit = .{ .insert_text = text },
829
+ .modifiers = canvas_frame_helpers.canvasWidgetKeyboardModifiers(input_event.modifiers),
830
+ },
831
+ },
832
+ });
833
+ }
834
+ }
835
+ }
417
836
  }
418
837
  // Wheel and keyboard scroll mutations above noted pending
419
838
  // scroll events on the view; deliver them after the input's
@@ -440,6 +859,43 @@ pub fn RuntimeGpuSurfaceEvents(comptime Runtime: type) type {
440
859
  try CanvasWidgetDisplayMethods().settleDeferredCanvasWidgetAccessibility(self);
441
860
  }
442
861
 
862
+ /// Whether a view-local point sits inside the view's surface
863
+ /// (the consumed-release retirement's outside test).
864
+ fn gpuViewContainsPoint(view: anytype, x: f32, y: f32) bool {
865
+ // The engine's own half-open rectangle containment, so a
866
+ // release at exactly the right or bottom edge counts as
867
+ // outside the way every hit test already treats it.
868
+ return geometry.RectF.fromSize(view.gpu_size).containsPoint(geometry.PointF.init(x, y));
869
+ }
870
+
871
+ /// Whether the view's focused widget structurally claims this
872
+ /// input's key as a control intent (Space/Enter activation and
873
+ /// kin) — the widget-precedence contract's step 2, applied to
874
+ /// the target-less committed-text fallback: a focused button
875
+ /// consumes Space to press, so the same keystroke must not ALSO
876
+ /// type a literal space through `on_text`. Characters the widget
877
+ /// does not claim still flow (typing into a terminal while a
878
+ /// button happens to hold focus). Text events that carry no key
879
+ /// name (a host's insertText path) probe by the one activation
880
+ /// key that produces text — a space payload probes as "space".
881
+ fn targetlessCommittedTextClaimedByFocusedWidget(self: *Runtime, index: usize, input_event: platform.GpuSurfaceInputEvent) bool {
882
+ const focused_id = self.views[index].canvas_widget_focused_id;
883
+ if (focused_id == 0) return false;
884
+ const node = self.views[index].widgetLayoutTree().findById(focused_id) orelse return false;
885
+ const probe_key = if (input_event.key.len > 0)
886
+ input_event.key
887
+ else if (std.mem.eql(u8, input_event.text, " "))
888
+ "space"
889
+ else
890
+ return false;
891
+ return canvas.widgetKeyboardControlIntent(node.widget, .{
892
+ .phase = .key_down,
893
+ .focused_id = focused_id,
894
+ .key = probe_key,
895
+ .modifiers = canvas_frame_helpers.canvasWidgetKeyboardModifiers(input_event.modifiers),
896
+ }) != null;
897
+ }
898
+
443
899
  /// Drain the view's pending scroll-event set into
444
900
  /// `canvas_widget_scroll` app events. Each entry reads the node's
445
901
  /// CURRENT scroll state, so motion that occurred since the note
@@ -659,6 +1115,10 @@ fn setFocusedView(self: anytype, window_id: platform.WindowId, label: []const u8
659
1115
  // blurred view's tooltip floating.
660
1116
  if (was_focused and !view.focused) {
661
1117
  try runtime_canvas_widget_events.RuntimeCanvasWidgetEvents(@TypeOf(self.*)).resetCanvasTooltipIntentForViewBlur(self, view_index);
1118
+ // Focus loss also disarms the cancel-to-commit grace and
1119
+ // releases its owner pin — the shared blur hygiene every
1120
+ // focus-mutation path applies (see `clearImeGraceOnViewBlur`).
1121
+ runtime_view.clearImeGraceOnViewBlur(self, view);
662
1122
  }
663
1123
  }
664
1124
  for (self.webviews[0..self.webview_count]) |*webview| {