@native-sdk/cli 0.5.3 → 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 (172) hide show
  1. package/build/app.zig +91 -2
  2. package/build.zig +756 -5
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +19 -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 +416 -9
  9. package/packages/core/sdk/core.ts +706 -5
  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 +3187 -264
  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 +42 -4
  17. package/skill-data/ts-core/SKILL.md +13 -3
  18. package/src/app_runner/root.zig +112 -10
  19. package/src/app_runner/ts_core_main.zig +4 -0
  20. package/src/automation/layout_fingerprint.zig +160 -0
  21. package/src/automation/protocol.zig +82 -33
  22. package/src/automation/root.zig +1 -0
  23. package/src/automation/server.zig +1 -1
  24. package/src/automation/snapshot.zig +68 -9
  25. package/src/automation/watcher.zig +1 -1
  26. package/src/embed/host.zig +5 -0
  27. package/src/embed/tests.zig +119 -1
  28. package/src/embed/ui_host.zig +13 -0
  29. package/src/platform/linux/gtk_host.c +639 -16
  30. package/src/platform/linux/gtk_host.h +25 -0
  31. package/src/platform/linux/root.zig +351 -7
  32. package/src/platform/macos/appkit_host.h +152 -8
  33. package/src/platform/macos/appkit_host.m +1816 -100
  34. package/src/platform/macos/cef_host.mm +372 -11
  35. package/src/platform/macos/root.zig +379 -11
  36. package/src/platform/null_platform.zig +696 -9
  37. package/src/platform/null_platform_tests.zig +35 -0
  38. package/src/platform/root.zig +42 -0
  39. package/src/platform/types.zig +524 -18
  40. package/src/platform/windows/root.zig +432 -9
  41. package/src/platform/windows/webview2_host.cpp +482 -45
  42. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  43. package/src/primitives/app_manifest/root.zig +1 -0
  44. package/src/primitives/app_manifest/types.zig +32 -0
  45. package/src/primitives/app_manifest/validation.zig +18 -0
  46. package/src/primitives/canvas/a11y_audit.zig +33 -11
  47. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  48. package/src/primitives/canvas/drawing.zig +25 -0
  49. package/src/primitives/canvas/equality.zig +8 -0
  50. package/src/primitives/canvas/events.zig +103 -16
  51. package/src/primitives/canvas/frame.zig +125 -1
  52. package/src/primitives/canvas/layout_audit.zig +22 -5
  53. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  54. package/src/primitives/canvas/render_tests.zig +13 -10
  55. package/src/primitives/canvas/root.zig +48 -0
  56. package/src/primitives/canvas/terminal_box.zig +471 -0
  57. package/src/primitives/canvas/terminal_grid.zig +978 -0
  58. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  59. package/src/primitives/canvas/test_support.zig +1 -0
  60. package/src/primitives/canvas/tests.zig +2 -0
  61. package/src/primitives/canvas/text_tests.zig +39 -4
  62. package/src/primitives/canvas/tokens.zig +115 -12
  63. package/src/primitives/canvas/ui.zig +535 -12
  64. package/src/primitives/canvas/ui_markup.zig +253 -36
  65. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  66. package/src/primitives/canvas/ui_markup_compiled.zig +195 -9
  67. package/src/primitives/canvas/ui_markup_compiled_tests.zig +188 -8
  68. package/src/primitives/canvas/ui_markup_contract.zig +89 -6
  69. package/src/primitives/canvas/ui_markup_contract_tests.zig +65 -1
  70. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  71. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  72. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  73. package/src/primitives/canvas/ui_markup_tests.zig +178 -9
  74. package/src/primitives/canvas/ui_markup_view.zig +220 -11
  75. package/src/primitives/canvas/ui_markup_view_tests.zig +553 -28
  76. package/src/primitives/canvas/ui_schema.zig +110 -17
  77. package/src/primitives/canvas/ui_schema_tests.zig +18 -10
  78. package/src/primitives/canvas/ui_tests.zig +141 -9
  79. package/src/primitives/canvas/widget_access.zig +28 -2
  80. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  81. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  82. package/src/primitives/canvas/widget_layout.zig +59 -11
  83. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  84. package/src/primitives/canvas/widget_render.zig +272 -18
  85. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  86. package/src/primitives/canvas/widget_routing.zig +59 -7
  87. package/src/primitives/canvas/widget_runtime.zig +23 -0
  88. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  89. package/src/primitives/canvas/widget_semantics.zig +119 -11
  90. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  91. package/src/primitives/canvas/widget_tree.zig +17 -0
  92. package/src/primitives/canvas/widgets.zig +132 -0
  93. package/src/root.zig +23 -0
  94. package/src/runtime/api.zig +76 -5
  95. package/src/runtime/automation_commands.zig +20 -1
  96. package/src/runtime/automation_snapshot.zig +25 -1
  97. package/src/runtime/automation_widget_dispatch.zig +73 -8
  98. package/src/runtime/bridge_payload.zig +8 -0
  99. package/src/runtime/bridge_responses.zig +5 -0
  100. package/src/runtime/builtin_bridge.zig +9 -4
  101. package/src/runtime/canvas_font_tests.zig +439 -2
  102. package/src/runtime/canvas_fonts.zig +61 -6
  103. package/src/runtime/canvas_frame.zig +62 -7
  104. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  105. package/src/runtime/canvas_frame_helpers.zig +190 -1
  106. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  107. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  108. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  109. package/src/runtime/canvas_image_tests.zig +195 -0
  110. package/src/runtime/canvas_images.zig +45 -12
  111. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  112. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  113. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  114. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  115. package/src/runtime/canvas_widget_display.zig +1 -1
  116. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  117. package/src/runtime/canvas_widget_events.zig +240 -24
  118. package/src/runtime/canvas_widget_runtime.zig +213 -29
  119. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  120. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  121. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  122. package/src/runtime/canvas_widget_state.zig +6 -0
  123. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  124. package/src/runtime/core.zig +131 -13
  125. package/src/runtime/effects.zig +8136 -180
  126. package/src/runtime/effects_channel_tests.zig +3519 -0
  127. package/src/runtime/effects_fetch_tests.zig +89 -1
  128. package/src/runtime/effects_file_tests.zig +41 -1
  129. package/src/runtime/effects_image_tests.zig +1304 -0
  130. package/src/runtime/effects_pty_tests.zig +1362 -0
  131. package/src/runtime/effects_tests.zig +167 -9
  132. package/src/runtime/effects_video_tests.zig +3609 -0
  133. package/src/runtime/flow.zig +42 -4
  134. package/src/runtime/gpu_surface_events.zig +473 -13
  135. package/src/runtime/media_surface.zig +116 -11
  136. package/src/runtime/media_surface_tests.zig +61 -0
  137. package/src/runtime/pty.zig +1252 -0
  138. package/src/runtime/pty_windows.zig +1780 -0
  139. package/src/runtime/root.zig +36 -0
  140. package/src/runtime/runtime_event_tests.zig +20 -1
  141. package/src/runtime/session_blobs.zig +689 -0
  142. package/src/runtime/session_journal.zig +432 -62
  143. package/src/runtime/session_record.zig +43 -2
  144. package/src/runtime/session_replay.zig +556 -20
  145. package/src/runtime/session_tests.zig +2087 -1
  146. package/src/runtime/shell_layout.zig +7 -0
  147. package/src/runtime/terminal_session.zig +1569 -0
  148. package/src/runtime/terminal_session_tests.zig +450 -0
  149. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  150. package/src/runtime/terminal_vt_stub.zig +13 -0
  151. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  152. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  153. package/src/runtime/tests.zig +8 -0
  154. package/src/runtime/ts_core_host.zig +1320 -45
  155. package/src/runtime/ts_core_host_tests.zig +1100 -1
  156. package/src/runtime/ts_ui_app.zig +23 -5
  157. package/src/runtime/ui_app.zig +2444 -69
  158. package/src/runtime/ui_app_tests.zig +3171 -744
  159. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  160. package/src/runtime/ui_app_window_tests.zig +736 -0
  161. package/src/runtime/view.zig +190 -2
  162. package/src/runtime/view_widget_scroll.zig +218 -68
  163. package/src/runtime/view_widget_text.zig +11 -2
  164. package/src/runtime/view_widget_tree.zig +111 -3
  165. package/src/runtime/window_command_bridge_tests.zig +117 -0
  166. package/src/runtime/window_storage.zig +77 -2
  167. package/src/runtime/window_view_runtime.zig +34 -2
  168. package/src/runtime/window_views.zig +62 -1
  169. package/src/terminal_session_tests_root.zig +15 -0
  170. package/src/tooling/manifest.zig +75 -0
  171. package/src/tooling/raw_manifest.zig +2 -0
  172. package/src/tooling/templates.zig +244 -8
