@native-sdk/cli 0.6.2 → 0.6.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 (36) 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/src/platform/macos/appkit_host.h +2 -0
  7. package/src/platform/macos/appkit_host.m +36 -10
  8. package/src/platform/macos/root.zig +7 -1
  9. package/src/platform/types.zig +2 -0
  10. package/src/primitives/canvas/equality.zig +3 -1
  11. package/src/primitives/canvas/events.zig +4 -0
  12. package/src/primitives/canvas/root.zig +11 -0
  13. package/src/primitives/canvas/text.zig +10 -0
  14. package/src/primitives/canvas/text_interaction.zig +164 -22
  15. package/src/primitives/canvas/text_layout.zig +107 -21
  16. package/src/primitives/canvas/text_tests.zig +86 -0
  17. package/src/primitives/canvas/ui.zig +21 -16
  18. package/src/primitives/canvas/ui_markup_reflect.zig +20 -3
  19. package/src/primitives/canvas/ui_markup_view_tests.zig +16 -0
  20. package/src/primitives/canvas/ui_tests.zig +6 -0
  21. package/src/primitives/canvas/widget_runtime.zig +1 -0
  22. package/src/primitives/canvas/widget_text_input.zig +23 -11
  23. package/src/runtime/api.zig +9 -0
  24. package/src/runtime/canvas_frame_helpers.zig +1 -0
  25. package/src/runtime/canvas_limits.zig +7 -0
  26. package/src/runtime/canvas_widget_accessibility_tests.zig +27 -1
  27. package/src/runtime/canvas_widget_clipboard_tests.zig +16 -0
  28. package/src/runtime/canvas_widget_display.zig +15 -1
  29. package/src/runtime/canvas_widget_events.zig +43 -19
  30. package/src/runtime/canvas_widget_runtime.zig +67 -10
  31. package/src/runtime/canvas_widget_state.zig +1 -0
  32. package/src/runtime/canvas_widget_text_tests.zig +1878 -70
  33. package/src/runtime/gpu_surface_events.zig +38 -0
  34. package/src/runtime/view.zig +30 -0
  35. package/src/runtime/view_widget_text.zig +1048 -10
  36. package/src/runtime/view_widget_tree.zig +2 -0
@@ -1,4 +1,5 @@
1
1
  const support = @import("test_support.zig");
2
+ const builtin = @import("builtin");
2
3
  const std = support.std;
3
4
  const geometry = support.geometry;
4
5
  const trace = support.trace;
@@ -431,101 +432,1754 @@ test "runtime applies text input to canvas textareas" {
431
432
  const newline_geometry = try harness.runtime.canvasWidgetTextGeometry(1, "canvas", 2);
432
433
  try std.testing.expect(newline_geometry.caret_bounds.?.y > textarea.frame.y + 24);
433
434
 
435
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
436
+ .window_id = 1,
437
+ .label = "canvas",
438
+ .kind = .key_down,
439
+ .key = "space",
440
+ .text = " ",
441
+ } });
442
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
443
+ try std.testing.expectEqualStrings("First!\n ", retained.nodes[1].widget.text);
444
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(8), retained.nodes[1].widget.text_selection.?);
445
+ const space_geometry = try harness.runtime.canvasWidgetTextGeometry(1, "canvas", 2);
446
+ try std.testing.expectEqual(newline_geometry.caret_bounds.?.y, space_geometry.caret_bounds.?.y);
447
+ try std.testing.expect(space_geometry.caret_bounds.?.x > newline_geometry.caret_bounds.?.x);
448
+
449
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
450
+ .window_id = 1,
451
+ .label = "canvas",
452
+ .kind = .key_down,
453
+ .key = "backspace",
454
+ } });
455
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
456
+ try std.testing.expectEqualStrings("First!\n", retained.nodes[1].widget.text);
457
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(7), retained.nodes[1].widget.text_selection.?);
458
+
459
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .insert_text = "Second" });
460
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
461
+ try std.testing.expectEqualStrings("First!\nSecond", retained.nodes[1].widget.text);
462
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(13), retained.nodes[1].widget.text_selection.?);
463
+
464
+ if (comptime builtin.os.tag == .macos) {
465
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
466
+ .window_id = 1,
467
+ .label = "canvas",
468
+ .kind = .key_down,
469
+ .key = "arrowleft",
470
+ .modifiers = .{ .command = true, .shift = true },
471
+ } });
472
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
473
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 13, .focus = 7 }, retained.nodes[1].widget.text_selection.?);
474
+
475
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
476
+ .window_id = 1,
477
+ .label = "canvas",
478
+ .kind = .key_down,
479
+ .key = "arrowright",
480
+ .modifiers = .{ .command = true },
481
+ } });
482
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
483
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(13), retained.nodes[1].widget.text_selection.?);
484
+
485
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
486
+ .window_id = 1,
487
+ .label = "canvas",
488
+ .kind = .key_down,
489
+ .key = "arrowleft",
490
+ .modifiers = .{ .command = true },
491
+ } });
492
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
493
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(7), retained.nodes[1].widget.text_selection.?);
494
+
495
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
496
+ .window_id = 1,
497
+ .label = "canvas",
498
+ .kind = .key_down,
499
+ .key = "arrowup",
500
+ .modifiers = .{ .command = true },
501
+ } });
502
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
503
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(0), retained.nodes[1].widget.text_selection.?);
504
+
505
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
506
+ .window_id = 1,
507
+ .label = "canvas",
508
+ .kind = .key_down,
509
+ .key = "arrowdown",
510
+ .modifiers = .{ .command = true },
511
+ } });
512
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
513
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(13), retained.nodes[1].widget.text_selection.?);
514
+ }
515
+
516
+ // On Ctrl-primary hosts the runtime folds Primary into `super`, so
517
+ // Ctrl arrives with BOTH bits set. It must not enter the macOS-only
518
+ // Command+Up document-boundary mapping.
519
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
520
+ .window_id = 1,
521
+ .label = "canvas",
522
+ .kind = .key_down,
523
+ .key = "arrowup",
524
+ .modifiers = .{ .primary = true, .control = true },
525
+ } });
526
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
527
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(13), retained.nodes[1].widget.text_selection.?);
528
+
529
+ const textarea_revision = harness.runtime.views[0].widget_revision;
530
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
531
+ .window_id = 1,
532
+ .label = "canvas",
533
+ .kind = .key_down,
534
+ .key = "arrowup",
535
+ } });
536
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
537
+ try std.testing.expectEqual(textarea_revision + 1, harness.runtime.views[0].widget_revision);
538
+ try std.testing.expectEqualStrings("First!\nSecond", retained.nodes[1].widget.text);
539
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(6), retained.nodes[1].widget.text_selection.?);
540
+ try std.testing.expectEqualDeep(canvas.TextRange.init(6, 6), runtimeViewWidgetSemantics(&harness.runtime.views[0])[0].text_selection.?);
541
+
542
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
543
+ .window_id = 1,
544
+ .label = "canvas",
545
+ .kind = .key_down,
546
+ .key = "arrowdown",
547
+ } });
548
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
549
+ try std.testing.expectEqual(textarea_revision + 2, harness.runtime.views[0].widget_revision);
550
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(13), retained.nodes[1].widget.text_selection.?);
551
+
552
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
553
+ .window_id = 1,
554
+ .label = "canvas",
555
+ .kind = .key_down,
556
+ .key = "arrowup",
557
+ .modifiers = .{ .shift = true },
558
+ } });
559
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
560
+ try std.testing.expectEqual(textarea_revision + 3, harness.runtime.views[0].widget_revision);
561
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 13, .focus = 6 }, retained.nodes[1].widget.text_selection.?);
562
+
563
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
564
+ .window_id = 1,
565
+ .label = "canvas",
566
+ .kind = .key_down,
567
+ .key = "arrowdown",
568
+ .modifiers = .{ .shift = true },
569
+ } });
570
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
571
+ try std.testing.expectEqual(textarea_revision + 4, harness.runtime.views[0].widget_revision);
572
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(13), retained.nodes[1].widget.text_selection.?);
573
+
574
+ const text_geometry = try harness.runtime.canvasWidgetTextGeometry(1, "canvas", 2);
575
+ try std.testing.expect(text_geometry.caret_bounds != null);
576
+ const snapshot = harness.runtime.automationSnapshot("Widgets");
577
+ try std.testing.expectEqualStrings("Message", snapshot.widgets[0].name);
578
+ try std.testing.expectEqualStrings("First!\nSecond", snapshot.widgets[0].text_value);
579
+ try std.testing.expect(snapshot.widgets[0].actions.set_text);
580
+ try std.testing.expect(snapshot.widgets[0].actions.set_selection);
581
+
582
+ _ = try harness.runtime.emitCanvasWidgetDisplayList(1, "canvas", .{});
583
+ const display_list = try harness.runtime.canvasDisplayList(1, "canvas");
584
+ var saw_textarea_text = false;
585
+ for (display_list.commands) |command| {
586
+ switch (command) {
587
+ .draw_text => |text| {
588
+ if (text.id == testCanvasWidgetPartId(2, 4)) {
589
+ try std.testing.expectEqualStrings("First!\nSecond", text.text);
590
+ try std.testing.expect(text.text_layout != null);
591
+ try std.testing.expectEqual(canvas.TextWrap.word, text.text_layout.?.wrap);
592
+ saw_textarea_text = true;
593
+ }
594
+ },
595
+ else => {},
596
+ }
597
+ }
598
+ try std.testing.expect(saw_textarea_text);
599
+
600
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .insert_text = "\nThird\nFourth\nFifth\nSixth\nSeventh\nEighth" });
601
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
602
+ try std.testing.expect(retained.nodes[1].widget.value > 0);
603
+ try std.testing.expect(canvas.textInputMaxScrollOffsetForWidget(retained.nodes[1].widget, .{}) > 0);
604
+ const scrolled_viewport = canvas.textInputViewportForWidget(retained.nodes[1].widget, .{}).?;
605
+ const scrolled_geometry = try harness.runtime.canvasWidgetTextGeometry(1, "canvas", 2);
606
+ const scrolled_caret = scrolled_geometry.caret_bounds.?;
607
+ try std.testing.expect(scrolled_caret.y >= scrolled_viewport.y - 0.001);
608
+ try std.testing.expect(scrolled_caret.maxY() <= scrolled_viewport.maxY() + 0.001);
609
+
610
+ _ = try harness.runtime.emitCanvasWidgetDisplayList(1, "canvas", .{});
611
+ const scrolled_display_list = try harness.runtime.canvasDisplayList(1, "canvas");
612
+ var saw_textarea_clip = false;
613
+ for (scrolled_display_list.commands) |command| {
614
+ switch (command) {
615
+ .push_clip => |clip| {
616
+ if (clip.id == testCanvasWidgetPartId(2, 16)) {
617
+ try std.testing.expectEqualDeep(scrolled_viewport, clip.rect);
618
+ saw_textarea_clip = true;
619
+ }
620
+ },
621
+ else => {},
622
+ }
623
+ }
624
+ try std.testing.expect(saw_textarea_clip);
625
+ }
626
+
627
+ test "textarea pointer and direct selection cannot split CRLF" {
628
+ const TestApp = struct {
629
+ fn app(self: *@This()) App {
630
+ return .{ .context = self, .name = "gpu-widget-textarea-crlf-pointer", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
631
+ }
632
+ };
633
+
634
+ const harness = try TestHarness().create(std.testing.allocator, .{});
635
+ defer harness.destroy(std.testing.allocator);
636
+ harness.null_platform.gpu_surfaces = true;
637
+ var app_state: TestApp = .{};
638
+ const app = app_state.app();
639
+ try harness.start(app);
640
+
641
+ _ = try harness.runtime.createView(.{
642
+ .window_id = 1,
643
+ .label = "canvas",
644
+ .kind = .gpu_surface,
645
+ .frame = geometry.RectF.init(0, 0, 260, 160),
646
+ });
647
+
648
+ const textarea = canvas.Widget{
649
+ .id = 2,
650
+ .kind = .textarea,
651
+ .frame = geometry.RectF.init(12, 16, 180, 84),
652
+ .text = "one\r\ntwo",
653
+ // Exercise reconciliation from an externally supplied selection
654
+ // inside the two-byte delimiter too.
655
+ .text_selection = canvas.TextSelection.collapsed(4),
656
+ };
657
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
658
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{textarea} }, geometry.RectF.init(0, 0, 260, 160), &nodes);
659
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
660
+
661
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
662
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(3), retained.nodes[1].widget.text_selection.?);
663
+ try std.testing.expectEqualDeep(
664
+ canvas.TextRange.init(3, 3),
665
+ runtimeViewWidgetSemantics(&harness.runtime.views[0])[0].text_selection.?,
666
+ );
667
+
668
+ const field = retained.nodes[1].widget;
669
+ const caret = canvas.textGeometryForWidget(field, harness.runtime.views[0].widget_tokens).caret_bounds.?;
670
+ const line_end = geometry.PointF.init(field.frame.maxX() - 1, caret.y + caret.height * 0.5);
671
+ try std.testing.expectEqual(
672
+ @as(usize, 3),
673
+ canvas.textOffsetForWidgetPoint(field, line_end, harness.runtime.views[0].widget_tokens).?,
674
+ );
675
+
676
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{
677
+ .set_selection = canvas.TextSelection.collapsed(4),
678
+ });
679
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
680
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(3), retained.nodes[1].widget.text_selection.?);
681
+
682
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
683
+ .window_id = 1,
684
+ .label = "canvas",
685
+ .kind = .pointer_down,
686
+ .x = line_end.x,
687
+ .y = line_end.y,
688
+ } });
689
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(3), try retainedTextSelection(harness, 1));
690
+
691
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
692
+ .window_id = 1,
693
+ .label = "canvas",
694
+ .kind = .key_down,
695
+ .key = "x",
696
+ .text = "X",
697
+ } });
698
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
699
+ try std.testing.expectEqualStrings("oneX\r\ntwo", retained.nodes[1].widget.text);
700
+ }
701
+
702
+ test "textarea vertical navigation retains its preferred column across short lines" {
703
+ const TestApp = struct {
704
+ fn app(self: *@This()) App {
705
+ return .{ .context = self, .name = "gpu-widget-textarea-vertical-goal", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
706
+ }
707
+ };
708
+
709
+ const harness = try TestHarness().create(std.testing.allocator, .{});
710
+ defer harness.destroy(std.testing.allocator);
711
+ harness.null_platform.gpu_surfaces = true;
712
+ var app_state: TestApp = .{};
713
+ const app = app_state.app();
714
+ try harness.start(app);
715
+
716
+ _ = try harness.runtime.createView(.{
717
+ .window_id = 1,
718
+ .label = "canvas",
719
+ .kind = .gpu_surface,
720
+ .frame = geometry.RectF.init(0, 0, 320, 180),
721
+ });
722
+
723
+ const textarea = canvas.Widget{
724
+ .id = 2,
725
+ .kind = .textarea,
726
+ .frame = geometry.RectF.init(12, 16, 260, 120),
727
+ .text = "0123456789\nx\n0123456789",
728
+ .text_selection = canvas.TextSelection.collapsed(8),
729
+ };
730
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
731
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{textarea} }, geometry.RectF.init(0, 0, 320, 180), &nodes);
732
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
733
+ harness.runtime.views[0].focused = true;
734
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
735
+
736
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
737
+ .window_id = 1,
738
+ .label = "canvas",
739
+ .kind = .key_down,
740
+ .key = "arrowdown",
741
+ } });
742
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
743
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(12), retained.nodes[1].widget.text_selection.?);
744
+
745
+ // A real discrete key press includes its release. The release must not
746
+ // end the vertical run or the next press would inherit the short line's
747
+ // column instead of the original preferred column.
748
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
749
+ .window_id = 1,
750
+ .label = "canvas",
751
+ .kind = .key_up,
752
+ .key = "arrowdown",
753
+ } });
754
+
755
+ // The preferred x is widget-local: moving the textarea between discrete
756
+ // presses must retain the original long-line column instead of hit-testing
757
+ // the next line at the old screen coordinate.
758
+ var moved_textarea = textarea;
759
+ moved_textarea.frame.x = 32;
760
+ moved_textarea.text_selection = canvas.TextSelection.collapsed(12);
761
+ var moved_nodes: [2]canvas.WidgetLayoutNode = undefined;
762
+ const moved_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{moved_textarea} }, geometry.RectF.init(0, 0, 320, 180), &moved_nodes);
763
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", moved_layout);
764
+
765
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
766
+ .window_id = 1,
767
+ .label = "canvas",
768
+ .kind = .key_down,
769
+ .key = "arrowdown",
770
+ } });
771
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
772
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(21), retained.nodes[1].widget.text_selection.?);
773
+
774
+ // A horizontal move ends the vertical run. The next Up/Down pair
775
+ // begins with the new column instead of reviving the old goal.
434
776
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
435
777
  .window_id = 1,
