@native-sdk/cli 0.8.0 → 0.8.2

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 (75) hide show
  1. package/README.md +4 -4
  2. package/build.zig +49 -23
  3. package/package.json +10 -10
  4. package/packages/core/compile-surface/core.ts +16 -0
  5. package/packages/core/package-lock.json +2 -2
  6. package/packages/core/package.json +1 -1
  7. package/packages/core/sdk/core.d.ts +11 -0
  8. package/packages/core/sdk/core.ts +29 -2
  9. package/packages/core/sdk/events.d.ts +10 -0
  10. package/packages/core/sdk/events.ts +24 -4
  11. package/packages/core/src/checker.ts +1 -1
  12. package/packages/core/src/contract.ts +4 -1
  13. package/packages/core/src/devhost.ts +10 -1
  14. package/packages/core/src/diagnostics.ts +1 -1
  15. package/skill-data/core/SKILL.md +48 -20
  16. package/skill-data/core/references/app-model-runtime.md +2 -2
  17. package/skill-data/core/references/project-anatomy.md +22 -6
  18. package/skill-data/native-ui/SKILL.md +71 -29
  19. package/skill-data/ts-core/SKILL.md +13 -8
  20. package/skill-data/zig/SKILL.md +1 -1
  21. package/skills/native-sdk/SKILL.md +6 -5
  22. package/src/app_runner/ts_core_main.zig +2 -1
  23. package/src/embed/tests.zig +3 -1
  24. package/src/platform/macos/appkit_host.h +4 -4
  25. package/src/platform/macos/appkit_host.m +24 -4
  26. package/src/platform/macos/cef_host.mm +22 -6
  27. package/src/platform/macos/root.zig +2 -2
  28. package/src/primitives/canvas/events.zig +8 -0
  29. package/src/primitives/canvas/markdown.zig +2010 -57
  30. package/src/primitives/canvas/markdown_hostile_tests.zig +40 -0
  31. package/src/primitives/canvas/markdown_tests.zig +778 -3
  32. package/src/primitives/canvas/root.zig +2 -0
  33. package/src/primitives/canvas/test_support.zig +1 -0
  34. package/src/primitives/canvas/ui.zig +53 -8
  35. package/src/primitives/canvas/ui_markup.zig +10 -1
  36. package/src/primitives/canvas/ui_markup_compiled.zig +61 -3
  37. package/src/primitives/canvas/ui_markup_compiled_tests.zig +27 -0
  38. package/src/primitives/canvas/ui_markup_contract.zig +31 -3
  39. package/src/primitives/canvas/ui_markup_reflect.zig +41 -0
  40. package/src/primitives/canvas/ui_markup_view.zig +49 -3
  41. package/src/primitives/canvas/ui_markup_view_tests.zig +61 -2
  42. package/src/primitives/canvas/ui_schema.zig +8 -1
  43. package/src/primitives/canvas/ui_schema_tests.zig +7 -6
  44. package/src/primitives/canvas/ui_tests.zig +14 -0
  45. package/src/primitives/canvas/widget_access.zig +4 -3
  46. package/src/primitives/canvas/widget_invalidation.zig +11 -0
  47. package/src/primitives/canvas/widget_layout.zig +26 -3
  48. package/src/primitives/canvas/widget_metrics.zig +21 -3
  49. package/src/primitives/canvas/widget_render.zig +87 -1
  50. package/src/primitives/canvas/widget_routing.zig +12 -7
  51. package/src/primitives/canvas/widget_runtime_tests.zig +132 -0
  52. package/src/primitives/canvas/widgets.zig +40 -0
  53. package/src/root.zig +1 -0
  54. package/src/runtime/automation_widget_dispatch.zig +19 -0
  55. package/src/runtime/canvas_limits.zig +5 -0
  56. package/src/runtime/canvas_widget_event_tests.zig +424 -8
  57. package/src/runtime/canvas_widget_events.zig +254 -7
  58. package/src/runtime/canvas_widget_state.zig +123 -1
  59. package/src/runtime/core.zig +6 -0
  60. package/src/runtime/effects.zig +36 -1
  61. package/src/runtime/effects_fetch_tests.zig +21 -0
  62. package/src/runtime/effects_notification_tests.zig +65 -0
  63. package/src/runtime/gpu_surface_events.zig +60 -14
  64. package/src/runtime/tests.zig +1 -0
  65. package/src/runtime/ts_core_host.zig +16 -0
  66. package/src/runtime/ts_ui_app.zig +55 -2
  67. package/src/runtime/ui_app.zig +112 -0
  68. package/src/runtime/ui_app_tests.zig +356 -0
  69. package/src/runtime/view.zig +35 -0
  70. package/src/runtime/view_widget_tree.zig +12 -0
  71. package/tools/corewire/core_abi.zig +1 -0
  72. package/tools/corewire/emit.zig +55 -8
  73. package/tools/corewire/emit_facade.zig +63 -2
  74. package/tools/corewire/emit_profile.zig +4 -1
  75. package/tools/corewire/sidecar.zig +36 -6
@@ -271,6 +271,20 @@ pub const CodeDiffLines = struct {
271
271
  removed: u128,
272
272
  };