@@ -24,8 +24,17 @@
24
24
  * NATIVE_SDK_ALLOW_WEBVIEW2_STUB. */
25
25
  #if defined(NATIVE_SDK_ALLOW_WEBKITGTK_STUB)
26
26
  #define NATIVE_SDK_HAS_WEBKITGTK 0
27
- #pragma message("Embedded web layer excluded by the build configuration: building the GTK host without WebKitGTK (canvas apps unaffected; WebView loads will report WebViewNotFound)")
28
- /* The stubbed web layer keeps the window/webview bookkeeping SHAPE so
27
+ /* Deliberately NO compiler diagnostic in this branch — not even an
28
+ * informational #pragma message. The stub is the expected, configured
29
+ * state of every native-only Linux build, and zig renders every clang
30
+ * diagnostic of a failing translation unit as `error:` (its serialized
31
+ * clang diagnostics carry no severity into the error bundle), so an
32
+ * informational note here masquerades as the build-killing error the
33
+ * moment any unrelated real error appears anywhere in this file. The
34
+ * teaching lives where it is actionable instead: a stubbed host
35
+ * reports WebViewNotFound the moment an app actually uses a WebView.
36
+ *
37
+ * The stubbed web layer keeps the window/webview bookkeeping SHAPE so
29
38
  * every GTK-only path (overlay reordering, focus lookups, window
30
39
  * teardown) compiles unchanged: the web-view pointers below are opaque
31
40
  * and permanently NULL — every path that could create one is compiled
@@ -43,6 +52,18 @@ typedef struct native_sdk_absent_content_manager WebKitUserContentManager;
43
52
  #error "webkit/webkit.h not found: install the WebKitGTK 6.0 development package (libwebkitgtk-6.0-dev on Debian/Ubuntu), or define NATIVE_SDK_ALLOW_WEBKITGTK_STUB to build without the embedded web layer"
44
53
  #endif
45
54
 
55
+ /* G_APPLICATION_DEFAULT_FLAGS arrived in GLib 2.74 as the
56
+ * non-deprecated spelling of "no flags". This host's GTK floor is 4.10
57
+ * (the GtkFileDialog family below), and GTK 4.10's own GLib floor is
58
+ * 2.72 — distros that backport GTK 4.10 onto a GLib 2.72 base (Ubuntu
59
+ * 22.04 derivatives) must still compile this file. Same value, gated to
60
+ * older GLib only; the pre-2.74 name G_APPLICATION_FLAGS_NONE is not
61
+ * used because it is deprecated from 2.74 on and would emit a warning
62
+ * exactly where the newer name exists. */
63
+ #if !GLIB_CHECK_VERSION(2, 74, 0)
64
+ #define G_APPLICATION_DEFAULT_FLAGS ((GApplicationFlags) 0)
65
+ #endif
66
+
46
67
  #define NATIVE_SDK_MAX_WINDOWS 16
47
68
  #define NATIVE_SDK_MAX_WEBVIEWS 16
48
69
  #define NATIVE_SDK_MAX_TIMERS 64
@@ -99,6 +120,40 @@ typedef struct native_sdk_gtk_menu_action {
99
120
  struct native_sdk_gtk_host *host;
100
121
  } native_sdk_gtk_menu_action_t;
101
122
 
123
+ /* One presented context menu (GtkPopoverMenu): its GMenu dispatches
124
+ * through a per-invocation GSimpleActionGroup inserted on the presenting
125
+ * widget, so nothing ever lands in the application action map the menu
126
+ * bar owns. Exactly one CONTEXT_MENU_ACTION event is emitted per
127
+ * request: the selected item's action sets `emitted`, and the popover's
128
+ * "closed" teardown idle emits the dismissal (item 0) only when no
129
+ * selection did. `popover` and `parent` are weak pointers (GLib nulls
130
+ * them if the widgets die first, e.g. the owning window closes while
131
+ * the menu is up); `host->context_menu` tracks the single CURRENT
132
+ * instance and `host->context_menu_teardowns` every state whose
133
+ * teardown idle is queued, so destroy can cancel each pending source
134
+ * before the captured pointers go stale. */
135
+ typedef struct native_sdk_gtk_context_menu {
136
+ struct native_sdk_gtk_host *host;
137
+ uint64_t window_id;
138
+ char *view_label;
139
+ uint64_t token;
140
+ /* Per-invocation action-group namespace ("native-sdk-context-<serial>").
141
+ * Popovers are asynchronous, so a re-click can install a successor
142
+ * menu while this one's deferred teardown idle is still queued; a
143
+ * shared namespace would let the stale teardown remove the action
144
+ * group the SUCCESSOR just inserted on the same parent, leaving the
145
+ * replacement menu inert. A unique name per request means teardown
146
+ * can only ever remove its own group. */
147
+ char group_name[48];
148
+ GtkWidget *popover;
149
+ GtkWidget *parent;
150
+ int emitted;
151
+ guint teardown_idle;
152
+ /* Link in `host->context_menu_teardowns` while `teardown_idle` is
153
+ * queued (threaded through the states themselves; no allocation). */
154
+ struct native_sdk_gtk_context_menu *next_teardown;
155
+ } native_sdk_gtk_context_menu_t;
156
+
102
157
  /* One rectangle of the runtime-pushed window-drag mirror (markup
103
158
  * `window-drag="true"`), in the owning gpu_surface view's logical
104
159
  * coordinates. Exclusions are the press-claiming widgets INSIDE a drag
@@ -188,6 +243,26 @@ typedef struct native_sdk_gtk_native_view {
188
243
  * matching release is swallowed too (no orphaned pointer_up). */
189
244
  native_sdk_gtk_drag_region_t *drag_regions;
190
245
  size_t drag_region_count;
246
+ /* Keys whose composition-consumed key_down was suppressed (see
247
+ * native_sdk_gpu_key_pressed): the matching key_up is swallowed too,
248
+ * so the runtime never sees an orphan release — the drag-region
249
+ * claimed-press discipline, applied to composition keys. Tracked by
250
+ * PHYSICAL KEYCODE, never keyval: releasing Shift before the key
251
+ * changes the keyval between press and release (Shift+A records A,
252
+ * the release reads a) while the keycode names the same key both
253
+ * ways. Zero means an empty slot; bounded rollover, and an
254
+ * overflowed press falls back to emitting its release (activation
255
+ * paths ignore a release without its press, so the fallback is
256
+ * hygiene, not correctness). */
257
+ guint gpu_suppressed_keycodes[8];
258
+ /* A plain (no converted commit) composition cancel was just emitted
259
+ * and its resolving key's own key_down is being suppressed: the
260
+ * runtime's cancel-to-commit grace armed by that cancel expects the
261
+ * key_down as its disarm, so the suppression emits a synthetic
262
+ * duplicate cancel instead — a no-op everywhere except the grace
263
+ * probe it disarms. Cleared when a converted commit's text_input
264
+ * follows the cancel (the text itself consumes the grace). */
265
+ int gpu_cancel_disarm_pending;
191
266
  int gpu_drag_claimed_press;
192
267
  } native_sdk_gtk_native_view_t;
193
268
 