436
778
  .label = "canvas",
437
779
  .kind = .key_down,
438
780
  .key = "arrowleft",
439
- .modifiers = .{ .command = true },
781
+ } });
782
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
783
+ .window_id = 1,
784
+ .label = "canvas",
785
+ .kind = .key_down,
786
+ .key = "arrowup",
787
+ } });
788
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
789
+ .window_id = 1,
790
+ .label = "canvas",
791
+ .kind = .key_down,
792
+ .key = "arrowdown",
440
793
  } });
441
794
  retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
442
- try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(0), retained.nodes[1].widget.text_selection.?);
795
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(20), retained.nodes[1].widget.text_selection.?);
796
+
797
+ // Typography changes alter caret geometry even when frame/text/focus do
798
+ // not, so they terminate the standing preferred-column run.
799
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_text_vertical_goal_id);
800
+ _ = try harness.runtime.setCanvasWidgetDesignTokens(1, "canvas", .{ .typography = .{ .body_size = 18 } });
801
+ try std.testing.expectEqual(@as(canvas.ObjectId, 0), harness.runtime.views[0].canvas_widget_text_vertical_goal_id);
802
+
803
+ // A vertical hit at the painted end of a CRLF line lands on LF in
804
+ // layout coordinates. Normalize it before CR so typing cannot split
805
+ // the two-byte line ending.
806
+ var crlf_textarea = textarea;
807
+ crlf_textarea.text = "one\r\n0123456789";
808
+ crlf_textarea.text_selection = canvas.TextSelection.collapsed(13);
809
+ var crlf_nodes: [2]canvas.WidgetLayoutNode = undefined;
810
+ const crlf_layout = try canvas.layoutWidgetTree(
811
+ .{ .kind = .stack, .children = &.{crlf_textarea} },
812
+ geometry.RectF.init(0, 0, 320, 180),
813
+ &crlf_nodes,
814
+ );
815
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", crlf_layout);
816
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
817
+ .window_id = 1,
818
+ .label = "canvas",
819
+ .kind = .key_down,
820
+ .key = "arrowup",
821
+ } });
822
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
823
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(3), retained.nodes[1].widget.text_selection.?);
824
+
825
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
826
+ .window_id = 1,
827
+ .label = "canvas",
828
+ .kind = .key_down,
829
+ .key = "x",
830
+ .text = "X",
831
+ } });
832
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
833
+ try std.testing.expectEqualStrings("oneX\r\n0123456789", retained.nodes[1].widget.text);
834
+ }
835
+
836
+ test "textarea unshifted vertical navigation starts at the selection's leading edge" {
837
+ const TestApp = struct {
838
+ fn app(self: *@This()) App {
839
+ return .{ .context = self, .name = "gpu-widget-textarea-selection-vertical", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
840
+ }
841
+ };
842
+
843
+ const harness = try TestHarness().create(std.testing.allocator, .{});
844
+ defer harness.destroy(std.testing.allocator);
845
+ harness.null_platform.gpu_surfaces = true;
846
+ var app_state: TestApp = .{};
847
+ const app = app_state.app();
848
+ try harness.start(app);
849
+
850
+ _ = try harness.runtime.createView(.{
851
+ .window_id = 1,
852
+ .label = "canvas",
853
+ .kind = .gpu_surface,
854
+ .frame = geometry.RectF.init(0, 0, 260, 160),
855
+ });
856
+
857
+ const textarea = canvas.Widget{
858
+ .id = 2,
859
+ .kind = .textarea,
860
+ .frame = geometry.RectF.init(12, 16, 180, 100),
861
+ .text = "012345\n678901\n234567",
862
+ .text_selection = .{ .anchor = 2, .focus = 4 },
863
+ };
864
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
865
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{textarea} }, geometry.RectF.init(0, 0, 260, 160), &nodes);
866
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
867
+ harness.runtime.views[0].focused = true;
868
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
869
+
870
+ // NSTextView collapses a non-empty selection to its normalized start
871
+ // before preserving that edge's painted column for Up or Down.
872
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
873
+ .window_id = 1,
874
+ .label = "canvas",
875
+ .kind = .key_down,
876
+ .key = "arrowdown",
877
+ } });
878
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
879
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(9), retained.nodes[1].widget.text_selection.?);
880
+
881
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{
882
+ .set_selection = .{ .anchor = 9, .focus = 11 },
883
+ });
884
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
885
+ .window_id = 1,
886
+ .label = "canvas",
887
+ .kind = .key_down,
888
+ .key = "arrowup",
889
+ } });
890
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
891
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(2), retained.nodes[1].widget.text_selection.?);
892
+ }
893
+
894
+ test "textarea Command Left and Right stop at painted soft-wrap boundaries" {
895
+ if (comptime builtin.os.tag != .macos) return error.SkipZigTest;
896
+
897
+ const TestApp = struct {
898
+ fn app(self: *@This()) App {
899
+ return .{ .context = self, .name = "gpu-widget-textarea-command-visual-line", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
900
+ }
901
+ };
902
+
903
+ const harness = try TestHarness().create(std.testing.allocator, .{});
904
+ defer harness.destroy(std.testing.allocator);
905
+ harness.null_platform.gpu_surfaces = true;
906
+ var app_state: TestApp = .{};
907
+ const app = app_state.app();
908
+ try harness.start(app);
909
+
910
+ _ = try harness.runtime.createView(.{
911
+ .window_id = 1,
912
+ .label = "canvas",
913
+ .kind = .gpu_surface,
914
+ .frame = geometry.RectF.init(0, 0, 240, 180),
915
+ });
916
+
917
+ const text = "alpha beta gamma delta epsilon zeta";
918
+ const textarea = canvas.Widget{
919
+ .id = 2,
920
+ .kind = .textarea,
921
+ .frame = geometry.RectF.init(12, 16, 92, 140),
922
+ .text = text,
923
+ .text_selection = canvas.TextSelection.collapsed(2),
924
+ };
925
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
926
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{textarea} }, geometry.RectF.init(0, 0, 240, 180), &nodes);
927
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
928
+ harness.runtime.views[0].focused = true;
929
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
930
+
931
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
932
+ var field = retained.nodes[1].widget;
933
+ var caret = canvas.textGeometryForWidget(field, harness.runtime.views[0].widget_tokens).caret_bounds.?;
934
+ const expected_right = canvas.textOffsetForWidgetPoint(
935
+ field,
936
+ geometry.PointF.init(field.frame.maxX() + 1, caret.y + caret.height * 0.5),
937
+ harness.runtime.views[0].widget_tokens,
938
+ ).?;
939
+ try std.testing.expect(expected_right > field.text_selection.?.focus);
940
+ try std.testing.expect(expected_right < text.len);
941
+
942
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
943
+ .window_id = 1,
944
+ .label = "canvas",
945
+ .kind = .key_down,
946
+ .key = "arrowright",
947
+ .modifiers = .{ .command = true },
948
+ } });
949
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
950
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(expected_right), retained.nodes[1].widget.text_selection.?);
951
+
952
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .set_selection = canvas.TextSelection.collapsed(text.len - 2) });
953
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
954
+ field = retained.nodes[1].widget;
955
+ caret = canvas.textGeometryForWidget(field, harness.runtime.views[0].widget_tokens).caret_bounds.?;
956
+ const expected_left = canvas.textOffsetForWidgetPoint(
957
+ field,
958
+ geometry.PointF.init(field.frame.x - 1, caret.y + caret.height * 0.5),
959
+ harness.runtime.views[0].widget_tokens,
960
+ ).?;
961
+ try std.testing.expect(expected_left > 0);
962
+ try std.testing.expect(expected_left < field.text_selection.?.focus);
963
+
964
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
965
+ .window_id = 1,
966
+ .label = "canvas",
967
+ .kind = .key_down,
968
+ .key = "arrowleft",
969
+ .modifiers = .{ .command = true },
970
+ } });
971
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
972
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(expected_left), retained.nodes[1].widget.text_selection.?);
973
+
974
+ // Without Shift, Cocoa first collapses a non-empty selection toward
975
+ // the command's direction, then finds that edge's visual line boundary.
976
+ // The active focus may be on a different line and in either orientation.
977
+ var selected_textarea = textarea;
978
+ selected_textarea.frame.width = 180;
979
+ selected_textarea.text = "abcdef\nuvwxyz\nmnopqr";
980
+ selected_textarea.text_selection = .{ .anchor = 2, .focus = 11 };
981
+ var selected_nodes: [2]canvas.WidgetLayoutNode = undefined;
982
+ var selected_layout = try canvas.layoutWidgetTree(
983
+ .{ .kind = .stack, .children = &.{selected_textarea} },
984
+ geometry.RectF.init(0, 0, 240, 180),
985
+ &selected_nodes,
986
+ );
987
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", selected_layout);
988
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
989
+ .window_id = 1,
990
+ .label = "canvas",
991
+ .kind = .key_down,
992
+ .key = "arrowleft",
993
+ .modifiers = .{ .command = true },
994
+ } });
995
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
996
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(0), retained.nodes[1].widget.text_selection.?);
997
+
998
+ selected_textarea.text_selection = .{ .anchor = 11, .focus = 2 };
999
+ selected_layout = try canvas.layoutWidgetTree(
1000
+ .{ .kind = .stack, .children = &.{selected_textarea} },
1001
+ geometry.RectF.init(0, 0, 240, 180),
1002
+ &selected_nodes,
1003
+ );
1004
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", selected_layout);
1005
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1006
+ .window_id = 1,
1007
+ .label = "canvas",
1008
+ .kind = .key_down,
1009
+ .key = "arrowright",
1010
+ .modifiers = .{ .command = true },
1011
+ } });
1012
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1013
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(13), retained.nodes[1].widget.text_selection.?);
1014
+
1015
+ // A CRLF hard line ends before both delimiter bytes. Command+Right
1016
+ // must not leave the caret between CR and LF, where the next typed
1017
+ // byte would split the line-ending pair.
1018
+ var crlf_textarea = textarea;
1019
+ crlf_textarea.text = "one\r\ntwo";
1020
+ crlf_textarea.text_selection = canvas.TextSelection.collapsed(1);
1021
+ var crlf_nodes: [2]canvas.WidgetLayoutNode = undefined;
1022
+ const crlf_layout = try canvas.layoutWidgetTree(
1023
+ .{ .kind = .stack, .children = &.{crlf_textarea} },
1024
+ geometry.RectF.init(0, 0, 240, 180),
1025
+ &crlf_nodes,
1026
+ );
1027
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", crlf_layout);
1028
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1029
+ .window_id = 1,
1030
+ .label = "canvas",
1031
+ .kind = .key_down,
1032
+ .key = "arrowright",
1033
+ .modifiers = .{ .command = true },
1034
+ } });
1035
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1036
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(3), retained.nodes[1].widget.text_selection.?);
1037
+
1038
+ // The following plain Right crosses the entire hard-line delimiter,
1039
+ // rather than exposing the byte between CR and LF as a caret stop.
1040
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1041
+ .window_id = 1,
1042
+ .label = "canvas",
1043
+ .kind = .key_down,
1044
+ .key = "arrowright",
1045
+ } });
1046
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1047
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(5), retained.nodes[1].widget.text_selection.?);
1048
+
1049
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1050
+ .window_id = 1,
1051
+ .label = "canvas",
1052
+ .kind = .key_down,
1053
+ .key = "x",
1054
+ .text = "X",
1055
+ } });
1056
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1057
+ try std.testing.expectEqualStrings("one\r\nXtwo", retained.nodes[1].widget.text);
1058
+ }
1059
+
1060
+ test "textarea visual navigation keeps an unbroken wrap boundary on its painted line" {
1061
+ const TestApp = struct {
1062
+ fn app(self: *@This()) App {
1063
+ return .{ .context = self, .name = "gpu-widget-textarea-wrap-affinity", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
1064
+ }
1065
+ };
1066
+
1067
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1068
+ defer harness.destroy(std.testing.allocator);
1069
+ harness.null_platform.gpu_surfaces = true;
1070
+ var app_state: TestApp = .{};
1071
+ const app = app_state.app();
1072
+ try harness.start(app);
1073
+
1074
+ _ = try harness.runtime.createView(.{
1075
+ .window_id = 1,
1076
+ .label = "canvas",
1077
+ .kind = .gpu_surface,
1078
+ .frame = geometry.RectF.init(0, 0, 200, 160),
1079
+ });
1080
+
1081
+ const text = "abcdefghijklmnopqrstuvwxyz0123456789";
1082
+ const textarea = canvas.Widget{
1083
+ .id = 2,
1084
+ .kind = .textarea,
1085
+ .frame = geometry.RectF.init(12, 16, 72, 120),
1086
+ .text = text,
1087
+ .text_selection = canvas.TextSelection.collapsed(0),
1088
+ };
1089
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
1090
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{textarea} }, geometry.RectF.init(0, 0, 200, 160), &nodes);
1091
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1092
+ harness.runtime.views[0].focused = true;
1093
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1094
+
1095
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1096
+ var field = retained.nodes[1].widget;
1097
+ const first_caret = canvas.textGeometryForWidget(field, harness.runtime.views[0].widget_tokens).caret_bounds.?;
1098
+ const second_line_start = canvas.textCaretPositionForWidgetPoint(
1099
+ field,
1100
+ geometry.PointF.init(field.frame.x - 1, first_caret.y + first_caret.height * 1.5),
1101
+ harness.runtime.views[0].widget_tokens,
1102
+ ).?;
1103
+ try std.testing.expectEqual(canvas.TextCaretAffinity.downstream, second_line_start.affinity);
1104
+ try std.testing.expect(second_line_start.offset > 0);
1105
+ try std.testing.expect(second_line_start.offset + 1 < text.len);
1106
+
1107
+ // Left from the next scalar first visits the downstream start of this
1108
+ // visual line, then the upstream end of the preceding visual line.
1109
+ _ = try harness.runtime.editCanvasWidgetText(
1110
+ 1,
1111
+ "canvas",
1112
+ 2,
1113
+ .{ .set_selection = canvas.TextSelection.collapsed(second_line_start.offset + 1) },
1114
+ );
1115
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1116
+ .window_id = 1,
1117
+ .label = "canvas",
1118
+ .kind = .key_down,
1119
+ .key = "arrowleft",
1120
+ } });
1121
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1122
+ try std.testing.expectEqualDeep(
1123
+ canvas.TextSelection.collapsedAt(second_line_start),
1124
+ retained.nodes[1].widget.text_selection.?,
1125
+ );
1126
+
1127
+ const pointer_selection = canvas.textSelectionForWidgetPoint(
1128
+ field,
1129
+ geometry.PointF.init(field.frame.x - 1, first_caret.y + first_caret.height * 1.5),
1130
+ null,
1131
+ harness.runtime.views[0].widget_tokens,
1132
+ ).?;
1133
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsedAt(second_line_start), pointer_selection);
1134
+
1135
+ // A selected range beginning at the shared byte belongs visually to
1136
+ // the downstream line. Plain Left collapses to that visible leading
1137
+ // edge instead of jumping to the previous line's upstream stop.
1138
+ _ = try harness.runtime.editCanvasWidgetText(
1139
+ 1,
1140
+ "canvas",
1141
+ 2,
1142
+ .{ .set_selection = .{
1143
+ .anchor = second_line_start.offset,
1144
+ .focus = second_line_start.offset + 1,
1145
+ } },
1146
+ );
1147
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1148
+ .window_id = 1,
1149
+ .label = "canvas",
1150
+ .kind = .key_down,
1151
+ .key = "arrowleft",
1152
+ } });
1153
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1154
+ try std.testing.expectEqualDeep(
1155
+ canvas.TextSelection.collapsedAt(second_line_start),
1156
+ retained.nodes[1].widget.text_selection.?,
1157
+ );
1158
+
1159
+ if (comptime builtin.os.tag == .macos) {
1160
+ _ = try harness.runtime.editCanvasWidgetText(
1161
+ 1,
1162
+ "canvas",
1163
+ 2,
1164
+ .{ .set_selection = canvas.TextSelection.collapsed(second_line_start.offset + 1) },
1165
+ );
1166
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1167
+ .window_id = 1,
1168
+ .label = "canvas",
1169
+ .kind = .key_down,
1170
+ .key = "arrowleft",
1171
+ .modifiers = .{ .command = true },
1172
+ } });
1173
+ } else {
1174
+ _ = try harness.runtime.editCanvasWidgetText(
1175
+ 1,
1176
+ "canvas",
1177
+ 2,
1178
+ .{ .set_selection = canvas.TextSelection.collapsedAt(second_line_start) },
1179
+ );
1180
+ }
1181
+
1182
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1183
+ field = retained.nodes[1].widget;
1184
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsedAt(second_line_start), field.text_selection.?);
1185
+ var navigated_caret = canvas.textGeometryForWidget(field, harness.runtime.views[0].widget_tokens).caret_bounds.?;
1186
+ try std.testing.expect(navigated_caret.y > first_caret.y);
1187
+
1188
+ // Plain Left/Right traverses both painted caret stops at the shared
1189
+ // soft-wrap byte offset before moving to another scalar.
1190
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1191
+ .window_id = 1,
1192
+ .label = "canvas",
1193
+ .kind = .key_down,
1194
+ .key = "arrowleft",
1195
+ } });
1196
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1197
+ field = retained.nodes[1].widget;
1198
+ try std.testing.expectEqualDeep(
1199
+ canvas.TextSelection.collapsedAt(.{ .offset = second_line_start.offset, .affinity = .upstream }),
1200
+ field.text_selection.?,
1201
+ );
1202
+ const upstream_caret = canvas.textGeometryForWidget(field, harness.runtime.views[0].widget_tokens).caret_bounds.?;
1203
+ try std.testing.expectApproxEqAbs(first_caret.y, upstream_caret.y, 0.001);
1204
+
1205
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1206
+ .window_id = 1,
1207
+ .label = "canvas",
1208
+ .kind = .key_down,
1209
+ .key = "arrowright",
1210
+ } });
1211
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1212
+ field = retained.nodes[1].widget;
1213
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsedAt(second_line_start), field.text_selection.?);
1214
+ navigated_caret = canvas.textGeometryForWidget(field, harness.runtime.views[0].widget_tokens).caret_bounds.?;
1215
+ try std.testing.expect(navigated_caret.y > upstream_caret.y);
1216
+
1217
+ // The single-codepoint undo fast path must restore the downstream
1218
+ // affinity recorded before typing, not just the shared byte offset.
1219
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1220
+ .window_id = 1,
1221
+ .label = "canvas",
1222
+ .kind = .key_down,
1223
+ .key = "Q",
1224
+ .text = "Q",
1225
+ } });
1226
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1227
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1228
+ field = retained.nodes[1].widget;
1229
+ try std.testing.expectEqualStrings(text, field.text);
1230
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsedAt(second_line_start), field.text_selection.?);
1231
+ navigated_caret = canvas.textGeometryForWidget(field, harness.runtime.views[0].widget_tokens).caret_bounds.?;
1232
+ try std.testing.expect(navigated_caret.y > upstream_caret.y);
1233
+
1234
+ // Controlled cores and the C ABI historically echo anchor/focus
1235
+ // without an affinity slot. Reconcile retains the visual-line owner
1236
+ // when that byte-identical selection comes back as the default
1237
+ // upstream value.
1238
+ var echoed = textarea;
1239
+ echoed.text_selection = canvas.TextSelection.collapsed(second_line_start.offset);
1240
+ var echoed_nodes: [2]canvas.WidgetLayoutNode = undefined;
1241
+ const echoed_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{echoed} }, geometry.RectF.init(0, 0, 200, 160), &echoed_nodes);
1242
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", echoed_layout);
1243
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1244
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsedAt(second_line_start), retained.nodes[1].widget.text_selection.?);
1245
+ const echoed_caret = canvas.textGeometryForWidget(retained.nodes[1].widget, harness.runtime.views[0].widget_tokens).caret_bounds.?;
1246
+ try std.testing.expectApproxEqAbs(navigated_caret.y, echoed_caret.y, 0.001);
1247
+
1248
+ // A source that does carry affinity can deliberately move between
1249
+ // the two painted caret stops without changing anchor/focus.
1250
+ var explicit_downstream = textarea;
1251
+ explicit_downstream.text_selection = canvas.TextSelection.collapsedAt(second_line_start);
1252
+ var explicit_downstream_nodes: [2]canvas.WidgetLayoutNode = undefined;
1253
+ const explicit_downstream_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{explicit_downstream} }, geometry.RectF.init(0, 0, 200, 160), &explicit_downstream_nodes);
1254
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", explicit_downstream_layout);
1255
+
1256
+ var explicit_upstream = textarea;
1257
+ explicit_upstream.text_selection = canvas.TextSelection.collapsedAt(.{
1258
+ .offset = second_line_start.offset,
1259
+ .affinity = .upstream,
1260
+ });
1261
+ var explicit_upstream_nodes: [2]canvas.WidgetLayoutNode = undefined;
1262
+ const explicit_upstream_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{explicit_upstream} }, geometry.RectF.init(0, 0, 200, 160), &explicit_upstream_nodes);
1263
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", explicit_upstream_layout);
1264
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1265
+ try std.testing.expectEqualDeep(explicit_upstream.text_selection.?, retained.nodes[1].widget.text_selection.?);
1266
+ const explicit_upstream_caret = canvas.textGeometryForWidget(retained.nodes[1].widget, harness.runtime.views[0].widget_tokens).caret_bounds.?;
1267
+ try std.testing.expectApproxEqAbs(upstream_caret.y, explicit_upstream_caret.y, 0.001);
1268
+
1269
+ // A newly controlled source can explicitly choose the downstream
1270
+ // stop even when its previous source tree omitted selection entirely.
1271
+ // Legacy two-field echoes still arrive as the default upstream value.
1272
+ var uncontrolled = textarea;
1273
+ uncontrolled.id = 3;
1274
+ uncontrolled.text_selection = null;
1275
+ var uncontrolled_nodes: [2]canvas.WidgetLayoutNode = undefined;
1276
+ const uncontrolled_layout = try canvas.layoutWidgetTree(
1277
+ .{ .kind = .stack, .children = &.{uncontrolled} },
1278
+ geometry.RectF.init(0, 0, 200, 160),
1279
+ &uncontrolled_nodes,
1280
+ );
1281
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", uncontrolled_layout);
1282
+ _ = try harness.runtime.editCanvasWidgetText(
1283
+ 1,
1284
+ "canvas",
1285
+ 3,
1286
+ .{ .set_selection = canvas.TextSelection.collapsedAt(.{
1287
+ .offset = second_line_start.offset,
1288
+ .affinity = .upstream,
1289
+ }) },
1290
+ );
1291
+
1292
+ var first_controlled = uncontrolled;
1293
+ first_controlled.text_selection = canvas.TextSelection.collapsedAt(second_line_start);
1294
+ var first_controlled_nodes: [2]canvas.WidgetLayoutNode = undefined;
1295
+ const first_controlled_layout = try canvas.layoutWidgetTree(
1296
+ .{ .kind = .stack, .children = &.{first_controlled} },
1297
+ geometry.RectF.init(0, 0, 200, 160),
1298
+ &first_controlled_nodes,
1299
+ );
1300
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", first_controlled_layout);
1301
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1302
+ try std.testing.expectEqualDeep(first_controlled.text_selection.?, retained.nodes[1].widget.text_selection.?);
1303
+ }
1304
+
1305
+ test "single-line history replay restores exact retained bytes" {
1306
+ const TestApp = struct {
1307
+ fn app(self: *@This()) App {
1308
+ return .{ .context = self, .name = "gpu-widget-input-exact-history", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
1309
+ }
1310
+ };
1311
+
1312
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1313
+ defer harness.destroy(std.testing.allocator);
1314
+ harness.null_platform.gpu_surfaces = true;
1315
+ var app_state: TestApp = .{};
1316
+ const app = app_state.app();
1317
+ try harness.start(app);
1318
+
1319
+ _ = try harness.runtime.createView(.{
1320
+ .window_id = 1,
1321
+ .label = "canvas",
1322
+ .kind = .gpu_surface,
1323
+ .frame = geometry.RectF.init(0, 0, 240, 100),
1324
+ });
1325
+ const input = canvas.Widget{
1326
+ .id = 2,
1327
+ .kind = .input,
1328
+ .frame = geometry.RectF.init(12, 16, 180, 32),
1329
+ // Single-line presentation tolerates raw model-provided bytes even
1330
+ // though newly entered line breaks are sanitized.
1331
+ .text = "a\nb",
1332
+ .text_selection = canvas.TextSelection.collapsed(2),
1333
+ };
1334
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
1335
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{input} }, geometry.RectF.init(0, 0, 240, 100), &nodes);
1336
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1337
+ harness.runtime.views[0].focused = true;
1338
+ harness.runtime.views[0].keyboard_active = true;
1339
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1340
+
1341
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1342
+ .window_id = 1,
1343
+ .label = "canvas",
1344
+ .kind = .key_down,
1345
+ .key = "backspace",
1346
+ } });
1347
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1348
+ try std.testing.expectEqualStrings("ab", retained.nodes[1].widget.text);
1349
+ try std.testing.expect(harness.runtime.views[0].canvasWidgetTextHistoryAvailability(2).can_undo);
1350
+
1351
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1352
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1353
+ try std.testing.expectEqualStrings("a\nb", retained.nodes[1].widget.text);
1354
+ try std.testing.expect(!harness.runtime.views[0].canvasWidgetTextHistoryAvailability(2).can_undo);
1355
+ try std.testing.expect(harness.runtime.views[0].canvasWidgetTextHistoryAvailability(2).can_redo);
1356
+ }
1357
+
1358
+ test "textarea history replays newly completed CRLF atomically" {
1359
+ const TestApp = struct {
1360
+ fn app(self: *@This()) App {
1361
+ return .{ .context = self, .name = "gpu-widget-crlf-history", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
1362
+ }
1363
+ };
1364
+
1365
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1366
+ defer harness.destroy(std.testing.allocator);
1367
+ harness.null_platform.gpu_surfaces = true;
1368
+ var app_state: TestApp = .{};
1369
+ const app = app_state.app();
1370
+ try harness.start(app);
1371
+
1372
+ _ = try harness.runtime.createView(.{
1373
+ .window_id = 1,
1374
+ .label = "canvas",
1375
+ .kind = .gpu_surface,
1376
+ .frame = geometry.RectF.init(0, 0, 240, 160),
1377
+ });
1378
+ const after_cr = canvas.Widget{
1379
+ .id = 2,
1380
+ .kind = .textarea,
1381
+ .frame = geometry.RectF.init(12, 16, 180, 48),
1382
+ .text = "a\rb",
1383
+ .text_selection = canvas.TextSelection.collapsed(2),
1384
+ };
1385
+ const before_lf = canvas.Widget{
1386
+ .id = 3,
1387
+ .kind = .textarea,
1388
+ .frame = geometry.RectF.init(12, 84, 180, 48),
1389
+ .text = "a\nb",
1390
+ .text_selection = canvas.TextSelection.collapsed(1),
1391
+ };
1392
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
1393
+ const layout = try canvas.layoutWidgetTree(
1394
+ .{ .kind = .stack, .children = &.{ after_cr, before_lf } },
1395
+ geometry.RectF.init(0, 0, 240, 160),
1396
+ &nodes,
1397
+ );
1398
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1399
+ harness.runtime.views[0].focused = true;
1400
+ harness.runtime.views[0].keyboard_active = true;
1401
+
1402
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1403
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1404
+ .window_id = 1,
1405
+ .label = "canvas",
1406
+ .kind = .key_down,
1407
+ .key = "x",
1408
+ .text = "\n",
1409
+ } });
1410
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1411
+ try std.testing.expectEqualStrings("a\r\nb", retained.nodes[1].widget.text);
1412
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1413
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1414
+ try std.testing.expectEqualStrings("a\rb", retained.nodes[1].widget.text);
1415
+ try dispatchTextareaHistoryShortcut(harness, app, true);
1416
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1417
+ try std.testing.expectEqualStrings("a\r\nb", retained.nodes[1].widget.text);
1418
+
1419
+ harness.runtime.views[0].canvas_widget_focused_id = 3;
1420
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1421
+ .window_id = 1,
1422
+ .label = "canvas",
1423
+ .kind = .key_down,
1424
+ .key = "x",
1425
+ .text = "\r",
1426
+ } });
1427
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1428
+ try std.testing.expectEqualStrings("a\r\nb", retained.nodes[2].widget.text);
1429
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1430
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1431
+ try std.testing.expectEqualStrings("a\nb", retained.nodes[2].widget.text);
1432
+ }
1433
+
1434
+ test "textarea IME history replays CRLF completion atomically" {
1435
+ const TestApp = struct {
1436
+ fn app(self: *@This()) App {
1437
+ return .{ .context = self, .name = "gpu-widget-ime-crlf-history", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
1438
+ }
1439
+ };
1440
+
1441
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1442
+ defer harness.destroy(std.testing.allocator);
1443
+ harness.null_platform.gpu_surfaces = true;
1444
+ var app_state: TestApp = .{};
1445
+ const app = app_state.app();
1446
+ try harness.start(app);
1447
+
1448
+ _ = try harness.runtime.createView(.{
1449
+ .window_id = 1,
1450
+ .label = "canvas",
1451
+ .kind = .gpu_surface,
1452
+ .frame = geometry.RectF.init(0, 0, 240, 160),
1453
+ });
1454
+ const before_lf = canvas.Widget{
1455
+ .id = 2,
1456
+ .kind = .textarea,
1457
+ .frame = geometry.RectF.init(12, 16, 180, 48),
1458
+ .text = "a\rb",
1459
+ .text_selection = canvas.TextSelection.collapsed(2),
1460
+ };
1461
+ const before_cr = canvas.Widget{
1462
+ .id = 3,
1463
+ .kind = .textarea,
1464
+ .frame = geometry.RectF.init(12, 84, 180, 48),
1465
+ .text = "a\nb",
1466
+ .text_selection = canvas.TextSelection.collapsed(1),
1467
+ };
1468
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
1469
+ const layout = try canvas.layoutWidgetTree(
1470
+ .{ .kind = .stack, .children = &.{ before_lf, before_cr } },
1471
+ geometry.RectF.init(0, 0, 240, 160),
1472
+ &nodes,
1473
+ );
1474
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1475
+ harness.runtime.views[0].focused = true;
1476
+ harness.runtime.views[0].keyboard_active = true;
1477
+
1478
+ // A later preedit rewrite completing an existing CR with LF must have
1479
+ // retained the CR as shared context from the first preview: Undo restores
1480
+ // it instead of deleting it too.
1481
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1482
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1483
+ .window_id = 1,
1484
+ .label = "canvas",
1485
+ .kind = .ime_set_composition,
1486
+ .text = "X",
1487
+ .composition_cursor = 1,
1488
+ } });
1489
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1490
+ .window_id = 1,
1491
+ .label = "canvas",
1492
+ .kind = .ime_set_composition,
1493
+ .text = "\n",
1494
+ .composition_cursor = 1,
1495
+ } });
1496
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1497
+ .window_id = 1,
1498
+ .label = "canvas",
1499
+ .kind = .ime_commit_composition,
1500
+ } });
1501
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1502
+ try std.testing.expectEqualStrings("a\r\nb", retained.nodes[1].widget.text);
1503
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1504
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1505
+ try std.testing.expectEqualStrings("a\rb", retained.nodes[1].widget.text);
1506
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(2), retained.nodes[1].widget.text_selection.?);
1507
+ try dispatchTextareaHistoryShortcut(harness, app, true);
1508
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1509
+ try std.testing.expectEqualStrings("a\r\nb", retained.nodes[1].widget.text);
1510
+
1511
+ // The reverse boundary has the same requirement: a later rewrite to CR
1512
+ // before an existing LF must not leave Undo selecting an unsplittable half.
1513
+ harness.runtime.views[0].canvas_widget_focused_id = 3;
1514
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1515
+ .window_id = 1,
1516
+ .label = "canvas",
1517
+ .kind = .ime_set_composition,
1518
+ .text = "X",
1519
+ .composition_cursor = 1,
1520
+ } });
1521
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1522
+ .window_id = 1,
1523
+ .label = "canvas",
1524
+ .kind = .ime_set_composition,
1525
+ .text = "\r",
1526
+ .composition_cursor = 1,
1527
+ } });
1528
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1529
+ .window_id = 1,
1530
+ .label = "canvas",
1531
+ .kind = .ime_commit_composition,
1532
+ } });
1533
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1534
+ try std.testing.expectEqualStrings("a\r\nb", retained.nodes[2].widget.text);
1535
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1536
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1537
+ try std.testing.expectEqualStrings("a\nb", retained.nodes[2].widget.text);
1538
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(1), retained.nodes[2].widget.text_selection.?);
1539
+ try dispatchTextareaHistoryShortcut(harness, app, true);
1540
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1541
+ try std.testing.expectEqualStrings("a\r\nb", retained.nodes[2].widget.text);
1542
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(1), retained.nodes[2].widget.text_selection.?);
1543
+ }
1544
+
1545
+ test "canvas textareas undo and redo keyboard edits" {
1546
+ const TestApp = struct {
1547
+ fn app(self: *@This()) App {
1548
+ return .{ .context = self, .name = "gpu-widget-textarea-history", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
1549
+ }
1550
+ };
1551
+
1552
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1553
+ defer harness.destroy(std.testing.allocator);
1554
+ harness.null_platform.gpu_surfaces = true;
1555
+ var app_state: TestApp = .{};
1556
+ const app = app_state.app();
1557
+ try harness.start(app);
1558
+
1559
+ _ = try harness.runtime.createView(.{
1560
+ .window_id = 1,
1561
+ .label = "canvas",
1562
+ .kind = .gpu_surface,
1563
+ .frame = geometry.RectF.init(0, 0, 260, 160),
1564
+ });
1565
+
1566
+ const textarea = canvas.Widget{
1567
+ .id = 2,
1568
+ .kind = .textarea,
1569
+ .frame = geometry.RectF.init(12, 16, 180, 84),
1570
+ .text = "alpha",
1571
+ .semantics = .{ .label = "Message" },
1572
+ };
1573
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
1574
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{textarea} }, geometry.RectF.init(0, 0, 260, 160), &nodes);
1575
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1576
+
1577
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1578
+ .window_id = 1,
1579
+ .label = "canvas",
1580
+ .kind = .pointer_down,
1581
+ .x = 188,
1582
+ .y = 28,
1583
+ } });
1584
+
1585
+ const typed = [_][]const u8{ "!", "?" };
1586
+ for (typed) |text| {
1587
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1588
+ .window_id = 1,
1589
+ .label = "canvas",
1590
+ .kind = .key_down,
1591
+ .key = text,
1592
+ .text = text,
1593
+ } });
1594
+ }
1595
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1596
+ try std.testing.expectEqualStrings("alpha!?", retained.nodes[1].widget.text);
1597
+
1598
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1599
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1600
+ try std.testing.expectEqualStrings("alpha!", retained.nodes[1].widget.text);
1601
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(6), retained.nodes[1].widget.text_selection.?);
1602
+
1603
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1604
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1605
+ try std.testing.expectEqualStrings("alpha", retained.nodes[1].widget.text);
1606
+
1607
+ try dispatchTextareaHistoryShortcut(harness, app, true);
1608
+ try dispatchTextareaHistoryShortcut(harness, app, true);
1609
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1610
+ try std.testing.expectEqualStrings("alpha!?", retained.nodes[1].widget.text);
1611
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(7), retained.nodes[1].widget.text_selection.?);
1612
+
1613
+ // A multi-character insertion exercises the compound history path: undo
1614
+ // selects the inserted range, removes it, and restores the old caret;
1615
+ // redo replays the same replacement as one logical shortcut.
1616
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1617
+ .window_id = 1,
1618
+ .label = "canvas",
1619
+ .kind = .key_down,
1620
+ .key = "x",
1621
+ .text = "XYZ",
1622
+ } });
1623
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1624
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1625
+ try std.testing.expectEqualStrings("alpha!?", retained.nodes[1].widget.text);
1626
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(7), retained.nodes[1].widget.text_selection.?);
1627
+ try dispatchTextareaHistoryShortcut(harness, app, true);
1628
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1629
+ try std.testing.expectEqualStrings("alpha!?XYZ", retained.nodes[1].widget.text);
1630
+
1631
+ // Every preedit rewrite belongs to one transaction. Commit produces
1632
+ // one undoable step containing only the final composed text.
1633
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1634
+ .window_id = 1,
1635
+ .label = "canvas",
1636
+ .kind = .ime_set_composition,
1637
+ .text = "é",
1638
+ .composition_cursor = 2,
1639
+ } });
1640
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1641
+ .window_id = 1,
1642
+ .label = "canvas",
1643
+ .kind = .ime_set_composition,
1644
+ .text = "漢字",
1645
+ .composition_cursor = 6,
1646
+ } });
1647
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1648
+ .window_id = 1,
1649
+ .label = "canvas",
1650
+ .kind = .ime_commit_composition,
1651
+ } });
1652
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1653
+ try std.testing.expectEqualStrings("alpha!?XYZ漢字", retained.nodes[1].widget.text);
1654
+ try std.testing.expect(retained.nodes[1].widget.text_composition == null);
1655
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1656
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1657
+ try std.testing.expectEqualStrings("alpha!?XYZ", retained.nodes[1].widget.text);
1658
+ try dispatchTextareaHistoryShortcut(harness, app, true);
1659
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1660
+ try std.testing.expectEqualStrings("alpha!?XYZ漢字", retained.nodes[1].widget.text);
1661
+
1662
+ // A composition that replaces a selection retains the removed bytes
1663
+ // in the same transaction, so Undo restores both text and selection.
1664
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .set_selection = .{ .anchor = 0, .focus = 5 } });
1665
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1666
+ .window_id = 1,
1667
+ .label = "canvas",
1668
+ .kind = .ime_set_composition,
1669
+ .text = "Ω",
1670
+ .composition_cursor = 2,
1671
+ } });
1672
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1673
+ .window_id = 1,
1674
+ .label = "canvas",
1675
+ .kind = .ime_commit_composition,
1676
+ } });
1677
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1678
+ try std.testing.expectEqualStrings("Ω!?XYZ漢字", retained.nodes[1].widget.text);
1679
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1680
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1681
+ try std.testing.expectEqualStrings("alpha!?XYZ漢字", retained.nodes[1].widget.text);
1682
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 5 }, retained.nodes[1].widget.text_selection.?);
1683
+
1684
+ // A live preedit can temporarily equal the bytes it replaced while
1685
+ // only its cursor changes. That intermediate no-op must keep the
1686
+ // provisional transaction alive for a later changed commit.
1687
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1688
+ .window_id = 1,
1689
+ .label = "canvas",
1690
+ .kind = .ime_set_composition,
1691
+ .text = "alpha",
1692
+ .composition_cursor = 0,
1693
+ } });
1694
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1695
+ .window_id = 1,
1696
+ .label = "canvas",
1697
+ .kind = .ime_set_composition,
1698
+ .text = "alpha",
1699
+ .composition_cursor = 5,
1700
+ } });
1701
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1702
+ .window_id = 1,
1703
+ .label = "canvas",
1704
+ .kind = .ime_set_composition,
1705
+ .text = "omega",
1706
+ .composition_cursor = 5,
1707
+ } });
1708
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1709
+ .window_id = 1,
1710
+ .label = "canvas",
1711
+ .kind = .ime_commit_composition,
1712
+ } });
1713
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1714
+ try std.testing.expectEqualStrings("omega!?XYZ漢字", retained.nodes[1].widget.text);
1715
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1716
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1717
+ try std.testing.expectEqualStrings("alpha!?XYZ漢字", retained.nodes[1].widget.text);
1718
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 5 }, retained.nodes[1].widget.text_selection.?);
1719
+
1720
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .set_selection = canvas.TextSelection.collapsed(retained.nodes[1].widget.text.len) });
1721
+
1722
+ // A cancelled preedit with no net text change drops only its
1723
+ // provisional step; the ordinary edit immediately before it remains.
1724
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1725
+ .window_id = 1,
1726
+ .label = "canvas",
1727
+ .kind = .key_down,
1728
+ .key = "q",
1729
+ .text = "Q",
1730
+ } });
1731
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1732
+ .window_id = 1,
1733
+ .label = "canvas",
1734
+ .kind = .ime_set_composition,
1735
+ .text = "draft",
1736
+ .composition_cursor = 5,
1737
+ } });
1738
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1739
+ .window_id = 1,
1740
+ .label = "canvas",
1741
+ .kind = .ime_cancel_composition,
1742
+ } });
1743
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1744
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1745
+ try std.testing.expectEqualStrings("alpha!?XYZ漢字", retained.nodes[1].widget.text);
1746
+
1747
+ // Canceling a no-op preedit also preserves a standing Redo branch.
1748
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1749
+ .window_id = 1,
1750
+ .label = "canvas",
1751
+ .kind = .ime_set_composition,
1752
+ .text = "draft",
1753
+ .composition_cursor = 5,
1754
+ } });
1755
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1756
+ .window_id = 1,
1757
+ .label = "canvas",
1758
+ .kind = .ime_cancel_composition,
1759
+ } });
1760
+ try dispatchTextareaHistoryShortcut(harness, app, true);
1761
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1762
+ try std.testing.expectEqualStrings("alpha!?XYZ漢字Q", retained.nodes[1].widget.text);
1763
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1764
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1765
+ try std.testing.expectEqualStrings("alpha!?XYZ漢字", retained.nodes[1].widget.text);
1766
+
1767
+ // A source-driven controlled replacement starts a new timeline on
1768
+ // the next edit. Exhausting Undo must not consume that edit's Redo
1769
+ // branch while rejecting history from the previous buffer.
1770
+ const replacement = canvas.Widget{
1771
+ .id = 2,
1772
+ .kind = .textarea,
1773
+ .frame = textarea.frame,
1774
+ .text = "external",
1775
+ };
1776
+ var replacement_nodes: [2]canvas.WidgetLayoutNode = undefined;
1777
+ const replacement_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{replacement} }, geometry.RectF.init(0, 0, 260, 160), &replacement_nodes);
1778
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", replacement_layout);
1779
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1780
+ .window_id = 1,
1781
+ .label = "canvas",
1782
+ .kind = .key_down,
1783
+ .key = "!",
1784
+ .text = "!",
1785
+ } });
1786
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1787
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1788
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1789
+ try std.testing.expectEqualStrings("external", retained.nodes[1].widget.text);
1790
+ try dispatchTextareaHistoryShortcut(harness, app, true);
1791
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1792
+ try std.testing.expectEqualStrings("external!", retained.nodes[1].widget.text);
1793
+ }
1794
+
1795
+ test "compound editor history reroutes every continuation after controlled rebuilds" {
1796
+ const TestApp = struct {
1797
+ replay_count: usize = 0,
1798
+ replay_parent_ids: [3]canvas.ObjectId = .{ 0, 0, 0 },
1799
+ replay_target_x: [3]f32 = .{ 0, 0, 0 },
1800
+ text_storage: [32]u8 = undefined,
1801
+ text_len: usize = 0,
1802
+
1803
+ fn app(self: *@This()) App {
1804
+ return .{
1805
+ .context = self,
1806
+ .name = "gpu-widget-editor-history-reroute",
1807
+ .source = platform.WebViewSource.html("<h1>Hello</h1>"),
1808
+ .event_fn = event,
1809
+ };
1810
+ }
1811
+
1812
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
1813
+ const self: *@This() = @ptrCast(@alignCast(context));
1814
+ switch (event_value) {
1815
+ .canvas_widget_keyboard => |keyboard_event| {
1816
+ if (keyboard_event.history_replay_serial == 0) return;
1817
+ if (self.replay_count >= self.replay_parent_ids.len) return error.TestUnexpectedResult;
1818
+
1819
+ for (keyboard_event.route) |entry| {
1820
+ if (entry.phase != .capture) continue;
1821
+ self.replay_parent_ids[self.replay_count] = entry.id;
1822
+ break;
1823
+ }
1824
+ const target = keyboard_event.target orelse return error.TestUnexpectedResult;
1825
+ self.replay_target_x[self.replay_count] = target.bounds.x;
1826
+ self.replay_count += 1;
1827
+
1828
+ const next_parent_id: canvas.ObjectId = switch (self.replay_count) {
1829
+ 1 => 20,
1830
+ 2 => 30,
1831
+ else => return,
1832
+ };
1833
+ try self.rebuildEditor(runtime, next_parent_id);
1834
+ },
1835
+ else => {},
1836
+ }
1837
+ }
1838
+
1839
+ fn rebuildEditor(self: *@This(), runtime: *Runtime, parent_id: canvas.ObjectId) anyerror!void {
1840
+ const retained = try runtime.canvasWidgetLayout(1, "canvas");
1841
+ const retained_editor = retained.findById(2) orelse return error.TestUnexpectedResult;
1842
+ if (retained_editor.widget.text.len > self.text_storage.len) return error.TestUnexpectedResult;
1843
+ @memcpy(self.text_storage[0..retained_editor.widget.text.len], retained_editor.widget.text);
1844
+ self.text_len = retained_editor.widget.text.len;
1845
+
1846
+ const editor_x: f32 = switch (parent_id) {
1847
+ 20 => 32,
1848
+ 30 => 52,
1849
+ else => 12,
1850
+ };
1851
+ const textarea = canvas.Widget{
1852
+ .id = 2,
1853
+ .kind = .textarea,
1854
+ .frame = geometry.RectF.init(editor_x, 20, 180, 84),
1855
+ .text = self.text_storage[0..self.text_len],
1856
+ .text_selection = retained_editor.widget.text_selection,
1857
+ .text_composition = retained_editor.widget.text_composition,
1858
+ };
1859
+ const parent = canvas.Widget{
1860
+ .id = parent_id,
1861
+ .kind = .stack,
1862
+ .frame = geometry.RectF.init(0, 0, 280, 140),
1863
+ .children = &.{textarea},
1864
+ };
1865
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
1866
+ const layout = try canvas.layoutWidgetTree(parent, parent.frame, &nodes);
1867
+ _ = try runtime.setCanvasWidgetLayout(1, "canvas", layout);
1868
+ }
1869
+ };
1870
+
1871
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1872
+ defer harness.destroy(std.testing.allocator);
1873
+ harness.null_platform.gpu_surfaces = true;
1874
+ var app_state: TestApp = .{};
1875
+ const app = app_state.app();
1876
+ try harness.start(app);
1877
+
1878
+ const frame = geometry.RectF.init(0, 0, 280, 140);
1879
+ _ = try harness.runtime.createView(.{
1880
+ .window_id = 1,
1881
+ .label = "canvas",
1882
+ .kind = .gpu_surface,
1883
+ .frame = frame,
1884
+ });
1885
+
1886
+ const textarea = canvas.Widget{
1887
+ .id = 2,
1888
+ .kind = .textarea,
1889
+ .frame = geometry.RectF.init(12, 20, 180, 84),
1890
+ .text = "old",
1891
+ .text_selection = .{ .anchor = 0, .focus = 3 },
1892
+ };
1893
+ const parent = canvas.Widget{
1894
+ .id = 10,
1895
+ .kind = .stack,
1896
+ .frame = frame,
1897
+ .children = &.{textarea},
1898
+ };
1899
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
1900
+ const layout = try canvas.layoutWidgetTree(parent, frame, &nodes);
1901
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1902
+ harness.runtime.views[0].focused = true;
1903
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
1904
+
1905
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
1906
+ .window_id = 1,
1907
+ .label = "canvas",
1908
+ .kind = .key_down,
1909
+ .key = "n",
1910
+ .text = "NEW",
1911
+ } });
1912
+ try dispatchTextareaHistoryShortcut(harness, app, false);
1913
+
1914
+ try std.testing.expectEqual(@as(usize, 3), app_state.replay_count);
1915
+ try std.testing.expectEqualDeep([3]canvas.ObjectId{ 10, 20, 30 }, app_state.replay_parent_ids);
1916
+ try std.testing.expectEqualDeep([3]f32{ 12, 32, 52 }, app_state.replay_target_x);
1917
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
1918
+ try std.testing.expectEqualStrings("old", retained.findById(2).?.widget.text);
1919
+ try std.testing.expectEqualDeep(
1920
+ canvas.TextSelection{ .anchor = 0, .focus = 3 },
1921
+ retained.findById(2).?.widget.text_selection.?,
1922
+ );
1923
+ }
1924
+
1925
+ test "compound editor history re-resolves bytes and identity after rebuilds" {
1926
+ const TestApp = struct {
1927
+ fn app(self: *@This()) App {
1928
+ return .{ .context = self, .name = "gpu-widget-editor-history-replay-lifetime", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
1929
+ }
1930
+ };
1931
+
1932
+ const harness = try TestHarness().create(std.testing.allocator, .{});
1933
+ defer harness.destroy(std.testing.allocator);
1934
+ harness.null_platform.gpu_surfaces = true;
1935
+ var app_state: TestApp = .{};
1936
+ const app = app_state.app();
1937
+ try harness.start(app);
1938
+
1939
+ const frame = geometry.RectF.init(0, 0, 320, 180);
1940
+ _ = try harness.runtime.createView(.{
1941
+ .window_id = 1,
1942
+ .label = "canvas",
1943
+ .kind = .gpu_surface,
1944
+ .frame = frame,
1945
+ });
1946
+
1947
+ const earlier_editor = canvas.Widget{
1948
+ .id = 2,
1949
+ .kind = .input,
1950
+ .frame = geometry.RectF.init(12, 16, 120, 32),
1951
+ .text = "b",
1952
+ };
1953
+ const textarea = canvas.Widget{
1954
+ .id = 3,
1955
+ .kind = .textarea,
1956
+ .frame = geometry.RectF.init(12, 56, 180, 84),
1957
+ .text = "old",
1958
+ };
1959
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
1960
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{ earlier_editor, textarea } }, frame, &nodes);
1961
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
1962
+ const view = &harness.runtime.views[0];
1963
+
1964
+ // Put a one-byte entry before the textarea's larger replacement entry.
1965
+ // Removing it during the compound replay forces overlapping history-byte
1966
+ // compaction, the case that used to corrupt a retained follow-up slice.
1967
+ _ = try view.applyCanvasWidgetTextEdit(2, .{ .insert_text = "!" });
1968
+ _ = try view.applyCanvasWidgetTextEdit(3, .{ .set_selection = .{ .anchor = 0, .focus = 3 } });
1969
+ _ = try view.applyCanvasWidgetTextEdit(3, .{ .insert_text = "NEW" });
1970
+ view.focused = true;
1971
+ view.canvas_widget_focused_id = 3;
1972
+
1973
+ var target = view.widgetLayoutTree().focusTargetById(3).?;
1974
+ const shortcut = view.canvasWidgetTextHistoryShortcut(target, .{
1975
+ .phase = .key_down,
1976
+ .key = "z",
1977
+ .modifiers = .{ .super = true },
1978
+ }).?;
1979
+ switch (shortcut.edit) {
1980
+ .set_selection => |selection| try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 3 }, selection),
1981
+ else => return error.TestUnexpectedResult,
1982
+ }
1983
+ _ = try view.applyCanvasWidgetTextEditWithoutHistory(3, shortcut.edit);
1984
+
1985
+ // The first controlled on-input rebuild unmounts the earlier editor.
1986
+ // History compacts, then the replacement bytes are resolved at their new
1987
+ // location immediately before the continuation is applied.
1988
+ var rebuilt_nodes: [2]canvas.WidgetLayoutNode = undefined;
1989
+ var rebuilt_textarea = textarea;
1990
+ rebuilt_textarea.text = "NEW";
1991
+ const rebuilt_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{rebuilt_textarea} }, frame, &rebuilt_nodes);
1992
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", rebuilt_layout);
1993
+
1994
+ const replacement = view.canvasWidgetTextHistoryReplayNext(target, shortcut.serial, shortcut.redo).?;
1995
+ switch (replacement) {
1996
+ .insert_text => |text| try std.testing.expectEqualStrings("old", text),
1997
+ else => return error.TestUnexpectedResult,
1998
+ }
1999
+ _ = try view.applyCanvasWidgetTextEditWithoutHistory(3, replacement);
2000
+ const restore_selection = view.canvasWidgetTextHistoryReplayNext(target, shortcut.serial, shortcut.redo).?;
2001
+ _ = try view.applyCanvasWidgetTextEditWithoutHistory(3, restore_selection);
2002
+ try std.testing.expect(view.canvasWidgetTextHistoryReplayNext(target, shortcut.serial, shortcut.redo) == null);
2003
+ try std.testing.expectEqualStrings("old", view.widgetLayoutTree().findById(3).?.widget.text);
2004
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 3 }, view.widgetLayoutTree().findById(3).?.widget.text_selection.?);
2005
+
2006
+ // Start another compound replacement, then let its first selection Msg
2007
+ // rebuild the same structural id as a different editable kind. The replay
2008
+ // token belongs to the retired textarea and must not edit the new input.
2009
+ _ = try view.setCanvasWidgetTextValue(3, "old\n");
2010
+ _ = try view.applyCanvasWidgetTextEdit(3, .{ .set_selection = .{ .anchor = 0, .focus = 4 } });
2011
+ _ = try view.applyCanvasWidgetTextEdit(3, .{ .insert_text = "next" });
2012
+ target = view.widgetLayoutTree().focusTargetById(3).?;
2013
+ const retiring_shortcut = view.canvasWidgetTextHistoryShortcut(target, .{
2014
+ .phase = .key_down,
2015
+ .key = "z",
2016
+ .modifiers = .{ .super = true },
2017
+ }).?;
2018
+ _ = try view.applyCanvasWidgetTextEditWithoutHistory(3, retiring_shortcut.edit);
2019
+
2020
+ const replacement_input = canvas.Widget{
2021
+ .id = 3,
2022
+ .kind = .input,
2023
+ .frame = textarea.frame,
2024
+ .text = "next",
2025
+ };
2026
+ var input_nodes: [2]canvas.WidgetLayoutNode = undefined;
2027
+ const input_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{replacement_input} }, frame, &input_nodes);
2028
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", input_layout);
2029
+ try std.testing.expect(view.canvasWidgetTextHistoryReplayNext(target, retiring_shortcut.serial, retiring_shortcut.redo) == null);
2030
+ try std.testing.expectEqualStrings("next", view.widgetLayoutTree().findById(3).?.widget.text);
2031
+ }
2032
+
2033
+ test "canvas editor history retires when its widget unmounts or changes kind" {
2034
+ const TestApp = struct {
2035
+ fn app(self: *@This()) App {
2036
+ return .{ .context = self, .name = "gpu-widget-editor-history-lifecycle", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
2037
+ }
2038
+ };
2039
+
2040
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2041
+ defer harness.destroy(std.testing.allocator);
2042
+ harness.null_platform.gpu_surfaces = true;
2043
+ var app_state: TestApp = .{};
2044
+ const app = app_state.app();
2045
+ try harness.start(app);
2046
+
2047
+ const frame = geometry.RectF.init(0, 0, 260, 160);
2048
+ _ = try harness.runtime.createView(.{
2049
+ .window_id = 1,
2050
+ .label = "canvas",
2051
+ .kind = .gpu_surface,
2052
+ .frame = frame,
2053
+ });
2054
+
2055
+ const textarea = canvas.Widget{
2056
+ .id = 2,
2057
+ .kind = .textarea,
2058
+ .frame = geometry.RectF.init(12, 16, 180, 84),
2059
+ .text = "base",
2060
+ };
2061
+ var textarea_nodes: [2]canvas.WidgetLayoutNode = undefined;
2062
+ const textarea_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{textarea} }, frame, &textarea_nodes);
2063
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", textarea_layout);
2064
+ harness.runtime.views[0].focused = true;
2065
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
443
2066
 
