@native-sdk/cli 0.6.3 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/build.zig +65 -91
  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 +25 -14
  6. package/packages/core/src/checker.ts +39 -0
  7. package/packages/core/src/emitter.ts +125 -1
  8. package/skill-data/native-ui/SKILL.md +23 -6
  9. package/src/platform/macos/appkit_host.m +25 -1
  10. package/src/platform/macos/cef_host.mm +24 -2
  11. package/src/platform/macos/root.zig +13 -0
  12. package/src/primitives/canvas/code.zig +1008 -0
  13. package/src/primitives/canvas/code_tests.zig +1912 -0
  14. package/src/primitives/canvas/events.zig +111 -1
  15. package/src/primitives/canvas/layout_audit.zig +3 -1
  16. package/src/primitives/canvas/markdown.zig +13 -11
  17. package/src/primitives/canvas/markdown_tests.zig +168 -6
  18. package/src/primitives/canvas/root.zig +8 -2
  19. package/src/primitives/canvas/tests.zig +1 -0
  20. package/src/primitives/canvas/text.zig +6 -0
  21. package/src/primitives/canvas/text_measure_cache.zig +4 -4
  22. package/src/primitives/canvas/text_span_tests.zig +254 -0
  23. package/src/primitives/canvas/text_spans.zig +556 -32
  24. package/src/primitives/canvas/themes/geist.zig +29 -0
  25. package/src/primitives/canvas/tokens.zig +72 -2
  26. package/src/primitives/canvas/ui.zig +400 -22
  27. package/src/primitives/canvas/ui_markup.zig +78 -7
  28. package/src/primitives/canvas/ui_markup_compiled.zig +89 -0
  29. package/src/primitives/canvas/ui_markup_compiled_tests.zig +45 -0
  30. package/src/primitives/canvas/ui_markup_contract.zig +41 -0
  31. package/src/primitives/canvas/ui_markup_view.zig +89 -0
  32. package/src/primitives/canvas/ui_markup_view_tests.zig +154 -3
  33. package/src/primitives/canvas/ui_schema.zig +30 -6
  34. package/src/primitives/canvas/ui_schema_tests.zig +13 -10
  35. package/src/primitives/canvas/ui_tests.zig +26 -0
  36. package/src/primitives/canvas/widget_invalidation.zig +4 -0
  37. package/src/primitives/canvas/widget_layout.zig +57 -8
  38. package/src/primitives/canvas/widget_layout_tests.zig +35 -0
  39. package/src/primitives/canvas/widget_metrics.zig +35 -1
  40. package/src/primitives/canvas/widget_render.zig +1077 -11
  41. package/src/primitives/canvas/widget_render_controls.zig +4 -4
  42. package/src/primitives/canvas/widget_runtime.zig +2 -0
  43. package/src/primitives/canvas/widget_text_input.zig +165 -8
  44. package/src/primitives/canvas/widget_text_select.zig +2 -2
  45. package/src/primitives/canvas/widget_tree.zig +6 -5
  46. package/src/primitives/canvas/widgets.zig +38 -0
  47. package/src/root.zig +1 -0
  48. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  49. package/src/runtime/canvas_limits.zig +13 -6
  50. package/src/runtime/canvas_widget_clipboard_tests.zig +137 -4
  51. package/src/runtime/canvas_widget_context_menu.zig +17 -2
  52. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  53. package/src/runtime/canvas_widget_events.zig +58 -21
  54. package/src/runtime/canvas_widget_runtime.zig +112 -6
  55. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  56. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  57. package/src/runtime/canvas_widget_state.zig +27 -0
  58. package/src/runtime/canvas_widget_text_tests.zig +383 -0
  59. package/src/runtime/core.zig +15 -5
  60. package/src/runtime/gpu_surface_events.zig +12 -12
  61. package/src/runtime/view.zig +61 -19
  62. package/src/runtime/view_widget_scroll.zig +48 -18
  63. package/src/runtime/view_widget_text.zig +248 -17
  64. package/src/runtime/window_view_runtime.zig +9 -0
