@native-sdk/cli 0.5.1 → 0.5.3

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 (98) hide show
  1. package/build.zig +41 -0
  2. package/package.json +9 -9
  3. package/packages/core/package-lock.json +2 -2
  4. package/packages/core/package.json +1 -1
  5. package/packages/core/sdk/events.ts +39 -2
  6. package/packages/core/src/checker.ts +1 -1
  7. package/packages/core/src/emitter.ts +39 -3
  8. package/packages/core/src/infer.ts +53 -7
  9. package/skill-data/automation/SKILL.md +7 -4
  10. package/skill-data/core/references/app-model-runtime.md +3 -0
  11. package/skill-data/native-ui/SKILL.md +3 -2
  12. package/src/app_runner/root.zig +25 -0
  13. package/src/automation/protocol.zig +20 -1
  14. package/src/embed/host.zig +27 -0
  15. package/src/embed/tests.zig +69 -0
  16. package/src/embed/ui_host.zig +4 -0
  17. package/src/platform/macos/appkit_host.h +11 -0
  18. package/src/platform/macos/appkit_host.m +130 -0
  19. package/src/platform/macos/root.zig +34 -0
  20. package/src/platform/null_platform_tests.zig +39 -0
  21. package/src/platform/root.zig +3 -0
  22. package/src/platform/types.zig +118 -4
  23. package/src/primitives/canvas/chart.zig +7 -7
  24. package/src/primitives/canvas/chart_tests.zig +66 -0
  25. package/src/primitives/canvas/commands.zig +48 -0
  26. package/src/primitives/canvas/events.zig +161 -0
  27. package/src/primitives/canvas/font_ttf.zig +155 -8
  28. package/src/primitives/canvas/font_ttf_tests.zig +686 -0
  29. package/src/primitives/canvas/reference.zig +67 -8
  30. package/src/primitives/canvas/render_fingerprints.zig +8 -0
  31. package/src/primitives/canvas/root.zig +26 -0
  32. package/src/primitives/canvas/tokens.zig +170 -7
  33. package/src/primitives/canvas/ui.zig +62 -7
  34. package/src/primitives/canvas/ui_markup.zig +51 -4
  35. package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
  36. package/src/primitives/canvas/ui_markup_compiled.zig +49 -4
  37. package/src/primitives/canvas/ui_markup_compiled_tests.zig +160 -0
  38. package/src/primitives/canvas/ui_markup_contract.zig +9 -0
  39. package/src/primitives/canvas/ui_markup_contract_tests.zig +24 -0
  40. package/src/primitives/canvas/ui_markup_tests.zig +73 -0
  41. package/src/primitives/canvas/ui_markup_view.zig +63 -5
  42. package/src/primitives/canvas/ui_markup_view_tests.zig +151 -2
  43. package/src/primitives/canvas/ui_schema.zig +31 -1
  44. package/src/primitives/canvas/ui_schema_tests.zig +11 -11
  45. package/src/primitives/canvas/ui_tests.zig +31 -0
  46. package/src/primitives/canvas/vector.zig +258 -141
  47. package/src/primitives/canvas/widget_access.zig +1 -1
  48. package/src/primitives/canvas/widget_builtin_tests.zig +259 -0
  49. package/src/primitives/canvas/widget_layout.zig +13 -5
  50. package/src/primitives/canvas/widget_layout_tests.zig +82 -0
  51. package/src/primitives/canvas/widget_render.zig +78 -40
  52. package/src/primitives/canvas/widget_render_controls.zig +15 -4
  53. package/src/primitives/canvas/widget_semantics.zig +6 -1
  54. package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
  55. package/src/primitives/canvas/widget_text_input.zig +118 -10
  56. package/src/primitives/canvas/widget_tree.zig +14 -2
  57. package/src/primitives/canvas/widgets.zig +28 -0
  58. package/src/root.zig +5 -0
  59. package/src/runtime/api.zig +13 -0
  60. package/src/runtime/automation_commands.zig +33 -0
  61. package/src/runtime/automation_widget_dispatch.zig +159 -28
  62. package/src/runtime/canvas_font_tests.zig +272 -3
  63. package/src/runtime/canvas_fonts.zig +60 -20
  64. package/src/runtime/canvas_frame.zig +15 -0
  65. package/src/runtime/canvas_frame_helpers.zig +15 -0
  66. package/src/runtime/canvas_image_tests.zig +26 -0
  67. package/src/runtime/canvas_images.zig +33 -7
  68. package/src/runtime/canvas_limits.zig +44 -8
  69. package/src/runtime/canvas_widget_anchored_tests.zig +253 -65
  70. package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
  71. package/src/runtime/canvas_widget_context_menu.zig +3 -2
  72. package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
  73. package/src/runtime/canvas_widget_events.zig +1295 -7
  74. package/src/runtime/canvas_widget_floating_tests.zig +2801 -0
  75. package/src/runtime/canvas_widget_runtime.zig +24 -7
  76. package/src/runtime/canvas_widget_scroll_drivers.zig +1 -1
  77. package/src/runtime/canvas_widget_state.zig +88 -6
  78. package/src/runtime/canvas_widget_text_tests.zig +277 -0
  79. package/src/runtime/core.zig +112 -6
  80. package/src/runtime/flow.zig +51 -1
  81. package/src/runtime/gpu_surface_events.zig +51 -5
  82. package/src/runtime/media_surface.zig +726 -0
  83. package/src/runtime/media_surface_tests.zig +1476 -0
  84. package/src/runtime/root.zig +9 -0
  85. package/src/runtime/session_journal.zig +52 -13
  86. package/src/runtime/session_record.zig +166 -0
  87. package/src/runtime/session_tests.zig +575 -0
  88. package/src/runtime/tests.zig +1 -0
  89. package/src/runtime/ts_ui_app.zig +56 -0
  90. package/src/runtime/ui_app.zig +64 -1
  91. package/src/runtime/ui_app_tests.zig +794 -0
  92. package/src/runtime/view.zig +86 -0
  93. package/src/runtime/view_widget_text.zig +18 -1
  94. package/src/runtime/view_widget_tree.zig +339 -20
  95. package/src/runtime/widget_bridge.zig +26 -0
  96. package/src/runtime/window_storage.zig +61 -15
  97. package/src/runtime/window_view_runtime.zig +13 -0
  98. package/src/runtime/window_views.zig +6 -0