273
273
 
274
+ /// Presentation-only FLIP motion for one draggable keyed widget. Layout and
275
+ /// semantics already describe the final pose; render temporarily translates
276
+ /// the widget from the pose the user was looking at and eases that offset to
277
+ /// zero on the recorded frame clock.
278
+ pub const WidgetLayoutMotion = struct {
279
+ id: ObjectId,
280
+ from_offset: geometry.OffsetF = .{},
281
+ offset: geometry.OffsetF = .{},
282
+ start_ns: u64 = 0,
283
+ duration_ms: u32 = 180,
284
+ easing: Easing = .emphasized,
285
+ spring: canvas.SpringToken = .{},
286
+ };
287
+
274
288
  pub const WidgetRenderState = struct {
275
289
  /// Whether the app is active and this widget tree's window is key.
276
290
  /// Runtime focus ids stay retained while false so focus-visible
@@ -282,6 +296,24 @@ pub const WidgetRenderState = struct {
282
296
  focus_visible_id: ?ObjectId = null,
283
297
  hovered_id: ?ObjectId = null,
284
298
  pressed_id: ?ObjectId = null,
299
+ /// The live widget drag. Normal emission reserves but does not paint the
300
+ /// source subtree's current layout slot; a final overlay pass paints its
301
+ /// fully opaque appearance under the pointer. `drag_preview_offset` is
302
+ /// the total pointer displacement from the gesture's down point.
303
+ drag_preview_id: ?ObjectId = null,
304
+ /// The source frame's origin when the gesture first resolved. Keeping
305
+ /// this stable lets an app move the standing source to a candidate
306
+ /// insertion slot while the floating preview stays under the pointer.
307
+ drag_preview_origin: ?geometry.PointF = null,
308
+ drag_preview_offset: geometry.OffsetF = .{},
309
+ /// Renderer-private namespace switch used only while emitting the
310
+ /// floating appearance, so its display-list ids cannot duplicate the source
311
+ /// subtree's ids.
312
+ rendering_drag_preview: bool = false,
313
+ /// Presentation offsets for draggable keyed widgets displaced by a live
314
+ /// insertion preview (and for the source settling after pointer-up).
315
+ /// These never alter hit testing, semantics, or final layout geometry.
316
+ layout_motions: []const WidgetLayoutMotion = &.{},
285
317
  /// The pointer position while `hovered_id` is a widget that draws
286
318
  /// hover-detail chrome (today: `.chart` with `hover_details`), in
287
319
  /// the same coordinate space as widget frames. Null everywhere
@@ -304,6 +336,14 @@ pub const WidgetRenderState = struct {
304
336
  }
305
337
  return false;
306
338
  }
339
+
340
+ pub fn layoutMotionOffset(self: WidgetRenderState, id: ObjectId) geometry.OffsetF {
341
+ if (id == 0 or self.rendering_drag_preview) return .{};
342
+ for (self.layout_motions) |motion| {
343
+ if (motion.id == id) return motion.offset;
344
+ }
345
+ return .{};
346
+ }
307
347
  };
308
348
 
