@native-sdk/cli 0.5.4 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/build/app.zig +50 -1
  2. package/build.zig +534 -3
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +3 -1
  5. package/package.json +9 -9
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/rt/rt.zig +280 -8
  9. package/packages/core/sdk/core.ts +481 -2
  10. package/packages/core/sdk/events.ts +17 -8
  11. package/packages/core/src/checker.ts +305 -2
  12. package/packages/core/src/diagnostics.ts +12 -0
  13. package/packages/core/src/emitter.ts +599 -3
  14. package/packages/core/src/typed_ast.ts +51 -4
  15. package/packages/core/src/types.ts +34 -1
  16. package/skill-data/native-ui/SKILL.md +39 -2
  17. package/skill-data/ts-core/SKILL.md +4 -2
  18. package/src/app_runner/root.zig +32 -10
  19. package/src/automation/layout_fingerprint.zig +160 -0
  20. package/src/automation/protocol.zig +82 -33
  21. package/src/automation/root.zig +1 -0
  22. package/src/automation/server.zig +1 -1
  23. package/src/automation/snapshot.zig +68 -9
  24. package/src/automation/watcher.zig +1 -1
  25. package/src/embed/host.zig +5 -0
  26. package/src/embed/ui_host.zig +13 -0
  27. package/src/platform/linux/gtk_host.c +527 -13
  28. package/src/platform/linux/gtk_host.h +25 -0
  29. package/src/platform/linux/root.zig +280 -7
  30. package/src/platform/macos/appkit_host.h +104 -6
  31. package/src/platform/macos/appkit_host.m +1283 -66
  32. package/src/platform/macos/cef_host.mm +110 -1
  33. package/src/platform/macos/root.zig +287 -7
  34. package/src/platform/null_platform.zig +398 -4
  35. package/src/platform/null_platform_tests.zig +35 -0
  36. package/src/platform/root.zig +9 -0
  37. package/src/platform/types.zig +386 -14
  38. package/src/platform/windows/root.zig +298 -9
  39. package/src/platform/windows/webview2_host.cpp +330 -5
  40. package/src/primitives/app_manifest/validation.zig +18 -0
  41. package/src/primitives/canvas/a11y_audit.zig +33 -11
  42. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  43. package/src/primitives/canvas/drawing.zig +25 -0
  44. package/src/primitives/canvas/equality.zig +8 -0
  45. package/src/primitives/canvas/events.zig +103 -16
  46. package/src/primitives/canvas/frame.zig +125 -1
  47. package/src/primitives/canvas/layout_audit.zig +22 -5
  48. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  49. package/src/primitives/canvas/render_tests.zig +13 -10
  50. package/src/primitives/canvas/root.zig +48 -0
  51. package/src/primitives/canvas/terminal_box.zig +471 -0
  52. package/src/primitives/canvas/terminal_grid.zig +978 -0
  53. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  54. package/src/primitives/canvas/test_support.zig +1 -0
  55. package/src/primitives/canvas/tests.zig +1 -0
  56. package/src/primitives/canvas/text_tests.zig +39 -4
  57. package/src/primitives/canvas/tokens.zig +115 -12
  58. package/src/primitives/canvas/ui.zig +524 -5
  59. package/src/primitives/canvas/ui_markup.zig +195 -2
  60. package/src/primitives/canvas/ui_markup_compiled.zig +172 -1
  61. package/src/primitives/canvas/ui_markup_compiled_tests.zig +133 -8
  62. package/src/primitives/canvas/ui_markup_contract.zig +88 -5
  63. package/src/primitives/canvas/ui_markup_contract_tests.zig +39 -1
  64. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  65. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  66. package/src/primitives/canvas/ui_markup_tests.zig +149 -0
  67. package/src/primitives/canvas/ui_markup_view.zig +191 -0
  68. package/src/primitives/canvas/ui_markup_view_tests.zig +403 -19
  69. package/src/primitives/canvas/ui_schema.zig +97 -17
  70. package/src/primitives/canvas/ui_schema_tests.zig +18 -11
  71. package/src/primitives/canvas/ui_tests.zig +141 -9
  72. package/src/primitives/canvas/widget_access.zig +28 -2
  73. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  74. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  75. package/src/primitives/canvas/widget_layout.zig +59 -11
  76. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  77. package/src/primitives/canvas/widget_render.zig +272 -18
  78. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  79. package/src/primitives/canvas/widget_routing.zig +59 -7
  80. package/src/primitives/canvas/widget_runtime.zig +23 -0
  81. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  82. package/src/primitives/canvas/widget_semantics.zig +119 -11
  83. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  84. package/src/primitives/canvas/widget_tree.zig +17 -0
  85. package/src/primitives/canvas/widgets.zig +132 -0
  86. package/src/root.zig +16 -0
  87. package/src/runtime/api.zig +70 -1
  88. package/src/runtime/automation_commands.zig +20 -1
  89. package/src/runtime/automation_snapshot.zig +25 -1
  90. package/src/runtime/automation_widget_dispatch.zig +73 -8
  91. package/src/runtime/bridge_payload.zig +1 -0
  92. package/src/runtime/canvas_frame.zig +62 -7
  93. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  94. package/src/runtime/canvas_frame_helpers.zig +190 -1
  95. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  96. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  97. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  98. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  99. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  100. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  101. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  102. package/src/runtime/canvas_widget_display.zig +1 -1
  103. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  104. package/src/runtime/canvas_widget_events.zig +240 -24
  105. package/src/runtime/canvas_widget_runtime.zig +213 -29
  106. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  107. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  108. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  109. package/src/runtime/canvas_widget_state.zig +6 -0
  110. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  111. package/src/runtime/core.zig +73 -2
  112. package/src/runtime/effects.zig +6389 -54
  113. package/src/runtime/effects_channel_tests.zig +3519 -0
  114. package/src/runtime/effects_pty_tests.zig +1362 -0
  115. package/src/runtime/effects_tests.zig +14 -9
  116. package/src/runtime/effects_video_tests.zig +3609 -0
  117. package/src/runtime/flow.zig +25 -4
  118. package/src/runtime/gpu_surface_events.zig +473 -13
  119. package/src/runtime/media_surface.zig +116 -11
  120. package/src/runtime/media_surface_tests.zig +61 -0
  121. package/src/runtime/pty.zig +1252 -0
  122. package/src/runtime/pty_windows.zig +1780 -0
  123. package/src/runtime/root.zig +25 -0
  124. package/src/runtime/runtime_event_tests.zig +20 -1
  125. package/src/runtime/session_journal.zig +367 -61
  126. package/src/runtime/session_record.zig +16 -0
  127. package/src/runtime/session_replay.zig +395 -11
  128. package/src/runtime/session_tests.zig +109 -15
  129. package/src/runtime/terminal_session.zig +1569 -0
  130. package/src/runtime/terminal_session_tests.zig +450 -0
  131. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  132. package/src/runtime/terminal_vt_stub.zig +13 -0
  133. package/src/runtime/tests.zig +6 -0
  134. package/src/runtime/ts_core_host.zig +980 -140
  135. package/src/runtime/ts_core_host_tests.zig +1059 -1
  136. package/src/runtime/ts_ui_app.zig +16 -5
  137. package/src/runtime/ui_app.zig +2333 -63
  138. package/src/runtime/ui_app_tests.zig +3171 -744
  139. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  140. package/src/runtime/ui_app_window_tests.zig +268 -0
  141. package/src/runtime/view.zig +190 -2
  142. package/src/runtime/view_widget_scroll.zig +218 -68
  143. package/src/runtime/view_widget_text.zig +11 -2
  144. package/src/runtime/view_widget_tree.zig +111 -3
  145. package/src/runtime/window_storage.zig +45 -2
  146. package/src/runtime/window_view_runtime.zig +34 -2
  147. package/src/runtime/window_views.zig +1 -0
  148. package/src/terminal_session_tests_root.zig +15 -0
  149. package/src/tooling/templates.zig +25 -7
@@ -8,6 +8,7 @@ const effects_mod = @import("effects.zig");
8
8
  const automation = @import("../automation/root.zig");
9
9
  const zero_platform = @import("../platform/root.zig");
10
10
  const null_platform_mod = @import("../platform/null_platform.zig");
11
+ const canvas_frame_helpers = @import("canvas_frame_helpers.zig");
11
12
 
12
13
  const canvas_label = "counter-canvas";
13
14
 
@@ -191,8 +192,9 @@ test "a declared context menu presents as the anchored fallback surface on prese
191
192
  const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
192
193
  defer harness.destroy(std.testing.allocator);
193
194
  harness.null_platform.gpu_surfaces = true;
194
- // A host without a native menu presenter (Linux GTK, Windows Win32
195
- // today). Service pointers are captured at init, so re-capture.
195
+ // A host without a native menu presenter (the mobile toolkit hosts
196
+ // and embed hosts today). Service pointers are captured at init, so
197
+ // re-capture.
196
198
  harness.null_platform.context_menus = false;
197
199
  harness.runtime.options.platform = harness.null_platform.platform();
198
200
 
@@ -261,6 +263,65 @@ test "a declared context menu presents as the anchored fallback surface on prese
261
263
  try std.testing.expect(app_state.tree.?.context_menu_fallback == null);
262
264
  }
263
265
 
266
+ test "the fallback surface mounts at the click point, not the wide row's edge" {
267
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
268
+ defer harness.destroy(std.testing.allocator);
269
+ harness.null_platform.gpu_surfaces = true;
270
+ harness.null_platform.context_menus = false;
271
+ harness.runtime.options.platform = harness.null_platform.platform();
272
+
273
+ const app_state = try std.testing.allocator.create(TaskRowApp);
274
+ defer std.testing.allocator.destroy(app_state);
275
+ app_state.* = TaskRowApp.init(std.heap.page_allocator, .{}, taskRowOptions());
276
+ defer app_state.deinit();
277
+ const app = app_state.app();
278
+ try harness.start(app);
279
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
280
+ .label = canvas_label,
281
+ .size = geometry.SizeF.init(400, 300),
282
+ .scale_factor = 2,
283
+ .frame_index = 1,
284
+ .timestamp_ns = 1_000_000,
285
+ .nonblank = true,
286
+ } });
287
+ try std.testing.expect(app_state.installed);
288
+
289
+ // Secondary-click the full-width row far from its left edge: the
290
+ // click point rides the request into the mounted surface's anchor.
291
+ const layout_before = try harness.runtime.canvasWidgetLayout(1, canvas_label);
292
+ const row_id = findWidgetIdByText(app_state.tree.?, .list_item, "Task").?;
293
+ const row_frame = blk: {
294
+ for (layout_before.nodes) |node| {
295
+ if (node.widget.id == row_id) break :blk node.frame;
296
+ }
297
+ return error.TestUnexpectedResult;
298
+ };
299
+ const click = geometry.PointF.init(row_frame.x + 150, row_frame.y + row_frame.height / 2);
300
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
301
+ .window_id = 1,
302
+ .label = canvas_label,
303
+ .kind = .pointer_down,
304
+ .button = 1,
305
+ .x = click.x,
306
+ .y = click.y,
307
+ .timestamp_ns = 1_000_000_000,
308
+ } });
309
+
310
+ try std.testing.expect(app_state.tree.?.context_menu_fallback != null);
311
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
312
+ const surface_frame = blk: {
313
+ for (layout.nodes) |node| {
314
+ if (node.widget.kind == .dropdown_menu) break :blk node.frame;
315
+ }
316
+ return error.TestUnexpectedResult;
317
+ };
318
+ // The surface corner sits at the pointer (space permits below and to
319
+ // the right here), NOT at the row's bottom-left corner.
320
+ try std.testing.expectEqual(click.x, surface_frame.x);
321
+ try std.testing.expectEqual(click.y, surface_frame.y);
322
+ try std.testing.expect(surface_frame.x != row_frame.x);
323
+ }
324
+
264
325
  // ------------------------------------------------- scroll event fixture
265
326
 
266
327
  const FeedModel = struct {
@@ -284,9 +345,9 @@ const FeedApp = ui_app_model.UiApp(FeedModel, FeedMsg);
284
345
  fn feedUpdate(model: *FeedModel, msg: FeedMsg) void {
285
346
  switch (msg) {
286
347
  .feed_scrolled => |scroll_state| {
287
- model.offset = scroll_state.offset;
288
- model.viewport_extent = scroll_state.viewport_extent;
289
- model.content_extent = scroll_state.content_extent;
348
+ model.offset = scroll_state.offset_y;
349
+ model.viewport_extent = scroll_state.viewport_extent_y;
350
+ model.content_extent = scroll_state.content_extent_y;
290
351
  model.scroll_events += 1;
291
352
  },
292
353
  }
@@ -419,6 +480,157 @@ test "ui app presents pixels when the packet service is unsupported" {
419
480
  try std.testing.expect(try retainedTextExists(&harness.runtime, "Count 1"));
420
481
  }
421
482
 
483
+ // ---------------------------------- reflow stale-pixel oracle fixture
484
+
485
+ const ReflowRow = struct {
486
+ id: u32,
487
+ open: bool,
488
+ in_progress: bool,
489
+ urgent: bool,
490
+ high: bool,
491
+ title: []const u8,
492
+ };
493
+
494
+ const reflow_rows_a = [_]ReflowRow{.{ .id = 1, .open = false, .in_progress = false, .urgent = true, .high = false, .title = "Login fails on retry" }};
495
+ const reflow_rows_b = [_]ReflowRow{.{ .id = 2, .open = false, .in_progress = true, .urgent = false, .high = true, .title = "Sync is slow" }};
496
+ // Same for-key as rows_a: the `<if>` arms swap INSIDE a stable keyed
497
+ // subtree (a status change on the selected item, not a reselection).
498
+ const reflow_rows_a_started = [_]ReflowRow{.{ .id = 1, .open = false, .in_progress = true, .urgent = false, .high = true, .title = "Login fails on retry" }};
499
+
500
+ const ReflowModel = struct {
501
+ selected: []const ReflowRow = &reflow_rows_a,
502
+ };
503
+
504
+ const ReflowMsg = union(enum) {
505
+ select_second,
506
+ start_first,
507
+ };
508
+
509
+ const ReflowApp = ui_app_model.UiApp(ReflowModel, ReflowMsg);
510
+
511
+ fn reflowUpdate(model: *ReflowModel, msg: ReflowMsg) void {
512
+ switch (msg) {
513
+ .select_second => model.selected = &reflow_rows_b,
514
+ .start_first => model.selected = &reflow_rows_a_started,
515
+ }
516
+ }
517
+
518
+ // The list-detail detail pane shape: a keyed subtree per selection
519
+ // wrapping conditional badges plus trailing text, so switching the
520
+ // selection removes pills, shrinks the row, and moves what remains.
521
+ const reflow_markup =
522
+ \\<column grow="1" background="surface">
523
+ \\ <for each="selected" key="id" as="s">
524
+ \\ <column gap="14" padding="24">
525
+ \\ <text size="heading" wrap="true">{s.title}</text>
526
+ \\ <row gap="8" cross="center">
527
+ \\ <if test="{s.open}"><badge foreground="info">Open</badge></if>
528
+ \\ <if test="{s.in_progress}"><badge foreground="warning">In progress</badge></if>
529
+ \\ <if test="{s.urgent}"><badge foreground="destructive">Urgent priority</badge></if>
530
+ \\ <if test="{s.high}"><badge foreground="warning">High priority</badge></if>
531
+ \\ <text foreground="text_muted">#{s.id} · reported by a user</text>
532
+ \\ </row>
533
+ \\ </column>
534
+ \\ </for>
535
+ \\</column>
536
+ ;
537
+
538
+ fn reflowOptions() ReflowApp.Options {
539
+ return .{
540
+ .name = "ui-app-reflow-oracle",
541
+ .scene = counter_scene,
542
+ .canvas_label = canvas_label,
543
+ .update = reflowUpdate,
544
+ .markup = .{ .source = reflow_markup },
545
+ };
546
+ }
547
+
548
+ test "selection-change reflow leaves no stale pixels on the ui app pixel path" {
549
+ const surface = geometry.SizeF.init(400, 300);
550
+ const scale: f32 = 2;
551
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = surface });
552
+ defer harness.destroy(std.testing.allocator);
553
+ harness.null_platform.gpu_surfaces = true;
554
+ // Pixel-only host shape (a platform with no packet presenter wired,
555
+ // like the embed hosts): presents ride the CPU pixel path
556
+ // incrementally instead of forcing packet-fallback full repaints.
557
+ harness.runtime.options.platform.services.present_gpu_surface_packet_fn = null;
558
+ harness.runtime.options.platform.services.present_gpu_surface_packet_binary_fn = null;
559
+ // The refined dirty-bounds path: a Msg rebuild derives its damage
560
+ // from the retained key+fingerprint edit script — the derivation
561
+ // packet hosts and embed hosts consume — instead of degrading to
562
+ // the window.
563
+ harness.runtime.options.pixel_present_retained_baseline = true;
564
+
565
+ const app_state = try std.testing.allocator.create(ReflowApp);
566
+ defer std.testing.allocator.destroy(app_state);
567
+ app_state.* = ReflowApp.init(std.heap.page_allocator, .{}, reflowOptions());
568
+ defer app_state.deinit();
569
+ const app = app_state.app();
570
+ try harness.start(app);
571
+
572
+ // Installing frame: full repaint of selection A through the CPU
573
+ // pixel path into the app's retained pixel buffer.
574
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
575
+ .label = canvas_label,
576
+ .size = surface,
577
+ .scale_factor = scale,
578
+ .frame_index = 1,
579
+ .timestamp_ns = 1_000_000,
580
+ } });
581
+ try std.testing.expect(app_state.installed);
582
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.gpu_surface_present_count);
583
+
584
+ const byte_len = (try canvas_frame_helpers.canvasSurfacePixelSize(surface, scale)).byte_len;
585
+ const after_swap = try std.testing.allocator.alloc(u8, byte_len);
586
+ defer std.testing.allocator.free(after_swap);
587
+ const full = try std.testing.allocator.alloc(u8, byte_len);
588
+ defer std.testing.allocator.free(full);
589
+ const full_scratch = try std.testing.allocator.alloc(u8, byte_len);
590
+ defer std.testing.allocator.free(full_scratch);
591
+
592
+ // Two reflows: the `<if>` arms swapping inside a stable keyed
593
+ // subtree (status change), then the keyed subtree replaced whole
594
+ // (reselection). Each presents incrementally and must leave the
595
+ // retained buffer byte-identical to a full repaint of the same
596
+ // scene.
597
+ const steps = [_]struct { msg: ReflowMsg, frame_index: u64 }{
598
+ .{ .msg = .start_first, .frame_index = 2 },
599
+ .{ .msg = .select_second, .frame_index = 3 },
600
+ };
601
+ var present_count: usize = 1;
602
+ for (steps) |step| {
603
+ try app_state.dispatch(&harness.runtime, 1, step.msg);
604
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
605
+ .label = canvas_label,
606
+ .size = surface,
607
+ .scale_factor = scale,
608
+ .frame_index = step.frame_index,
609
+ .timestamp_ns = step.frame_index * 17_000_000,
610
+ } });
611
+ present_count += 1;
612
+ try std.testing.expectEqual(present_count, harness.null_platform.gpu_surface_present_count);
613
+ // Proof the reflow rode the incremental path: the present's
614
+ // dirty bounds are a sub-window region.
615
+ const swap_dirty = harness.null_platform.gpu_surface_present_dirty_bounds.?;
616
+ try std.testing.expect(swap_dirty.width < surface.width or swap_dirty.height < surface.height);
617
+ @memcpy(after_swap, app_state.pixel_buffer[0..byte_len]);
618
+
619
+ // Ground truth: the same scene fully repainted into a fresh
620
+ // buffer (a direct render, so the app's incremental buffer
621
+ // stays untouched).
622
+ _ = try harness.runtime.presentNextCanvasFramePixels(1, canvas_label, .{
623
+ .frame_index = step.frame_index + 100,
624
+ .timestamp_ns = (step.frame_index + 100) * 17_000_000,
625
+ .surface_size = surface,
626
+ .scale = scale,
627
+ .full_repaint = true,
628
+ }, harness.runtime.canvasFrameScratchStorage(), full, full_scratch, app_state.effectiveTokens().colors.background);
629
+ present_count += 1;
630
+ try std.testing.expectEqualSlices(u8, full, after_swap);
631
+ }
632
+ }
633
+
422
634
  const counter_markup =
423
635
  \\<column gap="8" padding="12">
424
636
  \\ <text>Count {count}</text>
@@ -2661,7 +2873,7 @@ test "an effects-wake rebuild past the widget budget fails tests loudly and degr
2661
2873
  );
2662
2874
  // Recording still happened before the propagate.
2663
2875
  try std.testing.expectEqual(@as(usize, 1), harness.runtime.dispatchErrors().len);
2664
- try std.testing.expectEqualStrings("effects_wake", harness.runtime.dispatchErrors()[0].event);
2876
+ try std.testing.expectEqualStrings("effects_wake", harness.runtime.dispatchErrors()[0].event());
2665
2877
  try std.testing.expectEqualStrings("WidgetLayoutListFull", harness.runtime.dispatchErrors()[0].error_name);
2666
2878
 
2667
2879
  // Production policy: the same failure degrades — recorded in the
@@ -2720,7 +2932,7 @@ test "a stale automation widget click degrades instead of killing the frame call
2720
2932
  try harness.runtime.dispatchPlatformEvent(app, .frame_requested);
2721
2933
  const errors = harness.runtime.dispatchErrors();
2722
2934
  try std.testing.expectEqual(@as(usize, 1), errors.len);
2723
- try std.testing.expectEqualStrings("automation.widget_click", errors[0].event);
2935
+ try std.testing.expectEqualStrings("automation.widget_click", errors[0].event());
2724
2936
  try std.testing.expectEqualStrings("InvalidCommand", errors[0].error_name);
2725
2937
 
2726
2938
  // The app is still alive: a real click keeps dispatching.
@@ -3346,7 +3558,9 @@ test "ui app dispatches native context menu selections as typed messages" {
3346
3558
  .timestamp_ns = 2_000_000,
3347
3559
  } });
3348
3560
  try std.testing.expectEqual(@as(usize, 1), harness.null_platform.context_menu_request_count);
3349
- try std.testing.expectEqual(@as(u64, row_id), harness.null_platform.context_menu_token);
3561
+ // The request carries a minted per-request token (opaque to the
3562
+ // platform, which only echoes it back on the action event).
3563
+ try std.testing.expect(harness.null_platform.context_menu_token != 0);
3350
3564
  try std.testing.expectEqual(@as(usize, 3), harness.null_platform.contextMenuItems().len);
3351
3565
 
3352
3566
  // Selecting "Delete" (item id 3 = third declared entry) dispatches
@@ -3354,99 +3568,78 @@ test "ui app dispatches native context menu selections as typed messages" {
3354
3568
  try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
3355
3569
  .window_id = 1,
3356
3570
  .view_label = canvas_label,
3357
- .token = row_id,
3571
+ .token = harness.null_platform.context_menu_token,
3358
3572
  .item_id = 3,
3359
3573
  } });
3360
3574
  try std.testing.expectEqual(@as(u32, 1), app_state.model.deleted);
3361
3575
  try std.testing.expectEqual(@as(u32, 0), app_state.model.completed);
3362
3576
  }
3363
3577
 
3364
- // ------------------------------------------------- press fall-through fixture
3578
+ // --------------------------------------- context-menu rebuild-race fixture
3365
3579
 
3366
- const RowsModel = struct {
3367
- picked: u32 = 0,
3368
- picks: u32 = 0,
3369
- button_hits: u32 = 0,
3580
+ const ReorderModel = struct {
3581
+ reordered: bool = false,
3582
+ completed: u32 = 0,
3583
+ deleted: u32 = 0,
3370
3584
  };
3371
3585
 
3372
- const RowsMsg = union(enum) {
3373
- pick: u32,
3374
- button_hit,
3586
+ const ReorderMsg = union(enum) {
3587
+ reorder,
3588
+ complete,
3589
+ delete,
3375
3590
  };
3376
3591
 
3377
- const RowsApp = ui_app_model.UiApp(RowsModel, RowsMsg);
3592
+ const ReorderApp = ui_app_model.UiApp(ReorderModel, ReorderMsg);
3378
3593
 
3379
- fn rowsUpdate(model: *RowsModel, msg: RowsMsg) void {
3594
+ fn reorderUpdate(model: *ReorderModel, msg: ReorderMsg) void {
3380
3595
  switch (msg) {
3381
- .pick => |id| {
3382
- model.picked = id;
3383
- model.picks += 1;
3384
- },
3385
- .button_hit => model.button_hits += 1,
3596
+ .reorder => model.reordered = true,
3597
+ .complete => model.completed += 1,
3598
+ .delete => model.deleted += 1,
3386
3599
  }
3387
3600
  }
3388
3601
 
3389
- fn rowsView(ui: *RowsApp.Ui, model: *const RowsModel) RowsApp.Ui.Node {
3390
- _ = model;
3391
- // The showcase row shape the press fall-through exists for: pressable
3392
- // panels whose visible content is plain (selectable) text — no
3393
- // empty-text overlays, no duplicated handlers on the text leaves.
3602
+ fn reorderView(ui: *ReorderApp.Ui, model: *const ReorderModel) ReorderApp.Ui.Node {
3603
+ // The conditional-menu shape the rebuild race exists for: an effect
3604
+ // (here a button press standing in for a timer) reorders the row's
3605
+ // declared items while the OS menu is open.
3606
+ const before = [_]ReorderApp.Ui.ContextMenuItem{
3607
+ .{ .label = "Complete", .msg = .complete },
3608
+ .{ .separator = true },
3609
+ .{ .label = "Delete", .msg = .delete },
3610
+ };
3611
+ const after = [_]ReorderApp.Ui.ContextMenuItem{
3612
+ .{ .label = "Delete", .msg = .delete },
3613
+ .{ .separator = true },
3614
+ .{ .label = "Complete", .msg = .complete },
3615
+ };
3394
3616
  return ui.column(.{ .gap = 8, .padding = 12 }, .{
3395
- ui.panel(.{ .on_press = RowsMsg{ .pick = 1 }, .height = 48 }, .{
3396
- ui.row(.{ .gap = 8, .padding = 8 }, .{
3397
- ui.text(.{ .grow = 1 }, "Alpha row label"),
3398
- }),
3399
- }),
3400
- ui.panel(.{ .on_press = RowsMsg{ .pick = 2 }, .height = 48 }, .{
3401
- ui.row(.{ .gap = 8, .padding = 8 }, .{
3402
- ui.text(.{ .grow = 1 }, "Beta row label"),
3403
- ui.button(.{ .on_press = .button_hit }, "Open"),
3404
- }),
3405
- }),
3617
+ ui.el(.list_item, .{
3618
+ .text = "Ship the release",
3619
+ .context_menu = if (model.reordered) &after else &before,
3620
+ }, .{}),
3621
+ ui.button(.{ .on_press = .reorder }, "Reorder"),
3406
3622
  });
3407
3623
  }
3408
3624
 
3409
- fn rowsOptions() RowsApp.Options {
3410
- return .{
3411
- .name = "ui-app-rows",
3412
- .scene = counter_scene,
3413
- .canvas_label = canvas_label,
3414
- .update = rowsUpdate,
3415
- .view = rowsView,
3416
- };
3417
- }
3418
-
3419
- fn rowsWidgetCenter(runtime: *core.Runtime, id: canvas.ObjectId) !geometry.PointF {
3420
- const layout = try runtime.canvasWidgetLayout(1, canvas_label);
3421
- return layout.findById(id).?.frame.normalized().center();
3422
- }
3423
-
3424
- fn rowsPointer(kind: zero_platform.GpuSurfaceInputKind, point: geometry.PointF, timestamp_ns: u64) zero_platform.Event {
3425
- return .{ .gpu_surface_input = .{
3426
- .label = canvas_label,
3427
- .kind = kind,
3428
- .timestamp_ns = timestamp_ns,
3429
- .x = point.x,
3430
- .y = point.y,
3431
- } };
3432
- }
3433
-
3434
- fn rowsTextSelection(runtime: *core.Runtime, id: canvas.ObjectId) !?canvas.TextSelection {
3435
- const layout = try runtime.canvasWidgetLayout(1, canvas_label);
3436
- return layout.findById(id).?.widget.text_selection;
3437
- }
3438
-
3439
- test "presses on a pressable row's plain text land on the row, live and via automation" {
3625
+ test "a rebuild while the native menu is open never redirects the visible selection" {
3440
3626
  const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
3441
3627
  defer harness.destroy(std.testing.allocator);
3442
3628
  harness.null_platform.gpu_surfaces = true;
3443
3629
 
3444
- const app_state = try std.testing.allocator.create(RowsApp);
3630
+ const app_state = try std.testing.allocator.create(ReorderApp);
3445
3631
  defer std.testing.allocator.destroy(app_state);
3446
- app_state.* = RowsApp.init(std.heap.page_allocator, .{}, rowsOptions());
3632
+ app_state.* = ReorderApp.init(std.heap.page_allocator, .{}, .{
3633
+ .name = "ui-app-context-menu-race",
3634
+ .scene = counter_scene,
3635
+ .canvas_label = canvas_label,
3636
+ .update = reorderUpdate,
3637
+ .view = reorderView,
3638
+ });
3447
3639
  defer app_state.deinit();
3448
3640
  const app = app_state.app();
3449
3641
  try harness.start(app);
3642
+
3450
3643
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
3451
3644
  .label = canvas_label,
3452
3645
  .size = geometry.SizeF.init(400, 300),
@@ -3457,51 +3650,176 @@ test "presses on a pressable row's plain text land on the row, live and via auto
3457
3650
  } });
3458
3651
  try std.testing.expect(app_state.installed);
3459
3652
 
3460
- const tree = app_state.tree.?;
3461
- const alpha_row_id = tree.root.children[0].id;
3462
- const alpha_text_id = findIn(tree.root, .text, "Alpha row label").?;
3463
- const button_id = findIn(tree.root, .button, "Open").?;
3653
+ // Right-click the row: the platform presents [Complete, —, Delete]
3654
+ // and the pending request is armed with a minted token.
3655
+ const row_id = findIn(app_state.tree.?.root, .list_item, "Ship the release").?;
3656
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
3657
+ var row_frame: geometry.RectF = .{};
3658
+ var button_frame: geometry.RectF = .{};
3659
+ for (layout.nodes) |node| {
3660
+ if (node.widget.id == row_id) row_frame = node.frame;
3661
+ if (node.widget.kind == .button) button_frame = node.frame;
3662
+ }
3663
+ try std.testing.expect(!row_frame.isEmpty());
3664
+ try std.testing.expect(!button_frame.isEmpty());
3665
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3666
+ .window_id = 1,
3667
+ .label = canvas_label,
3668
+ .kind = .pointer_down,
3669
+ .button = 1,
3670
+ .x = row_frame.x + 4,
3671
+ .y = row_frame.y + 4,
3672
+ .timestamp_ns = 2_000_000,
3673
+ } });
3674
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.context_menu_request_count);
3675
+ const shown_token = harness.null_platform.context_menu_token;
3676
+ const recorded = harness.null_platform.contextMenuItems();
3677
+ try std.testing.expectEqualStrings("Complete", recorded[0].label);
3464
3678
 