@@ -132,80 +132,89 @@ fn pickerOptions() PickerApp.Options {
132
132
  };
133
133
  }
134
134
 
135
- const Fixture = struct {
136
- harness: *core.TestHarness(),
137
- app_state: *PickerApp,
138
- app: core.App,
139
-
140
- fn create() !Fixture {
141
- const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
142
- errdefer harness.destroy(std.testing.allocator);
143
- harness.null_platform.gpu_surfaces = true;
144
- const app_state = try PickerApp.create(std.heap.page_allocator, pickerOptions());
145
- const app = app_state.app();
146
- try harness.start(app);
147
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
148
- .label = canvas_label,
149
- .size = geometry.SizeF.init(400, 300),
150
- .scale_factor = 2,
151
- .frame_index = 1,
152
- .timestamp_ns = 1_000_000,
153
- .nonblank = true,
154
- } });
155
- return .{ .harness = harness, .app_state = app_state, .app = app };
156
- }
135
+ /// The TEA harness shape every anchored fixture in this file shares,
136
+ /// parameterized over the app so the tooltip-coexistence picker below
137
+ /// reuses the picker's dispatch helpers verbatim.
138
+ fn AppFixture(comptime AppType: type, comptime appOptions: fn () AppType.Options) type {
139
+ return struct {
140
+ harness: *core.TestHarness(),
141
+ app_state: *AppType,
142
+ app: core.App,
143
+
144
+ const Self = @This();
145
+
146
+ fn create() !Self {
147
+ const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
148
+ errdefer harness.destroy(std.testing.allocator);
149
+ harness.null_platform.gpu_surfaces = true;
150
+ const app_state = try AppType.create(std.heap.page_allocator, appOptions());
151
+ const app = app_state.app();
152
+ try harness.start(app);
153
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_frame = .{
154
+ .label = canvas_label,
155
+ .size = geometry.SizeF.init(400, 300),
156
+ .scale_factor = 2,
157
+ .frame_index = 1,
158
+ .timestamp_ns = 1_000_000,
159
+ .nonblank = true,
160
+ } });
161
+ return .{ .harness = harness, .app_state = app_state, .app = app };
162
+ }
157
163
 
158
- fn destroy(self: Fixture) void {
159
- self.app_state.destroy();
160
- self.harness.destroy(std.testing.allocator);
161
- }
164
+ fn destroy(self: Self) void {
165
+ self.app_state.destroy();
166
+ self.harness.destroy(std.testing.allocator);
167
+ }
162
168
 
