@native-sdk/cli 0.6.2 → 0.7.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 (64) hide show
  1. package/build.zig +8 -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/text.ts +62 -21
  6. package/skill-data/native-ui/SKILL.md +17 -2
  7. package/src/platform/macos/appkit_host.h +2 -0
  8. package/src/platform/macos/appkit_host.m +36 -10
  9. package/src/platform/macos/root.zig +7 -1
  10. package/src/platform/types.zig +2 -0
  11. package/src/primitives/canvas/code.zig +590 -0
  12. package/src/primitives/canvas/code_tests.zig +1062 -0
  13. package/src/primitives/canvas/equality.zig +3 -1
  14. package/src/primitives/canvas/events.zig +4 -0
  15. package/src/primitives/canvas/layout_audit.zig +3 -1
  16. package/src/primitives/canvas/markdown.zig +13 -11
  17. package/src/primitives/canvas/markdown_tests.zig +167 -4
  18. package/src/primitives/canvas/root.zig +15 -2
  19. package/src/primitives/canvas/tests.zig +1 -0
  20. package/src/primitives/canvas/text.zig +10 -0
  21. package/src/primitives/canvas/text_interaction.zig +164 -22
  22. package/src/primitives/canvas/text_layout.zig +107 -21
  23. package/src/primitives/canvas/text_measure_cache.zig +4 -4
  24. package/src/primitives/canvas/text_span_tests.zig +254 -0
  25. package/src/primitives/canvas/text_spans.zig +556 -32
  26. package/src/primitives/canvas/text_tests.zig +86 -0
  27. package/src/primitives/canvas/themes/geist.zig +29 -0
  28. package/src/primitives/canvas/tokens.zig +72 -2
  29. package/src/primitives/canvas/ui.zig +325 -21
  30. package/src/primitives/canvas/ui_markup.zig +59 -2
  31. package/src/primitives/canvas/ui_markup_compiled.zig +76 -0
  32. package/src/primitives/canvas/ui_markup_compiled_tests.zig +33 -0
  33. package/src/primitives/canvas/ui_markup_contract.zig +34 -0
  34. package/src/primitives/canvas/ui_markup_reflect.zig +20 -3
  35. package/src/primitives/canvas/ui_markup_view.zig +73 -0
  36. package/src/primitives/canvas/ui_markup_view_tests.zig +101 -0
  37. package/src/primitives/canvas/ui_schema.zig +17 -6
  38. package/src/primitives/canvas/ui_schema_tests.zig +7 -6
  39. package/src/primitives/canvas/ui_tests.zig +6 -0
  40. package/src/primitives/canvas/widget_invalidation.zig +3 -0
  41. package/src/primitives/canvas/widget_layout.zig +57 -8
  42. package/src/primitives/canvas/widget_layout_tests.zig +35 -0
  43. package/src/primitives/canvas/widget_metrics.zig +30 -1
  44. package/src/primitives/canvas/widget_render.zig +454 -9
  45. package/src/primitives/canvas/widget_runtime.zig +1 -0
  46. package/src/primitives/canvas/widget_text_input.zig +23 -11
  47. package/src/primitives/canvas/widget_text_select.zig +2 -2
  48. package/src/primitives/canvas/widgets.zig +13 -0
  49. package/src/root.zig +1 -0
  50. package/src/runtime/api.zig +9 -0
  51. package/src/runtime/canvas_frame_helpers.zig +1 -0
  52. package/src/runtime/canvas_limits.zig +7 -0
  53. package/src/runtime/canvas_widget_accessibility_tests.zig +27 -1
  54. package/src/runtime/canvas_widget_clipboard_tests.zig +146 -0
  55. package/src/runtime/canvas_widget_context_menu.zig +12 -2
  56. package/src/runtime/canvas_widget_display.zig +15 -1
  57. package/src/runtime/canvas_widget_events.zig +50 -20
  58. package/src/runtime/canvas_widget_runtime.zig +74 -12
  59. package/src/runtime/canvas_widget_state.zig +1 -0
  60. package/src/runtime/canvas_widget_text_tests.zig +1935 -70
  61. package/src/runtime/gpu_surface_events.zig +38 -0
  62. package/src/runtime/view.zig +37 -3
  63. package/src/runtime/view_widget_text.zig +1242 -14
  64. package/src/runtime/view_widget_tree.zig +2 -0
