@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
@@ -1479,7 +1479,7 @@ fn widgetIconGlyphScale(widget: Widget) f32 {
1479
1479
  };
1480
1480
  }
1481
1481
 
1482
- fn emitWidgetTextSelectionRects(
1482
+ pub fn emitWidgetTextSelectionRects(
1483
1483
  builder: *Builder,
1484
1484
  widget: Widget,
1485
1485
  text: DrawText,
@@ -1513,7 +1513,7 @@ fn emitWidgetTextSelectionRects(
1513
1513
  /// (re-shaping the selected substring instead would re-kern at the
1514
1514
  /// segment boundary and shift the halves apart). Runs after the base
1515
1515
  /// text so the recolored glyphs paint over their normal-ink twins.
1516
- fn emitWidgetTextSelectedGlyphs(
1516
+ pub fn emitWidgetTextSelectedGlyphs(
1517
1517
  builder: *Builder,
1518
1518
  widget: Widget,
1519
1519
  text: DrawText,
@@ -1550,7 +1550,7 @@ fn textSelectionOverlayCommandId(seed: u64, widget_id: ObjectId, ordinal: usize)
1550
1550
  return if (value == 0) 1 else value;
1551
1551
  }
1552
1552
 
1553
- fn emitWidgetTextCompositionLines(
1553
+ pub fn emitWidgetTextCompositionLines(
1554
1554
  builder: *Builder,
1555
1555
  widget: Widget,
1556
1556
  text: DrawText,
@@ -1587,7 +1587,7 @@ fn widgetTextRangePart(first_part: ObjectId, overflow_first_part: ObjectId, inde
1587
1587
  return overflow_first_part + @as(ObjectId, @intCast(index - 1));
1588
1588
  }
1589
1589
 
1590
- fn emitWidgetTextCaret(
1590
+ pub fn emitWidgetTextCaret(
1591
1591
  builder: *Builder,
1592
1592
  widget: Widget,
1593
1593
  text: DrawText,
@@ -49,6 +49,8 @@ pub const textInputViewportForWidget = widget_text_input.textInputViewportForWid
49
49
  pub const textInputClearButtonRect = widget_text_input.textInputClearButtonRect;
50
50
  pub const textInputClearButtonHitRect = widget_text_input.textInputClearButtonHitRect;
51
51
  pub const textInputContentExtentForWidget = widget_text_input.textInputContentExtentForWidget;
52
+ pub const textInputContentWidthForWidget = widget_text_input.textInputContentWidthForWidget;
53
+ pub const cacheTextInputContentWidthForWidget = widget_text_input.cacheTextInputContentWidthForWidget;
52
54
  pub const textInputMaxScrollOffsetForWidget = widget_text_input.textInputMaxScrollOffsetForWidget;
53
55
  pub const clampedTextInputScrollOffsetForWidget = widget_text_input.clampedTextInputScrollOffsetForWidget;
54
56
  pub const textInputMaxHorizontalScrollOffsetForWidget = widget_text_input.textInputMaxHorizontalScrollOffsetForWidget;
@@ -6,6 +6,7 @@ const token_model = @import("tokens.zig");
6
6
  const widget_model = @import("widgets.zig");
7
7
  const widget_access = @import("widget_access.zig");
8
8
  const widget_metrics = @import("widget_metrics.zig");
9
+ const text_measure_cache = @import("text_measure_cache.zig");
9
10
 
10
11
  const FontId = @import("root.zig").FontId;
11
12
  const Builder = @import("commands.zig").Builder;
@@ -85,12 +86,12 @@ pub fn widgetTextInputTextNeedsPresentation(widget: Widget) bool {
85
86
  /// scroll measures) substitute into. Every entry point re-derives it,
86
87
  /// and within one widget's computation repeated derivations write
87
88
  /// identical bytes, so the aliasing is harmless. Sized to the runtime's
88
- /// per-view widget-text budget (65536). Nothing EMITTED may retain a
89
+ /// per-view widget-text budget. Nothing EMITTED may retain a
89
90
  /// slice into it: render emitters persist presented bytes into the
90
91
  /// display-list builder via `persistWidgetTextInputPresentedText`, whose
91
92
  /// storage lives exactly as long as the emitted commands do.
92
93
  const WidgetTextPresentationScratch = struct {
93
- slot: [65536]u8,
94
+ slot: [text_model.max_widget_text_bytes_per_view]u8,
94
95
  };
95
96
  const widget_text_presentation_scratch = @import("lazy_tls.zig").LazyTls(WidgetTextPresentationScratch);
96
97
 
@@ -113,6 +114,18 @@ fn presentedSingleLineText(text: []const u8) []const u8 {
113
114
  return scratch.slot[0..text.len];
114
115
  }
115
116
 
117
+ test "single-line presentation rewrites values above the former 64 KiB ceiling" {
118
+ const input = try std.testing.allocator.alloc(u8, 65537);
119
+ defer std.testing.allocator.free(input);
120
+ @memset(input, 'a');
121
+ input[32768] = '\n';
122
+
123
+ const presented = widgetTextInputPresentedText(.{ .kind = .input, .text = input });
124
+ try std.testing.expectEqual(input.len, presented.len);
125
+ try std.testing.expectEqual(@as(u8, ' '), presented[32768]);
126
+ try std.testing.expect(std.mem.indexOfAny(u8, presented, "\r\n") == null);
127
+ }
128
+
116
129
  /// Persist presented bytes into the display-list BUILDER so an emitted
117
130
  /// command outlives the shared slot (a later widget's presentation
118
131
  /// overwrites it) — builder-owned storage, because the builder contract
@@ -178,12 +191,14 @@ fn widgetTextInputLineHeight(text_size: f32) f32 {
178
191
  }
179
192
 
180
193
  fn widgetTextInputWrap(widget: Widget, line_height: f32) TextWrap {
194
+ if (widget.code_editor) return if (widget.text_no_wrap) .none else .word;
181
195
  if (widget.kind == .textarea) return .word;
182
196
  if (widget.kind == .text_field and widget.frame.height >= line_height * 2.25) return .word;
183
197
  return .none;
184
198
  }
185
199
 
186
200
  fn widgetTextInputVerticalInset(widget: Widget, tokens: DesignTokens, text_size: f32, options: TextLayoutOptions) f32 {
201
+ if (widget.code_editor) return 0;
187
202
  if (options.wrap != .none) return widget_metrics.widgetControlInset(widget, tokens, tokens.spacing.sm);
188
203
  return @max(0, (widget.frame.height - widgetTextInputLineHeight(text_size)) * 0.5);
189
204
  }
@@ -205,7 +220,8 @@ const text_input_caret_reserve: f32 = 1;
205
220
  /// unscrolled field always has.
206
221
  fn widgetTextInputHorizontalScrollOffset(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) f32 {
207
222
  if (options.wrap != .none) return 0;
208
- return std.math.clamp(widget.value, 0, widgetTextInputMaxHorizontalScrollOffset(widget, tokens, text_size, text_inset, options));
223
+ const offset = if (widget.code_editor) widget.value_x else widget.value;
224
+ return std.math.clamp(offset, 0, widgetTextInputMaxHorizontalScrollOffset(widget, tokens, text_size, text_inset, options));
209
225
  }
210
226
 
211
227
  fn widgetTextInputMaxHorizontalScrollOffset(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) f32 {
@@ -215,11 +231,24 @@ fn widgetTextInputMaxHorizontalScrollOffset(widget: Widget, tokens: DesignTokens
215
231
  // Measure the PRESENTED bytes — the ones the field lays out and
216
232
  // paints — so a value holding a line break scrolls by the same
217
233
  // single-line extent it draws.
218
- const text_width = measureTextWidthForFont(options.measure, tokens.typography.font_id, widgetTextInputPresentedText(widget), text_size);
234
+ const font_id = widgetTextInputFontId(widget, tokens);
235
+ const text_width = if (widget.code_editor)
236
+ if (codeContentWidthCacheCurrent(widget, font_id, text_size))
237
+ widget.code_content_width
238
+ else
239
+ widestLogicalLineWidth(options.measure, font_id, widget.text, text_size)
240
+ else
241
+ measureTextWidthForFont(options.measure, font_id, widgetTextInputPresentedText(widget), text_size);
219
242
  return @max(0, text_width + text_input_caret_reserve - viewport.width);
220
243
  }
221
244
 
222
245
  pub fn widgetTextInputOrigin(widget: Widget, tokens: DesignTokens, text_size: f32, inset: f32, options: TextLayoutOptions) geometry.PointF {
246
+ if (widget.code_editor) {
247
+ return geometry.PointF.init(
248
+ widget.frame.x + inset - widgetTextInputHorizontalScrollOffset(widget, tokens, text_size, inset, options),
249
+ widget.frame.y + text_size - widgetTextInputScrollOffset(widget, tokens, text_size, inset, options),
250
+ );
251
+ }
223
252
  if (options.wrap != .none) {
224
253
  const scroll_offset = widgetTextInputScrollOffset(widget, tokens, text_size, inset, options);
225
254
  return geometry.PointF.init(
@@ -263,7 +292,20 @@ pub fn textInputContentExtentForWidget(widget: Widget, tokens: DesignTokens) f32
263
292
  const text_inset = widgetTextInputInset(widget, tokens);
264
293
  const options = widgetTextInputLayoutOptions(widget, tokens, text_size, text_inset);
265
294
  const line_height = widgetTextInputLineHeight(text_size);
266
- return @as(f32, @floatFromInt(widgetTextInputLineCount(widget, tokens.typography.font_id, text_size, options))) * line_height;
295
+ return @as(f32, @floatFromInt(widgetTextInputLineCount(widget, widgetTextInputFontId(widget, tokens), text_size, options))) * line_height;
296
+ }
297
+
298
+ pub fn textInputContentWidthForWidget(widget: Widget, tokens: DesignTokens) f32 {
299
+ if (!widget_access.widgetTextInputKind(widget.kind)) return 0;
300
+ const text_size = widgetTextInputSize(widget, tokens);
301
+ const inset = widgetTextInputInset(widget, tokens);
302
+ const options = widgetTextInputLayoutOptions(widget, tokens, text_size, inset);
303
+ const viewport = widgetTextInputClipRect(widget, tokens, text_size, inset, options);
304
+ // Content and viewport share the source-text coordinate space. In a
305
+ // numbered editor the outer frame also contains the pinned gutter;
306
+ // counting that frame here while scrolling against `viewport.width`
307
+ // manufactures a gutter-wide range even when the longest line fits.
308
+ return viewport.width + widgetTextInputMaxHorizontalScrollOffset(widget, tokens, text_size, inset, options);
267
309
  }
268
310
 
269
311
  pub fn textInputMaxScrollOffsetForWidget(widget: Widget, tokens: DesignTokens) f32 {
@@ -323,7 +365,17 @@ pub fn textInputCaretVisibleScrollOffsetForWidget(widget: Widget, tokens: Design
323
365
  // selection rects measure with — the PRESENTED bytes (byte offsets
324
366
  // are interchangeable: the presentation substitutes 1:1).
325
367
  const caret_offset = snapTextOffset(widget.text, selection.focus);
326
- const caret_x = measureTextWidthForFont(options.measure, tokens.typography.font_id, widgetTextInputPresentedText(widget)[0..caret_offset], text_size);
368
+ const caret_line_start = if (widget.code_editor)
369
+ (std.mem.lastIndexOfScalar(u8, widget.text[0..caret_offset], '\n') orelse 0) +
370
+ @as(usize, @intFromBool(std.mem.lastIndexOfScalar(u8, widget.text[0..caret_offset], '\n') != null))
371
+ else
372
+ 0;
373
+ const caret_x = measureTextWidthForFont(
374
+ options.measure,
375
+ widgetTextInputFontId(widget, tokens),
376
+ widgetTextInputPresentedText(widget)[caret_line_start..caret_offset],
377
+ text_size,
378
+ );
327
379
  const margin = textInputCaretVisibleMargin(viewport.width);
328
380
  var next = clamped;
329
381
  const scrolled_in = caret_x + text_input_caret_reserve + margin - viewport.width;
@@ -358,7 +410,7 @@ pub fn widgetTextInputClipsText(widget: Widget, tokens: DesignTokens, text_size:
358
410
 
359
411
  fn widgetTextInputMaxScrollOffset(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) f32 {
360
412
  const viewport = widgetTextInputClipRect(widget, tokens, text_size, text_inset, options);
361
- return @max(0, textInputContentExtentForWidgetWithOptions(widget, tokens.typography.font_id, text_size, options) - viewport.height);
413
+ return @max(0, textInputContentExtentForWidgetWithOptions(widget, widgetTextInputFontId(widget, tokens), text_size, options) - viewport.height);
362
414
  }
363
415
 
364
416
  fn textInputContentExtentForWidgetWithOptions(widget: Widget, font_id: FontId, text_size: f32, options: TextLayoutOptions) f32 {
@@ -397,7 +449,7 @@ pub fn widgetTextInputDrawText(
397
449
  options: TextLayoutOptions,
398
450
  ) DrawText {
399
451
  return .{
400
- .font_id = tokens.typography.font_id,
452
+ .font_id = widgetTextInputFontId(widget, tokens),
401
453
  .size = text_size,
402
454
  .origin = pixelSnapTextPoint(tokens, origin),
403
455
  .color = color,
@@ -412,6 +464,7 @@ pub fn widgetTextInputDrawText(
412
464
  }
413
465
 
414
466
  pub fn widgetTextInputInset(widget: Widget, tokens: DesignTokens) f32 {
467
+ if (widget.code_editor) return widget_metrics.widgetCodeLineNumberGutterWidth(widget, tokens);
415
468
  const text_size = widgetTextInputSize(widget, tokens);
416
469
  return switch (widget.kind) {
417
470
  .search_field, .combobox => widget_metrics.widgetControlInset(widget, tokens, tokens.spacing.md) + @max(widget_metrics.widgetSizedDensityValue(widget, tokens, 8), text_size - 2) + widget_metrics.widgetControlInset(widget, tokens, tokens.spacing.sm),
@@ -419,7 +472,111 @@ pub fn widgetTextInputInset(widget: Widget, tokens: DesignTokens) f32 {
419
472
  };
420
473
  }
421
474
 
475
+ fn widgetTextInputFontId(widget: Widget, tokens: DesignTokens) FontId {
476
+ return if (widget.code_editor) tokens.typography.mono_font_id else tokens.typography.font_id;
477
+ }
478
+
479
+ fn codeContentWidthCacheCurrent(widget: Widget, font_id: FontId, text_size: f32) bool {
480
+ return widget.code_content_width_generation == text_measure_cache.textMeasureGeneration() and
481
+ widget.code_content_width_font_id == font_id and
482
+ widget.code_content_width_size_bits == @as(u32, @bitCast(text_size)) and
483
+ widget.code_content_width >= 0 and
484
+ std.math.isFinite(widget.code_content_width);
485
+ }
486
+
487
+ /// Populate the retained longest-line width once per source/font generation.
488
+ /// Runtime reconciliation carries this cache across unchanged app rebuilds;
489
+ /// edits invalidate it before caret scrolling recomputes the new document.
490
+ pub fn cacheTextInputContentWidthForWidget(widget: *Widget, tokens: DesignTokens) void {
491
+ if (widget.kind != .textarea or !widget.code_editor or !widget.text_no_wrap) return;
492
+ const text_size = widgetTextInputSize(widget.*, tokens);
493
+ const font_id = widgetTextInputFontId(widget.*, tokens);
494
+ if (codeContentWidthCacheCurrent(widget.*, font_id, text_size)) return;
495
+ widget.code_content_width = widestLogicalLineWidth(tokens.text_measure, font_id, widget.text, text_size);
496
+ widget.code_content_width_generation = text_measure_cache.textMeasureGeneration();
497
+ widget.code_content_width_font_id = font_id;
498
+ widget.code_content_width_size_bits = @bitCast(text_size);
499
+ }
500
+
501
+ fn widestLogicalLineWidth(
502
+ measure: ?*const text_model.TextMeasureProvider,
503
+ font_id: FontId,
504
+ text: []const u8,
505
+ text_size: f32,
506
+ ) f32 {
507
+ if (measure) |provider| {
508
+ if (widestLogicalLineWidthBatched(provider, font_id, text, text_size)) |width| return width;
509
+ }
510
+
511
+ var widest: f32 = 0;
512
+ var start: usize = 0;
513
+ while (start <= text.len) {
514
+ const end = std.mem.indexOfScalarPos(u8, text, start, '\n') orelse text.len;
515
+ widest = @max(widest, measureTextWidthForFont(measure, font_id, text[start..end], text_size));
516
+ if (end == text.len) break;
517
+ start = end + 1;
518
+ }
519
+ return widest;
520
+ }
521
+
522
+ /// Measure many ordinary source lines in one provider round-trip. Chunks end
523
+ /// at a newline so shaping never crosses a logical-line boundary. A single
524
+ /// extraordinary line above the batched scratch bound takes one width call;
525
+ /// the common 10,000-line document takes only a handful of batched calls.
526
+ fn widestLogicalLineWidthBatched(
527
+ provider: *const text_model.TextMeasureProvider,
528
+ font_id: FontId,
529
+ text: []const u8,
530
+ text_size: f32,
531
+ ) ?f32 {
532
+ if (provider.measure_advances_fn == null) return null;
533
+ if (text.len == 0) return 0;
534
+
535
+ var widest: f32 = 0;
536
+ var chunk_start: usize = 0;
537
+ while (chunk_start < text.len) {
538
+ const limit_end = @min(
539
+ text.len,
540
+ chunk_start +| text_measure_cache.max_batched_advance_run_bytes,
541
+ );
542
+ var chunk_end = limit_end;
543
+ if (limit_end < text.len) {
544
+ const bounded = text[chunk_start..limit_end];
545
+ if (std.mem.lastIndexOfScalar(u8, bounded, '\n')) |newline| {
546
+ chunk_end = chunk_start + newline + 1;
547
+ } else {
548
+ const line_end = std.mem.indexOfScalarPos(u8, text, chunk_start, '\n') orelse text.len;
549
+ widest = @max(
550
+ widest,
551
+ measureTextWidthForFont(provider, font_id, text[chunk_start..line_end], text_size),
552
+ );
553
+ chunk_start = if (line_end < text.len) line_end + 1 else line_end;
554
+ continue;
555
+ }
556
+ }
557
+
558
+ const chunk = text[chunk_start..chunk_end];
559
+ const advances = text_measure_cache.textRunAdvances(provider, font_id, text_size, chunk) orelse return null;
560
+ var line_start: usize = 0;
561
+ for (chunk, 0..) |byte, index| {
562
+ if (byte != '\n') continue;
563
+ widest = @max(widest, text_measure_cache.advanceSliceWidth(advances, line_start, index));
564
+ line_start = index + 1;
565
+ }
566
+ if (line_start < chunk.len) {
567
+ widest = @max(widest, text_measure_cache.advanceSliceWidth(advances, line_start, chunk.len));
568
+ }
569
+ chunk_start = chunk_end;
570
+ }
571
+ return widest;
572
+ }
573
+
422
574
  fn widgetTextInputTrailingInset(widget: Widget, text_size: f32, inset: f32) f32 {
575
+ // A code editor's leading inset is its line-number gutter, not symmetric
576
+ // field padding. Its source viewport runs all the way to the trailing
577
+ // edge; mirroring the gutter here invents horizontal overflow for lines
578
+ // that visibly fit beside the numbers.
579
+ if (widget.code_editor) return 0;
423
580
  if (widget.kind == .combobox) return inset + @max(8, text_size - 4);
424
581
  // A search field holding text reserves the trailing slot for the
425
582
  // built-in clear affordance so the text never runs under the x.
@@ -57,7 +57,7 @@ pub fn staticTextSelectionForWidgetPoint(
57
57
  pub fn staticTextOffsetForWidgetPoint(widget: Widget, point: geometry.PointF, tokens: DesignTokens) ?usize {
58
58
  if (!widgetStaticTextSelectable(widget)) return null;
59
59
  if (widget.spans.len > 0) {
60
- const content = widget.frame.inset(widget.layout.padding);
60
+ const content = widget_metrics.widgetTextSpanContentFrame(widget, tokens);
61
61
  const options = widget_metrics.widgetTextSpanLayoutOptions(widget, tokens, content.width);
62
62
  return text_spans_model.textSpanOffsetForPoint(
63
63
  widget.text,
@@ -82,7 +82,7 @@ pub fn staticTextSelectionRects(
82
82
  ) []const TextSelectionRect {
83
83
  if (widget.kind != .text or widget.text.len == 0) return output[0..0];
84
84
  if (widget.spans.len > 0) {
85
- const content = widget.frame.inset(widget.layout.padding);
85
+ const content = widget_metrics.widgetTextSpanContentFrame(widget, tokens);
86
86
  const options = widget_metrics.widgetTextSpanLayoutOptions(widget, tokens, content.width);
87
87
  const rects = text_spans_model.textSpanSelectionRects(widget.text, widget.spans, options, range, output);
88
88
  for (output[0..rects.len]) |*rect| {
@@ -120,14 +120,15 @@ pub fn widgetVirtualRuntimeScrolled(widget: Widget) bool {
120
120
  /// their axis through it, and the scrollbar/driver emit only the axes
121
121
  /// it grants. Vertical: every scrollable kind (scroll views unless
122
122
  /// declared `horizontal`, textareas, the virtualized containers).
123
- /// Horizontal: only a non-virtualized `.scroll_view` declared
124
- /// `horizontal` or `both` — virtualization is vertical machinery
125
- /// (windowed ranges price rows, not columns), so a virtualized region
126
- /// never grants the horizontal axis.
123
+ /// Horizontal: a non-virtualized `.scroll_view` declared `horizontal` or
124
+ /// `both`, plus an editable no-wrap code textarea. Virtualization is
125
+ /// vertical machinery (windowed ranges price rows, not columns), so a
126
+ /// virtualized region never grants the horizontal axis.
127
127
  pub fn widgetScrollsAxis(widget: Widget, axis: token_model.ScrollAxis) bool {
128
128
  return switch (axis) {
129
129
  .vertical => widget.kind != .scroll_view or widget.scroll_axes.scrollsVertically() or widget.layout.virtualized,
130
- .horizontal => widget.kind == .scroll_view and widget.scroll_axes.scrollsHorizontally() and !widget.layout.virtualized,
130
+ .horizontal => (widget.kind == .scroll_view and widget.scroll_axes.scrollsHorizontally() and !widget.layout.virtualized) or
131
+ (widget.kind == .textarea and widget.code_editor and widget.text_no_wrap),
131
132
  };
132
133
  }
133
134
 
@@ -3,6 +3,7 @@ const geometry = @import("geometry");
3
3
  const canvas = @import("root.zig");
4
4
  const text_model = @import("text.zig");
5
5
  const text_spans_model = @import("text_spans.zig");
6
+ const code_model = @import("code.zig");
6
7
  const token_model = @import("tokens.zig");
7
8
  const chart_model = @import("chart.zig");
8
9
  const terminal_grid_model = @import("terminal_grid.zig");
@@ -863,6 +864,43 @@ pub const Widget = struct {
863
864
  /// tooling and write-back can round-trip it. Span paragraphs
864
865
  /// (`spans`) wrap by design and ignore it.
865
866
  text_no_wrap: bool = false,
867
+ /// Renderer-owned logical-line gutter for a syntax-code paragraph.
868
+ /// Zero keeps an ordinary paragraph; a positive value is the decimal
869
+ /// digit width of the largest marker. The gutter is decoration, not
870
+ /// retained text, so selection/copy remains the exact source bytes.
871
+ /// Stamped only by `Ui.code`; there is no generic builder/markup
872
+ /// channel for turning arbitrary paragraphs into numbered code.
873
+ code_line_number_digits: u8 = 0,
874
+ /// Optional one-based logical depth for a flat sequence of tree rows.
875
+ /// Zero keeps the structural nesting contract. A nonzero value lets a
876
+ /// `<for>` render visible rows as siblings while Left/Right still find
877
+ /// the logical parent and first child from their declared levels.
878
+ tree_level: u16 = 0,
879
+ /// Internal marker for an editable `Ui.code` surface. The widget keeps
880
+ /// the `.textarea` kind so the existing editor, IME, clipboard, undo,
881
+ /// selection, and accessibility paths all apply; this bit only swaps
882
+ /// the textarea's visual/geometry policy to bare monospace highlighted
883
+ /// code (no control fill, border, radius, focus ring, or inset).
884
+ code_editor: bool = false,
885
+ /// Syntax grammar for an editable `Ui.code` surface. Editable code
886
+ /// retains one plain source span and tokenizes only visible logical
887
+ /// lines during paint, so large documents do not consume the view's
888
+ /// bounded span pool on offscreen syntax runs.
889
+ code_language: code_model.Language = .plain,
890
+ /// Runtime-owned longest-line measurement for an editable no-wrap code
891
+ /// surface. The signature keeps the value honest across font/appearance
892
+ /// changes; source edits invalidate it before caret and scroll geometry
893
+ /// read it. These fields are renderer state, not builder options.
894
+ code_content_width: f32 = 0,
895
+ code_content_width_generation: u64 = 0,
896
+ code_content_width_font_id: u64 = 0,
897
+ code_content_width_size_bits: u32 = 0,
898
+ /// Nonzero on bounded paragraph chunks that together present one
899
+ /// selectable source-code document. The group id is the structural id
900
+ /// of their internal parent; offsets order each chunk's exact source
901
+ /// bytes without duplicating the full source in retained storage.
902
+ static_text_group_id: ObjectId = 0,
903
+ static_text_group_offset: usize = 0,
866
904
  /// What a single-line text run does with content that does not fit
867
905
  /// its frame (`ElementOptions.overflow` / markup `overflow=` on
868
906
  /// text leaves): `.ellipsis` (default) elides the tail behind a
package/src/root.zig CHANGED
@@ -6,6 +6,7 @@ pub const trace = @import("trace");
6
6
  pub const diagnostics = @import("diagnostics");
7
7
  pub const platform_info = @import("platform_info");
8
8
  pub const canvas = @import("canvas");
9
+ pub const code = canvas.code;
9
10
  pub const markdown = canvas.markdown;
10
11
 
11
12
  pub const runtime = @import("runtime/root.zig");
@@ -67,8 +67,9 @@ fn stressUpdate(model: *StressModel, msg: StressMsg) void {
67
67
  }
68
68
 
69
69
  // Static payload storage: tests are single-threaded and the view only
70
- // reads these.
71
- var text_payload: [96 * 1024]u8 = undefined;
70
+ // reads these. Size it from the live retained-text edge so raising that
71
+ // budget cannot make this test fixture fail before the runtime does.
72
+ var text_payload: [canvas_limits.max_canvas_widget_text_bytes_per_view]u8 = undefined;
72
73
  var chart_values: [256]f32 = undefined;
73
74
  var chart_low: [256]f32 = undefined;
74
75
 
@@ -1,3 +1,5 @@
1
+ const canvas = @import("canvas");
2
+
1
3
  // Per-view canvas budgets. Fixed, documented, loud: capacities are compile
2
4
  // time constants sized for a dense desktop view, overflow errors name the
3
5
  // budget, and the automation snapshot reports headroom (widget_nodes=N/MAX).
@@ -169,12 +171,16 @@ pub const max_registered_canvas_font_bytes: usize = 24 * 1024 * 1024;
169
171
  // lockstep.
170
172
  pub const max_canvas_widget_nodes_per_view: usize = 1024;
171
173
  pub const max_canvas_widget_semantics_per_view: usize = 1024;
172
- // Raised from 2048 with the inline-span/markdown work: a rendered document
173
- // retains its full plain text (paragraph bytes are stored once; span slices
174
- // rebase into them) plus link payloads, and 2048 bytes could not hold a
175
- // README-sized document. Raised again with the node-budget raise:
176
- // a 1024-node view retains proportionally more text.
177
- pub const max_canvas_widget_text_bytes_per_view: usize = 65536;
174
+ // Raised from 2048 with the inline-span/markdown work, then from 64 KiB for
175
+ // editable code surfaces: a simple editor must retain a practical source
176
+ // file (roughly 10k ordinary code lines) plus the surrounding view chrome.
177
+ // Syntax paint remains viewport-bounded, so this larger SOURCE budget does
178
+ // not enlarge the display list or retained span pools.
179
+ pub const max_canvas_widget_text_bytes_per_view: usize = canvas.max_widget_text_bytes_per_view;
180
+ /// Ordinary views retain the historical 64 KiB pools inline. A view upgrades
181
+ /// to the practical source-file ceiling above only when its adopted layout
182
+ /// actually needs it, so empty/ordinary view creation remains allocation-free.
183
+ pub const max_canvas_widget_inline_text_bytes_per_view: usize = 64 * 1024;
178
184
  pub const max_canvas_widget_source_text_entries_per_view: usize = 256;
179
185
  /// Retained editor undo/redo is delta-based: changed bytes share this
180
186
  /// per-view pool and metadata stays bounded independently. Matching the
@@ -182,6 +188,7 @@ pub const max_canvas_widget_source_text_entries_per_view: usize = 256;
182
188
  /// be undone; an unusually large whole-document replacement whose removed
183
189
  /// plus inserted bytes exceed the pool simply starts a fresh history.
184
190
  pub const max_canvas_widget_text_history_bytes_per_view: usize = max_canvas_widget_text_bytes_per_view;
191
+ pub const max_canvas_widget_inline_text_history_bytes_per_view: usize = 64 * 1024;
185
192
  pub const max_canvas_widget_text_history_entries_per_view: usize = 128;
186
193
  // Inline styled runs retained across all `.text` widgets of a view. Each
187
194
  // span is a small struct (style flags + slices into the widget text
@@ -350,16 +350,19 @@ test "paste clamps to view text capacity and flags truncation loudly" {
350
350
  const used = harness.runtime.views[0].widget_text_len;
351
351
  const available = canvas_limits.max_canvas_widget_text_bytes_per_view - used;
352
352
 
353
- // A clipboard payload over the view's REMAINING shared text storage
354
- // (the semantics label already consumed a few bytes, and the widget
355
- // text capacity now matches the clipboard bound exactly).
353
+ // Fill the view with repeated maximum clipboard payloads. The retained
354
+ // source budget is intentionally larger than one clipboard transfer;
355
+ // only the final paste clamps against the remaining shared storage.
356
356
  const big = try std.testing.allocator.alloc(u8, platform.max_clipboard_data_bytes);
357
357
  defer std.testing.allocator.free(big);
358
358
  @memset(big, 'a');
359
359
  try harness.runtime.writeClipboard(big);
360
360
 
361
361
  try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 100, 30));
362
- try harness.runtime.dispatchPlatformEvent(app, keyInput("v", cmd));
362
+ const paste_count = std.math.divCeil(usize, available, big.len) catch unreachable;
363
+ for (0..paste_count) |_| {
364
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("v", cmd));
365
+ }
363
366
 
364
367
  const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
365
368
  try std.testing.expectEqual(available, retained.nodes[1].widget.text.len);
@@ -501,6 +504,90 @@ test "static text drag selection highlights, copies, and clears" {
501
504
  try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_up, 211, 75));
502
505
  }
503
506
 
507
+ test "grouped code paragraphs select and copy across chunk boundaries" {
508
+ var app_state: ClipboardTestApp = .{};
509
+ const app = app_state.app();
510
+ const harness = try createClipboardHarness(app);
511
+ defer harness.destroy(std.testing.allocator);
512
+
513
+ const first = "alpha\r\n";
514
+ const second = "beta";
515
+ const first_spans = [_]canvas.TextSpan{.{
516
+ .text = first,
517
+ .monospace = true,
518
+ .color = .syntax_plain,
519
+ }};
520
+ const second_spans = [_]canvas.TextSpan{.{
521
+ .text = second,
522
+ .monospace = true,
523
+ .color = .syntax_plain,
524
+ }};
525
+ const group_id: canvas.ObjectId = 77;
526
+ const children = [_]canvas.Widget{
527
+ .{
528
+ .id = 2,
529
+ .kind = .text,
530
+ .frame = geometry.RectF.init(12, 16, 220, 40),
531
+ .text = first,
532
+ .spans = &first_spans,
533
+ .static_text_group_id = group_id,
534
+ .static_text_group_offset = 0,
535
+ },
536
+ .{
537
+ .id = 3,
538
+ .kind = .text,
539
+ .frame = geometry.RectF.init(12, 64, 220, 40),
540
+ .text = second,
541
+ .spans = &second_spans,
542
+ .static_text_group_id = group_id,
543
+ .static_text_group_offset = first.len,
544
+ },
545
+ .{
546
+ .id = 4,
547
+ .kind = .button,
548
+ .frame = geometry.RectF.init(12, 128, 100, 32),
549
+ .text = "Clear",
550
+ },
551
+ };
552
+ var nodes: [4]canvas.WidgetLayoutNode = undefined;
553
+ const layout = try canvas.layoutWidgetTree(
554
+ .{ .kind = .stack, .children = &children },
555
+ geometry.RectF.init(0, 0, 320, 200),
556
+ &nodes,
557
+ );
558
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
559
+
560
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 13, 18));
561
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_drag, 231, 103));
562
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
563
+ try std.testing.expectEqualDeep(
564
+ canvas.TextSelection{ .anchor = 0, .focus = first.len },
565
+ retained.findById(2).?.widget.text_selection.?,
566
+ );
567
+ try std.testing.expectEqualDeep(
568
+ canvas.TextSelection{ .anchor = 0, .focus = second.len },
569
+ retained.findById(3).?.widget.text_selection.?,
570
+ );
571
+
572
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("c", cmd));
573
+ var clipboard_buffer: [32]u8 = undefined;
574
+ try std.testing.expectEqualStrings(
575
+ "alpha\r\nbeta",
576
+ try harness.runtime.readClipboard(&clipboard_buffer),
577
+ );
578
+
579
+ // Every selected chunk survives an unchanged rebuild, and pressing
580
+ // elsewhere clears the whole grouped highlight.
581
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
582
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
583
+ try std.testing.expect(retained.findById(2).?.widget.text_selection != null);
584
+ try std.testing.expect(retained.findById(3).?.widget.text_selection != null);
585
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 20, 140));
586
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
587
+ try std.testing.expect(retained.findById(2).?.widget.text_selection == null);
588
+ try std.testing.expect(retained.findById(3).?.widget.text_selection == null);
589
+ }
590
+
504
591
  test "span paragraph drag selection copies the concatenated bytes" {
505
592
  var app_state: ClipboardTestApp = .{};
506
593
  const app = app_state.app();
@@ -539,6 +626,52 @@ test "span paragraph drag selection copies the concatenated bytes" {
539
626
  try std.testing.expectEqualStrings(paragraph, copied);
540
627
  }
541
628
 
629
+ test "numbered code selection copies source without decorative gutter digits" {
630
+ var app_state: ClipboardTestApp = .{};
631
+ const app = app_state.app();
632
+ const harness = try createClipboardHarness(app);
633
+ defer harness.destroy(std.testing.allocator);
634
+
635
+ const source = "alpha\nbeta";
636
+ const spans = [_]canvas.TextSpan{.{
637
+ .text = source,
638
+ .monospace = true,
639
+ .color = .syntax_plain,
640
+ }};
641
+ const children = [_]canvas.Widget{.{
642
+ .id = 2,
643
+ .kind = .text,
644
+ .frame = geometry.RectF.init(12, 16, 220, 60),
645
+ .text = source,
646
+ .spans = &spans,
647
+ .code_line_number_digits = 1,
648
+ }};
649
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
650
+ const layout = try canvas.layoutWidgetTree(
651
+ .{ .kind = .stack, .children = &children },
652
+ geometry.RectF.init(0, 0, 320, 200),
653
+ &nodes,
654
+ );
655
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
656
+
657
+ // Start inside the source column (past the renderer-owned gutter) and
658
+ // drag beyond the final visual line.
659
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 34, 18));
660
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_drag, 231, 75));
661
+ const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
662
+ try std.testing.expectEqualDeep(
663
+ canvas.TextSelection{ .anchor = 0, .focus = source.len },
664
+ retained.nodes[1].widget.text_selection.?,
665
+ );
666
+
667
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("c", cmd));
668
+ var clipboard_buffer: [32]u8 = undefined;
669
+ try std.testing.expectEqualStrings(
670
+ source,
671
+ try harness.runtime.readClipboard(&clipboard_buffer),
672
+ );
673
+ }
674
+
542
675
  test "focused editable selection wins copy over a stale static selection" {
543
676
  var app_state: ClipboardTestApp = .{};
544
677
  const app = app_state.app();