@@ -326,6 +401,13 @@ struct native_sdk_gtk_host {
326
401
  int did_shutdown;
327
402
  int app_active;
328
403
  guint frame_timer;
404
+ /* The quit verb's queued stop turn (native_sdk_gtk_stop), tracked
405
+ * like frame_timer so it can be coalesced while pending and
406
+ * removed at destroy — a bare g_idle_add would leave a second stop
407
+ * request (the shutdown handler's error path can make one) holding
408
+ * a freed host after the loop quits. Zero while none is queued;
409
+ * the idle clears it as its first act. */
410
+ guint stop_idle_source;
329
411
 
330
412
  char **allowed_origins;
331
413
  int allowed_origins_count;
@@ -338,10 +420,25 @@ struct native_sdk_gtk_host {
338
420
  GMenuModel *menu_model;
339
421
  native_sdk_gtk_menu_action_t menu_actions[NATIVE_SDK_MAX_MENU_ITEMS];
340
422
  int menu_action_count;
423
+ /* The one live context menu (menus are modal-per-pointer, so one at
424
+ * a time); owned here between show and the teardown idle. A
425
+ * superseded menu is no longer pointed to from here but stays alive
426
+ * until its own teardown idle runs — tracked below so destroy can
427
+ * still reach it. */
428
+ native_sdk_gtk_context_menu_t *context_menu;
429
+ /* Every menu state with a teardown idle queued (the "closed"
430
+ * handler pushes, native_sdk_context_menu_free unlinks) — tracked
431
+ * like stop_idle_source so destroy can g_source_remove each pending
432
+ * receipt and free its captured state inline instead of letting the
433
+ * idle fire into a freed host. Singly linked through the states. */
434
+ native_sdk_gtk_context_menu_t *context_menu_teardowns;
435
+ /* Feeds each context menu's per-invocation action-group name. */
436
+ uint64_t context_menu_serial;
341
437
  native_sdk_gtk_audio_t audio;
342
438
  };
343
439
 
344
440
  static void native_sdk_emit(native_sdk_gtk_host_t *host, native_sdk_gtk_event_t event);
441
+ static void native_sdk_context_menu_free(native_sdk_gtk_context_menu_t *menu);
345
442
  static gboolean native_sdk_nudge_chrome_requery(gpointer data);
346
443
  static gboolean native_sdk_on_file_drop(GtkDropTarget *target, const GValue *value, double x, double y, gpointer data);
347
444
  static GtkWindow *native_sdk_parent_window(native_sdk_gtk_host_t *host);
@@ -523,6 +620,39 @@ static int native_sdk_valid_native_view_frame(double x, double y, double width,
523
620
  return x >= 0 && y >= 0 && width >= 0 && height >= 0;
524
621
  }
525
622
 
623
+ /* String-parameter byte caps for the native-view calls, mirroring the
624
+ * runtime's platform limits (types.zig: max_view_label_bytes 64,
625
+ * max_view_role_bytes 64, max_view_accessibility_label_bytes 256,
626
+ * max_view_text_bytes 1024, and the command-id cap 128). The runtime
627
+ * validates the same bounds before ever calling in, so a length past
628
+ * them at THIS boundary means the arguments were corrupted in transit —
629
+ * a caller compiled with a broken C ABI hands over shifted stack slots
630
+ * where a pointer poses as a length. Refuse loudly and name the field
631
+ * instead of memcpy-ing from a garbage pointer and faulting. */
632
+ #define NATIVE_SDK_MAX_VIEW_LABEL_BYTES 64
633
+ #define NATIVE_SDK_MAX_VIEW_ROLE_BYTES 64
634
+ #define NATIVE_SDK_MAX_VIEW_ACCESSIBILITY_LABEL_BYTES 256
635
+ #define NATIVE_SDK_MAX_VIEW_TEXT_BYTES 1024
636
+ #define NATIVE_SDK_MAX_VIEW_COMMAND_BYTES 128
637
+
638
+ static int native_sdk_valid_native_view_string(const char *what, size_t len, size_t max_len) {
639
+ if (len <= max_len) return 1;
640
+ g_warning("native view %s length %zu exceeds the platform cap %zu; refusing the call - "
641
+ "the runtime never sends this, so the arguments likely arrived corrupted "
642
+ "(a miscompiled C-ABI boundary hands the host shifted stack slots)",
643
+ what, len, max_len);
644
+ return 0;
645
+ }
646
+
647
+ static int native_sdk_valid_native_view_strings(size_t label_len, size_t parent_len, size_t role_len, size_t accessibility_label_len, size_t text_len, size_t command_len) {
648
+ return native_sdk_valid_native_view_string("label", label_len, NATIVE_SDK_MAX_VIEW_LABEL_BYTES) &&
649
+ native_sdk_valid_native_view_string("parent", parent_len, NATIVE_SDK_MAX_VIEW_LABEL_BYTES) &&
650
+ native_sdk_valid_native_view_string("role", role_len, NATIVE_SDK_MAX_VIEW_ROLE_BYTES) &&
651
+ native_sdk_valid_native_view_string("accessibility_label", accessibility_label_len, NATIVE_SDK_MAX_VIEW_ACCESSIBILITY_LABEL_BYTES) &&
652
+ native_sdk_valid_native_view_string("text", text_len, NATIVE_SDK_MAX_VIEW_TEXT_BYTES) &&
653
+ native_sdk_valid_native_view_string("command", command_len, NATIVE_SDK_MAX_VIEW_COMMAND_BYTES);
654
+ }
655
+
526
656
  static int native_sdk_native_extent(double value) {
527
657
  return value > 0 ? (int)(value + 0.5) : 0;
528
658
  }
@@ -728,6 +858,7 @@ static GtkWidget *native_sdk_make_native_widget(int kind, const char *label, con
728
858
  #define NATIVE_SDK_GTK_GPU_INPUT_IME_SET_COMPOSITION 8
729
859
  #define NATIVE_SDK_GTK_GPU_INPUT_IME_COMMIT_COMPOSITION 9
730
860
  #define NATIVE_SDK_GTK_GPU_INPUT_IME_CANCEL_COMPOSITION 10
861
+ #define NATIVE_SDK_GTK_GPU_INPUT_POINTER_CANCEL 11
731
862
 
732
863
  static uint64_t native_sdk_gpu_timestamp_ns(void) {
733
864
  return (uint64_t)g_get_monotonic_time() * 1000ull;
@@ -810,6 +941,9 @@ static void native_sdk_gpu_im_commit(GtkIMContext *context, const char *text, gp
810
941
  native_sdk_emit_gpu_surface_text_input(view, NATIVE_SDK_GTK_GPU_INPUT_IME_CANCEL_COMPOSITION, "", 0, 0);
811
942
  }
812
943
  native_sdk_emit_gpu_surface_text_input(view, NATIVE_SDK_GTK_GPU_INPUT_TEXT_INPUT, text, 0, 0);
944
+ /* The converted commit's trailing text_input consumes the runtime's
945
+ * cancel grace itself; no synthetic disarm is owed. */
946
+ view->gpu_cancel_disarm_pending = 0;
813
947
  }
814
948
 
815
949
  /* IM context preedit (composition) changed. Mirrors AppKit's setMarkedText:
@@ -828,6 +962,11 @@ static void native_sdk_gpu_im_preedit_changed(GtkIMContext *context, gpointer da
828
962
  if (native_sdk_gpu_surface_has_preedit(view)) {
829
963
  native_sdk_gpu_surface_clear_preedit(view);
830
964
  native_sdk_emit_gpu_surface_text_input(view, NATIVE_SDK_GTK_GPU_INPUT_IME_CANCEL_COMPOSITION, "", 0, 0);
965
+ /* A PLAIN cancel (no converted commit follows within this
966
+ * filter pass): the resolving key's suppressed key_down owes
967
+ * the runtime's cancel grace its disarm (see
968
+ * gpu_cancel_disarm_pending). */
969
+ view->gpu_cancel_disarm_pending = 1;
831
970
  }
832
971
  return;
833
972
  }
@@ -1146,13 +1285,30 @@ static int native_sdk_gpu_point_in_drag_region(const native_sdk_gtk_native_view_
1146
1285
  return 0;
1147
1286
  }
1148
1287
 
1288
+ /* GDK numbers pointer buttons primary=1, MIDDLE=2, SECONDARY=3; the
1289
+ * runtime's pointer contract is primary=0, secondary=1, middle=2 (the
1290
+ * web/Win32 ordering, which the secondary-button context-menu check
1291
+ * depends on). A plain subtract-one swaps secondary and middle, so map
1292
+ * explicitly. Extra buttons (back/forward, 8+) keep the subtract-one
1293
+ * offset the runtime already expects for indices above 2. */
1294
+ static int native_sdk_gpu_runtime_button(unsigned int gdk_button) {
1295
+ switch (gdk_button) {
1296
+ case GDK_BUTTON_PRIMARY: return 0;
1297
+ case GDK_BUTTON_SECONDARY: return 1;
1298
+ case GDK_BUTTON_MIDDLE: return 2;
1299
+ case 0: return 0;
1300
+ default: return (int)gdk_button - 1;
1301
+ }
1302
+ }
1303
+
1149
1304
  static void native_sdk_gpu_pointer_pressed(GtkGestureClick *gesture, int n_press, double x, double y, gpointer data) {
1150
1305
  native_sdk_gtk_native_view_t *view = data;
1151
1306
  if (!view || !view->widget) return;
1152
1307
  gtk_widget_grab_focus(view->widget);
1153
1308
  view->gpu_pointer_x = x;
1154
1309
  view->gpu_pointer_y = y;
1155
- const int button = (int)gtk_gesture_single_get_current_button(GTK_GESTURE_SINGLE(gesture)) - 1;
1310
+ const unsigned int gdk_button = gtk_gesture_single_get_current_button(GTK_GESTURE_SINGLE(gesture));
1311
+ const int button = native_sdk_gpu_runtime_button(gdk_button);
1156
1312
  const uint32_t modifiers = native_sdk_gpu_modifier_flags(gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(gesture)));
