@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
@@ -120,6 +120,40 @@ typedef struct native_sdk_gtk_menu_action {
120
120
  struct native_sdk_gtk_host *host;
121
121
  } native_sdk_gtk_menu_action_t;
122
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
+
123
157
  /* One rectangle of the runtime-pushed window-drag mirror (markup
124
158
  * `window-drag="true"`), in the owning gpu_surface view's logical
125
159
  * coordinates. Exclusions are the press-claiming widgets INSIDE a drag
@@ -209,6 +243,26 @@ typedef struct native_sdk_gtk_native_view {
209
243
  * matching release is swallowed too (no orphaned pointer_up). */
210
244
  native_sdk_gtk_drag_region_t *drag_regions;
211
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;
212
266
  int gpu_drag_claimed_press;
213
267
  } native_sdk_gtk_native_view_t;
214
268
 
@@ -366,10 +420,25 @@ struct native_sdk_gtk_host {
366
420
  GMenuModel *menu_model;
367
421
  native_sdk_gtk_menu_action_t menu_actions[NATIVE_SDK_MAX_MENU_ITEMS];
368
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;
369
437
  native_sdk_gtk_audio_t audio;
370
438
  };
371
439
 
372
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);
373
442
  static gboolean native_sdk_nudge_chrome_requery(gpointer data);
374
443
  static gboolean native_sdk_on_file_drop(GtkDropTarget *target, const GValue *value, double x, double y, gpointer data);
375
444
  static GtkWindow *native_sdk_parent_window(native_sdk_gtk_host_t *host);
