@native-sdk/cli 0.11.0 → 0.11.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 (182) hide show
  1. package/build/app.zig +73 -37
  2. package/build.zig +239 -5
  3. package/native-sdk.d.ts +5 -1
  4. package/package.json +13 -12
  5. package/packages/core/compile-surface/core.ts +30 -0
  6. package/packages/core/package-lock.json +6 -6
  7. package/packages/core/package.json +3 -3
  8. package/packages/core/scripts/scriptc-toolchain.json +44 -44
  9. package/packages/core/scripts/stage_external_core.mjs +27 -16
  10. package/packages/core/sdk/core.d.ts +24 -1
  11. package/packages/core/sdk/core.ts +51 -1
  12. package/packages/core/sdk/events.d.ts +5 -0
  13. package/packages/core/sdk/events.ts +10 -0
  14. package/packages/core/src/bridge_descriptors.ts +3 -3
  15. package/packages/core/src/checker.ts +20 -0
  16. package/packages/core/src/cli.ts +2 -11
  17. package/packages/core/src/context_menus.ts +76 -0
  18. package/packages/core/src/diagnostics.ts +7 -0
  19. package/packages/core/src/extent_program.ts +53 -0
  20. package/packages/core/src/frontend.ts +12 -10
  21. package/packages/core/src/markup_canonical.ts +140 -0
  22. package/packages/core/src/markup_construction.ts +189 -0
  23. package/packages/core/src/markup_display.ts +43 -0
  24. package/packages/core/src/markup_document.ts +226 -0
  25. package/packages/core/src/markup_elements.ts +225 -0
  26. package/packages/core/src/markup_expressions.ts +79 -0
  27. package/packages/core/src/markup_imports.ts +146 -0
  28. package/packages/core/src/markup_parser.ts +228 -0
  29. package/packages/core/src/markup_runtime.ts +93 -0
  30. package/packages/core/src/markup_scalars.ts +143 -0
  31. package/packages/core/src/markup_shapes.ts +243 -0
  32. package/packages/core/src/markup_specialized.ts +242 -0
  33. package/packages/core/src/markup_text.ts +86 -0
  34. package/packages/core/src/markup_values.ts +336 -0
  35. package/packages/core/src/portable_sources.json +24 -0
  36. package/packages/core/src/registered_font.ts +2 -3
  37. package/packages/core/src/runtime_policy.ts +24 -0
  38. package/packages/core/src/scalar_text.ts +1 -3
  39. package/packages/core/src/scroll_mutation.ts +86 -0
  40. package/packages/core/src/scroll_tree.ts +86 -0
  41. package/packages/core/src/service_contract.ts +53 -2
  42. package/packages/core/src/sqlite_cli.ts +3 -46
  43. package/packages/core/src/sqlite_codegen.ts +84 -0
  44. package/packages/core/src/staged_rebuild.ts +60 -0
  45. package/packages/core/src/view_bridge.ts +161 -0
  46. package/packages/core/src/view_check.ts +145 -0
  47. package/packages/core/src/view_evaluator.ts +823 -0
  48. package/packages/core/src/view_frontend.ts +340 -218
  49. package/packages/core/src/view_program.ts +221 -0
  50. package/packages/core/src/view_reload.ts +166 -0
  51. package/packages/core/src/view_reload_watch.ts +98 -0
  52. package/packages/core/src/view_sources.ts +34 -0
  53. package/packages/core/src/virtual_windows.ts +88 -0
  54. package/packages/core/src/web_panes.ts +67 -0
  55. package/packages/core/src/widget_continuity.ts +37 -0
  56. package/packages/core/src/widget_reconcile.ts +63 -0
  57. package/packages/core/src/widget_retention.ts +62 -0
  58. package/skill-data/native-ui/SKILL.md +2 -2
  59. package/skill-data/ts-core/SKILL.md +5 -3
  60. package/skill-data/ts-services/references/service-surface.md +29 -5
  61. package/src/app_runner/root.zig +14 -0
  62. package/src/app_runner/ts_compiled_view.zig +88 -17
  63. package/src/app_runner/ts_core_main.zig +43 -5
  64. package/src/app_runner/ts_core_mobile.zig +1 -1
  65. package/src/automation/server.zig +56 -3
  66. package/src/embed/host.zig +2 -0
  67. package/src/embed/tests.zig +2 -0
  68. package/src/embed/ui_host.zig +2 -0
  69. package/src/platform/keyboard_input.zig +32 -0
  70. package/src/platform/linux/root.zig +2 -0
  71. package/src/platform/macos/appkit_host.h +7 -2
  72. package/src/platform/macos/appkit_host.m +31 -9
  73. package/src/platform/macos/cef_host.mm +15 -1
  74. package/src/platform/macos/global_shortcuts.zig +138 -0
  75. package/src/platform/macos/key_codes.zig +102 -0
  76. package/src/platform/macos/keyboard_input.zig +81 -0
  77. package/src/platform/macos/root.zig +25 -3
  78. package/src/platform/macos/webview_inspection_test.m +106 -0
  79. package/src/platform/null_platform.zig +104 -0
  80. package/src/platform/root.zig +14 -0
  81. package/src/platform/types.zig +102 -0
  82. package/src/platform/windows/global_shortcuts.zig +157 -0
  83. package/src/platform/windows/key_codes.zig +63 -0
  84. package/src/platform/windows/keyboard_input.zig +136 -0
  85. package/src/platform/windows/root.zig +15 -0
  86. package/src/primitives/canvas/decision_reuse.zig +133 -0
  87. package/src/primitives/canvas/font_ttf.zig +27 -3
  88. package/src/primitives/canvas/fonts/Geist-Bold.ttf +0 -0
  89. package/src/primitives/canvas/fonts/Geist-Medium.ttf +0 -0
  90. package/src/primitives/canvas/fonts/OFL.txt +93 -0
  91. package/src/primitives/canvas/fonts/README.md +15 -0
  92. package/src/primitives/canvas/intrinsic_measure_policy.zig +4 -1
  93. package/src/primitives/canvas/markdown_tests.zig +3 -3
  94. package/src/primitives/canvas/reference.zig +3 -5
  95. package/src/primitives/canvas/reference_tests.zig +58 -0
  96. package/src/primitives/canvas/registered_font_policy.zig +1 -1
  97. package/src/primitives/canvas/root.zig +7 -4
  98. package/src/primitives/canvas/text_metrics.zig +32 -39
  99. package/src/primitives/canvas/text_metrics_tests.zig +52 -7
  100. package/src/primitives/canvas/text_query_policy.zig +142 -3
  101. package/src/primitives/canvas/text_scalar_policy.zig +80 -2
  102. package/src/primitives/canvas/tokens.zig +9 -0
  103. package/src/primitives/canvas/ui.zig +1 -0
  104. package/src/primitives/canvas/ui_markup.zig +69 -14
  105. package/src/primitives/canvas/ui_markup_canonical.zig +237 -0
  106. package/src/primitives/canvas/ui_markup_compiled.zig +1407 -23
  107. package/src/primitives/canvas/ui_markup_compiled_values.zig +76 -0
  108. package/src/primitives/canvas/ui_markup_construction.zig +146 -0
  109. package/src/primitives/canvas/ui_markup_display.zig +82 -0
  110. package/src/primitives/canvas/ui_markup_document.zig +196 -0
  111. package/src/primitives/canvas/ui_markup_elements.zig +163 -0
  112. package/src/primitives/canvas/ui_markup_expr.zig +192 -2
  113. package/src/primitives/canvas/ui_markup_imports.zig +262 -0
  114. package/src/primitives/canvas/ui_markup_parser.zig +117 -0
  115. package/src/primitives/canvas/ui_markup_runtime.zig +76 -0
  116. package/src/primitives/canvas/ui_markup_scalars.zig +115 -0
  117. package/src/primitives/canvas/ui_markup_shapes.zig +224 -0
  118. package/src/primitives/canvas/ui_markup_specialized.zig +106 -0
  119. package/src/primitives/canvas/ui_markup_text.zig +128 -0
  120. package/src/primitives/canvas/ui_markup_values.zig +368 -0
  121. package/src/primitives/canvas/ui_markup_view.zig +2817 -74
  122. package/src/primitives/canvas/virtual_extent_policy.zig +58 -0
  123. package/src/primitives/canvas/virtual_extents.zig +47 -33
  124. package/src/primitives/canvas/widget_metric_policy.zig +4 -1
  125. package/src/primitives/canvas/widget_runtime.zig +2 -1
  126. package/src/root.zig +5 -0
  127. package/src/runtime/api.zig +1 -0
  128. package/src/runtime/bridge_descriptor_policy.zig +1 -1
  129. package/src/runtime/bridge_payload.zig +2 -0
  130. package/src/runtime/canvas_widget_context_menu.zig +179 -0
  131. package/src/runtime/canvas_widget_context_menu_test_cases.zig +1147 -0
  132. package/src/runtime/canvas_widget_context_menu_tests.zig +23 -1098
  133. package/src/runtime/canvas_widget_runtime.zig +306 -92
  134. package/src/runtime/context_menu_policy.zig +112 -0
  135. package/src/runtime/core.zig +14 -0
  136. package/src/runtime/effects.zig +172 -4
  137. package/src/runtime/effects_host_tests.zig +25 -0
  138. package/src/runtime/flow.zig +1 -0
  139. package/src/runtime/flow_reference.zig +1 -0
  140. package/src/runtime/global_shortcuts.zig +75 -0
  141. package/src/runtime/keyboard_input.zig +86 -0
  142. package/src/runtime/root.zig +3 -0
  143. package/src/runtime/scroll_mutation_policy.zig +130 -0
  144. package/src/runtime/scroll_mutation_runtime.zig +158 -0
  145. package/src/runtime/scroll_tree_policy.zig +99 -0
  146. package/src/runtime/session_record.zig +7 -0
  147. package/src/runtime/staged_rebuild_policy.zig +83 -0
  148. package/src/runtime/system_services.zig +19 -0
  149. package/src/runtime/tests.zig +1 -0
  150. package/src/runtime/ts_core_host.zig +126 -0
  151. package/src/runtime/ts_ui_app.zig +24 -0
  152. package/src/runtime/ui_app.zig +274 -31
  153. package/src/runtime/ui_app_tests.zig +48 -179
  154. package/src/runtime/ui_app_window_tests.zig +133 -0
  155. package/src/runtime/view_reload.zig +226 -0
  156. package/src/runtime/view_widget_scroll.zig +30 -21
  157. package/src/runtime/view_widget_tree.zig +43 -13
  158. package/src/runtime/virtual_window_policy.zig +128 -0
  159. package/src/runtime/web_pane_policy.zig +111 -0
  160. package/src/runtime/web_pane_test_cases.zig +204 -0
  161. package/src/runtime/widget_continuity_policy.zig +73 -0
  162. package/src/runtime/widget_reconcile_policy.zig +81 -0
  163. package/src/runtime/widget_retention_policy.zig +75 -0
  164. package/src/runtime/window_views.zig +19 -4
  165. package/src/security/root.zig +1 -0
  166. package/src/tooling/templates.zig +8 -0
  167. package/src/tooling/ts_core.zig +84 -1
  168. package/src/tooling/verbs.zig +79 -15
  169. package/tools/corewire/core.test.ts +25 -3
  170. package/tools/corewire/core_abi.zig +4 -0
  171. package/tools/corewire/core_contract.ts +1 -1
  172. package/tools/corewire/core_goldens.json +75 -75
  173. package/tools/corewire/core_projection.ts +3 -2
  174. package/tools/corewire/emit_mirror.ts +19 -9
  175. package/tools/corewire/facade_templates.ts +1 -1
  176. package/tools/corewire/intake.test.ts +4 -0
  177. package/tools/corewire/intake_goldens.json +50 -50
  178. package/tools/corewire/invocation.test.ts +4 -0
  179. package/tools/corewire/invocation_goldens.json +3 -3
  180. package/tools/corewire/mirror_templates.ts +4 -0
  181. package/tools/corewire/profile_emission.ts +2 -0
  182. package/tools/corewire/sidecar.zig +2 -0