1157
1313
  /* Stash the press for the widget `window_drag` channel: an
1158
1314
  * interactive window move must begin from the originating device,
@@ -1167,7 +1323,9 @@ static void native_sdk_gpu_pointer_pressed(GtkGestureClick *gesture, int n_press
1167
1323
  }
1168
1324
  view->window->last_press_device = event ? gdk_event_get_device(event) : NULL;
1169
1325
  view->window->last_press_time = event ? gdk_event_get_time(event) : GDK_CURRENT_TIME;
1170
- view->window->last_press_button = button < 0 ? 0 : button + 1;
1326
+ /* gdk_toplevel_begin_move takes the GDK button number, so the
1327
+ * stash keeps GDK numbering — no runtime mapping here. */
1328
+ view->window->last_press_button = (int)gdk_button;
1171
1329
  view->window->last_press_x = window_point.x;
1172
1330
  view->window->last_press_y = window_point.y;
1173
1331
  }
@@ -1189,7 +1347,7 @@ static void native_sdk_gpu_pointer_pressed(GtkGestureClick *gesture, int n_press
1189
1347
  return;
1190
1348
  }
1191
1349
  view->gpu_pointer_down = 1;
1192
- native_sdk_emit_gpu_surface_input(view, NATIVE_SDK_GTK_GPU_INPUT_POINTER_DOWN, x, y, button < 0 ? 0 : button, 0, 0, "", "", modifiers);
1350
+ native_sdk_emit_gpu_surface_input(view, NATIVE_SDK_GTK_GPU_INPUT_POINTER_DOWN, x, y, button, 0, 0, "", "", modifiers);
1193
1351
  }
1194
1352
 
1195
1353
  static void native_sdk_gpu_pointer_released(GtkGestureClick *gesture, int n_press, double x, double y, gpointer data) {
@@ -1205,9 +1363,31 @@ static void native_sdk_gpu_pointer_released(GtkGestureClick *gesture, int n_pres
1205
1363
  view->gpu_drag_claimed_press = 0;
1206
1364
  return;
1207
1365
  }
1208
- const int button = (int)gtk_gesture_single_get_current_button(GTK_GESTURE_SINGLE(gesture)) - 1;
1366
+ const int button = native_sdk_gpu_runtime_button(gtk_gesture_single_get_current_button(GTK_GESTURE_SINGLE(gesture)));
1209
1367
  const uint32_t modifiers = native_sdk_gpu_modifier_flags(gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(gesture)));
1210
- native_sdk_emit_gpu_surface_input(view, NATIVE_SDK_GTK_GPU_INPUT_POINTER_UP, x, y, button < 0 ? 0 : button, 0, 0, "", "", modifiers);
1368
+ native_sdk_emit_gpu_surface_input(view, NATIVE_SDK_GTK_GPU_INPUT_POINTER_UP, x, y, button, 0, 0, "", "", modifiers);
1369
+ }
1370
+
1371
+ static void native_sdk_gpu_pointer_cancelled(GtkGesture *gesture, GdkEventSequence *sequence, gpointer data) {
1372
+ (void)gesture;
1373
+ (void)sequence;
1374
+ native_sdk_gtk_native_view_t *view = data;
1375
+ if (!view) return;
1376
+ /* The pressed sequence was cancelled - the grab transferred (an
1377
+ * interactive window move beginning) or broken (a compositor grab,
1378
+ * widget reparenting). A drag-claimed press consumed the down before
1379
+ * the widget pipeline heard it, so it rolls back silently; a press
1380
+ * the pipeline DID see rolls back like a Win32 capture loss, so no
1381
+ * widget stays pressed and later leave events are not suppressed by
1382
+ * a stale pressed flag. */
1383
+ if (view->gpu_drag_claimed_press) {
1384
+ view->gpu_drag_claimed_press = 0;
1385
+ view->gpu_pointer_down = 0;
1386
+ return;
1387
+ }
1388
+ if (!view->gpu_pointer_down) return;
1389
+ view->gpu_pointer_down = 0;
1390
+ native_sdk_emit_gpu_surface_input(view, NATIVE_SDK_GTK_GPU_INPUT_POINTER_CANCEL, view->gpu_pointer_x, view->gpu_pointer_y, 0, 0, 0, "", "", 0);
1211
1391
  }
1212
1392
 