309
349
  pub const WidgetMainAlignment = enum {
package/src/root.zig CHANGED
@@ -183,6 +183,7 @@ pub const SaveDialogOptions = platform.SaveDialogOptions;
183
183
  pub const MessageDialogStyle = platform.MessageDialogStyle;
184
184
  pub const MessageDialogResult = platform.MessageDialogResult;
185
185
  pub const MessageDialogOptions = platform.MessageDialogOptions;
186
+ pub const NotificationOptions = platform.NotificationOptions;
186
187
  pub const TrayItemId = platform.TrayItemId;
187
188
  pub const TrayOptions = platform.TrayOptions;
188
189
  pub const TrayMenuItem = platform.TrayMenuItem;
@@ -679,6 +679,14 @@ pub fn RuntimeAutomationWidgetDispatch(comptime Runtime: type) type {
679
679
  const previous_pressed_id = self.views[view_index].canvas_widget_pressed_id;
680
680
  const previous_state = self.views[view_index].canvasWidgetRenderState();
681
681
  self.views[view_index].canvas_widget_pressed_id = id;
682
+ // Accessibility owns the outer action record and therefore seeds
683
+ // the press directly instead of journaling a synthetic down. Give
684
+ // the shared physical drag path the same origin a down would have
685
+ // recorded; platform pointer-drag deltas are not cumulative (and
686
+ // are zero on several hosts), so reconstruction from the event's
687
+ // delta cannot recover this point.
688
+ self.views[view_index].canvas_widget_drag_start_point = origin;
689
+ self.views[view_index].canvas_widget_drag_pointer_id = 0;
682
690
  if (previous_pressed_id != id) try CanvasWidgetEventMethods().invalidateForCanvasWidgetRenderStateChange(self, view_index, previous_state, self.views[view_index].canvasWidgetRenderState());
683
691
  errdefer {
684
692
  if (view_index < self.view_count and self.views[view_index].canvas_widget_pressed_id == id) {
@@ -696,6 +704,17 @@ pub fn RuntimeAutomationWidgetDispatch(comptime Runtime: type) type {
696
704
  .delta_y = delta.dy,
697
705
  } });
698
706
 
707
+ // Complete the gesture through the same release path as a
708
+ // physical pointer so markup `on-drag` handlers receive their
709
+ // release position instead of automation stopping mid-drag.
710
+ try self.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
711
+ .window_id = window_id,
712
+ .label = label,
713
+ .kind = .pointer_up,
714
+ .x = origin.x + delta.dx,
715
+ .y = origin.y + delta.dy,
716
+ } });
717
+
699
718
  if (runtimeFindViewIndex(self, window_id, label)) |current_index| {
700
719
  if (self.views[current_index].canvas_widget_pressed_id == id) {
701
720
  const release_previous_state = self.views[current_index].canvasWidgetRenderState();
@@ -315,6 +315,11 @@ pub const max_canvas_widget_loop_animations_per_view: usize = 64;
315
315
  /// dropping the state change (motion degrades, truth never does).
316
316
  pub const max_canvas_widget_layout_tweens_per_view: usize = 8;
317
317
 
318
+ /// Draggable keyed widgets that may be displaced by one live insertion
319
+ /// preview. This matches the practical card/list scale and, on overflow,
320
+ /// extra items simply snap while the layout result still lands exactly.
321
+ pub const max_canvas_widget_drag_layout_motions_per_view: usize = 64;
322
+
318
323
  /// Nodes one disclosure tween may move per view: a disclosure flip
319
324
  /// reflows the flipped item plus everything stacked below it (siblings,
320
325
  /// content-hugging ancestors, and THEIR following siblings), and the
@@ -2698,6 +2698,7 @@ test "runtime dispatches opted-in canvas widget drag events" {
2698
2698
  raw_input_count: u32 = 0,
2699
2699
  widget_pointer_count: u32 = 0,
2700
2700
  widget_drag_count: u32 = 0,
2701
+ command_count: u32 = 0,
2701
2702
  last_drag_source_id: canvas.ObjectId = 0,
2702
2703
  last_drag_route_len: usize = 0,
2703
2704
  last_drag_x: f32 = 0,
@@ -2713,6 +2714,7 @@ test "runtime dispatches opted-in canvas widget drag events" {
2713
2714
  switch (event_value) {
2714
2715
  .gpu_surface_input => self.raw_input_count += 1,
2715
2716
  .canvas_widget_pointer => self.widget_pointer_count += 1,
2717
+ .command => self.command_count += 1,
2716
2718
  .canvas_widget_drag => |drag_event| {
2717
2719
  self.widget_drag_count += 1;
2718
2720
  self.last_drag_source_id = if (drag_event.source) |source| source.id else 0;
@@ -2745,6 +2747,7 @@ test "runtime dispatches opted-in canvas widget drag events" {
2745
2747
  .kind = .button,
2746
2748
  .frame = geometry.RectF.init(12, 16, 96, 32),
2747
2749
  .text = "Drag",
2750
+ .command = "drag.press",
2748
2751
  .semantics = .{ .actions = .{ .drag = true } },
2749
2752
  },
2750
2753
  .{
@@ -2770,6 +2773,8 @@ test "runtime dispatches opted-in canvas widget drag events" {
2770
2773
  try std.testing.expectEqual(@as(u32, 1), app_state.widget_pointer_count);
2771
2774
  try std.testing.expectEqual(@as(u32, 1), app_state.raw_input_count);
2772
2775
 
2776
+ // A plain click on a draggable remains a press. It produces no terminal
2777
+ // drag and cannot leave a zero-origin landing armed for a later rebuild.
2773
2778
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2774
2779
  .window_id = 1,
2775
2780
  .label = "canvas",
@@ -2777,27 +2782,433 @@ test "runtime dispatches opted-in canvas widget drag events" {
2777
2782
  .x = 20,
2778
2783
  .y = 28,
2779
2784
  } });
2785
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2786
+ .window_id = 1,
2787
+ .label = "canvas",
2788
+ .kind = .pointer_up,
2789
+ .x = 20,
2790
+ .y = 28,
2791
+ } });
2792
+ try std.testing.expectEqual(@as(u32, 1), app_state.command_count);
2793
+ try std.testing.expectEqual(@as(u32, 0), app_state.widget_drag_count);
2794
+ try std.testing.expect(!harness.runtime.views[0].canvas_widget_drag_layout_motion_armed);
2795
+ try std.testing.expectEqual(@as(canvas.ObjectId, 0), harness.runtime.views[0].canvas_widget_drag_landing_source_id);
2796
+
2797
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2798
+ .window_id = 1,
2799
+ .label = "canvas",
2800
+ .kind = .pointer_down,
2801
+ .x = 20,
2802
+ .y = 28,
2803
+ } });
2804
+ // Motion inside the slop stays observable to the low-level channel so it
2805
+ // owns text selection, but it activates no preview or landing state.
2780
2806
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2781
2807
  .window_id = 1,
2782
2808
  .label = "canvas",
2783
2809
  .kind = .pointer_drag,
