@native-sdk/cli 0.5.4 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/build/app.zig +50 -1
  2. package/build.zig +588 -32
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +3 -1
  5. package/package.json +9 -9
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/rt/rt.zig +280 -8
  9. package/packages/core/sdk/core.ts +481 -2
  10. package/packages/core/sdk/events.ts +17 -8
  11. package/packages/core/src/checker.ts +305 -2
  12. package/packages/core/src/diagnostics.ts +12 -0
  13. package/packages/core/src/emitter.ts +599 -3
  14. package/packages/core/src/typed_ast.ts +51 -4
  15. package/packages/core/src/types.ts +34 -1
  16. package/skill-data/native-ui/SKILL.md +39 -2
  17. package/skill-data/ts-core/SKILL.md +4 -2
  18. package/src/app_runner/root.zig +32 -10
  19. package/src/automation/layout_fingerprint.zig +160 -0
  20. package/src/automation/protocol.zig +82 -33
  21. package/src/automation/root.zig +1 -0
  22. package/src/automation/server.zig +1 -1
  23. package/src/automation/snapshot.zig +68 -9
  24. package/src/automation/watcher.zig +1 -1
  25. package/src/embed/host.zig +5 -0
  26. package/src/embed/ui_host.zig +13 -0
  27. package/src/platform/linux/gtk_host.c +527 -13
  28. package/src/platform/linux/gtk_host.h +25 -0
  29. package/src/platform/linux/root.zig +280 -7
  30. package/src/platform/macos/appkit_host.h +104 -6
  31. package/src/platform/macos/appkit_host.m +1368 -74
  32. package/src/platform/macos/cef_host.mm +110 -1
  33. package/src/platform/macos/root.zig +287 -7
  34. package/src/platform/null_platform.zig +398 -4
  35. package/src/platform/null_platform_tests.zig +35 -0
  36. package/src/platform/root.zig +9 -0
  37. package/src/platform/types.zig +386 -14
  38. package/src/platform/windows/root.zig +298 -9
  39. package/src/platform/windows/webview2_host.cpp +330 -5
  40. package/src/primitives/app_manifest/validation.zig +18 -0
  41. package/src/primitives/canvas/a11y_audit.zig +33 -11
  42. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  43. package/src/primitives/canvas/drawing.zig +25 -0
  44. package/src/primitives/canvas/equality.zig +8 -0
  45. package/src/primitives/canvas/events.zig +103 -16
  46. package/src/primitives/canvas/frame.zig +125 -1
  47. package/src/primitives/canvas/layout_audit.zig +22 -5
  48. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  49. package/src/primitives/canvas/render_tests.zig +13 -10
  50. package/src/primitives/canvas/root.zig +48 -0
  51. package/src/primitives/canvas/terminal_box.zig +471 -0
  52. package/src/primitives/canvas/terminal_grid.zig +978 -0
  53. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  54. package/src/primitives/canvas/test_support.zig +1 -0
  55. package/src/primitives/canvas/tests.zig +1 -0
  56. package/src/primitives/canvas/text_tests.zig +39 -4
  57. package/src/primitives/canvas/tokens.zig +115 -12
  58. package/src/primitives/canvas/ui.zig +524 -5
  59. package/src/primitives/canvas/ui_markup.zig +195 -2
  60. package/src/primitives/canvas/ui_markup_compiled.zig +172 -1
  61. package/src/primitives/canvas/ui_markup_compiled_tests.zig +133 -8
  62. package/src/primitives/canvas/ui_markup_contract.zig +88 -5
  63. package/src/primitives/canvas/ui_markup_contract_tests.zig +39 -1
  64. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  65. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  66. package/src/primitives/canvas/ui_markup_tests.zig +149 -0
  67. package/src/primitives/canvas/ui_markup_view.zig +191 -0
  68. package/src/primitives/canvas/ui_markup_view_tests.zig +403 -19
  69. package/src/primitives/canvas/ui_schema.zig +97 -17
  70. package/src/primitives/canvas/ui_schema_tests.zig +18 -11
  71. package/src/primitives/canvas/ui_tests.zig +141 -9
  72. package/src/primitives/canvas/widget_access.zig +28 -2
  73. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  74. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  75. package/src/primitives/canvas/widget_layout.zig +59 -11
  76. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  77. package/src/primitives/canvas/widget_render.zig +272 -18
  78. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  79. package/src/primitives/canvas/widget_routing.zig +59 -7
  80. package/src/primitives/canvas/widget_runtime.zig +23 -0
  81. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  82. package/src/primitives/canvas/widget_semantics.zig +119 -11
  83. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  84. package/src/primitives/canvas/widget_tree.zig +17 -0
  85. package/src/primitives/canvas/widgets.zig +132 -0
  86. package/src/root.zig +16 -0
  87. package/src/runtime/api.zig +76 -6
  88. package/src/runtime/automation_commands.zig +20 -1
  89. package/src/runtime/automation_snapshot.zig +25 -1
  90. package/src/runtime/automation_widget_dispatch.zig +73 -8
  91. package/src/runtime/bridge_payload.zig +1 -0
  92. package/src/runtime/canvas_frame.zig +62 -7
  93. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  94. package/src/runtime/canvas_frame_helpers.zig +190 -1
  95. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  96. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  97. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  98. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  99. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  100. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  101. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  102. package/src/runtime/canvas_widget_display.zig +1 -1
  103. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  104. package/src/runtime/canvas_widget_events.zig +268 -26
  105. package/src/runtime/canvas_widget_runtime.zig +213 -29
  106. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  107. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  108. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  109. package/src/runtime/canvas_widget_state.zig +6 -0
  110. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  111. package/src/runtime/core.zig +73 -2
  112. package/src/runtime/effects.zig +6389 -54
  113. package/src/runtime/effects_channel_tests.zig +3519 -0
  114. package/src/runtime/effects_pty_tests.zig +1362 -0
  115. package/src/runtime/effects_tests.zig +14 -9
  116. package/src/runtime/effects_video_tests.zig +3609 -0
  117. package/src/runtime/flow.zig +25 -4
  118. package/src/runtime/gpu_surface_events.zig +475 -15
  119. package/src/runtime/media_surface.zig +116 -11
  120. package/src/runtime/media_surface_tests.zig +61 -0
  121. package/src/runtime/pty.zig +1252 -0
  122. package/src/runtime/pty_windows.zig +1780 -0
  123. package/src/runtime/root.zig +25 -0
  124. package/src/runtime/runtime_event_tests.zig +20 -1
  125. package/src/runtime/session_journal.zig +367 -61
  126. package/src/runtime/session_record.zig +16 -0
  127. package/src/runtime/session_replay.zig +395 -11
  128. package/src/runtime/session_tests.zig +114 -19
  129. package/src/runtime/terminal_session.zig +1569 -0
  130. package/src/runtime/terminal_session_tests.zig +450 -0
  131. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  132. package/src/runtime/terminal_vt_stub.zig +13 -0
  133. package/src/runtime/tests.zig +6 -0
  134. package/src/runtime/ts_core_host.zig +980 -140
  135. package/src/runtime/ts_core_host_tests.zig +1059 -1
  136. package/src/runtime/ts_ui_app.zig +16 -5
  137. package/src/runtime/ui_app.zig +2338 -67
  138. package/src/runtime/ui_app_tests.zig +3171 -744
  139. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  140. package/src/runtime/ui_app_window_tests.zig +268 -0
  141. package/src/runtime/view.zig +190 -2
  142. package/src/runtime/view_widget_scroll.zig +218 -68
  143. package/src/runtime/view_widget_text.zig +11 -2
  144. package/src/runtime/view_widget_tree.zig +111 -3
  145. package/src/runtime/window_storage.zig +45 -2
  146. package/src/runtime/window_view_runtime.zig +34 -2
  147. package/src/runtime/window_views.zig +1 -0
  148. package/src/terminal_session_tests_root.zig +15 -0
  149. package/src/tooling/templates.zig +25 -7
@@ -496,6 +496,203 @@ test "runtime dispatches routed canvas widget pointer events" {
496
496
  try std.testing.expect(app_state.last_keyboard_shift);
497
497
  }
498
498
 