3465
- // A live click on the row's plain text lands on the row's on_press.
3466
- const text_center = try rowsWidgetCenter(&harness.runtime, alpha_text_id);
3467
- try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_down, text_center, 2_000_000));
3468
- try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_up, text_center, 2_100_000));
3469
- try std.testing.expectEqual(@as(u32, 1), app_state.model.picks);
3470
- try std.testing.expectEqual(@as(u32, 1), app_state.model.picked);
3679
+ // The GTK popover is asynchronous: while it is open, a press
3680
+ // rebuilds the tree with the items REVERSED (a timer or effect
3681
+ // reordering conditional items behaves identically).
3682
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3683
+ .window_id = 1,
3684
+ .label = canvas_label,
3685
+ .kind = .pointer_down,
3686
+ .x = button_frame.x + 4,
3687
+ .y = button_frame.y + 4,
3688
+ .timestamp_ns = 3_000_000,
3689
+ } });
3690
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3691
+ .window_id = 1,
3692
+ .label = canvas_label,
3693
+ .kind = .pointer_up,
3694
+ .x = button_frame.x + 4,
3695
+ .y = button_frame.y + 4,
3696
+ .timestamp_ns = 4_000_000,
3697
+ } });
3698
+ try std.testing.expect(app_state.model.reordered);
3471
3699
 
3472
- // A button inside a pressable row claims its own press — the button
3473
- // wins, the row does not fire.
3474
- const button_center = try rowsWidgetCenter(&harness.runtime, button_id);
3475
- try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_down, button_center, 3_000_000));
3476
- try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_up, button_center, 3_100_000));
3477
- try std.testing.expectEqual(@as(u32, 1), app_state.model.button_hits);
3478
- try std.testing.expectEqual(@as(u32, 1), app_state.model.picks);
3700
+ // The user picks the FIRST visible item — the menu still shows
3701
+ // "Complete" there. Resolution must come from the presented menu's
3702
+ // snapshot, never the rebuilt live tree (whose index 0 is now
3703
+ // "Delete").
3704
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
3705
+ .window_id = 1,
3706
+ .view_label = canvas_label,
3707
+ .token = shown_token,
3708
+ .item_id = 1,
3709
+ } });
3710
+ try std.testing.expectEqual(@as(u32, 0), app_state.model.deleted);
3711
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.completed);
3479
3712
 
3480
- // The automation click path agrees with the live hit test: clicking
3481
- // the row id (whose center is covered by its text) and clicking the
3482
- // text id both land on the row.
3483
- var command_buffer: [96]u8 = undefined;
3484
- const row_click = try std.fmt.bufPrint(&command_buffer, "widget-click {s} {d}", .{ canvas_label, alpha_row_id });
3485
- try harness.runtime.dispatchAutomationCommand(app, row_click);
3486
- try std.testing.expectEqual(@as(u32, 2), app_state.model.picks);
3487
- var text_command_buffer: [96]u8 = undefined;
3488
- const text_click = try std.fmt.bufPrint(&text_command_buffer, "widget-click {s} {d}", .{ canvas_label, alpha_text_id });
3489
- try harness.runtime.dispatchAutomationCommand(app, text_click);
3490
- try std.testing.expectEqual(@as(u32, 3), app_state.model.picks);
3491
- try std.testing.expectEqual(@as(u32, 1), app_state.model.button_hits);
3713
+ // Dismissal after a rebuild stays inert, and the consumed token
3714
+ // cannot resolve again.
3715
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
3716
+ .window_id = 1,
3717
+ .view_label = canvas_label,
3718
+ .token = shown_token,
3719
+ .item_id = 0,
3720
+ } });
3721
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
3722
+ .window_id = 1,
3723
+ .view_label = canvas_label,
3724
+ .token = shown_token,
3725
+ .item_id = 1,
3726
+ } });
3727
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.completed);
3728
+ try std.testing.expectEqual(@as(u32, 0), app_state.model.deleted);
3492
3729
  }
3493
3730
 
3494
- test "selection drag inside a pressable row selects without pressing; a click still presses" {
3731
+ // ------------------------------------- context-menu arena-payload fixture
3732
+
3733
+ const ArenaPayloadModel = struct {
3734
+ generation: u32 = 0,
3735
+ sends: u32 = 0,
3736
+ received_storage: [64]u8 = undefined,
3737
+ received_len: usize = 0,
3738
+ /// Address of the dispatched payload's first byte: the pinned-arena
3739
+ /// design promises the ORIGINAL slice, so native selection carries
3740
+ /// the same pointer identity the fallback surface and the
3741
+ /// automation verb dispatch.
3742
+ received_ptr: usize = 0,
3743
+ };
3744
+
3745
+ const ArenaPayloadMsg = union(enum) {
3746
+ bump,
3747
+ send: []const u8,
3748
+ /// The error-union shape: pinning is shape-blind (the ORIGINAL
3749
+ /// value dispatches), so a payload behind an error union needs no
3750
+ /// special handling — this arm proves it end to end.
3751
+ send_result: error{Overloaded}![]const u8,
3752
+ };
3753
+
3754
+ const ArenaPayloadApp = ui_app_model.UiApp(ArenaPayloadModel, ArenaPayloadMsg);
3755
+
3756
+ fn arenaPayloadUpdate(model: *ArenaPayloadModel, msg: ArenaPayloadMsg) void {
3757
+ switch (msg) {
3758
+ .bump => model.generation += 1,
3759
+ .send => |bytes| recordArenaPayload(model, bytes),
3760
+ .send_result => |result| recordArenaPayload(model, result catch &.{}),
3761
+ }
3762
+ }
3763
+
3764
+ fn recordArenaPayload(model: *ArenaPayloadModel, bytes: []const u8) void {
3765
+ const len = @min(bytes.len, model.received_storage.len);
3766
+ @memcpy(model.received_storage[0..len], bytes[0..len]);
3767
+ model.received_len = len;
3768
+ model.received_ptr = @intFromPtr(bytes.ptr);
3769
+ model.sends += 1;
3770
+ }
3771
+
3772
+ fn arenaPayloadView(ui: *ArenaPayloadApp.Ui, model: *const ArenaPayloadModel) ArenaPayloadApp.Ui.Node {
3773
+ // Keep the build arena a single address-stable chunk: the first
3774
+ // allocation of every build is a slab larger than the whole build,
3775
+ // filled with sentinel bytes, so every later allocation lands at
3776
+ // the same offset build after build. Under the pinned-arena design
3777
+ // nothing here is ever overwritten while the menu is open; if the
3778
+ // pin regresses, this determinism makes the failure exact — the
3779
+ // reset arena rewrites the payload's storage with the next
3780
+ // generation's bytes.
3781
+ const slab = ui.arena.alloc(u8, 256 * 1024) catch @panic("arena slab");
3782
+ @memset(slab, '!');
3783
+ // The documented Msg-payload shape: a display string formatted into
3784
+ // the BUILD ARENA and carried by a context-menu item's Msg. Every
3785
+ // build formats a same-length, generation-stamped payload, so after
3786
+ // the arena reset the next build's bytes land exactly where a stale
3787
+ // present-time slice points.
3788
+ const payload = ui.fmt("payload-gen-{d:0>4}", .{model.generation});
3789
+ return ui.column(.{ .gap = 8, .padding = 12 }, .{
3790
+ ui.el(.list_item, .{
3791
+ .text = "Ship the release",
3792
+ .context_menu = &.{
3793
+ .{ .label = "Send", .msg = .{ .send = payload } },
3794
+ .{ .label = "Send result", .msg = .{ .send_result = payload } },
3795
+ },
3796
+ }, .{}),
3797
+ ui.button(.{ .on_press = .bump }, "Rebuild"),
3798
+ ui.el(.text_field, .{ .text = "notes", .width = 200 }, .{}),
3799
+ });
3800
+ }
3801
+
3802
+ test "context menu selection dispatches the original arena payload across rebuilds (pinned build arena)" {
3495
3803
  const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
3496
3804
  defer harness.destroy(std.testing.allocator);
3497
3805
  harness.null_platform.gpu_surfaces = true;
3498
3806
 
3499
- const app_state = try std.testing.allocator.create(RowsApp);
3807
+ // The leak-detecting backing allocator is part of the assertion:
3808
+ // every snapshot copy must be freed — superseded, dismissed, or
3809
+ // still armed at teardown.
3810
+ const app_state = try std.testing.allocator.create(ArenaPayloadApp);
3500
3811
  defer std.testing.allocator.destroy(app_state);
3501
- app_state.* = RowsApp.init(std.heap.page_allocator, .{}, rowsOptions());
3812
+ app_state.* = ArenaPayloadApp.init(std.testing.allocator, .{}, .{
3813
+ .name = "ui-app-context-menu-arena",
3814
+ .scene = counter_scene,
3815
+ .canvas_label = canvas_label,
3816
+ .update = arenaPayloadUpdate,
3817
+ .view = arenaPayloadView,
3818
+ });
3502
3819
  defer app_state.deinit();
3503
3820
  const app = app_state.app();
3504
3821
  try harness.start(app);
3822
+
3505
3823
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
3506
3824
  .label = canvas_label,
3507
3825
  .size = geometry.SizeF.init(400, 300),
@@ -3510,100 +3828,277 @@ test "selection drag inside a pressable row selects without pressing; a click st
3510
3828
  .timestamp_ns = 1_000_000,
3511
3829
  .nonblank = true,
3512
3830
  } });
3831
+ try std.testing.expect(app_state.installed);
3513
3832
 
3514
- const alpha_text_id = findIn(app_state.tree.?.root, .text, "Alpha row label").?;
3833
+ const row_id = findIn(app_state.tree.?.root, .list_item, "Ship the release").?;
3515
3834
  const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
3516
- const text_frame = layout.findById(alpha_text_id).?.frame.normalized();
3517
- const start = geometry.PointF.init(text_frame.x + 2, text_frame.center().y);
3518
- const end = geometry.PointF.init(text_frame.x + text_frame.width * 0.7, text_frame.center().y);
3835
+ var row_frame: geometry.RectF = .{};
3836
+ var button_frame: geometry.RectF = .{};
3837
+ for (layout.nodes) |node| {
3838
+ if (node.widget.id == row_id) row_frame = node.frame;
3839
+ if (node.widget.kind == .button) button_frame = node.frame;
3840
+ }
3841
+ try std.testing.expect(!row_frame.isEmpty());
3842
+ try std.testing.expect(!button_frame.isEmpty());
3843
+
3844
+ // Round 1 — the plain const slice. Present the menu (GTK popovers
3845
+ // are asynchronous: it stays on the glass while the app keeps
3846
+ // rebuilding underneath), then rebuild TWICE: view builds
3847
+ // double-buffer two arenas, so the second rebuild resets the arena
3848
+ // the presented tree was built in and overwrites the payload's
3849
+ // storage with the next generation's bytes.
3850
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3851
+ .window_id = 1,
3852
+ .label = canvas_label,
3853
+ .kind = .pointer_down,
3854
+ .button = 1,
3855
+ .x = row_frame.x + 4,
3856
+ .y = row_frame.y + 4,
3857
+ .timestamp_ns = 2_000_000,
3858
+ } });
3859
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.context_menu_request_count);
3860
+ const shown_token = harness.null_platform.context_menu_token;
3861
+ try std.testing.expect(shown_token != 0);
3862
+ // The presented build's arena is pinned while the request is
3863
+ // pending, so the eventual dispatch is the ORIGINAL Msg value —
3864
+ // capture its payload address at present time.
3865
+ try std.testing.expect(app_state.context_menu_pin != null);
3866
+ const presented_send = app_state.tree.?.msgForContextMenu(row_id, 0).?;
3867
+ const presented_send_ptr = @intFromPtr(presented_send.send.ptr);
3868
+
3869
+ for (0..2) |press| {
3870
+ const base: u64 = 3_000_000 + @as(u64, @intCast(press)) * 2_000_000;
3871
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3872
+ .window_id = 1,
3873
+ .label = canvas_label,
3874
+ .kind = .pointer_down,
3875
+ .x = button_frame.x + 4,
3876
+ .y = button_frame.y + 4,
3877
+ .timestamp_ns = base,
3878
+ } });
3879
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3880
+ .window_id = 1,
3881
+ .label = canvas_label,
3882
+ .kind = .pointer_up,
3883
+ .x = button_frame.x + 4,
3884
+ .y = button_frame.y + 4,
3885
+ .timestamp_ns = base + 1_000_000,
3886
+ } });
3887
+ }
3888
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.generation);
3519
3889
 
3520
- // Down + drag + up: the gesture selects text within the row and
3521
- // presses NOTHING — dragging selects, clicking presses.
3522
- try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_down, start, 2_000_000));
3523
- try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_drag, end, 2_050_000));
3524
- try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_up, end, 2_100_000));
3525
- const selection = (try rowsTextSelection(&harness.runtime, alpha_text_id)).?;
3526
- try std.testing.expect(!selection.isCollapsed("Alpha row label".len));
3527
- try std.testing.expectEqual(@as(u32, 0), app_state.model.picks);
3890
+ // The user picks "Send": the dispatched Msg must carry the bytes
3891
+ // the user SAW at present time, never whatever the reset build
3892
+ // arena holds at selection time.
3893
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
3894
+ .window_id = 1,
3895
+ .view_label = canvas_label,
3896
+ .token = shown_token,
3897
+ .item_id = 1,
3898
+ } });
3899
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.sends);
3900
+ try std.testing.expectEqualStrings("payload-gen-0000", app_state.model.received_storage[0..app_state.model.received_len]);
3901
+ // Pointer identity: the dispatched slice IS the presented slice —
3902
+ // the same value the fallback surface or the automation verb would
3903
+ // have dispatched — and the resolved request released the pin.
3904
+ try std.testing.expectEqual(presented_send_ptr, app_state.model.received_ptr);
3905
+ try std.testing.expect(app_state.context_menu_pin == null);
3906
+
3907
+ // Round 2 — the error-union payload: pinning is shape-blind, so
3908
+ // the same two-rebuild race dispatches the presented generation's
3909
+ // bytes (and address), not the arena's current ones.
3910
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3911
+ .window_id = 1,
3912
+ .label = canvas_label,
3913
+ .kind = .pointer_down,
3914
+ .button = 1,
3915
+ .x = row_frame.x + 4,
3916
+ .y = row_frame.y + 4,
3917
+ .timestamp_ns = 8_000_000,
3918
+ } });
3919
+ try std.testing.expectEqual(@as(usize, 2), harness.null_platform.context_menu_request_count);
3920
+ const result_token = harness.null_platform.context_menu_token;
3921
+ try std.testing.expect(result_token != shown_token);
3922
+ const presented_result = app_state.tree.?.msgForContextMenu(row_id, 1).?;
3923
+ const presented_result_ptr = @intFromPtr((presented_result.send_result catch unreachable).ptr);
3924
+
3925
+ for (0..2) |press| {
3926
+ const base: u64 = 9_000_000 + @as(u64, @intCast(press)) * 2_000_000;
3927
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3928
+ .window_id = 1,
3929
+ .label = canvas_label,
3930
+ .kind = .pointer_down,
3931
+ .x = button_frame.x + 4,
3932
+ .y = button_frame.y + 4,
3933
+ .timestamp_ns = base,
3934
+ } });
3935
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3936
+ .window_id = 1,
3937
+ .label = canvas_label,
3938
+ .kind = .pointer_up,
3939
+ .x = button_frame.x + 4,
3940
+ .y = button_frame.y + 4,
3941
+ .timestamp_ns = base + 1_000_000,
3942
+ } });
3943
+ }
3944
+ try std.testing.expectEqual(@as(u32, 4), app_state.model.generation);
3528
3945
 
3529
- // A plain click on the same text collapses the selection on the way
3530
- // down and the press lands on the row.
3531
- const center = try rowsWidgetCenter(&harness.runtime, alpha_text_id);
3532
- try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_down, center, 3_000_000));
3533
- try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_up, center, 3_100_000));
3534
- try std.testing.expectEqual(@as(u32, 1), app_state.model.picks);
3535
- try std.testing.expectEqual(@as(u32, 1), app_state.model.picked);
3946
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
3947
+ .window_id = 1,
3948
+ .view_label = canvas_label,
3949
+ .token = result_token,
3950
+ .item_id = 2,
3951
+ } });
3952
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.sends);
3953
+ try std.testing.expectEqualStrings("payload-gen-0002", app_state.model.received_storage[0..app_state.model.received_len]);
3954
+ try std.testing.expectEqual(presented_result_ptr, app_state.model.received_ptr);
3955
+ try std.testing.expect(app_state.context_menu_pin == null);
3956
+
3957
+ // Round 3 — supersession and dismissal: a re-presented menu
3958
+ // replaces the previous snapshot and pin, and the runtime's
3959
+ // dismissed notice releases the successor's — an abandoned menu
3960
+ // must not exempt an arena generation from resets forever.
3961
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3962
+ .window_id = 1,
3963
+ .label = canvas_label,
3964
+ .kind = .pointer_down,
3965
+ .button = 1,
3966
+ .x = row_frame.x + 4,
3967
+ .y = row_frame.y + 4,
3968
+ .timestamp_ns = 15_000_000,
3969
+ } });
3970
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3971
+ .window_id = 1,
3972
+ .label = canvas_label,
3973
+ .kind = .pointer_down,
3974
+ .button = 1,
3975
+ .x = row_frame.x + 4,
3976
+ .y = row_frame.y + 4,
3977
+ .timestamp_ns = 16_000_000,
3978
+ } });
3979
+ try std.testing.expectEqual(@as(usize, 4), harness.null_platform.context_menu_request_count);
3980
+ const superseding_token = harness.null_platform.context_menu_token;
3981
+ try std.testing.expect(app_state.context_menu_pin != null);
3982
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
3983
+ .window_id = 1,
3984
+ .view_label = canvas_label,
3985
+ .token = superseding_token,
3986
+ .item_id = 0,
3987
+ } });
3988
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.sends);
3989
+ try std.testing.expect(app_state.context_menu_pin == null);
3536
3990
  }
3537
3991
 
3538
- // --------------------------------------------------------------- on_chrome
3539
-
3540
- const ChromeInsetModel = struct {
3541
- leading: f32 = 0,
3542
- top: f32 = 0,
3543
- /// The traffic-light cluster's vertical centerline — what a header
3544
- /// centers its controls against in the tall band.
3545
- buttons_center_y: f32 = 0,
3546
- deliveries: u32 = 0,
3547
- };
3548
-
3549
- const ChromeInsetMsg = union(enum) {
3550
- chrome: zero_platform.WindowChrome,
3551
- };
3992
+ test "any superseding presentation or dispatch releases the app menu's snapshot and pin" {
3993
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
3994
+ defer harness.destroy(std.testing.allocator);
3995
+ harness.null_platform.gpu_surfaces = true;
3552
3996
 
3553
- const ChromeInsetApp = ui_app_model.UiApp(ChromeInsetModel, ChromeInsetMsg);
3997
+ const app_state = try std.testing.allocator.create(ArenaPayloadApp);
3998
+ defer std.testing.allocator.destroy(app_state);
3999
+ app_state.* = ArenaPayloadApp.init(std.testing.allocator, .{}, .{
4000
+ .name = "ui-app-context-menu-supersede",
4001
+ .scene = counter_scene,
4002
+ .canvas_label = canvas_label,
4003
+ .update = arenaPayloadUpdate,
4004
+ .view = arenaPayloadView,
4005
+ });
4006
+ defer app_state.deinit();
4007
+ const app = app_state.app();
4008
+ try harness.start(app);
4009
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
4010
+ .label = canvas_label,
4011
+ .size = geometry.SizeF.init(400, 300),
4012
+ .scale_factor = 2,
4013
+ .frame_index = 1,
4014
+ .timestamp_ns = 1_000_000,
4015
+ .nonblank = true,
4016
+ } });
4017
+ try std.testing.expect(app_state.installed);
3554
4018
 
3555
- fn chromeInsetUpdate(model: *ChromeInsetModel, msg: ChromeInsetMsg) void {
3556
- switch (msg) {
3557
- .chrome => |chrome| {
3558
- model.leading = chrome.insets.left;
3559
- model.top = chrome.insets.top;
3560
- model.buttons_center_y = chrome.buttons.y + chrome.buttons.height / 2;
3561
- model.deliveries += 1;
3562
- },
4019
+ const row_id = findIn(app_state.tree.?.root, .list_item, "Ship the release").?;
4020
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
4021
+ var row_frame: geometry.RectF = .{};
4022
+ var field_frame: geometry.RectF = .{};
4023
+ for (layout.nodes) |node| {
4024
+ if (node.widget.id == row_id) row_frame = node.frame;
4025
+ if (node.widget.kind == .text_field) field_frame = node.frame;
3563
4026
  }
3564
- }
4027
+ try std.testing.expect(!row_frame.isEmpty());
4028
+ try std.testing.expect(!field_frame.isEmpty());
3565
4029
 
3566
- fn chromeInsetView(ui: *ChromeInsetApp.Ui, model: *const ChromeInsetModel) ChromeInsetApp.Ui.Node {
3567
- return ui.column(.{ .gap = 8 }, .{
3568
- ui.row(.{ .window_drag = true, .height = 40 }, .{
3569
- ui.el(.stack, .{ .width = model.leading }, .{}),
3570
- ui.text(.{}, "Header"),
3571
- }),
3572
- ui.text(.{}, ui.fmt("leading {d}", .{model.leading})),
3573
- });
3574
- }
4030
+ // Present the row's app menu: snapshot armed, build generation
4031
+ // pinned.
4032
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4033
+ .window_id = 1,
4034
+ .label = canvas_label,
4035
+ .kind = .pointer_down,
4036
+ .button = 1,
4037
+ .x = row_frame.x + 4,
4038
+ .y = row_frame.y + 4,
4039
+ .timestamp_ns = 2_000_000,
4040
+ } });
4041
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.context_menu_request_count);
4042
+ try std.testing.expect(app_state.context_menu_pin != null);
4043
+ try std.testing.expect(app_state.context_menu_shown_token != 0);
3575
4044
 
3576
- fn chromeInsetMap(chrome: zero_platform.WindowChrome) ?ChromeInsetMsg {
3577
- return .{ .chrome = chrome };
4045
+ // A right-click on the editable field presents the DEFAULT edit
4046
+ // menu — a different pending kind, but it supersedes the app
4047
+ // menu's request all the same, so the snapshot and pin release.
4048
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4049
+ .window_id = 1,
4050
+ .label = canvas_label,
4051
+ .kind = .pointer_down,
4052
+ .button = 1,
4053
+ .x = field_frame.x + 4,
4054
+ .y = field_frame.y + 4,
4055
+ .timestamp_ns = 3_000_000,
4056
+ } });
4057
+ try std.testing.expectEqual(@as(usize, 2), harness.null_platform.context_menu_request_count);
4058
+ try std.testing.expect(app_state.context_menu_pin == null);
4059
+ try std.testing.expectEqual(@as(u64, 0), app_state.context_menu_shown_token);
4060
+
4061
+ // Re-present the app menu, then drive the item through the
4062
+ // automation verb: its direct dispatch supersedes the open
4063
+ // presentation (releasing snapshot and pin) and resolves against
4064
+ // the live tree.
4065
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4066
+ .window_id = 1,
4067
+ .label = canvas_label,
4068
+ .kind = .pointer_down,
4069
+ .button = 1,
4070
+ .x = row_frame.x + 4,
4071
+ .y = row_frame.y + 4,
4072
+ .timestamp_ns = 4_000_000,
4073
+ } });
4074
+ try std.testing.expectEqual(@as(usize, 3), harness.null_platform.context_menu_request_count);
4075
+ try std.testing.expect(app_state.context_menu_pin != null);
4076
+ var command_buffer: [96]u8 = undefined;
4077
+ const command = try std.fmt.bufPrint(&command_buffer, "widget-context-menu {s} {d} 0", .{ canvas_label, row_id });
4078
+ try harness.runtime.dispatchAutomationCommand(app, command);
4079
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.sends);
4080
+ try std.testing.expectEqualStrings("payload-gen-0000", app_state.model.received_storage[0..app_state.model.received_len]);
4081
+ try std.testing.expect(app_state.context_menu_pin == null);
4082
+ try std.testing.expectEqual(@as(u64, 0), app_state.context_menu_shown_token);
3578
4083
  }