1213
1393
  static void native_sdk_gpu_pointer_motion(GtkEventControllerMotion *controller, double x, double y, gpointer data) {
@@ -1220,6 +1400,22 @@ static void native_sdk_gpu_pointer_motion(GtkEventControllerMotion *controller,
1220
1400
  native_sdk_emit_gpu_surface_input(view, kind, x, y, 0, 0, 0, "", "", modifiers);
1221
1401
  }
1222
1402
 
1403
+ static void native_sdk_gpu_pointer_leave(GtkEventControllerMotion *controller, gpointer data) {
1404
+ native_sdk_gtk_native_view_t *view = data;
1405
+ if (!view) return;
1406
+ /* The pointer left the canvas without a press in flight: the
1407
+ * window-leave edge the runtime retires hover state on (washes,
1408
+ * hover Msgs, tooltip intent), matching the AppKit host's
1409
+ * mouseExited. A leave DURING a click-gesture press is the pointer
1410
+ * wandering outside the widget mid-gesture, not abandoning it -
1411
+ * the press's own release, gesture cancel, or the runtime's
1412
+ * outside-release check settles that, so a mid-press leave emits
1413
+ * nothing here. */
1414
+ if (view->gpu_pointer_down) return;
1415
+ const uint32_t modifiers = native_sdk_gpu_modifier_flags(gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(controller)));
1416
+ native_sdk_emit_gpu_surface_input(view, NATIVE_SDK_GTK_GPU_INPUT_POINTER_CANCEL, view->gpu_pointer_x, view->gpu_pointer_y, 0, 0, 0, "", "", modifiers);
1417
+ }
1418
+
1223
1419
  static gboolean native_sdk_gpu_scroll(GtkEventControllerScroll *controller, double dx, double dy, gpointer data) {
1224
1420
  native_sdk_gtk_native_view_t *view = data;
1225
1421
  if (!view) return FALSE;
@@ -1261,17 +1457,61 @@ static gboolean native_sdk_gpu_key_event(native_sdk_gtk_native_view_t *view, gui
1261
1457
  }
1262
1458
 
1263
1459
  static gboolean native_sdk_gpu_key_pressed(GtkEventControllerKey *controller, guint keyval, guint keycode, GdkModifierType state, gpointer data) {
1264
- (void)keycode;
1265
1460
  native_sdk_gtk_native_view_t *view = data;
1266
1461
  if (!view) return FALSE;
1267
1462
  GdkEvent *event = gtk_event_controller_get_current_event(GTK_EVENT_CONTROLLER(controller));
1463
+ const int had_preedit = view->gpu_im_context && native_sdk_gpu_surface_has_preedit(view);
1268
1464
  if (view->gpu_im_context && event && gtk_im_context_filter_keypress(view->gpu_im_context, event)) {
1269
- /* The IM context consumed the key: committed text and preedit updates
1270
- * already flowed through the commit / preedit-changed handlers as
1271
- * text_input / ime_* events. Still surface a key_down with an empty
1272
- * text payload so activation keys (space, enter) and canvas key
1273
- * handlers keep firing; the runtime only inserts text from key_down
1274
- * events that carry text, so nothing is inserted twice. */
1465
+ /* The IM context consumed the key. A key consumed WHILE a composition
1466
+ * is active (before the filter ran, or opened by it) is composition
1467
+ * machinery — candidate navigation, the confirming Enter, a
1468
+ * cancelling Escape — and belongs wholly to the input method:
1469
+ * surface nothing, or an app-level key consumer (a terminal) would
1470
+ * double the confirm into its own key handling. This covers both
1471
+ * synchronous IMs (the commit/cancel handlers ran inside the filter
1472
+ * call, clearing the preedit had_preedit captured) and asynchronous
1473
+ * ones (the preedit is still visible while the key is consumed). */
1474
+ if (had_preedit || native_sdk_gpu_surface_has_preedit(view)) {
1475
+ /* Remember the key — by PHYSICAL KEYCODE — so its release is
1476
+ * swallowed too: a key_up without its key_down would be an
1477
+ * orphan event, and the keyval would rename between press
1478
+ * and release if Shift lifts first. DEDUPLICATED: autorepeat
1479
+ * delivers many suppressed key_downs for one held key but
1480
+ * only one final release, so a repeat must not stack entries
1481
+ * a later ordinary release would then be eaten by. */
1482
+ size_t free_slot = G_N_ELEMENTS(view->gpu_suppressed_keycodes);
1483
+ gboolean recorded = FALSE;
1484
+ for (size_t i = 0; i < G_N_ELEMENTS(view->gpu_suppressed_keycodes); i++) {
1485
+ if (view->gpu_suppressed_keycodes[i] == keycode) {
1486
+ recorded = TRUE;
1487
+ break;
1488
+ }
1489
+ if (view->gpu_suppressed_keycodes[i] == 0 && free_slot == G_N_ELEMENTS(view->gpu_suppressed_keycodes)) {
1490
+ free_slot = i;
1491
+ }
1492
+ }
1493
+ if (!recorded && free_slot < G_N_ELEMENTS(view->gpu_suppressed_keycodes)) {
1494
+ view->gpu_suppressed_keycodes[free_slot] = keycode;
1495
+ }
1496
+ /* A plain cancel armed the runtime's cancel-to-commit grace
1497
+ * expecting THIS key_down as its disarm: suppression owes it
1498
+ * a synthetic duplicate cancel instead (a no-op everywhere
1499
+ * but the grace probe), or the next ordinary text_input
1500
+ * would be mistaken for a converted commit. */
1501
+ if (view->gpu_cancel_disarm_pending) {
1502
+ view->gpu_cancel_disarm_pending = 0;
1503
+ native_sdk_emit_gpu_surface_text_input(view, NATIVE_SDK_GTK_GPU_INPUT_IME_CANCEL_COMPOSITION, "", 0, 0);
1504
+ }
1505
+ return TRUE;
1506
+ }
1507
+ /* Consumed with no composition anywhere in sight (the everyday
1508
+ * GtkIMContextSimple commit of a plain printable): committed text
1509
+ * and preedit updates already flowed through the commit /
1510
+ * preedit-changed handlers as text_input / ime_* events. Still
1511
+ * surface a key_down with an empty text payload so activation keys
1512
+ * (space, enter) and canvas key handlers keep firing; the runtime
1513
+ * only inserts text from key_down events that carry text, so nothing
1514
+ * is inserted twice. */
1275
1515
  (void)native_sdk_gpu_key_event(view, keyval, state, NATIVE_SDK_GTK_GPU_INPUT_KEY_DOWN, 0);
1276
1516
  return TRUE;
1277
1517
  }
@@ -1279,11 +1519,19 @@ static gboolean native_sdk_gpu_key_pressed(GtkEventControllerKey *controller, gu
1279
1519
  }
1280
1520
 
1281
1521
  static void native_sdk_gpu_key_released(GtkEventControllerKey *controller, guint keyval, guint keycode, GdkModifierType state, gpointer data) {
1282
- (void)keycode;
1283
1522
  native_sdk_gtk_native_view_t *view = data;
1284
1523
  if (!view) return;
1285
1524
  GdkEvent *event = gtk_event_controller_get_current_event(GTK_EVENT_CONTROLLER(controller));
1286
1525
  if (view->gpu_im_context && event) (void)gtk_im_context_filter_keypress(view->gpu_im_context, event);
1526
+ /* A composition-consumed key whose key_down was suppressed swallows
1527
+ * its release too (see gpu_suppressed_keycodes; matched by physical
1528
+ * keycode so a modifier lifted mid-hold cannot rename the key). */
1529
+ for (size_t i = 0; i < G_N_ELEMENTS(view->gpu_suppressed_keycodes); i++) {
1530
+ if (view->gpu_suppressed_keycodes[i] == keycode) {
1531
+ view->gpu_suppressed_keycodes[i] = 0;
1532
+ return;
1533
+ }
1534
+ }
1287
1535
  (void)native_sdk_gpu_key_event(view, keyval, state, NATIVE_SDK_GTK_GPU_INPUT_KEY_UP, 0);
1288
1536
  }
1289
1537
 
@@ -1306,6 +1554,11 @@ static void native_sdk_gpu_focus_leave(GtkEventControllerFocus *controller, gpoi
1306
1554
  }
1307
1555
  gtk_im_context_reset(view->gpu_im_context);
1308
1556
  gtk_im_context_focus_out(view->gpu_im_context);
1557
+ /* Pending suppressed releases die with the focus: their key_ups go
1558
+ * to whichever surface focuses next (or nowhere), and a stale entry
1559
+ * here would eat the same key's legitimate release after refocus. */
1560
+ memset(view->gpu_suppressed_keycodes, 0, sizeof(view->gpu_suppressed_keycodes));
1561
+ view->gpu_cancel_disarm_pending = 0;
1309
1562
  }
1310
1563
 
1311
1564
  static void native_sdk_setup_gpu_surface_view(native_sdk_gtk_native_view_t *view) {
@@ -1317,10 +1570,12 @@ static void native_sdk_setup_gpu_surface_view(native_sdk_gtk_native_view_t *view
1317
1570
  gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(click), 0);
1318
1571
  g_signal_connect(click, "pressed", G_CALLBACK(native_sdk_gpu_pointer_pressed), view);
1319
1572
  g_signal_connect(click, "released", G_CALLBACK(native_sdk_gpu_pointer_released), view);
1573
+ g_signal_connect(click, "cancel", G_CALLBACK(native_sdk_gpu_pointer_cancelled), view);
1320
1574
  gtk_widget_add_controller(view->widget, GTK_EVENT_CONTROLLER(click));
1321
1575
 
1322
1576
  GtkEventController *motion = gtk_event_controller_motion_new();
1323
1577
  g_signal_connect(motion, "motion", G_CALLBACK(native_sdk_gpu_pointer_motion), view);
1578
+ g_signal_connect(motion, "leave", G_CALLBACK(native_sdk_gpu_pointer_leave), view);
1324
1579
  gtk_widget_add_controller(view->widget, motion);
1325
1580
 
1326
1581
  GtkEventController *scroll = gtk_event_controller_scroll_new(GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES);
@@ -2047,6 +2302,24 @@ static const char *native_sdk_accel_key_name(const char *key) {
2047
2302
  if (strcmp(key, "arrowright") == 0) return "Right";
2048
2303
  if (strcmp(key, "arrowup") == 0) return "Up";
2049
2304
  if (strcmp(key, "arrowdown") == 0) return "Down";
2305
+ if (strcmp(key, "delete") == 0) return "Delete";
2306
+ if (strcmp(key, "home") == 0) return "Home";
2307
+ if (strcmp(key, "end") == 0) return "End";
2308
+ if (strcmp(key, "pageup") == 0) return "Page_Up";
2309
+ if (strcmp(key, "pagedown") == 0) return "Page_Down";
2310
+ if (strcmp(key, "insert") == 0) return "Insert";
2311
+ if (strcmp(key, "f1") == 0) return "F1";
2312
+ if (strcmp(key, "f2") == 0) return "F2";
2313
+ if (strcmp(key, "f3") == 0) return "F3";
2314
+ if (strcmp(key, "f4") == 0) return "F4";
2315
+ if (strcmp(key, "f5") == 0) return "F5";
2316
+ if (strcmp(key, "f6") == 0) return "F6";
2317
+ if (strcmp(key, "f7") == 0) return "F7";
2318
+ if (strcmp(key, "f8") == 0) return "F8";
2319
+ if (strcmp(key, "f9") == 0) return "F9";
2320
+ if (strcmp(key, "f10") == 0) return "F10";
2321
+ if (strcmp(key, "f11") == 0) return "F11";
2322
+ if (strcmp(key, "f12") == 0) return "F12";
2050
2323
  return key;
2051
2324
  }
2052
2325
 
@@ -2292,6 +2565,30 @@ static const char *native_sdk_shortcut_key_for_keyval(guint keyval, char *buffer
2292
2565
  case GDK_KEY_Right: return "arrowright";
2293
2566
  case GDK_KEY_Up: return "arrowup";
2294
2567
  case GDK_KEY_Down: return "arrowdown";
2568
+ case GDK_KEY_Delete:
2569
+ case GDK_KEY_KP_Delete: return "delete";
2570
+ case GDK_KEY_Home:
2571
+ case GDK_KEY_KP_Home: return "home";
2572
+ case GDK_KEY_End:
2573
+ case GDK_KEY_KP_End: return "end";
2574
+ case GDK_KEY_Page_Up:
2575
+ case GDK_KEY_KP_Page_Up: return "pageup";
2576
+ case GDK_KEY_Page_Down:
2577
+ case GDK_KEY_KP_Page_Down: return "pagedown";
2578
+ case GDK_KEY_Insert:
2579
+ case GDK_KEY_KP_Insert: return "insert";
2580
+ case GDK_KEY_F1: return "f1";
2581
+ case GDK_KEY_F2: return "f2";
2582
+ case GDK_KEY_F3: return "f3";
2583
+ case GDK_KEY_F4: return "f4";
2584
+ case GDK_KEY_F5: return "f5";
2585
+ case GDK_KEY_F6: return "f6";
2586
+ case GDK_KEY_F7: return "f7";
2587
+ case GDK_KEY_F8: return "f8";
2588
+ case GDK_KEY_F9: return "f9";
2589
+ case GDK_KEY_F10: return "f10";
2590
+ case GDK_KEY_F11: return "f11";
2591
+ case GDK_KEY_F12: return "f12";
2295
2592
  default: return "";
2296
2593
  }
2297
2594
  }
@@ -2562,6 +2859,46 @@ static void native_sdk_register_zero_scheme(native_sdk_gtk_host_t *host, WebKitW
2562
2859
  host->scheme_registered = 1;
2563
2860
  }
2564
2861
 
2862
+ /* WebKit owns its pointer stream, so a press never reaches the runtime's
2863
+ * gpu_surface input seam. Observe without claiming the gesture and report
2864
+ * which WebView the press will focus; the runtime can then blur its canvas
2865
+ * sibling before WebKit receives the same event. */
2866
+ static void native_sdk_webview_pointer_pressed(GtkGestureClick *gesture, int n_press, double x, double y, gpointer data) {
2867
+ (void)n_press;
2868
+ (void)x;
2869
+ (void)y;
2870
+ native_sdk_gtk_window_t *win = data;
2871
+ if (!win || !win->host) return;
2872
+ GtkWidget *widget = gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(gesture));
2873
+ const char *label = NULL;
2874
+ if (win->web_view && widget == GTK_WIDGET(win->web_view)) {
2875
+ label = "main";
2876
+ } else {
2877
+ for (int i = 0; i < win->webview_count; i++) {
2878
+ if (win->webviews[i].web_view && widget == GTK_WIDGET(win->webviews[i].web_view)) {
2879
+ label = win->webviews[i].label;
2880
+ break;
2881
+ }
2882
+ }
2883
+ }
2884
+ if (!label) return;
2885
+ native_sdk_emit(win->host, (native_sdk_gtk_event_t){
2886
+ .kind = NATIVE_SDK_GTK_EVENT_VIEW_FOCUSED,
2887
+ .window_id = win->id,
2888
+ .view_label = label,
2889
+ .view_label_len = strlen(label),
2890
+ });
2891
+ }
2892
+
2893
+ static void native_sdk_watch_webview_pointer_focus(native_sdk_gtk_window_t *win, WebKitWebView *web_view) {
2894
+ if (!win || !web_view) return;
2895
+ GtkGesture *click = gtk_gesture_click_new();
2896
+ gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(click), 0);
2897
+ gtk_event_controller_set_propagation_phase(GTK_EVENT_CONTROLLER(click), GTK_PHASE_CAPTURE);
2898
+ g_signal_connect(click, "pressed", G_CALLBACK(native_sdk_webview_pointer_pressed), win);
2899
+ gtk_widget_add_controller(GTK_WIDGET(web_view), GTK_EVENT_CONTROLLER(click));
2900
+ }
2901
+
2565
2902
  /* Create-on-first-use for a window's main WebView (the AppKit host's
2566
2903
  * ensureMainWebViewForWindowId:). Pure peek reads (event emission,
2567
2904
  * bridge completion echoes, reorder passes, focus) keep checking
@@ -2584,6 +2921,7 @@ static WebKitWebView *native_sdk_ensure_main_webview(native_sdk_gtk_window_t *wi
2584
2921
  native_sdk_register_zero_scheme(win->host, wv);
2585
2922
  native_sdk_setup_bridge(win);
2586
2923
  g_signal_connect(wv, "decide-policy", G_CALLBACK(on_decide_policy), win);
2924
+ native_sdk_watch_webview_pointer_focus(win, wv);
2587
2925
 
2588
2926
  /* The overlay's main-child slot, where the eager create used to put
2589
2927
  * it: the overlay allocates its main child the full stack area on the
@@ -2791,10 +3129,28 @@ void native_sdk_gtk_destroy(native_sdk_gtk_host_t *host) {
2791
3129
  * anything else goes away; everything runs on this thread. */