@@ -789,6 +858,7 @@ static GtkWidget *native_sdk_make_native_widget(int kind, const char *label, con
789
858
  #define NATIVE_SDK_GTK_GPU_INPUT_IME_SET_COMPOSITION 8
790
859
  #define NATIVE_SDK_GTK_GPU_INPUT_IME_COMMIT_COMPOSITION 9
791
860
  #define NATIVE_SDK_GTK_GPU_INPUT_IME_CANCEL_COMPOSITION 10
861
+ #define NATIVE_SDK_GTK_GPU_INPUT_POINTER_CANCEL 11
792
862
 
793
863
  static uint64_t native_sdk_gpu_timestamp_ns(void) {
794
864
  return (uint64_t)g_get_monotonic_time() * 1000ull;
@@ -871,6 +941,9 @@ static void native_sdk_gpu_im_commit(GtkIMContext *context, const char *text, gp
871
941
  native_sdk_emit_gpu_surface_text_input(view, NATIVE_SDK_GTK_GPU_INPUT_IME_CANCEL_COMPOSITION, "", 0, 0);
872
942
  }
873
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;
874
947
  }
875
948
 
876
949
  /* IM context preedit (composition) changed. Mirrors AppKit's setMarkedText:
@@ -889,6 +962,11 @@ static void native_sdk_gpu_im_preedit_changed(GtkIMContext *context, gpointer da
889
962
  if (native_sdk_gpu_surface_has_preedit(view)) {
890
963
  native_sdk_gpu_surface_clear_preedit(view);
891
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;
892
970
  }
893
971
  return;
894
972
  }
@@ -1207,13 +1285,30 @@ static int native_sdk_gpu_point_in_drag_region(const native_sdk_gtk_native_view_
1207
1285
  return 0;
1208
1286
  }
1209
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
+
1210
1304
  static void native_sdk_gpu_pointer_pressed(GtkGestureClick *gesture, int n_press, double x, double y, gpointer data) {
1211
1305
  native_sdk_gtk_native_view_t *view = data;
1212
1306
  if (!view || !view->widget) return;
1213
1307
  gtk_widget_grab_focus(view->widget);
1214
1308
  view->gpu_pointer_x = x;
1215
1309
  view->gpu_pointer_y = y;
1216
- 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);
1217
1312
  const uint32_t modifiers = native_sdk_gpu_modifier_flags(gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(gesture)));
1218
1313
  /* Stash the press for the widget `window_drag` channel: an
1219
1314
  * interactive window move must begin from the originating device,
@@ -1228,7 +1323,9 @@ static void native_sdk_gpu_pointer_pressed(GtkGestureClick *gesture, int n_press
1228
1323
  }
1229
1324
  view->window->last_press_device = event ? gdk_event_get_device(event) : NULL;
1230
1325
  view->window->last_press_time = event ? gdk_event_get_time(event) : GDK_CURRENT_TIME;
1231
- 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;
1232
1329
  view->window->last_press_x = window_point.x;
1233
1330
  view->window->last_press_y = window_point.y;
1234
1331
  }
@@ -1250,7 +1347,7 @@ static void native_sdk_gpu_pointer_pressed(GtkGestureClick *gesture, int n_press
1250
1347
  return;
1251
1348
  }
1252
1349
  view->gpu_pointer_down = 1;
1253
- 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);
1254
1351
  }
1255
1352
 
1256
1353
  static void native_sdk_gpu_pointer_released(GtkGestureClick *gesture, int n_press, double x, double y, gpointer data) {
@@ -1266,9 +1363,31 @@ static void native_sdk_gpu_pointer_released(GtkGestureClick *gesture, int n_pres
1266
1363
  view->gpu_drag_claimed_press = 0;
1267
1364
  return;
1268
1365
  }
1269
- 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)));
1270
1367
  const uint32_t modifiers = native_sdk_gpu_modifier_flags(gtk_event_controller_get_current_event_state(GTK_EVENT_CONTROLLER(gesture)));
1271
- 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);
1272
1391
  }
1273
1392
 
1274
1393
  static void native_sdk_gpu_pointer_motion(GtkEventControllerMotion *controller, double x, double y, gpointer data) {
@@ -1281,6 +1400,22 @@ static void native_sdk_gpu_pointer_motion(GtkEventControllerMotion *controller,
1281
1400
  native_sdk_emit_gpu_surface_input(view, kind, x, y, 0, 0, 0, "", "", modifiers);
1282
1401
  }
1283
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
+
1284
1419
  static gboolean native_sdk_gpu_scroll(GtkEventControllerScroll *controller, double dx, double dy, gpointer data) {
1285
1420
  native_sdk_gtk_native_view_t *view = data;
1286
1421
  if (!view) return FALSE;
@@ -1322,17 +1457,61 @@ static gboolean native_sdk_gpu_key_event(native_sdk_gtk_native_view_t *view, gui
1322
1457
  }
1323
1458
 
1324
1459
  static gboolean native_sdk_gpu_key_pressed(GtkEventControllerKey *controller, guint keyval, guint keycode, GdkModifierType state, gpointer data) {
1325
- (void)keycode;
1326
1460
  native_sdk_gtk_native_view_t *view = data;
1327
1461
  if (!view) return FALSE;
1328
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);
1329
1464
  if (view->gpu_im_context && event && gtk_im_context_filter_keypress(view->gpu_im_context, event)) {
1330
- /* The IM context consumed the key: committed text and preedit updates
1331
- * already flowed through the commit / preedit-changed handlers as
1332
- * text_input / ime_* events. Still surface a key_down with an empty
1333
- * text payload so activation keys (space, enter) and canvas key
1334
- * handlers keep firing; the runtime only inserts text from key_down
1335
- * 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. */
1336
1515
  (void)native_sdk_gpu_key_event(view, keyval, state, NATIVE_SDK_GTK_GPU_INPUT_KEY_DOWN, 0);
1337
1516
  return TRUE;
1338
1517
  }
@@ -1340,11 +1519,19 @@ static gboolean native_sdk_gpu_key_pressed(GtkEventControllerKey *controller, gu
1340
1519
  }
1341
1520
 
1342
1521
  static void native_sdk_gpu_key_released(GtkEventControllerKey *controller, guint keyval, guint keycode, GdkModifierType state, gpointer data) {
1343
- (void)keycode;
1344
1522
  native_sdk_gtk_native_view_t *view = data;
1345
1523
  if (!view) return;
1346
1524
  GdkEvent *event = gtk_event_controller_get_current_event(GTK_EVENT_CONTROLLER(controller));
1347
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
+ }
1348
1535
  (void)native_sdk_gpu_key_event(view, keyval, state, NATIVE_SDK_GTK_GPU_INPUT_KEY_UP, 0);
1349
1536
  }
1350
1537
 
@@ -1367,6 +1554,11 @@ static void native_sdk_gpu_focus_leave(GtkEventControllerFocus *controller, gpoi
1367
1554
  }
1368
1555
  gtk_im_context_reset(view->gpu_im_context);
1369
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;
1370
1562
  }
1371
1563
 