3579
4084
 
3580
- test "ui app delivers chrome overlay geometry before install and on change" {
4085
+ test "rebuilds under an open menu stay bounded at two trees" {
3581
4086
  const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
3582
4087
  defer harness.destroy(std.testing.allocator);
3583
4088
  harness.null_platform.gpu_surfaces = true;
3584
- // Model a tall hidden-titlebar macOS host: unified band 52pt tall,
3585
- // traffic lights ending 78pt in and vertically centered in the band.
3586
- harness.null_platform.window_chrome = .{
3587
- .insets = .{ .top = 52, .left = 78 },
3588
- .buttons = geometry.RectF.init(12, 19, 54, 14),
3589
- };
3590
4089
 
3591
- const app_state = try std.testing.allocator.create(ChromeInsetApp);
4090
+ const app_state = try std.testing.allocator.create(ArenaPayloadApp);
3592
4091
  defer std.testing.allocator.destroy(app_state);
3593
- app_state.* = ChromeInsetApp.init(std.heap.page_allocator, .{}, .{
3594
- .name = "ui-app-chrome",
4092
+ app_state.* = ArenaPayloadApp.init(std.testing.allocator, .{}, .{
4093
+ .name = "ui-app-context-menu-bounded",
3595
4094
  .scene = counter_scene,
3596
4095
  .canvas_label = canvas_label,
3597
- .update = chromeInsetUpdate,
3598
- .view = chromeInsetView,
3599
- .on_chrome = chromeInsetMap,
4096
+ .update = arenaPayloadUpdate,
4097
+ .view = arenaPayloadView,
3600
4098
  });
3601
4099
  defer app_state.deinit();
3602
4100
  const app = app_state.app();
3603
4101
  try harness.start(app);
3604
-
3605
- // The insets land in the model BEFORE the first view build, so the
3606
- // installing frame already renders the padded header.
3607
4102
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
3608
4103
  .label = canvas_label,
3609
4104
  .size = geometry.SizeF.init(400, 300),
@@ -3612,151 +4107,152 @@ test "ui app delivers chrome overlay geometry before install and on change" {
3612
4107
  .timestamp_ns = 1_000_000,
3613
4108
  .nonblank = true,
3614
4109
  } });
3615
- try std.testing.expect(app_state.installed);
3616
- try std.testing.expectEqual(@as(u32, 1), app_state.model.deliveries);
3617
- try std.testing.expectEqual(@as(f32, 78), app_state.model.leading);
3618
- try std.testing.expectEqual(@as(f32, 52), app_state.model.top);
3619
- // The buttons frame carries the vertical truth: centerline at
3620
- // 19 + 14/2 = 26, the tall band's midpoint.
3621
- try std.testing.expectEqual(@as(f32, 26), app_state.model.buttons_center_y);
3622
- try std.testing.expect(try retainedTextExists(&harness.runtime, "leading 78"));
3623
4110
 
3624
- // A resize with unchanged insets dispatches nothing extra.
3625
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_resized = .{
3626
- .window_id = 1,
3627
- .label = canvas_label,
3628
- .frame = geometry.RectF.init(0, 0, 640, 480),
3629
- .scale_factor = 2,
3630
- } });
3631
- try std.testing.expectEqual(@as(u32, 1), app_state.model.deliveries);
4111
+ const row_id = findIn(app_state.tree.?.root, .list_item, "Ship the release").?;
4112
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
4113
+ var row_frame: geometry.RectF = .{};
4114
+ var button_frame: geometry.RectF = .{};
4115
+ for (layout.nodes) |node| {
4116
+ if (node.widget.id == row_id) row_frame = node.frame;
4117
+ if (node.widget.kind == .button) button_frame = node.frame;
4118
+ }
3632
4119
 
3633
- // Fullscreen hides the chrome: the resize that accompanies the
3634
- // transition re-queries and delivers the zeroed geometry.
3635
- harness.null_platform.window_chrome = .{};
3636
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_resized = .{
4120
+ // Present the menu and hold it open for the whole test.
4121
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3637
4122
  .window_id = 1,
3638
4123
  .label = canvas_label,
3639
- .frame = geometry.RectF.init(0, 0, 1440, 900),
3640
- .scale_factor = 2,
4124
+ .kind = .pointer_down,
4125
+ .button = 1,
4126
+ .x = row_frame.x + 4,
4127
+ .y = row_frame.y + 4,
4128
+ .timestamp_ns = 2_000_000,
3641
4129
  } });
3642
- try std.testing.expectEqual(@as(u32, 2), app_state.model.deliveries);
3643
- try std.testing.expectEqual(@as(f32, 0), app_state.model.leading);
3644
- try std.testing.expectEqual(@as(f32, 0), app_state.model.buttons_center_y);
3645
- try std.testing.expect(try retainedTextExists(&harness.runtime, "leading 0"));
4130
+ try std.testing.expect(app_state.context_menu_pin != null);
4131
+
4132
+ const press = struct {
4133
+ fn once(h: *core.TestHarness(), a: core.App, frame: geometry.RectF, base: u64) !void {
4134
+ try h.runtime.dispatchPlatformEvent(a, .{ .gpu_surface_input = .{
4135
+ .window_id = 1,
4136
+ .label = canvas_label,
4137
+ .kind = .pointer_down,
4138
+ .x = frame.x + 4,
4139
+ .y = frame.y + 4,
4140
+ .timestamp_ns = base,
4141
+ } });
4142
+ try h.runtime.dispatchPlatformEvent(a, .{ .gpu_surface_input = .{
4143
+ .window_id = 1,
4144
+ .label = canvas_label,
4145
+ .kind = .pointer_up,
4146
+ .x = frame.x + 4,
4147
+ .y = frame.y + 4,
4148
+ .timestamp_ns = base + 1_000_000,
4149
+ } });
4150
+ }
4151
+ };
4152
+
4153
+ // Warm the partner arena's capacity to its fixed point, then drive
4154
+ // many more rebuilds: while the pin freezes the presented
4155
+ // generation, every rebuild routes through the partner with its
4156
+ // normal reset cadence, so total build storage holds at exactly
4157
+ // two trees — capacity must not grow with the rebuild count.
4158
+ for (0..4) |index| {
4159
+ try press.once(harness, app, button_frame, 3_000_000 + @as(u64, @intCast(index)) * 2_000_000);
4160
+ }
4161
+ const warmed = app_state.arenas[0].queryCapacity() + app_state.arenas[1].queryCapacity();
4162
+ for (0..10) |index| {
4163
+ try press.once(harness, app, button_frame, 20_000_000 + @as(u64, @intCast(index)) * 2_000_000);
4164
+ }
4165
+ try std.testing.expectEqual(@as(u32, 14), app_state.model.generation);
4166
+ try std.testing.expectEqual(warmed, app_state.arenas[0].queryCapacity() + app_state.arenas[1].queryCapacity());
4167
+ try std.testing.expect(app_state.context_menu_pin != null);
3646
4168
  }
3647
4169
 
3648
- // ---------------------------------- window-control clearance fixtures
4170
+ // ---------------------------------------- pinned rebuild failure recovery
3649
4171
 
3650
- const CaptionModel = struct {
3651
- /// The trailing chrome reservation the CONTRACT app consumes (the
3652
- /// soundboard pattern: a spacer sized to `insets.right`); the naive
3653
- /// app leaves it unwired at 0, exactly like an app that never
3654
- /// subscribed to the chrome channel.
3655
- trailing: f32 = 0,
4172
+ const PinFailureModel = struct {
4173
+ row_count: usize = 4,
4174
+ sends: u32 = 0,
4175
+ received_storage: [64]u8 = undefined,
4176
+ received_len: usize = 0,
4177
+
4178
+ pub fn rows(model: *const PinFailureModel, arena: std.mem.Allocator) []const usize {
4179
+ const out = arena.alloc(usize, model.row_count) catch return &.{};
4180
+ for (out, 0..) |*slot, index| slot.* = index;
4181
+ return out;
4182
+ }
3656
4183
  };
3657
4184
 
3658
- const CaptionMsg = union(enum) {
3659
- chrome: zero_platform.WindowChrome,
4185
+ const PinFailureMsg = union(enum) {
4186
+ set_rows: usize,
4187
+ send: []const u8,
3660
4188
  };
3661
4189
 
3662
- const CaptionApp = ui_app_model.UiApp(CaptionModel, CaptionMsg);
4190
+ const PinFailureApp = ui_app_model.UiApp(PinFailureModel, PinFailureMsg);
3663
4191
 
3664
- fn captionUpdate(model: *CaptionModel, msg: CaptionMsg) void {
4192
+ fn pinFailureUpdate(model: *PinFailureModel, msg: PinFailureMsg) void {
3665
4193
  switch (msg) {
3666
- .chrome => |chrome| model.trailing = chrome.insets.right,
4194
+ .set_rows => |count| model.row_count = count,
4195
+ .send => |bytes| {
4196
+ const len = @min(bytes.len, model.received_storage.len);
4197
+ @memcpy(model.received_storage[0..len], bytes[0..len]);
4198
+ model.received_len = len;
4199
+ model.sends += 1;
4200
+ },
3667
4201
  }
3668
4202
  }
3669
4203
 
3670
- /// A hidden-titlebar header that never consumed the chrome channel:
3671
- /// title leading, status text trailing — the system-monitor shape whose
3672
- /// status line rendered UNDER the Windows caption buttons.
3673
- fn captionNaiveView(ui: *CaptionApp.Ui, model: *const CaptionModel) CaptionApp.Ui.Node {
3674
- _ = model;
3675
- return ui.column(.{}, .{
3676
- ui.row(.{ .window_drag = true, .height = 40 }, .{
3677
- ui.text(.{}, "Monitor"),
3678
- ui.el(.stack, .{ .grow = 1 }, .{}),
3679
- ui.text(.{}, "sampling"),
3680
- }),
3681
- ui.text(.{}, "body"),
3682
- });
4204
+ fn pinFailureKey(index: *const usize) canvas.UiKey {
4205
+ return canvas.uiKey(@as(u64, index.*));
3683
4206
  }
3684
4207
 
3685
- /// The documented contract shape (the soundboard pattern): the header
3686
- /// ends with a spacer sized to `insets.right`, so its own content
3687
- /// already clears the caption cluster and the runtime reservation must
3688
- /// stay out of the way.
3689
- fn captionContractView(ui: *CaptionApp.Ui, model: *const CaptionModel) CaptionApp.Ui.Node {
3690
- return ui.column(.{}, .{
3691
- ui.row(.{ .window_drag = true, .height = 40 }, .{
3692
- ui.text(.{}, "Monitor"),
3693
- ui.el(.stack, .{ .grow = 1 }, .{}),
3694
- ui.text(.{}, "sampling"),
3695
- ui.el(.stack, .{ .width = model.trailing }, .{}),
3696
- }),
3697
- ui.text(.{}, "body"),
3698
- });
4208
+ fn pinFailureRow(ui: *PinFailureApp.Ui, index: *const usize) PinFailureApp.Ui.Node {
4209
+ return ui.text(.{}, ui.fmt("Row {d}", .{index.*}));
3699
4210
  }
3700
4211
 
3701
- /// The centered-title pattern: ONE grow text spanning the header row.
3702
- /// Its FRAME runs under the caption cluster, but the centered glyphs
3703
- /// sit well clear of it — nothing here needs (or may pay for) the
3704
- /// clearance retry.
3705
- fn captionCenteredView(ui: *CaptionApp.Ui, model: *const CaptionModel) CaptionApp.Ui.Node {
3706
- _ = model;
3707
- return ui.column(.{}, .{
3708
- ui.row(.{ .window_drag = true, .height = 40 }, .{
3709
- ui.text(.{ .grow = 1, .text_alignment = .center }, "Monitor"),
3710
- }),
3711
- ui.text(.{}, "body"),
4212
+ fn pinFailureView(ui: *PinFailureApp.Ui, model: *const PinFailureModel) PinFailureApp.Ui.Node {
4213
+ const payload = ui.fmt("payload-rows-{d:0>4}", .{model.row_count});
4214
+ return ui.column(.{ .gap = 2, .padding = 12 }, .{
4215
+ ui.el(.list_item, .{
4216
+ .text = "Ship the release",
4217
+ .context_menu = &.{
4218
+ .{ .label = "Send", .msg = .{ .send = payload } },
4219
+ // The poison item: its update pushes the roster far past
4220
+ // the per-view widget budget, so the selection's rebuild
4221
+ // fails.
4222
+ .{ .label = "Grow", .msg = .{ .set_rows = core.max_canvas_widget_nodes_per_view + 40 } },
4223
+ },
4224
+ }, .{}),
4225
+ ui.button(.{ .on_press = PinFailureMsg{ .set_rows = 4 } }, "Shrink"),
4226
+ ui.column(.{ .gap = 2 }, ui.each(model.rows(ui.arena), pinFailureKey, pinFailureRow)),
3712
4227
  });
3713
4228
  }
3714
4229
 
3715
- fn captionChromeMap(chrome: zero_platform.WindowChrome) ?CaptionMsg {
3716
- return .{ .chrome = chrome };
3717
- }
3718
-
3719
- fn captionTextFrame(runtime: *core.Runtime, text: []const u8) !geometry.RectF {
3720
- const layout = try runtime.canvasWidgetLayout(1, canvas_label);
3721
- for (layout.nodes) |node| {
3722
- if (node.widget.kind == .text and std.mem.eql(u8, node.widget.text, text)) return node.frame;
3723
- }
3724
- return error.TestUnexpectedResult;
3725
- }
4230
+ test "a failing rebuild routed into the live arena under an open menu drops the tree instead of dangling" {
4231
+ // The failing layout warns through std.log (the teaching diagnostic
4232
+ // under test would otherwise fail the build runner's stderr check).
4233
+ const saved_log_level = std.testing.log_level;
4234
+ std.testing.log_level = .err;
4235
+ defer std.testing.log_level = saved_log_level;
3726
4236
 
3727
- test "drag header trailing content stays clear of the Windows caption cluster" {
3728
- // Windows-shaped hidden-titlebar chrome on a 400pt window: the DWM
3729
- // caption cluster overlays the trailing 138pt of the top band, and
3730
- // the platform reports it through the same chrome channel macOS
3731
- // reports the traffic lights on. The app never consumed the
3732
- // channel, so its right-aligned header status would lay out flush
3733
- // to the window edge — UNDER the min/max/close buttons. The runtime
3734
- // detects the collision after layout and re-lays the drag header
3735
- // with the cluster reserved, so the status text ends at the
3736
- // cluster's leading edge instead.
3737
- const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
4237
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 2000) });
3738
4238
  defer harness.destroy(std.testing.allocator);
3739
4239
  harness.null_platform.gpu_surfaces = true;
3740
- harness.null_platform.window_chrome = .{
3741
- .insets = .{ .top = 32, .right = 138 },
3742
- .buttons = geometry.RectF.init(262, 0, 138, 32),
3743
- };
3744
4240
 
3745
- const app_state = try std.testing.allocator.create(CaptionApp);
4241
+ const app_state = try std.testing.allocator.create(PinFailureApp);
3746
4242
  defer std.testing.allocator.destroy(app_state);
3747
- app_state.* = CaptionApp.init(std.heap.page_allocator, .{}, .{
3748
- .name = "ui-app-caption-naive",
4243
+ app_state.* = PinFailureApp.init(std.heap.page_allocator, .{}, .{
4244
+ .name = "ui-app-pin-rebuild-failure",
3749
4245
  .scene = counter_scene,
3750
4246
  .canvas_label = canvas_label,
3751
- .update = captionUpdate,
3752
- .view = captionNaiveView,
4247
+ .update = pinFailureUpdate,
4248
+ .view = pinFailureView,
3753
4249
  });
3754
4250
  defer app_state.deinit();
3755
4251
  const app = app_state.app();
3756
4252
  try harness.start(app);
3757
4253
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
3758
4254
  .label = canvas_label,
3759
- .size = geometry.SizeF.init(400, 300),
4255
+ .size = geometry.SizeF.init(400, 2000),
3760
4256
  .scale_factor = 1,
3761
4257
  .frame_index = 1,
3762
4258
  .timestamp_ns = 1_000_000,
@@ -3764,92 +4260,158 @@ test "drag header trailing content stays clear of the Windows caption cluster" {
3764
4260
  } });
3765
4261
  try std.testing.expect(app_state.installed);
3766
4262
 
3767
- // The trailing status text ends at (or before) the cluster's
3768
- // leading edge; without the reservation it ended at the window's
3769
- // right edge, inside the cluster.
3770
- const status = try captionTextFrame(&harness.runtime, "sampling");
3771
- try std.testing.expect(status.maxX() <= 262 + 0.01);
3772
- // The leading title and the body below the band are untouched.
3773
- const title = try captionTextFrame(&harness.runtime, "Monitor");
3774
- try std.testing.expectEqual(@as(f32, 0), title.x);
4263
+ const row_id = findIn(app_state.tree.?.root, .list_item, "Ship the release").?;
4264
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
4265
+ const row_frame = layout.findById(row_id).?.frame;
4266
+
4267
+ // Present the row's menu: the presenting build's arena is pinned.
4268
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4269
+ .window_id = 1,
4270
+ .label = canvas_label,
4271
+ .kind = .pointer_down,
4272
+ .button = 1,
4273
+ .x = row_frame.x + 4,
4274
+ .y = row_frame.y + 4,
4275
+ .timestamp_ns = 2_000_000,
4276
+ } });
4277
+ const shown_token = harness.null_platform.context_menu_token;
4278
+ try std.testing.expect(app_state.context_menu_pin != null);
4279
+
4280
+ // One successful rebuild under the open menu lands in the partner
4281
+ // arena; from here every rebuild reuses that LIVE side (the pinned
4282
+ // side stays frozen).
4283
+ try app_state.dispatch(&harness.runtime, 1, .{ .set_rows = 5 });
4284
+ try std.testing.expect(app_state.tree != null);
4285
+
4286
+ // The over-budget rebuild resets the live arena and fails mid-pass
4287
+ // under the harness's `.propagate` policy: the tree reference must
4288
+ // drop with it — a handler table dangling into reset, partially
4289
+ // rewritten storage must never stay dispatchable.
4290
+ try std.testing.expectError(
4291
+ error.WidgetLayoutListFull,
4292
+ app_state.dispatch(&harness.runtime, 1, .{ .set_rows = core.max_canvas_widget_nodes_per_view + 40 }),
4293
+ );
4294
+ try std.testing.expect(app_state.tree == null);
4295
+
4296
+ // Recovery: the next in-budget rebuild restores the tree.
4297
+ try app_state.dispatch(&harness.runtime, 1, .{ .set_rows = 4 });
4298
+ try std.testing.expect(app_state.tree != null);
4299
+
4300
+ // The pinned presentation rode through the failure untouched: the
4301
+ // user's pick still dispatches the presented generation's payload
4302
+ // from the frozen arena.
4303
+ try std.testing.expect(app_state.context_menu_pin != null);
4304
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
4305
+ .window_id = 1,
4306
+ .view_label = canvas_label,
4307
+ .token = shown_token,
4308
+ .item_id = 1,
4309
+ } });
4310
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.sends);
4311
+ try std.testing.expectEqualStrings("payload-rows-0004", app_state.model.received_storage[0..app_state.model.received_len]);
4312
+ try std.testing.expect(app_state.context_menu_pin == null);
3775
4313
  }
3776
4314
 
3777
- test "drag header that already pads the caption cluster keeps its layout" {
3778
- // The contract app (the soundboard shape): its header ends with a
3779
- // spacer consuming `insets.right`, so nothing collides and the
3780
- // runtime reservation must NOT fire — a double reservation would
3781
- // shove the status text a full cluster-width further left.
3782
- const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
4315
+ test "dismissing the menu after a failed pinned rebuild restores the dropped tree" {
4316
+ // The failing layout warns through std.log (the teaching diagnostic
4317
+ // under test would otherwise fail the build runner's stderr check).
4318
+ const saved_log_level = std.testing.log_level;
4319
+ std.testing.log_level = .err;
4320
+ defer std.testing.log_level = saved_log_level;
4321
+
4322
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 2000) });
3783
4323
  defer harness.destroy(std.testing.allocator);
3784
4324
  harness.null_platform.gpu_surfaces = true;
3785
- harness.null_platform.window_chrome = .{
3786
- .insets = .{ .top = 32, .right = 138 },
3787
- .buttons = geometry.RectF.init(262, 0, 138, 32),
3788
- };
3789
4325
 
3790
- const app_state = try std.testing.allocator.create(CaptionApp);
4326
+ const app_state = try std.testing.allocator.create(PinFailureApp);
3791
4327
  defer std.testing.allocator.destroy(app_state);
3792
- app_state.* = CaptionApp.init(std.heap.page_allocator, .{}, .{
3793
- .name = "ui-app-caption-contract",
4328
+ app_state.* = PinFailureApp.init(std.heap.page_allocator, .{}, .{
4329
+ .name = "ui-app-pin-dismiss-restore",
3794
4330
  .scene = counter_scene,
3795
4331
  .canvas_label = canvas_label,
3796
- .update = captionUpdate,
3797
- .view = captionContractView,
3798
- .on_chrome = captionChromeMap,
4332
+ .update = pinFailureUpdate,
4333
+ .view = pinFailureView,
3799
4334
  });
3800
4335
  defer app_state.deinit();
3801
4336
  const app = app_state.app();
3802
4337
  try harness.start(app);
3803
4338
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
3804
4339
  .label = canvas_label,
3805
- .size = geometry.SizeF.init(400, 300),
4340
+ .size = geometry.SizeF.init(400, 2000),
3806
4341
  .scale_factor = 1,
3807
4342
  .frame_index = 1,
3808
4343
  .timestamp_ns = 1_000_000,
3809
4344
  .nonblank = true,
3810
4345
  } });
3811
4346
  try std.testing.expect(app_state.installed);
3812
- try std.testing.expectEqual(@as(f32, 138), app_state.model.trailing);
3813
4347
 
3814
- // The app's own spacer puts the status text at the cluster's edge;
3815
- // the runtime must not reserve on top of it (a double reservation
3816
- // would land it near 262 - 138 = 124).
3817
- const status = try captionTextFrame(&harness.runtime, "sampling");
3818
- try std.testing.expect(@abs(status.maxX() - 262) < 0.5);
4348
+ const row_id = findIn(app_state.tree.?.root, .list_item, "Ship the release").?;
4349
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
4350
+ const row_frame = layout.findById(row_id).?.frame;
4351
+
4352
+ // Present the menu, rebuild once under it, then fail the rebuild
4353
+ // routed into the live arena: the tree reference drops.
4354
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4355
+ .window_id = 1,
4356
+ .label = canvas_label,
4357
+ .kind = .pointer_down,
4358
+ .button = 1,
4359
+ .x = row_frame.x + 4,
4360
+ .y = row_frame.y + 4,
4361
+ .timestamp_ns = 2_000_000,
4362
+ } });
4363
+ const shown_token = harness.null_platform.context_menu_token;
4364
+ try app_state.dispatch(&harness.runtime, 1, .{ .set_rows = 5 });
4365
+ try std.testing.expectError(
4366
+ error.WidgetLayoutListFull,
4367
+ app_state.dispatch(&harness.runtime, 1, .{ .set_rows = core.max_canvas_widget_nodes_per_view + 40 }),
4368
+ );
4369
+ try std.testing.expect(app_state.tree == null);
4370
+
4371
+ // The model comes back in budget WITHOUT a Msg (an effect result,
4372
+ // or the failing state was transient) — no rebuild has run yet.
4373
+ app_state.model.row_count = 4;
4374
+
4375
+ // The user dismisses the open menu. Its resolution dispatches no
4376
+ // Msg, so the release itself must restore the dropped tree —
4377
+ // otherwise every handler no-ops (no tree, no Msgs) until an
4378
+ // unrelated resize or effect happens to rebuild.
4379
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
4380
+ .window_id = 1,
4381
+ .view_label = canvas_label,
4382
+ .token = shown_token,
4383
+ .item_id = 0,
4384
+ } });
4385
+ try std.testing.expect(app_state.tree != null);
4386
+ try std.testing.expect(app_state.context_menu_pin == null);
3819
4387
  }
3820
4388
 
3821
- test "drag header centered title never pays the clearance retry" {
3822
- // The false positive the painted-bounds scan removes: a grow text
3823
- // spanning the header row with centered glyphs. Its FRAME runs
3824
- // under the caption cluster, but nothing inked does — a frame-based
3825
- // scan paid the one retry here, and the remedy's trimmed content
3826
- // box then visibly shifted the centered title left. The scan judges
3827
- // aligned painted bounds, so no retry fires and the layout is
3828
- // byte-identical to an unstamped build: the title's grow frame
3829
- // still spans the full row, cluster and all.
3830
- const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
4389
+ test "a selection whose update breaks the build budget keeps the live tree and input alive" {
4390
+ // The failing layout warns through std.log (the teaching diagnostic
4391
+ // under test would otherwise fail the build runner's stderr check).
4392
+ const saved_log_level = std.testing.log_level;
4393
+ std.testing.log_level = .err;
4394
+ defer std.testing.log_level = saved_log_level;
4395
+
4396
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 2000) });
3831
4397
  defer harness.destroy(std.testing.allocator);
3832
4398
  harness.null_platform.gpu_surfaces = true;
3833
- harness.null_platform.window_chrome = .{
3834
- .insets = .{ .top = 32, .right = 138 },
3835
- .buttons = geometry.RectF.init(262, 0, 138, 32),
3836
- };
3837
4399
 
3838
- const app_state = try std.testing.allocator.create(CaptionApp);
4400
+ const app_state = try std.testing.allocator.create(PinFailureApp);
3839
4401
  defer std.testing.allocator.destroy(app_state);
3840
- app_state.* = CaptionApp.init(std.heap.page_allocator, .{}, .{
3841
- .name = "ui-app-caption-centered",
4402
+ app_state.* = PinFailureApp.init(std.heap.page_allocator, .{}, .{
4403
+ .name = "ui-app-pin-selection-failure",
3842
4404
  .scene = counter_scene,
3843
4405
  .canvas_label = canvas_label,
3844
- .update = captionUpdate,
3845
- .view = captionCenteredView,
4406
+ .update = pinFailureUpdate,
4407
+ .view = pinFailureView,
3846
4408
  });
3847
4409
  defer app_state.deinit();
3848
4410
  const app = app_state.app();
3849
4411
  try harness.start(app);
3850
4412
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
3851
4413
  .label = canvas_label,
3852
- .size = geometry.SizeF.init(400, 300),
4414
+ .size = geometry.SizeF.init(400, 2000),
3853
4415
  .scale_factor = 1,
3854
4416
  .frame_index = 1,
3855
4417
  .timestamp_ns = 1_000_000,
@@ -3857,96 +4419,2032 @@ test "drag header centered title never pays the clearance retry" {
3857
4419
  } });