2792
3130
  native_sdk_audio_release(host, 1);
2793
3131
  if (host->frame_timer) g_source_remove(host->frame_timer);
3132
+ /* A stop turn still queued at teardown (the loop quit before the
3133
+ * idle ran) must not fire into freed memory. */
3134
+ if (host->stop_idle_source) {
3135
+ g_source_remove(host->stop_idle_source);
3136
+ host->stop_idle_source = 0;
3137
+ }
2794
3138
  for (int i = 0; i < NATIVE_SDK_MAX_TIMERS; i++) {
2795
3139
  if (host->timers[i].in_use && host->timers[i].source) g_source_remove(host->timers[i].source);
2796
3140
  host->timers[i].in_use = 0;
2797
3141
  }
3142
+ /* Before the windows go: an open context menu's popover is owned by
3143
+ * a widget inside one of them, and the state's weak pointers must
3144
+ * be unhooked while the objects are still alive. Then drain the
3145
+ * pending-teardown list: a superseded menu's deferred teardown idle
3146
+ * still captures its state (and this host) raw, so each pending
3147
+ * source is removed and its state freed inline — the weak pointers
3148
+ * only cover the WIDGETS dying first; the host dying is covered
3149
+ * here. Cancelling swallows the superseded request's owed dismissal
3150
+ * event, which is correct: the runtime that would gate on that
3151
+ * token is being torn down with the host. */
3152
+ if (host->context_menu) native_sdk_context_menu_free(host->context_menu);
3153
+ while (host->context_menu_teardowns) native_sdk_context_menu_free(host->context_menu_teardowns);
2798
3154
  for (int i = 0; i < host->window_count; i++) {
2799
3155
  native_sdk_clear_window(&host->windows[i]);
2800
3156
  }
@@ -2819,7 +3175,15 @@ void native_sdk_gtk_run(native_sdk_gtk_host_t *host, native_sdk_gtk_event_callba
2819
3175
  g_application_run(G_APPLICATION(host->app), 0, NULL);
2820
3176
  }
2821
3177
 