163
- fn widgetIdByText(self: Fixture, kind: canvas.WidgetKind, text: []const u8) ?canvas.ObjectId {
164
- return findIn(self.app_state.tree.?.root, kind, text);
165
- }
169
+ fn widgetIdByText(self: Self, kind: canvas.WidgetKind, text: []const u8) ?canvas.ObjectId {
170
+ return findIn(self.app_state.tree.?.root, kind, text);
171
+ }
166
172
 
167
- fn findIn(widget: canvas.Widget, kind: canvas.WidgetKind, text: []const u8) ?canvas.ObjectId {
168
- if (widget.kind == kind and std.mem.eql(u8, widget.text, text)) return widget.id;
169
- for (widget.children) |child| {
170
- if (findIn(child, kind, text)) |id| return id;
173
+ fn findIn(widget: canvas.Widget, kind: canvas.WidgetKind, text: []const u8) ?canvas.ObjectId {
174
+ if (widget.kind == kind and std.mem.eql(u8, widget.text, text)) return widget.id;
175
+ for (widget.children) |child| {
176
+ if (findIn(child, kind, text)) |id| return id;
177
+ }
178
+ return null;
171
179
  }
172
- return null;
173
- }
174
180
 
175
- fn retainedFrame(self: Fixture, id: canvas.ObjectId) !?geometry.RectF {
176
- const layout = try self.harness.runtime.canvasWidgetLayout(1, canvas_label);
177
- const node = layout.findById(id) orelse return null;
178
- if (node.widget.semantics.hidden) return null;
179
- return node.frame;
180
- }
181
+ fn retainedFrame(self: Self, id: canvas.ObjectId) !?geometry.RectF {
182
+ const layout = try self.harness.runtime.canvasWidgetLayout(1, canvas_label);
183
+ const node = layout.findById(id) orelse return null;
184
+ if (node.widget.semantics.hidden) return null;
185
+ return node.frame;
186
+ }
181
187
 
182
- fn pointer(self: Fixture, kind: support.platform.GpuSurfaceInputKind, point: geometry.PointF) !void {
183
- try self.harness.runtime.dispatchPlatformEvent(self.app, .{ .gpu_surface_input = .{
184
- .label = canvas_label,
185
- .kind = kind,
186
- .x = point.x,
187
- .y = point.y,
188
- } });
189
- }
188
+ fn pointer(self: Self, kind: support.platform.GpuSurfaceInputKind, point: geometry.PointF) !void {
189
+ try self.harness.runtime.dispatchPlatformEvent(self.app, .{ .gpu_surface_input = .{
190
+ .label = canvas_label,
191
+ .kind = kind,
192
+ .x = point.x,
193
+ .y = point.y,
194
+ } });
195
+ }
190
196
 
191
- fn click(self: Fixture, point: geometry.PointF) !void {
192
- try self.pointer(.pointer_down, point);
193
- try self.pointer(.pointer_up, point);
194
- }
197
+ fn click(self: Self, point: geometry.PointF) !void {
198
+ try self.pointer(.pointer_down, point);
199
+ try self.pointer(.pointer_up, point);
200
+ }
195
201
 
196
- fn clickWidget(self: Fixture, id: canvas.ObjectId) !void {
197
- const frame = (try self.retainedFrame(id)) orelse return error.TestUnexpectedResult;
198
- try self.click(frame.center());
199
- }
202
+ fn clickWidget(self: Self, id: canvas.ObjectId) !void {
203
+ const frame = (try self.retainedFrame(id)) orelse return error.TestUnexpectedResult;
204
+ try self.click(frame.center());
205
+ }
200
206
 
201
- fn key(self: Fixture, name: []const u8) !void {
202
- try self.harness.runtime.dispatchPlatformEvent(self.app, .{ .gpu_surface_input = .{
203
- .label = canvas_label,
204
- .kind = .key_down,
205
- .key = name,
206
- } });
207
- }
208
- };
207
+ fn key(self: Self, name: []const u8) !void {
208
+ try self.harness.runtime.dispatchPlatformEvent(self.app, .{ .gpu_surface_input = .{
209
+ .label = canvas_label,
210
+ .kind = .key_down,
211
+ .key = name,
212
+ } });
213
+ }
214
+ };
215
+ }
216
+
217
+ const Fixture = AppFixture(PickerApp, pickerOptions);
209
218
 