3858
4420
  try std.testing.expect(app_state.installed);
3859
4421
 
3860
- // No reservation fired: the title's frame still spans the whole
3861
- // row. A paid retry would have trimmed the drag row's content box
3862
- // at the cluster's leading edge (262) and re-centered the glyphs
3863
- // inside the trimmed frame — the visible title shift.
3864
- const title = try captionTextFrame(&harness.runtime, "Monitor");
3865
- try std.testing.expectEqual(@as(f32, 0), title.x);
3866
- try std.testing.expectEqual(@as(f32, 400), title.maxX());
4422
+ const row_id = findIn(app_state.tree.?.root, .list_item, "Ship the release").?;
4423
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
4424
+ const row_frame = layout.findById(row_id).?.frame;
4425
+
4426
+ // Present the menu, then rebuild once under it: the live tree moves
4427
+ // to the partner arena, adjacent to the pinned generation.
4428
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4429
+ .window_id = 1,
4430
+ .label = canvas_label,
4431
+ .kind = .pointer_down,
4432
+ .button = 1,
4433
+ .x = row_frame.x + 4,
4434
+ .y = row_frame.y + 4,
4435
+ .timestamp_ns = 2_000_000,
4436
+ } });
4437
+ const shown_token = harness.null_platform.context_menu_token;
4438
+ try app_state.dispatch(&harness.runtime, 1, .{ .set_rows = 5 });
4439
+
4440
+ // Selecting "Grow" dispatches from the snapshot; its update pushes
4441
+ // the model past the widget budget and the rebuild fails. The pin
4442
+ // released before the dispatch, so the rebuild routed into the
4443
+ // partner arena — the LIVE tree survives the failure and input
4444
+ // keeps working (production's degraded contract; the harness's
4445
+ // `.propagate` policy surfaces the recorded error here).
4446
+ try std.testing.expectError(error.WidgetLayoutListFull, harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
4447
+ .window_id = 1,
4448
+ .view_label = canvas_label,
4449
+ .token = shown_token,
4450
+ .item_id = 2,
4451
+ } }));
4452
+ try std.testing.expect(app_state.tree != null);
4453
+ try std.testing.expect(app_state.context_menu_pin == null);
4454
+
4455
+ // The app's own controls recover the model THROUGH the surviving
4456
+ // handler table: a real pointer click on "Shrink" (still routed by
4457
+ // the retained layout of the last successful build) dispatches its
4458
+ // Msg and the next rebuild succeeds.
4459
+ const retained = try harness.runtime.canvasWidgetLayout(1, canvas_label);
4460
+ var button_frame: geometry.RectF = .{};
4461
+ for (retained.nodes) |node| {
4462
+ if (node.widget.kind == .button) button_frame = node.frame;
4463
+ }
4464
+ try std.testing.expect(!button_frame.isEmpty());
4465
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4466
+ .window_id = 1,
4467
+ .label = canvas_label,
4468
+ .kind = .pointer_down,
4469
+ .x = button_frame.x + 4,
4470
+ .y = button_frame.y + 4,
4471
+ .timestamp_ns = 3_000_000,
4472
+ } });
4473
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4474
+ .window_id = 1,
4475
+ .label = canvas_label,
4476
+ .kind = .pointer_up,
4477
+ .x = button_frame.x + 4,
4478
+ .y = button_frame.y + 4,
4479
+ .timestamp_ns = 4_000_000,
4480
+ } });
4481
+ try std.testing.expectEqual(@as(usize, 4), app_state.model.row_count);
4482
+ try std.testing.expect(app_state.tree != null);
3867
4483
  }
3868
4484
 
3869
- // -------------------------------------- windowed virtual list fixture
4485
+ test "a menu presented while the tree is dropped still resolves once the model recovers" {
4486
+ // The failing layout warns through std.log (the teaching diagnostic
4487
+ // under test would otherwise fail the build runner's stderr check).
4488
+ const saved_log_level = std.testing.log_level;
4489
+ std.testing.log_level = .err;
4490
+ defer std.testing.log_level = saved_log_level;
3870
4491
 
3871
- const VirtualFeedModel = struct {
3872
- /// Items currently loaded; `load_more` appends batches to a cap.
3873
- loaded: usize = 400,
3874
- /// Every reach-end dispatch, including the ones past the cap.
3875
- fetches: u32 = 0,
3876
- };
4492
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 2000) });
4493
+ defer harness.destroy(std.testing.allocator);
4494
+ harness.null_platform.gpu_surfaces = true;
3877
4495
 
3878
- const VirtualFeedMsg = union(enum) {
3879
- load_more,
3880
- };
4496
+ const app_state = try std.testing.allocator.create(PinFailureApp);
4497
+ defer std.testing.allocator.destroy(app_state);
4498
+ app_state.* = PinFailureApp.init(std.heap.page_allocator, .{}, .{
4499
+ .name = "ui-app-pin-shown-recovery",
4500
+ .scene = counter_scene,
4501
+ .canvas_label = canvas_label,
4502
+ .update = pinFailureUpdate,
4503
+ .view = pinFailureView,
4504
+ });
4505
+ defer app_state.deinit();
4506
+ const app = app_state.app();
4507
+ try harness.start(app);
4508
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
4509
+ .label = canvas_label,
4510
+ .size = geometry.SizeF.init(400, 2000),
4511
+ .scale_factor = 1,
4512
+ .frame_index = 1,
4513
+ .timestamp_ns = 1_000_000,
4514
+ .nonblank = true,
4515
+ } });
4516
+ try std.testing.expect(app_state.installed);
3881
4517
 
3882
- const VirtualFeedApp = ui_app_model.UiApp(VirtualFeedModel, VirtualFeedMsg);
4518
+ const row_id = findIn(app_state.tree.?.root, .list_item, "Ship the release").?;
4519
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
4520
+ const row_frame = layout.findById(row_id).?.frame;
4521
+ const right_click: zero_platform.Event = .{ .gpu_surface_input = .{
4522
+ .window_id = 1,
4523
+ .label = canvas_label,
4524
+ .kind = .pointer_down,
4525
+ .button = 1,
4526
+ .x = row_frame.x + 4,
4527
+ .y = row_frame.y + 4,
4528
+ .timestamp_ns = 2_000_000,
4529
+ } };
3883
4530
 
3884
- const virtual_feed_batch: usize = 100;
3885
- const virtual_feed_cap: usize = 600;
4531
+ // Menu A is open when a rebuild routed into the live arena fails:
4532
+ // the tree drops while the model stays unbuildable.
4533
+ try harness.runtime.dispatchPlatformEvent(app, right_click);
4534
+ try app_state.dispatch(&harness.runtime, 1, .{ .set_rows = 5 });
4535
+ try std.testing.expectError(
4536
+ error.WidgetLayoutListFull,
4537
+ app_state.dispatch(&harness.runtime, 1, .{ .set_rows = core.max_canvas_widget_nodes_per_view + 40 }),
4538
+ );
4539
+ try std.testing.expect(app_state.tree == null);
4540
+
4541
+ // Superseding A with menu B: A's dismissal releases the snapshot
4542
+ // and its restore attempt fails loudly (the model is still past the
4543
+ // budget under the harness's `.propagate` policy). B commits
4544
+ // runtime-side, but no snapshot could arm for it.
4545
+ try std.testing.expectError(
4546
+ error.WidgetLayoutListFull,
4547
+ harness.runtime.dispatchPlatformEvent(app, right_click),
4548
+ );
4549
+ const b_token = harness.null_platform.context_menu_token;
4550
+ try std.testing.expectEqual(@as(usize, 2), harness.null_platform.context_menu_request_count);
4551
+
4552
+ // The model recovers WITHOUT a rebuild (an effect result fixed it).
4553
+ app_state.model.row_count = 4;
4554
+
4555
+ // Selecting from menu B resolves snapshot-less: the handler
4556
+ // restores the dropped tree before resolving, so the selection
4557
+ // dispatches its Msg instead of falling through a null tree.
4558
+ try harness.runtime.dispatchPlatformEvent(app, .{ .context_menu_action = .{
4559
+ .window_id = 1,
4560
+ .view_label = canvas_label,
4561
+ .token = b_token,
4562
+ .item_id = 1,
4563
+ } });
4564
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.sends);
4565
+ try std.testing.expectEqualStrings("payload-rows-0004", app_state.model.received_storage[0..app_state.model.received_len]);
4566
+ try std.testing.expect(app_state.tree != null);
4567
+ }
4568
+
4569
+ // ------------------------------------------------- press fall-through fixture
4570
+
4571
+ const RowsModel = struct {
4572
+ picked: u32 = 0,
4573
+ picks: u32 = 0,
4574
+ button_hits: u32 = 0,
4575
+ };
4576
+
4577
+ const RowsMsg = union(enum) {
4578
+ pick: u32,
4579
+ button_hit,
4580
+ };
4581
+
4582
+ const RowsApp = ui_app_model.UiApp(RowsModel, RowsMsg);
4583
+
4584
+ fn rowsUpdate(model: *RowsModel, msg: RowsMsg) void {
4585
+ switch (msg) {
4586
+ .pick => |id| {
4587
+ model.picked = id;
4588
+ model.picks += 1;
4589
+ },
4590
+ .button_hit => model.button_hits += 1,
4591
+ }
4592
+ }
4593
+
4594
+ fn rowsView(ui: *RowsApp.Ui, model: *const RowsModel) RowsApp.Ui.Node {
4595
+ _ = model;
4596
+ // The showcase row shape the press fall-through exists for: pressable
4597
+ // panels whose visible content is plain (selectable) text — no
4598
+ // empty-text overlays, no duplicated handlers on the text leaves.
4599
+ return ui.column(.{ .gap = 8, .padding = 12 }, .{
4600
+ ui.panel(.{ .on_press = RowsMsg{ .pick = 1 }, .height = 48 }, .{
4601
+ ui.row(.{ .gap = 8, .padding = 8 }, .{
4602
+ ui.text(.{ .grow = 1 }, "Alpha row label"),
4603
+ }),
4604
+ }),
4605
+ ui.panel(.{ .on_press = RowsMsg{ .pick = 2 }, .height = 48 }, .{
4606
+ ui.row(.{ .gap = 8, .padding = 8 }, .{
4607
+ ui.text(.{ .grow = 1 }, "Beta row label"),
4608
+ ui.button(.{ .on_press = .button_hit }, "Open"),
4609
+ }),
4610
+ }),
4611
+ });
4612
+ }
4613
+
4614
+ fn rowsOptions() RowsApp.Options {
4615
+ return .{
4616
+ .name = "ui-app-rows",
4617
+ .scene = counter_scene,
4618
+ .canvas_label = canvas_label,
4619
+ .update = rowsUpdate,
4620
+ .view = rowsView,
4621
+ };
4622
+ }
4623
+
4624
+ fn rowsWidgetCenter(runtime: *core.Runtime, id: canvas.ObjectId) !geometry.PointF {
4625
+ const layout = try runtime.canvasWidgetLayout(1, canvas_label);
4626
+ return layout.findById(id).?.frame.normalized().center();
4627
+ }
4628
+
4629
+ fn rowsPointer(kind: zero_platform.GpuSurfaceInputKind, point: geometry.PointF, timestamp_ns: u64) zero_platform.Event {
4630
+ return .{ .gpu_surface_input = .{
4631
+ .label = canvas_label,
4632
+ .kind = kind,
4633
+ .timestamp_ns = timestamp_ns,
4634
+ .x = point.x,
4635
+ .y = point.y,
4636
+ } };
4637
+ }
4638
+
4639
+ fn rowsTextSelection(runtime: *core.Runtime, id: canvas.ObjectId) !?canvas.TextSelection {
4640
+ const layout = try runtime.canvasWidgetLayout(1, canvas_label);
4641
+ return layout.findById(id).?.widget.text_selection;
4642
+ }
4643
+
4644
+ test "presses on a pressable row's plain text land on the row, live and via automation" {
4645
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
4646
+ defer harness.destroy(std.testing.allocator);
4647
+ harness.null_platform.gpu_surfaces = true;
4648
+
4649
+ const app_state = try std.testing.allocator.create(RowsApp);
4650
+ defer std.testing.allocator.destroy(app_state);
4651
+ app_state.* = RowsApp.init(std.heap.page_allocator, .{}, rowsOptions());
4652
+ defer app_state.deinit();
4653
+ const app = app_state.app();
4654
+ try harness.start(app);
4655
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
4656
+ .label = canvas_label,
4657
+ .size = geometry.SizeF.init(400, 300),
4658
+ .scale_factor = 2,
4659
+ .frame_index = 1,
4660
+ .timestamp_ns = 1_000_000,
4661
+ .nonblank = true,
4662
+ } });
4663
+ try std.testing.expect(app_state.installed);
4664
+
4665
+ const tree = app_state.tree.?;
4666
+ const alpha_row_id = tree.root.children[0].id;
4667
+ const alpha_text_id = findIn(tree.root, .text, "Alpha row label").?;
4668
+ const button_id = findIn(tree.root, .button, "Open").?;
4669
+
4670
+ // A live click on the row's plain text lands on the row's on_press.
4671
+ const text_center = try rowsWidgetCenter(&harness.runtime, alpha_text_id);
4672
+ try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_down, text_center, 2_000_000));
4673
+ try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_up, text_center, 2_100_000));
4674
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.picks);
4675
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.picked);
4676
+
4677
+ // A button inside a pressable row claims its own press — the button
4678
+ // wins, the row does not fire.
4679
+ const button_center = try rowsWidgetCenter(&harness.runtime, button_id);
4680
+ try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_down, button_center, 3_000_000));
4681
+ try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_up, button_center, 3_100_000));
4682
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.button_hits);
4683
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.picks);
4684
+
4685
+ // The automation click path agrees with the live hit test: clicking
4686
+ // the row id (whose center is covered by its text) and clicking the
4687
+ // text id both land on the row.
4688
+ var command_buffer: [96]u8 = undefined;
4689
+ const row_click = try std.fmt.bufPrint(&command_buffer, "widget-click {s} {d}", .{ canvas_label, alpha_row_id });
4690
+ try harness.runtime.dispatchAutomationCommand(app, row_click);
4691
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.picks);
4692
+ var text_command_buffer: [96]u8 = undefined;
4693
+ const text_click = try std.fmt.bufPrint(&text_command_buffer, "widget-click {s} {d}", .{ canvas_label, alpha_text_id });
4694
+ try harness.runtime.dispatchAutomationCommand(app, text_click);
4695
+ try std.testing.expectEqual(@as(u32, 3), app_state.model.picks);
4696
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.button_hits);
4697
+ }
4698
+
4699
+ test "selection drag inside a pressable row selects without pressing; a click still presses" {
4700
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
4701
+ defer harness.destroy(std.testing.allocator);
4702
+ harness.null_platform.gpu_surfaces = true;
4703
+
4704
+ const app_state = try std.testing.allocator.create(RowsApp);
4705
+ defer std.testing.allocator.destroy(app_state);
4706
+ app_state.* = RowsApp.init(std.heap.page_allocator, .{}, rowsOptions());
4707
+ defer app_state.deinit();
4708
+ const app = app_state.app();
4709
+ try harness.start(app);
4710
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
4711
+ .label = canvas_label,
4712
+ .size = geometry.SizeF.init(400, 300),
4713
+ .scale_factor = 2,
4714
+ .frame_index = 1,
4715
+ .timestamp_ns = 1_000_000,
4716
+ .nonblank = true,
4717
+ } });
4718
+
4719
+ const alpha_text_id = findIn(app_state.tree.?.root, .text, "Alpha row label").?;
4720
+ const layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
4721
+ const text_frame = layout.findById(alpha_text_id).?.frame.normalized();
4722
+ const start = geometry.PointF.init(text_frame.x + 2, text_frame.center().y);
4723
+ const end = geometry.PointF.init(text_frame.x + text_frame.width * 0.7, text_frame.center().y);
4724
+
4725
+ // Down + drag + up: the gesture selects text within the row and
4726
+ // presses NOTHING — dragging selects, clicking presses.
4727
+ try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_down, start, 2_000_000));
4728
+ try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_drag, end, 2_050_000));
4729
+ try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_up, end, 2_100_000));
4730
+ const selection = (try rowsTextSelection(&harness.runtime, alpha_text_id)).?;
4731
+ try std.testing.expect(!selection.isCollapsed("Alpha row label".len));
4732
+ try std.testing.expectEqual(@as(u32, 0), app_state.model.picks);
4733
+
4734
+ // A plain click on the same text collapses the selection on the way
4735
+ // down and the press lands on the row.
4736
+ const center = try rowsWidgetCenter(&harness.runtime, alpha_text_id);
4737
+ try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_down, center, 3_000_000));
4738
+ try harness.runtime.dispatchPlatformEvent(app, rowsPointer(.pointer_up, center, 3_100_000));
4739
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.picks);
4740
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.picked);
4741
+ }
4742
+
4743
+ // --------------------------------------------------------------- on_chrome
4744
+
4745
+ const ChromeInsetModel = struct {
4746
+ leading: f32 = 0,
4747
+ top: f32 = 0,
4748
+ /// The traffic-light cluster's vertical centerline — what a header
4749
+ /// centers its controls against in the tall band.
4750
+ buttons_center_y: f32 = 0,
4751
+ deliveries: u32 = 0,
4752
+ };
4753
+
4754
+ const ChromeInsetMsg = union(enum) {
4755
+ chrome: zero_platform.WindowChrome,
4756
+ };
4757
+
4758
+ const ChromeInsetApp = ui_app_model.UiApp(ChromeInsetModel, ChromeInsetMsg);
4759
+
4760
+ fn chromeInsetUpdate(model: *ChromeInsetModel, msg: ChromeInsetMsg) void {
4761
+ switch (msg) {
4762
+ .chrome => |chrome| {
4763
+ model.leading = chrome.insets.left;
4764
+ model.top = chrome.insets.top;
4765
+ model.buttons_center_y = chrome.buttons.y + chrome.buttons.height / 2;
4766
+ model.deliveries += 1;
4767
+ },
4768
+ }
4769
+ }
4770
+
4771
+ fn chromeInsetView(ui: *ChromeInsetApp.Ui, model: *const ChromeInsetModel) ChromeInsetApp.Ui.Node {
4772
+ return ui.column(.{ .gap = 8 }, .{
4773
+ ui.row(.{ .window_drag = true, .height = 40 }, .{
4774
+ ui.el(.stack, .{ .width = model.leading }, .{}),
4775
+ ui.text(.{}, "Header"),
4776
+ }),
4777
+ ui.text(.{}, ui.fmt("leading {d}", .{model.leading})),
4778
+ });
4779
+ }
4780
+
4781
+ fn chromeInsetMap(chrome: zero_platform.WindowChrome) ?ChromeInsetMsg {
4782
+ return .{ .chrome = chrome };
4783
+ }
4784
+
4785
+ test "ui app delivers chrome overlay geometry before install and on change" {
4786
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
4787
+ defer harness.destroy(std.testing.allocator);
4788
+ harness.null_platform.gpu_surfaces = true;
4789
+ // Model a tall hidden-titlebar macOS host: unified band 52pt tall,
4790
+ // traffic lights ending 78pt in and vertically centered in the band.
4791
+ harness.null_platform.window_chrome = .{
4792
+ .insets = .{ .top = 52, .left = 78 },
4793
+ .buttons = geometry.RectF.init(12, 19, 54, 14),
4794
+ };
4795
+
4796
+ const app_state = try std.testing.allocator.create(ChromeInsetApp);
4797
+ defer std.testing.allocator.destroy(app_state);
4798
+ app_state.* = ChromeInsetApp.init(std.heap.page_allocator, .{}, .{
4799
+ .name = "ui-app-chrome",
4800
+ .scene = counter_scene,
4801
+ .canvas_label = canvas_label,
4802
+ .update = chromeInsetUpdate,
4803
+ .view = chromeInsetView,
4804
+ .on_chrome = chromeInsetMap,
4805
+ });
4806
+ defer app_state.deinit();
4807
+ const app = app_state.app();
4808
+ try harness.start(app);
4809
+
4810
+ // The insets land in the model BEFORE the first view build, so the
4811
+ // installing frame already renders the padded header.
4812
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
4813
+ .label = canvas_label,
4814
+ .size = geometry.SizeF.init(400, 300),
4815
+ .scale_factor = 2,
4816
+ .frame_index = 1,
4817
+ .timestamp_ns = 1_000_000,
4818
+ .nonblank = true,
4819
+ } });
4820
+ try std.testing.expect(app_state.installed);
4821
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.deliveries);
4822
+ try std.testing.expectEqual(@as(f32, 78), app_state.model.leading);
4823
+ try std.testing.expectEqual(@as(f32, 52), app_state.model.top);
4824
+ // The buttons frame carries the vertical truth: centerline at
4825
+ // 19 + 14/2 = 26, the tall band's midpoint.
4826
+ try std.testing.expectEqual(@as(f32, 26), app_state.model.buttons_center_y);
4827
+ try std.testing.expect(try retainedTextExists(&harness.runtime, "leading 78"));
4828
+
4829
+ // A resize with unchanged insets dispatches nothing extra.
4830
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_resized = .{
4831
+ .window_id = 1,
4832
+ .label = canvas_label,
4833
+ .frame = geometry.RectF.init(0, 0, 640, 480),
4834
+ .scale_factor = 2,
4835
+ } });
4836
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.deliveries);
4837
+
4838
+ // Fullscreen hides the chrome: the resize that accompanies the
4839
+ // transition re-queries and delivers the zeroed geometry.
4840
+ harness.null_platform.window_chrome = .{};
4841
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_resized = .{
4842
+ .window_id = 1,
4843
+ .label = canvas_label,
4844
+ .frame = geometry.RectF.init(0, 0, 1440, 900),
4845
+ .scale_factor = 2,
4846
+ } });
4847
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.deliveries);
4848
+ try std.testing.expectEqual(@as(f32, 0), app_state.model.leading);
4849
+ try std.testing.expectEqual(@as(f32, 0), app_state.model.buttons_center_y);
4850
+ try std.testing.expect(try retainedTextExists(&harness.runtime, "leading 0"));
4851
+ }
4852
+
4853
+ // ---------------------------------- window-control clearance fixtures
4854
+
4855
+ const CaptionModel = struct {
4856
+ /// The trailing chrome reservation the CONTRACT app consumes (the
4857
+ /// soundboard pattern: a spacer sized to `insets.right`); the naive
4858
+ /// app leaves it unwired at 0, exactly like an app that never
4859
+ /// subscribed to the chrome channel.
4860
+ trailing: f32 = 0,
4861
+ };
4862
+
4863
+ const CaptionMsg = union(enum) {
4864
+ chrome: zero_platform.WindowChrome,
4865
+ };
4866
+
4867
+ const CaptionApp = ui_app_model.UiApp(CaptionModel, CaptionMsg);
4868
+
4869
+ fn captionUpdate(model: *CaptionModel, msg: CaptionMsg) void {
4870
+ switch (msg) {
4871
+ .chrome => |chrome| model.trailing = chrome.insets.right,
4872
+ }
4873
+ }
4874
+
4875
+ /// A hidden-titlebar header that never consumed the chrome channel:
4876
+ /// title leading, status text trailing — the system-monitor shape whose
4877
+ /// status line rendered UNDER the Windows caption buttons.
4878
+ fn captionNaiveView(ui: *CaptionApp.Ui, model: *const CaptionModel) CaptionApp.Ui.Node {
4879
+ _ = model;
4880
+ return ui.column(.{}, .{
4881
+ ui.row(.{ .window_drag = true, .height = 40 }, .{
4882
+ ui.text(.{}, "Monitor"),
4883
+ ui.el(.stack, .{ .grow = 1 }, .{}),
4884
+ ui.text(.{}, "sampling"),
4885
+ }),
4886
+ ui.text(.{}, "body"),
4887
+ });
4888
+ }
4889
+
4890
+ /// The documented contract shape (the soundboard pattern): the header
4891
+ /// ends with a spacer sized to `insets.right`, so its own content
4892
+ /// already clears the caption cluster and the runtime reservation must
4893
+ /// stay out of the way.
4894
+ fn captionContractView(ui: *CaptionApp.Ui, model: *const CaptionModel) CaptionApp.Ui.Node {
4895
+ return ui.column(.{}, .{
4896
+ ui.row(.{ .window_drag = true, .height = 40 }, .{
4897
+ ui.text(.{}, "Monitor"),
4898
+ ui.el(.stack, .{ .grow = 1 }, .{}),
4899
+ ui.text(.{}, "sampling"),
4900
+ ui.el(.stack, .{ .width = model.trailing }, .{}),
4901
+ }),
4902
+ ui.text(.{}, "body"),
4903
+ });
4904
+ }
4905
+
4906
+ /// The centered-title pattern: ONE grow text spanning the header row.
4907
+ /// Its FRAME runs under the caption cluster, but the centered glyphs
4908
+ /// sit well clear of it — nothing here needs (or may pay for) the
4909
+ /// clearance retry.
4910
+ fn captionCenteredView(ui: *CaptionApp.Ui, model: *const CaptionModel) CaptionApp.Ui.Node {
4911
+ _ = model;
4912
+ return ui.column(.{}, .{
4913
+ ui.row(.{ .window_drag = true, .height = 40 }, .{
4914
+ ui.text(.{ .grow = 1, .text_alignment = .center }, "Monitor"),
4915
+ }),
4916
+ ui.text(.{}, "body"),
4917
+ });
4918
+ }
4919
+
4920
+ fn captionChromeMap(chrome: zero_platform.WindowChrome) ?CaptionMsg {
4921
+ return .{ .chrome = chrome };
4922
+ }
4923
+
4924
+ fn captionTextFrame(runtime: *core.Runtime, text: []const u8) !geometry.RectF {
4925
+ const layout = try runtime.canvasWidgetLayout(1, canvas_label);
4926
+ for (layout.nodes) |node| {
4927
+ if (node.widget.kind == .text and std.mem.eql(u8, node.widget.text, text)) return node.frame;
4928
+ }
4929
+ return error.TestUnexpectedResult;
4930
+ }
4931
+
4932
+ test "drag header trailing content stays clear of the Windows caption cluster" {
4933
+ // Windows-shaped hidden-titlebar chrome on a 400pt window: the DWM
4934
+ // caption cluster overlays the trailing 138pt of the top band, and
4935
+ // the platform reports it through the same chrome channel macOS
4936
+ // reports the traffic lights on. The app never consumed the
4937
+ // channel, so its right-aligned header status would lay out flush
4938
+ // to the window edge — UNDER the min/max/close buttons. The runtime
4939
+ // detects the collision after layout and re-lays the drag header
4940
+ // with the cluster reserved, so the status text ends at the
4941
+ // cluster's leading edge instead.
4942
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
4943
+ defer harness.destroy(std.testing.allocator);
4944
+ harness.null_platform.gpu_surfaces = true;
4945
+ harness.null_platform.window_chrome = .{
4946
+ .insets = .{ .top = 32, .right = 138 },
4947
+ .buttons = geometry.RectF.init(262, 0, 138, 32),
4948
+ };
4949
+
4950
+ const app_state = try std.testing.allocator.create(CaptionApp);
4951
+ defer std.testing.allocator.destroy(app_state);
4952
+ app_state.* = CaptionApp.init(std.heap.page_allocator, .{}, .{
4953
+ .name = "ui-app-caption-naive",
4954
+ .scene = counter_scene,
4955
+ .canvas_label = canvas_label,
4956
+ .update = captionUpdate,
4957
+ .view = captionNaiveView,
4958
+ });
4959
+ defer app_state.deinit();
4960
+ const app = app_state.app();
4961
+ try harness.start(app);
4962
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
4963
+ .label = canvas_label,
4964
+ .size = geometry.SizeF.init(400, 300),
4965
+ .scale_factor = 1,
4966
+ .frame_index = 1,
4967
+ .timestamp_ns = 1_000_000,
4968
+ .nonblank = true,
4969
+ } });
4970
+ try std.testing.expect(app_state.installed);
4971
+
4972
+ // The trailing status text ends at (or before) the cluster's
4973
+ // leading edge; without the reservation it ended at the window's
4974
+ // right edge, inside the cluster.
4975
+ const status = try captionTextFrame(&harness.runtime, "sampling");
4976
+ try std.testing.expect(status.maxX() <= 262 + 0.01);
4977
+ // The leading title and the body below the band are untouched.
4978
+ const title = try captionTextFrame(&harness.runtime, "Monitor");
4979
+ try std.testing.expectEqual(@as(f32, 0), title.x);
4980
+ }
4981
+
4982
+ test "drag header that already pads the caption cluster keeps its layout" {
4983
+ // The contract app (the soundboard shape): its header ends with a
4984
+ // spacer consuming `insets.right`, so nothing collides and the
4985
+ // runtime reservation must NOT fire — a double reservation would
4986
+ // shove the status text a full cluster-width further left.
4987
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
4988
+ defer harness.destroy(std.testing.allocator);
4989
+ harness.null_platform.gpu_surfaces = true;
4990
+ harness.null_platform.window_chrome = .{
4991
+ .insets = .{ .top = 32, .right = 138 },
4992
+ .buttons = geometry.RectF.init(262, 0, 138, 32),
4993
+ };
4994
+
4995
+ const app_state = try std.testing.allocator.create(CaptionApp);
4996
+ defer std.testing.allocator.destroy(app_state);
4997
+ app_state.* = CaptionApp.init(std.heap.page_allocator, .{}, .{
4998
+ .name = "ui-app-caption-contract",
4999
+ .scene = counter_scene,
5000
+ .canvas_label = canvas_label,
5001
+ .update = captionUpdate,
5002
+ .view = captionContractView,
5003
+ .on_chrome = captionChromeMap,
5004
+ });
5005
+ defer app_state.deinit();
5006
+ const app = app_state.app();
5007
+ try harness.start(app);
5008
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
5009
+ .label = canvas_label,
5010
+ .size = geometry.SizeF.init(400, 300),
5011
+ .scale_factor = 1,
5012
+ .frame_index = 1,
5013
+ .timestamp_ns = 1_000_000,
5014
+ .nonblank = true,
5015
+ } });
5016
+ try std.testing.expect(app_state.installed);
5017
+ try std.testing.expectEqual(@as(f32, 138), app_state.model.trailing);
5018
+
5019
+ // The app's own spacer puts the status text at the cluster's edge;
5020
+ // the runtime must not reserve on top of it (a double reservation
5021
+ // would land it near 262 - 138 = 124).
5022
+ const status = try captionTextFrame(&harness.runtime, "sampling");
5023
+ try std.testing.expect(@abs(status.maxX() - 262) < 0.5);
5024
+ }
5025
+
5026
+ test "drag header centered title never pays the clearance retry" {
5027
+ // The false positive the painted-bounds scan removes: a grow text
5028
+ // spanning the header row with centered glyphs. Its FRAME runs
5029
+ // under the caption cluster, but nothing inked does — a frame-based
5030
+ // scan paid the one retry here, and the remedy's trimmed content
5031
+ // box then visibly shifted the centered title left. The scan judges
5032
+ // aligned painted bounds, so no retry fires and the layout is
5033
+ // byte-identical to an unstamped build: the title's grow frame
5034
+ // still spans the full row, cluster and all.
5035
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
5036
+ defer harness.destroy(std.testing.allocator);
5037
+ harness.null_platform.gpu_surfaces = true;
5038
+ harness.null_platform.window_chrome = .{
5039
+ .insets = .{ .top = 32, .right = 138 },
5040
+ .buttons = geometry.RectF.init(262, 0, 138, 32),
5041
+ };
5042
+
5043
+ const app_state = try std.testing.allocator.create(CaptionApp);
5044
+ defer std.testing.allocator.destroy(app_state);
5045
+ app_state.* = CaptionApp.init(std.heap.page_allocator, .{}, .{
5046
+ .name = "ui-app-caption-centered",
5047
+ .scene = counter_scene,
5048
+ .canvas_label = canvas_label,
5049
+ .update = captionUpdate,
5050
+ .view = captionCenteredView,
5051
+ });
5052
+ defer app_state.deinit();
5053
+ const app = app_state.app();
5054
+ try harness.start(app);
5055
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
5056
+ .label = canvas_label,
5057
+ .size = geometry.SizeF.init(400, 300),
5058
+ .scale_factor = 1,
5059
+ .frame_index = 1,
5060
+ .timestamp_ns = 1_000_000,
5061
+ .nonblank = true,
5062
+ } });
5063
+ try std.testing.expect(app_state.installed);
5064
+
5065
+ // No reservation fired: the title's frame still spans the whole
5066
+ // row. A paid retry would have trimmed the drag row's content box
5067
+ // at the cluster's leading edge (262) and re-centered the glyphs
5068
+ // inside the trimmed frame — the visible title shift.
5069
+ const title = try captionTextFrame(&harness.runtime, "Monitor");
5070
+ try std.testing.expectEqual(@as(f32, 0), title.x);
5071
+ try std.testing.expectEqual(@as(f32, 400), title.maxX());
5072
+ }
5073
+
5074
+ // -------------------------------------- windowed virtual list fixture
5075
+
5076
+ const VirtualFeedModel = struct {
5077
+ /// Items currently loaded; `load_more` appends batches to a cap.
5078
+ loaded: usize = 400,
5079
+ /// Every reach-end dispatch, including the ones past the cap.
5080
+ fetches: u32 = 0,
5081
+ };
5082
+
5083
+ const VirtualFeedMsg = union(enum) {
5084
+ load_more,
5085
+ };
5086
+
5087
+ const VirtualFeedApp = ui_app_model.UiApp(VirtualFeedModel, VirtualFeedMsg);
5088
+
5089
+ const virtual_feed_batch: usize = 100;
5090
+ const virtual_feed_cap: usize = 600;
3886
5091
  const virtual_row_extent: f32 = 24;