499
+ test "only a focused live terminal owns Tab and focus entry suppresses the whole gesture" {
500
+ const TestApp = struct {
501
+ keyboard_count: u32 = 0,
502
+ target_id: canvas.ObjectId = 0,
503
+ target_kind: canvas.WidgetKind = .stack,
504
+ focus_moved: bool = true,
505
+
506
+ fn app(self: *@This()) App {
507
+ return .{
508
+ .context = self,
509
+ .name = "gpu-terminal-tab",
510
+ .source = platform.WebViewSource.html("<h1>GPU</h1>"),
511
+ .event_fn = event,
512
+ };
513
+ }
514
+
515
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
516
+ _ = runtime;
517
+ const self: *@This() = @ptrCast(@alignCast(context));
518
+ switch (event_value) {
519
+ .canvas_widget_keyboard => |keyboard_event| {
520
+ self.keyboard_count += 1;
521
+ self.focus_moved = keyboard_event.keyboard.focus_moved;
522
+ if (keyboard_event.target) |target| {
523
+ self.target_id = target.id;
524
+ self.target_kind = target.kind;
525
+ }
526
+ },
527
+ else => {},
528
+ }
529
+ }
530
+ };
531
+
532
+ const harness = try TestHarness().create(std.testing.allocator, .{});
533
+ defer harness.destroy(std.testing.allocator);
534
+ harness.null_platform.gpu_surfaces = true;
535
+ var app_state: TestApp = .{};
536
+ const app = app_state.app();
537
+ try harness.start(app);
538
+
539
+ _ = try harness.runtime.createView(.{
540
+ .window_id = 1,
541
+ .label = "canvas",
542
+ .kind = .gpu_surface,
543
+ .frame = geometry.RectF.init(0, 0, 320, 180),
544
+ });
545
+ var terminal_grid = canvas.TerminalGrid{
546
+ .background = canvas.Color.rgba(0, 0, 0, 1),
547
+ .foreground = canvas.Color.rgba(1, 1, 1, 1),
548
+ .cursor_color = canvas.Color.rgba(1, 1, 1, 1),
549
+ .selection_color = canvas.Color.rgba(0, 0.5, 1, 1),
550
+ };
551
+ var children = [_]canvas.Widget{
552
+ .{
553
+ .id = 2,
554
+ .kind = .terminal,
555
+ .frame = geometry.RectF.init(10, 10, 200, 100),
556
+ .terminal = .{ .pty = 7, .grid = &terminal_grid },
557
+ },
558
+ .{
559
+ .id = 3,
560
+ .kind = .button,
561
+ .frame = geometry.RectF.init(220, 10, 80, 32),
562
+ .text = "Run",
563
+ },
564
+ };
565
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
566
+ const layout = try canvas.layoutWidgetTree(
567
+ .{ .id = 1, .kind = .panel, .children = &children },
568
+ geometry.RectF.init(0, 0, 320, 180),
569
+ &nodes,
570
+ );
571
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
572
+
573
+ // Pointer focus starts in the terminal. Tab must route to that same
574
+ // widget with no focus move; the UiApp layer then hands it to the
575
+ // bound terminal session instead of the neighboring button.
576
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
577
+ .window_id = 1,
578
+ .label = "canvas",
579
+ .kind = .pointer_down,
580
+ .x = 40,
581
+ .y = 40,
582
+ } });
583
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
584
+
585
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
586
+ .window_id = 1,
587
+ .label = "canvas",
588
+ .kind = .key_down,
589
+ .key = "tab",
590
+ } });
591
+ try std.testing.expectEqual(@as(u32, 1), app_state.keyboard_count);
592
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
593
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.target_id);
594
+ try std.testing.expectEqual(canvas.WidgetKind.terminal, app_state.target_kind);
595
+ try std.testing.expect(!app_state.focus_moved);
596
+
597
+ // A Tab pressed while the terminal already owns focus is terminal
598
+ // input for its whole lifetime, including the release protocols
599
+ // such as kitty keyboard reporting can encode.
600
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
601
+ .window_id = 1,
602
+ .label = "canvas",
603
+ .kind = .key_up,
604
+ .key = "tab",
605
+ } });
606
+ try std.testing.expectEqual(@as(u32, 2), app_state.keyboard_count);
607
+
608
+ // The inverse gesture is focus traversal: Shift+Tab from the button
609
+ // enters the live terminal, but its repeat and release must not be
610
+ // reinterpreted against the newly focused pty.
611
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
612
+ .window_id = 1,
613
+ .label = "canvas",
614
+ .kind = .pointer_down,
615
+ .x = 240,
616
+ .y = 24,
617
+ } });
618
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), harness.runtime.views[0].canvas_widget_focused_id);
619
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
620
+ .window_id = 1,
621
+ .label = "canvas",
622
+ .kind = .key_down,
623
+ .key = "tab",
624
+ .modifiers = .{ .shift = true },
625
+ } });
626
+ try std.testing.expectEqual(@as(u32, 3), app_state.keyboard_count);
627
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
628
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.target_id);
629
+ try std.testing.expect(app_state.focus_moved);
630
+ try std.testing.expect(harness.runtime.views[0].canvas_widget_terminal_focus_entry_tab_held);
631
+
632
+ // Auto-repeat while Tab is held, then its key-up: neither produces
633
+ // another widget event at the terminal.
634
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
635
+ .window_id = 1,
636
+ .label = "canvas",
637
+ .kind = .key_down,
638
+ .key = "tab",
639
+ .modifiers = .{ .shift = true },
640
+ } });
641
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
642
+ .window_id = 1,
643
+ .label = "canvas",
644
+ .kind = .key_up,
645
+ .key = "tab",
646
+ .modifiers = .{ .shift = true },
647
+ } });
648
+ try std.testing.expectEqual(@as(u32, 3), app_state.keyboard_count);
649
+ try std.testing.expect(!harness.runtime.views[0].canvas_widget_terminal_focus_entry_tab_held);
650
+
651
+ // The same nonzero binding after session exit is no longer an input
652
+ // owner: its published grid says the session cannot accept input,
653
+ // so Tab resumes ordinary traversal to the button.
654
+ terminal_grid.running = false;
655
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
656
+ .window_id = 1,
657
+ .label = "canvas",
658
+ .kind = .key_down,
659
+ .key = "tab",
660
+ } });
661
+ try std.testing.expectEqual(@as(u32, 4), app_state.keyboard_count);
662
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), harness.runtime.views[0].canvas_widget_focused_id);
663
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.target_id);
664
+ try std.testing.expect(app_state.focus_moved);
665
+
666
+ // The explicit unbound sentinel behaves the same even if a caller
667
+ // supplied a running-looking grid by mistake: no pty means no Tab
668
+ // input owner.
669
+ terminal_grid.running = true;
670
+ children[0].terminal.pty = 0;
671
+ const unbound_layout = try canvas.layoutWidgetTree(
672
+ .{ .id = 1, .kind = .panel, .children = &children },
673
+ geometry.RectF.init(0, 0, 320, 180),
674
+ &nodes,
675
+ );
676
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", unbound_layout);
677
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
678
+ .window_id = 1,
679
+ .label = "canvas",
680
+ .kind = .pointer_down,
681
+ .x = 40,
682
+ .y = 40,
683
+ } });
684
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
685
+ .window_id = 1,
686
+ .label = "canvas",
687
+ .kind = .key_down,
688
+ .key = "tab",
689
+ } });
690
+ try std.testing.expectEqual(@as(u32, 5), app_state.keyboard_count);
691
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), harness.runtime.views[0].canvas_widget_focused_id);
692
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.target_id);
693
+ try std.testing.expect(app_state.focus_moved);
694
+ }
695
+
499
696
  test "runtime routes captured canvas pointer drags without outside release activation" {
500
697
  const TestApp = struct {
501
698
  command_count: u32 = 0,
@@ -866,6 +1063,1632 @@ test "runtime applies GPU text and IME input to focused canvas text fields" {
866
1063
  try std.testing.expectEqual(@as(u32, 5), app_state.widget_keyboard_count);
867
1064
  }
868
1065
 
1066
+ test "a claimed key stays claimed when its command rebuilds the tree mid-dispatch" {
1067
+ const TestApp = struct {
1068
+ committed_count: u32 = 0,
1069
+ activations: u32 = 0,
1070
+
1071
+ fn app(self: *@This()) App {
1072
+ return .{ .context = self, .name = "gpu-claim-vs-rebuild", .source = platform.WebViewSource.html("<h1>R</h1>"), .event_fn = event };
1073
+ }
1074
+
1075
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
1076
+ const self: *@This() = @ptrCast(@alignCast(context));
1077
+ switch (event_value) {
1078
+ .canvas_widget_keyboard => |keyboard_event| {
1079
+ if (keyboard_event.target != null and keyboard_event.keyboard.phase == .key_down) {
1080
+ // The button's command: rebuild WITHOUT the
1081
+ // button — the exact tree mutation that used to
1082
+ // erase the claim before the committed-text
1083
+ // check ran.
1084
+ self.activations += 1;
1085
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
1086
+ const layout = canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{} }, geometry.RectF.init(0, 0, 240, 120), &nodes) catch return;
1087
+ _ = runtime.setCanvasWidgetLayout(1, "canvas", layout) catch return;
1088
+ runtime.views[0].canvas_widget_focused_id = 0;
1089
+ return;
1090
+ }
1091
+ if (keyboard_event.keyboard.phase == .text_input and keyboard_event.target == null) {
1092
+ self.committed_count += 1;
1093
+ }
1094
+ },
1095
+ else => {},
1096
+ }
1097
+ }
1098
+ };
1099
+
1100
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1101
+ defer harness.destroy(std.testing.allocator);
1102
+ harness.null_platform.gpu_surfaces = true;
1103
+ var app_state: TestApp = .{};
1104
+ const app = app_state.app();
1105
+ try harness.start(app);
1106
+
1107
+ _ = try harness.runtime.createView(.{
1108
+ .window_id = 1,
1109
+ .label = "canvas",
1110
+ .kind = .gpu_surface,
1111
+ .frame = geometry.RectF.init(0, 0, 240, 120),
1112
+ });
1113
+ const children = [_]canvas.Widget{
1114
+ .{
1115
+ .id = 2,
1116
+ .kind = .button,
1117
+ .frame = geometry.RectF.init(10, 10, 96, 32),
1118
+ .text = "Run",
1119
+ },
1120
+ };
1121
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
1122
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
1123
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1124
+ harness.runtime.views[0].focused = true;
1125
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1126
+
1127
+ // ONE physical Space: the focused button claims it (activation) and
1128
+ // its command removes the button. The claim was decided against the
1129
+ // tree the input routed through, so the same keystroke must NOT
1130
+ // also type a literal space through the target-less fallback.
1131
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1132
+ .window_id = 1,
1133
+ .label = "canvas",
1134
+ .kind = .key_down,
1135
+ .key = "space",
1136
+ .text = " ",
1137
+ } });
1138
+ try std.testing.expect(app_state.activations > 0);
1139
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
1140
+
1141
+ // The SPLIT-EVENT shape (AppKit/Windows): the claimed key_down
1142
+ // carries no text and the committed character follows as a separate
1143
+ // event — the claim must carry across the split, because the
1144
+ // activation already rebuilt the tree by the time the text arrives.
1145
+ const again = [_]canvas.Widget{
1146
+ .{
1147
+ .id = 4,
1148
+ .kind = .button,
1149
+ .frame = geometry.RectF.init(10, 10, 96, 32),
1150
+ .text = "Run",
1151
+ },
1152
+ };
1153
+ var nodes_again: [3]canvas.WidgetLayoutNode = undefined;
1154
+ const layout_again = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &again }, geometry.RectF.init(0, 0, 240, 120), &nodes_again);
1155
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout_again);
1156
+ harness.runtime.views[0].canvas_widget_focused_id = 4;
1157
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1158
+ .window_id = 1,
1159
+ .label = "canvas",
1160
+ .kind = .key_down,
1161
+ .key = "space",
1162
+ } });
1163
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1164
+ .window_id = 1,
1165
+ .label = "canvas",
1166
+ .kind = .text_input,
1167
+ .text = " ",
1168
+ } });
1169
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
1170
+
1171
+ // The carry is ONE-SHOT: ordinary typing afterwards still flows.
1172
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1173
+ .window_id = 1,
1174
+ .label = "canvas",
1175
+ .kind = .text_input,
1176
+ .text = "x",
1177
+ } });
1178
+ try std.testing.expectEqual(@as(u32, 1), app_state.committed_count);
1179
+
1180
+ // The carry DIES AT BLUR: it marks "the text arriving next belongs
1181
+ // to the claimed key_down just routed", and a focus move breaks
1182
+ // that adjacency. A claimed activation that moves focus before its
1183
+ // committed text arrives (Enter/Space commands routinely do) must
1184
+ // not leave the carry armed to swallow the refocused surface's
1185
+ // first unrelated commit.
1186
+ _ = try harness.runtime.createView(.{
1187
+ .window_id = 1,
1188
+ .label = "other",
1189
+ .kind = .gpu_surface,
1190
+ .frame = geometry.RectF.init(0, 120, 240, 120),
1191
+ });
1192
+ const rearmed = [_]canvas.Widget{
1193
+ .{
1194
+ .id = 6,
1195
+ .kind = .button,
1196
+ .frame = geometry.RectF.init(10, 10, 96, 32),
1197
+ .text = "Run",
1198
+ },
1199
+ };
1200
+ var nodes_rearmed: [3]canvas.WidgetLayoutNode = undefined;
1201
+ const layout_rearmed = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &rearmed }, geometry.RectF.init(0, 0, 240, 120), &nodes_rearmed);
1202
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout_rearmed);
1203
+ harness.runtime.views[0].canvas_widget_focused_id = 6;
1204
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1205
+ .window_id = 1,
1206
+ .label = "canvas",
1207
+ .kind = .key_down,
1208
+ .key = "space",
1209
+ } });
1210
+ try harness.runtime.focusView(1, "other");
1211
+ try harness.runtime.focusView(1, "canvas");
1212
+ // The space literal itself — exactly what a surviving carry would
1213
+ // swallow — must flow: the grace died at blur.
1214
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1215
+ .window_id = 1,
1216
+ .label = "canvas",
1217
+ .kind = .text_input,
1218
+ .text = " ",
1219
+ } });
1220
+ try std.testing.expectEqual(@as(u32, 2), app_state.committed_count);
1221
+
1222
+ // And the carry is KEYED: armed by a claimed Space, a DIFFERENT
1223
+ // key's committed text arriving next (hosts that emit input-method
1224
+ // text before its key_down can interleave one) flows instead of
1225
+ // feeding the stale latch.
1226
+ const rearmed_again = [_]canvas.Widget{
1227
+ .{
1228
+ .id = 8,
1229
+ .kind = .button,
1230
+ .frame = geometry.RectF.init(10, 10, 96, 32),
1231
+ .text = "Run",
1232
+ },
1233
+ };
1234
+ var nodes_rearmed_again: [3]canvas.WidgetLayoutNode = undefined;
1235
+ const layout_rearmed_again = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &rearmed_again }, geometry.RectF.init(0, 0, 240, 120), &nodes_rearmed_again);
1236
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout_rearmed_again);
1237
+ harness.runtime.views[0].canvas_widget_focused_id = 8;
1238
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1239
+ .window_id = 1,
1240
+ .label = "canvas",
1241
+ .kind = .key_down,
1242
+ .key = "space",
1243
+ } });
1244
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1245
+ .window_id = 1,
1246
+ .label = "canvas",
1247
+ .kind = .text_input,
1248
+ .text = "q",
1249
+ } });
1250
+ try std.testing.expectEqual(@as(u32, 3), app_state.committed_count);
1251
+ }
1252
+
1253
+ test "a widget-started composition resolves in its starting editor after focus moves" {
1254
+ const TestApp = struct {
1255
+ committed_count: u32 = 0,
1256
+
1257
+ fn app(self: *@This()) App {
1258
+ return .{ .context = self, .name = "gpu-widget-ime-ownership", .source = platform.WebViewSource.html("<h1>W</h1>"), .event_fn = event };
1259
+ }
1260
+
1261
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
1262
+ _ = runtime;
1263
+ const self: *@This() = @ptrCast(@alignCast(context));
1264
+ switch (event_value) {
1265
+ .canvas_widget_keyboard => |keyboard_event| {
1266
+ if (keyboard_event.keyboard.phase != .text_input) return;
1267
+ if (keyboard_event.target != null) return;
1268
+ self.committed_count += 1;
1269
+ },
1270
+ else => {},
1271
+ }
1272
+ }
1273
+ };
1274
+
1275
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1276
+ defer harness.destroy(std.testing.allocator);
1277
+ harness.null_platform.gpu_surfaces = true;
1278
+ var app_state: TestApp = .{};
1279
+ const app = app_state.app();
1280
+ try harness.start(app);
1281
+
1282
+ _ = try harness.runtime.createView(.{
1283
+ .window_id = 1,
1284
+ .label = "canvas",
1285
+ .kind = .gpu_surface,
1286
+ .frame = geometry.RectF.init(0, 0, 240, 120),
1287
+ });
1288
+ const children = [_]canvas.Widget{
1289
+ .{
1290
+ .id = 2,
1291
+ .kind = .text_field,
1292
+ .frame = geometry.RectF.init(10, 10, 160, 32),
1293
+ .text = "",
1294
+ },
1295
+ };
1296
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
1297
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
1298
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1299
+ harness.runtime.views[0].focused = true;
1300
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1301
+
1302
+ // The composition STARTS in the field.
1303
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1304
+ .window_id = 1,
1305
+ .label = "canvas",
1306
+ .kind = .ime_set_composition,
1307
+ .text = "ka",
1308
+ .composition_cursor = 2,
1309
+ } });
1310
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1311
+ try std.testing.expectEqualStrings("ka", retained.findById(2).?.widget.text);
1312
+ try std.testing.expect(retained.findById(2).?.widget.text_composition != null);
1313
+
1314
+ // Focus leaves the field mid-sequence; the host then commits the
1315
+ // marked text unchanged. The commit resolves the EDITOR IT STARTED
1316
+ // IN — its marked text becomes plain — and never leaks through the
1317
+ // target-less fallback.
1318
+ harness.runtime.views[0].canvas_widget_focused_id = 0;
1319
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1320
+ .window_id = 1,
1321
+ .label = "canvas",
1322
+ .kind = .ime_commit_composition,
1323
+ .text = "",
1324
+ } });
1325
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1326
+ try std.testing.expectEqualStrings("ka", retained.findById(2).?.widget.text);
1327
+ try std.testing.expect(retained.findById(2).?.widget.text_composition == null);
1328
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
1329
+
1330
+ // The sequence is closed: target-less typing flows normally again.
1331
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1332
+ .window_id = 1,
1333
+ .label = "canvas",
1334
+ .kind = .text_input,
1335
+ .text = "x",
1336
+ } });
1337
+ try std.testing.expectEqual(@as(u32, 1), app_state.committed_count);
1338
+ }
1339
+
1340
+ test "a target-less converted commit stays target-less past a focus move" {
1341
+ const TestApp = struct {
1342
+ committed_count: u32 = 0,
1343
+ last_committed: [16]u8 = undefined,
1344
+ last_committed_len: usize = 0,
1345
+
1346
+ fn app(self: *@This()) App {
1347
+ return .{ .context = self, .name = "gpu-targetless-converted-commit", .source = platform.WebViewSource.html("<h1>T</h1>"), .event_fn = event };
1348
+ }
1349
+
1350
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
1351
+ _ = runtime;
1352
+ const self: *@This() = @ptrCast(@alignCast(context));
1353
+ switch (event_value) {
1354
+ .canvas_widget_keyboard => |keyboard_event| {
1355
+ if (keyboard_event.keyboard.phase != .text_input) return;
1356
+ if (keyboard_event.target != null) return;
1357
+ self.committed_count += 1;
1358
+ const text = keyboard_event.keyboard.text;
1359
+ const len = @min(text.len, self.last_committed.len);
1360
+ @memcpy(self.last_committed[0..len], text[0..len]);
1361
+ self.last_committed_len = len;
1362
+ },
1363
+ else => {},
1364
+ }
1365
+ }
1366
+ };
1367
+
1368
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1369
+ defer harness.destroy(std.testing.allocator);
1370
+ harness.null_platform.gpu_surfaces = true;
1371
+ var app_state: TestApp = .{};
1372
+ const app = app_state.app();
1373
+ try harness.start(app);
1374
+
1375
+ _ = try harness.runtime.createView(.{
1376
+ .window_id = 1,
1377
+ .label = "canvas",
1378
+ .kind = .gpu_surface,
1379
+ .frame = geometry.RectF.init(0, 0, 240, 120),
1380
+ });
1381
+ const children = [_]canvas.Widget{
1382
+ .{ .id = 2, .kind = .text_field, .frame = geometry.RectF.init(10, 10, 160, 32), .text = "" },
1383
+ };
1384
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
1385
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
1386
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1387
+ harness.runtime.views[0].focused = true;
1388
+
1389
+ // The composition starts TARGET-LESS; a text field then takes
1390
+ // focus mid-sequence. The converted commit (cancel-then-text_input)
1391
+ // still belongs to the target-less consumer: the trailing text
1392
+ // delivers target-less, the field stays untouched.
1393
+ harness.runtime.views[0].canvas_widget_focused_id = 0;
1394
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1395
+ .window_id = 1,
1396
+ .label = "canvas",
1397
+ .kind = .ime_set_composition,
1398
+ .text = "ka",
1399
+ } });
1400
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1401
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1402
+ .window_id = 1,
1403
+ .label = "canvas",
1404
+ .kind = .ime_cancel_composition,
1405
+ } });
1406
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1407
+ .window_id = 1,
1408
+ .label = "canvas",
1409
+ .kind = .text_input,
1410
+ .text = "KA",
1411
+ } });
1412
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1413
+ try std.testing.expectEqualStrings("", retained.findById(2).?.widget.text);
1414
+ try std.testing.expectEqual(@as(u32, 1), app_state.committed_count);
1415
+ try std.testing.expectEqualStrings("KA", app_state.last_committed[0..app_state.last_committed_len]);
1416
+
1417
+ // A plain target-less cancel is chased by its own key_down, which
1418
+ // disarms the grace: the next typing routes to the focused field.
1419
+ harness.runtime.views[0].canvas_widget_focused_id = 0;
1420
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1421
+ .window_id = 1,
1422
+ .label = "canvas",
1423
+ .kind = .ime_set_composition,
1424
+ .text = "ne",
1425
+ } });
1426
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1427
+ .window_id = 1,
1428
+ .label = "canvas",
1429
+ .kind = .ime_cancel_composition,
1430
+ } });
1431
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1432
+ .window_id = 1,
1433
+ .label = "canvas",
1434
+ .kind = .key_down,
1435
+ .key = "escape",
1436
+ } });
1437
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1438
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1439
+ .window_id = 1,
1440
+ .label = "canvas",
1441
+ .kind = .text_input,
1442
+ .text = "z",
1443
+ } });
1444
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1445
+ try std.testing.expectEqualStrings("z", retained.findById(2).?.widget.text);
1446
+ try std.testing.expectEqual(@as(u32, 1), app_state.committed_count);
1447
+ }
1448
+
1449
+ test "a converted commit's trailing text lands in the composing editor, not the new focus" {
1450
+ const TestApp = struct {
1451
+ committed_count: u32 = 0,
1452
+
1453
+ fn app(self: *@This()) App {
1454
+ return .{ .context = self, .name = "gpu-ime-converted-commit", .source = platform.WebViewSource.html("<h1>C</h1>"), .event_fn = event };
1455
+ }
1456
+
1457
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
1458
+ _ = runtime;
1459
+ const self: *@This() = @ptrCast(@alignCast(context));
1460
+ switch (event_value) {
1461
+ .canvas_widget_keyboard => |keyboard_event| {
1462
+ if (keyboard_event.keyboard.phase != .text_input) return;
1463
+ if (keyboard_event.target != null) return;
1464
+ self.committed_count += 1;
1465
+ },
1466
+ else => {},
1467
+ }
1468
+ }
1469
+ };
1470
+
1471
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1472
+ defer harness.destroy(std.testing.allocator);
1473
+ harness.null_platform.gpu_surfaces = true;
1474
+ var app_state: TestApp = .{};
1475
+ const app = app_state.app();
1476
+ try harness.start(app);
1477
+
1478
+ _ = try harness.runtime.createView(.{
1479
+ .window_id = 1,
1480
+ .label = "canvas",
1481
+ .kind = .gpu_surface,
1482
+ .frame = geometry.RectF.init(0, 0, 240, 120),
1483
+ });
1484
+ const two_fields = [_]canvas.Widget{
1485
+ .{ .id = 2, .kind = .text_field, .frame = geometry.RectF.init(10, 10, 160, 32), .text = "" },
1486
+ .{ .id = 3, .kind = .text_field, .frame = geometry.RectF.init(10, 52, 160, 32), .text = "" },
1487
+ };
1488
+ var nodes: [4]canvas.WidgetLayoutNode = undefined;
1489
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &two_fields }, geometry.RectF.init(0, 0, 240, 120), &nodes);
1490
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1491
+ harness.runtime.views[0].focused = true;
1492
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1493
+
1494
+ // Field A composes; focus moves to B mid-sequence. The hosts encode
1495
+ // a converted commit (result differs from the marked text) as
1496
+ // cancel-then-text_input: BOTH must land in A — the editor that
1497
+ // composed — never in B, never target-less.
1498
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1499
+ .window_id = 1,
1500
+ .label = "canvas",
1501
+ .kind = .ime_set_composition,
1502
+ .text = "ka",
1503
+ .composition_cursor = 2,
1504
+ } });
1505
+ harness.runtime.views[0].canvas_widget_focused_id = 3;
1506
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1507
+ .window_id = 1,
1508
+ .label = "canvas",
1509
+ .kind = .ime_cancel_composition,
1510
+ } });
1511
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1512
+ .window_id = 1,
1513
+ .label = "canvas",
1514
+ .kind = .text_input,
1515
+ .text = "KA",
1516
+ } });
1517
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1518
+ try std.testing.expectEqualStrings("KA", retained.findById(2).?.widget.text);
1519
+ try std.testing.expectEqualStrings("", retained.findById(3).?.widget.text);
1520
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
1521
+
1522
+ // A PLAIN cancel is chased by its own key_down, which disarms the
1523
+ // grace: the user's next typing routes by focus as usual.
1524
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1525
+ .window_id = 1,
1526
+ .label = "canvas",
1527
+ .kind = .ime_set_composition,
1528
+ .text = "ne",
1529
+ .composition_cursor = 2,
1530
+ } });
1531
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1532
+ try std.testing.expectEqualStrings("ne", retained.findById(3).?.widget.text);
1533
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1534
+ .window_id = 1,
1535
+ .label = "canvas",
1536
+ .kind = .ime_cancel_composition,
1537
+ } });
1538
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1539
+ .window_id = 1,
1540
+ .label = "canvas",
1541
+ .kind = .key_down,
1542
+ .key = "escape",
1543
+ } });
1544
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1545
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1546
+ .window_id = 1,
1547
+ .label = "canvas",
1548
+ .kind = .text_input,
1549
+ .text = "z",
1550
+ } });
1551
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1552
+ try std.testing.expectEqualStrings("KAz", retained.findById(2).?.widget.text);
1553
+ try std.testing.expectEqualStrings("", retained.findById(3).?.widget.text);
1554
+ }
1555
+
1556
+ test "a converted commit whose owner vanished after the cancel is swallowed" {
1557
+ const TestApp = struct {
1558
+ committed_count: u32 = 0,
1559
+
1560
+ fn app(self: *@This()) App {
1561
+ return .{ .context = self, .name = "gpu-ime-grace-orphan", .source = platform.WebViewSource.html("<h1>G</h1>"), .event_fn = event };
1562
+ }
1563
+
1564
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
1565
+ _ = runtime;
1566
+ const self: *@This() = @ptrCast(@alignCast(context));
1567
+ switch (event_value) {
1568
+ .canvas_widget_keyboard => |keyboard_event| {
1569
+ if (keyboard_event.keyboard.phase != .text_input) return;
1570
+ if (keyboard_event.target != null) return;
1571
+ self.committed_count += 1;
1572
+ },
1573
+ else => {},
1574
+ }
1575
+ }
1576
+ };
1577
+
1578
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1579
+ defer harness.destroy(std.testing.allocator);
1580
+ harness.null_platform.gpu_surfaces = true;
1581
+ var app_state: TestApp = .{};
1582
+ const app = app_state.app();
1583
+ try harness.start(app);
1584
+
1585
+ _ = try harness.runtime.createView(.{
1586
+ .window_id = 1,
1587
+ .label = "canvas",
1588
+ .kind = .gpu_surface,
1589
+ .frame = geometry.RectF.init(0, 0, 240, 120),
1590
+ });
1591
+ const two_fields = [_]canvas.Widget{
1592
+ .{ .id = 2, .kind = .text_field, .frame = geometry.RectF.init(10, 10, 160, 32), .text = "" },
1593
+ .{ .id = 3, .kind = .text_field, .frame = geometry.RectF.init(10, 52, 160, 32), .text = "" },
1594
+ };
1595
+ var nodes: [4]canvas.WidgetLayoutNode = undefined;
1596
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &two_fields }, geometry.RectF.init(0, 0, 240, 120), &nodes);
1597
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1598
+ harness.runtime.views[0].focused = true;
1599
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1600
+
1601
+ // A composes; focus moves to B; the cancel routes to A and arms the
1602
+ // grace — and then A is rebuilt away before the trailing commit.
1603
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1604
+ .window_id = 1,
1605
+ .label = "canvas",
1606
+ .kind = .ime_set_composition,
1607
+ .text = "ka",
1608
+ .composition_cursor = 2,
1609
+ } });
1610
+ harness.runtime.views[0].canvas_widget_focused_id = 3;
1611
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1612
+ .window_id = 1,
1613
+ .label = "canvas",
1614
+ .kind = .ime_cancel_composition,
1615
+ } });
1616
+ const only_b = [_]canvas.Widget{
1617
+ .{ .id = 3, .kind = .text_field, .frame = geometry.RectF.init(10, 52, 160, 32), .text = "" },
1618
+ };
1619
+ var nodes_b: [3]canvas.WidgetLayoutNode = undefined;
1620
+ const layout_b = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &only_b }, geometry.RectF.init(0, 0, 240, 120), &nodes_b);
1621
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout_b);
1622
+
1623
+ // The dead owner converts the grace to a swallow: the composed
1624
+ // result resolves nowhere — never in B, never target-less.
1625
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1626
+ .window_id = 1,
1627
+ .label = "canvas",
1628
+ .kind = .text_input,
1629
+ .text = "KA",
1630
+ } });
1631
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1632
+ try std.testing.expectEqualStrings("", retained.findById(3).?.widget.text);
1633
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
1634
+ }
1635
+
1636
+ test "a recreated surface never inherits a stale target-less composition" {
1637
+ const TestApp = struct {
1638
+ committed_count: u32 = 0,
1639
+
1640
+ fn app(self: *@This()) App {
1641
+ return .{ .context = self, .name = "gpu-ime-view-recreate", .source = platform.WebViewSource.html("<h1>V</h1>"), .event_fn = event };
1642
+ }
1643
+
1644
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
1645
+ _ = runtime;
1646
+ const self: *@This() = @ptrCast(@alignCast(context));
1647
+ switch (event_value) {
1648
+ .canvas_widget_keyboard => |keyboard_event| {
1649
+ if (keyboard_event.keyboard.phase != .text_input) return;
1650
+ if (keyboard_event.target != null) return;
1651
+ self.committed_count += 1;
1652
+ },
1653
+ else => {},
1654
+ }
1655
+ }
1656
+ };
1657
+
1658
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1659
+ defer harness.destroy(std.testing.allocator);
1660
+ harness.null_platform.gpu_surfaces = true;
1661
+ var app_state: TestApp = .{};
1662
+ const app = app_state.app();
1663
+ try harness.start(app);
1664
+
1665
+ // The FIRST surface composes target-less mid-preedit...
1666
+ _ = try harness.runtime.createView(.{
1667
+ .window_id = 1,
1668
+ .label = "canvas",
1669
+ .kind = .gpu_surface,
1670
+ .frame = geometry.RectF.init(0, 0, 240, 120),
1671
+ });
1672
+ harness.runtime.views[0].focused = true;
1673
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1674
+ .window_id = 1,
1675
+ .label = "canvas",
1676
+ .kind = .ime_set_composition,
1677
+ .text = "ka",
1678
+ } });
1679
+
1680
+ // ...and is CLOSED, then recreated under the same window+label,
1681
+ // this time with a focused editor. The stale sequence died with the
1682
+ // old view: the editor's first composition is ITS OWN, not a
1683
+ // continuation to bypass.
1684
+ try harness.runtime.closeView(1, "canvas");
1685
+ _ = try harness.runtime.createView(.{
1686
+ .window_id = 1,
1687
+ .label = "canvas",
1688
+ .kind = .gpu_surface,
1689
+ .frame = geometry.RectF.init(0, 0, 240, 120),
1690
+ });
1691
+ const children = [_]canvas.Widget{
1692
+ .{ .id = 2, .kind = .text_field, .frame = geometry.RectF.init(10, 10, 160, 32), .text = "" },
1693
+ };
1694
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
1695
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
1696
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1697
+ harness.runtime.views[0].focused = true;
1698
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1699
+
1700
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1701
+ .window_id = 1,
1702
+ .label = "canvas",
1703
+ .kind = .ime_set_composition,
1704
+ .text = "ne",
1705
+ .composition_cursor = 2,
1706
+ } });
1707
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1708
+ try std.testing.expectEqualStrings("ne", retained.findById(2).?.widget.text);
1709
+ try std.testing.expect(retained.findById(2).?.widget.text_composition != null);
1710
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
1711
+ }
1712
+
1713
+ test "a programmatic focus move disarms the cancel grace like a pointer one" {
1714
+ const TestApp = struct {
1715
+ committed_count: u32 = 0,
1716
+
1717
+ fn app(self: *@This()) App {
1718
+ return .{ .context = self, .name = "gpu-ime-grace-focusview", .source = platform.WebViewSource.html("<h1>F</h1>"), .event_fn = event };
1719
+ }
1720
+
1721
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
1722
+ _ = runtime;
1723
+ const self: *@This() = @ptrCast(@alignCast(context));
1724
+ switch (event_value) {
1725
+ .canvas_widget_keyboard => |keyboard_event| {
1726
+ if (keyboard_event.keyboard.phase != .text_input) return;
1727
+ if (keyboard_event.target != null) return;
1728
+ self.committed_count += 1;
1729
+ },
1730
+ else => {},
1731
+ }
1732
+ }
1733
+ };
1734
+
1735
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1736
+ defer harness.destroy(std.testing.allocator);
1737
+ harness.null_platform.gpu_surfaces = true;
1738
+ var app_state: TestApp = .{};
1739
+ const app = app_state.app();
1740
+ try harness.start(app);
1741
+
1742
+ _ = try harness.runtime.createView(.{
1743
+ .window_id = 1,
1744
+ .label = "canvas-a",
1745
+ .kind = .gpu_surface,
1746
+ .frame = geometry.RectF.init(0, 0, 240, 120),
1747
+ });
1748
+ _ = try harness.runtime.createView(.{
1749
+ .window_id = 1,
1750
+ .label = "canvas-b",
1751
+ .kind = .gpu_surface,
1752
+ .frame = geometry.RectF.init(0, 120, 240, 120),
1753
+ });
1754
+ const children = [_]canvas.Widget{
1755
+ .{ .id = 2, .kind = .text_field, .frame = geometry.RectF.init(10, 10, 160, 32), .text = "" },
1756
+ };
1757
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
1758
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
1759
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas-a", layout);
1760
+ try harness.runtime.focusView(1, "canvas-a");
1761
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1762
+
1763
+ // A composition cancels (arming the grace), then the PROGRAMMATIC
1764
+ // focus path — focusView, the third grace-lifecycle entry point —
1765
+ // moves focus away and the canvas is rebuilt without the owner.
1766
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1767
+ .window_id = 1,
1768
+ .label = "canvas-a",
1769
+ .kind = .ime_set_composition,
1770
+ .text = "ka",
1771
+ .composition_cursor = 2,
1772
+ } });
1773
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1774
+ .window_id = 1,
1775
+ .label = "canvas-a",
1776
+ .kind = .ime_cancel_composition,
1777
+ } });
1778
+ try harness.runtime.focusView(1, "canvas-b");
1779
+ const replacement = [_]canvas.Widget{
1780
+ .{ .id = 3, .kind = .text_field, .frame = geometry.RectF.init(10, 10, 160, 32), .text = "" },
1781
+ };
1782
+ var nodes_b: [3]canvas.WidgetLayoutNode = undefined;
1783
+ const layout_b = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &replacement }, geometry.RectF.init(0, 0, 240, 120), &nodes_b);
1784
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas-a", layout_b);
1785
+ try harness.runtime.focusView(1, "canvas-a");
1786
+ harness.runtime.views[0].canvas_widget_focused_id = 3;
1787
+
1788
+ // The blur disarmed the grace: this fresh commit belongs to the
1789
+ // NOW-focused editor — never swallowed with (or routed to) the
1790
+ // stale sequence.
1791
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1792
+ .window_id = 1,
1793
+ .label = "canvas-a",
1794
+ .kind = .text_input,
1795
+ .text = "x",
1796
+ } });
1797
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas-a");
1798
+ try std.testing.expectEqualStrings("x", retained.findById(3).?.widget.text);
1799
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
1800
+ }
1801
+
1802
+ test "a target-less composition owns its surface's keys - and its resolution releases them" {
1803
+ const TestApp = struct {
1804
+ keydown_count: u32 = 0,
1805
+ committed_count: u32 = 0,
1806
+
1807
+ fn app(self: *@This()) App {
1808
+ return .{ .context = self, .name = "gpu-ime-key-ownership", .source = platform.WebViewSource.html("<h1>K</h1>"), .event_fn = event };
1809
+ }
1810
+
1811
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
1812
+ _ = runtime;
1813
+ const self: *@This() = @ptrCast(@alignCast(context));
1814
+ switch (event_value) {
1815
+ .canvas_widget_keyboard => |keyboard_event| {
1816
+ if (keyboard_event.target != null) return;
1817
+ if (keyboard_event.keyboard.phase == .key_down) self.keydown_count += 1;
1818
+ if (keyboard_event.keyboard.phase == .text_input) self.committed_count += 1;
1819
+ },
1820
+ else => {},
1821
+ }
1822
+ }
1823
+ };
1824
+
1825
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1826
+ defer harness.destroy(std.testing.allocator);
1827
+ harness.null_platform.gpu_surfaces = true;
1828
+ var app_state: TestApp = .{};
1829
+ const app = app_state.app();
1830
+ try harness.start(app);
1831
+
1832
+ _ = try harness.runtime.createView(.{
1833
+ .window_id = 1,
1834
+ .label = "canvas",
1835
+ .kind = .gpu_surface,
1836
+ .frame = geometry.RectF.init(0, 0, 240, 120),
1837
+ });
1838
+ harness.runtime.views[0].focused = true;
1839
+
1840
+ // DURING the composition, key_downs are input-method machinery
1841
+ // (candidate navigation, the confirming Enter on hosts that surface
1842
+ // the key before the commit): never app-level keys — a terminal
1843
+ // mapping ArrowDown would scroll while the user is picking a
1844
+ // candidate.
1845
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1846
+ .window_id = 1,
1847
+ .label = "canvas",
1848
+ .kind = .ime_set_composition,
1849
+ .text = "\xe3\x81\x8b",
1850
+ } });
1851
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1852
+ .window_id = 1,
1853
+ .label = "canvas",
1854
+ .kind = .key_down,
1855
+ .key = "arrowdown",
1856
+ } });
1857
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1858
+ .window_id = 1,
1859
+ .label = "canvas",
1860
+ .kind = .key_down,
1861
+ .key = "enter",
1862
+ } });
1863
+ try std.testing.expectEqual(@as(u32, 0), app_state.keydown_count);
1864
+
1865
+ // COMMAND CHORDS stay live mid-composition: input methods never
1866
+ // consume them, so Ctrl+Alt+C here is a genuine shortcut the app
1867
+ // must hear (on Windows alone Ctrl+Alt is AltGr and composes text;
1868
+ // this build treats it as the chord it is on this host).
1869
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1870
+ .window_id = 1,
1871
+ .label = "canvas",
1872
+ .kind = .key_down,
1873
+ .key = "c",
1874
+ .modifiers = .{ .control = true, .option = true },
1875
+ } });
1876
+ try std.testing.expectEqual(@as(u32, 1), app_state.keydown_count);
1877
+
1878
+ // The commit delivers the composed text and ENDS the ownership: a
1879
+ // key_down arriving after the resolution is a genuine keystroke on
1880
+ // every host (hosts whose input method consumes the resolving key
1881
+ // suppress it at the source), so a candidate committed by MOUSE in
1882
+ // the OS popup — no trailing key at all — never costs the next
1883
+ // Enter, arrow, or Backspace.
1884
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1885
+ .window_id = 1,
1886
+ .label = "canvas",
1887
+ .kind = .ime_commit_composition,
1888
+ .text = "",
1889
+ } });
1890
+ try std.testing.expectEqual(@as(u32, 1), app_state.committed_count);
1891
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1892
+ .window_id = 1,
1893
+ .label = "canvas",
1894
+ .kind = .key_down,
1895
+ .key = "enter",
1896
+ } });
1897
+ try std.testing.expectEqual(@as(u32, 2), app_state.keydown_count);
1898
+
1899
+ // A cancel releases the keys the same way.
1900
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1901
+ .window_id = 1,
1902
+ .label = "canvas",
1903
+ .kind = .ime_set_composition,
1904
+ .text = "\xe3\x81\xad",
1905
+ } });
1906
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1907
+ .window_id = 1,
1908
+ .label = "canvas",
1909
+ .kind = .key_down,
1910
+ .key = "backspace",
1911
+ } });
1912
+ try std.testing.expectEqual(@as(u32, 2), app_state.keydown_count);
1913
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1914
+ .window_id = 1,
1915
+ .label = "canvas",
1916
+ .kind = .ime_cancel_composition,
1917
+ } });
1918
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1919
+ .window_id = 1,
1920
+ .label = "canvas",
1921
+ .kind = .key_down,
1922
+ .key = "escape",
1923
+ } });
1924
+ try std.testing.expectEqual(@as(u32, 3), app_state.keydown_count);
1925
+ }
1926
+
1927
+ test "an input-method-owned key never activates a freshly focused widget" {
1928
+ const TestApp = struct {
1929
+ activations: u32 = 0,
1930
+ committed_count: u32 = 0,
1931
+
1932
+ fn app(self: *@This()) App {
1933
+ return .{ .context = self, .name = "gpu-ime-widget-activation", .source = platform.WebViewSource.html("<h1>A</h1>"), .event_fn = event };
1934
+ }
1935
+
1936
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
1937
+ _ = runtime;
1938
+ const self: *@This() = @ptrCast(@alignCast(context));
1939
+ switch (event_value) {
1940
+ .canvas_widget_keyboard => |keyboard_event| {
1941
+ if (keyboard_event.target != null and keyboard_event.keyboard.phase == .key_down) {
1942
+ self.activations += 1;
1943
+ }
1944
+ if (keyboard_event.target == null and keyboard_event.keyboard.phase == .text_input) {
1945
+ self.committed_count += 1;
1946
+ }
1947
+ },
1948
+ else => {},
1949
+ }
1950
+ }
1951
+ };
1952
+
1953
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1954
+ defer harness.destroy(std.testing.allocator);
1955
+ harness.null_platform.gpu_surfaces = true;
1956
+ var app_state: TestApp = .{};
1957
+ const app = app_state.app();
1958
+ try harness.start(app);
1959
+
1960
+ _ = try harness.runtime.createView(.{
1961
+ .window_id = 1,
1962
+ .label = "canvas",
1963
+ .kind = .gpu_surface,
1964
+ .frame = geometry.RectF.init(0, 0, 240, 120),
1965
+ });
1966
+ harness.runtime.views[0].focused = true;
1967
+
1968
+ // The composition starts target-less (nothing focused)...
1969
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1970
+ .window_id = 1,
1971
+ .label = "canvas",
1972
+ .kind = .ime_set_composition,
1973
+ .text = "\xe3\x81\x8b",
1974
+ } });
1975
+ // ...then a button takes focus while it is still open. The
1976
+ // confirming Enter — surfaced BEFORE the commit on some hosts —
1977
+ // belongs to the input method: it must not press the button.
1978
+ const children = [_]canvas.Widget{
1979
+ .{ .id = 2, .kind = .button, .frame = geometry.RectF.init(10, 10, 96, 32), .text = "Run" },
1980
+ };
1981
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
1982
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
1983
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1984
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1985
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1986
+ .window_id = 1,
1987
+ .label = "canvas",
1988
+ .kind = .key_down,
1989
+ .key = "enter",
1990
+ } });
1991
+ try std.testing.expectEqual(@as(u32, 0), app_state.activations);
1992
+
1993
+ // The commit resolves target-less (the composition's owner), and
1994
+ // only a GENUINE Enter afterwards presses the button.
1995
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1996
+ .window_id = 1,
1997
+ .label = "canvas",
1998
+ .kind = .ime_commit_composition,
1999
+ .text = "",
2000
+ } });
2001
+ try std.testing.expectEqual(@as(u32, 1), app_state.committed_count);
2002
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2003
+ .window_id = 1,
2004
+ .label = "canvas",
2005
+ .kind = .key_down,
2006
+ .key = "enter",
2007
+ } });
2008
+ try std.testing.expectEqual(@as(u32, 1), app_state.activations);
2009
+ }
2010
+
2011
+ test "a duplicate cancel disarms the widget grace without re-arming it ownerless" {
2012
+ const TestApp = struct {
2013
+ committed_count: u32 = 0,
2014
+
2015
+ fn app(self: *@This()) App {
2016
+ return .{ .context = self, .name = "gpu-ime-duplicate-cancel", .source = platform.WebViewSource.html("<h1>D</h1>"), .event_fn = event };
2017
+ }
2018
+
2019
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
2020
+ _ = runtime;
2021
+ const self: *@This() = @ptrCast(@alignCast(context));
2022
+ switch (event_value) {
2023
+ .canvas_widget_keyboard => |keyboard_event| {
2024
+ if (keyboard_event.keyboard.phase != .text_input) return;
2025
+ if (keyboard_event.target != null) return;
2026
+ self.committed_count += 1;
2027
+ },
2028
+ else => {},
2029
+ }
2030
+ }
2031
+ };
2032
+
2033
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2034
+ defer harness.destroy(std.testing.allocator);
2035
+ harness.null_platform.gpu_surfaces = true;
2036
+ var app_state: TestApp = .{};
2037
+ const app = app_state.app();
2038
+ try harness.start(app);
2039
+
2040
+ _ = try harness.runtime.createView(.{
2041
+ .window_id = 1,
2042
+ .label = "canvas",
2043
+ .kind = .gpu_surface,
2044
+ .frame = geometry.RectF.init(0, 0, 240, 120),
2045
+ });
2046
+ const children = [_]canvas.Widget{
2047
+ .{ .id = 2, .kind = .text_field, .frame = geometry.RectF.init(10, 10, 160, 32), .text = "" },
2048
+ };
2049
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
2050
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
2051
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
2052
+ harness.runtime.views[0].focused = true;
2053
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
2054
+
2055
+ // A widget-owned composition cancels; hosts whose input method
2056
+ // consumed the cancelling key follow up with a SYNTHETIC duplicate
2057
+ // cancel (the grace's disarm stand-in for the suppressed key_down).
2058
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2059
+ .window_id = 1,
2060
+ .label = "canvas",
2061
+ .kind = .ime_set_composition,
2062
+ .text = "ka",
2063
+ .composition_cursor = 2,
2064
+ } });
2065
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2066
+ .window_id = 1,
2067
+ .label = "canvas",
2068
+ .kind = .ime_cancel_composition,
2069
+ } });
2070
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2071
+ .window_id = 1,
2072
+ .label = "canvas",
2073
+ .kind = .ime_cancel_composition,
2074
+ } });
2075
+
2076
+ // The duplicate DISARMED the grace and released the owner — it must
2077
+ // not re-arm ownerless, or this ordinary character would convert to
2078
+ // a dead-owner swallow. It lands in the focused editor.
2079
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2080
+ .window_id = 1,
2081
+ .label = "canvas",
2082
+ .kind = .text_input,
2083
+ .text = "x",
2084
+ } });
2085
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2086
+ try std.testing.expectEqualStrings("x", retained.findById(2).?.widget.text);
2087
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
2088
+ }
2089
+
2090
+ test "a target-less composition dies at its surface's blur - the next composition belongs to the focused editor" {
2091
+ const TestApp = struct {
2092
+ committed_count: u32 = 0,
2093
+
2094
+ fn app(self: *@This()) App {
2095
+ return .{ .context = self, .name = "gpu-ime-targetless-blur", .source = platform.WebViewSource.html("<h1>B</h1>"), .event_fn = event };
2096
+ }
2097
+
2098
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
2099
+ _ = runtime;
2100
+ const self: *@This() = @ptrCast(@alignCast(context));
2101
+ switch (event_value) {
2102
+ .canvas_widget_keyboard => |keyboard_event| {
2103
+ if (keyboard_event.keyboard.phase != .text_input) return;
2104
+ if (keyboard_event.target != null) return;
2105
+ self.committed_count += 1;
2106
+ },
2107
+ else => {},
2108
+ }
2109
+ }
2110
+ };
2111
+
2112
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2113
+ defer harness.destroy(std.testing.allocator);
2114
+ harness.null_platform.gpu_surfaces = true;
2115
+ var app_state: TestApp = .{};
2116
+ const app = app_state.app();
2117
+ try harness.start(app);
2118
+
2119
+ _ = try harness.runtime.createView(.{
2120
+ .window_id = 1,
2121
+ .label = "canvas-a",
2122
+ .kind = .gpu_surface,
2123
+ .frame = geometry.RectF.init(0, 0, 240, 120),
2124
+ });
2125
+ _ = try harness.runtime.createView(.{
2126
+ .window_id = 1,
2127
+ .label = "canvas-b",
2128
+ .kind = .gpu_surface,
2129
+ .frame = geometry.RectF.init(0, 120, 240, 120),
2130
+ });
2131
+ try harness.runtime.focusView(1, "canvas-a");
2132
+
2133
+ // A target-less composition starts (no editor focused), then focus
2134
+ // leaves the surface WITHOUT a host cancellation and returns.
2135
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2136
+ .window_id = 1,
2137
+ .label = "canvas-a",
2138
+ .kind = .ime_set_composition,
2139
+ .text = "\xe3\x81\x8b",
2140
+ } });
2141
+ try harness.runtime.focusView(1, "canvas-b");
2142
+ try harness.runtime.focusView(1, "canvas-a");
2143
+
2144
+ // An editor takes focus and a NEW composition begins: it belongs to
2145
+ // the editor — a stale target-less preedit surviving the blur would
2146
+ // claim it as a continuation and steal its commit.
2147
+ const children = [_]canvas.Widget{
2148
+ .{ .id = 2, .kind = .text_field, .frame = geometry.RectF.init(10, 10, 160, 32), .text = "" },
2149
+ };
2150
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
2151
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
2152
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas-a", layout);
2153
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
2154
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2155
+ .window_id = 1,
2156
+ .label = "canvas-a",
2157
+ .kind = .ime_set_composition,
2158
+ .text = "ne",
2159
+ .composition_cursor = 2,
2160
+ } });
2161
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas-a");
2162
+ try std.testing.expectEqualStrings("ne", retained.findById(2).?.widget.text);
2163
+ try std.testing.expect(retained.findById(2).?.widget.text_composition != null);
2164
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2165
+ .window_id = 1,
2166
+ .label = "canvas-a",
2167
+ .kind = .ime_commit_composition,
2168
+ .text = "ne",
2169
+ } });
2170
+ try std.testing.expectEqualStrings("ne", (try harness.runtime.canvasWidgetLayout(1, "canvas-a")).findById(2).?.widget.text);
2171
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
2172
+ }
2173
+
2174
+ test "a cancel grace dies with its view - the trailing text reaches the new surface's editor" {
2175
+ const TestApp = struct {
2176
+ committed_count: u32 = 0,
2177
+
2178
+ fn app(self: *@This()) App {
2179
+ return .{ .context = self, .name = "gpu-ime-grace-recreate", .source = platform.WebViewSource.html("<h1>D</h1>"), .event_fn = event };
2180
+ }
2181
+
2182
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
2183
+ _ = runtime;
2184
+ const self: *@This() = @ptrCast(@alignCast(context));
2185
+ switch (event_value) {
2186
+ .canvas_widget_keyboard => |keyboard_event| {
2187
+ if (keyboard_event.keyboard.phase != .text_input) return;
2188
+ if (keyboard_event.target != null) return;
2189
+ self.committed_count += 1;
2190
+ },
2191
+ else => {},
2192
+ }
2193
+ }
2194
+ };
2195
+
2196
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2197
+ defer harness.destroy(std.testing.allocator);
2198
+ harness.null_platform.gpu_surfaces = true;
2199
+ var app_state: TestApp = .{};
2200
+ const app = app_state.app();
2201
+ try harness.start(app);
2202
+
2203
+ // A target-less composition CANCELS (arming the converted-commit
2204
+ // grace, its preedit already zeroed), and the cancel's handling
2205
+ // closes the surface and recreates the label with a focused editor.
2206
+ _ = try harness.runtime.createView(.{
2207
+ .window_id = 1,
2208
+ .label = "canvas",
2209
+ .kind = .gpu_surface,
2210
+ .frame = geometry.RectF.init(0, 0, 240, 120),
2211
+ });
2212
+ harness.runtime.views[0].focused = true;
2213
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2214
+ .window_id = 1,
2215
+ .label = "canvas",
2216
+ .kind = .ime_set_composition,
2217
+ .text = "ka",
2218
+ } });
2219
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2220
+ .window_id = 1,
2221
+ .label = "canvas",
2222
+ .kind = .ime_cancel_composition,
2223
+ } });
2224
+ try harness.runtime.closeView(1, "canvas");
2225
+ _ = try harness.runtime.createView(.{
2226
+ .window_id = 1,
2227
+ .label = "canvas",
2228
+ .kind = .gpu_surface,
2229
+ .frame = geometry.RectF.init(0, 0, 240, 120),
2230
+ });
2231
+ const children = [_]canvas.Widget{
2232
+ .{ .id = 2, .kind = .text_field, .frame = geometry.RectF.init(10, 10, 160, 32), .text = "" },
2233
+ };
2234
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
2235
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
2236
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
2237
+ harness.runtime.views[0].focused = true;
2238
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
2239
+
2240
+ // The grace died with the old view: this text belongs to the NEW
2241
+ // surface's focused editor, never leaked target-less.
2242
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2243
+ .window_id = 1,
2244
+ .label = "canvas",
2245
+ .kind = .text_input,
2246
+ .text = "KA",
2247
+ } });
2248
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2249
+ try std.testing.expectEqualStrings("KA", retained.findById(2).?.widget.text);
2250
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
2251
+ }
2252
+
2253
+ test "an orphaned composition is swallowed, never rerouted to another editor" {
2254
+ const TestApp = struct {
2255
+ committed_count: u32 = 0,
2256
+
2257
+ fn app(self: *@This()) App {
2258
+ return .{ .context = self, .name = "gpu-ime-orphan", .source = platform.WebViewSource.html("<h1>X</h1>"), .event_fn = event };
2259
+ }
2260
+
2261
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
2262
+ _ = runtime;
2263
+ const self: *@This() = @ptrCast(@alignCast(context));
2264
+ switch (event_value) {
2265
+ .canvas_widget_keyboard => |keyboard_event| {
2266
+ if (keyboard_event.keyboard.phase != .text_input) return;
2267
+ if (keyboard_event.target != null) return;
2268
+ self.committed_count += 1;
2269
+ },
2270
+ else => {},
2271
+ }
2272
+ }
2273
+ };
2274
+
2275
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2276
+ defer harness.destroy(std.testing.allocator);
2277
+ harness.null_platform.gpu_surfaces = true;
2278
+ var app_state: TestApp = .{};
2279
+ const app = app_state.app();
2280
+ try harness.start(app);
2281
+
2282
+ _ = try harness.runtime.createView(.{
2283
+ .window_id = 1,
2284
+ .label = "canvas",
2285
+ .kind = .gpu_surface,
2286
+ .frame = geometry.RectF.init(0, 0, 240, 120),
2287
+ });
2288
+ const two_fields = [_]canvas.Widget{
2289
+ .{ .id = 2, .kind = .text_field, .frame = geometry.RectF.init(10, 10, 160, 32), .text = "" },
2290
+ .{ .id = 3, .kind = .text_field, .frame = geometry.RectF.init(10, 52, 160, 32), .text = "" },
2291
+ };
2292
+ var nodes: [4]canvas.WidgetLayoutNode = undefined;
2293
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &two_fields }, geometry.RectF.init(0, 0, 240, 120), &nodes);
2294
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
2295
+ harness.runtime.views[0].focused = true;
2296
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
2297
+
2298
+ // Field A composes; a rebuild removes it and focus lands on B.
2299
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2300
+ .window_id = 1,
2301
+ .label = "canvas",
2302
+ .kind = .ime_set_composition,
2303
+ .text = "ka",
2304
+ .composition_cursor = 2,
2305
+ } });
2306
+ const only_b = [_]canvas.Widget{
2307
+ .{ .id = 3, .kind = .text_field, .frame = geometry.RectF.init(10, 52, 160, 32), .text = "" },
2308
+ };
2309
+ var nodes_b: [3]canvas.WidgetLayoutNode = undefined;
2310
+ const layout_b = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &only_b }, geometry.RectF.init(0, 0, 240, 120), &nodes_b);
2311
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout_b);
2312
+ harness.runtime.views[0].canvas_widget_focused_id = 3;
2313
+
2314
+ // A PREEDIT UPDATE of the orphaned sequence is swallowed too — the
2315
+ // sequence is still open and still belongs to the vanished editor,
2316
+ // so field B must not inherit its updates.
2317
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2318
+ .window_id = 1,
2319
+ .label = "canvas",
2320
+ .kind = .ime_set_composition,
2321
+ .text = "kaa",
2322
+ .composition_cursor = 3,
2323
+ } });
2324
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2325
+ try std.testing.expectEqualStrings("", retained.findById(3).?.widget.text);
2326
+
2327
+ // The orphaned sequence's commit resolves NOWHERE: field B never saw
2328
+ // the composition, and the target-less fallback never composed it —
2329
+ // swallowed, both by contract, and the close releases the pin.
2330
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2331
+ .window_id = 1,
2332
+ .label = "canvas",
2333
+ .kind = .ime_commit_composition,
2334
+ .text = "ka",
2335
+ } });
2336
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2337
+ try std.testing.expectEqualStrings("", retained.findById(3).?.widget.text);
2338
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
2339
+
2340
+ // The stale pin cleared: a FRESH composition opens in field B.
2341
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2342
+ .window_id = 1,
2343
+ .label = "canvas",
2344
+ .kind = .ime_set_composition,
2345
+ .text = "ne",
2346
+ .composition_cursor = 2,
2347
+ } });
2348
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2349
+ try std.testing.expectEqualStrings("ne", retained.findById(3).?.widget.text);
2350
+ try std.testing.expect(retained.findById(3).?.widget.text_composition != null);
2351
+ }
2352
+
2353
+ test "a target-less composition stays target-less when a text field takes focus mid-sequence" {
2354
+ const TestApp = struct {
2355
+ committed_count: u32 = 0,
2356
+ last_committed: [16]u8 = undefined,
2357
+ last_committed_len: usize = 0,
2358
+
2359
+ fn app(self: *@This()) App {
2360
+ return .{ .context = self, .name = "gpu-targetless-ime-ownership", .source = platform.WebViewSource.html("<h1>O</h1>"), .event_fn = event };
2361
+ }
2362
+
2363
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
2364
+ _ = runtime;
2365
+ const self: *@This() = @ptrCast(@alignCast(context));
2366
+ switch (event_value) {
2367
+ .canvas_widget_keyboard => |keyboard_event| {
2368
+ if (keyboard_event.keyboard.phase != .text_input) return;
2369
+ if (keyboard_event.target != null) return;
2370
+ self.committed_count += 1;
2371
+ const text = keyboard_event.keyboard.text;
2372
+ const len = @min(text.len, self.last_committed.len);
2373
+ @memcpy(self.last_committed[0..len], text[0..len]);
2374
+ self.last_committed_len = len;
2375
+ },
2376
+ else => {},
2377
+ }
2378
+ }
2379
+ };
2380
+
2381
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2382
+ defer harness.destroy(std.testing.allocator);
2383
+ harness.null_platform.gpu_surfaces = true;
2384
+ var app_state: TestApp = .{};
2385
+ const app = app_state.app();
2386
+ try harness.start(app);
2387
+
2388
+ _ = try harness.runtime.createView(.{
2389
+ .window_id = 1,
2390
+ .label = "canvas",
2391
+ .kind = .gpu_surface,
2392
+ .frame = geometry.RectF.init(0, 0, 240, 120),
2393
+ });
2394
+ const children = [_]canvas.Widget{
2395
+ .{
2396
+ .id = 2,
2397
+ .kind = .text_field,
2398
+ .frame = geometry.RectF.init(10, 10, 160, 32),
2399
+ .text = "",
2400
+ },
2401
+ };
2402
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
2403
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
2404
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
2405
+ harness.runtime.views[0].focused = true;
2406
+
2407
+ // The composition STARTS target-less (nothing focused): the preedit
2408
+ // buffers for the surface's own consumer.
2409
+ harness.runtime.views[0].canvas_widget_focused_id = 0;
2410
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2411
+ .window_id = 1,
2412
+ .label = "canvas",
2413
+ .kind = .ime_set_composition,
2414
+ .text = "\xe3\x81\x8b", // か
2415
+ } });
2416
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
2417
+
2418
+ // Focus lands on a text field mid-sequence. The empty commit
2419
+ // resolves the composition it STARTED — the target-less one — not
2420
+ // the newly focused editor: the composed bytes reach the
2421
+ // target-less consumer and the field's text is untouched.
2422
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
2423
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2424
+ .window_id = 1,
2425
+ .label = "canvas",
2426
+ .kind = .ime_commit_composition,
2427
+ .text = "",
2428
+ } });
2429
+ try std.testing.expectEqual(@as(u32, 1), app_state.committed_count);
2430
+ try std.testing.expectEqualStrings("\xe3\x81\x8b", app_state.last_committed[0..app_state.last_committed_len]);
2431
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2432
+ try std.testing.expectEqualStrings("", retained.findById(2).?.widget.text);
2433
+
2434
+ // With the sequence resolved, a fresh composition belongs to the
2435
+ // focused editor as usual.
2436
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2437
+ .window_id = 1,
2438
+ .label = "canvas",
2439
+ .kind = .ime_set_composition,
2440
+ .text = "ne",
2441
+ } });
2442
+ const composing = try harness.runtime.canvasWidgetLayout(1, "canvas");
2443
+ try std.testing.expectEqualStrings("ne", composing.findById(2).?.widget.text);
2444
+ try std.testing.expectEqual(@as(u32, 1), app_state.committed_count);
2445
+ }
2446
+
2447
+ test "a focused widget's claimed key never doubles into target-less committed text" {
2448
+ const TestApp = struct {
2449
+ committed_count: u32 = 0,
2450
+ last_committed: [16]u8 = undefined,
2451
+ last_committed_len: usize = 0,
2452
+
2453
+ fn app(self: *@This()) App {
2454
+ return .{ .context = self, .name = "gpu-targetless-precedence", .source = platform.WebViewSource.html("<h1>P</h1>"), .event_fn = event };
2455
+ }
2456
+
2457
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
2458
+ _ = runtime;
2459
+ const self: *@This() = @ptrCast(@alignCast(context));
2460
+ switch (event_value) {
2461
+ .canvas_widget_keyboard => |keyboard_event| {
2462
+ if (keyboard_event.keyboard.phase != .text_input) return;
2463
+ if (keyboard_event.target != null) return;
2464
+ self.committed_count += 1;
2465
+ const text = keyboard_event.keyboard.text;
2466
+ const len = @min(text.len, self.last_committed.len);
2467
+ @memcpy(self.last_committed[0..len], text[0..len]);
2468
+ self.last_committed_len = len;
2469
+ },
2470
+ else => {},
2471
+ }
2472
+ }
2473
+ };
2474
+
2475
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2476
+ defer harness.destroy(std.testing.allocator);
2477
+ harness.null_platform.gpu_surfaces = true;
2478
+ var app_state: TestApp = .{};
2479
+ const app = app_state.app();
2480
+ try harness.start(app);
2481
+
2482
+ _ = try harness.runtime.createView(.{
2483
+ .window_id = 1,
2484
+ .label = "canvas",
2485
+ .kind = .gpu_surface,
2486
+ .frame = geometry.RectF.init(0, 0, 240, 120),
2487
+ });
2488
+ const children = [_]canvas.Widget{
2489
+ .{
2490
+ .id = 2,
2491
+ .kind = .button,
2492
+ .frame = geometry.RectF.init(10, 10, 96, 32),
2493
+ .text = "Run",
2494
+ },
2495
+ };
2496
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
2497
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
2498
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
2499
+ harness.runtime.views[0].focused = true;
2500
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
2501
+
2502
+ // Space activates the focused button (widget precedence, step 2):
2503
+ // the SAME keystroke must not also type a literal space through the
2504
+ // target-less committed fallback — on either committed-text carrier.
2505
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2506
+ .window_id = 1,
2507
+ .label = "canvas",
2508
+ .kind = .key_down,
2509
+ .key = "space",
2510
+ .text = " ",
2511
+ } });
2512
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
2513
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2514
+ .window_id = 1,
2515
+ .label = "canvas",
2516
+ .kind = .text_input,
2517
+ .text = " ",
2518
+ } });
2519
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
2520
+
2521
+ // A key the button does NOT claim still flows — typing into a
2522
+ // terminal while a button happens to hold focus.
2523
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2524
+ .window_id = 1,
2525
+ .label = "canvas",
2526
+ .kind = .key_down,
2527
+ .key = "j",
2528
+ .text = "j",
2529
+ } });
2530
+ try std.testing.expectEqual(@as(u32, 1), app_state.committed_count);
2531
+ try std.testing.expectEqualStrings("j", app_state.last_committed[0..app_state.last_committed_len]);
2532
+
2533
+ // With focus off the button, space typing flows again.
2534
+ harness.runtime.views[0].canvas_widget_focused_id = 0;
2535
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2536
+ .window_id = 1,
2537
+ .label = "canvas",
2538
+ .kind = .key_down,
2539
+ .key = "space",
2540
+ .text = " ",
2541
+ } });
2542
+ try std.testing.expectEqual(@as(u32, 2), app_state.committed_count);
2543
+ try std.testing.expectEqualStrings(" ", app_state.last_committed[0..app_state.last_committed_len]);
2544
+ }
2545
+
2546
+ test "target-less IME preedit is per-surface and command chords never commit text" {
2547
+ const TestApp = struct {
2548
+ committed_count: u32 = 0,
2549
+ last_committed: [64]u8 = undefined,
2550
+ last_committed_len: usize = 0,
2551
+ last_committed_label: []const u8 = "",
2552
+
2553
+ fn app(self: *@This()) App {
2554
+ return .{ .context = self, .name = "gpu-targetless-ime", .source = platform.WebViewSource.html("<h1>IME</h1>"), .event_fn = event };
2555
+ }
2556
+
2557
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
2558
+ _ = runtime;
2559
+ const self: *@This() = @ptrCast(@alignCast(context));
2560
+ switch (event_value) {
2561
+ .canvas_widget_keyboard => |keyboard_event| {
2562
+ // Only the target-less committed synthesis: phase
2563
+ // text_input with no focused-widget target.
2564
+ if (keyboard_event.keyboard.phase != .text_input) return;
2565
+ if (keyboard_event.target != null) return;
2566
+ self.committed_count += 1;
2567
+ const text = keyboard_event.keyboard.text;
2568
+ const len = @min(text.len, self.last_committed.len);
2569
+ @memcpy(self.last_committed[0..len], text[0..len]);
2570
+ self.last_committed_len = len;
2571
+ self.last_committed_label = keyboard_event.view_label;
2572
+ },
2573
+ else => {},
2574
+ }
2575
+ }
2576
+ };
2577
+
2578
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2579
+ defer harness.destroy(std.testing.allocator);
2580
+ harness.null_platform.gpu_surfaces = true;
2581
+ var app_state: TestApp = .{};
2582
+ const app = app_state.app();
2583
+ try harness.start(app);
2584
+
2585
+ // TWO focused surfaces, no widgets anywhere: every text event takes
2586
+ // the target-less path.
2587
+ _ = try harness.runtime.createView(.{
2588
+ .window_id = 1,
2589
+ .label = "canvas-a",
2590
+ .kind = .gpu_surface,
2591
+ .frame = geometry.RectF.init(0, 0, 240, 120),
2592
+ });
2593
+ _ = try harness.runtime.createView(.{
2594
+ .window_id = 1,
2595
+ .label = "canvas-b",
2596
+ .kind = .gpu_surface,
2597
+ .frame = geometry.RectF.init(0, 120, 240, 120),
2598
+ });
2599
+ harness.runtime.views[0].focused = true;
2600
+ harness.runtime.views[1].focused = true;
2601
+
2602
+ // Surface A composes; the preedit is provisional — nothing commits.
2603
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2604
+ .window_id = 1,
2605
+ .label = "canvas-a",
2606
+ .kind = .ime_set_composition,
2607
+ .text = "\xe3\x81\x8b", // か
2608
+ } });
2609
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
2610
+
2611
+ // THE per-surface pin: surface B's empty commit must NOT insert
2612
+ // surface A's buffered composition — B owns no preedit.
2613
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2614
+ .window_id = 1,
2615
+ .label = "canvas-b",
2616
+ .kind = .ime_commit_composition,
2617
+ .text = "",
2618
+ } });
2619
+ try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
2620
+
2621
+ // A's own empty commit still resolves ITS composition — B's stray
2622
+ // commit neither consumed nor cleared it.
2623
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2624
+ .window_id = 1,
2625
+ .label = "canvas-a",
2626
+ .kind = .ime_commit_composition,
2627
+ .text = "",
2628
+ } });
2629
+ try std.testing.expectEqual(@as(u32, 1), app_state.committed_count);
2630
+ try std.testing.expectEqualStrings("\xe3\x81\x8b", app_state.last_committed[0..app_state.last_committed_len]);
2631
+ try std.testing.expectEqualStrings("canvas-a", app_state.last_committed_label);
2632
+
2633
+ // THE chord pin: a command-modified text event is a shortcut, not
2634
+ // typing — the same gate focused text widgets apply — so Ctrl+C
2635
+ // commits nothing on the target-less path either.
2636
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2637
+ .window_id = 1,
2638
+ .label = "canvas-a",
2639
+ .kind = .text_input,
2640
+ .key = "c",
2641
+ .text = "c",
2642
+ .modifiers = .{ .control = true },
2643
+ } });
2644
+ try std.testing.expectEqual(@as(u32, 1), app_state.committed_count);
2645
+
2646
+ // Unmodified typing still commits.
2647
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2648
+ .window_id = 1,
2649
+ .label = "canvas-a",
2650
+ .kind = .text_input,
2651
+ .text = "x",
2652
+ } });
2653
+ try std.testing.expectEqual(@as(u32, 2), app_state.committed_count);
2654
+ try std.testing.expectEqualStrings("x", app_state.last_committed[0..app_state.last_committed_len]);
2655
+
2656
+ // A key_down CARRYING text is the other committed-text shape (hosts
2657
+ // whose plain typing rides the key event, no separate text event):
2658
+ // it must reach the target-less consumer too.
2659
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2660
+ .window_id = 1,
2661
+ .label = "canvas-a",
2662
+ .kind = .key_down,
2663
+ .key = "j",
2664
+ .text = "j",
2665
+ } });
2666
+ try std.testing.expectEqual(@as(u32, 3), app_state.committed_count);
2667
+ try std.testing.expectEqualStrings("j", app_state.last_committed[0..app_state.last_committed_len]);
2668
+
2669
+ // The same chord gate applies on the key_down carrier: a chorded
2670
+ // key never types its literal character.
2671
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2672
+ .window_id = 1,
2673
+ .label = "canvas-a",
2674
+ .kind = .key_down,
2675
+ .key = "j",
2676
+ .text = "j",
2677
+ .modifiers = .{ .control = true },
2678
+ } });
2679
+ try std.testing.expectEqual(@as(u32, 3), app_state.committed_count);
2680
+
2681
+ // A key_down WITHOUT text (a special, or an IM-consumed key echoed
2682
+ // for activation) commits nothing.
2683
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2684
+ .window_id = 1,
2685
+ .label = "canvas-a",
2686
+ .kind = .key_down,
2687
+ .key = "enter",
2688
+ } });
2689
+ try std.testing.expectEqual(@as(u32, 3), app_state.committed_count);
2690
+ }
2691
+
869
2692
  test "runtime dispatches opted-in canvas widget drag events" {
870
2693
  const TestApp = struct {
871
2694
  raw_input_count: u32 = 0,