@@ -28,6 +28,27 @@ const canvasWidgetAccessibilityActionSupported = widget_bridge.canvasWidgetAcces
28
28
  const platformWidgetAccessibilityActionKindFromCanvas = widget_bridge.platformWidgetAccessibilityActionKindFromCanvas;
29
29
  const canvasWidgetBooleanSelected = canvas_widget_runtime.canvasWidgetBooleanSelected;
30
30
 
31
+ fn canvasWidgetLayoutNeedsLargeTextStorage(layout: canvas.WidgetLayoutTree) bool {
32
+ var text_len: usize = 0;
33
+ for (layout.nodes) |node| {
34
+ const widget = node.widget;
35
+ if (widget.code_editor) return true;
36
+ text_len +|= widget.text.len +| widget.icon.len +| widget.command.len +| widget.semantics.label.len;
37
+ for (widget.spans) |span| text_len +|= span.text.len +| span.link.len;
38
+ for (widget.context_menu) |item| text_len +|= item.label.len;
39
+ for (widget.chart.series) |series| text_len +|= series.label.len;
40
+ for (widget.chart.x_labels) |label| text_len +|= label.len;
41
+ if (text_len > canvas_limits.max_canvas_widget_inline_text_bytes_per_view) return true;
42
+ }
43
+ return false;
44
+ }
45
+
46
+ fn ensureCanvasWidgetLargeTextStorageThrough(runtime: anytype, index: usize) !void {
47
+ for (runtime.views[0 .. index + 1]) |*view| {
48
+ try view.ensureLargeCanvasWidgetTextStorage(runtime.owned_allocator);
49
+ }
50
+ }
51
+
31
52
  pub fn RuntimeCanvasWidgetState(comptime Runtime: type) type {
32
53
  return struct {
33
54
  pub fn setCanvasWidgetLayout(self: *Runtime, window_id: platform.WindowId, label: []const u8, layout: canvas.WidgetLayoutTree) anyerror!platform.ViewInfo {
@@ -85,6 +106,9 @@ pub fn RuntimeCanvasWidgetState(comptime Runtime: type) type {
85
106
  armed_tween_ids[0..armed_tween_count],
86
107
  canvasWidgetPressedSplitId(self, index),
87
108
  );
109
+ if (canvasWidgetLayoutNeedsLargeTextStorage(reconciled_layout)) {
110
+ try ensureCanvasWidgetLargeTextStorageThrough(self, index);
111
+ }
88
112
  // Native scroll drivers: mark natively driven scroll
89
113
  // regions before the copy so rebuild-time clamping and display
90
114
  // emission both see the flag (engine scrollbar + engine clamp
@@ -831,6 +855,9 @@ pub fn RuntimeCanvasWidgetState(comptime Runtime: type) type {
831
855
  // field's editor never accepts a line break, whoever writes.
832
856
  const node = self.views[index].widgetLayoutTree().findById(id) orelse return error.InvalidCommand;
833
857
  const sanitized = canvas.sanitizedSingleLineTextInputEvent(node.widget.kind, edit) orelse return self.views[index].info();
858
+ if (self.views[index].canvasWidgetTextEditNeedsLargeStorage(id, sanitized)) {
859
+ try ensureCanvasWidgetLargeTextStorageThrough(self, index);
860
+ }
834
861
  const dirty = try self.views[index].applyCanvasWidgetTextEdit(id, sanitized) orelse return self.views[index].info();
835
862
  try CanvasWidgetEventMethods(Runtime).invalidateForCanvasWidgetDirty(self, index, dirty);
836
863
  _ = try CanvasWidgetDisplayMethods(Runtime).refreshCanvasWidgetDisplayListIfOwned(self, index);
@@ -78,6 +78,52 @@ test "the builder's presented-text store mirrors the per-view draw-text budget"
78
78
  );
79
79
  }
80
80
 
81
+ test "closing an earlier view transfers a later view's expanded text storage" {
82
+ const TestApp = struct {
83
+ fn app(self: *@This()) App {
84
+ return .{ .context = self, .name = "gpu-widget-expanded-storage-compaction", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
85
+ }
86
+ };
87
+
88
+ const harness = try TestHarness().create(std.testing.allocator, .{});
89
+ defer harness.destroy(std.testing.allocator);
90
+ harness.null_platform.gpu_surfaces = true;
91
+ var app_state: TestApp = .{};
92
+ try harness.start(app_state.app());
93
+
94
+ for ([_][]const u8{ "canvas-a", "canvas-b" }) |label| {
95
+ _ = try harness.runtime.createView(.{
96
+ .window_id = 1,
97
+ .label = label,
98
+ .kind = .gpu_surface,
99
+ .frame = geometry.RectF.init(0, 0, 240, 160),
100
+ });
101
+ }
102
+ const ordinary = canvas.Widget{ .id = 2, .kind = .text, .text = "ordinary" };
103
+ var ordinary_nodes: [1]canvas.WidgetLayoutNode = undefined;
104
+ const ordinary_layout = try canvas.layoutWidgetTree(ordinary, geometry.RectF.init(0, 0, 240, 160), &ordinary_nodes);
105
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas-a", ordinary_layout);
106
+
107
+ const editor = canvas.Widget{
108
+ .id = 3,
109
+ .kind = .textarea,
110
+ .text = "expanded",
111
+ .text_no_wrap = true,
112
+ .code_editor = true,
113
+ };
114
+ var editor_nodes: [1]canvas.WidgetLayoutNode = undefined;
115
+ const editor_layout = try canvas.layoutWidgetTree(editor, geometry.RectF.init(0, 0, 240, 160), &editor_nodes);
116
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas-b", editor_layout);
117
+ try std.testing.expect(harness.runtime.views[0].widget_text_bytes_heap_owned);
118
+ try std.testing.expect(harness.runtime.views[1].widget_text_bytes_heap_owned);
119
+
120
+ try harness.runtime.closeView(1, "canvas-a");
121
+ try std.testing.expectEqual(@as(usize, 1), harness.runtime.view_count);
122
+ try std.testing.expectEqualStrings("canvas-b", harness.runtime.views[0].label);
123
+ try std.testing.expect(harness.runtime.views[0].widget_text_bytes_heap_owned);
124
+ try std.testing.expectEqualStrings("expanded", harness.runtime.views[0].widgetLayoutTree().nodes[0].widget.text);
125
+ }
126
+
81
127
  test "runtime exposes retained canvas widget text geometry" {
82
128
  const TestApp = struct {
83
129
  fn app(self: *@This()) App {
@@ -3744,6 +3790,180 @@ test "a widget text budget overflow on input degrades instead of exiting" {
3744
3790
  } });
3745
3791
  }
3746
3792
 
3793
+ test "maximal numbered code fits the retained text budget" {
3794
+ const TestApp = struct {
3795
+ fn app(self: *@This()) App {
3796
+ return .{ .context = self, .name = "gpu-widget-numbered-code-budget", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
3797
+ }
3798
+ };
3799
+
3800
+ const harness = try TestHarness().create(std.testing.allocator, .{});
3801
+ defer harness.destroy(std.testing.allocator);
3802
+ harness.null_platform.gpu_surfaces = true;
3803
+ var app_state: TestApp = .{};
3804
+ try harness.start(app_state.app());
3805
+ _ = try harness.runtime.createView(.{
3806
+ .window_id = 1,
3807
+ .label = "canvas",
3808
+ .kind = .gpu_surface,
3809
+ .frame = geometry.RectF.init(0, 0, 320, 100),
3810
+ });
3811
+
3812
+ const source = try std.testing.allocator.alloc(
3813
+ u8,
3814
+ runtime_module.max_canvas_widget_text_bytes_per_view,
3815
+ );
3816
+ defer std.testing.allocator.free(source);
3817
+ @memset(source, 'x');
3818
+
3819
+ const CodeUi = canvas.Ui(enum { noop });
3820
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
3821
+ defer arena.deinit();
3822
+ var ui = CodeUi.init(arena.allocator());
3823
+ const tree = try ui.finalize(ui.code(.{
3824
+ .language = .plain,
3825
+ .line_numbers = true,
3826
+ .wrap = false,
3827
+ .width = 320,
3828
+ .height = 100,
3829
+ }, source));
3830
+ var nodes: [16]canvas.WidgetLayoutNode = undefined;
3831
+ const layout = try canvas.layoutWidgetTree(
3832
+ tree.root,
3833
+ geometry.RectF.init(0, 0, 320, 100),
3834
+ &nodes,
3835
+ );
3836
+
3837
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
3838
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
3839
+ var found_source = false;
3840
+ for (retained.nodes) |node| {
3841
+ if (node.widget.code_line_number_digits == 0) continue;
3842
+ found_source = true;
3843
+ try std.testing.expectEqual(@as(u8, 1), node.widget.code_line_number_digits);
3844
+ try std.testing.expectEqual(source.len, node.widget.text.len);
3845
+ try std.testing.expectEqualStrings(source, node.widget.text);
3846
+ }
3847
+ try std.testing.expect(found_source);
3848
+ }
3849
+
3850
+ test "editable code owns plain Tab and stamps its inferred indentation edit" {
3851
+ const TestApp = struct {
3852
+ edit_count: usize = 0,
3853
+ insertion: [8]u8 = @splat(0),
3854
+ insertion_len: usize = 0,
3855
+
3856
+ fn app(self: *@This()) App {
3857
+ return .{
3858
+ .context = self,
3859
+ .name = "gpu-widget-code-tab",
3860
+ .source = platform.WebViewSource.html("<h1>Hello</h1>"),
3861
+ .event_fn = event,
3862
+ };
3863
+ }
3864
+
3865
+ fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
3866
+ _ = runtime;
3867
+ const self: *@This() = @ptrCast(@alignCast(context));
3868
+ switch (event_value) {
3869
+ .canvas_widget_keyboard => |keyboard_event| {
3870
+ const edit = keyboard_event.keyboard.edit orelse return;
3871
+ switch (edit) {
3872
+ .insert_text => |text| {
3873
+ self.edit_count += 1;
3874
+ self.insertion_len = @min(text.len, self.insertion.len);
3875
+ @memcpy(self.insertion[0..self.insertion_len], text[0..self.insertion_len]);
3876
+ },
3877
+ else => {},
3878
+ }
3879
+ },
3880
+ else => {},
3881
+ }
3882
+ }
3883
+ };
3884
+
3885
+ const harness = try TestHarness().create(std.testing.allocator, .{});
3886
+ defer harness.destroy(std.testing.allocator);
3887
+ harness.null_platform.gpu_surfaces = true;
3888
+ var app_state: TestApp = .{};
3889
+ const app = app_state.app();
3890
+ try harness.start(app);
3891
+ _ = try harness.runtime.createView(.{
3892
+ .window_id = 1,
3893
+ .label = "canvas",
3894
+ .kind = .gpu_surface,
3895
+ .frame = geometry.RectF.init(0, 0, 320, 180),
3896
+ });
3897
+
3898
+ const source = "if (ready) {\n run();\n}\n";
3899
+ const children = [_]canvas.Widget{
3900
+ .{
3901
+ .id = 3,
3902
+ .kind = .button,
3903
+ .frame = geometry.RectF.init(240, 16, 68, 32),
3904
+ .text = "Run",
3905
+ },
3906
+ .{
3907
+ .id = 2,
3908
+ .kind = .textarea,
3909
+ .code_editor = true,
3910
+ .text_no_wrap = true,
3911
+ .frame = geometry.RectF.init(12, 16, 220, 100),
3912
+ .text = source,
3913
+ .text_selection = canvas.TextSelection.collapsed(source.len),
3914
+ },
3915
+ };
3916
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
3917
+ const layout = try canvas.layoutWidgetTree(
3918
+ .{ .id = 1, .kind = .stack, .children = &children },
3919
+ geometry.RectF.init(0, 0, 320, 180),
3920
+ &nodes,
3921
+ );
3922
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
3923
+
3924
+ // Tab from the preceding button enters the editor as focus
3925
+ // traversal and must not indent on that same physical gesture.
3926
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3927
+ .window_id = 1,
3928
+ .label = "canvas",
3929
+ .kind = .pointer_down,
3930
+ .x = 260,
3931
+ .y = 28,
3932
+ } });
3933
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3934
+ .window_id = 1,
3935
+ .label = "canvas",
3936
+ .kind = .key_down,
3937
+ .key = "tab",
3938
+ } });
3939
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
3940
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
3941
+ try std.testing.expectEqualStrings(source, retained.nodes[2].widget.text);
3942
+ try std.testing.expectEqual(@as(usize, 0), app_state.edit_count);
3943
+ try std.testing.expect(harness.runtime.views[0].canvas_widget_tab_input_focus_entry_held);
3944
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3945
+ .window_id = 1,
3946
+ .label = "canvas",
3947
+ .kind = .key_up,
3948
+ .key = "tab",
3949
+ } });
3950
+ try std.testing.expect(!harness.runtime.views[0].canvas_widget_tab_input_focus_entry_held);
3951
+
3952
+ // Once focused, plain Tab is an ordinary stamped edit. Four-space
3953
+ // indentation wins from the source's existing leading whitespace.
3954
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
3955
+ .window_id = 1,
3956
+ .label = "canvas",
3957
+ .kind = .key_down,
3958
+ .key = "tab",
3959
+ } });
3960
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
3961
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
3962
+ try std.testing.expectEqualStrings("if (ready) {\n run();\n}\n ", retained.nodes[2].widget.text);
3963
+ try std.testing.expectEqual(@as(usize, 1), app_state.edit_count);
3964
+ try std.testing.expectEqualStrings(" ", app_state.insertion[0..app_state.insertion_len]);
3965
+ }
3966
+
3747
3967
  /// Scan the widget's frame for a pointer location whose caret offset is