@@ -863,6 +863,19 @@ pub const Widget = struct {
863
863
  /// tooling and write-back can round-trip it. Span paragraphs
864
864
  /// (`spans`) wrap by design and ignore it.
865
865
  text_no_wrap: bool = false,
866
+ /// Renderer-owned logical-line gutter for a syntax-code paragraph.
867
+ /// Zero keeps an ordinary paragraph; a positive value is the decimal
868
+ /// digit width of the largest marker. The gutter is decoration, not
869
+ /// retained text, so selection/copy remains the exact source bytes.
870
+ /// Stamped only by `Ui.code`; there is no generic builder/markup
871
+ /// channel for turning arbitrary paragraphs into numbered code.
872
+ code_line_number_digits: u8 = 0,
873
+ /// Nonzero on bounded paragraph chunks that together present one
874
+ /// selectable source-code document. The group id is the structural id
875
+ /// of their internal parent; offsets order each chunk's exact source
876
+ /// bytes without duplicating the full source in retained storage.
877
+ static_text_group_id: ObjectId = 0,
878
+ static_text_group_offset: usize = 0,
866
879
  /// What a single-line text run does with content that does not fit
867
880
  /// its frame (`ElementOptions.overflow` / markup `overflow=` on
868
881
  /// text leaves): `.ellipsis` (default) elides the tail behind a
package/src/root.zig CHANGED
@@ -6,6 +6,7 @@ pub const trace = @import("trace");
6
6
  pub const diagnostics = @import("diagnostics");
7
7
  pub const platform_info = @import("platform_info");
8
8
  pub const canvas = @import("canvas");
9
+ pub const code = canvas.code;
9
10
  pub const markdown = canvas.markdown;
10
11
 
11
12
  pub const runtime = @import("runtime/root.zig");
@@ -95,6 +95,15 @@ pub const CanvasWidgetKeyboardEvent = struct {
95
95
  /// tail so an edit that unmounts the hovered listener never
96
96
  /// strands its leave.
97
97
  standalone: bool = false,
98
+ /// Continuation edits replay an existing history entry and must not
99
+ /// themselves create a new entry.
100
+ history_replay: bool = false,
101
+ /// Stable retained-history identity for a compound undo/redo. Follow-up
102
+ /// edits are re-derived from this entry after every controlled-tree
103
+ /// rebuild instead of retaining borrowed slices into the compactable
104
+ /// history byte pool.
105
+ history_replay_serial: u64 = 0,
106
+ history_replay_redo: bool = false,
98
107
  };
99
108
 
100
109
  /// A scroll container's offset changed from a user gesture (wheel,
@@ -327,6 +327,7 @@ pub fn canvasWidgetPointerEventFromGpuInput(input_event: GpuSurfaceInputEvent) ?
327
327
  .delta = geometry.OffsetF.init(input_event.delta_x, input_event.delta_y),
328
328
  .pointer_id = input_event.pointer_id,
329
329
  .button = input_event.button,
330
+ .modifiers = canvasWidgetKeyboardModifiers(input_event.modifiers),
330
331
  };
331
332
  }
332
333
 
@@ -176,6 +176,13 @@ pub const max_canvas_widget_semantics_per_view: usize = 1024;
176
176
  // a 1024-node view retains proportionally more text.
177
177
  pub const max_canvas_widget_text_bytes_per_view: usize = 65536;
178
178
  pub const max_canvas_widget_source_text_entries_per_view: usize = 256;
179
+ /// Retained editor undo/redo is delta-based: changed bytes share this
180
+ /// per-view pool and metadata stays bounded independently. Matching the
181
+ /// live text budget guarantees that clearing a maximal editor can still
182
+ /// be undone; an unusually large whole-document replacement whose removed
183
+ /// plus inserted bytes exceed the pool simply starts a fresh history.
184
+ pub const max_canvas_widget_text_history_bytes_per_view: usize = max_canvas_widget_text_bytes_per_view;
185
+ pub const max_canvas_widget_text_history_entries_per_view: usize = 128;
179
186
  // Inline styled runs retained across all `.text` widgets of a view. Each
180
187
  // span is a small struct (style flags + slices into the widget text
181
188
  // bytes); per-paragraph capacity is `canvas.max_text_spans_per_paragraph`.
@@ -846,6 +846,7 @@ test "runtime publishes canvas widget accessibility snapshots to platform" {
846
846
  .{ .id = 4, .kind = .text_field, .frame = geometry.RectF.init(12, 96, 160, 28), .text = "Search", .placeholder = "Search deployments", .text_selection = canvas.TextSelection{ .anchor = 1, .focus = 4 }, .text_composition = canvas.TextRange.init(2, 5), .state = .{ .required = true, .read_only = true, .invalid = true } },
847
847
  .{ .id = 5, .kind = .select, .frame = geometry.RectF.init(184, 96, 120, 28), .text = "Production", .state = .{ .expanded = false }, .semantics = .{ .label = "Environment" } },
848
848
  .{ .id = 10, .kind = .data_grid, .frame = geometry.RectF.init(12, 132, 220, 64), .text = "Deployments", .layout = .{ .gap = 2 }, .children = &rows },
849
+ .{ .id = 6, .kind = .input, .frame = geometry.RectF.init(184, 58, 120, 28), .text = "Draft" },
849
850
  };
850
851
  var layout_nodes: [16]canvas.WidgetLayoutNode = undefined;
851
852
  const layout = try canvas.layoutWidgetTree(.{
@@ -860,7 +861,7 @@ test "runtime publishes canvas widget accessibility snapshots to platform" {
860
861
  try std.testing.expect(platform_state.update_count >= 1);
861
862
  try std.testing.expectEqual(@as(platform.WindowId, 1), platform_state.window_id);
862
863
  try std.testing.expectEqualStrings("canvas", platform_state.view_label[0..platform_state.view_label_len]);
863
- try std.testing.expectEqual(@as(usize, 12), platform_state.node_count);
864
+ try std.testing.expectEqual(@as(usize, 13), platform_state.node_count);
864
865
  try std.testing.expectEqual(platform.WidgetAccessibilityRole.group, platform_state.nodes[0].role);
865
866
  try std.testing.expectEqual(platform.WidgetAccessibilityRole.button, platform_state.nodes[1].role);
866
867
  try std.testing.expectEqual(platform.WidgetAccessibilityRole.checkbox, platform_state.nodes[2].role);
@@ -870,12 +871,15 @@ test "runtime publishes canvas widget accessibility snapshots to platform" {
870
871
  const cell_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 16).?;
871
872
  const text_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 4).?;
872
873
  const select_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 5).?;
874
+ const initial_editor_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 6).?;
873
875
  try std.testing.expectEqual(@as(?bool, false), select_node.expanded);