444
2067
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
445
2068
  .window_id = 1,
446
2069
  .label = "canvas",
447
- .kind = .key_down,
448
- .key = "arrowright",
449
- .modifiers = .{ .command = true },
2070
+ .kind = .text_input,
2071
+ .text = "!",
450
2072
  } });
451
- retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
452
- try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(7), retained.nodes[1].widget.text_selection.?);
453
2073
 
454
- const textarea_revision = harness.runtime.views[0].widget_revision;
2074
+ // Reusing the structural id for a different editor kind creates a new
2075
+ // control, even when its source text equals the old history boundary.
2076
+ const input = canvas.Widget{
2077
+ .id = 2,
2078
+ .kind = .input,
2079
+ .frame = textarea.frame,
2080
+ .text = "base!",
2081
+ };
2082
+ var input_nodes: [2]canvas.WidgetLayoutNode = undefined;
2083
+ const input_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{input} }, frame, &input_nodes);
2084
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", input_layout);
2085
+ try dispatchTextareaHistoryShortcut(harness, app, false);
2086
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2087
+ try std.testing.expectEqualStrings("base!", retained.nodes[1].widget.text);
2088
+
455
2089
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
456
2090
  .window_id = 1,
457
2091
  .label = "canvas",
458
- .kind = .key_down,
459
- .key = "arrowup",
2092
+ .kind = .text_input,
2093
+ .text = "?",
460
2094
  } });