3887
5092
 
3888
- fn virtualFeedUpdate(model: *VirtualFeedModel, msg: VirtualFeedMsg) void {
5093
+ fn virtualFeedUpdate(model: *VirtualFeedModel, msg: VirtualFeedMsg) void {
5094
+ switch (msg) {
5095
+ .load_more => {
5096
+ model.fetches += 1;
5097
+ if (model.loaded < virtual_feed_cap) model.loaded += virtual_feed_batch;
5098
+ },
5099
+ }
5100
+ }
5101
+
5102
+ fn virtualFeedOptions(model: *const VirtualFeedModel) VirtualFeedApp.Ui.VirtualListOptions {
5103
+ return .{
5104
+ .id = "vfeed",
5105
+ .item_count = model.loaded,
5106
+ .item_extent = virtual_row_extent,
5107
+ .overscan = 2,
5108
+ .grow = 1,
5109
+ .on_reach_end = .load_more,
5110
+ };
5111
+ }
5112
+
5113
+ /// The windowed pattern: ask for the visible range, build ONLY those
5114
+ /// rows from the model, hand both to `virtualList`. Deliberately no
5115
+ /// `on_scroll` binding — the runtime re-derives the view on scroll for
5116
+ /// mounted virtual lists on its own.
5117
+ fn virtualFeedView(ui: *VirtualFeedApp.Ui, model: *const VirtualFeedModel) VirtualFeedApp.Ui.Node {
5118
+ const options = virtualFeedOptions(model);
5119
+ const window = ui.virtualWindow(options);
5120
+ const rows = ui.arena.alloc(VirtualFeedApp.Ui.Node, window.itemCount()) catch {
5121
+ ui.failed = true;
5122
+ return ui.column(.{}, .{});
5123
+ };
5124
+ for (rows, 0..) |*row, offset| {
5125
+ const index = window.start_index + offset;
5126
+ var node = ui.listItem(.{}, ui.fmt("Item {d}", .{index}));
5127
+ node.key = .{ .int = @intCast(index) };
5128
+ row.* = node;
5129
+ }
5130
+ return ui.virtualList(options, window, .{rows});
5131
+ }
5132
+
5133
+ fn virtualFeedAppOptions() VirtualFeedApp.Options {
5134
+ return .{
5135
+ .name = "ui-app-virtual-feed",
5136
+ .scene = counter_scene,
5137
+ .canvas_label = canvas_label,
5138
+ .update = virtualFeedUpdate,
5139
+ .view = virtualFeedView,
5140
+ };
5141
+ }
5142
+
5143
+ test "windowed virtual list scrolls, re-windows, budgets to the viewport, and fires reach-end with hysteresis" {
5144
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
5145
+ defer harness.destroy(std.testing.allocator);
5146
+ harness.null_platform.gpu_surfaces = true;
5147
+
5148
+ const app_state = try std.testing.allocator.create(VirtualFeedApp);
5149
+ defer std.testing.allocator.destroy(app_state);
5150
+ app_state.* = VirtualFeedApp.init(std.heap.page_allocator, .{}, virtualFeedAppOptions());
5151
+ defer app_state.deinit();
5152
+ const app = app_state.app();
5153
+ try harness.start(app);
5154
+
5155
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
5156
+ .label = canvas_label,
5157
+ .size = geometry.SizeF.init(400, 300),
5158
+ .scale_factor = 2,
5159
+ .frame_index = 1,
5160
+ .timestamp_ns = 1_000_000,
5161
+ .nonblank = true,
5162
+ } });
5163
+ try std.testing.expect(app_state.installed);
5164
+
5165
+ // The first build materialized only the first window (300pt viewport
5166
+ // at 24pt rows + 2 overscan = 15 rows), never the 400 loaded items.
5167
+ const list_id = canvas.globalWidgetId(.scroll_view, .{ .str = "vfeed" });
5168
+ try std.testing.expectEqual(list_id, app_state.tree.?.root.id);
5169
+ try std.testing.expectEqual(@as(usize, 15), app_state.tree.?.root.children.len);
5170
+ try std.testing.expectEqual(@as(usize, 0), app_state.tree.?.root.layout.virtual_first_index);
5171
+
5172
+ // A wheel scroll with NO on_scroll binding still re-windows the
5173
+ // view: the runtime owns the offset, the scroll observation itself
5174
+ // triggers the re-derivation.
5175
+ var command_buffer: [96]u8 = undefined;
5176
+ var wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 240", .{ canvas_label, list_id });
5177
+ try harness.runtime.dispatchAutomationCommand(app, wheel);
5178
+ try std.testing.expectEqual(@as(usize, 8), app_state.tree.?.root.layout.virtual_first_index);
5179
+ try std.testing.expect(findIn(app_state.tree.?.root, .list_item, "Item 8") != null);
5180
+ try std.testing.expect(findIn(app_state.tree.?.root, .list_item, "Item 0") == null);
5181
+ const retained = try harness.runtime.canvasWidgetLayout(1, canvas_label);
5182
+ try std.testing.expectEqual(@as(f32, 240), retained.findById(list_id).?.widget.value);
5183
+
5184
+ // Row identity is the item, not the slot: item 10 was in both
5185
+ // windows and kept its structural id across the shift.
5186
+ // (The id is derived from the list id and the item key alone.)
5187
+ try std.testing.expect(findIn(app_state.tree.?.root, .list_item, "Item 10") != null);
5188
+
5189
+ // Scroll to the end of the loaded 400 items (content 9600, viewport
5190
+ // 300, max offset 9300): the approach-end signal fires ONCE and the
5191
+ // model appends a batch.
5192
+ try std.testing.expectEqual(@as(u32, 0), app_state.model.fetches);
5193
+ wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 9060", .{ canvas_label, list_id });
5194
+ try harness.runtime.dispatchAutomationCommand(app, wheel);
5195
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.fetches);
5196
+ try std.testing.expectEqual(@as(usize, 500), app_state.model.loaded);
5197
+
5198
+ // The appended batch grew the extent (12_000), pulling the offset
5199
+ // out of the band — the next small scroll re-arms, no fire.
5200
+ wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 24", .{ canvas_label, list_id });
5201
+ try harness.runtime.dispatchAutomationCommand(app, wheel);
5202
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.fetches);
5203
+
5204
+ // Ride to the new end: fires again (offset 9324 + 2376 = 11_700 =
5205
+ // the new max), appends to the 600 cap.
5206
+ wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 2376", .{ canvas_label, list_id });
5207
+ try harness.runtime.dispatchAutomationCommand(app, wheel);
5208
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.fetches);
5209
+ try std.testing.expectEqual(@as(usize, 600), app_state.model.loaded);
5210
+
5211
+ // At the cap the model stops appending, so the extent stops
5212
+ // growing — riding the end must NOT dispatch a fetch storm.
5213
+ wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 24", .{ canvas_label, list_id });
5214
+ try harness.runtime.dispatchAutomationCommand(app, wheel);
5215
+ wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 2376", .{ canvas_label, list_id });
5216
+ try harness.runtime.dispatchAutomationCommand(app, wheel);
5217
+ try std.testing.expectEqual(@as(u32, 3), app_state.model.fetches);
5218
+ wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} -24", .{ canvas_label, list_id });
5219
+ try harness.runtime.dispatchAutomationCommand(app, wheel);
5220
+ wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 24", .{ canvas_label, list_id });
5221
+ try harness.runtime.dispatchAutomationCommand(app, wheel);
5222
+ try std.testing.expectEqual(@as(u32, 3), app_state.model.fetches);
5223
+
5224
+ // Budget stays viewport-sized throughout: ~19 mounted rows against
5225
+ // 600 loaded items, and the scroll semantics report the full extent.
5226
+ const final_layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
5227
+ try std.testing.expect(final_layout.nodes.len < 40);
5228
+ const scroll_state = harness.runtime.views[0].canvasWidgetScrollStateById(list_id).?;
5229
+ try std.testing.expectEqual(@as(f32, 600 * virtual_row_extent), scroll_state.content_extent_y);
5230
+
5231
+ // A window-growing resize converges within ONE rebuild: the first
5232
+ // build pass reads the stale 300pt viewport, the coverage check sees
5233
+ // the fresh 600pt geometry under-covered, and the retry pass builds
5234
+ // the wider window against it.
5235
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_resized = .{
5236
+ .window_id = 1,
5237
+ .label = canvas_label,
5238
+ .frame = geometry.RectF.init(0, 0, 400, 600),
5239
+ .scale_factor = 2,
5240
+ } });
5241
+ const root = app_state.tree.?.root;
5242
+ const first_index = root.layout.virtual_first_index;
5243
+ const mounted = root.children.len;
5244
+ // Offset clamps to the new max (14_400 - 600 = 13_800): visible rows
5245
+ // 575..600 must all be inside the built window.
5246
+ try std.testing.expect(first_index <= 575);
5247
+ try std.testing.expectEqual(@as(usize, 600), first_index + mounted);
5248
+ try std.testing.expect(mounted >= 25);
5249
+ }
5250
+
5251
+ // ------------------------------------------------------- pinch channel
5252
+
5253
+ const ZoomModel = struct {
5254
+ /// Cumulative gesture scale: the running product of `1 + delta`
5255
+ /// across change events — the semantics the channel documents.
5256
+ scale: f32 = 1,
5257
+ begins: u32 = 0,
5258
+ ends: u32 = 0,
5259
+ anchor_x: f32 = 0,
5260
+ anchor_y: f32 = 0,
5261
+ /// Source-identity mirrors: the window and view the last pinch
5262
+ /// event named (x/y are view-local, so a coordinate without its
5263
+ /// view is not a position — multi-window apps tell pinches apart
5264
+ /// by these).
5265
+ window_id: u64 = 0,
5266
+ label: []const u8 = "",
5267
+ };
5268
+
5269
+ const ZoomMsg = union(enum) {
5270
+ pinch: zero_platform.PinchEvent,
5271
+ };
5272
+
5273
+ const ZoomApp = ui_app_model.UiApp(ZoomModel, ZoomMsg);
5274
+
5275
+ fn zoomUpdate(model: *ZoomModel, msg: ZoomMsg) void {
5276
+ switch (msg) {
5277
+ .pinch => |pinch| {
5278
+ model.window_id = pinch.window_id;
5279
+ model.label = pinch.label;
5280
+ switch (pinch.phase) {
5281
+ .begin => {
5282
+ model.begins += 1;
5283
+ model.anchor_x = pinch.x;
5284
+ model.anchor_y = pinch.y;
5285
+ },
5286
+ .change => model.scale *= (1 + pinch.scale),
5287
+ .end => model.ends += 1,
5288
+ }
5289
+ },
5290
+ }
5291
+ }
5292
+
5293
+ fn zoomView(ui: *ZoomApp.Ui, model: *const ZoomModel) ZoomApp.Ui.Node {
5294
+ return ui.column(.{ .gap = 8, .padding = 12 }, .{
5295
+ ui.text(.{}, ui.fmt("Zoom {d:.2}", .{model.scale})),
5296
+ });
5297
+ }
5298
+
5299
+ fn zoomPinch(pinch: zero_platform.PinchEvent) ?ZoomMsg {
5300
+ return .{ .pinch = pinch };
5301
+ }
5302
+
5303
+ test "trackpad pinch reaches the app through on_pinch with product-of-deltas scale" {
5304
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
5305
+ defer harness.destroy(std.testing.allocator);
5306
+ harness.null_platform.gpu_surfaces = true;
5307
+
5308
+ const app_state = try std.testing.allocator.create(ZoomApp);
5309
+ defer std.testing.allocator.destroy(app_state);
5310
+ app_state.* = ZoomApp.init(std.heap.page_allocator, .{}, .{
5311
+ .name = "ui-app-zoom",
5312
+ .scene = counter_scene,
5313
+ .canvas_label = canvas_label,
5314
+ .update = zoomUpdate,
5315
+ .view = zoomView,
5316
+ .on_pinch = zoomPinch,
5317
+ });
5318
+ defer app_state.deinit();
5319
+ const app = app_state.app();
5320
+ try harness.start(app);
5321
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
5322
+ .label = canvas_label,
5323
+ .size = geometry.SizeF.init(400, 300),
5324
+ .scale_factor = 2,
5325
+ .frame_index = 1,
5326
+ .timestamp_ns = 1_000_000,
5327
+ .nonblank = true,
5328
+ } });
5329
+ try std.testing.expect(app_state.installed);
5330
+
5331
+ // begin -> change -> change -> change -> end, the host's phase
5332
+ // stream: the model hears every phase, the pointer anchor rides
5333
+ // x/y, and the cumulative scale is the PRODUCT of (1 + delta) —
5334
+ // two +25% steps land on 1.5625, never the 1.45 a sum-of-deltas
5335
+ // would produce (deltas are raw NSEvent.magnification, the
5336
+ // multiplicative per-event delta per the engine convention; see
5337
+ // the doctrine note in appkit_host.m).
5338
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5339
+ .window_id = 1,
5340
+ .label = canvas_label,
5341
+ .kind = .pinch_begin,
5342
+ .x = 120,
5343
+ .y = 80,
5344
+ } });
5345
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.begins);
5346
+ try std.testing.expectEqual(@as(f32, 120), app_state.model.anchor_x);
5347
+ try std.testing.expectEqual(@as(f32, 80), app_state.model.anchor_y);
5348
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5349
+ .window_id = 1,
5350
+ .label = canvas_label,
5351
+ .kind = .pinch_change,
5352
+ .x = 120,
5353
+ .y = 80,
5354
+ .scale = 0.25,
5355
+ } });
5356
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5357
+ .window_id = 1,
5358
+ .label = canvas_label,
5359
+ .kind = .pinch_change,
5360
+ .x = 120,
5361
+ .y = 80,
5362
+ .scale = 0.25,
5363
+ } });
5364
+ try std.testing.expectEqual(@as(f32, 1.5625), app_state.model.scale);
5365
+ try std.testing.expectEqual(@as(u32, 0), app_state.model.ends);
5366
+ // A terminal-delta Ended: AppKit's Ended/Cancelled event still
5367
+ // carries the magnification since the previous event, so the host
5368
+ // forwards it as one last change BEFORE the end marker — the
5369
+ // product folds it in (1.5625 * 1.25 = 1.953125, binary-exact).
5370
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5371
+ .window_id = 1,
5372
+ .label = canvas_label,
5373
+ .kind = .pinch_change,
5374
+ .x = 120,
5375
+ .y = 80,
5376
+ .scale = 0.25,
5377
+ } });
5378
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5379
+ .window_id = 1,
5380
+ .label = canvas_label,
5381
+ .kind = .pinch_end,
5382
+ .x = 120,
5383
+ .y = 80,
5384
+ } });
5385
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.ends);
5386
+ try std.testing.expectEqual(@as(f32, 1.953125), app_state.model.scale);
5387
+ // The dispatched Msgs rebuilt the view from the model.
5388
+ try std.testing.expect(try retainedTextExists(&harness.runtime, "Zoom 1.95"));
5389
+
5390
+ // A zero-delta Ended is just the end marker: begin then end moves
5391
+ // no scale.
5392
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5393
+ .window_id = 1,
5394
+ .label = canvas_label,
5395
+ .kind = .pinch_begin,
5396
+ .x = 120,
5397
+ .y = 80,
5398
+ } });
5399
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5400
+ .window_id = 1,
5401
+ .label = canvas_label,
5402
+ .kind = .pinch_end,
5403
+ .x = 120,
5404
+ .y = 80,
5405
+ } });
5406
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.begins);
5407
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.ends);
5408
+ try std.testing.expectEqual(@as(f32, 1.953125), app_state.model.scale);
5409
+
5410
+ // Non-pinch raw input never leaks into the channel: a scroll leaves
5411
+ // the model untouched.
5412
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5413
+ .window_id = 1,
5414
+ .label = canvas_label,
5415
+ .kind = .scroll,
5416
+ .delta_y = 24,
5417
+ } });
5418
+ try std.testing.expectEqual(@as(f32, 1.953125), app_state.model.scale);
5419
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.begins);
5420
+
5421
+ // The automation verb drives the same real events: one full gesture
5422
+ // whose change carries scale - 1, anchor defaulting to the view
5423
+ // center, so tests and users can pinch without a trackpad.
5424
+ app_state.model = .{};
5425
+ var command_buffer: [96]u8 = undefined;
5426
+ const pinch_default = try std.fmt.bufPrint(&command_buffer, "widget-pinch {s} 1.5", .{canvas_label});
5427
+ try harness.runtime.dispatchAutomationCommand(app, pinch_default);
5428
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.begins);
5429
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.ends);
5430
+ try std.testing.expectEqual(@as(f32, 1.5), app_state.model.scale);
5431
+ try std.testing.expectEqual(@as(f32, 200), app_state.model.anchor_x);
5432
+ try std.testing.expectEqual(@as(f32, 150), app_state.model.anchor_y);
5433
+
5434
+ // An explicit anchor point rides through; the cumulative scale keeps
5435
+ // compounding across gestures exactly as deltas compound within one.
5436
+ const pinch_at = try std.fmt.bufPrint(&command_buffer, "widget-pinch {s} 0.5 40 60", .{canvas_label});
5437
+ try harness.runtime.dispatchAutomationCommand(app, pinch_at);
5438
+ try std.testing.expectEqual(@as(f32, 0.75), app_state.model.scale);
5439
+ try std.testing.expectEqual(@as(f32, 40), app_state.model.anchor_x);
5440
+ try std.testing.expectEqual(@as(f32, 60), app_state.model.anchor_y);
5441
+
5442
+ // A malformed scale is loud driver misuse, not a dispatched gesture
5443
+ // (the product of 1 + delta can never reach a non-positive scale).
5444
+ const pinch_bad = try std.fmt.bufPrint(&command_buffer, "widget-pinch {s} 0", .{canvas_label});
5445
+ try std.testing.expectError(error.InvalidCommand, harness.runtime.dispatchAutomationCommand(app, pinch_bad));
5446
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.begins);
5447
+
5448
+ // The f32 wire can betray the parser's `scale > 0` guard: a tiny
5449
+ // positive scale rounds `scale - 1` to exactly -1 — factor 0 on the
5450
+ // wire — so the dispatch refuses it with the wire-minimum teaching
5451
+ // instead of emitting a zoom through zero scale. Nothing dispatches:
5452
+ // no begin, no journaled partial gesture.
5453
+ const pinch_tiny = try std.fmt.bufPrint(&command_buffer, "widget-pinch {s} 1e-20", .{canvas_label});
5454
+ try std.testing.expectError(error.PinchScaleBelowWireMinimum, harness.runtime.dispatchAutomationCommand(app, pinch_tiny));
5455
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.begins);
5456
+ try std.testing.expectEqual(@as(f32, 0.75), app_state.model.scale);
5457
+
5458
+ // The smallest accepted scale — the first f32 above 2^-25 — round-
5459
+ // trips with a positive factor: its delta rounds to -1 + 2^-24, so
5460
+ // the factor is exactly 2^-24 and the model's product stays > 0
5461
+ // (0.75 * 2^-24 = 0x1.8p-25, binary-exact).
5462
+ const pinch_min = try std.fmt.bufPrint(&command_buffer, "widget-pinch {s} 2.9802326e-8", .{canvas_label});
5463
+ try harness.runtime.dispatchAutomationCommand(app, pinch_min);
5464
+ try std.testing.expectEqual(@as(u32, 3), app_state.model.begins);
5465
+ try std.testing.expectEqual(@as(u32, 3), app_state.model.ends);
5466
+ try std.testing.expect(app_state.model.scale > 0);
5467
+ try std.testing.expectEqual(@as(f32, 0x1.8p-25), app_state.model.scale);
5468
+ }
5469
+
5470
+ const zoom_pair_main_views = [_]app_manifest.ShellView{
5471
+ .{ .label = "zoom-main-canvas", .kind = .gpu_surface, .fill = true, .gpu_backend = .metal },
5472
+ };
5473
+ const zoom_pair_inspector_views = [_]app_manifest.ShellView{
5474
+ .{ .label = "zoom-inspector-canvas", .kind = .gpu_surface, .fill = true, .gpu_backend = .metal },
5475
+ };
5476
+ const zoom_pair_windows = [_]app_manifest.ShellWindow{ .{
5477
+ .label = "main",
5478
+ .title = "Zoom",
5479
+ .width = 400,
5480
+ .height = 300,
5481
+ .views = &zoom_pair_main_views,
5482
+ }, .{
5483
+ .label = "inspector",
5484
+ .title = "Zoom Inspector",
5485
+ .width = 300,
5486
+ .height = 300,
5487
+ .views = &zoom_pair_inspector_views,
5488
+ } };
5489
+ const zoom_pair_scene: app_manifest.ShellConfig = .{ .windows = &zoom_pair_windows };
5490
+
5491
+ test "pinch identity distinguishes windows and views in the Msg" {
5492
+ // Two windows, two gpu-surface views: the pinch channel forwards
5493
+ // the source identity (window_id + view label) the journaled
5494
+ // platform event already carries, so a multi-window app hears WHICH
5495
+ // view a gesture zoomed — x/y are view-local, and a coordinate
5496
+ // without its view is not a position.
5497
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
5498
+ defer harness.destroy(std.testing.allocator);
5499
+ harness.null_platform.gpu_surfaces = true;
5500
+
5501
+ const app_state = try std.testing.allocator.create(ZoomApp);
5502
+ defer std.testing.allocator.destroy(app_state);
5503
+ app_state.* = ZoomApp.init(std.heap.page_allocator, .{}, .{
5504
+ .name = "ui-app-zoom-pair",
5505
+ .scene = zoom_pair_scene,
5506
+ .canvas_label = "zoom-main-canvas",
5507
+ .update = zoomUpdate,
5508
+ .view = zoomView,
5509
+ .on_pinch = zoomPinch,
5510
+ });
5511
+ defer app_state.deinit();
5512
+ const app = app_state.app();
5513
+ try harness.start(app);
5514
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
5515
+ .label = "zoom-main-canvas",
5516
+ .size = geometry.SizeF.init(400, 300),
5517
+ .scale_factor = 2,
5518
+ .frame_index = 1,
5519
+ .timestamp_ns = 1_000_000,
5520
+ .nonblank = true,
5521
+ } });
5522
+ try std.testing.expect(app_state.installed);
5523
+
5524
+ // A gesture on the main window's canvas names its source on every
5525
+ // phase.
5526
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5527
+ .window_id = 1,
5528
+ .label = "zoom-main-canvas",
5529
+ .kind = .pinch_begin,
5530
+ .x = 100,
5531
+ .y = 50,
5532
+ } });
5533
+ try std.testing.expectEqual(@as(u64, 1), app_state.model.window_id);
5534
+ try std.testing.expectEqualStrings("zoom-main-canvas", app_state.model.label);
5535
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5536
+ .window_id = 1,
5537
+ .label = "zoom-main-canvas",
5538
+ .kind = .pinch_change,
5539
+ .x = 100,
5540
+ .y = 50,
5541
+ .scale = 0.25,
5542
+ } });
5543
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5544
+ .window_id = 1,
5545
+ .label = "zoom-main-canvas",
5546
+ .kind = .pinch_end,
5547
+ .x = 100,
5548
+ .y = 50,
5549
+ } });
5550
+ try std.testing.expectEqual(@as(f32, 1.25), app_state.model.scale);
5551
+
5552
+ // A gesture on the second window's view is distinguishable: same
5553
+ // channel, different identity in the Msg.
5554
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5555
+ .window_id = 2,
5556
+ .label = "zoom-inspector-canvas",
5557
+ .kind = .pinch_begin,
5558
+ .x = 10,
5559
+ .y = 20,
5560
+ } });
5561
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5562
+ .window_id = 2,
5563
+ .label = "zoom-inspector-canvas",
5564
+ .kind = .pinch_change,
5565
+ .x = 10,
5566
+ .y = 20,
5567
+ .scale = -0.5,
5568
+ } });
5569
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5570
+ .window_id = 2,
5571
+ .label = "zoom-inspector-canvas",
5572
+ .kind = .pinch_end,
5573
+ .x = 10,
5574
+ .y = 20,
5575
+ } });
5576
+ try std.testing.expectEqual(@as(u64, 2), app_state.model.window_id);
5577
+ try std.testing.expectEqualStrings("zoom-inspector-canvas", app_state.model.label);
5578
+ // Both gestures compounded the one model's zoom (1.25 * 0.5,
5579
+ // binary-exact) and every phase was heard.
5580
+ try std.testing.expectEqual(@as(f32, 0.625), app_state.model.scale);
5581
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.begins);
5582
+ try std.testing.expectEqual(@as(u32, 2), app_state.model.ends);
5583
+ }
5584
+
5585
+ // ------------------------------------------------------------ hover fixture
5586
+
5587
+ const HoverEntry = union(enum) {
5588
+ outer_enter,
5589
+ outer_leave,
5590
+ row_enter: u32,
5591
+ row_leave: u32,
5592
+ vanish_enter,
5593
+ vanish_leave,
5594
+ };
5595
+
5596
+ const HoverModel = struct {
5597
+ log: [24]HoverEntry = undefined,
5598
+ log_len: usize = 0,
5599
+ show_vanish: bool = true,
5600
+ outer_bound: bool = true,
5601
+
5602
+ fn push(model: *HoverModel, entry: HoverEntry) void {
5603
+ if (model.log_len >= model.log.len) return;
5604
+ model.log[model.log_len] = entry;
5605
+ model.log_len += 1;
5606
+ }
5607
+
5608
+ fn entries(model: *const HoverModel) []const HoverEntry {
5609
+ return model.log[0..model.log_len];
5610
+ }
5611
+ };
5612
+
5613
+ const HoverMsg = union(enum) {
5614
+ outer_enter,
5615
+ outer_leave,
5616
+ row_enter: u32,
5617
+ row_leave: u32,
5618
+ vanish_enter,
5619
+ vanish_leave,
5620
+ unbind_outer,
5621
+ };
5622
+
5623
+ const HoverApp = ui_app_model.UiApp(HoverModel, HoverMsg);
5624
+
5625
+ fn hoverUpdate(model: *HoverModel, msg: HoverMsg) void {
5626
+ switch (msg) {
5627
+ .outer_enter => model.push(.outer_enter),
5628
+ .outer_leave => model.push(.outer_leave),
5629
+ .row_enter => |id| model.push(.{ .row_enter = id }),
5630
+ .row_leave => |id| model.push(.{ .row_leave = id }),
5631
+ .vanish_enter => {
5632
+ // The enter handler unmounts the hovered element: the very
5633
+ // next drain pass owes (and delivers) the captured leave.
5634
+ model.push(.vanish_enter);
5635
+ model.show_vanish = false;
5636
+ },
5637
+ .vanish_leave => model.push(.vanish_leave),
5638
+ .unbind_outer => model.outer_bound = false,
5639
+ }
5640
+ }
5641
+
5642
+ /// A listening panel (y 0..120) holding two listening rows (y 0..40 and
5643
+ /// 40..80; the band at 80..120 is the panel's own), then a listening row
5644
+ /// (y 120..160) whose ENTER unmounts it. Dead space below. Fixed heights
5645
+ /// so the tests probe laid-out geometry deterministically.
5646
+ fn hoverView(ui: *HoverApp.Ui, model: *const HoverModel) HoverApp.Ui.Node {
5647
+ const rows = ui.column(.{ .gap = 0 }, .{
5648
+ ui.row(.{ .height = 40, .on_hover_enter = .{ .row_enter = 1 }, .on_hover_leave = .{ .row_leave = 1 } }, .{
5649
+ ui.text(.{}, "Row one"),
5650
+ }),
5651
+ ui.row(.{ .height = 40, .on_hover_enter = .{ .row_enter = 2 }, .on_hover_leave = .{ .row_leave = 2 } }, .{
5652
+ ui.text(.{}, "Row two"),
5653
+ }),
5654
+ });
5655
+ const outer = if (model.outer_bound)
5656
+ ui.panel(.{ .height = 120, .on_hover_enter = .outer_enter, .on_hover_leave = .outer_leave }, .{rows})
5657
+ else
5658
+ ui.panel(.{ .height = 120 }, .{rows});
5659
+ if (model.show_vanish) {
5660
+ return ui.column(.{ .gap = 0 }, .{
5661
+ outer,
5662
+ ui.row(.{ .height = 40, .on_hover_enter = .vanish_enter, .on_hover_leave = .vanish_leave }, .{
5663
+ ui.text(.{}, "Vanish"),
5664
+ }),
5665
+ });
5666
+ }
5667
+ return ui.column(.{ .gap = 0 }, .{outer});
5668
+ }
5669
+
5670
+ const hover_views = [_]app_manifest.ShellView{
5671
+ .{ .label = canvas_label, .kind = .gpu_surface, .fill = true, .gpu_backend = .metal },
5672
+ };
5673
+ const hover_windows = [_]app_manifest.ShellWindow{.{
5674
+ .label = "main",
5675
+ .title = "Hover",
5676
+ .width = 400,
5677
+ .height = 300,
5678
+ .views = &hover_views,
5679
+ }};
5680
+ const hover_scene: app_manifest.ShellConfig = .{ .windows = &hover_windows };
5681
+
5682
+ fn hoverOptions() HoverApp.Options {
5683
+ return .{
5684
+ .name = "ui-app-hover",
5685
+ .scene = hover_scene,
5686
+ .canvas_label = canvas_label,
5687
+ .update = hoverUpdate,
5688
+ .view = hoverView,
5689
+ };
5690
+ }
5691
+
5692
+ fn hoverMove(harness: anytype, app: anytype, x: f32, y: f32) !void {
5693
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5694
+ .window_id = 1,
5695
+ .label = canvas_label,
5696
+ .kind = .pointer_move,
5697
+ .x = x,
5698
+ .y = y,
5699
+ } });
5700
+ }
5701
+
5702
+ fn expectHoverLog(model: *const HoverModel, expected: []const HoverEntry) !void {
5703
+ try std.testing.expectEqualSlices(HoverEntry, expected, model.entries());
5704
+ }
5705
+
5706
+ test "hover msgs dispatch containment edges: nested listeners, cancel, and coalesced moves" {
5707
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
5708
+ defer harness.destroy(std.testing.allocator);
5709
+ harness.null_platform.gpu_surfaces = true;
5710
+
5711
+ const app_state = try std.testing.allocator.create(HoverApp);
5712
+ defer std.testing.allocator.destroy(app_state);
5713
+ app_state.* = HoverApp.init(std.heap.page_allocator, .{}, hoverOptions());
5714
+ defer app_state.deinit();
5715
+ const app = app_state.app();
5716
+ try harness.start(app);
5717
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
5718
+ .label = canvas_label,
5719
+ .size = geometry.SizeF.init(400, 300),
5720
+ .scale_factor = 2,
5721
+ .frame_index = 1,
5722
+ .timestamp_ns = 1_000_000,
5723
+ .nonblank = true,
5724
+ } });
5725
+
5726
+ // Dead space: nothing listens, nothing dispatches, and the runtime
5727
+ // holds no standing chain (unbound surfaces pay nothing).
5728
+ try hoverMove(harness, app, 50, 220);
5729
+ try expectHoverLog(&app_state.model, &.{});
5730
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.views[0].canvas_widget_hover_msg_chain_len);
5731
+
5732
+ // Onto row one (over its plain TEXT child — containment falls
5733
+ // through to the listeners): enters fire outermost first.
5734
+ try hoverMove(harness, app, 50, 20);
5735
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 } });
5736
+
5737
+ // Row one to row two: the shared panel ancestor stays entered — the
5738
+ // pointer never left it, the DOM mouseenter/mouseleave rule.
5739
+ try hoverMove(harness, app, 50, 60);
5740
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 }, .{ .row_leave = 1 }, .{ .row_enter = 2 } });
5741
+
5742
+ // Onto the panel's own band below the rows: only the row leaves.
5743
+ try hoverMove(harness, app, 50, 100);
5744
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 }, .{ .row_leave = 1 }, .{ .row_enter = 2 }, .{ .row_leave = 2 } });
5745
+
5746
+ // Off the panel entirely.
5747
+ try hoverMove(harness, app, 50, 220);
5748
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 }, .{ .row_leave = 1 }, .{ .row_enter = 2 }, .{ .row_leave = 2 }, .outer_leave });
5749
+
5750
+ // Back inside, then the pointer LEAVES THE VIEW (the pointer-cancel
5751
+ // hosts emit on window exit): leaves fire innermost first.
5752
+ app_state.model = .{};
5753
+ try hoverMove(harness, app, 50, 60);
5754
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 2 } });
5755
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5756
+ .window_id = 1,
5757
+ .label = canvas_label,
5758
+ .kind = .pointer_cancel,
5759
+ .x = 50,
5760
+ .y = 60,
5761
+ } });
5762
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 2 }, .{ .row_leave = 2 }, .outer_leave });
5763
+ }
5764
+
5765
+ test "unbinding an outer listener leaves it alone: the standing inner row never flickers" {
5766
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
5767
+ defer harness.destroy(std.testing.allocator);
5768
+ harness.null_platform.gpu_surfaces = true;
5769
+
5770
+ const app_state = try std.testing.allocator.create(HoverApp);
5771
+ defer std.testing.allocator.destroy(app_state);
5772
+ app_state.* = HoverApp.init(std.heap.page_allocator, .{}, hoverOptions());
5773
+ defer app_state.deinit();
5774
+ const app = app_state.app();
5775
+ try harness.start(app);
5776
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
5777
+ .label = canvas_label,
5778
+ .size = geometry.SizeF.init(400, 300),
5779
+ .scale_factor = 2,
5780
+ .frame_index = 1,
5781
+ .timestamp_ns = 1_000_000,
5782
+ .nonblank = true,
5783
+ } });
5784
+
5785
+ // Inside row one: panel and row both stand.
5786
+ try hoverMove(harness, app, 50, 20);
5787
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 } });
5788
+
5789
+ // A rebuild removes only the PANEL's hover bindings: the panel's
5790
+ // leave dispatches, and the row — whose containment never changed —
5791
+ // dispatches NOTHING (per-listener containment, not a positional
5792
+ // chain).
5793
+ try app_state.dispatch(&harness.runtime, 1, .unbind_outer);
5794
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 }, .outer_leave });
5795
+
5796
+ // The row's own exit still delivers its captured leave.
5797
+ try hoverMove(harness, app, 50, 220);
5798
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 }, .outer_leave, .{ .row_leave = 1 } });
5799
+ }
5800
+
5801
+ test "hover enter that unmounts its element still delivers the captured leave" {
5802
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
5803
+ defer harness.destroy(std.testing.allocator);
5804
+ harness.null_platform.gpu_surfaces = true;
5805
+
5806
+ const app_state = try std.testing.allocator.create(HoverApp);
5807
+ defer std.testing.allocator.destroy(app_state);
5808
+ app_state.* = HoverApp.init(std.heap.page_allocator, .{}, hoverOptions());
5809
+ defer app_state.deinit();
5810
+ const app = app_state.app();
5811
+ try harness.start(app);
5812
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
5813
+ .label = canvas_label,
5814
+ .size = geometry.SizeF.init(400, 300),
5815
+ .scale_factor = 2,
5816
+ .frame_index = 1,
5817
+ .timestamp_ns = 1_000_000,
5818
+ .nonblank = true,
5819
+ } });
5820
+
5821
+ // The enter handler hides the row; the rebuild prunes it from the
5822
+ // standing chain and the SAME event's drain delivers the leave Msg
5823
+ // captured when the enter dispatched — the live tree can no longer
5824
+ // resolve it. No enter without its eventual leave.
5825
+ try hoverMove(harness, app, 50, 140);
5826
+ try expectHoverLog(&app_state.model, &.{ .vanish_enter, .vanish_leave });
5827
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.views[0].canvas_widget_hover_msg_chain_len);
5828
+
5829
+ // The pointer now stands over dead space (the row is gone); moving
5830
+ // again dispatches nothing further.
5831
+ try hoverMove(harness, app, 50, 141);
5832
+ try expectHoverLog(&app_state.model, &.{ .vanish_enter, .vanish_leave });
5833
+ }
5834
+
5835
+ // ------------------------------------------------- hover-under-scroll fixture
5836
+
5837
+ const ScrollHoverModel = struct {
5838
+ log: [24]HoverEntry = undefined,
5839
+ log_len: usize = 0,
5840
+
5841
+ fn push(model: *ScrollHoverModel, entry: HoverEntry) void {
5842
+ if (model.log_len >= model.log.len) return;
5843
+ model.log[model.log_len] = entry;
5844
+ model.log_len += 1;
5845
+ }
5846
+
5847
+ fn entries(model: *const ScrollHoverModel) []const HoverEntry {
5848
+ return model.log[0..model.log_len];
5849
+ }
5850
+ };
5851
+
5852
+ const ScrollHoverApp = ui_app_model.UiApp(ScrollHoverModel, HoverMsg);
5853
+
5854
+ fn scrollHoverUpdate(model: *ScrollHoverModel, msg: HoverMsg) void {
5855
+ switch (msg) {
5856
+ .row_enter => |id| model.push(.{ .row_enter = id }),
5857
+ .row_leave => |id| model.push(.{ .row_leave = id }),
5858
+ else => {},
5859
+ }
5860
+ }
5861
+
5862
+ /// Ten listening rows of 40 points inside a 120-point scroll viewport:
5863
+ /// scrolling moves rows under a stationary pointer.
5864
+ fn scrollHoverView(ui: *ScrollHoverApp.Ui, model: *const ScrollHoverModel) ScrollHoverApp.Ui.Node {
5865
+ _ = model;
5866
+ var rows: [10]ScrollHoverApp.Ui.Node = undefined;
5867
+ for (&rows, 0..) |*node, index| {
5868
+ const id: u32 = @intCast(index + 1);
5869
+ node.* = ui.row(.{
5870
+ .height = 40,
5871
+ .key = canvas.uiKey(@as(u64, id)),
5872
+ .on_hover_enter = .{ .row_enter = id },
5873
+ .on_hover_leave = .{ .row_leave = id },
5874
+ }, .{ui.text(.{}, ui.fmt("Row {d}", .{id}))});
5875
+ }
5876
+ const row_nodes: []const ScrollHoverApp.Ui.Node = rows[0..];
5877
+ return ui.scroll(.{ .height = 120 }, .{ui.column(.{ .gap = 0 }, row_nodes)});
5878
+ }
5879
+
5880
+ fn scrollHoverOptions() ScrollHoverApp.Options {
5881
+ return .{
5882
+ .name = "ui-app-scroll-hover",
5883
+ .scene = hover_scene,
5884
+ .canvas_label = canvas_label,
5885
+ .update = scrollHoverUpdate,
5886
+ .view = scrollHoverView,
5887
+ };
5888
+ }
5889
+
5890
+ test "hover msgs re-resolve when content scrolls under a stationary pointer" {
5891
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
5892
+ defer harness.destroy(std.testing.allocator);
5893
+ harness.null_platform.gpu_surfaces = true;
5894
+
5895
+ const app_state = try std.testing.allocator.create(ScrollHoverApp);
5896
+ defer std.testing.allocator.destroy(app_state);
5897
+ app_state.* = ScrollHoverApp.init(std.heap.page_allocator, .{}, scrollHoverOptions());
5898
+ defer app_state.deinit();
5899
+ const app = app_state.app();
5900
+ try harness.start(app);
5901
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
5902
+ .label = canvas_label,
5903
+ .size = geometry.SizeF.init(400, 300),
5904
+ .scale_factor = 2,
5905
+ .frame_index = 1,
5906
+ .timestamp_ns = 1_000_000,
5907
+ .nonblank = true,
5908
+ } });
5909
+
5910
+ // Park the pointer on row 1.
5911
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5912
+ .window_id = 1,
5913
+ .label = canvas_label,
5914
+ .kind = .pointer_move,
5915
+ .x = 50,
5916
+ .y = 20,
5917
+ } });
5918
+ try std.testing.expectEqualSlices(HoverEntry, &.{.{ .row_enter = 1 }}, app_state.model.entries());
5919
+
5920
+ // A wheel scroll of 80 points slides row 3 under the stationary
5921
+ // pointer: the same leave/enter a real move off row 1 onto row 3
5922
+ // would dispatch — content moved, containment followed.
5923
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5924
+ .window_id = 1,
5925
+ .label = canvas_label,
5926
+ .kind = .scroll,
5927
+ .timestamp_ns = 2_000_000,
5928
+ .x = 50,
5929
+ .y = 20,
5930
+ .delta_y = 80,
5931
+ } });
5932
+ try std.testing.expectEqualSlices(HoverEntry, &.{ .{ .row_enter = 1 }, .{ .row_leave = 1 }, .{ .row_enter = 3 } }, app_state.model.entries());
5933
+ }
5934
+
5935
+ test "touch-shaped input (down/up/drag without hover) never synthesizes hover msgs" {
5936
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
5937
+ defer harness.destroy(std.testing.allocator);
5938
+ harness.null_platform.gpu_surfaces = true;
5939
+
5940
+ const app_state = try std.testing.allocator.create(HoverApp);
5941
+ defer std.testing.allocator.destroy(app_state);
5942
+ app_state.* = HoverApp.init(std.heap.page_allocator, .{}, hoverOptions());
5943
+ defer app_state.deinit();
5944
+ const app = app_state.app();
5945
+ try harness.start(app);
5946
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
5947
+ .label = canvas_label,
5948
+ .size = geometry.SizeF.init(400, 300),
5949
+ .scale_factor = 2,
5950
+ .frame_index = 1,
5951
+ .timestamp_ns = 1_000_000,
5952
+ .nonblank = true,
5953
+ } });
5954
+
5955
+ // A tap (down/up) and a scrub (drag) over a listening row — the
5956
+ // touch input shape, which never includes a hover-phase move —
5957
+ // dispatch no hover Msgs and leave no standing containment: touch
5958
+ // cannot hover, mechanically.
5959
+ for ([_]zero_platform.GpuSurfaceInputKind{ .pointer_down, .pointer_drag, .pointer_up }) |kind| {
5960
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5961
+ .window_id = 1,
5962
+ .label = canvas_label,
5963
+ .kind = kind,
5964
+ .x = 50,
5965
+ .y = 20,
5966
+ } });
5967
+ }
5968
+ try expectHoverLog(&app_state.model, &.{});
5969
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.views[0].canvas_widget_hover_msg_chain_len);
5970
+
5971
+ // A TOUCH-STAMPED hover-phase move (the mouse-shaped float Windows
5972
+ // synthesizes for taps, marked by the host with the reserved
5973
+ // pointer-id bit) earns no proof either: the fake float dispatches
5974
+ // nothing and leaves no standing containment.
5975
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5976
+ .window_id = 1,
5977
+ .label = canvas_label,
5978
+ .kind = .pointer_move,
5979
+ .pointer_id = zero_platform.touch_pointer_id_bit | 1,
5980
+ .x = 50,
5981
+ .y = 20,
5982
+ } });
5983
+ try expectHoverLog(&app_state.model, &.{});
5984
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.views[0].canvas_widget_hover_msg_chain_len);
5985
+
5986
+ // A hover-phase move proves a hover-capable pointer; from then on
5987
+ // clicks and drags keep full mouse fidelity.
5988
+ try hoverMove(harness, app, 50, 20);
5989
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 } });
5990
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
5991
+ .window_id = 1,
5992
+ .label = canvas_label,
5993
+ .kind = .pointer_drag,
5994
+ .x = 50,
5995
+ .y = 220,
5996
+ } });
5997
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 }, .{ .row_leave = 1 }, .outer_leave });
5998
+ }
5999
+
6000
+ // ------------------------------------------------ hover capture fixture
6001
+
6002
+ const CaptureModel = struct {
6003
+ churns: u32 = 0,
6004
+ show_row: bool = true,
6005
+ bind_leave: bool = true,
6006
+ exploded: bool = false,
6007
+ two_entered: u32 = 0,
6008
+ two_left: u32 = 0,
6009
+ field: canvas.TextBuffer(16) = .{},
6010
+ left_storage: [32]u8 = [_]u8{0} ** 32,
6011
+ left_len: usize = 0,
6012
+
6013
+ fn leftText(model: *const CaptureModel) []const u8 {
6014
+ return model.left_storage[0..model.left_len];
6015
+ }
6016
+
6017
+ pub fn fieldText(model: *const CaptureModel) []const u8 {
6018
+ return model.field.text();
6019
+ }
6020
+ };
6021
+
6022
+ const CaptureMsg = union(enum) {
6023
+ churn,
6024
+ hide_row,
6025
+ entered,
6026
+ left: []const u8,
6027
+ entered_two,
6028
+ left_two,
6029
+ bind_leave_now,
6030
+ explode,
6031
+ calm,
6032
+ field_edit: canvas.TextInputEvent,
6033
+ };
6034
+
6035
+ const CaptureApp = ui_app_model.UiApp(CaptureModel, CaptureMsg);
6036
+
6037
+ fn captureUpdate(model: *CaptureModel, msg: CaptureMsg) void {
6038
+ switch (msg) {
6039
+ .churn => model.churns += 1,
6040
+ .hide_row => model.show_row = false,
6041
+ .entered => {},
6042
+ .left => |text| {
6043
+ const len = @min(text.len, model.left_storage.len);
6044
+ @memcpy(model.left_storage[0..len], text[0..len]);
6045
+ model.left_len = len;
6046
+ },
6047
+ .entered_two => model.two_entered += 1,
6048
+ .left_two => model.two_left += 1,
6049
+ .bind_leave_now => model.bind_leave = true,
6050
+ .explode => model.exploded = true,
6051
+ .calm => model.exploded = false,
6052
+ .field_edit => |edit| {
6053
+ // Any edit — the standalone accessibility selection verb
6054
+ // included — unmounts the hovered row, the shape that
6055
+ // strands a leave if the standalone event skips the drain.
6056
+ model.field.apply(edit);
6057
+ model.show_row = false;
6058
+ },
6059
+ }
6060
+ }
6061
+
6062
+ /// The leave Msg's payload is a `ui.fmt` slice — build-arena bytes that
6063
+ /// the arena pair recycles two builds later. The capture must own them.
6064
+ /// `exploded` renders a view past the per-view widget-node budget, so
6065
+ /// its rebuild FAILS AT PUBLICATION — `self.tree` moved to a build the
6066
+ /// runtime never adopted, the exact stale-tree seam the hover drain's
6067
+ /// currency invariant covers.
6068
+ fn captureView(ui: *CaptureApp.Ui, model: *const CaptureModel) CaptureApp.Ui.Node {
6069
+ if (model.exploded) {
6070
+ var rows: [1100]CaptureApp.Ui.Node = undefined;
6071
+ for (&rows, 0..) |*node, index| {
6072
+ node.* = ui.text(.{ .key = canvas.uiKey(@as(u64, index)) }, "x");
6073
+ }
6074
+ const row_nodes: []const CaptureApp.Ui.Node = rows[0..];
6075
+ return ui.column(.{ .gap = 0 }, row_nodes);
6076
+ }
6077
+ if (!model.show_row) {
6078
+ return ui.column(.{ .gap = 0 }, .{ui.text(.{}, ui.fmt("churn {d}", .{model.churns}))});
6079
+ }
6080
+ return ui.column(.{ .gap = 0 }, .{
6081
+ ui.row(.{
6082
+ .height = 40,
6083
+ .on_hover_enter = .entered,
6084
+ .on_hover_leave = if (model.bind_leave) CaptureMsg{ .left = ui.fmt("left-after-{d}-churns", .{model.churns}) } else null,
6085
+ }, .{ui.text(.{}, "Capture row")}),
6086
+ ui.row(.{
6087
+ .height = 40,
6088
+ .on_hover_enter = .entered_two,
6089
+ .on_hover_leave = .left_two,
6090
+ }, .{ui.text(.{}, "Second row")}),
6091
+ ui.el(.text_field, .{
6092
+ .height = 30,
6093
+ .text = model.fieldText(),
6094
+ .placeholder = "notes",
6095
+ .on_input = CaptureApp.Ui.inputMsg(.field_edit),
6096
+ }, .{}),
6097
+ ui.text(.{}, ui.fmt("churn {d}", .{model.churns})),
6098
+ });
6099
+ }
6100
+
6101
+ fn captureCommand(name: []const u8) ?CaptureMsg {
6102
+ if (std.mem.eql(u8, name, "capture.churn")) return .churn;
6103
+ if (std.mem.eql(u8, name, "capture.hide")) return .hide_row;
6104
+ return null;
6105
+ }
6106
+
6107
+ fn captureOptions() CaptureApp.Options {
6108
+ return .{
6109
+ .name = "ui-app-hover-capture",
6110
+ .scene = hover_scene,
6111
+ .canvas_label = canvas_label,
6112
+ .update = captureUpdate,
6113
+ .view = captureView,
6114
+ .on_command = captureCommand,
6115
+ };
6116
+ }
6117
+
6118
+ test "a captured hover-leave msg owns its arena payload across rebuilds and unmount" {
6119
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6120
+ defer harness.destroy(std.testing.allocator);
6121
+ harness.null_platform.gpu_surfaces = true;
6122
+
6123
+ const app_state = try std.testing.allocator.create(CaptureApp);
6124
+ defer std.testing.allocator.destroy(app_state);
6125
+ app_state.* = CaptureApp.init(std.heap.page_allocator, .{}, captureOptions());
6126
+ defer app_state.deinit();
6127
+ const app = app_state.app();
6128
+ try harness.start(app);
6129
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
6130
+ .label = canvas_label,
6131
+ .size = geometry.SizeF.init(400, 300),
6132
+ .scale_factor = 2,
6133
+ .frame_index = 1,
6134
+ .timestamp_ns = 1_000_000,
6135
+ .nonblank = true,
6136
+ } });
6137
+
6138
+ // Enter the row: its leave Msg carries a `ui.fmt` payload built in
6139
+ // THIS build's arena.
6140
+ try hoverMove(harness, app, 50, 20);
6141
+
6142
+ // Churn rebuilds until the arena pair has recycled the bytes the
6143
+ // enter-time payload lived in. The capture deep-copies each build's
6144
+ // LATEST binding (kept fresh as the trees change), so the payload
6145
+ // is always owned bytes, never a pointer into a recycled arena.
6146
+ for (0..3) |_| {
6147
+ try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "capture.churn", .window_id = 1 } });
6148
+ }
6149
+ try std.testing.expectEqual(@as(u32, 3), app_state.model.churns);
6150
+
6151
+ // Unmount the row under the stationary pointer: the captured leave
6152
+ // dispatches the last build's binding, bytes intact.
6153
+ try harness.runtime.dispatchPlatformEvent(app, .{ .menu_command = .{ .name = "capture.hide", .window_id = 1 } });
6154
+ try std.testing.expectEqualStrings("left-after-3-churns", app_state.model.leftText());
6155
+ }
6156
+
6157
+ test "a direct dispatch that unmounts the hovered element delivers its leave immediately" {
6158
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6159
+ defer harness.destroy(std.testing.allocator);
6160
+ harness.null_platform.gpu_surfaces = true;
6161
+
6162
+ const app_state = try std.testing.allocator.create(CaptureApp);
6163
+ defer std.testing.allocator.destroy(app_state);
6164
+ app_state.* = CaptureApp.init(std.heap.page_allocator, .{}, captureOptions());
6165
+ defer app_state.deinit();
6166
+ const app = app_state.app();
6167
+ try harness.start(app);
6168
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
6169
+ .label = canvas_label,
6170
+ .size = geometry.SizeF.init(400, 300),
6171
+ .scale_factor = 2,
6172
+ .frame_index = 1,
6173
+ .timestamp_ns = 1_000_000,
6174
+ .nonblank = true,
6175
+ } });
6176
+ try hoverMove(harness, app, 50, 20);
6177
+
6178
+ // A DIRECT dispatch (a command handler, an embedder, a test) that
6179
+ // unmounts the hovered row settles the leave at its own tail — no
6180
+ // later platform event required.
6181
+ try app_state.dispatch(&harness.runtime, 1, .hide_row);
6182
+ try std.testing.expectEqualStrings("left-after-0-churns", app_state.model.leftText());
6183
+ }
6184
+
6185
+ test "another pointer's contact never rides a mouse's hover proof" {
6186
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6187
+ defer harness.destroy(std.testing.allocator);
6188
+ harness.null_platform.gpu_surfaces = true;
6189
+
6190
+ const app_state = try std.testing.allocator.create(HoverApp);
6191
+ defer std.testing.allocator.destroy(app_state);
6192
+ app_state.* = HoverApp.init(std.heap.page_allocator, .{}, hoverOptions());
6193
+ defer app_state.deinit();
6194
+ const app = app_state.app();
6195
+ try harness.start(app);
6196
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
6197
+ .label = canvas_label,
6198
+ .size = geometry.SizeF.init(400, 300),
6199
+ .scale_factor = 2,
6200
+ .frame_index = 1,
6201
+ .timestamp_ns = 1_000_000,
6202
+ .nonblank = true,
6203
+ } });
6204
+
6205
+ // The mouse (pointer 0) proves hover over row one.
6206
+ try hoverMove(harness, app, 50, 20);
6207
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 } });
6208
+
6209
+ // A touch contact (another pointer id) taps row two and lifts with
6210
+ // a cancel: on a host that distinguishes pointers, none of it moves
6211
+ // the mouse's standing containment.
6212
+ for ([_]zero_platform.GpuSurfaceInputKind{ .pointer_down, .pointer_up, .pointer_cancel }) |kind| {
6213
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
6214
+ .window_id = 1,
6215
+ .label = canvas_label,
6216
+ .kind = kind,
6217
+ .pointer_id = 7,
6218
+ .x = 50,
6219
+ .y = 60,
6220
+ } });
6221
+ }
6222
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 } });
6223
+
6224
+ // The mouse's own departure retires it.
6225
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
6226
+ .window_id = 1,
6227
+ .label = canvas_label,
6228
+ .kind = .pointer_cancel,
6229
+ .x = 50,
6230
+ .y = 20,
6231
+ } });
6232
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 }, .{ .row_leave = 1 }, .outer_leave });
6233
+ }
6234
+
6235
+ test "a leave handler added while the listener stands is captured before the unmount needs it" {
6236
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6237
+ defer harness.destroy(std.testing.allocator);
6238
+ harness.null_platform.gpu_surfaces = true;
6239
+
6240
+ const app_state = try std.testing.allocator.create(CaptureApp);
6241
+ defer std.testing.allocator.destroy(app_state);
6242
+ app_state.* = CaptureApp.init(std.heap.page_allocator, .{}, captureOptions());
6243
+ defer app_state.deinit();
6244
+ app_state.model.bind_leave = false;
6245
+ const app = app_state.app();
6246
+ try harness.start(app);
6247
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
6248
+ .label = canvas_label,
6249
+ .size = geometry.SizeF.init(400, 300),
6250
+ .scale_factor = 2,
6251
+ .frame_index = 1,
6252
+ .timestamp_ns = 1_000_000,
6253
+ .nonblank = true,
6254
+ } });
6255
+
6256
+ // Enter with NO leave handler bound: nothing to capture yet.
6257
+ try hoverMove(harness, app, 50, 20);
6258
+ try std.testing.expectEqualStrings("", app_state.model.leftText());
6259
+
6260
+ // A rebuild ADDS the leave binding while the listener stands: the
6261
+ // capture refresh picks it up (id-identical chains included), so
6262
+ // the later unmount can still deliver the pair.
6263
+ try app_state.dispatch(&harness.runtime, 1, .bind_leave_now);
6264
+ try app_state.dispatch(&harness.runtime, 1, .hide_row);
6265
+ try std.testing.expectEqualStrings("left-after-0-churns", app_state.model.leftText());
6266
+ }
6267
+
6268
+ test "hover edges defer while a tree is stale and deliver after recovery" {
6269
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6270
+ defer harness.destroy(std.testing.allocator);
6271
+ harness.null_platform.gpu_surfaces = true;
6272
+
6273
+ const app_state = try std.testing.allocator.create(CaptureApp);
6274
+ defer std.testing.allocator.destroy(app_state);
6275
+ app_state.* = CaptureApp.init(std.heap.page_allocator, .{}, captureOptions());
6276
+ defer app_state.deinit();
6277
+ const app = app_state.app();
6278
+ try harness.start(app);
6279
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
6280
+ .label = canvas_label,
6281
+ .size = geometry.SizeF.init(400, 300),
6282
+ .scale_factor = 2,
6283
+ .frame_index = 1,
6284
+ .timestamp_ns = 1_000_000,
6285
+ .nonblank = true,
6286
+ } });
6287
+
6288
+ // White-box: mark the main tree stale, the state a failure INSIDE
6289
+ // the install window leaves behind (publication adopted, handler
6290
+ // tree not yet) — the narrow seam no budget lever reaches
6291
+ // deterministically from outside. An enter-only transition then
6292
+ // has nothing deliverable: the enter DEFERS rather than resolving
6293
+ // through the stale tree (any owed leave would still dispatch and
6294
+ // its rebuild would heal the staleness — the pure-enter case is
6295
+ // the one that must wait).
6296
+ app_state.main_tree_current = false;
6297
+ try hoverMove(harness, app, 50, 60);
6298
+ try std.testing.expectEqual(@as(u32, 0), app_state.model.two_entered);
6299
+
6300
+ // Recovery: the next successful rebuild restores currency at the
6301
+ // install seam, and its own drain delivers the deferred enter.
6302
+ try app_state.dispatch(&harness.runtime, 1, .churn);
6303
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.two_entered);
6304
+
6305
+ // Leaves never wait on currency: stale again, moving off the row
6306
+ // still delivers the captured leave — and that dispatch's own
6307
+ // successful rebuild heals the flag at the install seam.
6308
+ app_state.main_tree_current = false;
6309
+ try hoverMove(harness, app, 50, 220);
6310
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.two_left);
6311
+ try std.testing.expect(app_state.main_tree_current);
6312
+ }
6313
+
6314
+ test "a failed build keeps the still-matching pair live: edges flow through it" {
6315
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6316
+ defer harness.destroy(std.testing.allocator);
6317
+ harness.null_platform.gpu_surfaces = true;
6318
+
6319
+ const app_state = try std.testing.allocator.create(CaptureApp);
6320
+ defer std.testing.allocator.destroy(app_state);
6321
+ app_state.* = CaptureApp.init(std.heap.page_allocator, .{}, captureOptions());
6322
+ defer app_state.deinit();
6323
+ const app = app_state.app();
6324
+ try harness.start(app);
6325
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
6326
+ .label = canvas_label,
6327
+ .size = geometry.SizeF.init(400, 300),
6328
+ .scale_factor = 2,
6329
+ .frame_index = 1,
6330
+ .timestamp_ns = 1_000_000,
6331
+ .nonblank = true,
6332
+ } });
6333
+ try hoverMove(harness, app, 50, 20);
6334
+
6335
+ // The explode rebuild fails at LAYOUT — before publication, before
6336
+ // `self.tree` moves — so the OLD handler tree still matches the
6337
+ // runtime's retained state and stays CURRENT: a failure outside
6338
+ // the install window must never defer hover edges (an idle app
6339
+ // might perform no further rebuild to lift the deferral).
6340
+ if (app_state.dispatch(&harness.runtime, 1, .explode)) |_| {
6341
+ return error.TestUnexpectedResult;
6342
+ } else |_| {}
6343
+ try std.testing.expect(app_state.model.exploded);
6344
+
6345
+ // The pointer moves to the second row (hit-tested against the
6346
+ // retained tree the failed build never replaced): row one's
6347
+ // captured leave AND row two's enter both flow through the
6348
+ // still-matching pair. Each edge's own dispatch rebuild fails (the
6349
+ // model remains exploded), so the event ERRORS after the Msgs
6350
+ // landed — the degradation contract: edges delivered, failures
6351
+ // loud.
6352
+ if (hoverMove(harness, app, 50, 60)) |_| {
6353
+ return error.TestUnexpectedResult;
6354
+ } else |_| {}
6355
+ try std.testing.expectEqualStrings("left-after-0-churns", app_state.model.leftText());
6356
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.two_entered);
6357
+
6358
+ // Recovery is clean: a successful rebuild changes nothing owed.
6359
+ try app_state.dispatch(&harness.runtime, 1, .calm);
6360
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.two_entered);
6361
+
6362
+ // And the pair completes on exit.
6363
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
6364
+ .window_id = 1,
6365
+ .label = canvas_label,
6366
+ .kind = .pointer_cancel,
6367
+ .x = 50,
6368
+ .y = 60,
6369
+ } });
6370
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.two_left);
6371
+ }
6372
+
6373
+ // -------------------------------------------- over-aligned payload probe
6374
+
6375
+ const AlignedModel = struct {
6376
+ bytes: [64]u8 align(64) = [_]u8{'a'} ** 64,
6377
+ show_row: bool = true,
6378
+ left_len: usize = 0,
6379
+ z_payload: [4]u8 = [_]u8{0} ** 4,
6380
+
6381
+ fn payload(model: *const AlignedModel) []align(64) const u8 {
6382
+ return @alignCast(model.bytes[0..8]);
6383
+ }
6384
+ };
6385
+
6386
+ const AlignedMsg = union(enum) {
6387
+ entered,
6388
+ left: []align(64) const u8,
6389
+ left_z: [3:0]u8,
6390
+ hide,
6391
+ };
6392
+
6393
+ /// Markup-free on purpose: the runtime markup interpreter's coercion
6394
+ /// vocabulary is plain `[]const u8`, so the over-aligned payload rides a
6395
+ /// builder-only app — which is exactly where such a payload can exist.
6396
+ const AlignedApp = ui_app_model.UiAppWithFeatures(AlignedModel, AlignedMsg, .{ .runtime_markup = false });
6397
+
6398
+ fn alignedUpdate(model: *AlignedModel, msg: AlignedMsg) void {
3889
6399
  switch (msg) {
3890
- .load_more => {
3891
- model.fetches += 1;
3892
- if (model.loaded < virtual_feed_cap) model.loaded += virtual_feed_batch;
6400
+ .entered => {},
6401
+ .left => |bytes| model.left_len = bytes.len,
6402
+ .left_z => |arr| {
6403
+ // Record the declared elements AND the sentinel storage the
6404
+ // copy must have stamped.
6405
+ model.z_payload = .{ arr[0], arr[1], arr[2], arr[3] };
3893
6406
  },
6407
+ .hide => model.show_row = false,
3894
6408
  }
3895
6409
  }
3896
6410
 
3897
- fn virtualFeedOptions(model: *const VirtualFeedModel) VirtualFeedApp.Ui.VirtualListOptions {
3898
- return .{
3899
- .id = "vfeed",
3900
- .item_count = model.loaded,
3901
- .item_extent = virtual_row_extent,
3902
- .overscan = 2,
3903
- .grow = 1,
3904
- .on_reach_end = .load_more,
3905
- };
3906
- }
3907
-
3908
- /// The windowed pattern: ask for the visible range, build ONLY those
3909
- /// rows from the model, hand both to `virtualList`. Deliberately no
3910
- /// `on_scroll` binding — the runtime re-derives the view on scroll for
3911
- /// mounted virtual lists on its own.
3912
- fn virtualFeedView(ui: *VirtualFeedApp.Ui, model: *const VirtualFeedModel) VirtualFeedApp.Ui.Node {
3913
- const options = virtualFeedOptions(model);
3914
- const window = ui.virtualWindow(options);
3915
- const rows = ui.arena.alloc(VirtualFeedApp.Ui.Node, window.itemCount()) catch {
3916
- ui.failed = true;
3917
- return ui.column(.{}, .{});
3918
- };
3919
- for (rows, 0..) |*row, offset| {
3920
- const index = window.start_index + offset;
3921
- var node = ui.listItem(.{}, ui.fmt("Item {d}", .{index}));
3922
- node.key = .{ .int = @intCast(index) };
3923
- row.* = node;
3924
- }
3925
- return ui.virtualList(options, window, .{rows});
6411
+ fn alignedView(ui: *AlignedApp.Ui, model: *const AlignedModel) AlignedApp.Ui.Node {
6412
+ if (!model.show_row) return ui.column(.{}, .{ui.text(.{}, "empty")});
6413
+ return ui.column(.{ .gap = 0 }, .{
6414
+ ui.row(.{
6415
+ .height = 40,
6416
+ .on_hover_enter = .entered,
6417
+ .on_hover_leave = .{ .left = model.payload() },
6418
+ }, .{ui.text(.{}, "Aligned row")}),
6419
+ ui.row(.{
6420
+ .height = 40,
6421
+ .on_hover_enter = .entered,
6422
+ .on_hover_leave = .{ .left_z = .{ 'x', 'y', 'z' } },
6423
+ }, .{ui.text(.{}, "Sentinel row")}),
6424
+ });
3926
6425
  }
3927
6426
 
3928
- fn virtualFeedAppOptions() VirtualFeedApp.Options {
6427
+ fn alignedOptions() AlignedApp.Options {
3929
6428
  return .{
3930
- .name = "ui-app-virtual-feed",
3931
- .scene = counter_scene,
6429
+ .name = "ui-app-hover-aligned",
6430
+ .scene = hover_scene,
3932
6431
  .canvas_label = canvas_label,
3933
- .update = virtualFeedUpdate,
3934
- .view = virtualFeedView,
6432
+ .update = alignedUpdate,
6433
+ .view = alignedView,
3935
6434
  };
3936
6435
  }
3937
6436
 
3938
- test "windowed virtual list scrolls, re-windows, budgets to the viewport, and fires reach-end with hysteresis" {
6437
+ test "an over-aligned slice leave payload captures, survives unmount, and delivers" {
3939
6438
  const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
3940
6439
  defer harness.destroy(std.testing.allocator);
3941
6440
  harness.null_platform.gpu_surfaces = true;
3942
6441
 
3943
- const app_state = try std.testing.allocator.create(VirtualFeedApp);
6442
+ const app_state = try std.testing.allocator.create(AlignedApp);
3944
6443
  defer std.testing.allocator.destroy(app_state);
3945
- app_state.* = VirtualFeedApp.init(std.heap.page_allocator, .{}, virtualFeedAppOptions());
6444
+ app_state.* = AlignedApp.init(std.heap.page_allocator, .{}, alignedOptions());
3946
6445
  defer app_state.deinit();
3947
6446
  const app = app_state.app();
3948
6447
  try harness.start(app);
3949
-
3950
6448
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
3951
6449
  .label = canvas_label,
3952
6450
  .size = geometry.SizeF.init(400, 300),
@@ -3955,161 +6453,250 @@ test "windowed virtual list scrolls, re-windows, budgets to the viewport, and fi
3955
6453
  .timestamp_ns = 1_000_000,
3956
6454
  .nonblank = true,
3957
6455
  } });
3958
- try std.testing.expect(app_state.installed);
3959
6456
 
3960
- // The first build materialized only the first window (300pt viewport
3961
- // at 24pt rows + 2 overscan = 15 rows), never the 400 loaded items.
3962
- const list_id = canvas.globalWidgetId(.scroll_view, .{ .str = "vfeed" });
3963
- try std.testing.expectEqual(list_id, app_state.tree.?.root.id);
3964
- try std.testing.expectEqual(@as(usize, 15), app_state.tree.?.root.children.len);
3965
- try std.testing.expectEqual(@as(usize, 0), app_state.tree.?.root.layout.virtual_first_index);
6457
+ // The capture deep-copies a `[]align(64) const u8` payload (the
6458
+ // alignment-preserving allocation path), and the unmount-driven
6459
+ // leave delivers it.
6460
+ try hoverMove(harness, app, 50, 20);
6461
+ try app_state.dispatch(&harness.runtime, 1, .hide);
6462
+ try std.testing.expectEqual(@as(usize, 8), app_state.model.left_len);
6463
+ }
3966
6464
 
3967
- // A wheel scroll with NO on_scroll binding still re-windows the
3968
- // view: the runtime owns the offset, the scroll observation itself
3969
- // triggers the re-derivation.
6465
+ test "a standalone accessibility edit that unmounts the hovered listener drains its leave" {
6466
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6467
+ defer harness.destroy(std.testing.allocator);
6468
+ harness.null_platform.gpu_surfaces = true;
6469
+
6470
+ const app_state = try std.testing.allocator.create(CaptureApp);
6471
+ defer std.testing.allocator.destroy(app_state);
6472
+ app_state.* = CaptureApp.init(std.heap.page_allocator, .{}, captureOptions());
6473
+ defer app_state.deinit();
6474
+ const app = app_state.app();
6475
+ try harness.start(app);
6476
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
6477
+ .label = canvas_label,
6478
+ .size = geometry.SizeF.init(400, 300),
6479
+ .scale_factor = 2,
6480
+ .frame_index = 1,
6481
+ .timestamp_ns = 1_000_000,
6482
+ .nonblank = true,
6483
+ } });
6484
+ try hoverMove(harness, app, 50, 20);
6485
+
6486
+ // The accessibility selection edit dispatches a STANDALONE keyboard
6487
+ // event — no gpu-surface input cycle follows it — and its Msg
6488
+ // unmounts the hovered row: the drain must run at the standalone
6489
+ // event's own tail, or the leave strands until an unrelated event.
6490
+ const field_id = findWidgetIdByText(app_state.tree.?, .text_field, "").?;
3970
6491
  var command_buffer: [96]u8 = undefined;