2822
- void native_sdk_gtk_stop(native_sdk_gtk_host_t *host) {
3178
+ /* Runs on the GLib main loop: the quit verb's deferred stop turn. The
3179
+ * same emitShutdown-then-quit the last window's close-request runs,
3180
+ * just one loop turn later (see native_sdk_gtk_stop). */
3181
+ static gboolean native_sdk_stop_idle(gpointer data) {
3182
+ native_sdk_gtk_host_t *host = data;
3183
+ /* This turn is running: clear the tracked id FIRST, so the source
3184
+ * cannot be double-removed (destroy) or wrongly treated as still
3185
+ * pending (a re-quit during the shutdown dispatch below). */
3186
+ host->stop_idle_source = 0;
2823
3187
  if (!host->did_shutdown) {
2824
3188
  host->did_shutdown = 1;
2825
3189
  native_sdk_emit(host, (native_sdk_gtk_event_t){ .kind = NATIVE_SDK_GTK_EVENT_SHUTDOWN });
@@ -2829,6 +3193,33 @@ void native_sdk_gtk_stop(native_sdk_gtk_host_t *host) {
2829
3193
  host->frame_timer = 0;
2830
3194
  }
2831
3195
  g_application_quit(G_APPLICATION(host->app));
3196
+ return G_SOURCE_REMOVE;
3197
+ }
3198
+
3199
+ void native_sdk_gtk_stop(native_sdk_gtk_host_t *host) {
3200
+ /* Queued, never synchronous: this stop is the quit verb's landing
3201
+ * point, and the verb arrives MID DISPATCH (a menu command's
3202
+ * update returned it), so a synchronous SHUTDOWN emit would nest
3203
+ * the shutdown dispatch inside the requesting command's — the
3204
+ * session recorder commits nested events innermost-first and seals
3205
+ * the journal on shutdown, so the nested seal drops the command
3206
+ * record and replay diverges. The idle hop (the same seam the wake
3207
+ * and frame-request paths ride) emits the identical
3208
+ * emitShutdown + quit on the next loop turn, after the requesting
3209
+ * dispatch has committed; g_application_run keeps pumping until
3210
+ * the queued g_application_quit lands, so a quit that is the last
3211
+ * thing an app ever does still exits promptly.
3212
+ *
3213
+ * Exactly-once hygiene: once the shutdown has emitted there is
3214
+ * nothing left to stop, and while a stop turn is already queued a
3215
+ * second request coalesces into it — a second bare g_idle_add
3216
+ * (the shutdown handler's error path can request one) would leave
3217
+ * an untracked source holding this host after the loop quits and
3218
+ * the host is freed. The pending source id is tracked on the host
3219
+ * (stop_idle_source, cleared by the idle itself) and removed at
3220
+ * native_sdk_gtk_destroy. */
3221
+ if (host->did_shutdown || host->stop_idle_source) return;
3222
+ host->stop_idle_source = g_idle_add(native_sdk_stop_idle, host);
2832
3223
  }
2833
3224
 
2834
3225
  /* Runs on the GLib main loop: emit the wake event there, so the runtime
@@ -3153,6 +3544,12 @@ void native_sdk_gtk_set_menus(native_sdk_gtk_host_t *host, const char *const *me
3153
3544
  free(key);
3154
3545
  continue;
3155
3546
  }
3547
+ /* Canonicalize to lowercase, the shortcut-storage rule: key
3548
+ * names validate case-insensitively, and the accelerator
3549
+ * translation below matches the canonical spellings. */
3550
+ for (char *p = key; *p; p++) {
3551
+ if (*p >= 'A' && *p <= 'Z') *p = (char)(*p - 'A' + 'a');
3552
+ }
3156
3553
 
3157
3554
  native_sdk_gtk_menu_action_t *menu_action = &host->menu_actions[host->menu_action_count];
3158
3555
  memset(menu_action, 0, sizeof(*menu_action));
@@ -3433,6 +3830,7 @@ int native_sdk_gtk_create_view(native_sdk_gtk_host_t *host, uint64_t window_id,
3433
3830
  native_sdk_gtk_window_t *win = native_sdk_find_window(host, window_id);
3434
3831
  if (!win || !win->stack_root || label_len == 0 || !native_sdk_valid_native_view_frame(x, y, width, height)) return 0;
3435
3832
  if (!native_sdk_is_supported_native_view_kind(kind)) return 0;
3833
+ if (!native_sdk_valid_native_view_strings(label_len, parent_len, role_len, accessibility_label_len, text_len, command_len)) return 0;
3436
3834
  if (win->native_view_count >= NATIVE_SDK_MAX_NATIVE_VIEWS) return 0;
3437
3835
 
3438
3836
  char *label_copy = native_sdk_strndup(label, label_len);
@@ -3540,6 +3938,224 @@ int native_sdk_gtk_create_view(native_sdk_gtk_host_t *host, uint64_t window_id,
3540
3938
  return 1;
3541
3939
  }
3542
3940
 
3941
+ /* --------------------------------------------------------------------
3942
+ * Native context menus (GtkPopoverMenu).
3943
+ *
3944
+ * GTK-only — deliberately OUTSIDE the WebKit fences, so the menu path
3945
+ * compiles identically in web and native-only (stub) builds.
3946
+ */
3947
+
3948
+ /* Emit the one CONTEXT_MENU_ACTION event this request may produce (the
3949
+ * macOS host's payload contract: widget_id echoes the token,
3950
+ * menu_item_id 0 means dismissed). */
3951
+ static void native_sdk_context_menu_emit(native_sdk_gtk_context_menu_t *menu, uint32_t item_id) {
3952
+ if (!menu || menu->emitted) return;
3953
+ menu->emitted = 1;
3954
+ native_sdk_emit(menu->host, (native_sdk_gtk_event_t){
3955
+ .kind = NATIVE_SDK_GTK_EVENT_CONTEXT_MENU_ACTION,
3956
+ .window_id = menu->window_id,
3957
+ .view_label = menu->view_label ? menu->view_label : "",
3958
+ .view_label_len = menu->view_label ? strlen(menu->view_label) : 0,
3959
+ .widget_id = menu->token,
3960
+ .menu_item_id = item_id,
3961
+ });
3962
+ }
3963
+
3964
+ /* Drop `menu` from the host's pending-teardown list if it is there.
3965
+ * Called only from native_sdk_context_menu_free, so a state is unlinked
3966
+ * on every exit path — the teardown idle running normally (it frees
3967
+ * as its last act), a supersession freeing a dead-popover state, or
3968
+ * destroy draining the list. */
3969
+ static void native_sdk_context_menu_unlink_teardown(native_sdk_gtk_context_menu_t *menu) {
3970
+ if (!menu->host) return;
3971
+ native_sdk_gtk_context_menu_t **cursor = &menu->host->context_menu_teardowns;
3972
+ while (*cursor) {
3973
+ if (*cursor == menu) {
3974
+ *cursor = menu->next_teardown;
3975
+ break;
3976
+ }
3977
+ cursor = &(*cursor)->next_teardown;
3978
+ }
3979
+ menu->next_teardown = NULL;
3980
+ }
3981
+
3982
+ /* Balanced teardown of one presented menu (NO event emission): remove
3983
+ * the per-invocation action group from the presenting widget, drop the
3984
+ * weak pointers, unparent the popover (the parent holds the only ref,
3985
+ * so unparent finalizes it), and free the state. This may run from a
3986
+ * STALE teardown idle after a re-click already installed a successor
3987
+ * menu, so it must only ever touch its own per-invocation state: the
3988
+ * group removal names this request's unique namespace, never the
3989
+ * successor's. (Only GTK needs this guard: popovers are asynchronous.
3990
+ * The Windows host's TrackPopupMenu blocks the message-loop thread and
3991
+ * emits inline from a moved-out request, and the macOS host's
3992
+ * presentation block captures its menu, target, and token as locals
3993
+ * while popUpMenuPositioningItem blocks the main queue — neither can
3994
+ * interleave a successor with a pending teardown.) */
3995
+ static void native_sdk_context_menu_free(native_sdk_gtk_context_menu_t *menu) {
3996
+ if (!menu) return;
3997
+ if (menu->teardown_idle) {
3998
+ g_source_remove(menu->teardown_idle);
3999
+ menu->teardown_idle = 0;
4000
+ }
4001
+ native_sdk_context_menu_unlink_teardown(menu);
4002
+ if (menu->parent) {
4003
+ gtk_widget_insert_action_group(menu->parent, menu->group_name, NULL);
4004
+ g_object_remove_weak_pointer(G_OBJECT(menu->parent), (gpointer *)&menu->parent);
4005
+ }
4006
+ if (menu->popover) {
4007
+ g_object_remove_weak_pointer(G_OBJECT(menu->popover), (gpointer *)&menu->popover);
4008
+ gtk_widget_unparent(menu->popover);
4009
+ }
4010
+ if (menu->host && menu->host->context_menu == menu) menu->host->context_menu = NULL;
4011
+ free(menu->view_label);
4012
+ free(menu);
4013
+ }
4014
+
4015
+ /* Runs one main-loop turn after the popover closed: a selection's
4016
+ * action activation (delivered around the close) has already landed by
4017
+ * now, so an un-emitted request is honestly a dismissal — the same
4018
+ * one-turn-later emission discipline the macOS host applies after its
4019
+ * tracking loop ends. */
4020
+ static gboolean native_sdk_context_menu_teardown_idle(gpointer data) {
4021
+ native_sdk_gtk_context_menu_t *menu = data;
4022
+ menu->teardown_idle = 0;
4023
+ native_sdk_context_menu_emit(menu, 0);
4024
+ native_sdk_context_menu_free(menu);
4025
+ return G_SOURCE_REMOVE;
4026
+ }
4027
+
4028
+ static void native_sdk_context_menu_closed(GtkPopover *popover, gpointer data) {
4029
+ (void)popover;
4030
+ native_sdk_gtk_context_menu_t *menu = data;
4031
+ if (!menu->teardown_idle) {
4032
+ /* The idle captures `menu` (and through it the host) raw, so
4033
+ * its receipt goes on the host's pending-teardown list the
4034
+ * moment it exists — `host->context_menu` alone cannot reach a
4035
+ * superseded state, and an idle outliving the host would fire
4036
+ * into freed memory. */
4037
+ menu->teardown_idle = g_idle_add(native_sdk_context_menu_teardown_idle, menu);
4038
+ menu->next_teardown = menu->host->context_menu_teardowns;
4039
+ menu->host->context_menu_teardowns = menu;
4040
+ }
4041
+ }
4042
+
4043
+ static void native_sdk_context_menu_item_activate(GSimpleAction *action, GVariant *parameter, gpointer data) {
4044
+ (void)parameter;
4045
+ native_sdk_gtk_context_menu_t *menu = data;
4046
+ uint32_t item_id = (uint32_t)GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(action), "native-sdk-item-id"));
4047
+ native_sdk_context_menu_emit(menu, item_id);
4048
+ }
4049
+
4050
+ 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) {
4051
+ if (!host || !items || count == 0) return 0;
4052
+ native_sdk_gtk_window_t *win = native_sdk_find_window(host, window_id);
4053
+ if (!win) return 0;
4054
+ /* Present on the view that took the click when the label resolves —
4055
+ * gtk_popover_set_pointing_to takes the PARENT widget's logical
4056
+ * coordinates, which is exactly the space the gpu-surface input
4057
+ * path reported the click's x/y in (widget-local logical doubles,
4058
+ * no scale conversion on either leg). Fall back to the window's
4059
+ * view host, mirroring the macOS contentView fallback. */
4060
+ GtkWidget *parent = win->stack_root;
4061
+ char *label_copy = label_len > 0 ? native_sdk_strndup(label, label_len) : NULL;
4062
+ native_sdk_gtk_native_view_t *view = native_sdk_find_native_view(win, label_copy);
4063
+ if (view && view->widget) parent = view->widget;
4064
+ if (!parent) {
4065
+ free(label_copy);
4066
+ return 0;
4067
+ }
4068
+
4069
+ /* A menu already up (re-click while open): dismiss it — its closed
4070
+ * handler schedules its own teardown and dismissal event. That
4071
+ * teardown runs a loop turn from now, AFTER this successor is
4072
+ * installed, and still emits the superseded request's one dismissal
4073
+ * (with the old request's token — the runtime's token gate resolves
4074
+ * or swallows it against ITS pending request, never the
4075
+ * successor's), while the per-invocation group namespace keeps its
4076
+ * cleanup off the successor's action group. A state whose popover
4077
+ * already died with its window (weak pointer nulled, "closed" never
4078
+ * delivered) has no handler coming: free it here. */
4079
+ if (host->context_menu) {
4080
+ if (host->context_menu->popover) {
4081
+ gtk_popover_popdown(GTK_POPOVER(host->context_menu->popover));
4082
+ } else {
4083
+ native_sdk_context_menu_free(host->context_menu);
4084
+ }
4085
+ }
4086
+
4087
+ native_sdk_gtk_context_menu_t *menu_state = calloc(1, sizeof(*menu_state));
4088
+ if (!menu_state) {
4089
+ free(label_copy);
4090
+ return 0;
4091
+ }
4092
+ menu_state->host = host;
4093
+ menu_state->window_id = window_id;
4094
+ menu_state->view_label = label_copy;
4095
+ menu_state->token = token;
4096
+ snprintf(menu_state->group_name, sizeof(menu_state->group_name), "native-sdk-context-%llu", (unsigned long long)++host->context_menu_serial);
4097
+ menu_state->parent = parent;
4098
+ g_object_add_weak_pointer(G_OBJECT(parent), (gpointer *)&menu_state->parent);
4099
+
4100
+ /* The declared items become a sectioned GMenu — separators split
4101
+ * sections, the GMenu convention — wired to a fresh action group
4102
+ * inserted on the presenting widget under this request's unique
4103
+ * namespace. Every builder ref is dropped as soon as the next owner
4104
+ * holds its own (the set_menus discipline). */
4105
+ GSimpleActionGroup *group = g_simple_action_group_new();
4106
+ GMenu *menu = g_menu_new();
4107
+ GMenu *section = g_menu_new();
4108
+ for (size_t i = 0; i < count; i++) {
4109
+ if (items[i].separator) {
4110
+ if (g_menu_model_get_n_items(G_MENU_MODEL(section)) > 0) {
4111
+ g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
4112
+ }
4113
+ g_object_unref(section);
4114
+ section = g_menu_new();
4115
+ continue;
4116
+ }
4117
+ char action_name[32];
4118
+ snprintf(action_name, sizeof(action_name), "item-%u", items[i].item_id);
4119
+ GSimpleAction *action = g_simple_action_new(action_name, NULL);
4120
+ g_simple_action_set_enabled(action, items[i].enabled != 0);
4121
+ g_object_set_data(G_OBJECT(action), "native-sdk-item-id", GUINT_TO_POINTER(items[i].item_id));
4122
+ g_signal_connect(action, "activate", G_CALLBACK(native_sdk_context_menu_item_activate), menu_state);
4123
+ g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(action));
4124
+ g_object_unref(action); /* the group holds its own ref */
4125
+ char *item_label = native_sdk_strndup(items[i].label ? items[i].label : "", items[i].label_len);
4126
+ char detailed[80];
4127
+ snprintf(detailed, sizeof(detailed), "%s.item-%u", menu_state->group_name, items[i].item_id);
4128
+ GMenuItem *gitem = g_menu_item_new(item_label ? item_label : "", detailed);
4129
+ g_menu_append_item(section, gitem);
4130
+ g_object_unref(gitem); /* the section holds its own ref */
4131
+ free(item_label);
4132
+ }
4133
+ if (g_menu_model_get_n_items(G_MENU_MODEL(section)) > 0) {
4134
+ g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
4135
+ }
4136
+ g_object_unref(section);
4137
+
4138
+ gtk_widget_insert_action_group(parent, menu_state->group_name, G_ACTION_GROUP(group));
4139
+ g_object_unref(group); /* the widget holds its own ref */
4140
+
4141
+ GtkWidget *popover = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu));
4142
+ g_object_unref(menu); /* the popover holds its own model ref */
4143
+ gtk_widget_set_parent(popover, parent); /* sinks the floating ref; the parent owns it */
4144
+ gtk_popover_set_has_arrow(GTK_POPOVER(popover), FALSE);
4145
+ GdkRectangle pointing_to = { (int)x, (int)y, 1, 1 };
4146
+ gtk_popover_set_pointing_to(GTK_POPOVER(popover), &pointing_to);
4147
+ menu_state->popover = popover;
4148
+ g_object_add_weak_pointer(G_OBJECT(popover), (gpointer *)&menu_state->popover);
4149
+ g_signal_connect(popover, "closed", G_CALLBACK(native_sdk_context_menu_closed), menu_state);
4150
+ host->context_menu = menu_state;
4151
+ /* Popovers are asynchronous (no nested tracking loop): popup returns
4152
+ * immediately and the selection arrives through the action group,
4153
+ * so presenting synchronously from the requesting dispatch is safe —
4154
+ * the macOS host defers a turn only because NSMenu's popUp blocks. */
4155
+ gtk_popover_popup(GTK_POPOVER(popover));
4156
+ return 1;
4157
+ }
4158
+
3543
4159
  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) {
3544
4160
  native_sdk_gtk_window_t *win = native_sdk_find_window(host, window_id);
3545
4161
  char *label_copy = label_len > 0 ? native_sdk_strndup(label, label_len) : NULL;
@@ -3620,6 +4236,12 @@ int native_sdk_gtk_present_gpu_surface_pixels(native_sdk_gtk_host_t *host, uint6
3620
4236
  }
3621
4237
 
3622
4238
  int native_sdk_gtk_update_view(native_sdk_gtk_host_t *host, uint64_t window_id, const char *label, size_t label_len, int has_frame, double x, double y, double width, double height, int has_layer, int layer, int has_visible, int visible, int has_enabled, int enabled, int has_role, const char *role, size_t role_len, int has_accessibility_label, const char *accessibility_label, size_t accessibility_label_len, int has_text, const char *text, size_t text_len, int has_command, const char *command, size_t command_len) {
4239
+ if (!native_sdk_valid_native_view_strings(label_len,
4240
+ 0,
4241
+ has_role ? role_len : 0,
4242
+ has_accessibility_label ? accessibility_label_len : 0,
4243
+ has_text ? text_len : 0,
4244
+ has_command ? command_len : 0)) return 0;
3623
4245
  native_sdk_gtk_window_t *win = native_sdk_find_window(host, window_id);
3624
4246
  char *label_copy = label_len > 0 ? native_sdk_strndup(label, label_len) : NULL;
3625
4247
  native_sdk_gtk_native_view_t *view = native_sdk_find_native_view(win, label_copy);
@@ -3779,6 +4401,7 @@ int native_sdk_gtk_create_webview(native_sdk_gtk_host_t *host, uint64_t window_i
3779
4401
  webview->transparent = transparent != 0;
3780
4402
  webview->bridge_enabled = bridge_enabled != 0;
3781
4403
  webview->content_manager = manager;
4404
+ native_sdk_watch_webview_pointer_focus(win, web_view);
3782
4405
  native_sdk_apply_webview_frame(webview);
3783
4406
  gtk_overlay_add_overlay(GTK_OVERLAY(win->stack_root), GTK_WIDGET(web_view));
3784
4407
  if (transparent) {