3748
3968
  /// exactly `target`, so multi-click tests aim at text offsets without
3749
3969
  /// hard-coding font metrics.
@@ -3776,6 +3996,93 @@ fn retainedTextSelection(harness: *TestHarness(), node_index: usize) !canvas.Tex
3776
3996
  return retained.nodes[node_index].widget.text_selection orelse error.TestExpectedSelection;
3777
3997
  }
3778
3998
 
3999
+ test "multi-click chains stay on one target and one physical pointer" {
4000
+ const TestApp = struct {
4001
+ fn app(self: *@This()) App {
4002
+ return .{ .context = self, .name = "gpu-widget-click-chain-identity", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
4003
+ }
4004
+ };
4005
+
4006
+ const harness = try TestHarness().create(std.testing.allocator, .{});
4007
+ defer harness.destroy(std.testing.allocator);
4008
+ harness.null_platform.gpu_surfaces = true;
4009
+ var app_state: TestApp = .{};
4010
+ const app = app_state.app();
4011
+ try harness.start(app);
4012
+
4013
+ _ = try harness.runtime.createView(.{
4014
+ .window_id = 1,
4015
+ .label = "canvas",
4016
+ .kind = .gpu_surface,
4017
+ .frame = geometry.RectF.init(0, 0, 320, 200),
4018
+ });
4019
+ const nodes = [_]canvas.WidgetLayoutNode{
4020
+ .{
4021
+ .widget = .{ .id = 2, .kind = .button, .text = "First" },
4022
+ .frame = geometry.RectF.init(0, 0, 20, 24),
4023
+ .depth = 0,
4024
+ },
4025
+ .{
4026
+ .widget = .{ .id = 3, .kind = .button, .text = "Second" },
4027
+ .frame = geometry.RectF.init(20, 0, 20, 24),
4028
+ .depth = 0,
4029
+ },
4030
+ };
4031
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", .{ .nodes = &nodes });
4032
+
4033
+ const ms = std.time.ns_per_ms;
4034
+ const clicks = [_]struct { kind: platform.GpuSurfaceInputKind, timestamp_ns: u64, x: f32, pointer_id: u64 }{
4035
+ .{ .kind = .pointer_down, .timestamp_ns = 1_000 * ms, .x = 19, .pointer_id = 11 },
4036
+ .{ .kind = .pointer_up, .timestamp_ns = 1_030 * ms, .x = 19, .pointer_id = 11 },
4037
+ .{ .kind = .pointer_down, .timestamp_ns = 1_100 * ms, .x = 21, .pointer_id = 11 },
4038
+ };
4039
+ for (clicks) |click| {
4040
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4041
+ .window_id = 1,
4042
+ .label = "canvas",
4043
+ .kind = click.kind,
4044
+ .timestamp_ns = click.timestamp_ns,
4045
+ .pointer_id = click.pointer_id,
4046
+ .x = click.x,
4047
+ .y = 12,
4048
+ } });
4049
+ }
4050
+ try std.testing.expectEqual(@as(u8, 1), harness.runtime.views[0].canvas_widget_click_count);
4051
+ try std.testing.expectEqual(@as(canvas.ObjectId, 3), harness.runtime.views[0].canvas_widget_click_target_id);
4052
+
4053
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4054
+ .window_id = 1,
4055
+ .label = "canvas",
4056
+ .kind = .pointer_up,
4057
+ .timestamp_ns = 1_130 * ms,
4058
+ .pointer_id = 11,
4059
+ .x = 21,
4060
+ .y = 12,
4061
+ } });
4062
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4063
+ .window_id = 1,
4064
+ .label = "canvas",
4065
+ .kind = .pointer_down,
4066
+ .timestamp_ns = 1_200 * ms,
4067
+ .pointer_id = 11,
4068
+ .x = 22,
4069
+ .y = 12,
4070
+ } });
4071
+ try std.testing.expectEqual(@as(u8, 2), harness.runtime.views[0].canvas_widget_click_count);
4072
+
4073
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4074
+ .window_id = 1,
4075
+ .label = "canvas",
4076
+ .kind = .pointer_down,
4077
+ .timestamp_ns = 1_300 * ms,
4078
+ .pointer_id = 12,
4079
+ .x = 22,
4080
+ .y = 12,
4081
+ } });
4082
+ try std.testing.expectEqual(@as(u8, 1), harness.runtime.views[0].canvas_widget_click_count);
4083
+ try std.testing.expectEqual(@as(u64, 12), harness.runtime.views[0].canvas_widget_click_pointer_id);
4084
+ }
4085
+
3779
4086
  test "Shift-click extends a textarea selection from the placed caret" {
3780
4087
  const TestApp = struct {
3781
4088
  fn app(self: *@This()) App {
@@ -4288,3 +4595,79 @@ test "runtime scrolls single-line fields horizontally to keep the caret visible"
4288
4595
  try std.testing.expectEqualStrings("short", retained.nodes[1].widget.text);
4289
4596
  try std.testing.expectEqual(@as(f32, 0), retained.nodes[1].widget.value);
4290
4597
  }
4598
+
4599
+ test "runtime scrolls editable no-wrap code horizontally and retains both axes" {
4600
+ const TestApp = struct {
4601
+ fn app(self: *@This()) App {
4602
+ return .{ .context = self, .name = "gpu-widget-code-editor-scroll", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
4603
+ }
4604
+ };
4605
+
4606
+ const harness = try TestHarness().create(std.testing.allocator, .{});
4607
+ defer harness.destroy(std.testing.allocator);
4608
+ harness.null_platform.gpu_surfaces = true;
4609
+ var app_state: TestApp = .{};
4610
+ const app = app_state.app();
4611
+ try harness.start(app);
4612
+
4613
+ _ = try harness.runtime.createView(.{
4614
+ .window_id = 1,
4615
+ .label = "canvas",
4616
+ .kind = .gpu_surface,
4617
+ .frame = geometry.RectF.init(0, 0, 260, 160),
4618
+ });
4619
+
4620
+ const editor = canvas.Widget{
4621
+ .id = 2,
4622
+ .kind = .textarea,
4623
+ .code_editor = true,
4624
+ .code_line_number_digits = 2,
4625
+ .text_no_wrap = true,
4626
+ .frame = geometry.RectF.init(12, 16, 120, 72),
4627
+ .semantics = .{ .label = "Source" },
4628
+ };
4629
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
4630
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{editor} }, geometry.RectF.init(0, 0, 260, 160), &nodes);
4631
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
4632
+
4633
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
4634
+ .window_id = 1,
4635
+ .label = "canvas",
4636
+ .kind = .pointer_down,
4637
+ .x = 20,
4638
+ .y = 30,
4639
+ } });
4640
+
4641
+ // The pinned line-number gutter is outside the source viewport and must
4642
+ // not manufacture a horizontal range of its own.
4643
+ try harness.runtime.dispatchAutomationCommand(app, "widget-wheel canvas 2 0 40");
4644
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
4645
+ try std.testing.expectEqual(@as(f32, 0), retained.nodes[1].widget.value_x);
4646
+
4647
+ _ = try harness.runtime.editCanvasWidgetText(
4648
+ 1,
4649
+ "canvas",
4650
+ 2,
4651
+ .{ .insert_text = "const deliberately_long_identifier = another_deliberately_long_identifier;" },
4652
+ );
4653
+
4654
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
4655
+ try std.testing.expect(retained.nodes[1].widget.value_x > 0);
4656
+ try std.testing.expectEqual(@as(f32, 0), retained.nodes[1].widget.value);
4657
+ const viewport = canvas.textInputViewportForWidget(retained.nodes[1].widget, .{}).?;
4658
+ const geometry_value = try harness.runtime.canvasWidgetTextGeometry(1, "canvas", 2);
4659
+ const caret = geometry_value.caret_bounds.?;
4660
+ try std.testing.expect(caret.x >= viewport.x - 0.001);
4661
+ try std.testing.expect(caret.maxX() <= viewport.maxX() + 0.001);
4662
+
4663
+ var rebuild_nodes: [2]canvas.WidgetLayoutNode = undefined;
4664
+ const rebuild = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{editor} }, geometry.RectF.init(0, 0, 260, 160), &rebuild_nodes);
4665
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", rebuild);
4666
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
4667
+ try std.testing.expect(retained.nodes[1].widget.value_x > 0);
4668
+
4669
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .move_caret = .{ .direction = .start } });
4670
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
4671
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(0), retained.nodes[1].widget.text_selection.?);
4672
+ try std.testing.expectEqual(@as(f32, 0), retained.nodes[1].widget.value_x);
4673
+ }
@@ -183,7 +183,8 @@ pub const Runtime = struct {
183
183
  options: Options,
184
184
  /// The allocator that owns every runtime-lifetime heap allocation
185
185
  /// (registered canvas font bytes, registered canvas image slot
186
- /// buffers, adopted media-surface texture buffers). Captured from
186
+ /// buffers, adopted media-surface texture buffers, and expanded live-view
187
+ /// text/history pools). Captured from
187
188
  /// `Options.allocator` at init precisely
188
189
  /// because `options` is public and mutable: allocation and free of
189
190
  /// owned storage can be a whole runtime lifetime apart, and they
@@ -517,10 +518,10 @@ pub const Runtime = struct {
517
518
  canvas.bumpTextMeasureGeneration();
518
519
  }
519
520
 
520
- /// Release the runtime's heap-owned on-demand storage (registered
521
- /// canvas font bytes, registered canvas image slot buffers, and
522
- /// adopted media-surface texture buffers, allocated from the
523
- /// init-frozen `owned_allocator`), return each registered font's
521
+ /// Release the runtime's heap-owned on-demand storage (expanded live-view
522
+ /// text/history pools, registered canvas font bytes, registered canvas
523
+ /// image slot buffers, and adopted media-surface texture buffers,
524
+ /// allocated from the init-frozen `owned_allocator`), return each registered font's
524
525
  /// host-side registration (through the unregister owner captured
525
526
  /// into the entry at registration time — host font state is
526
527
  /// per-process, this runtime's ids are not, and live `options` may
@@ -537,6 +538,15 @@ pub const Runtime = struct {
537
538
  /// host) call it to return the storage.
538
539
  pub fn deinit(self: *Runtime) void {
539
540
  self.disarmMediaSurfaceWakes();
541
+ for (self.views[0..self.view_count]) |*view| {
542
+ if (view.widget_text_bytes_heap_owned) self.owned_allocator.free(view.widget_text_bytes);
543
+ if (view.canvas_widget_text_history_bytes_heap_owned) self.owned_allocator.free(view.canvas_widget_text_history_bytes);
544
+ view.widget_text_bytes = &.{};
545
+ view.widget_text_bytes_heap_owned = false;
546
+ view.canvas_widget_text_history_bytes = &.{};
547
+ view.canvas_widget_text_history_bytes_heap_owned = false;
548
+ }
549
+ self.view_count = 0;
540
550
  if (self.targetless_ime_preedit.len > 0) {
541
551
  self.owned_allocator.free(self.targetless_ime_preedit);
542
552
  self.targetless_ime_preedit = &.{};
@@ -353,30 +353,30 @@ pub fn RuntimeGpuSurfaceEvents(comptime Runtime: type) type {
353
353
  self.targetless_ime_preedit_label[0..self.targetless_ime_preedit_label_len],
354
354
  input_event.label,
355
355
  );
356
- // A Tab key-down that moved focus INTO a live terminal is a
357
- // focus gesture for its whole physical lifetime. Suppress
358
- // its auto-repeats and release before any widget pass can
359
- // reinterpret them against the newly focused terminal.
360
- const terminal_focus_entry_tab_suppressed =
361
- CanvasWidgetEventMethods().consumeCanvasWidgetTerminalFocusEntryTab(self, input_event);
356
+ // A Tab key-down that moved focus INTO a Tab-owning editor is
357
+ // a focus gesture for its whole physical lifetime. Suppress
358
+ // repeats and release before a live terminal or editable code
359
+ // can reinterpret them as input.
360
+ const tab_input_focus_entry_suppressed =
361
+ CanvasWidgetEventMethods().consumeCanvasWidgetTabInputFocusEntry(self, input_event);
362
362
  // Terminal Paste owns its matching physical V release even
363
363
  // when Command/Ctrl came up first. Classify it before focus,
364
364
  // routing, or app fallbacks can reinterpret the orphan.
365
365
  const terminal_paste_release_suppressed =
366
366
  CanvasWidgetEventMethods().consumeCanvasWidgetTerminalPasteKeyLifetime(self, input_event);
367
- const terminal_key_lifetime_suppressed =
368
- terminal_focus_entry_tab_suppressed or terminal_paste_release_suppressed;
369
- const keyboard_dismissed_id = if (targetless_composition_owns_keys or terminal_key_lifetime_suppressed)
367
+ const widget_key_lifetime_suppressed =
368
+ tab_input_focus_entry_suppressed or terminal_paste_release_suppressed;
369
+ const keyboard_dismissed_id = if (targetless_composition_owns_keys or widget_key_lifetime_suppressed)
370
370
  0
371
371
  else
372
372
  try CanvasWidgetEventMethods().dismissCanvasWidgetSurfaceFromKeyboardInput(self, input_event);
373
373
  if (keyboard_dismissed_id != 0) dismissed_surface_id = keyboard_dismissed_id;
374
374
  const widget_surface_dismissed = keyboard_dismissed_id != 0;
375
- const widget_focus_moved = if (widget_surface_dismissed or targetless_composition_owns_keys or terminal_key_lifetime_suppressed)
375
+ const widget_focus_moved = if (widget_surface_dismissed or targetless_composition_owns_keys or widget_key_lifetime_suppressed)
376
376
  false
377
377
  else
378
378
  try CanvasWidgetEventMethods().updateCanvasWidgetFocusFromKeyboardInput(self, input_event);
379
- var widget_keyboard_event = if (widget_surface_dismissed or targetless_composition_owns_keys or terminal_key_lifetime_suppressed)
379
+ var widget_keyboard_event = if (widget_surface_dismissed or targetless_composition_owns_keys or widget_key_lifetime_suppressed)
380
380
  null
381
381
  else
382
382
  CanvasWidgetEventMethods().routeCanvasWidgetKeyboardInput(self, input_event, &self.widget_event_route_entries) catch |err| switch (err) {
@@ -676,7 +676,7 @@ pub fn RuntimeGpuSurfaceEvents(comptime Runtime: type) type {
676
676
  try self.dispatchEvent(app, .{ .canvas_widget_keyboard = followup });
677
677
  }
678
678
  } else if ((input_event.kind == .key_down or input_event.kind == .key_up) and
679
- !widget_surface_dismissed and !terminal_key_lifetime_suppressed)
679
+ !widget_surface_dismissed and !widget_key_lifetime_suppressed)
680
680
  {
681
681
  // No focused widget routed this key (nothing is
682
682
  // focused, or the focused id is gone from the tree): the