3971
- var wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 240", .{ canvas_label, list_id });
3972
- try harness.runtime.dispatchAutomationCommand(app, wheel);
3973
- try std.testing.expectEqual(@as(usize, 8), app_state.tree.?.root.layout.virtual_first_index);
3974
- try std.testing.expect(findIn(app_state.tree.?.root, .list_item, "Item 8") != null);
3975
- try std.testing.expect(findIn(app_state.tree.?.root, .list_item, "Item 0") == null);
3976
- const retained = try harness.runtime.canvasWidgetLayout(1, canvas_label);
3977
- try std.testing.expectEqual(@as(f32, 240), retained.findById(list_id).?.widget.value);
6492
+ const command = try std.fmt.bufPrint(&command_buffer, "widget-action {s} {d} set-selection 0 0", .{ canvas_label, field_id });
6493
+ try harness.runtime.dispatchAutomationCommand(app, command);
6494
+ try std.testing.expect(!app_state.model.show_row);
6495
+ try std.testing.expectEqualStrings("left-after-0-churns", app_state.model.leftText());
6496
+ }
3978
6497
 
3979
- // Row identity is the item, not the slot: item 10 was in both
3980
- // windows and kept its structural id across the shift.
3981
- // (The id is derived from the list id and the item key alone.)
3982
- try std.testing.expect(findIn(app_state.tree.?.root, .list_item, "Item 10") != null);
6498
+ test "two dispatches in one cycle deliver the LATEST leave binding on unmount" {
6499
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6500
+ defer harness.destroy(std.testing.allocator);
6501
+ harness.null_platform.gpu_surfaces = true;
3983
6502
 
3984
- // Scroll to the end of the loaded 400 items (content 9600, viewport
3985
- // 300, max offset 9300): the approach-end signal fires ONCE and the
3986
- // model appends a batch.
3987
- try std.testing.expectEqual(@as(u32, 0), app_state.model.fetches);
3988
- wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 9060", .{ canvas_label, list_id });
3989
- try harness.runtime.dispatchAutomationCommand(app, wheel);
3990
- try std.testing.expectEqual(@as(u32, 1), app_state.model.fetches);
3991
- try std.testing.expectEqual(@as(usize, 500), app_state.model.loaded);
6503
+ const app_state = try std.testing.allocator.create(CaptureApp);
6504
+ defer std.testing.allocator.destroy(app_state);
6505
+ app_state.* = CaptureApp.init(std.heap.page_allocator, .{}, captureOptions());
6506
+ defer app_state.deinit();
6507
+ const app = app_state.app();
6508
+ try harness.start(app);
6509
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
6510
+ .label = canvas_label,
6511
+ .size = geometry.SizeF.init(400, 300),
6512
+ .scale_factor = 2,
6513
+ .frame_index = 1,
6514
+ .timestamp_ns = 1_000_000,
6515
+ .nonblank = true,
6516
+ } });
6517
+ try hoverMove(harness, app, 50, 20);
6518
+
6519
+ // White-box cycle shape: two dispatches with the drain deferred to
6520
+ // the cycle's end (an event handler dispatching twice). The first
6521
+ // moves the leave payload, the second unmounts — the capture must
6522
+ // refresh at EACH rebuild commit, not just at drain time against
6523
+ // the final (unmounted) tree.
6524
+ app_state.hover_msg_event_depth = 1;
6525
+ try app_state.dispatch(&harness.runtime, 1, .churn);
6526
+ try app_state.dispatch(&harness.runtime, 1, .hide_row);
6527
+ app_state.hover_msg_event_depth = 0;
6528
+ try harness.runtime.dispatchPlatformEvent(app, .wake);
6529
+ try std.testing.expectEqualStrings("left-after-1-churns", app_state.model.leftText());
6530
+ }
3992
6531
 