2784
- .x = 64,
2810
+ .x = 24,
2785
2811
  .y = 30,
2786
- .delta_x = 44,
2787
2812
  } });
2788
2813
  try std.testing.expectEqual(@as(u32, 1), app_state.widget_drag_count);
2789
- try std.testing.expectEqual(@as(u32, 3), app_state.widget_pointer_count);
2790
- try std.testing.expectEqual(@as(u32, 3), app_state.raw_input_count);
2814
+ try std.testing.expect(harness.runtime.views[0].canvasWidgetRenderState().drag_preview_id == null);
2815
+ try std.testing.expect(!harness.runtime.views[0].canvas_widget_drag_layout_motion_armed);
2816
+
2817
+ try harness.runtime.dispatchPlatformEvent(app, .{
2818
+ .gpu_surface_input = .{
2819
+ .window_id = 1,
2820
+ .label = "canvas",
2821
+ .kind = .pointer_drag,
2822
+ .x = 64,
2823
+ .y = 30,
2824
+ // Physical hosts report per-event deltas (often zero); widget drag
2825
+ // displacement must come from the pointer-down origin instead.
2826
+ .delta_x = 0,
2827
+ .delta_y = 0,
2828
+ },
2829
+ });
2830
+ try std.testing.expectEqual(@as(u32, 2), app_state.widget_drag_count);
2831
+ try std.testing.expectEqual(@as(u32, 6), app_state.widget_pointer_count);
2832
+ try std.testing.expectEqual(@as(u32, 6), app_state.raw_input_count);
2791
2833
  try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_drag_source_id);
2792
2834
  try std.testing.expectEqual(@as(usize, 3), app_state.last_drag_route_len);
2793
2835
  try std.testing.expectEqual(@as(f32, 64), app_state.last_drag_x);
2794
2836
  try std.testing.expectEqual(@as(f32, 44), app_state.last_drag_dx);
2837
+ const dragging_state = harness.runtime.views[0].canvasWidgetRenderState();
2838
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), dragging_state.drag_preview_id.?);
2839
+ try std.testing.expectEqual(@as(f32, 12), dragging_state.drag_preview_origin.?.x);
2840
+ try std.testing.expectEqual(@as(f32, 16), dragging_state.drag_preview_origin.?.y);
2841
+ try std.testing.expectEqual(@as(f32, 44), dragging_state.drag_preview_offset.dx);
2842
+ try std.testing.expectEqual(@as(f32, 2), dragging_state.drag_preview_offset.dy);
2843
+
2844
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2845
+ .window_id = 1,
2846
+ .label = "canvas",
2847
+ .kind = .pointer_up,
2848
+ .x = 64,
2849
+ .y = 30,
2850
+ } });
2851
+ try std.testing.expectEqual(@as(u32, 3), app_state.widget_drag_count);
2852
+ // The captured release ended a real drag, so it cannot also activate the
2853
+ // source button's command/on_press path.
2854
+ try std.testing.expectEqual(@as(u32, 1), app_state.command_count);
2855
+ try std.testing.expect(harness.runtime.views[0].canvasWidgetRenderState().drag_preview_id == null);
2856
+
2857
+ // A drag-driven rebuild keeps truthful final layout geometry while the
2858
+ // moved keyed widget begins at its previously presented position and
2859
+ // springs to zero offset on recorded frame timestamps.
2860
+ const moved_children = [_]canvas.Widget{
2861
+ .{
2862
+ .id = 2,
2863
+ .kind = .button,
2864
+ .frame = geometry.RectF.init(12, 58, 96, 32),
2865
+ .text = "Drag",
2866
+ .semantics = .{ .actions = .{ .drag = true } },
2867
+ },
2868
+ children[1],
2869
+ };
2870
+ var moved_nodes: [4]canvas.WidgetLayoutNode = undefined;
2871
+ const moved_layout = try canvas.layoutWidgetTree(.{ .id = 1, .kind = .panel, .children = &moved_children }, geometry.RectF.init(0, 0, 240, 120), &moved_nodes);
2872
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", moved_layout);
2873
+ var motion_state = harness.runtime.views[0].canvasWidgetRenderState();
2874
+ try std.testing.expectEqual(@as(usize, 1), motion_state.layout_motions.len);
2875
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), motion_state.layout_motions[0].id);
2876
+ try std.testing.expectEqual(@as(f32, 44), motion_state.layout_motions[0].offset.dx);
2877
+ try std.testing.expectEqual(@as(f32, -40), motion_state.layout_motions[0].offset.dy);
2878
+ try harness.runtime.advanceCanvasWidgetDragLayoutMotionForFrame(0, 1_000_000_000);
2879
+ motion_state = harness.runtime.views[0].canvasWidgetRenderState();
2880
+ try std.testing.expectEqual(@as(f32, 44), motion_state.layout_motions[0].offset.dx);
2881
+ try std.testing.expectEqual(@as(f32, -40), motion_state.layout_motions[0].offset.dy);
2882
+ try harness.runtime.advanceCanvasWidgetDragLayoutMotionForFrame(0, 1_100_000_000);
2883
+ motion_state = harness.runtime.views[0].canvasWidgetRenderState();
2884
+ try std.testing.expect(@abs(motion_state.layout_motions[0].offset.dy) < 40);
2885
+ try harness.runtime.advanceCanvasWidgetDragLayoutMotionForFrame(0, 2_000_000_000);
2886
+ try std.testing.expectEqual(@as(usize, 0), harness.runtime.views[0].canvasWidgetRenderState().layout_motions.len);
2795
2887
 