2095
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2096
+ try std.testing.expectEqualStrings("base!?", retained.nodes[1].widget.text);
2097
+
2098
+ // An absent adoption retires the input's timeline. Remounting the same
2099
+ // id and kind with matching bytes must not resurrect it.
2100
+ var empty_nodes: [1]canvas.WidgetLayoutNode = undefined;
2101
+ const empty_layout = try canvas.layoutWidgetTree(.{ .kind = .stack }, frame, &empty_nodes);
2102
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", empty_layout);
2103
+
2104
+ var remount_nodes: [2]canvas.WidgetLayoutNode = undefined;
2105
+ var remounted = input;
2106
+ remounted.text = "base!?";
2107
+ const remount_layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{remounted} }, frame, &remount_nodes);
2108
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", remount_layout);
2109
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
2110
+ try dispatchTextareaHistoryShortcut(harness, app, false);
2111
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2112
+ try std.testing.expectEqualStrings("base!?", retained.nodes[1].widget.text);
2113
+ }
2114
+
2115
+ fn dispatchTextareaHistoryShortcut(harness: *TestHarness(), app: App, redo: bool) !void {
461
2116
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
462
2117
  .window_id = 1,
463
2118
  .label = "canvas",
464
2119
  .kind = .key_down,
465
- .key = "arrowdown",
2120
+ .key = "z",
2121
+ .modifiers = .{ .command = true, .shift = redo },
466
2122
  } });