874
876
  try std.testing.expect(text_node.required);
875
877
  try std.testing.expect(text_node.read_only);
876
878
  try std.testing.expect(text_node.invalid);
877
879
  try std.testing.expect(!text_node.actions.set_text);
878
880
  try std.testing.expect(text_node.actions.set_selection);
881
+ try std.testing.expect(!initial_editor_node.can_undo);
882
+ try std.testing.expect(!initial_editor_node.can_redo);
879
883
  try std.testing.expectEqual(platform.WidgetAccessibilityRole.grid, grid_node.role);
880
884
  try std.testing.expectEqual(@as(?usize, 2), grid_node.grid_row_count);
881
885
  try std.testing.expectEqual(@as(?usize, 2), grid_node.grid_column_count);
@@ -900,6 +904,28 @@ test "runtime publishes canvas widget accessibility snapshots to platform" {
900
904
  try std.testing.expectEqual(@as(f32, 12), platform_state.nodes[1].bounds.x);
901
905
  try std.testing.expectEqual(@as(f32, 14), platform_state.nodes[1].bounds.y);
902
906
 
907
+ runtime.views[0].focused = true;
908
+ runtime.views[0].keyboard_active = true;
909
+ runtime.views[0].canvas_widget_focused_id = 6;
910
+ _ = try runtime.views[0].applyCanvasWidgetTextEdit(6, .{ .insert_text = "!" });
911
+ _ = try runtime.refreshCanvasWidgetDisplayListIfOwned(0);
912
+ const edited_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 6).?;
913
+ try std.testing.expect(edited_node.can_undo);
914
+ try std.testing.expect(!edited_node.can_redo);
915
+
916
+ const editor_target = runtime.views[0].widgetLayoutTree().focusTargetById(6).?;
917
+ const undo = runtime.views[0].canvasWidgetTextHistoryShortcut(editor_target, .{
918
+ .phase = .key_down,
919
+ .key = "z",
920
+ .modifiers = .{ .super = true },
921
+ }).?;
922
+ _ = try runtime.views[0].applyCanvasWidgetTextEditWithoutHistory(6, undo.edit);
923
+ runtime.views[0].commitCanvasWidgetTextHistoryReplayIfComplete(editor_target, undo.serial, undo.redo);
924
+ _ = try runtime.refreshCanvasWidgetDisplayListIfOwned(0);
925
+ const undone_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 6).?;
926
+ try std.testing.expect(!undone_node.can_undo);
927
+ try std.testing.expect(undone_node.can_redo);
928
+
903
929
  const published_after_layout = platform_state.update_count;
904
930
  try runtime.dispatchPlatformEvent(app_state.app(), .{ .gpu_surface_input = .{
905
931
  .window_id = 1,
@@ -483,6 +483,106 @@ test "static text drag selection highlights, copies, and clears" {
483
483
  // With nothing selected, copy leaves the clipboard untouched.
484
484
  try harness.runtime.dispatchPlatformEvent(app, keyInput("c", cmd));
485
485
  try std.testing.expectEqualStrings(body, try harness.runtime.readClipboard(&clipboard_buffer));
486
+
487
+ // Shift is an editable-text extension gesture. It must not prevent a
488
+ // fresh static-text drag from establishing its own press anchor.
489
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
490
+ .window_id = 1,
491
+ .label = "canvas",
492
+ .kind = .pointer_down,
493
+ .x = 13,
494
+ .y = 18,
495
+ .modifiers = .{ .shift = true },
496
+ } });
497
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_drag, 211, 75));
498
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
499
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = body.len }, retained.nodes[1].widget.text_selection.?);
500
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_selected_text_id);
501
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_up, 211, 75));
502
+ }
503
+
504
+ test "grouped code paragraphs select and copy across chunk boundaries" {
505
+ var app_state: ClipboardTestApp = .{};
506
+ const app = app_state.app();
507
+ const harness = try createClipboardHarness(app);
508
+ defer harness.destroy(std.testing.allocator);
509
+
510
+ const first = "alpha\r\n";
511
+ const second = "beta";
512
+ const first_spans = [_]canvas.TextSpan{.{
513
+ .text = first,
514
+ .monospace = true,
515
+ .color = .syntax_plain,
516
+ }};
517
+ const second_spans = [_]canvas.TextSpan{.{
518
+ .text = second,
519
+ .monospace = true,
520
+ .color = .syntax_plain,
521
+ }};
522
+ const group_id: canvas.ObjectId = 77;
523
+ const children = [_]canvas.Widget{
524
+ .{
525
+ .id = 2,
526
+ .kind = .text,
527
+ .frame = geometry.RectF.init(12, 16, 220, 40),
528
+ .text = first,
529
+ .spans = &first_spans,
530
+ .static_text_group_id = group_id,
531
+ .static_text_group_offset = 0,
532
+ },
533
+ .{
534
+ .id = 3,
535
+ .kind = .text,
536
+ .frame = geometry.RectF.init(12, 64, 220, 40),
537
+ .text = second,
538
+ .spans = &second_spans,
539
+ .static_text_group_id = group_id,
540
+ .static_text_group_offset = first.len,
541
+ },
542
+ .{
543
+ .id = 4,
544
+ .kind = .button,
545
+ .frame = geometry.RectF.init(12, 128, 100, 32),
546
+ .text = "Clear",
547
+ },
548
+ };
549
+ var nodes: [4]canvas.WidgetLayoutNode = undefined;
550
+ const layout = try canvas.layoutWidgetTree(
551
+ .{ .kind = .stack, .children = &children },
552
+ geometry.RectF.init(0, 0, 320, 200),
553
+ &nodes,
554
+ );
555
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
556
+
557
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 13, 18));
558
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_drag, 231, 103));
559
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
560
+ try std.testing.expectEqualDeep(
561
+ canvas.TextSelection{ .anchor = 0, .focus = first.len },
562
+ retained.findById(2).?.widget.text_selection.?,
563
+ );
564
+ try std.testing.expectEqualDeep(
565
+ canvas.TextSelection{ .anchor = 0, .focus = second.len },
566
+ retained.findById(3).?.widget.text_selection.?,
567
+ );
568
+
569
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("c", cmd));
570
+ var clipboard_buffer: [32]u8 = undefined;
571
+ try std.testing.expectEqualStrings(
572
+ "alpha\r\nbeta",
573
+ try harness.runtime.readClipboard(&clipboard_buffer),
574
+ );
575
+
576
+ // Every selected chunk survives an unchanged rebuild, and pressing
577
+ // elsewhere clears the whole grouped highlight.
578
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
579
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
580
+ try std.testing.expect(retained.findById(2).?.widget.text_selection != null);
581
+ try std.testing.expect(retained.findById(3).?.widget.text_selection != null);
582
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 20, 140));
583
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
584
+ try std.testing.expect(retained.findById(2).?.widget.text_selection == null);
585
+ try std.testing.expect(retained.findById(3).?.widget.text_selection == null);
486
586
  }