2796
2888
  const snapshot = harness.runtime.automationSnapshot("Widgets");
2797
2889
  try std.testing.expect(snapshot.widgets[1].actions.drag);
2798
2890
  try std.testing.expect(!snapshot.widgets[2].actions.drag);
2799
2891
  }
2800
2892
 
2893
+ test "drag routing preserves a nested pointer route until pointer dispatch" {
2894
+ const TestApp = struct {
2895
+ pointer_target_id: canvas.ObjectId = 0,
2896
+ drag_source_id: canvas.ObjectId = 0,
2897
+ pointer_route: [5]canvas.WidgetEventRouteEntry = undefined,
2898
+ pointer_route_len: usize = 0,
2899
+
2900
+ fn app(self: *@This()) App {
2901
+ return .{ .context = self, .name = "gpu-widget-drag-routes", .source = platform.WebViewSource.html("<h1>GPU</h1>"), .event_fn = event };
2902
+ }
2903
+
2904
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
2905
+ _ = runtime;
2906
+ const self: *@This() = @ptrCast(@alignCast(context));
2907
+ switch (event_value) {
2908
+ .canvas_widget_pointer => |pointer_event| {
2909
+ if (pointer_event.pointer.phase != .move) return;
2910
+ self.pointer_target_id = if (pointer_event.target) |target| target.id else 0;
2911
+ self.pointer_route_len = @min(pointer_event.route.len, self.pointer_route.len);
2912
+ @memcpy(self.pointer_route[0..self.pointer_route_len], pointer_event.route[0..self.pointer_route_len]);
2913
+ },
2914
+ .canvas_widget_drag => |drag_event| {
2915
+ self.drag_source_id = if (drag_event.source) |source| source.id else 0;
2916
+ },
2917
+ else => {},
2918
+ }
2919
+ }
2920
+ };
2921
+
2922
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2923
+ defer harness.destroy(std.testing.allocator);
2924
+ harness.null_platform.gpu_surfaces = true;
2925
+ var app_state: TestApp = .{};
2926
+ const app = app_state.app();
2927
+ try harness.start(app);
2928
+
2929
+ _ = try harness.runtime.createView(.{
2930
+ .window_id = 1,
2931
+ .label = "canvas",
2932
+ .kind = .gpu_surface,
2933
+ .frame = geometry.RectF.init(0, 0, 240, 120),
2934
+ });
2935
+
2936
+ const label = canvas.Widget{
2937
+ .id = 3,
2938
+ .kind = .text,
2939
+ .frame = geometry.RectF.init(0, 0, 80, 20),
2940
+ .text = "Drag label",
2941
+ };
2942
+ const source = canvas.Widget{
2943
+ .id = 2,
2944
+ .kind = .row,
2945
+ .frame = geometry.RectF.init(0, 0, 0, 40),
2946
+ .layout = .{ .padding = geometry.InsetsF.all(8) },
2947
+ .semantics = .{ .actions = .{ .drag = true } },
2948
+ .children = &.{label},
2949
+ };
2950
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
2951
+ const layout = try canvas.layoutWidgetTree(
2952
+ .{ .id = 1, .kind = .panel, .children = &.{source} },
2953
+ geometry.RectF.init(0, 0, 240, 120),
2954
+ &nodes,
2955
+ );
2956
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
2957
+ const point = (try harness.runtime.canvasWidgetLayout(1, "canvas")).findById(3).?.frame.normalized().center();
2958
+
2959
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2960
+ .window_id = 1,
2961
+ .label = "canvas",
2962
+ .kind = .pointer_down,
2963
+ .x = point.x,
2964
+ .y = point.y,
2965
+ } });
2966
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2967
+ .window_id = 1,
2968
+ .label = "canvas",
2969
+ .kind = .pointer_drag,
2970
+ .x = point.x + 12,
2971
+ .y = point.y,
2972
+ } });
2973
+
2974
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.pointer_target_id);
2975
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.drag_source_id);
2976
+ try std.testing.expectEqual(@as(usize, 5), app_state.pointer_route_len);
2977
+ try std.testing.expectEqual(canvas.WidgetEventPhase.capture, app_state.pointer_route[0].phase);
2978
+ try std.testing.expectEqual(@as(canvas.ObjectId, 1), app_state.pointer_route[0].id);
2979
+ try std.testing.expectEqual(canvas.WidgetEventPhase.capture, app_state.pointer_route[1].phase);
2980
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.pointer_route[1].id);
2981
+ try std.testing.expectEqual(canvas.WidgetEventPhase.target, app_state.pointer_route[2].phase);
2982
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.pointer_route[2].id);
2983
+ try std.testing.expectEqual(canvas.WidgetEventPhase.bubble, app_state.pointer_route[3].phase);
2984
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.pointer_route[3].id);
2985
+ try std.testing.expectEqual(canvas.WidgetEventPhase.bubble, app_state.pointer_route[4].phase);
2986
+ try std.testing.expectEqual(@as(canvas.ObjectId, 1), app_state.pointer_route[4].id);
2987
+ }
2988
+
2989
+ test "unmounted drag source still dispatches one captured cancel" {
2990
+ const TestApp = struct {
2991
+ widget_drag_count: u32 = 0,
2992
+ last_drag_phase: canvas.WidgetDragPhase = .change,
2993
+ last_drag_source_id: canvas.ObjectId = 0,
2994
+ last_drag_route_len: usize = 0,
2995
+ source_unmounted: bool = false,
2996
+
2997
+ fn app(self: *@This()) App {
2998
+ return .{ .context = self, .name = "gpu-widget-drag-unmount", .source = platform.WebViewSource.html("<h1>GPU</h1>"), .event_fn = event };
2999
+ }
3000
+
3001
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
3002
+ const self: *@This() = @ptrCast(@alignCast(context));
3003
+ switch (event_value) {
3004
+ .canvas_widget_drag => |drag_event| {
3005
+ self.widget_drag_count += 1;
3006
+ self.last_drag_phase = drag_event.drag.phase;
3007
+ self.last_drag_source_id = if (drag_event.source) |source| source.id else 0;
3008
+ self.last_drag_route_len = drag_event.route.len;
3009
+ if (drag_event.drag.phase == .change and !self.source_unmounted) {
3010
+ self.source_unmounted = true;
3011
+ var empty_nodes: [1]canvas.WidgetLayoutNode = undefined;
3012
+ const empty_layout = try canvas.layoutWidgetTree(
3013
+ .{ .id = 1, .kind = .panel },
3014
+ geometry.RectF.init(0, 0, 240, 120),
3015
+ &empty_nodes,
3016
+ );
3017
+ _ = try runtime.setCanvasWidgetLayout(1, "canvas", empty_layout);
3018
+ }
3019
+ },
3020
+ else => {},
3021
+ }
3022
+ }
3023
+ };
3024
+
3025
+ const harness = try TestHarness().create(std.testing.allocator, .{});
3026
+ defer harness.destroy(std.testing.allocator);
3027
+ harness.null_platform.gpu_surfaces = true;
3028
+ var app_state: TestApp = .{};
3029
+ const app = app_state.app();
3030
+ try harness.start(app);
3031
+
3032
+ _ = try harness.runtime.createView(.{
3033
+ .window_id = 1,
3034
+ .label = "canvas",
3035
+ .kind = .gpu_surface,
3036
+ .frame = geometry.RectF.init(0, 0, 240, 120),
3037
+ });
3038
+ const children = [_]canvas.Widget{.{
3039
+ .id = 2,
3040
+ .kind = .button,
3041
+ .frame = geometry.RectF.init(12, 16, 96, 32),
3042
+ .text = "Drag",
3043
+ .semantics = .{ .actions = .{ .drag = true } },
3044
+ }};
3045
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
3046
+ const layout = try canvas.layoutWidgetTree(.{ .id = 1, .kind = .panel, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
3047
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
3048
+
3049
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3050
+ .window_id = 1,
3051
+ .label = "canvas",
3052
+ .kind = .pointer_down,
3053
+ .x = 20,
3054
+ .y = 28,
3055
+ } });
3056
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3057
+ .window_id = 1,
3058
+ .label = "canvas",
3059
+ .kind = .pointer_drag,
3060
+ .x = 64,
3061
+ .y = 30,
3062
+ } });
3063
+ try std.testing.expectEqual(@as(u32, 1), app_state.widget_drag_count);
3064
+ try std.testing.expectEqual(canvas.WidgetDragPhase.change, app_state.last_drag_phase);
3065
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_drag_source_id);
3066
+ try std.testing.expect(!harness.runtime.views[0].canvas_widget_drag_source_attached);
3067
+ try std.testing.expect(harness.runtime.views[0].canvasWidgetRenderState().drag_preview_id == null);
3068
+
3069
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3070
+ .window_id = 1,
3071
+ .label = "canvas",
3072
+ .kind = .pointer_up,
3073
+ .x = 70,
3074
+ .y = 32,
3075
+ } });
3076
+ try std.testing.expectEqual(@as(u32, 2), app_state.widget_drag_count);
3077
+ try std.testing.expectEqual(canvas.WidgetDragPhase.cancel, app_state.last_drag_phase);
3078
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_drag_source_id);
3079
+ try std.testing.expectEqual(@as(usize, 3), app_state.last_drag_route_len);
3080
+ try std.testing.expectEqual(@as(canvas.ObjectId, 0), harness.runtime.views[0].canvas_widget_drag_source_id);
3081
+ try std.testing.expectEqual(@as(u64, 0), harness.runtime.views[0].canvas_widget_drag_pointer_id);
3082
+
3083
+ // The physical sequence is retired with the synthesized cancellation;
3084
+ // another terminal edge cannot deliver the captured route twice.
3085
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3086
+ .window_id = 1,
3087
+ .label = "canvas",
3088
+ .kind = .pointer_cancel,
3089
+ .x = 70,
3090
+ .y = 32,
3091
+ } });
3092
+ try std.testing.expectEqual(@as(u32, 2), app_state.widget_drag_count);
3093
+ }
3094
+
3095
+ test "plain escape cancels an active canvas widget drag and release cannot revive it" {
3096
+ const TestApp = struct {
3097
+ widget_drag_count: u32 = 0,
3098
+ widget_keyboard_count: u32 = 0,
3099
+ last_drag_phase: canvas.WidgetDragPhase = .change,
3100
+ last_drag_point: geometry.PointF = .{},
3101
+ last_drag_delta: geometry.OffsetF = .{},
3102
+
3103
+ fn app(self: *@This()) App {
3104
+ return .{ .context = self, .name = "gpu-widget-drag-escape", .source = platform.WebViewSource.html("<h1>GPU</h1>"), .event_fn = event };
3105
+ }
3106
+
3107
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
3108
+ _ = runtime;
3109
+ const self: *@This() = @ptrCast(@alignCast(context));
3110
+ switch (event_value) {
3111
+ .canvas_widget_drag => |drag_event| {
3112
+ self.widget_drag_count += 1;
3113
+ self.last_drag_phase = drag_event.drag.phase;
3114
+ self.last_drag_point = drag_event.drag.point;
3115
+ self.last_drag_delta = drag_event.drag.delta;
3116
+ },
3117
+ .canvas_widget_keyboard => self.widget_keyboard_count += 1,
3118
+ else => {},
3119
+ }
3120
+ }
3121
+ };
3122
+
3123
+ const harness = try TestHarness().create(std.testing.allocator, .{});
3124
+ defer harness.destroy(std.testing.allocator);
3125
+ harness.null_platform.gpu_surfaces = true;
3126
+ var app_state: TestApp = .{};
3127
+ const app = app_state.app();
3128
+ try harness.start(app);
3129
+
3130
+ _ = try harness.runtime.createView(.{
3131
+ .window_id = 1,
3132
+ .label = "canvas",
3133
+ .kind = .gpu_surface,
3134
+ .frame = geometry.RectF.init(0, 0, 240, 120),
3135
+ });
3136
+
3137
+ const children = [_]canvas.Widget{.{
3138
+ .id = 2,
3139
+ .kind = .button,
3140
+ .frame = geometry.RectF.init(12, 16, 96, 32),
3141
+ .text = "Drag",
3142
+ .semantics = .{ .actions = .{ .drag = true } },
3143
+ }};
3144
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
3145
+ const layout = try canvas.layoutWidgetTree(.{ .id = 1, .kind = .panel, .children = &children }, geometry.RectF.init(0, 0, 240, 120), &nodes);
3146
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
3147
+
3148
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3149
+ .window_id = 1,
3150
+ .label = "canvas",
3151
+ .kind = .pointer_down,
3152
+ .x = 20,
3153
+ .y = 28,
3154
+ } });
3155
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3156
+ .window_id = 1,
3157
+ .label = "canvas",
3158
+ .kind = .pointer_drag,
3159
+ .x = 64,
3160
+ .y = 30,
3161
+ .delta_x = 44,
3162
+ .delta_y = 2,
3163
+ } });
3164
+ try std.testing.expectEqual(@as(u32, 1), app_state.widget_drag_count);
3165
+ try std.testing.expectEqual(canvas.WidgetDragPhase.change, app_state.last_drag_phase);
3166
+
3167
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3168
+ .window_id = 1,
3169
+ .label = "canvas",
3170
+ .kind = .key_down,
3171
+ .key = "escape",
3172
+ } });
3173
+ try std.testing.expectEqual(@as(u32, 2), app_state.widget_drag_count);
3174
+ try std.testing.expectEqual(@as(u32, 0), app_state.widget_keyboard_count);
3175
+ try std.testing.expectEqual(canvas.WidgetDragPhase.cancel, app_state.last_drag_phase);
3176
+ try std.testing.expectEqualDeep(geometry.PointF.init(64, 30), app_state.last_drag_point);
3177
+ try std.testing.expectEqualDeep(geometry.OffsetF.init(44, 2), app_state.last_drag_delta);
3178
+ const cancelled_state = harness.runtime.views[0].canvasWidgetRenderState();
3179
+ try std.testing.expect(cancelled_state.drag_preview_id == null);
3180
+ try std.testing.expect(cancelled_state.pressed_id == null);
3181
+
3182
+ // The app's phase-2 rebuild restores the original keyed slot. Its
3183
+ // presentation begins exactly where the opaque floating card was when
3184
+ // Escape landed, then eases back to the source frame.
3185
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
3186
+ const landing_state = harness.runtime.views[0].canvasWidgetRenderState();
3187
+ try std.testing.expectEqual(@as(usize, 1), landing_state.layout_motions.len);
3188
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), landing_state.layout_motions[0].id);
3189
+ try std.testing.expectEqualDeep(geometry.OffsetF.init(44, 2), landing_state.layout_motions[0].offset);
3190
+
3191
+ // The still-held physical pointer no longer owns a press. Its remaining
3192
+ // motion and release cannot restart or commit the cancelled drag.
3193
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3194
+ .window_id = 1,
3195
+ .label = "canvas",
3196
+ .kind = .pointer_drag,
3197
+ .x = 72,
3198
+ .y = 34,
3199
+ .delta_x = 52,
3200
+ .delta_y = 6,
3201
+ } });
3202
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3203
+ .window_id = 1,
3204
+ .label = "canvas",
3205
+ .kind = .pointer_up,
3206
+ .x = 72,
3207
+ .y = 34,
3208
+ } });
3209
+ try std.testing.expectEqual(@as(u32, 2), app_state.widget_drag_count);
3210
+ }
3211
+
2801
3212
  test "runtime resizes retained canvas resizable widgets from pointer drag" {
2802
3213
  const TestApp = struct {
2803
3214
  fn app(self: *@This()) App {
@@ -2830,6 +3241,7 @@ test "runtime resizes retained canvas resizable widgets from pointer drag" {
2830
3241
  const layout = try canvas.layoutWidgetTree(.{ .id = 1, .kind = .stack, .children = &.{resizable} }, geometry.RectF.init(0, 0, 260, 120), &nodes);
2831
3242
  _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
2832
3243
  _ = try harness.runtime.emitCanvasWidgetDisplayList(1, "canvas", .{});
3244
+ const baseline_command_count = (try harness.runtime.canvasDisplayList(1, "canvas")).commandCount();
2833
3245
 
2834
3246
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2835
3247
  .window_id = 1,
@@ -2852,6 +3264,9 @@ test "runtime resizes retained canvas resizable widgets from pointer drag" {
2852
3264
  try std.testing.expectEqualDeep(geometry.RectF.init(10, 16, 150, 44), retained.findById(2).?.widget.frame);
2853
3265
 
2854
3266
  var display_list = try harness.runtime.canvasDisplayList(1, "canvas");
3267
+ // Built-in resize semantics mutate retained geometry in place; they do
3268
+ // not also paint the generic translated drag preview.
3269
+ try std.testing.expectEqual(baseline_command_count, display_list.commandCount());
2855
3270
  switch (display_list.findCommandById(testCanvasWidgetPartId(2, 5)).?.command) {
2856
3271
  .draw_line => |line| try std.testing.expect(line.from.x > 152),
2857
3272
  else => return error.TestUnexpectedResult,
@@ -2991,12 +3406,13 @@ test "runtime dispatches automation canvas widget actions" {
2991
3406
  try std.testing.expectEqualStrings("enter", app_state.last_keyboard_key);
2992
3407
 
2993
3408
  try dispatchAutomationWidgetAction(&harness.runtime, app, .{ .view_label = "canvas", .id = 2, .action = .drag, .value = "18 2" });
2994
- try std.testing.expectEqual(@as(u32, 1), app_state.widget_drag_count);
3409
+ // Automation completes the gesture with the release event too.
3410
+ try std.testing.expectEqual(@as(u32, 2), app_state.widget_drag_count);
2995
3411
  try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_drag_source_id);
2996
3412
  try std.testing.expectEqual(@as(f32, 18), app_state.last_drag_dx);
2997
3413
 
2998
3414
  _ = try harness.runtime.dispatchCanvasWidgetAccessibilityAction(app, 1, "canvas", .{ .id = 2, .action = .drag, .text = "8 1" });
2999
- try std.testing.expectEqual(@as(u32, 2), app_state.widget_drag_count);
3415
+ try std.testing.expectEqual(@as(u32, 4), app_state.widget_drag_count);
3000
3416
  try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_drag_source_id);
3001
3417
  try std.testing.expectEqual(@as(f32, 8), app_state.last_drag_dx);
3002
3418
 
@@ -3006,7 +3422,7 @@ test "runtime dispatches automation canvas widget actions" {
3006
3422
  .id = 2,
3007
3423
  .action = .drag,
3008
3424
  } });
3009
- try std.testing.expectEqual(@as(u32, 3), app_state.widget_drag_count);
3425
+ try std.testing.expectEqual(@as(u32, 6), app_state.widget_drag_count);
3010
3426
  try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_drag_source_id);
3011
3427
  try std.testing.expectEqual(@as(f32, 16), app_state.last_drag_dx);
3012
3428
 
@@ -3299,7 +3715,7 @@ test "runtime rejects automation canvas widget actions for scroll clipped target
3299
3715
  try std.testing.expectEqual(@as(u32, 0), app_state.raw_input_count);
3300
3716
 
3301
3717
  try dispatchAutomationWidgetAction(&harness.runtime, app, .{ .view_label = "canvas", .id = 3, .action = .drag, .value = "8 0" });
3302
- try std.testing.expectEqual(@as(u32, 1), app_state.widget_drag_count);
3718
+ try std.testing.expectEqual(@as(u32, 2), app_state.widget_drag_count);
3303
3719
  try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_drag_source_id);
3304
3720
 
3305
3721
  const drop_children = [_]canvas.Widget{