3993
- // The appended batch grew the extent (12_000), pulling the offset
3994
- // out of the band — the next small scroll re-arms, no fire.
3995
- wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 24", .{ canvas_label, list_id });
3996
- try harness.runtime.dispatchAutomationCommand(app, wheel);
3997
- try std.testing.expectEqual(@as(u32, 1), app_state.model.fetches);
6532
+ test "a secondary drag released outside the view retires the entered chain" {
6533
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6534
+ defer harness.destroy(std.testing.allocator);
6535
+ harness.null_platform.gpu_surfaces = true;
3998
6536
 
3999
- // Ride to the new end: fires again (offset 9324 + 2376 = 11_700 =
4000
- // the new max), appends to the 600 cap.
4001
- wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 2376", .{ canvas_label, list_id });
4002
- try harness.runtime.dispatchAutomationCommand(app, wheel);
4003
- try std.testing.expectEqual(@as(u32, 2), app_state.model.fetches);
4004
- try std.testing.expectEqual(@as(usize, 600), app_state.model.loaded);
6537
+ const app_state = try std.testing.allocator.create(HoverApp);
6538
+ defer std.testing.allocator.destroy(app_state);
6539
+ app_state.* = HoverApp.init(std.heap.page_allocator, .{}, hoverOptions());
6540
+ defer app_state.deinit();
6541
+ const app = app_state.app();
6542
+ try harness.start(app);
6543
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
6544
+ .label = canvas_label,
6545
+ .size = geometry.SizeF.init(400, 300),
6546
+ .scale_factor = 2,
6547
+ .frame_index = 1,
6548
+ .timestamp_ns = 1_000_000,
6549
+ .nonblank = true,
6550
+ } });
4005
6551
 
4006
- // At the cap the model stops appending, so the extent stops
4007
- // growing — riding the end must NOT dispatch a fetch storm.
4008
- wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 24", .{ canvas_label, list_id });
4009
- try harness.runtime.dispatchAutomationCommand(app, wheel);
4010
- wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 2376", .{ canvas_label, list_id });
4011
- try harness.runtime.dispatchAutomationCommand(app, wheel);
4012
- try std.testing.expectEqual(@as(u32, 3), app_state.model.fetches);
4013
- wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} -24", .{ canvas_label, list_id });
4014
- try harness.runtime.dispatchAutomationCommand(app, wheel);
4015
- wheel = try std.fmt.bufPrint(&command_buffer, "widget-wheel {s} {d} 24", .{ canvas_label, list_id });
4016
- try harness.runtime.dispatchAutomationCommand(app, wheel);
4017
- try std.testing.expectEqual(@as(u32, 3), app_state.model.fetches);
6552
+ try hoverMove(harness, app, 50, 20);
6553
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 } });
6554
+
6555
+ // A right-drag: the consumed secondary stream freezes containment
6556
+ // through the gesture (a menu gesture is not hovering) — but a
6557
+ // release OUTSIDE the view is the pointer already gone, the leave
6558
+ // the freeze suppressed. No motion-leave will fire (the implicit
6559
+ // grab held it), so the release itself must retire the chain.
6560
+ for ([_]struct { kind: zero_platform.GpuSurfaceInputKind, x: f32, y: f32 }{
6561
+ .{ .kind = .pointer_down, .x = 50, .y = 20 },
6562
+ .{ .kind = .pointer_drag, .x = 450, .y = 320 },
6563
+ .{ .kind = .pointer_up, .x = 450, .y = 320 },
6564
+ }) |step| {
6565
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
6566
+ .window_id = 1,
6567
+ .label = canvas_label,
6568
+ .kind = step.kind,
6569
+ .button = 1,
6570
+ .x = step.x,
6571
+ .y = step.y,
6572
+ } });
6573
+ }
6574
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 }, .{ .row_leave = 1 }, .outer_leave });
6575
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.views[0].canvas_widget_hover_msg_chain_len);
6576
+ }
4018
6577
 