467
- retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
468
- try std.testing.expectEqual(@as(u64, textarea_revision), harness.runtime.views[0].widget_revision);
469
- try std.testing.expectEqualStrings("First!\n", retained.nodes[1].widget.text);
470
- try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(7), retained.nodes[1].widget.text_selection.?);
2123
+ }
471
2124
 
472
- _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .insert_text = "Second" });
473
- retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
474
- try std.testing.expectEqualStrings("First!\nSecond", retained.nodes[1].widget.text);
475
- try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(13), retained.nodes[1].widget.text_selection.?);
476
- try std.testing.expectEqualDeep(canvas.TextRange.init(13, 13), runtimeViewWidgetSemantics(&harness.runtime.views[0])[0].text_selection.?);
2125
+ test "IME history snaps source selections at UTF-8 boundaries" {
2126
+ const TestApp = struct {
2127
+ fn app(self: *@This()) App {
2128
+ return .{ .context = self, .name = "gpu-widget-ime-history-snap", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
2129
+ }
2130
+ };
477
2131
 
478
- const text_geometry = try harness.runtime.canvasWidgetTextGeometry(1, "canvas", 2);
479
- try std.testing.expect(text_geometry.caret_bounds != null);
480
- const snapshot = harness.runtime.automationSnapshot("Widgets");
481
- try std.testing.expectEqualStrings("Message", snapshot.widgets[0].name);
482
- try std.testing.expectEqualStrings("First!\nSecond", snapshot.widgets[0].text_value);
483
- try std.testing.expect(snapshot.widgets[0].actions.set_text);
484
- try std.testing.expect(snapshot.widgets[0].actions.set_selection);
2132
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2133
+ defer harness.destroy(std.testing.allocator);
2134
+ harness.null_platform.gpu_surfaces = true;
2135
+ var app_state: TestApp = .{};
2136
+ const app = app_state.app();
2137
+ try harness.start(app);
485
2138
 
486
- _ = try harness.runtime.emitCanvasWidgetDisplayList(1, "canvas", .{});
487
- const display_list = try harness.runtime.canvasDisplayList(1, "canvas");
488
- var saw_textarea_text = false;
489
- for (display_list.commands) |command| {
490
- switch (command) {
491
- .draw_text => |text| {
492
- if (text.id == testCanvasWidgetPartId(2, 4)) {
493
- try std.testing.expectEqualStrings("First!\nSecond", text.text);
494
- try std.testing.expect(text.text_layout != null);
495
- try std.testing.expectEqual(canvas.TextWrap.word, text.text_layout.?.wrap);
496
- saw_textarea_text = true;
497
- }
498
- },
499
- else => {},
500
- }
501
- }
502
- try std.testing.expect(saw_textarea_text);
2139
+ _ = try harness.runtime.createView(.{
2140
+ .window_id = 1,
2141
+ .label = "canvas",
2142
+ .kind = .gpu_surface,
2143
+ .frame = geometry.RectF.init(0, 0, 260, 160),
2144
+ });
503
2145
 
504
- _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .insert_text = "\nThird\nFourth\nFifth\nSixth\nSeventh\nEighth" });
505
- retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
506
- try std.testing.expect(retained.nodes[1].widget.value > 0);
507
- try std.testing.expect(canvas.textInputMaxScrollOffsetForWidget(retained.nodes[1].widget, .{}) > 0);
508
- const scrolled_viewport = canvas.textInputViewportForWidget(retained.nodes[1].widget, .{}).?;
509
- const scrolled_geometry = try harness.runtime.canvasWidgetTextGeometry(1, "canvas", 2);
510
- const scrolled_caret = scrolled_geometry.caret_bounds.?;
511
- try std.testing.expect(scrolled_caret.y >= scrolled_viewport.y - 0.001);
512
- try std.testing.expect(scrolled_caret.maxY() <= scrolled_viewport.maxY() + 0.001);
2146
+ // Source selections are byte offsets, and the editor promises to snap a
2147
+ // continuation-byte offset before applying an edit. History must record
2148
+ // that same normalized replacement range or Undo can splice back only a
2149
+ // suffix of the scalar and leave invalid UTF-8 behind.
2150
+ const textarea = canvas.Widget{
2151
+ .id = 2,
2152
+ .kind = .textarea,
2153
+ .frame = geometry.RectF.init(12, 16, 180, 84),
2154
+ .text = "éx",
2155
+ .text_selection = .{ .anchor = 1, .focus = 3 },
2156
+ };
2157
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
2158
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{textarea} }, geometry.RectF.init(0, 0, 260, 160), &nodes);
2159
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
2160
+ harness.runtime.views[0].focused = true;
2161
+ harness.runtime.views[0].canvas_widget_focused_id = 2;
513
2162
 