1372
1564
  static void native_sdk_setup_gpu_surface_view(native_sdk_gtk_native_view_t *view) {
@@ -1378,10 +1570,12 @@ static void native_sdk_setup_gpu_surface_view(native_sdk_gtk_native_view_t *view
1378
1570
  gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(click), 0);
1379
1571
  g_signal_connect(click, "pressed", G_CALLBACK(native_sdk_gpu_pointer_pressed), view);
1380
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);
1381
1574
  gtk_widget_add_controller(view->widget, GTK_EVENT_CONTROLLER(click));
1382
1575
 
1383
1576
  GtkEventController *motion = gtk_event_controller_motion_new();
1384
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);
1385
1579
  gtk_widget_add_controller(view->widget, motion);
1386
1580
 
1387
1581
  GtkEventController *scroll = gtk_event_controller_scroll_new(GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES);
@@ -2108,6 +2302,24 @@ static const char *native_sdk_accel_key_name(const char *key) {
2108
2302
  if (strcmp(key, "arrowright") == 0) return "Right";
2109
2303
  if (strcmp(key, "arrowup") == 0) return "Up";
2110
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";
2111
2323
  return key;
2112
2324
  }
2113
2325
 
@@ -2353,6 +2565,30 @@ static const char *native_sdk_shortcut_key_for_keyval(guint keyval, char *buffer
2353
2565
  case GDK_KEY_Right: return "arrowright";
2354
2566
  case GDK_KEY_Up: return "arrowup";
2355
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";
2356
2592
  default: return "";
2357
2593
  }
2358
2594
  }
@@ -2623,6 +2859,46 @@ static void native_sdk_register_zero_scheme(native_sdk_gtk_host_t *host, WebKitW
2623
2859
  host->scheme_registered = 1;
2624
2860
  }
2625
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
+
2626
2902
  /* Create-on-first-use for a window's main WebView (the AppKit host's
2627
2903
  * ensureMainWebViewForWindowId:). Pure peek reads (event emission,
2628
2904
  * bridge completion echoes, reorder passes, focus) keep checking
@@ -2645,6 +2921,7 @@ static WebKitWebView *native_sdk_ensure_main_webview(native_sdk_gtk_window_t *wi
2645
2921
  native_sdk_register_zero_scheme(win->host, wv);
2646
2922
  native_sdk_setup_bridge(win);
2647
2923
  g_signal_connect(wv, "decide-policy", G_CALLBACK(on_decide_policy), win);
2924
+ native_sdk_watch_webview_pointer_focus(win, wv);
2648
2925
 
2649
2926
  /* The overlay's main-child slot, where the eager create used to put
2650
2927
  * it: the overlay allocates its main child the full stack area on the
@@ -2862,6 +3139,18 @@ void native_sdk_gtk_destroy(native_sdk_gtk_host_t *host) {
2862
3139
  if (host->timers[i].in_use && host->timers[i].source) g_source_remove(host->timers[i].source);
2863
3140
  host->timers[i].in_use = 0;
2864
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);
2865
3154
  for (int i = 0; i < host->window_count; i++) {
2866
3155
  native_sdk_clear_window(&host->windows[i]);
2867
3156
  }
@@ -3255,6 +3544,12 @@ void native_sdk_gtk_set_menus(native_sdk_gtk_host_t *host, const char *const *me
3255
3544
  free(key);
3256
3545
  continue;
3257
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
+ }
3258
3553
 
3259
3554
  native_sdk_gtk_menu_action_t *menu_action = &host->menu_actions[host->menu_action_count];
3260
3555
  memset(menu_action, 0, sizeof(*menu_action));
@@ -3643,6 +3938,224 @@ int native_sdk_gtk_create_view(native_sdk_gtk_host_t *host, uint64_t window_id,
3643
3938
  return 1;
3644
3939
  }
3645
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
+
3646
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) {
3647
4160
  native_sdk_gtk_window_t *win = native_sdk_find_window(host, window_id);
3648
4161
  char *label_copy = label_len > 0 ? native_sdk_strndup(label, label_len) : NULL;
@@ -3888,6 +4401,7 @@ int native_sdk_gtk_create_webview(native_sdk_gtk_host_t *host, uint64_t window_i
3888
4401
  webview->transparent = transparent != 0;
3889
4402
  webview->bridge_enabled = bridge_enabled != 0;
3890
4403
  webview->content_manager = manager;
4404
+ native_sdk_watch_webview_pointer_focus(win, web_view);
3891
4405
  native_sdk_apply_webview_frame(webview);
3892
4406
  gtk_overlay_add_overlay(GTK_OVERLAY(win->stack_root), GTK_WIDGET(web_view));
3893
4407
  if (transparent) {