4019
- // Budget stays viewport-sized throughout: ~19 mounted rows against
4020
- // 600 loaded items, and the scroll semantics report the full extent.
4021
- const final_layout = try harness.runtime.canvasWidgetLayout(1, canvas_label);
4022
- try std.testing.expect(final_layout.nodes.len < 40);
4023
- const scroll_state = harness.runtime.views[0].canvasWidgetScrollStateById(list_id).?;
4024
- try std.testing.expectEqual(@as(f32, 600 * virtual_row_extent), scroll_state.content_extent);
6578
+ test "repeated re-entry with rebuilding edge handlers never exhausts capture slots" {
6579
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6580
+ defer harness.destroy(std.testing.allocator);
6581
+ harness.null_platform.gpu_surfaces = true;
4025
6582
 
4026
- // A window-growing resize converges within ONE rebuild: the first
4027
- // build pass reads the stale 300pt viewport, the coverage check sees
4028
- // the fresh 600pt geometry under-covered, and the retry pass builds
4029
- // the wider window against it.
4030
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_resized = .{
4031
- .window_id = 1,
6583
+ const app_state = try std.testing.allocator.create(HoverApp);
6584
+ defer std.testing.allocator.destroy(app_state);
6585
+ app_state.* = HoverApp.init(std.heap.page_allocator, .{}, hoverOptions());
6586
+ defer app_state.deinit();
6587
+ const app = app_state.app();
6588
+ try harness.start(app);
6589
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
4032
6590
  .label = canvas_label,
4033
- .frame = geometry.RectF.init(0, 0, 400, 600),
6591
+ .size = geometry.SizeF.init(400, 300),
4034
6592
  .scale_factor = 2,
6593
+ .frame_index = 1,
6594
+ .timestamp_ns = 1_000_000,
6595
+ .nonblank = true,
4035
6596
  } });
4036
- const root = app_state.tree.?.root;
4037
- const first_index = root.layout.virtual_first_index;
4038
- const mounted = root.children.len;
4039
- // Offset clamps to the new max (14_400 - 600 = 13_800): visible rows
4040
- // 575..600 must all be inside the built window.
4041
- try std.testing.expect(first_index <= 575);
4042
- try std.testing.expectEqual(@as(usize, 600), first_index + mounted);
4043
- try std.testing.expect(mounted >= 25);
6597
+
6598
+ // Every edge Msg rebuilds, and each rebuild's capture refresh can
6599
+ // fill a mid-transition slot the enter loop then replaces: cycling
6600
+ // far past the slot budget proves replacement always releases —
6601
+ // a leaked claim per cycle would exhaust the slots and trap.
6602
+ for (0..40) |_| {
6603
+ try hoverMove(harness, app, 50, 60);
6604
+ try hoverMove(harness, app, 50, 220);
6605
+ }
6606
+ var used: usize = 0;
6607
+ for (app_state.hover_msg_slot_used) |slot_used| used += @intFromBool(slot_used);
6608
+ try std.testing.expectEqual(@as(usize, 0), used);
4044
6609
  }
4045
6610
 
4046
- // ------------------------------------------------------- pinch channel
6611
+ // ---------------------------------------------- unownable payload probe
4047
6612
 
4048
- const ZoomModel = struct {
4049
- /// Cumulative gesture scale: the running product of `1 + delta`
4050
- /// across change events — the semantics the channel documents.
4051
- scale: f32 = 1,
4052
- begins: u32 = 0,
4053
- ends: u32 = 0,
4054
- anchor_x: f32 = 0,
4055
- anchor_y: f32 = 0,
4056
- /// Source-identity mirrors: the window and view the last pinch
4057
- /// event named (x/y are view-local, so a coordinate without its
4058
- /// view is not a position — multi-window apps tell pinches apart
4059
- /// by these).
4060
- window_id: u64 = 0,
4061
- label: []const u8 = "",
6613
+ const PtrModel = struct {
6614
+ entered: u32 = 0,
6615
+ anchor: u32 = 7,
6616
+ bind_ptr: bool = true,
4062
6617
  };
4063
6618
 
4064
- const ZoomMsg = union(enum) {
4065
- pinch: zero_platform.PinchEvent,
6619
+ const PtrMsg = union(enum) {
6620
+ entered,
6621
+ left_ptr: *const u32,
6622
+ bind_ptr_now,
4066
6623
  };
4067
6624
 
4068
- const ZoomApp = ui_app_model.UiApp(ZoomModel, ZoomMsg);
6625
+ /// Markup-free like the aligned probe: a single-item-pointer payload is
6626
+ /// builder-only territory.
6627
+ const PtrApp = ui_app_model.UiAppWithFeatures(PtrModel, PtrMsg, .{ .runtime_markup = false });
4069
6628
 
4070
- fn zoomUpdate(model: *ZoomModel, msg: ZoomMsg) void {
6629
+ fn ptrUpdate(model: *PtrModel, msg: PtrMsg) void {
4071
6630
  switch (msg) {
4072
- .pinch => |pinch| {
4073
- model.window_id = pinch.window_id;
4074
- model.label = pinch.label;
4075
- switch (pinch.phase) {
4076
- .begin => {
4077
- model.begins += 1;
4078
- model.anchor_x = pinch.x;
4079
- model.anchor_y = pinch.y;
4080
- },
4081
- .change => model.scale *= (1 + pinch.scale),
4082
- .end => model.ends += 1,
4083
- }
4084
- },
6631
+ .entered => model.entered += 1,
6632
+ .left_ptr => {},
6633
+ .bind_ptr_now => model.bind_ptr = true,
4085
6634
  }
4086
6635
  }
4087
6636
 
4088
- fn zoomView(ui: *ZoomApp.Ui, model: *const ZoomModel) ZoomApp.Ui.Node {
4089
- return ui.column(.{ .gap = 8, .padding = 12 }, .{
4090
- ui.text(.{}, ui.fmt("Zoom {d:.2}", .{model.scale})),
6637
+ fn ptrView(ui: *PtrApp.Ui, model: *const PtrModel) PtrApp.Ui.Node {
6638
+ return ui.column(.{ .gap = 0 }, .{
6639
+ ui.row(.{
6640
+ .height = 40,
6641
+ .on_hover_enter = .entered,
6642
+ .on_hover_leave = if (model.bind_ptr) PtrMsg{ .left_ptr = &model.anchor } else null,
6643
+ }, .{ui.text(.{}, "Pointer row")}),
4091
6644
  });
4092
6645
  }
4093
6646
 
4094
- fn zoomPinch(pinch: zero_platform.PinchEvent) ?ZoomMsg {
4095
- return .{ .pinch = pinch };
6647
+ fn ptrOptions() PtrApp.Options {
6648
+ return .{
6649
+ .name = "ui-app-hover-ptr",
6650
+ .scene = hover_scene,
6651
+ .canvas_label = canvas_label,
6652
+ .update = ptrUpdate,
6653
+ .view = ptrView,
6654
+ };
6655
+ }
6656
+
6657
+ test "an unownable leave payload disables the pair: no enter, no dangling leave" {
6658
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6659
+ defer harness.destroy(std.testing.allocator);
6660
+ harness.null_platform.gpu_surfaces = true;
6661
+
6662
+ const app_state = try std.testing.allocator.create(PtrApp);
6663
+ defer std.testing.allocator.destroy(app_state);
6664
+ app_state.* = PtrApp.init(std.heap.page_allocator, .{}, ptrOptions());
6665
+ defer app_state.deinit();
6666
+ const app = app_state.app();
6667
+ try harness.start(app);
6668
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
6669
+ .label = canvas_label,
6670
+ .size = geometry.SizeF.init(400, 300),
6671
+ .scale_factor = 2,
6672
+ .frame_index = 1,
6673
+ .timestamp_ns = 1_000_000,
6674
+ .nonblank = true,
6675
+ } });
6676
+
6677
+ // The leave payload is a single-item pointer no copy can own: the
6678
+ // PAIR is refused — the enter never dispatches (no enter without a
6679
+ // deliverable leave), the exit owes nothing, and the refusal
6680
+ // settles instead of retrying every event.
6681
+ try hoverMove(harness, app, 50, 20);
6682
+ try std.testing.expectEqual(@as(u32, 0), app_state.model.entered);
6683
+ try hoverMove(harness, app, 50, 220);
6684
+ try std.testing.expectEqual(@as(u32, 0), app_state.model.entered);
6685
+
6686
+ // Re-entry retries the refusal (a rebind could have made the
6687
+ // payload ownable); with the same binding it refuses identically.
6688
+ try hoverMove(harness, app, 50, 20);
6689
+ try std.testing.expectEqual(@as(u32, 0), app_state.model.entered);
4096
6690
  }
4097
6691
 
4098
- test "trackpad pinch reaches the app through on_pinch with product-of-deltas scale" {
6692
+ test "a sentinel-terminated array payload captures with its sentinel stamped" {
4099
6693
  const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
4100
6694
  defer harness.destroy(std.testing.allocator);
4101
6695
  harness.null_platform.gpu_surfaces = true;
4102
6696
 
4103
- const app_state = try std.testing.allocator.create(ZoomApp);
6697
+ const app_state = try std.testing.allocator.create(AlignedApp);
4104
6698
  defer std.testing.allocator.destroy(app_state);
4105
- app_state.* = ZoomApp.init(std.heap.page_allocator, .{}, .{
4106
- .name = "ui-app-zoom",
4107
- .scene = counter_scene,
4108
- .canvas_label = canvas_label,
4109
- .update = zoomUpdate,
4110
- .view = zoomView,
4111
- .on_pinch = zoomPinch,
4112
- });
6699
+ app_state.* = AlignedApp.init(std.heap.page_allocator, .{}, alignedOptions());
4113
6700
  defer app_state.deinit();
4114
6701
  const app = app_state.app();
4115
6702
  try harness.start(app);
@@ -4121,258 +6708,98 @@ test "trackpad pinch reaches the app through on_pinch with product-of-deltas sca
4121
6708
  .timestamp_ns = 1_000_000,
4122
6709
  .nonblank = true,
4123
6710
  } });
4124
- try std.testing.expect(app_state.installed);
4125
6711
 
4126
- // begin -> change -> change -> change -> end, the host's phase
4127
- // stream: the model hears every phase, the pointer anchor rides
4128
- // x/y, and the cumulative scale is the PRODUCT of (1 + delta) —
4129
- // two +25% steps land on 1.5625, never the 1.45 a sum-of-deltas
4130
- // would produce (deltas are raw NSEvent.magnification, the
4131
- // multiplicative per-event delta per the engine convention; see
4132
- // the doctrine note in appkit_host.m).
4133
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4134
- .window_id = 1,
4135
- .label = canvas_label,
4136
- .kind = .pinch_begin,
4137
- .x = 120,
4138
- .y = 80,
4139
- } });
4140
- try std.testing.expectEqual(@as(u32, 1), app_state.model.begins);
4141
- try std.testing.expectEqual(@as(f32, 120), app_state.model.anchor_x);
4142
- try std.testing.expectEqual(@as(f32, 80), app_state.model.anchor_y);
4143
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4144
- .window_id = 1,
4145
- .label = canvas_label,
4146
- .kind = .pinch_change,
4147
- .x = 120,
4148
- .y = 80,
4149
- .scale = 0.25,
4150
- } });
4151
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4152
- .window_id = 1,
4153
- .label = canvas_label,
4154
- .kind = .pinch_change,
4155
- .x = 120,
4156
- .y = 80,
4157
- .scale = 0.25,
4158
- } });
4159
- try std.testing.expectEqual(@as(f32, 1.5625), app_state.model.scale);
4160
- try std.testing.expectEqual(@as(u32, 0), app_state.model.ends);
4161
- // A terminal-delta Ended: AppKit's Ended/Cancelled event still
4162
- // carries the magnification since the previous event, so the host
4163
- // forwards it as one last change BEFORE the end marker — the
4164
- // product folds it in (1.5625 * 1.25 = 1.953125, binary-exact).
4165
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4166
- .window_id = 1,
4167
- .label = canvas_label,
4168
- .kind = .pinch_change,
4169
- .x = 120,
4170
- .y = 80,
4171
- .scale = 0.25,
4172
- } });
4173
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4174
- .window_id = 1,
4175
- .label = canvas_label,
4176
- .kind = .pinch_end,
4177
- .x = 120,
4178
- .y = 80,
4179
- } });
4180
- try std.testing.expectEqual(@as(u32, 1), app_state.model.ends);
4181
- try std.testing.expectEqual(@as(f32, 1.953125), app_state.model.scale);
4182
- // The dispatched Msgs rebuilt the view from the model.
4183
- try std.testing.expect(try retainedTextExists(&harness.runtime, "Zoom 1.95"));
6712
+ // Hover the sentinel row, unmount, and read the delivered array's
6713
+ // FULL storage: the declared elements and the zero sentinel one
6714
+ // past them — never undefined bytes.
6715
+ try hoverMove(harness, app, 50, 60);
6716
+ try app_state.dispatch(&harness.runtime, 1, .hide);
6717
+ try std.testing.expectEqualSlices(u8, &.{ 'x', 'y', 'z', 0 }, &app_state.model.z_payload);
6718
+ }
4184
6719
 
4185
- // A zero-delta Ended is just the end marker: begin then end moves
4186
- // no scale.
4187
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4188
- .window_id = 1,
4189
- .label = canvas_label,
4190
- .kind = .pinch_begin,
4191
- .x = 120,
4192
- .y = 80,
4193
- } });
4194
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4195
- .window_id = 1,
4196
- .label = canvas_label,
4197
- .kind = .pinch_end,
4198
- .x = 120,
4199
- .y = 80,
4200
- } });
4201
- try std.testing.expectEqual(@as(u32, 2), app_state.model.begins);
4202
- try std.testing.expectEqual(@as(u32, 2), app_state.model.ends);
4203
- try std.testing.expectEqual(@as(f32, 1.953125), app_state.model.scale);
6720
+ test "an unownable leave rebind on a standing element degrades loudly, never traps" {
6721
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
6722
+ defer harness.destroy(std.testing.allocator);
6723
+ harness.null_platform.gpu_surfaces = true;
4204
6724
 
4205
- // Non-pinch raw input never leaks into the channel: a scroll leaves
4206
- // the model untouched.
4207
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4208
- .window_id = 1,
6725
+ const app_state = try std.testing.allocator.create(PtrApp);
6726
+ defer std.testing.allocator.destroy(app_state);
6727
+ app_state.* = PtrApp.init(std.heap.page_allocator, .{}, ptrOptions());
6728
+ defer app_state.deinit();
6729
+ app_state.model.bind_ptr = false;
6730
+ const app = app_state.app();
6731
+ try harness.start(app);
6732
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
4209
6733
  .label = canvas_label,
4210
- .kind = .scroll,
4211
- .delta_y = 24,
6734
+ .size = geometry.SizeF.init(400, 300),
6735
+ .scale_factor = 2,
6736
+ .frame_index = 1,
6737
+ .timestamp_ns = 1_000_000,
6738
+ .nonblank = true,
4212
6739
  } });
4213
- try std.testing.expectEqual(@as(f32, 1.953125), app_state.model.scale);
4214
- try std.testing.expectEqual(@as(u32, 2), app_state.model.begins);
4215
-
4216
- // The automation verb drives the same real events: one full gesture
4217
- // whose change carries scale - 1, anchor defaulting to the view
4218
- // center, so tests and users can pinch without a trackpad.
4219
- app_state.model = .{};
4220
- var command_buffer: [96]u8 = undefined;
4221
- const pinch_default = try std.fmt.bufPrint(&command_buffer, "widget-pinch {s} 1.5", .{canvas_label});
4222
- try harness.runtime.dispatchAutomationCommand(app, pinch_default);
4223
- try std.testing.expectEqual(@as(u32, 1), app_state.model.begins);
4224
- try std.testing.expectEqual(@as(u32, 1), app_state.model.ends);
4225
- try std.testing.expectEqual(@as(f32, 1.5), app_state.model.scale);
4226
- try std.testing.expectEqual(@as(f32, 200), app_state.model.anchor_x);
4227
- try std.testing.expectEqual(@as(f32, 150), app_state.model.anchor_y);
4228
-
4229
- // An explicit anchor point rides through; the cumulative scale keeps
4230
- // compounding across gestures exactly as deltas compound within one.
4231
- const pinch_at = try std.fmt.bufPrint(&command_buffer, "widget-pinch {s} 0.5 40 60", .{canvas_label});
4232
- try harness.runtime.dispatchAutomationCommand(app, pinch_at);
4233
- try std.testing.expectEqual(@as(f32, 0.75), app_state.model.scale);
4234
- try std.testing.expectEqual(@as(f32, 40), app_state.model.anchor_x);
4235
- try std.testing.expectEqual(@as(f32, 60), app_state.model.anchor_y);
4236
6740
 
4237
- // A malformed scale is loud driver misuse, not a dispatched gesture
4238
- // (the product of 1 + delta can never reach a non-positive scale).
4239
- const pinch_bad = try std.fmt.bufPrint(&command_buffer, "widget-pinch {s} 0", .{canvas_label});
4240
- try std.testing.expectError(error.InvalidCommand, harness.runtime.dispatchAutomationCommand(app, pinch_bad));
4241
- try std.testing.expectEqual(@as(u32, 2), app_state.model.begins);
6741
+ // Enter with NO leave bound: the enter dispatches (an ownable-free
6742
+ // pair is fine).
6743
+ try hoverMove(harness, app, 50, 20);
6744
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.entered);
4242
6745
 
4243
- // The f32 wire can betray the parser's `scale > 0` guard: a tiny
4244
- // positive scale rounds `scale - 1` to exactly -1 — factor 0 on the
4245
- // wire — so the dispatch refuses it with the wire-minimum teaching
4246
- // instead of emitting a zoom through zero scale. Nothing dispatches:
4247
- // no begin, no journaled partial gesture.
4248
- const pinch_tiny = try std.fmt.bufPrint(&command_buffer, "widget-pinch {s} 1e-20", .{canvas_label});
4249
- try std.testing.expectError(error.PinchScaleBelowWireMinimum, harness.runtime.dispatchAutomationCommand(app, pinch_tiny));
4250
- try std.testing.expectEqual(@as(u32, 2), app_state.model.begins);
4251
- try std.testing.expectEqual(@as(f32, 0.75), app_state.model.scale);
6746
+ // A rebuild BINDS an unownable leave while the element stands: the
6747
+ // refresh degrades with a warning (once) and the mirror settles —
6748
+ // subsequent rebuilds and moves neither trap nor re-enter.
6749
+ try app_state.dispatch(&harness.runtime, 1, .bind_ptr_now);
6750
+ try hoverMove(harness, app, 55, 20);
6751
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.entered);
4252
6752
 
4253
- // The smallest accepted scale — the first f32 above 2^-25 — round-
4254
- // trips with a positive factor: its delta rounds to -1 + 2^-24, so
4255
- // the factor is exactly 2^-24 and the model's product stays > 0
4256
- // (0.75 * 2^-24 = 0x1.8p-25, binary-exact).
4257
- const pinch_min = try std.fmt.bufPrint(&command_buffer, "widget-pinch {s} 2.9802326e-8", .{canvas_label});
4258
- try harness.runtime.dispatchAutomationCommand(app, pinch_min);
4259
- try std.testing.expectEqual(@as(u32, 3), app_state.model.begins);
4260
- try std.testing.expectEqual(@as(u32, 3), app_state.model.ends);
4261
- try std.testing.expect(app_state.model.scale > 0);
4262
- try std.testing.expectEqual(@as(f32, 0x1.8p-25), app_state.model.scale);
6753
+ // The exit resolves the leave from the live tree (the element still
6754
+ // stands), so the standing pair still closes — through live
6755
+ // resolution rather than a capture.
6756
+ try hoverMove(harness, app, 50, 220);
6757
+ try std.testing.expectEqual(@as(u32, 1), app_state.model.entered);
4263
6758
  }
4264
6759
 
4265
- const zoom_pair_main_views = [_]app_manifest.ShellView{
4266
- .{ .label = "zoom-main-canvas", .kind = .gpu_surface, .fill = true, .gpu_backend = .metal },
4267
- };
4268
- const zoom_pair_inspector_views = [_]app_manifest.ShellView{
4269
- .{ .label = "zoom-inspector-canvas", .kind = .gpu_surface, .fill = true, .gpu_backend = .metal },
4270
- };
4271
- const zoom_pair_windows = [_]app_manifest.ShellWindow{ .{
4272
- .label = "main",
4273
- .title = "Zoom",
4274
- .width = 400,
4275
- .height = 300,
4276
- .views = &zoom_pair_main_views,
4277
- }, .{
4278
- .label = "inspector",
4279
- .title = "Zoom Inspector",
4280
- .width = 300,
4281
- .height = 300,
4282
- .views = &zoom_pair_inspector_views,
4283
- } };
4284
- const zoom_pair_scene: app_manifest.ShellConfig = .{ .windows = &zoom_pair_windows };
4285
-
4286
- test "pinch identity distinguishes windows and views in the Msg" {
4287
- // Two windows, two gpu-surface views: the pinch channel forwards
4288
- // the source identity (window_id + view label) the journaled
4289
- // platform event already carries, so a multi-window app hears WHICH
4290
- // view a gesture zoomed — x/y are view-local, and a coordinate
4291
- // without its view is not a position.
6760
+ test "a primary drag released outside the view retires containment and its proof" {
4292
6761
  const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
4293
6762
  defer harness.destroy(std.testing.allocator);
4294
6763
  harness.null_platform.gpu_surfaces = true;
4295
6764
 
4296
- const app_state = try std.testing.allocator.create(ZoomApp);
6765
+ const app_state = try std.testing.allocator.create(HoverApp);
4297
6766
  defer std.testing.allocator.destroy(app_state);
4298
- app_state.* = ZoomApp.init(std.heap.page_allocator, .{}, .{
4299
- .name = "ui-app-zoom-pair",
4300
- .scene = zoom_pair_scene,
4301
- .canvas_label = "zoom-main-canvas",
4302
- .update = zoomUpdate,
4303
- .view = zoomView,
4304
- .on_pinch = zoomPinch,
4305
- });
6767
+ app_state.* = HoverApp.init(std.heap.page_allocator, .{}, hoverOptions());
4306
6768
  defer app_state.deinit();
4307
6769
  const app = app_state.app();
4308
6770
  try harness.start(app);
4309
6771
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
4310
- .label = "zoom-main-canvas",
6772
+ .label = canvas_label,
4311
6773
  .size = geometry.SizeF.init(400, 300),
4312
6774
  .scale_factor = 2,
4313
6775
  .frame_index = 1,
4314
6776
  .timestamp_ns = 1_000_000,
4315
6777
  .nonblank = true,
4316
6778
  } });
4317
- try std.testing.expect(app_state.installed);
4318
-
4319
- // A gesture on the main window's canvas names its source on every
4320
- // phase.
4321
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4322
- .window_id = 1,
4323
- .label = "zoom-main-canvas",
4324
- .kind = .pinch_begin,
4325
- .x = 100,
4326
- .y = 50,
4327
- } });
4328
- try std.testing.expectEqual(@as(u64, 1), app_state.model.window_id);
4329
- try std.testing.expectEqualStrings("zoom-main-canvas", app_state.model.label);
4330
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4331
- .window_id = 1,
4332
- .label = "zoom-main-canvas",
4333
- .kind = .pinch_change,
4334
- .x = 100,
4335
- .y = 50,
4336
- .scale = 0.25,
4337
- } });
4338
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4339
- .window_id = 1,
4340
- .label = "zoom-main-canvas",
4341
- .kind = .pinch_end,
4342
- .x = 100,
4343
- .y = 50,
4344
- } });
4345
- try std.testing.expectEqual(@as(f32, 1.25), app_state.model.scale);
4346
6779
 
4347
- // A gesture on the second window's view is distinguishable: same
4348
- // channel, different identity in the Msg.
4349
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4350
- .window_id = 2,
4351
- .label = "zoom-inspector-canvas",
4352
- .kind = .pinch_begin,
4353
- .x = 10,
4354
- .y = 20,
4355
- } });
4356
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4357
- .window_id = 2,
4358
- .label = "zoom-inspector-canvas",
4359
- .kind = .pinch_change,
4360
- .x = 10,
4361
- .y = 20,
4362
- .scale = -0.5,
4363
- } });
4364
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4365
- .window_id = 2,
4366
- .label = "zoom-inspector-canvas",
4367
- .kind = .pinch_end,
4368
- .x = 10,
4369
- .y = 20,
4370
- } });
4371
- try std.testing.expectEqual(@as(u64, 2), app_state.model.window_id);
4372
- try std.testing.expectEqualStrings("zoom-inspector-canvas", app_state.model.label);
4373
- // Both gestures compounded the one model's zoom (1.25 * 0.5,
4374
- // binary-exact) and every phase was heard.
4375
- try std.testing.expectEqual(@as(f32, 0.625), app_state.model.scale);
4376
- try std.testing.expectEqual(@as(u32, 2), app_state.model.begins);
4377
- try std.testing.expectEqual(@as(u32, 2), app_state.model.ends);
6780
+ try hoverMove(harness, app, 50, 20);
6781
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 } });
6782
+
6783
+ // A primary drag released beyond the surface: hosts held a grab
6784
+ // through it (no motion-leave fired) and send no later cancel, so
6785
+ // the release itself retires containment AND the hover proof — an
6786
+ // overflowing listener can never stay entered off-view, and no
6787
+ // rebuild can re-enter one at a parked off-view anchor.
6788
+ for ([_]struct { kind: zero_platform.GpuSurfaceInputKind, x: f32, y: f32 }{
6789
+ .{ .kind = .pointer_down, .x = 50, .y = 20 },
6790
+ .{ .kind = .pointer_drag, .x = 450, .y = 320 },
6791
+ .{ .kind = .pointer_up, .x = 450, .y = 320 },
6792
+ }) |step| {
6793
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
6794
+ .window_id = 1,
6795
+ .label = canvas_label,
6796
+ .kind = step.kind,
6797
+ .button = 0,
6798
+ .x = step.x,
6799
+ .y = step.y,
6800
+ } });
6801
+ }
6802
+ try expectHoverLog(&app_state.model, &.{ .outer_enter, .{ .row_enter = 1 }, .{ .row_leave = 1 }, .outer_leave });
6803
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.views[0].canvas_widget_hover_msg_chain_len);
6804
+ try std.testing.expect(!harness.runtime.views[0].canvas_widget_hover_pointer_live);
4378
6805
  }