514
- _ = try harness.runtime.emitCanvasWidgetDisplayList(1, "canvas", .{});
515
- const scrolled_display_list = try harness.runtime.canvasDisplayList(1, "canvas");
516
- var saw_textarea_clip = false;
517
- for (scrolled_display_list.commands) |command| {
518
- switch (command) {
519
- .push_clip => |clip| {
520
- if (clip.id == testCanvasWidgetPartId(2, 16)) {
521
- try std.testing.expectEqualDeep(scrolled_viewport, clip.rect);
522
- saw_textarea_clip = true;
523
- }
524
- },
525
- else => {},
526
- }
527
- }
528
- try std.testing.expect(saw_textarea_clip);
2163
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2164
+ .window_id = 1,
2165
+ .label = "canvas",
2166
+ .kind = .ime_set_composition,
2167
+ .text = "Q",
2168
+ .composition_cursor = 1,
2169
+ } });
2170
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2171
+ .window_id = 1,
2172
+ .label = "canvas",
2173
+ .kind = .ime_commit_composition,
2174
+ } });
2175
+ try dispatchTextareaHistoryShortcut(harness, app, false);
2176
+
2177
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2178
+ try std.testing.expectEqualStrings("éx", retained.nodes[1].widget.text);
2179
+ try std.testing.expectEqualDeep(
2180
+ canvas.TextSelection{ .anchor = 0, .focus = 3 },
2181
+ retained.nodes[1].widget.text_selection.?,
2182
+ );
529
2183
  }