210
219
  test "anchored picker: trigger opens, menu floats, item click picks and closes" {
211
220
  const fixture = try Fixture.create();
@@ -676,3 +685,182 @@ test "the per-view anchored budget rejects a surface per row loudly" {
676
685
  harness.runtime.setCanvasWidgetLayout(1, "canvas", over_budget),
677
686
  );
678
687
  }
688
+
689
+ // ------------------------------------------- tooltip + menu coexistence
690
+ //
691
+ // With `<tooltip>` anchorable beside `dropdown-menu`, one trigger stack
692
+ // can float TWO anchored surfaces at once: Tab earns the focus-shown
693
+ // tooltip, ArrowDown opens the menu beneath it. The tests below pin the
694
+ // keymap against that coexistence — the menu-specific lookups must scan
695
+ // FOR menu kinds instead of kind-checking whatever floats topmost, and
696
+ // Escape peels one surface per press, topmost (the tooltip) first.
697
+
698
+ const CoexistModel = struct {
699
+ open: bool = false,
700
+ picked: u32 = 99,
701
+ picks: u32 = 0,
702
+ toggles: u32 = 0,
703
+ dismissals: u32 = 0,
704
+ };
705
+
706
+ const CoexistMsg = union(enum) {
707
+ toggle_picker,
708
+ close_picker,
709
+ pick: u32,
710
+ noop,
711
+ };
712
+
713
+ const CoexistApp = ui_app_model.UiApp(CoexistModel, CoexistMsg);
714
+
715
+ fn coexistUpdate(model: *CoexistModel, msg: CoexistMsg) void {
716
+ switch (msg) {
717
+ .toggle_picker => {
718
+ model.open = !model.open;
719
+ model.toggles += 1;
720
+ },
721
+ .close_picker => {
722
+ model.open = false;
723
+ model.dismissals += 1;
724
+ },
725
+ .pick => |index| {
726
+ model.picked = index;
727
+ model.picks += 1;
728
+ model.open = false;
729
+ },
730
+ .noop => {},
731
+ }
732
+ }
733
+
734
+ /// The coexistence stack in the order that shadows: select trigger,
735
+ /// then the anchored dropdown-menu, then the anchored tooltip LAST —
736
+ /// while focus-shown, the tooltip is the topmost anchored child of the
737
+ /// very stack that anchors the open menu. The tooltip carries an
738
+ /// explicit key so its identity survives the open/close rebuilds that
739
+ /// insert and remove the menu ahead of it.
740
+ fn coexistView(ui: *CoexistApp.Ui, model: *const CoexistModel) CoexistApp.Ui.Node {
741
+ const trigger = ui.el(.select, .{ .text = "Repo", .width = 160, .on_press = .toggle_picker }, .{});
742
+ const tooltip = ui.el(.tooltip, .{ .key = .{ .int = 7 }, .text = "Choose the repository", .anchor = .above }, .{});
743
+ const picker = if (model.open) ui.stack(.{ .height = 28 }, .{
744
+ trigger,
745
+ ui.el(.dropdown_menu, .{
746
+ .key = .{ .int = 3 },
747
+ .anchor = .below,
748
+ .anchor_alignment = .stretch,
749
+ .width = 160,
750
+ .height = 90,
751
+ .on_dismiss = .close_picker,
752
+ }, .{
753
+ ui.el(.menu_item, .{ .key = .{ .int = 0 }, .text = "Alpha", .height = 26, .selected = model.picked == 0, .on_press = CoexistMsg{ .pick = 0 } }, .{}),
754
+ ui.el(.menu_item, .{ .key = .{ .int = 1 }, .text = "Beta", .height = 26, .selected = model.picked == 1, .on_press = CoexistMsg{ .pick = 1 } }, .{}),
755
+ }),
756
+ tooltip,
757
+ }) else ui.stack(.{ .height = 28 }, .{ trigger, tooltip });
758
+
759
+ return ui.column(.{ .gap = 8, .padding = 12 }, .{
760
+ picker,
761
+ ui.button(.{ .on_press = .noop }, "After"),
762
+ });
763
+ }
764
+
765
+ fn coexistOptions() CoexistApp.Options {
766
+ return .{
767
+ .name = "ui-app-coexist",
768
+ .scene = picker_scene,
769
+ .canvas_label = canvas_label,
770
+ .update = coexistUpdate,
771
+ .view = coexistView,
772
+ };
773
+ }
774
+
775
+ const CoexistFixture = AppFixture(CoexistApp, coexistOptions);
776
+
777
+ /// Tab onto the select earns the focus-shown tooltip, then ArrowDown
778
+ /// opens the menu beneath it. Returns the trigger id after asserting
779
+ /// the coexistence really holds: menu mounted AND tooltip shown.
780
+ fn coexistOpenWithTooltip(fixture: CoexistFixture) !canvas.ObjectId {
781
+ const trigger_id = fixture.widgetIdByText(.select, "Repo").?;
782
+ try fixture.key("tab");
783
+ try std.testing.expectEqual(trigger_id, fixture.harness.runtime.views[0].canvas_widget_focus_visible_id);
784
+ const tooltip_id = fixture.widgetIdByText(.tooltip, "Choose the repository").?;
785
+ try std.testing.expectEqual(tooltip_id, fixture.harness.runtime.views[0].canvas_tooltip_shown_id);
786
+
787
+ // ArrowDown on the closed trigger presses it (the model-owned
788
+ // open); the arrow is no activation key, so the focus-shown
789
+ // tooltip survives the press AND the rebuild that mounts the menu.
790
+ try fixture.key("arrowdown");
791
+ try std.testing.expect(fixture.app_state.model.open);
792
+ try std.testing.expectEqual(@as(u32, 1), fixture.app_state.model.toggles);
793
+ try std.testing.expectEqual(trigger_id, fixture.harness.runtime.views[0].canvas_widget_focused_id);
794
+ try std.testing.expectEqual(tooltip_id, fixture.harness.runtime.views[0].canvas_tooltip_shown_id);
795
+ try std.testing.expect((try fixture.retainedFrame(tooltip_id)) != null);
796
+ try std.testing.expect((try fixture.retainedFrame(fixture.widgetIdByText(.menu_item, "Alpha").?)) != null);
797
+ return trigger_id;
798
+ }
799
+
800
+ test "tooltip coexistence: arrows walk into the open menu beneath the focus-shown tooltip" {
801
+ const fixture = try CoexistFixture.create();
802
+ defer fixture.destroy();
803
+
804
+ _ = try coexistOpenWithTooltip(fixture);
805
+
806
+ // The next ArrowDown walks INTO the menu — the tooltip floating
807
+ // topmost must not shadow the owned-menu lookup into re-pressing
808
+ // the trigger (which would toggle the picker closed).
809
+ const alpha_id = fixture.widgetIdByText(.menu_item, "Alpha").?;
810
+ try fixture.key("arrowdown");
811
+ try std.testing.expectEqual(alpha_id, fixture.harness.runtime.views[0].canvas_widget_focused_id);
812
+ try std.testing.expect(fixture.app_state.model.open);
813
+ try std.testing.expectEqual(@as(u32, 1), fixture.app_state.model.toggles);
814
+
815
+ // Entering the menu moved focus off the trigger: the focus-shown
816
+ // tooltip hides through its own blur cause, not through any menu
817
+ // machinery.
818
+ try std.testing.expectEqual(@as(canvas.ObjectId, 0), fixture.harness.runtime.views[0].canvas_tooltip_shown_id);
819
+ }
820
+
821
+ test "tooltip coexistence: Tab departure closes the menu the tooltip floats over" {
822
+ const fixture = try CoexistFixture.create();
823
+ defer fixture.destroy();
824
+
825
+ const trigger_id = try coexistOpenWithTooltip(fixture);
826
+ const tooltip_id = fixture.harness.runtime.views[0].canvas_tooltip_shown_id;
827
+
828
+ // Tab is focus departure: it must find and dismiss the MENU even
829
+ // though the tooltip is the topmost anchored child, consume the
830
+ // Tab, and leave the keyboard on the trigger. The tooltip is not
831
+ // the departure's business: focus never left the trigger, so the
832
+ // focus-shown tooltip stays.
833
+ try fixture.key("tab");
834
+ try std.testing.expect(!fixture.app_state.model.open);
835
+ try std.testing.expectEqual(@as(u32, 1), fixture.app_state.model.dismissals);
836
+ try std.testing.expectEqual(@as(u32, 0), fixture.app_state.model.picks);
837
+ try std.testing.expectEqual(trigger_id, fixture.harness.runtime.views[0].canvas_widget_focused_id);
838
+ try std.testing.expectEqual(tooltip_id, fixture.harness.runtime.views[0].canvas_tooltip_shown_id);
839
+ }
840
+
841
+ test "tooltip coexistence: escape peels the tooltip first, then the menu, one per press" {
842
+ const fixture = try CoexistFixture.create();
843
+ defer fixture.destroy();
844
+
845
+ const trigger_id = try coexistOpenWithTooltip(fixture);
846
+ const tooltip_id = fixture.harness.runtime.views[0].canvas_tooltip_shown_id;
847
+
848
+ // Escape dismisses exactly one surface per press, topmost
849
+ // (last-mounted) first: the tooltip goes while the menu stays
850
+ // open and the keyboard stays on the trigger.
851
+ try fixture.key("escape");
852
+ try std.testing.expectEqual(@as(canvas.ObjectId, 0), fixture.harness.runtime.views[0].canvas_tooltip_shown_id);
853
+ try std.testing.expect((try fixture.retainedFrame(tooltip_id)) == null);
854
+ try std.testing.expect(fixture.app_state.model.open);
855
+ try std.testing.expectEqual(@as(u32, 0), fixture.app_state.model.dismissals);
856
+ try std.testing.expectEqual(trigger_id, fixture.harness.runtime.views[0].canvas_widget_focused_id);
857
+
858
+ // The next Escape reaches the menu through the model's on_dismiss,
859
+ // still without committing, and the keyboard stays on the trigger.
860
+ try fixture.key("escape");
861
+ try std.testing.expect(!fixture.app_state.model.open);
862
+ try std.testing.expectEqual(@as(u32, 1), fixture.app_state.model.dismissals);
863
+ try std.testing.expectEqual(@as(u32, 0), fixture.app_state.model.picks);
864
+ try std.testing.expectEqual(@as(u32, 99), fixture.app_state.model.picked);
865
+ try std.testing.expectEqual(trigger_id, fixture.harness.runtime.views[0].canvas_widget_focused_id);
866
+ }
@@ -168,6 +168,129 @@ test "keyboard-only selection (shift+arrows) feeds copy" {
168
168
  try std.testing.expectEqualStrings("ry", try harness.runtime.readClipboard(&clipboard_buffer));
169
169
  }