@@ -0,0 +1,1147 @@
1
+ //! Native context menu tests: a secondary-button press asks the
2
+ //! platform to present the OS menu (recorded by the null platform), the
3
+ //! platform's `context_menu_action` event resolves to a
4
+ //! `.canvas_widget_context_menu` runtime event for app-declared menus,
5
+ //! and the zero-code defaults (editable-text Cut/Copy/Paste/Select All,
6
+ //! terminal Copy/Paste, static-selection Copy) drive the existing
7
+ //! clipboard and committed-input actions.
8
+
9
+ const support = @import("test_support.zig");
10
+ const std = support.std;
11
+ const geometry = support.geometry;
12
+ const canvas = support.canvas;
13
+ const platform = support.platform;
14
+ const automation = support.automation;
15
+ const App = support.App;
16
+ const Runtime = support.Runtime;
17
+ const Event = support.Event;
18
+ const TestHarness = support.TestHarness;
19
+ const canvas_limits = @import("canvas_limits.zig");
20
+
21
+ pub fn Cases(comptime policy: ?*const fn ([]const u8, []u8) usize) type {
22
+ return struct {
23
+ const MenuTestApp = struct {
24
+ pointer_count: u32 = 0,
25
+ raw_input_count: u32 = 0,
26
+ menu_count: u32 = 0,
27
+ last_menu_target: canvas.ObjectId = 0,
28
+ last_menu_item_index: usize = 0,
29
+ last_menu_label: [32]u8 = undefined,
30
+ last_menu_label_len: usize = 0,
31
+ shown_count: u32 = 0,
32
+ last_shown_token: u64 = 0,
33
+ last_shown_label: [32]u8 = undefined,
34
+ last_shown_label_len: usize = 0,
35
+ request_count: u32 = 0,
36
+ last_request_target: canvas.ObjectId = 0,
37
+ last_request_point: geometry.PointF = .{},
38
+ last_edit_insert: [64]u8 = undefined,
39
+ last_edit_insert_len: usize = 0,
40
+ keyboard_count: u32 = 0,
41
+ last_keyboard_phase: canvas.WidgetKeyboardPhase = .key_down,
42
+ last_keyboard_focused_id: ?canvas.ObjectId = null,
43
+ last_keyboard_terminal_paste: bool = false,
44
+ last_keyboard_standalone: bool = false,
45
+ saw_truncated: bool = false,
46
+ dismissed_count: u32 = 0,
47
+ last_dismissed_token: u64 = 0,
48
+ last_dismissed_label: [32]u8 = undefined,
49
+ last_dismissed_label_len: usize = 0,
50
+ /// When set, the dismissed-notice handler errors AFTER recording
51
+ /// the notice — the app heard it, then blew up.
52
+ dismissal_error: ?anyerror = null,
53
+ /// When set, the dismissed-notice handler closes this view on
54
+ /// window 1 — app code that compacts the runtime's view indices
55
+ /// while a supersession is mid-dispatch.
56
+ close_view_on_dismissal: ?[]const u8 = null,
57
+ /// When set, the dismissed-notice handler right-clicks this view's
58
+ /// declared-menu widget — app code that synchronously presents a
59
+ /// SUPERSEDING menu while a supersession is mid-dispatch.
60
+ present_menu_on_dismissal: ?[]const u8 = null,
61
+
62
+ fn app(self: *@This()) App {
63
+ return .{ .context = self, .name = "context-menus", .source = platform.WebViewSource.html("<h1>Hello</h1>"), .event_fn = event, .context_menu_policy = policy };
64
+ }
65
+
66
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
67
+ const self: *@This() = @ptrCast(@alignCast(context));
68
+ switch (event_value) {
69
+ .canvas_widget_pointer => self.pointer_count += 1,
70
+ .gpu_surface_input => self.raw_input_count += 1,
71
+ .canvas_widget_context_menu => |menu_event| {
72
+ self.menu_count += 1;
73
+ self.last_menu_target = menu_event.target_id;
74
+ self.last_menu_item_index = menu_event.item_index;
75
+ const len = @min(menu_event.view_label.len, self.last_menu_label.len);
76
+ @memcpy(self.last_menu_label[0..len], menu_event.view_label[0..len]);
77
+ self.last_menu_label_len = len;
78
+ },
79
+ .canvas_widget_context_menu_shown => |shown_event| {
80
+ self.shown_count += 1;
81
+ self.last_shown_token = shown_event.token;
82
+ const len = @min(shown_event.view_label.len, self.last_shown_label.len);
83
+ @memcpy(self.last_shown_label[0..len], shown_event.view_label[0..len]);
84
+ self.last_shown_label_len = len;
85
+ },
86
+ .canvas_widget_context_menu_request => |request_event| {
87
+ self.request_count += 1;
88
+ self.last_request_target = request_event.target_id;
89
+ self.last_request_point = request_event.point;
90
+ },
91
+ .canvas_widget_context_menu_dismissed => |dismissed_event| {
92
+ self.dismissed_count += 1;
93
+ self.last_dismissed_token = dismissed_event.token;
94
+ const len = @min(dismissed_event.view_label.len, self.last_dismissed_label.len);
95
+ @memcpy(self.last_dismissed_label[0..len], dismissed_event.view_label[0..len]);
96
+ self.last_dismissed_label_len = len;
97
+ if (self.close_view_on_dismissal) |close_label| {
98
+ self.close_view_on_dismissal = null;
99
+ try runtime.closeView(1, close_label);
100
+ }
101
+ if (self.present_menu_on_dismissal) |present_label| {
102
+ self.present_menu_on_dismissal = null;
103
+ try runtime.dispatchPlatformEvent(self.app(), rightClickOn(present_label, 50, 20));
104
+ }
105
+ if (self.dismissal_error) |err| return err;
106
+ },
107
+ .canvas_widget_keyboard => |keyboard_event| {
108
+ self.keyboard_count += 1;
109
+ self.last_keyboard_phase = keyboard_event.keyboard.phase;
110
+ self.last_keyboard_focused_id = keyboard_event.keyboard.focused_id;
111
+ self.last_keyboard_terminal_paste = keyboard_event.terminal_paste;
112
+ self.last_keyboard_standalone = keyboard_event.standalone;
113
+ if (keyboard_event.keyboard.edit_truncated) self.saw_truncated = true;
114
+ if (keyboard_event.keyboard.edit) |edit| switch (edit) {
115
+ .insert_text => |text| {
116
+ const len = @min(text.len, self.last_edit_insert.len);
117
+ @memcpy(self.last_edit_insert[0..len], text[0..len]);
118
+ self.last_edit_insert_len = len;
119
+ },
120
+ else => {},
121
+ };
122
+ },
123
+ else => {},
124
+ }
125
+ }
126
+ };
127
+
128
+ fn rightClick(x: f32, y: f32) platform.Event {
129
+ return rightClickOn("canvas", x, y);
130
+ }
131
+
132
+ fn rightClickOn(label: []const u8, x: f32, y: f32) platform.Event {
133
+ return .{ .gpu_surface_input = .{
134
+ .window_id = 1,
135
+ .label = label,
136
+ .kind = .pointer_down,
137
+ .button = 1,
138
+ .x = x,
139
+ .y = y,
140
+ .timestamp_ns = 1_000_000_000,
141
+ } };
142
+ }
143
+
144
+ fn menuAction(token: u64, item_id: u32) platform.Event {
145
+ return .{ .context_menu_action = .{
146
+ .window_id = 1,
147
+ .view_label = "canvas",
148
+ .token = token,
149
+ .item_id = item_id,
150
+ } };
151
+ }
152
+
153
+ fn createMenuHarness(app: App) !*TestHarness() {
154
+ const harness = try TestHarness().create(std.testing.allocator, .{});
155
+ harness.null_platform.gpu_surfaces = true;
156
+ try harness.start(app);
157
+ _ = try harness.runtime.createView(.{
158
+ .window_id = 1,
159
+ .label = "canvas",
160
+ .kind = .gpu_surface,
161
+ .frame = geometry.RectF.init(0, 0, 320, 200),
162
+ });
163
+ return harness;
164
+ }
165
+
166
+ pub fn case0() !void {
167
+ var app_state: MenuTestApp = .{};
168
+ const app = app_state.app();
169
+ const harness = try createMenuHarness(app);
170
+ defer harness.destroy(std.testing.allocator);
171
+
172
+ const items = [_]canvas.WidgetContextMenuItem{
173
+ .{ .label = "Complete" },
174
+ .{ .separator = true },
175
+ .{ .label = "Delete" },
176
+ };
177
+ const row = canvas.Widget{
178
+ .id = 2,
179
+ .kind = .list_item,
180
+ .frame = geometry.RectF.init(10, 10, 200, 40),
181
+ .text = "Task",
182
+ .context_menu = &items,
183
+ };
184
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
185
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{row} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
186
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
187
+
188
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
189
+
190
+ // The platform was asked to present at the pointer with the declared
191
+ // items (ids are 1-based item indices; separators stay separators).
192
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.context_menu_request_count);
193
+ try std.testing.expectEqualStrings("canvas", harness.null_platform.contextMenuLabel());
194
+ // The request's correlation token is minted per request (opaque to
195
+ // the platform, which only echoes it on the action event).
196
+ try std.testing.expect(harness.null_platform.context_menu_token != 0);
197
+ try std.testing.expectEqualDeep(geometry.PointF.init(50, 20), harness.null_platform.context_menu_point);
198
+ const recorded = harness.null_platform.contextMenuItems();
199
+ try std.testing.expectEqual(@as(usize, 3), recorded.len);
200
+ try std.testing.expectEqual(@as(u32, 1), recorded[0].id);
201
+ try std.testing.expectEqualStrings("Complete", recorded[0].label);
202
+ try std.testing.expect(recorded[1].separator);
203
+ try std.testing.expectEqual(@as(u32, 3), recorded[2].id);
204
+ try std.testing.expectEqualStrings("Delete", recorded[2].label);
205
+
206
+ // The right press never acted as a primary press; the raw input event
207
+ // stayed observable.
208
+ try std.testing.expectEqual(@as(u32, 0), app_state.pointer_count);
209
+ try std.testing.expectEqual(@as(u32, 1), app_state.raw_input_count);
210
+
211
+ // Selecting "Delete" dispatches the typed context-menu event.
212
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 3));
213
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
214
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_menu_target);
215
+ try std.testing.expectEqual(@as(usize, 2), app_state.last_menu_item_index);
216
+
217
+ // A dismissal (item 0) resolves silently.
218
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
219
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 0));
220
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
221
+ }
222
+
223
+ pub fn case1() !void {
224
+ var app_state: MenuTestApp = .{};
225
+ const app = app_state.app();
226
+ const harness = try createMenuHarness(app);
227
+ defer harness.destroy(std.testing.allocator);
228
+
229
+ const first_items = [_]canvas.WidgetContextMenuItem{.{ .label = "Complete" }};
230
+ const second_items = [_]canvas.WidgetContextMenuItem{ .{ .label = "Open" }, .{ .label = "Delete" } };
231
+ const first_row = canvas.Widget{
232
+ .id = 2,
233
+ .kind = .list_item,
234
+ .frame = geometry.RectF.init(10, 10, 200, 40),
235
+ .text = "First",
236
+ .context_menu = &first_items,
237
+ };
238
+ const second_row = canvas.Widget{
239
+ .id = 3,
240
+ .kind = .list_item,
241
+ .frame = geometry.RectF.init(10, 60, 200, 40),
242
+ .text = "Second",
243
+ .context_menu = &second_items,
244
+ };
245
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
246
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{ first_row, second_row } }, geometry.RectF.init(0, 0, 320, 200), &nodes);
247
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
248
+
249
+ // The second presentation supersedes the first: the app hears one
250
+ // dismissed notice carrying the OLD token and the VIEW the dismissed
251
+ // menu was presented on (a raw app tracking per-canvas menu state
252
+ // needs the correlation — the notice must never publish an empty
253
+ // label).
254
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
255
+ const first_token = harness.null_platform.context_menu_token;
256
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 70));
257
+ const second_token = harness.null_platform.context_menu_token;
258
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
259
+ try std.testing.expectEqual(first_token, app_state.last_dismissed_token);
260
+ try std.testing.expectEqualStrings("canvas", app_state.last_dismissed_label[0..app_state.last_dismissed_label_len]);
261
+
262
+ // The replacement pending was committed BEFORE the (fallible)
263
+ // dismissed notice dispatched, so the successor menu on the glass
264
+ // resolves normally.
265
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(second_token, 2));
266
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
267
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
268
+ try std.testing.expectEqual(@as(usize, 1), app_state.last_menu_item_index);
269
+ }
270
+
271
+ pub fn case2() !void {
272
+ var app_state: MenuTestApp = .{};
273
+ const app = app_state.app();
274
+ const harness = try createMenuHarness(app);
275
+ defer harness.destroy(std.testing.allocator);
276
+
277
+ // Two rows, each with its own declared menu: the second right-click
278
+ // supersedes the first request while the first menu's deferred
279
+ // dismissal is still in flight (the GTK presenter tears the old menu
280
+ // down one loop turn later).
281
+ const first_items = [_]canvas.WidgetContextMenuItem{.{ .label = "Complete" }};
282
+ const second_items = [_]canvas.WidgetContextMenuItem{ .{ .label = "Open" }, .{ .label = "Delete" } };
283
+ const first_row = canvas.Widget{
284
+ .id = 2,
285
+ .kind = .list_item,
286
+ .frame = geometry.RectF.init(10, 10, 200, 40),
287
+ .text = "First",
288
+ .context_menu = &first_items,
289
+ };
290
+ const second_row = canvas.Widget{
291
+ .id = 3,
292
+ .kind = .list_item,
293
+ .frame = geometry.RectF.init(10, 60, 200, 40),
294
+ .text = "Second",
295
+ .context_menu = &second_items,
296
+ };
297
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
298
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{ first_row, second_row } }, geometry.RectF.init(0, 0, 320, 200), &nodes);
299
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
300
+
301
+ // First request presents, then the second arrives before the first's
302
+ // dismissal is delivered. Each request carries its own minted token.
303
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
304
+ const first_token = harness.null_platform.context_menu_token;
305
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 70));
306
+ const second_token = harness.null_platform.context_menu_token;
307
+ try std.testing.expectEqual(@as(usize, 2), harness.null_platform.context_menu_request_count);
308
+ try std.testing.expect(first_token != second_token);
309
+
310
+ // The superseded request's dismissal arrives late, carrying the OLD
311
+ // token: it is swallowed, never resolving (or clearing) the pending
312
+ // successor.
313
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(first_token, 0));
314
+ try std.testing.expectEqual(@as(u32, 0), app_state.menu_count);
315
+
316
+ // The successor stays pending and resolvable: its own selection
317
+ // dispatches exactly one context-menu event for the second target.
318
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(second_token, 2));
319
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
320
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
321
+ try std.testing.expectEqual(@as(usize, 1), app_state.last_menu_item_index);
322
+
323
+ // One event per request: neither token can resolve again.
324
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(first_token, 1));
325
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(second_token, 2));
326
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
327
+ }
328
+
329
+ pub fn case3() !void {
330
+ var app_state: MenuTestApp = .{};
331
+ const app = app_state.app();
332
+ const harness = try createMenuHarness(app);
333
+ defer harness.destroy(std.testing.allocator);
334
+
335
+ // ONE widget: with a widget-id token the two requests would be
336
+ // indistinguishable and the superseded menu's deferred dismissal
337
+ // would resolve (clear) the replacement's pending request.
338
+ const items = [_]canvas.WidgetContextMenuItem{ .{ .label = "Complete" }, .{ .label = "Delete" } };
339
+ const row = canvas.Widget{
340
+ .id = 2,
341
+ .kind = .list_item,
342
+ .frame = geometry.RectF.init(10, 10, 200, 40),
343
+ .text = "Task",
344
+ .context_menu = &items,
345
+ };
346
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
347
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{row} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
348
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
349
+
350
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
351
+ const first_token = harness.null_platform.context_menu_token;
352
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(60, 24));
353
+ const second_token = harness.null_platform.context_menu_token;
354
+ try std.testing.expect(first_token != second_token);
355
+
356
+ // The first menu's deferred dismissal lands after the re-click: its
357
+ // stale token is swallowed and the replacement stays live.
358
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(first_token, 0));
359
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(second_token, 2));
360
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
361
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_menu_target);
362
+ try std.testing.expectEqual(@as(usize, 1), app_state.last_menu_item_index);
363
+ }
364
+
365
+ pub fn case4() !void {
366
+ var app_state: MenuTestApp = .{};
367
+ const app = app_state.app();
368
+ const harness = try createMenuHarness(app);
369
+ defer harness.destroy(std.testing.allocator);
370
+
371
+ const text_field = canvas.Widget{
372
+ .id = 2,
373
+ .kind = .text_field,
374
+ .frame = geometry.RectF.init(12, 16, 200, 36),
375
+ .text = "Query",
376
+ .semantics = .{ .label = "Search" },
377
+ };
378
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
379
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{text_field} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
380
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
381
+
382
+ // Select "Query" through the keyboard select-all path, then right-click.
383
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
384
+ .window_id = 1,
385
+ .label = "canvas",
386
+ .kind = .pointer_down,
387
+ .x = 100,
388
+ .y = 30,
389
+ } });
390
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
391
+ .window_id = 1,
392
+ .label = "canvas",
393
+ .kind = .key_down,
394
+ .key = "a",
395
+ .modifiers = .{ .primary = true, .command = true },
396
+ } });
397
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 30));
398
+
399
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.context_menu_request_count);
400
+ const recorded = harness.null_platform.contextMenuItems();
401
+ try std.testing.expectEqual(@as(usize, 5), recorded.len);
402
+ try std.testing.expectEqualStrings("Cut", recorded[0].label);
403
+ try std.testing.expect(recorded[0].enabled);
404
+ try std.testing.expectEqualStrings("Copy", recorded[1].label);
405
+ try std.testing.expectEqualStrings("Paste", recorded[2].label);
406
+ try std.testing.expect(recorded[3].separator);
407
+ try std.testing.expectEqualStrings("Select All", recorded[4].label);
408
+
409
+ // Copy through the menu: clipboard captures the selection, text stays.
410
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 2));
411
+ var clipboard_buffer: [256]u8 = undefined;
412
+ try std.testing.expectEqualStrings("Query", try harness.runtime.readClipboard(&clipboard_buffer));
413
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
414
+ try std.testing.expectEqualStrings("Query", retained.nodes[1].widget.text);
415
+
416
+ // Cut through the menu: clipboard keeps the selection, field empties.
417
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 30));
418
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 1));
419
+ try std.testing.expectEqualStrings("Query", try harness.runtime.readClipboard(&clipboard_buffer));
420
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
421
+ try std.testing.expectEqualStrings("", retained.nodes[1].widget.text);
422
+
423
+ // Paste through the menu: the clipboard lands at the caret.
424
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 30));
425
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 3));
426
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
427
+ try std.testing.expectEqualStrings("Query", retained.nodes[1].widget.text);
428
+
429
+ // Select All through the menu re-selects everything.
430
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 30));
431
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 4));
432
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
433
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 5 }, retained.nodes[1].widget.text_selection.?);
434
+ }
435
+
436
+ pub fn case5() !void {
437
+ var app_state: MenuTestApp = .{};
438
+ const app = app_state.app();
439
+ const harness = try createMenuHarness(app);
440
+ defer harness.destroy(std.testing.allocator);
441
+
442
+ // Same boundary as the cmd+V shortcut test: exactly three free bytes
443
+ // in the view's shared widget-text storage, clipboard "a\nbc". The
444
+ // context menu's Paste flows through the same sanitize-then-clamp
445
+ // helper, so the sanitized "abc" lands whole instead of the raw
446
+ // clamp's "a\nb" -> "ab".
447
+ const fill_len = canvas_limits.max_canvas_widget_text_bytes_per_view - 3;
448
+ const fill = try std.testing.allocator.alloc(u8, fill_len);
449
+ defer std.testing.allocator.free(fill);
450
+ @memset(fill, 'x');
451
+ const text_field = canvas.Widget{
452
+ .id = 2,
453
+ .kind = .text_field,
454
+ .frame = geometry.RectF.init(12, 16, 200, 36),
455
+ .text = fill,
456
+ };
457
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
458
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{text_field} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
459
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
460
+ try std.testing.expectEqual(fill_len, harness.runtime.views[0].widget_text_len);
461
+
462
+ try harness.runtime.writeClipboard("a\nbc");
463
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 30));
464
+ // Paste is the default edit menu's third item.
465
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 3));
466
+
467
+ // Retained editor and the app's stamped edit hear the same whole
468
+ // sanitized suffix, with no false truncation flag.
469
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
470
+ const text = retained.nodes[1].widget.text;
471
+ try std.testing.expectEqual(fill_len + 3, text.len);
472
+ try std.testing.expectEqualStrings("abc", text[text.len - 3 ..]);
473
+ try std.testing.expectEqualStrings("abc", app_state.last_edit_insert[0..app_state.last_edit_insert_len]);
474
+ try std.testing.expect(!app_state.saw_truncated);
475
+ }
476
+
477
+ pub fn case6() !void {
478
+ var app_state: MenuTestApp = .{};
479
+ const app = app_state.app();
480
+ const harness = try createMenuHarness(app);
481
+ defer harness.destroy(std.testing.allocator);
482
+
483
+ var grid = canvas.TerminalGrid{
484
+ .background = canvas.Color.rgba(0, 0, 0, 1),
485
+ .foreground = canvas.Color.rgba(1, 1, 1, 1),
486
+ .cursor_color = canvas.Color.rgba(1, 1, 1, 1),
487
+ .selection_color = canvas.Color.rgba(0, 0.5, 1, 1),
488
+ .screen_text = "alpha beta",
489
+ .selection_text = "beta",
490
+ .selection_active = true,
491
+ };
492
+ const terminal = canvas.Widget{
493
+ .id = 2,
494
+ .kind = .terminal,
495
+ .frame = geometry.RectF.init(12, 16, 280, 120),
496
+ .terminal = .{ .pty = 7, .grid = &grid },
497
+ };
498
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
499
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{terminal} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
500
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
501
+
502
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 40));
503
+
504
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.context_menu_request_count);
505
+ const recorded = harness.null_platform.contextMenuItems();
506
+ try std.testing.expectEqual(@as(usize, 2), recorded.len);
507
+ try std.testing.expectEqual(@as(u32, 2), recorded[0].id);
508
+ try std.testing.expectEqualStrings("Copy", recorded[0].label);
509
+ try std.testing.expect(recorded[0].enabled);
510
+ try std.testing.expectEqual(@as(u32, 3), recorded[1].id);
511
+ try std.testing.expectEqualStrings("Paste", recorded[1].label);
512
+ try std.testing.expect(recorded[1].enabled);
513
+ // Secondary-click focus ensures the eventual paste addresses this
514
+ // terminal even when another editor previously owned focus.
515
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
516
+
517
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 2));
518
+ var clipboard_buffer: [64]u8 = undefined;
519
+ try std.testing.expectEqualStrings("beta", try harness.runtime.readClipboard(&clipboard_buffer));
520
+
521
+ const paste = "printf 'ready'\n";
522
+ try harness.runtime.writeClipboard(paste);
523
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 40));
524
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 3));
525
+ try std.testing.expectEqual(canvas.WidgetKeyboardPhase.text_input, app_state.last_keyboard_phase);
526
+ try std.testing.expectEqual(@as(?canvas.ObjectId, 2), app_state.last_keyboard_focused_id);
527
+ try std.testing.expect(app_state.last_keyboard_terminal_paste);
528
+ try std.testing.expect(app_state.last_keyboard_standalone);
529
+ try std.testing.expectEqualStrings(paste, app_state.last_edit_insert[0..app_state.last_edit_insert_len]);
530
+
531
+ // The keyboard shortcut takes the same provenance-sensitive path,
532
+ // but remains part of the gpu-input cycle rather than a standalone
533
+ // native-menu dispatch.
534
+ const shortcut_paste = "echo shortcut\n";
535
+ try harness.runtime.writeClipboard(shortcut_paste);
536
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
537
+ .window_id = 1,
538
+ .label = "canvas",
539
+ .kind = .key_down,
540
+ .key = "v",
541
+ .modifiers = .{ .primary = true, .command = true },
542
+ } });
543
+ try std.testing.expectEqual(canvas.WidgetKeyboardPhase.text_input, app_state.last_keyboard_phase);
544
+ try std.testing.expectEqual(@as(?canvas.ObjectId, 2), app_state.last_keyboard_focused_id);
545
+ try std.testing.expect(app_state.last_keyboard_terminal_paste);
546
+ try std.testing.expect(!app_state.last_keyboard_standalone);
547
+ try std.testing.expectEqualStrings(shortcut_paste, app_state.last_edit_insert[0..app_state.last_edit_insert_len]);
548
+ }
549
+
550
+ pub fn case7() !void {
551
+ var app_state: MenuTestApp = .{};
552
+ const app = app_state.app();
553
+ const harness = try createMenuHarness(app);
554
+ defer harness.destroy(std.testing.allocator);
555
+
556
+ var grid = canvas.TerminalGrid{
557
+ .background = canvas.Color.rgba(0, 0, 0, 1),
558
+ .foreground = canvas.Color.rgba(1, 1, 1, 1),
559
+ .cursor_color = canvas.Color.rgba(1, 1, 1, 1),
560
+ .selection_color = canvas.Color.rgba(0, 0.5, 1, 1),
561
+ };
562
+ const terminal = canvas.Widget{
563
+ .id = 2,
564
+ .kind = .terminal,
565
+ .frame = geometry.RectF.init(12, 16, 280, 120),
566
+ .terminal = .{ .pty = 7, .grid = &grid },
567
+ };
568
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
569
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{terminal} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
570
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
571
+
572
+ try harness.runtime.writeClipboard("must-not-escape");
573
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 40));
574
+ const live_token = harness.null_platform.context_menu_token;
575
+ try std.testing.expect(harness.null_platform.contextMenuItems()[1].enabled);
576
+
577
+ // GTK resolves menus asynchronously: the pty can exit while its
578
+ // live menu remains open. The action-time guard must swallow that
579
+ // stale Paste instead of emitting terminal input that could fall
580
+ // through to an app-level text handler.
581
+ grid.running = false;
582
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(live_token, 3));
583
+ try std.testing.expectEqual(@as(u32, 0), app_state.keyboard_count);
584
+
585
+ // A new menu on the ended terminal describes the same truth up
586
+ // front: Copy remains selection-dependent, Paste is disabled.
587
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 40));
588
+ const ended_items = harness.null_platform.contextMenuItems();
589
+ try std.testing.expectEqual(@as(usize, 2), ended_items.len);
590
+ try std.testing.expect(!ended_items[1].enabled);
591
+ }
592
+
593
+ pub fn case8() !void {
594
+ var app_state: MenuTestApp = .{};
595
+ const app = app_state.app();
596
+ const harness = try createMenuHarness(app);
597
+ defer harness.destroy(std.testing.allocator);
598
+
599
+ const text = canvas.Widget{
600
+ .id = 2,
601
+ .kind = .text,
602
+ .frame = geometry.RectF.init(12, 16, 200, 20),
603
+ .text = "Release notes",
604
+ };
605
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
606
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{text} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
607
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
608
+
609
+ // Establish the view's live static selection the way click-drag does.
610
+ harness.runtime.views[0].canvas_widget_selected_text_id = 2;
611
+ harness.runtime.views[0].widget_layout_nodes[1].widget.text_selection = .{ .anchor = 0, .focus = 7 };
612
+
613
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 24));
614
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.context_menu_request_count);
615
+ const recorded = harness.null_platform.contextMenuItems();
616
+ try std.testing.expectEqual(@as(usize, 1), recorded.len);
617
+ try std.testing.expectEqualStrings("Copy", recorded[0].label);
618
+
619
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(harness.null_platform.context_menu_token, 2));
620
+ var clipboard_buffer: [64]u8 = undefined;
621
+ try std.testing.expectEqualStrings("Release", try harness.runtime.readClipboard(&clipboard_buffer));
622
+ }
623
+
624
+ pub fn case9() !void {
625
+ var app_state: MenuTestApp = .{};
626
+ const app = app_state.app();
627
+ const harness = try createMenuHarness(app);
628
+ defer harness.destroy(std.testing.allocator);
629
+
630
+ const button = canvas.Widget{
631
+ .id = 2,
632
+ .kind = .button,
633
+ .frame = geometry.RectF.init(10, 10, 96, 32),
634
+ .text = "Run",
635
+ };
636
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
637
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{button} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
638
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
639
+
640
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
641
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.context_menu_request_count);
642
+ try std.testing.expectEqual(@as(u32, 0), app_state.pointer_count);
643
+
644
+ // A stray action event with no pending request is a no-op.
645
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(2, 1));
646
+ try std.testing.expectEqual(@as(u32, 0), app_state.menu_count);
647
+ }
648
+
649
+ pub fn case10() !void {
650
+ var app_state: MenuTestApp = .{};
651
+ const app = app_state.app();
652
+ const harness = try TestHarness().create(std.testing.allocator, .{});
653
+ harness.null_platform.gpu_surfaces = true;
654
+ // Model a host without a native menu presenter (the mobile toolkit
655
+ // hosts and embed hosts today): the service is null and the feature
656
+ // reports false.
657
+ // Service POINTERS are captured at init, so re-capture after the
658
+ // flip (feature FLAGS like gpu_surfaces read live through context).
659
+ harness.null_platform.context_menus = false;
660
+ harness.runtime.options.platform = harness.null_platform.platform();
661
+ try harness.start(app);
662
+ defer harness.destroy(std.testing.allocator);
663
+ _ = try harness.runtime.createView(.{
664
+ .window_id = 1,
665
+ .label = "canvas",
666
+ .kind = .gpu_surface,
667
+ .frame = geometry.RectF.init(0, 0, 320, 200),
668
+ });
669
+
670
+ const items = [_]canvas.WidgetContextMenuItem{
671
+ .{ .label = "Complete" },
672
+ .{ .label = "Delete" },
673
+ };
674
+ const row = canvas.Widget{
675
+ .id = 2,
676
+ .kind = .list_item,
677
+ .frame = geometry.RectF.init(10, 10, 200, 40),
678
+ .text = "Task",
679
+ .context_menu = &items,
680
+ };
681
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
682
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{row} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
683
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
684
+
685
+ // Right-click: nothing to present natively, so the app loop is asked
686
+ // to mount the anchored fallback surface for the declared target.
687
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
688
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.context_menu_request_count);
689
+ try std.testing.expectEqual(@as(u32, 1), app_state.request_count);
690
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_request_target);
691
+ // The click point rides the request: the fallback surface anchors at
692
+ // the pointer, not a target-widget edge.
693
+ try std.testing.expectEqualDeep(geometry.PointF.init(50, 20), app_state.last_request_point);
694
+ // Never the hold alternative: a declared menu consumed the press.
695
+ try std.testing.expectEqual(@as(u32, 0), app_state.pointer_count);
696
+
697
+ // The automation verb still drives the SAME selection dispatch — no
698
+ // presenter required (the OS tracking loop is skipped on macOS too).
699
+ try harness.runtime.dispatchAutomationCommand(app, "widget-context-menu canvas 2 1");
700
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
701
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_menu_target);
702
+ try std.testing.expectEqual(@as(usize, 1), app_state.last_menu_item_index);
703
+ }
704
+
705
+ pub fn case11() !void {
706
+ var app_state: MenuTestApp = .{};
707
+ const app = app_state.app();
708
+ const harness = try createMenuHarness(app);
709
+ defer harness.destroy(std.testing.allocator);
710
+
711
+ const items = [_]canvas.WidgetContextMenuItem{
712
+ .{ .label = "Complete" },
713
+ .{ .separator = true },
714
+ .{ .label = "Delete" },
715
+ .{ .label = "Archive", .enabled = false },
716
+ };
717
+ const row = canvas.Widget{
718
+ .id = 2,
719
+ .kind = .list_item,
720
+ .frame = geometry.RectF.init(10, 10, 200, 40),
721
+ .text = "Task",
722
+ .context_menu = &items,
723
+ };
724
+ const plain = canvas.Widget{
725
+ .id = 3,
726
+ .kind = .list_item,
727
+ .frame = geometry.RectF.init(10, 60, 200, 40),
728
+ .text = "Bare",
729
+ };
730
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
731
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{ row, plain } }, geometry.RectF.init(0, 0, 320, 200), &nodes);
732
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
733
+
734
+ // Invoking "Delete" (index 2) takes the same dispatch a real pick
735
+ // does: a context_menu_action platform event resolved against the
736
+ // pending request the verb armed.
737
+ try harness.runtime.dispatchAutomationCommand(app, "widget-context-menu canvas 2 2");
738
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
739
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_menu_target);
740
+ try std.testing.expectEqual(@as(usize, 2), app_state.last_menu_item_index);
741
+ // The verb never asked the platform to present: the OS menu's
742
+ // tracking loop cannot be driven programmatically.
743
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.context_menu_request_count);
744
+
745
+ // Dead invocations fail by name instead of silently doing nothing.
746
+ try std.testing.expectError(error.ContextMenuUndeclared, harness.runtime.dispatchAutomationCommand(app, "widget-context-menu canvas 3 0"));
747
+ try std.testing.expectError(error.ContextMenuItemOutOfRange, harness.runtime.dispatchAutomationCommand(app, "widget-context-menu canvas 2 4"));
748
+ try std.testing.expectError(error.ContextMenuItemSeparator, harness.runtime.dispatchAutomationCommand(app, "widget-context-menu canvas 2 1"));
749
+ try std.testing.expectError(error.ContextMenuItemDisabled, harness.runtime.dispatchAutomationCommand(app, "widget-context-menu canvas 2 3"));
750
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
751
+ }
752
+
753
+ pub fn case12() !void {
754
+ var app_state: MenuTestApp = .{};
755
+ const app = app_state.app();
756
+ const harness = try createMenuHarness(app);
757
+ defer harness.destroy(std.testing.allocator);
758
+
759
+ const first_items = [_]canvas.WidgetContextMenuItem{.{ .label = "Complete" }};
760
+ const second_items = [_]canvas.WidgetContextMenuItem{ .{ .label = "Open" }, .{ .label = "Delete" } };
761
+ const first_row = canvas.Widget{
762
+ .id = 2,
763
+ .kind = .list_item,
764
+ .frame = geometry.RectF.init(10, 10, 200, 40),
765
+ .text = "First",
766
+ .context_menu = &first_items,
767
+ };
768
+ const second_row = canvas.Widget{
769
+ .id = 3,
770
+ .kind = .list_item,
771
+ .frame = geometry.RectF.init(10, 60, 200, 40),
772
+ .text = "Second",
773
+ .context_menu = &second_items,
774
+ };
775
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
776
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{ first_row, second_row } }, geometry.RectF.init(0, 0, 320, 200), &nodes);
777
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
778
+
779
+ // A real menu is on the glass for the first row when the verb
780
+ // supersedes it — and the app's dismissed handler errors under the
781
+ // harness's `.propagate` policy.
782
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(50, 20));
783
+ const first_token = harness.null_platform.context_menu_token;
784
+ app_state.dismissal_error = error.DismissalHandlerBlewUp;
785
+
786
+ // The verb's synthetic selection is its request's ONLY outcome (no
787
+ // native menu presents), so the erroring notice must not skip it:
788
+ // the selection dispatches, the notice error still propagates, and
789
+ // no error path leaves a pending token with no presented menu and
790
+ // no delivered outcome.
791
+ try std.testing.expectError(error.DismissalHandlerBlewUp, harness.runtime.dispatchAutomationCommand(app, "widget-context-menu canvas 3 0"));
792
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
793
+ try std.testing.expectEqual(first_token, app_state.last_dismissed_token);
794
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
795
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
796
+ try std.testing.expectEqual(@as(usize, 0), app_state.last_menu_item_index);
797
+ try std.testing.expect(harness.runtime.canvas_widget_context_menu_pending == null);
798
+ }
799
+
800
+ /// Three same-window views with a declared-menu widget on the first
801
+ /// two: alpha holds the superseded menu, beta the superseding action,
802
+ /// and gamma is the view a stale index would land on after alpha's
803
+ /// removal shifts the array down.
804
+ fn createCompactionHarness(app: App) !*TestHarness() {
805
+ const harness = try TestHarness().create(std.testing.allocator, .{});
806
+ harness.null_platform.gpu_surfaces = true;
807
+ try harness.start(app);
808
+ for ([_][]const u8{ "alpha", "beta", "gamma" }) |label| {
809
+ _ = try harness.runtime.createView(.{
810
+ .window_id = 1,
811
+ .label = label,
812
+ .kind = .gpu_surface,
813
+ .frame = geometry.RectF.init(0, 0, 320, 200),
814
+ });
815
+ }
816
+ const alpha_items = [_]canvas.WidgetContextMenuItem{.{ .label = "Complete" }};
817
+ const beta_items = [_]canvas.WidgetContextMenuItem{ .{ .label = "Open" }, .{ .label = "Delete" } };
818
+ const alpha_row = canvas.Widget{
819
+ .id = 2,
820
+ .kind = .list_item,
821
+ .frame = geometry.RectF.init(10, 10, 200, 40),
822
+ .text = "First",
823
+ .context_menu = &alpha_items,
824
+ };
825
+ const beta_row = canvas.Widget{
826
+ .id = 3,
827
+ .kind = .list_item,
828
+ .frame = geometry.RectF.init(10, 10, 200, 40),
829
+ .text = "Second",
830
+ .context_menu = &beta_items,
831
+ };
832
+ var alpha_nodes: [2]canvas.WidgetLayoutNode = undefined;
833
+ const alpha_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{alpha_row} }, geometry.RectF.init(0, 0, 320, 200), &alpha_nodes);
834
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "alpha", alpha_layout);
835
+ var beta_nodes: [2]canvas.WidgetLayoutNode = undefined;
836
+ const beta_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{beta_row} }, geometry.RectF.init(0, 0, 320, 200), &beta_nodes);
837
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "beta", beta_layout);
838
+ return harness;
839
+ }
840
+
841
+ pub fn case13() !void {
842
+ var app_state: MenuTestApp = .{};
843
+ const app = app_state.app();
844
+ const harness = try createCompactionHarness(app);
845
+ defer harness.destroy(std.testing.allocator);
846
+
847
+ // Alpha's menu is pending when the verb supersedes it from beta —
848
+ // and the app's dismissal handler closes alpha, shifting beta and
849
+ // gamma down one index mid-dispatch.
850
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
851
+ const first_token = harness.null_platform.context_menu_token;
852
+ app_state.close_view_on_dismissal = "alpha";
853
+
854
+ try harness.runtime.dispatchAutomationCommand(app, "widget-context-menu beta 3 0");
855
+
856
+ // The notice named the superseded menu and ran the closure...
857
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
858
+ try std.testing.expectEqual(first_token, app_state.last_dismissed_token);
859
+ try std.testing.expectEqual(@as(usize, 2), harness.runtime.view_count);
860
+ // ...and the synthetic selection still dispatched against BETA:
861
+ // the event names its view from the request's own copy, never a
862
+ // compacted index (which now points at gamma). No pending request
863
+ // survives.
864
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
865
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
866
+ try std.testing.expectEqual(@as(usize, 0), app_state.last_menu_item_index);
867
+ try std.testing.expectEqualStrings("beta", app_state.last_menu_label[0..app_state.last_menu_label_len]);
868
+ try std.testing.expect(harness.runtime.canvas_widget_context_menu_pending == null);
869
+ }
870
+
871
+ pub fn case14() !void {
872
+ var app_state: MenuTestApp = .{};
873
+ const app = app_state.app();
874
+ const harness = try createCompactionHarness(app);
875
+ defer harness.destroy(std.testing.allocator);
876
+
877
+ // Alpha's menu is pending; the verb targets alpha's own item, but
878
+ // the dismissal notice for the superseded presentation right-clicks
879
+ // beta — app code that synchronously presents a SUPERSEDING menu,
880
+ // replacing the verb's freshly armed request before its synthetic
881
+ // action dispatches.
882
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
883
+ app_state.present_menu_on_dismissal = "beta";
884
+ try std.testing.expectError(
885
+ error.ContextMenuSuperseded,
886
+ harness.runtime.dispatchAutomationCommand(app, "widget-context-menu alpha 2 0"),
887
+ );
888
+
889
+ // Never a silent success: the requested item did not dispatch, and
890
+ // the driver heard why. Both supersessions were announced (the
891
+ // right-click's menu, then the verb's own).
892
+ try std.testing.expectEqual(@as(u32, 0), app_state.menu_count);
893
+ try std.testing.expectEqual(@as(u32, 2), app_state.dismissed_count);
894
+
895
+ // The handler's successor menu is on the glass with its own pending
896
+ // request — nothing orphaned, still resolvable.
897
+ try std.testing.expectEqual(@as(usize, 2), harness.null_platform.context_menu_request_count);
898
+ const successor_token = harness.null_platform.context_menu_token;
899
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
900
+ .window_id = 1,
901
+ .view_label = "beta",
902
+ .token = successor_token,
903
+ .item_id = 2,
904
+ } });
905
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
906
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
907
+ try std.testing.expectEqual(@as(usize, 1), app_state.last_menu_item_index);
908
+ }
909
+
910
+ pub fn case15() !void {
911
+ var app_state: MenuTestApp = .{};
912
+ const app = app_state.app();
913
+ const harness = try createCompactionHarness(app);
914
+ defer harness.destroy(std.testing.allocator);
915
+
916
+ // Alpha's menu is pending; the verb targets beta, but the dismissal
917
+ // notice for the superseded presentation closes BETA — the verb's
918
+ // own target view. Its armed request can never resolve, so the verb
919
+ // disarms it and refuses by name instead of reporting success while
920
+ // the action dispatch silently drops the selection.
921
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
922
+ app_state.close_view_on_dismissal = "beta";
923
+ try std.testing.expectError(
924
+ error.ContextMenuViewClosed,
925
+ harness.runtime.dispatchAutomationCommand(app, "widget-context-menu beta 3 0"),
926
+ );
927
+
928
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
929
+ try std.testing.expectEqual(@as(u32, 0), app_state.menu_count);
930
+ try std.testing.expectEqual(@as(usize, 2), harness.runtime.view_count);
931
+ // No orphan: the unresolvable request is disarmed with the refusal.
932
+ try std.testing.expect(harness.runtime.canvas_widget_context_menu_pending == null);
933
+ }
934
+
935
+ pub fn case16() !void {
936
+ var app_state: MenuTestApp = .{};
937
+ const app = app_state.app();
938
+ const harness = try createCompactionHarness(app);
939
+ defer harness.destroy(std.testing.allocator);
940
+
941
+ // Menu A is pending on alpha; right-clicking beta presents menu B,
942
+ // and A's dismissal notice closes BETA — the view B presented on.
943
+ // B's request can never resolve, so it disarms without announcing:
944
+ // a shown event would promise a snapshot for a menu whose action
945
+ // can never be delivered.
946
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
947
+ try std.testing.expectEqual(@as(u32, 1), app_state.shown_count);
948
+ app_state.close_view_on_dismissal = "beta";
949
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("beta", 50, 20));
950
+ const dead_token = harness.null_platform.context_menu_token;
951
+
952
+ try std.testing.expectEqual(@as(usize, 2), harness.null_platform.context_menu_request_count);
953
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
954
+ try std.testing.expectEqual(@as(u32, 1), app_state.shown_count);
955
+ try std.testing.expectEqual(@as(usize, 2), harness.runtime.view_count);
956
+ try std.testing.expect(harness.runtime.canvas_widget_context_menu_pending == null);
957
+
958
+ // A late action carrying the dead request's token is inert...
959
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(dead_token, 1));
960
+ try std.testing.expectEqual(@as(u32, 0), app_state.menu_count);
961
+
962
+ // ...and a fresh presentation arms cleanly.
963
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
964
+ try std.testing.expectEqual(@as(u32, 2), app_state.shown_count);
965
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
966
+ .window_id = 1,
967
+ .view_label = "alpha",
968
+ .token = harness.null_platform.context_menu_token,
969
+ .item_id = 1,
970
+ } });
971
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
972
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_menu_target);
973
+ }
974
+
975
+ pub fn case17() !void {
976
+ var app_state: MenuTestApp = .{};
977
+ const app = app_state.app();
978
+ const harness = try createCompactionHarness(app);
979
+ defer harness.destroy(std.testing.allocator);
980
+
981
+ // Menu A is pending on alpha. Right-clicking beta presents menu B,
982
+ // whose supersession notice for A synchronously re-presents alpha's
983
+ // menu (C) — replacing B's freshly committed request before B's
984
+ // shown event dispatches.
985
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
986
+ try std.testing.expectEqual(@as(u32, 1), app_state.shown_count);
987
+ app_state.present_menu_on_dismissal = "alpha";
988
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("beta", 50, 20));
989
+
990
+ // Three presentations reached the platform (A, B, C), but only TWO
991
+ // announced themselves: B was superseded mid-notice, so its late
992
+ // shown event must not overwrite the successor's snapshot — the
993
+ // last announcement is C's, carrying C's token on alpha's canvas.
994
+ try std.testing.expectEqual(@as(usize, 3), harness.null_platform.context_menu_request_count);
995
+ try std.testing.expectEqual(@as(u32, 2), app_state.shown_count);
996
+ const successor_token = harness.null_platform.context_menu_token;
997
+ try std.testing.expectEqual(successor_token, app_state.last_shown_token);
998
+ try std.testing.expectEqualStrings("alpha", app_state.last_shown_label[0..app_state.last_shown_label_len]);
999
+ // Both supersessions were announced: A's (by B), then B's (by C).
1000
+ try std.testing.expectEqual(@as(u32, 2), app_state.dismissed_count);
1001
+
1002
+ // The successor resolves normally against its own token.
1003
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
1004
+ .window_id = 1,
1005
+ .view_label = "alpha",
1006
+ .token = successor_token,
1007
+ .item_id = 1,
1008
+ } });
1009
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
1010
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_menu_target);
1011
+ try std.testing.expectEqual(@as(usize, 0), app_state.last_menu_item_index);
1012
+ try std.testing.expect(harness.runtime.canvas_widget_context_menu_pending == null);
1013
+ }
1014
+
1015
+ pub fn case18() !void {
1016
+ var app_state: MenuTestApp = .{};
1017
+ const app = app_state.app();
1018
+ const harness = try createCompactionHarness(app);
1019
+ defer harness.destroy(std.testing.allocator);
1020
+
1021
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("alpha", 50, 20));
1022
+ try std.testing.expectEqual(@as(u32, 1), app_state.shown_count);
1023
+ try std.testing.expectEqualStrings("alpha", app_state.last_shown_label[0..app_state.last_shown_label_len]);
1024
+
1025
+ // Beta's presentation supersedes alpha's; the dismissal handler
1026
+ // closes alpha, compacting the view array while the shown event is
1027
+ // still to be dispatched.
1028
+ app_state.close_view_on_dismissal = "alpha";
1029
+ try harness.runtime.dispatchPlatformEvent(app, rightClickOn("beta", 50, 20));
1030
+ const second_token = harness.null_platform.context_menu_token;
1031
+ try std.testing.expectEqual(@as(u32, 1), app_state.dismissed_count);
1032
+ // The shown event describes the menu on the glass from the request
1033
+ // itself: beta's canvas, beta's token — never the view a stale
1034
+ // index lands on after the compaction.
1035
+ try std.testing.expectEqual(@as(u32, 2), app_state.shown_count);
1036
+ try std.testing.expectEqual(second_token, app_state.last_shown_token);
1037
+ try std.testing.expectEqualStrings("beta", app_state.last_shown_label[0..app_state.last_shown_label_len]);
1038
+
1039
+ // The successor resolves normally on its own view.
1040
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
1041
+ .window_id = 1,
1042
+ .view_label = "beta",
1043
+ .token = second_token,
1044
+ .item_id = 2,
1045
+ } });
1046
+ try std.testing.expectEqual(@as(u32, 1), app_state.menu_count);
1047
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_menu_target);
1048
+ try std.testing.expectEqual(@as(usize, 1), app_state.last_menu_item_index);
1049
+ try std.testing.expectEqualStrings("beta", app_state.last_menu_label[0..app_state.last_menu_label_len]);
1050
+ }
1051
+
1052
+ pub fn case19() !void {
1053
+ var app_state: MenuTestApp = .{};
1054
+ const app = app_state.app();
1055
+ const harness = try createMenuHarness(app);
1056
+ defer harness.destroy(std.testing.allocator);
1057
+
1058
+ const items = [_]canvas.WidgetContextMenuItem{
1059
+ .{ .label = "Complete" },
1060
+ .{ .separator = true },
1061
+ .{ .label = "Archive", .enabled = false },
1062
+ };
1063
+ const row = canvas.Widget{
1064
+ .id = 2,
1065
+ .kind = .list_item,
1066
+ .frame = geometry.RectF.init(10, 10, 200, 40),
1067
+ .text = "Task",
1068
+ .context_menu = &items,
1069
+ };
1070
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
1071
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{row} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
1072
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1073
+
1074
+ var buffer: [16384]u8 = undefined;
1075
+ var writer = std.Io.Writer.fixed(&buffer);
1076
+ try automation.snapshot.writeText(harness.runtime.automationSnapshot("Menus"), &writer);
1077
+ // List position = the widget-context-menu item index; separators
1078
+ // keep their slots and disabled items say so.
1079
+ try std.testing.expect(std.mem.indexOf(u8, writer.buffered(), "context_menu=[\"Complete\",separator,\"Archive\"(disabled)]") != null);
1080
+ }
1081
+
1082
+ pub fn case20() !void {
1083
+ var app_state: MenuTestApp = .{};
1084
+ const app = app_state.app();
1085
+ const harness = try createMenuHarness(app);
1086
+ defer harness.destroy(std.testing.allocator);
1087
+
1088
+ const items = [_]canvas.WidgetContextMenuItem{
1089
+ .{ .label = "Complete" },
1090
+ .{ .separator = true },
1091
+ .{ .label = "Delete" },
1092
+ };
1093
+ const row = canvas.Widget{
1094
+ .id = 2,
1095
+ .kind = .list_item,
1096
+ .frame = geometry.RectF.init(10, 10, 200, 40),
1097
+ .text = "Task",
1098
+ .context_menu = &items,
1099
+ };
1100
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
1101
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{row} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
1102
+ const info = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1103
+
1104
+ // Separators count against the budget: they occupy retained slots.
1105
+ try std.testing.expectEqual(@as(usize, 3), info.widget_context_menu_item_count);
1106
+
1107
+ // The gpu_surface view line reports declared/budget headroom so
1108
+ // authors watch the cliff without overflowing.
1109
+ var buffer: [16384]u8 = undefined;
1110
+ var writer = std.Io.Writer.fixed(&buffer);
1111
+ try automation.snapshot.writeText(harness.runtime.automationSnapshot("Menus"), &writer);
1112
+ const expected = std.fmt.comptimePrint("context_menu_items=3/{d}", .{canvas_limits.max_canvas_widget_context_menu_items_per_view});
1113
+ try std.testing.expect(std.mem.indexOf(u8, writer.buffered(), expected) != null);
1114
+ }
1115
+
1116
+ pub fn case21() !void {
1117
+ const budget = canvas_limits.max_canvas_widget_context_menu_items_per_view;
1118
+ var app_state: MenuTestApp = .{};
1119
+ const app = app_state.app();
1120
+ const harness = try createMenuHarness(app);
1121
+ defer harness.destroy(std.testing.allocator);
1122
+
1123
+ const bulk = [_]canvas.WidgetContextMenuItem{.{ .label = "Op" }} ** (budget - 1);
1124
+ const pair = [_]canvas.WidgetContextMenuItem{ .{ .label = "A" }, .{ .label = "B" } };
1125
+ var nodes = [_]canvas.WidgetLayoutNode{
1126
+ .{
1127
+ .widget = .{ .id = 2, .kind = .list_item, .text = "Bulk", .context_menu = &bulk },
1128
+ .frame = geometry.RectF.init(0, 0, 200, 40),
1129
+ .depth = 0,
1130
+ },
1131
+ .{
1132
+ .widget = .{ .id = 3, .kind = .list_item, .text = "Tail", .context_menu = pair[0..1] },
1133
+ .frame = geometry.RectF.init(0, 40, 200, 40),
1134
+ .depth = 0,
1135
+ },
1136
+ };
1137
+
1138
+ // Exactly at the budget: the layout retains every declared item.
1139
+ const info = try harness.runtime.setCanvasWidgetLayout(1, "canvas", .{ .nodes = &nodes });
1140
+ try std.testing.expectEqual(budget, info.widget_context_menu_item_count);
1141
+
1142
+ // One extra declared item anywhere in the view overflows loudly.
1143
+ nodes[1].widget.context_menu = &pair;
1144
+ try std.testing.expectError(error.WidgetContextMenuLimitReached, harness.runtime.setCanvasWidgetLayout(1, "canvas", .{ .nodes = &nodes }));
1145
+ }
1146
+ };
1147
+ }