530
2184
 
531
2185
  test "plain Enter inserts a newline in a canvas textarea; chorded Enter never edits" {
@@ -1998,6 +3652,18 @@ test "a widget text budget overflow on input degrades instead of exiting" {
1998
3652
  .y = 30,
1999
3653
  } });
2000
3654
 
3655
+ // Seed a redo branch. A refused edit must not fork or evict it.
3656
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3657
+ .window_id = 1,
3658
+ .label = "canvas",
3659
+ .kind = .key_down,
3660
+ .key = "!",
3661
+ .text = "!",
3662
+ } });
3663
+ try dispatchTextareaHistoryShortcut(harness, app, false);
3664
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
3665
+ try std.testing.expectEqual(filler.len, retained.nodes[1].widget.text.len);
3666
+
2001
3667
  const burst = [_]u8{'b'} ** 510;
2002
3668
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2003
3669
  .window_id = 1,
@@ -2008,14 +3674,68 @@ test "a widget text budget overflow on input degrades instead of exiting" {
2008
3674
  } });
2009
3675
 
2010
3676
  // The edit was refused, the error recorded, the app still running.
2011
- const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
3677
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2012
3678
  try std.testing.expectEqual(filler.len, retained.nodes[1].widget.text.len);
2013
3679
  const errors = harness.runtime.dispatchErrors();