170
170
 
171
+ test "a multi-line paste into a single-line field strips line breaks; a textarea keeps them" {
172
+ var app_state: ClipboardTestApp = .{};
173
+ const app = app_state.app();
174
+ const harness = try createClipboardHarness(app);
175
+ defer harness.destroy(std.testing.allocator);
176
+
177
+ const children = [_]canvas.Widget{
178
+ .{
179
+ .id = 2,
180
+ .kind = .text_field,
181
+ .frame = geometry.RectF.init(12, 16, 200, 36),
182
+ .text = "",
183
+ },
184
+ .{
185
+ .id = 3,
186
+ .kind = .textarea,
187
+ .frame = geometry.RectF.init(12, 64, 280, 100),
188
+ .text = "",
189
+ },
190
+ };
191
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
192
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 320, 200), &nodes);
193
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
194
+
195
+ // Interior LF, CRLF, and a trailing CR: the single-line field strips
196
+ // every break (the HTML value-sanitization rule — lines join with
197
+ // nothing between them), and the app-facing keyboard event carries
198
+ // the SAME stripped insert so a model mirror stays byte-identical.
199
+ try harness.runtime.writeClipboard("alpha\nbeta\r\ngamma\r");
200
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 100, 30));
201
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("v", cmd));
202
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
203
+ try std.testing.expectEqualStrings("alphabetagamma", retained.nodes[1].widget.text);
204
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(14), retained.nodes[1].widget.text_selection.?);
205
+ try std.testing.expectEqualStrings("alphabetagamma", app_state.last_edit_insert[0..app_state.last_edit_insert_len]);
206
+ try std.testing.expect(!app_state.saw_truncated);
207
+
208
+ // The SAME clipboard pasted into the textarea keeps its breaks: only
209
+ // single-line kinds sanitize.
210
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 100, 100));
211
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("v", cmd));
212
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
213
+ try std.testing.expectEqualStrings("alpha\nbeta\r\ngamma\r", retained.nodes[2].widget.text);
214
+ try std.testing.expectEqualStrings("alpha\nbeta\r\ngamma\r", app_state.last_edit_insert[0..app_state.last_edit_insert_len]);
215
+ }
216
+
217
+ test "a paste of only line breaks into a single-line field inserts nothing and keeps the selection" {
218
+ var app_state: ClipboardTestApp = .{};
219
+ const app = app_state.app();
220
+ const harness = try createClipboardHarness(app);
221
+ defer harness.destroy(std.testing.allocator);
222
+
223
+ const text_field = canvas.Widget{
224
+ .id = 2,
225
+ .kind = .text_field,
226
+ .frame = geometry.RectF.init(12, 16, 200, 36),
227
+ .text = "Keep",
228
+ };
229
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
230
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{text_field} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
231
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
232
+
233
+ // Select all, then paste bare newlines: the sanitized insert is
234
+ // EMPTY, so the edit suppresses — nothing inserts, the live
235
+ // selection survives, and the app hears no insert at all (an empty
236
+ // insert is cut's delete-selection edit, which this must never be).
237
+ try harness.runtime.writeClipboard("\r\n\n");
238
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 100, 30));
239
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("a", cmd));
240
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("v", cmd));
241
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
242
+ try std.testing.expectEqualStrings("Keep", retained.nodes[1].widget.text);
243
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 4 }, retained.nodes[1].widget.text_selection.?);
244
+ try std.testing.expect(!app_state.saw_empty_insert);
245
+ try std.testing.expectEqual(@as(usize, 0), app_state.last_edit_insert_len);
246
+ }
247
+
248
+ test "a near-capacity multi-line paste sanitizes before it clamps" {
249
+ var app_state: ClipboardTestApp = .{};
250
+ const app = app_state.app();
251
+ const harness = try createClipboardHarness(app);
252
+ defer harness.destroy(std.testing.allocator);
253
+
254
+ // Fill the view's shared widget-text storage to EXACTLY three free
255
+ // bytes, the boundary where the sanitize/clamp ordering shows:
256
+ // pasting "a\nbc" must land "abc" (sanitized 3 bytes, fits whole).
257
+ // Clamping the raw clipboard first would spend the third byte on
258
+ // the '\n' the seam strips anyway — "a\nb" then "ab" — silently
259
+ // discarding the valid 'c' and flagging a truncation that never
260
+ // happened to the bytes that count.
261
+ const fill_len = canvas_limits.max_canvas_widget_text_bytes_per_view - 3;
262
+ const fill = try std.testing.allocator.alloc(u8, fill_len);
263
+ defer std.testing.allocator.free(fill);
264
+ @memset(fill, 'x');
265
+ const text_field = canvas.Widget{
266
+ .id = 2,
267
+ .kind = .text_field,
268
+ .frame = geometry.RectF.init(12, 16, 200, 36),
269
+ .text = fill,
270
+ };
271
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
272
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{text_field} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
273
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
274
+ // Pin the boundary premise: exactly 3 bytes of capacity remain.
275
+ try std.testing.expectEqual(fill_len, harness.runtime.views[0].widget_text_len);
276
+
277
+ try harness.runtime.writeClipboard("a\nbc");
278
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 100, 30));
279
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("end", .{}));
280
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("v", cmd));
281
+
282
+ // The retained editor holds the whole sanitized suffix ...
283
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
284
+ const text = retained.nodes[1].widget.text;
285
+ try std.testing.expectEqual(fill_len + 3, text.len);
286
+ try std.testing.expectEqualStrings("abc", text[text.len - 3 ..]);
287
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(text.len), retained.nodes[1].widget.text_selection.?);
288
+ // ... the app's stamped edit is byte-identical to what the editor
289
+ // applied, and nothing was (falsely) reported truncated.
290
+ try std.testing.expectEqualStrings("abc", app_state.last_edit_insert[0..app_state.last_edit_insert_len]);
291
+ try std.testing.expect(!app_state.saw_truncated);
292
+ }
293
+
171
294
  test "paste clamps to view text capacity and flags truncation loudly" {
172
295
  var app_state: ClipboardTestApp = .{};
173
296
  const app = app_state.app();
@@ -274,8 +274,9 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
274
274
  /// the event to the app — the same two motions keyboard-driven
275
275
  /// edits perform, so runtime widget and app model stay in sync.
276
276
  fn applyEditKeyboardEvent(self: *Runtime, app: runtime_api.App(Runtime), keyboard_event: runtime_api.CanvasWidgetKeyboardEvent) anyerror!void {
277
- try CanvasWidgetEventMethods().updateCanvasWidgetTextFromKeyboard(self, keyboard_event);
278
- try self.dispatchEvent(app, .{ .canvas_widget_keyboard = keyboard_event });
277
+ var event = keyboard_event;
278
+ try CanvasWidgetEventMethods().updateCanvasWidgetTextFromKeyboard(self, &event);
279
+ try self.dispatchEvent(app, .{ .canvas_widget_keyboard = event });
279
280
  }
280
281
 
281
282
  fn editableSelectionText(self: *Runtime, view_index: usize, target_id: canvas.ObjectId) ?[]const u8 {
@@ -25,6 +25,9 @@ const MenuTestApp = struct {
25
25
  last_menu_item_index: usize = 0,
26
26
  request_count: u32 = 0,
27
27
  last_request_target: canvas.ObjectId = 0,
28
+ last_edit_insert: [64]u8 = undefined,
29
+ last_edit_insert_len: usize = 0,
30
+ saw_truncated: bool = false,
28
31
 
29
32
  fn app(self: *@This()) App {
30
33
  return .{ .context = self, .name = "context-menus", .source = platform.WebViewSource.html("<h1>Hello</h1>"), .event_fn = event };
@@ -45,6 +48,17 @@ const MenuTestApp = struct {
45
48
  self.request_count += 1;
46
49
  self.last_request_target = request_event.target_id;
47
50
  },
51
+ .canvas_widget_keyboard => |keyboard_event| {
52
+ if (keyboard_event.keyboard.edit_truncated) self.saw_truncated = true;
53
+ if (keyboard_event.keyboard.edit) |edit| switch (edit) {
54
+ .insert_text => |text| {
55
+ const len = @min(text.len, self.last_edit_insert.len);
56
+ @memcpy(self.last_edit_insert[0..len], text[0..len]);
57
+ self.last_edit_insert_len = len;
58
+ },
59
+ else => {},
60
+ };
61
+ },
48
62
  else => {},
49
63
  }
50
64
  }
@@ -210,6 +224,47 @@ test "right click on editable text presents the default edit menu wired to clipb
210
224
  try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 5 }, retained.nodes[1].widget.text_selection.?);