487
587
 
488
588
  test "span paragraph drag selection copies the concatenated bytes" {
@@ -523,6 +623,52 @@ test "span paragraph drag selection copies the concatenated bytes" {
523
623
  try std.testing.expectEqualStrings(paragraph, copied);
524
624
  }
525
625
 
626
+ test "numbered code selection copies source without decorative gutter digits" {
627
+ var app_state: ClipboardTestApp = .{};
628
+ const app = app_state.app();
629
+ const harness = try createClipboardHarness(app);
630
+ defer harness.destroy(std.testing.allocator);
631
+
632
+ const source = "alpha\nbeta";
633
+ const spans = [_]canvas.TextSpan{.{
634
+ .text = source,
635
+ .monospace = true,
636
+ .color = .syntax_plain,
637
+ }};
638
+ const children = [_]canvas.Widget{.{
639
+ .id = 2,
640
+ .kind = .text,
641
+ .frame = geometry.RectF.init(12, 16, 220, 60),
642
+ .text = source,
643
+ .spans = &spans,
644
+ .code_line_number_digits = 1,
645
+ }};
646
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
647
+ const layout = try canvas.layoutWidgetTree(
648
+ .{ .kind = .stack, .children = &children },
649
+ geometry.RectF.init(0, 0, 320, 200),
650
+ &nodes,
651
+ );
652
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
653
+
654
+ // Start inside the source column (past the renderer-owned gutter) and
655
+ // drag beyond the final visual line.
656
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 34, 18));
657
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_drag, 231, 75));
658
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
659
+ try std.testing.expectEqualDeep(
660
+ canvas.TextSelection{ .anchor = 0, .focus = source.len },
661
+ retained.nodes[1].widget.text_selection.?,
662
+ );
663
+
664
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("c", cmd));
665
+ var clipboard_buffer: [32]u8 = undefined;
666
+ try std.testing.expectEqualStrings(
667
+ source,
668
+ try harness.runtime.readClipboard(&clipboard_buffer),
669
+ );
670
+ }
671
+
526
672
  test "focused editable selection wins copy over a stale static selection" {
527
673
  var app_state: ClipboardTestApp = .{};
528
674
  const app = app_state.app();
@@ -30,6 +30,7 @@ const geometry = @import("geometry");
30
30
  const canvas = @import("canvas");
31
31
  const platform = @import("../platform/root.zig");
32
32
  const runtime_api = @import("api.zig");
33
+ const canvas_limits = @import("canvas_limits.zig");
33
34
  const canvas_widget_runtime = @import("canvas_widget_runtime.zig");
34
35
  const runtime_canvas_widget_events = @import("canvas_widget_events.zig");
35
36
 
@@ -225,7 +226,15 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
225
226
 
226
227
  // 4. Static text with a live selection: Copy only.
227
228
  const selected_id = self.views[index].canvas_widget_selected_text_id;
228
- if (selected_id != 0 and selected_id == target.id) {
229
+ const selected_group_target = if (selected_id != 0 and selected_id != target.id) blk: {
230
+ const selected_index = self.views[index].canvasWidgetNodeIndexById(selected_id) orelse break :blk false;
231
+ const target_index = self.views[index].canvasWidgetNodeIndexById(target.id) orelse break :blk false;
232
+ const selected_widget = self.views[index].widget_layout_nodes[selected_index].widget;
233
+ const target_widget = self.views[index].widget_layout_nodes[target_index].widget;
234
+ break :blk selected_widget.static_text_group_id != 0 and
235
+ selected_widget.static_text_group_id == target_widget.static_text_group_id;
236
+ } else false;
237
+ if (selected_id != 0 and (selected_id == target.id or selected_group_target)) {
229
238
  if (!has_presenter) return;
230
239
  items[0] = .{ .id = default_item_copy, .label = "Copy" };
231
240
  _ = try showMenu(self, app, index, .{
@@ -389,7 +398,8 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
389
398
  .terminal => try applyDefaultTerminalAction(self, app, index, pending.target_id, event.item_id),
390
399
  .static_copy => {
391
400
  if (event.item_id != default_item_copy) return;
392
- const text = self.views[index].canvasWidgetCopyText() orelse return;
401
+ var group_buffer: [canvas_limits.max_canvas_widget_text_bytes_per_view]u8 = undefined;
402
+ const text = self.views[index].canvasWidgetCopyText(&group_buffer) orelse return;
393
403
  self.writeClipboard(text) catch return;
394
404
  },
395
405
  }
@@ -5,6 +5,7 @@ const platform = @import("../platform/root.zig");
5
5
  const validation = @import("validation.zig");
6
6
  const runtime_api = @import("api.zig");
7
7
  const runtime_view = @import("view.zig");
8
+ const view_widget_text = @import("view_widget_text.zig");
8
9
  const canvas_limits = @import("canvas_limits.zig");
9
10
  const canvas_frame_helpers = @import("canvas_frame.zig");
10
11
  const canvas_widget_runtime = @import("canvas_widget_runtime.zig");
@@ -41,6 +42,7 @@ pub fn RuntimeCanvasWidgetDisplay(comptime Runtime: type) type {
41
42
  const index = runtimeFindViewIndex(self, window_id, label) orelse return error.ViewNotFound;
42
43
  if (self.views[index].kind != .gpu_surface) return error.InvalidViewOptions;
43
44
  if (!std.meta.eql(self.views[index].widget_tokens, tokens)) {
45
+ self.views[index].clearCanvasWidgetTextVerticalGoal();
44
46
  self.views[index].widget_tokens = tokens;
45
47
  self.views[index].widget_revision += 1;
46
48
  }
@@ -213,6 +215,14 @@ pub fn RuntimeCanvasWidgetDisplay(comptime Runtime: type) type {
213
215
  const semantics = view.widgetSemantics();
214
216
  const count = @min(semantics.len, nodes.len);
215
217
  for (semantics[0..count], 0..) |node, index| {
218
+ const focused = node.state.focused or (view.keyboard_active and view.focused and node.id == view.canvas_widget_focused_id);
219
+ // Native menu validation only consults the focused editor.
220
+ // Resolving history for that one node also avoids searching
221
+ // the retained layout once per accessibility node.
222
+ const history = if (focused)
223
+ view.canvasWidgetTextHistoryAvailability(node.id)
224
+ else
225
+ view_widget_text.CanvasWidgetTextHistoryAvailability{};
216
226
  nodes[index] = .{
217
227
  .id = node.id,
218
228
  .parent_id = canvasWidgetSemanticParentId(semantics, node.parent_index),
@@ -234,7 +244,7 @@ pub fn RuntimeCanvasWidgetDisplay(comptime Runtime: type) type {
234
244
  .scroll_viewport_extent = if (node.scroll.present) node.scroll.viewport_extent else null,
235
245
  .scroll_content_extent = if (node.scroll.present) node.scroll.content_extent else null,
236
246
  .enabled = !node.state.disabled,
237
- .focused = node.state.focused or (view.keyboard_active and view.focused and node.id == view.canvas_widget_focused_id),
247
+ .focused = focused,
238
248
  .hovered = node.state.hovered or (node.id != 0 and node.id == view.canvas_widget_hovered_id),
239
249
  .pressed = node.state.pressed or (node.id != 0 and node.id == view.canvas_widget_pressed_id),
240
250
  .selected = canvasWidgetSelectedState(node),
@@ -242,6 +252,8 @@ pub fn RuntimeCanvasWidgetDisplay(comptime Runtime: type) type {
242
252
  .required = node.state.required,
243
253
  .read_only = node.state.read_only,
244
254
  .invalid = node.state.invalid,
255
+ .can_undo = history.can_undo,
256
+ .can_redo = history.can_redo,
245
257
  .focusable = node.focusable,
246
258
  .actions = platformWidgetAccessibilityActions(node.actions),
247
259
  };
@@ -594,6 +606,8 @@ fn hashWidgetAccessibilityNodes(nodes: []const platform.WidgetAccessibilityNode)
594
606
  hashAccessibilityValue(&hasher, node.required);
595
607
  hashAccessibilityValue(&hasher, node.read_only);
596
608
  hashAccessibilityValue(&hasher, node.invalid);
609
+ hashAccessibilityValue(&hasher, node.can_undo);
610
+ hashAccessibilityValue(&hasher, node.can_redo);
597
611
  hashAccessibilityValue(&hasher, node.focusable);
598
612
  inline for (comptime std.meta.fieldNames(platform.WidgetAccessibilityActions)) |field_name| {
599
613
  hashAccessibilityValue(&hasher, @field(node.actions, field_name));
@@ -103,6 +103,12 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
103
103
  }
104
104
  if (raw.kind != .text) return press_target;
105
105
  if (self.views[view_index].canvas_widget_selected_text_id != raw.id) return press_target;
106
+ if (self.views[view_index].canvas_widget_selected_text_group_id != 0 and
107
+ self.views[view_index].canvas_widget_selected_text_group_anchor !=
108
+ self.views[view_index].canvas_widget_selected_text_group_focus)
109
+ {
110
+ return null;
111
+ }
106
112
  const node = self.views[view_index].widgetLayoutTree().findById(raw.id) orelse return press_target;
107
113
  const selection = node.widget.text_selection orelse return press_target;
108
114
  if (selection.isCollapsed(node.widget.text.len)) return press_target;
@@ -1952,7 +1958,7 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
1952
1958
 
1953
1959
  switch (action) {
1954
1960
  .copy => {
1955
- const text = self.views[index].canvasWidgetCopyText() orelse return;
1961
+ const text = self.views[index].canvasWidgetCopyText(paste_buffer) orelse return;
1956
1962
  self.writeClipboard(text) catch return;
1957
1963
  },
1958
1964
  .cut => {
@@ -2020,7 +2026,7 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
2020
2026
  /// and clear-button stamping precedent, made the rule). Before
2021
2027
  /// the stamp, edits that only THIS derivation produced — Escape's
2022
2028
  /// search-field clear, Escape's composition cancel, the
2023
- /// single-line ArrowUp/Down caret jumps — mutated the editor
2029
+ /// runtime's geometry-aware ArrowUp/Down caret jumps — mutated the editor
2024
2030
  /// while the app-side dispatch re-derived the key on its own and
2025
2031
  /// heard nothing: the field visibly cleared while `model.query`
2026
2032
  /// kept the stale term, and the next keystroke dispatched
@@ -2031,27 +2037,50 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
2031
2037
  const index = runtimeFindViewIndex(self, keyboard_event.window_id, keyboard_event.view_label) orelse return;
2032
2038
  if (self.views[index].kind != .gpu_surface) return;
2033
2039
  const target = keyboard_event.target orelse return;
2034
- const derived = self.views[index].canvasWidgetKeyboardTextEdit(target, keyboard_event.keyboard) orelse return;
2035
- // Single-line sanitization happens HERE — after derivation,
2040
+ const history_shortcut = if (!keyboard_event.history_replay)
2041
+ self.views[index].canvasWidgetTextHistoryShortcut(target, keyboard_event.keyboard)
2042
+ else
2043
+ null;
2044
+ const derived = if (history_shortcut) |shortcut|
2045
+ shortcut.edit
2046
+ else
2047
+ self.views[index].canvasWidgetKeyboardTextEdit(target, keyboard_event.keyboard) orelse return;
2048
+ if (history_shortcut) |shortcut| {
2049
+ keyboard_event.history_replay = true;
2050
+ keyboard_event.history_replay_serial = shortcut.serial;
2051
+ keyboard_event.history_replay_redo = shortcut.redo;
2052
+ }
2053
+ // New single-line input is sanitized HERE — after derivation,
2036
2054
  // BEFORE the stamp — so the retained editor and the app's
2037
- // `on_input` mirror hear byte-identical sanitized inserts
2038
- // (clipboard paste from both entry points, typed and
2039
- // automation text_input, IME composition — every insertion
2040
- // source flows through this one seam). Pre-stamped pastes
2041
- // arrive already sanitized (`clampCanvasWidgetPasteText`
2042
- // strips BEFORE clamping so capacity never counts stripped
2043
- // bytes); re-sanitizing them is a no-op. A suppressed edit (an
2044
- // insert that was ONLY line breaks) also clears any raw
2045
- // pre-stamped paste so the app can never hear bytes the
2046
- // editor refused; the app-side fallback derivation applies
2047
- // the same sanitize rule, so both derivations still agree.
2048
- const edit = canvas.sanitizedSingleLineTextInputEvent(target.kind, derived) orelse {
2049
- keyboard_event.keyboard.edit = null;
2050
- return;
2051
- };
2055
+ // `on_input` mirror hear byte-identical sanitized inserts.
2056
+ // Clipboard paste, typed and automation text_input, and IME
2057
+ // composition all flow through this seam. History replay is
2058
+ // the deliberate exception below: its payload is retained
2059
+ // state rather than new input.
2060
+ // History payloads are retained bytes, not new user input:
2061
+ // replay them exactly so Undo can restore a model-provided
2062
+ // single-line value that contains raw line breaks. Every
2063
+ // non-history insertion still crosses the sanitizer below.
2064
+ const edit = if (keyboard_event.history_replay)
2065
+ derived
2066
+ else
2067
+ canvas.sanitizedSingleLineTextInputEvent(target.kind, derived) orelse {
2068
+ keyboard_event.keyboard.edit = null;
2069
+ return;
2070
+ };
2052
2071
  keyboard_event.keyboard.edit = edit;
2053
2072
 
2054
- const dirty = try self.views[index].applyCanvasWidgetTextEdit(target.id, edit) orelse return;
2073
+ const dirty = if (keyboard_event.history_replay)
2074
+ try self.views[index].applyCanvasWidgetTextEditWithoutHistory(target.id, edit) orelse return
2075
+ else
2076
+ try self.views[index].applyCanvasWidgetTextEdit(target.id, edit) orelse return;
2077
+ if (keyboard_event.history_replay) {
2078
+ self.views[index].commitCanvasWidgetTextHistoryReplayIfComplete(
2079
+ target,
2080
+ keyboard_event.history_replay_serial,
2081
+ keyboard_event.history_replay_redo,
2082
+ );
2083
+ }
2055
2084
  if (canvasDirtyRegionForView(self.views[index].frame, dirty)) |dirty_region| {
2056
2085
  self.invalidateFor(.state, dirty_region);
2057
2086
  } else {
@@ -2102,6 +2131,7 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
2102
2131
  target_id,
2103
2132
  pointer_event.pointer.point,
2104
2133
  pointer_event.pointer.phase == .move,
2134
+ pointer_event.pointer.phase == .down and pointer_event.pointer.modifiers.shift,
2105
2135
  pointer_event.pointer.click_count,
2106
2136
  );
2107
2137
  // Pointer placement lives first in the retained editor, but a
@@ -149,8 +149,15 @@ pub const CanvasWidgetTextReconcileEntry = struct {
149
149
  id: canvas.ObjectId = 0,
150
150
  kind: canvas.WidgetKind = .text_field,
151
151
  text: []const u8 = &.{},
152
+ static_text_group_id: canvas.ObjectId = 0,
152
153
  source_text_len: usize = 0,
153
154
  source_text_hash: u64 = 0,
155
+ /// The selection declared by the previous SOURCE tree, distinct from
156
+ /// the runtime-owned selection in `text_selection`. Legacy controlled
157
+ /// models always materialize the default `.upstream` affinity, so an
158
+ /// unchanged source affinity is an echo; a changed affinity is an
159
+ /// explicit source-side visual-caret move.
160
+ source_text_selection: ?canvas.TextSelection = null,
154
161
  text_selection: ?canvas.TextSelection = null,
155
162
  text_composition: ?canvas.TextRange = null,
156
163
  value: f32 = 0,
@@ -246,6 +253,7 @@ pub const CanvasWidgetSourceTextEntry = struct {
246
253
  kind: canvas.WidgetKind = .text_field,
247
254
  text_len: usize = 0,
248
255
  text_hash: u64 = 0,
256
+ text_selection: ?canvas.TextSelection = null,
249
257
  };
250
258
 
251
259
  /// The SOURCE-side selected state (and value, for sliders) of one
@@ -588,13 +596,19 @@ pub fn collectCanvasWidgetTextReconcileEntries(
588
596
  if (node.widget.id == 0 or !canvasWidgetTextReconcileKind(node.widget)) continue;
589
597
  if (len >= output.len) break;
590
598
  const text_range = try appendWidgetTextStorageRange(text_storage, text_len, node.widget.text);
591
- const source_text = canvasWidgetSourceTextByIdKind(source_entries, node.widget.id, node.widget.kind) orelse canvasWidgetSourceTextFingerprint(node.widget.text);
599
+ const source_entry = canvasWidgetSourceTextEntryByIdKind(source_entries, node.widget.id, node.widget.kind);
600
+ const source_text = if (source_entry) |entry|
601
+ CanvasWidgetSourceTextFingerprint{ .len = entry.text_len, .hash = entry.text_hash }
602
+ else
603
+ canvasWidgetSourceTextFingerprint(node.widget.text);
592
604
  output[len] = .{
593
605
  .id = node.widget.id,
594
606
  .kind = node.widget.kind,
595
607
  .text = text_storage[text_range.start..text_range.end],
608
+ .static_text_group_id = node.widget.static_text_group_id,
596
609
  .source_text_len = source_text.len,
597
610
  .source_text_hash = source_text.hash,
611
+ .source_text_selection = if (source_entry) |entry| entry.text_selection else null,
598
612
  .text_selection = node.widget.text_selection,
599
613
  .text_composition = node.widget.text_composition,
600
614
  .value = node.widget.value,
@@ -632,12 +646,21 @@ pub fn canvasWidgetSourceTextByIdKind(
632
646
  id: canvas.ObjectId,
633
647
  kind: canvas.WidgetKind,
634
648
  ) ?CanvasWidgetSourceTextFingerprint {
649
+ const entry = canvasWidgetSourceTextEntryByIdKind(entries, id, kind) orelse return null;
650
+ return .{
651
+ .len = entry.text_len,
652
+ .hash = entry.text_hash,
653
+ };
654
+ }
655
+
656
+ fn canvasWidgetSourceTextEntryByIdKind(
657
+ entries: []const CanvasWidgetSourceTextEntry,
658
+ id: canvas.ObjectId,
659
+ kind: canvas.WidgetKind,
660
+ ) ?CanvasWidgetSourceTextEntry {
635
661
  for (entries) |entry| {
636
662
  if (entry.id != id or entry.kind != kind) continue;
637
- return .{
638
- .len = entry.text_len,
639
- .hash = entry.text_hash,
640
- };
663
+ return entry;
641
664
  }
642
665
  return null;
643
666
  }
@@ -755,18 +778,23 @@ pub fn canvasWidgetLayoutNodeWithTextReconcileState(
755
778
  previous: *const CanvasWidgetTextEntryIndex,
756
779
  ) canvas.WidgetLayoutNode {
757
780
  var copy = node;
758
- if (copy.widget.id == 0) return copy;
759
- if (copy.widget.state.disabled or canvasWidgetLayoutNodeHidden(layout, node_index)) return copy;
781
+ if (copy.widget.id == 0) return canvasWidgetLayoutNodeWithCaretSafeSelection(copy);
782
+ if (copy.widget.state.disabled or canvasWidgetLayoutNodeHidden(layout, node_index)) {
783
+ return canvasWidgetLayoutNodeWithCaretSafeSelection(copy);
784
+ }
760
785
 
761
786
  if (copy.widget.kind == .text) {
762
787
  // Static text selections survive rebuilds only while the source
763
- // text is byte-identical; changed text drops the selection.
788
+ // text and its optional grouped document are byte-identical.
764
789
  if (previous.firstWithKind(copy.widget.id, copy.widget.kind)) |entry| {
765
- if (copy.widget.text_selection == null and std.mem.eql(u8, entry.text, copy.widget.text)) {
790
+ if (copy.widget.text_selection == null and
791
+ std.mem.eql(u8, entry.text, copy.widget.text) and
792
+ entry.static_text_group_id == copy.widget.static_text_group_id)
793
+ {
766
794
  copy.widget.text_selection = entry.text_selection;
767
795
  }
768
796
  }
769
- return copy;
797
+ return canvasWidgetLayoutNodeWithCaretSafeSelection(copy);
770
798
  }
771
799
  if (!canvasWidgetEditableTextKind(copy.widget.kind)) return copy;
772
800
 
@@ -780,7 +808,9 @@ pub fn canvasWidgetLayoutNodeWithTextReconcileState(
780
808
  const next_source_text = canvasWidgetSourceTextFingerprint(copy.widget.text);
781
809
  const source_unchanged = entry.source_text_len == next_source_text.len and entry.source_text_hash == next_source_text.hash;
782
810
  const source_matches_runtime_text = std.mem.eql(u8, entry.text, copy.widget.text);
783
- if (!source_unchanged and !source_matches_runtime_text) return copy;
811
+ if (!source_unchanged and !source_matches_runtime_text) {
812
+ return canvasWidgetLayoutNodeWithCaretSafeSelection(copy);
813
+ }
784
814
  if (source_unchanged) copy.widget.text = entry.text;
785
815
  // The retained scroll offset rides `value` for every editable
786
816
  // text kind: the textarea's vertical offset and the single-line
@@ -792,8 +822,40 @@ pub fn canvasWidgetLayoutNodeWithTextReconcileState(
792
822
  if (copy.widget.text_selection == null and copy.widget.text_composition == null) {
793
823
  copy.widget.text_selection = entry.text_selection;
794
824
  copy.widget.text_composition = entry.text_composition;
825
+ } else if (copy.widget.text_selection) |*source_selection| {
826
+ // Caret affinity is runtime geometry at a byte offset. Core
827
+ // and C-ABI controlled models historically echo only
828
+ // anchor/focus, so the bridge materializes `.upstream` on
829
+ // every rebuild. When those offsets faithfully echo the
830
+ // retained selection, keep the retained visual-line owner;
831
+ // changing either offset remains the source-authoritative
832
+ // way to move the selection.
833
+ if (entry.text_selection) |retained_selection| {
834
+ if (source_selection.anchor == retained_selection.anchor and
835
+ source_selection.focus == retained_selection.focus)
836
+ {
837
+ const source_affinity_changed = if (entry.source_text_selection) |previous_source_selection|
838
+ source_selection.affinity != previous_source_selection.affinity
839
+ else
840
+ // The legacy shape can only materialize upstream;
841
+ // a first downstream value is therefore explicit.
842
+ source_selection.affinity != .upstream;
843
+ if (!source_affinity_changed) {
844
+ source_selection.affinity = retained_selection.affinity;
845
+ }
846
+ }
847
+ }
795
848
  }
796
849
  }
850
+ return canvasWidgetLayoutNodeWithCaretSafeSelection(copy);
851
+ }
852
+
853
+ fn canvasWidgetLayoutNodeWithCaretSafeSelection(node: canvas.WidgetLayoutNode) canvas.WidgetLayoutNode {
854
+ var copy = node;
855
+ if (!canvasWidgetEditableTextKind(copy.widget.kind)) return copy;
856
+ if (copy.widget.text_selection) |selection| {
857
+ copy.widget.text_selection = canvas.snapTextCaretSelection(copy.widget.text, selection);
858
+ }
797
859
  return copy;
798
860
  }
799
861
 
@@ -1174,7 +1236,7 @@ pub fn canvasWidgetTextEditUnchanged(previous: canvas.TextEditState, next: canva
1174
1236
  }
1175
1237
 
1176
1238
  pub fn canvasTextSelectionsEqual(a: canvas.TextSelection, b: canvas.TextSelection) bool {
1177
- return a.anchor == b.anchor and a.focus == b.focus;
1239
+ return a.anchor == b.anchor and a.focus == b.focus and a.affinity == b.affinity;
1178
1240
  }
1179
1241
 
1180
1242
  pub fn textSelectionCollapsedAt(selection: ?canvas.TextSelection, offset: usize) bool {