2014
3680
  try std.testing.expect(errors.len >= 1);
2015
3681
  try std.testing.expectEqualStrings("gpu_surface_input", errors[errors.len - 1].event());
2016
3682
  try std.testing.expectEqualStrings("WidgetTextTooLarge", errors[errors.len - 1].error_name);
2017
3683
 
2018
- // The next interaction dispatches clean.
3684
+ // The redo branch survived because the rejected edit never mutated
3685
+ // history, and the next interaction dispatches clean.
3686
+ try dispatchTextareaHistoryShortcut(harness, app, true);
3687
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
3688
+ try std.testing.expectEqual(filler.len + 1, retained.nodes[1].widget.text.len);
3689
+ try std.testing.expect(std.mem.indexOfScalar(u8, retained.nodes[1].widget.text, '!') != null);
3690
+
3691
+ // The direct setter has the same preflight contract: an oversized
3692
+ // replacement must not clear the timeline before it is refused.
3693
+ try dispatchTextareaHistoryShortcut(harness, app, false);
3694
+ const oversized = filler ++ burst;
3695
+ try std.testing.expectError(
3696
+ error.WidgetTextTooLarge,
3697
+ harness.runtime.views[0].setCanvasWidgetTextValue(2, &oversized),
3698
+ );
3699
+ try dispatchTextareaHistoryShortcut(harness, app, true);
3700
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
3701
+ try std.testing.expectEqual(filler.len + 1, retained.nodes[1].widget.text.len);
3702
+ try std.testing.expect(std.mem.indexOfScalar(u8, retained.nodes[1].widget.text, '!') != null);
3703
+
3704
+ // A replay itself can become too large after another widget consumes
3705
+ // capacity between Undo and Redo. Refusing that Redo must leave its
3706
+ // direction pending; after the blocker unmounts, the same Redo succeeds.
3707
+ try dispatchTextareaHistoryShortcut(harness, app, false);
3708
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
3709
+ try std.testing.expectEqual(filler.len, retained.nodes[1].widget.text.len);
3710
+
3711
+ const blocker = [_]u8{'c'} ** 505;
3712
+ const blocking_text = canvas.Widget{
3713
+ .id = 3,
3714
+ .kind = .text,
3715
+ .frame = geometry.RectF.init(12, 112, 180, 24),
3716
+ .text = &blocker,
3717
+ };
3718
+ var blocked_nodes: [3]canvas.WidgetLayoutNode = undefined;
3719
+ const blocked_layout = try canvas.layoutWidgetTree(
3720
+ .{ .kind = .stack, .children = &.{ textarea, blocking_text } },
3721
+ geometry.RectF.init(0, 0, 260, 160),
3722
+ &blocked_nodes,
3723
+ );
3724
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", blocked_layout);
3725
+ const errors_before_replay = harness.runtime.dispatchErrors().len;
3726
+ try dispatchTextareaHistoryShortcut(harness, app, true);
3727
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
3728
+ try std.testing.expectEqual(filler.len, retained.nodes[1].widget.text.len);
3729
+ const replay_errors = harness.runtime.dispatchErrors();
3730
+ try std.testing.expectEqual(errors_before_replay + 1, replay_errors.len);
3731
+ try std.testing.expectEqualStrings("WidgetTextTooLarge", replay_errors[replay_errors.len - 1].error_name);
3732
+
3733
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
3734
+ try dispatchTextareaHistoryShortcut(harness, app, true);
3735
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
3736
+ try std.testing.expectEqual(filler.len + 1, retained.nodes[1].widget.text.len);
3737
+ try std.testing.expect(std.mem.indexOfScalar(u8, retained.nodes[1].widget.text, '!') != null);
3738
+
2019
3739
  try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2020
3740
  .window_id = 1,
2021
3741
  .label = "canvas",
@@ -2056,6 +3776,94 @@ fn retainedTextSelection(harness: *TestHarness(), node_index: usize) !canvas.Tex
2056
3776
  return retained.nodes[node_index].widget.text_selection orelse error.TestExpectedSelection;
2057
3777
  }
2058
3778
 
3779
+ test "Shift-click extends a textarea selection from the placed caret" {
3780
+ const TestApp = struct {
3781
+ fn app(self: *@This()) App {
3782
+ return .{ .context = self, .name = "gpu-widget-shift-click-selection", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
3783
+ }
3784
+ };
3785
+
3786
+ const harness = try TestHarness().create(std.testing.allocator, .{});
3787
+ defer harness.destroy(std.testing.allocator);
3788
+ harness.null_platform.gpu_surfaces = true;
3789
+ var app_state: TestApp = .{};
3790
+ const app = app_state.app();
3791
+ try harness.start(app);
3792
+
3793
+ _ = try harness.runtime.createView(.{
3794
+ .window_id = 1,
3795
+ .label = "canvas",
3796
+ .kind = .gpu_surface,
3797
+ .frame = geometry.RectF.init(0, 0, 320, 200),
3798
+ });
3799
+
3800
+ const textarea = canvas.Widget{
3801
+ .id = 2,
3802
+ .kind = .textarea,
3803
+ .frame = geometry.RectF.init(12, 16, 240, 96),
3804
+ .text = "alpha beta\ngamma delta",
3805
+ .semantics = .{ .label = "Message" },
3806
+ };
3807
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
3808
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{textarea} }, geometry.RectF.init(0, 0, 320, 200), &nodes);
3809
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
3810
+ const widget = (try harness.runtime.canvasWidgetLayout(1, "canvas")).nodes[1].widget;
3811
+
3812
+ const anchor_point = pointForTextOffset(widget, .{}, 2).?;
3813
+ const focus_point = pointForTextOffset(widget, .{}, 18).?;
3814
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3815
+ .window_id = 1,
3816
+ .label = "canvas",
3817
+ .kind = .pointer_down,
3818
+ .x = anchor_point.x,
3819
+ .y = anchor_point.y,
3820
+ } });
3821
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(2), try retainedTextSelection(harness, 1));
3822
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3823
+ .window_id = 1,
3824
+ .label = "canvas",
3825
+ .kind = .pointer_up,
3826
+ .x = anchor_point.x,
3827
+ .y = anchor_point.y,
3828
+ } });
3829
+
3830
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3831
+ .window_id = 1,
3832
+ .label = "canvas",
3833
+ .kind = .pointer_down,
3834
+ .x = focus_point.x,
3835
+ .y = focus_point.y,
3836
+ .modifiers = .{ .shift = true },
3837
+ } });
3838
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 2, .focus = 18 }, try retainedTextSelection(harness, 1));
3839
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3840
+ .window_id = 1,
3841
+ .label = "canvas",
3842
+ .kind = .pointer_up,
3843
+ .x = focus_point.x,
3844
+ .y = focus_point.y,
3845
+ .modifiers = .{ .shift = true },
3846
+ } });
3847
+
3848
+ // A rapid shifted second click is a shifted double-click, not a drag
3849
+ // continuation. Its word-wise anchor comes from the standing caret;
3850
+ // no stale/default multi-click anchor may pull the selection to byte 0.
3851
+ const ms = std.time.ns_per_ms;
3852
+ const beta_point = pointForTextOffset(widget, .{}, 7).?;
3853
+ try dispatchTimedPointer(harness, app, .pointer_down, beta_point, 1_000 * ms);
3854
+ try dispatchTimedPointer(harness, app, .pointer_up, beta_point, 1_030 * ms);
3855
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3856
+ .window_id = 1,
3857
+ .label = "canvas",
3858
+ .kind = .pointer_down,
3859
+ .timestamp_ns = 1_100 * ms,
3860
+ .x = beta_point.x,
3861
+ .y = beta_point.y,
3862
+ .modifiers = .{ .shift = true },
3863
+ } });
3864
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 6, .focus = 10 }, try retainedTextSelection(harness, 1));
3865
+ }
3866
+
2059
3867
  test "double-click selects the word run under the pointer; a slow second click only moves the caret" {
2060
3868
  const TestApp = struct {
2061
3869
  fn app(self: *@This()) App {