211
225
  }
212
226
 
227
+ test "a near-capacity multi-line context-menu paste sanitizes before it clamps" {
228
+ var app_state: MenuTestApp = .{};
229
+ const app = app_state.app();
230
+ const harness = try createMenuHarness(app);
231
+ defer harness.destroy(std.testing.allocator);
232
+
233
+ // Same boundary as the cmd+V shortcut test: exactly three free bytes
234
+ // in the view's shared widget-text storage, clipboard "a\nbc". The
235
+ // context menu's Paste flows through the same sanitize-then-clamp
236
+ // helper, so the sanitized "abc" lands whole instead of the raw
237
+ // clamp's "a\nb" -> "ab".
238
+ const fill_len = canvas_limits.max_canvas_widget_text_bytes_per_view - 3;
239
+ const fill = try std.testing.allocator.alloc(u8, fill_len);
240
+ defer std.testing.allocator.free(fill);
241
+ @memset(fill, 'x');
242
+ const text_field = canvas.Widget{
243
+ .id = 2,
244
+ .kind = .text_field,
245
+ .frame = geometry.RectF.init(12, 16, 200, 36),
246
+ .text = fill,
247
+ };
248
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
249
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{text_field} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
250
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
251
+ try std.testing.expectEqual(fill_len, harness.runtime.views[0].widget_text_len);
252
+
253
+ try harness.runtime.writeClipboard("a\nbc");
254
+ try harness.runtime.dispatchPlatformEvent(app, rightClick(100, 30));
255
+ // Paste is the default edit menu's third item.
256
+ try harness.runtime.dispatchPlatformEvent(app, menuAction(2, 3));
257
+
258
+ // Retained editor and the app's stamped edit hear the same whole
259
+ // sanitized suffix, with no false truncation flag.
260
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
261
+ const text = retained.nodes[1].widget.text;
262
+ try std.testing.expectEqual(fill_len + 3, text.len);
263
+ try std.testing.expectEqualStrings("abc", text[text.len - 3 ..]);
264
+ try std.testing.expectEqualStrings("abc", app_state.last_edit_insert[0..app_state.last_edit_insert_len]);
265
+ try std.testing.expect(!app_state.saw_truncated);
266
+ }
267
+
213
268
  test "right click on selected static text presents a copy-only menu" {
214
269
  var app_state: MenuTestApp = .{};
215
270
  const app = app_state.app();