@native-sdk/cli 0.7.0 → 0.7.2

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 (81) hide show
  1. package/build/app.zig +8 -0
  2. package/build.zig +114 -103
  3. package/native-sdk.d.ts +6 -3
  4. package/package.json +9 -9
  5. package/packages/core/package-lock.json +2 -2
  6. package/packages/core/package.json +1 -1
  7. package/packages/core/sdk/text.ts +25 -14
  8. package/packages/core/src/checker.ts +39 -0
  9. package/packages/core/src/emitter.ts +125 -1
  10. package/skill-data/native-ui/SKILL.md +11 -7
  11. package/src/platform/macos/appkit_host.m +76 -4
  12. package/src/platform/macos/cef_host.mm +24 -2
  13. package/src/platform/macos/root.zig +13 -0
  14. package/src/platform/types.zig +4 -0
  15. package/src/platform/windows/gpu_surface_renderer.cpp +2280 -0
  16. package/src/platform/windows/gpu_surface_renderer.h +86 -0
  17. package/src/platform/windows/root.zig +298 -20
  18. package/src/platform/windows/webview2_host.cpp +343 -33
  19. package/src/primitives/canvas/code.zig +501 -33
  20. package/src/primitives/canvas/code_tests.zig +1029 -6
  21. package/src/primitives/canvas/events.zig +111 -1
  22. package/src/primitives/canvas/frame.zig +45 -1
  23. package/src/primitives/canvas/gpu.zig +6 -0
  24. package/src/primitives/canvas/markdown_tests.zig +17 -18
  25. package/src/primitives/canvas/render_tests.zig +93 -0
  26. package/src/primitives/canvas/root.zig +6 -0
  27. package/src/primitives/canvas/serialization.zig +150 -21
  28. package/src/primitives/canvas/text.zig +6 -0
  29. package/src/primitives/canvas/themes/geist.zig +51 -5
  30. package/src/primitives/canvas/tokens.zig +33 -4
  31. package/src/primitives/canvas/ui.zig +160 -36
  32. package/src/primitives/canvas/ui_markup.zig +60 -9
  33. package/src/primitives/canvas/ui_markup_compiled.zig +41 -0
  34. package/src/primitives/canvas/ui_markup_compiled_tests.zig +12 -0
  35. package/src/primitives/canvas/ui_markup_contract.zig +17 -1
  36. package/src/primitives/canvas/ui_markup_view.zig +44 -0
  37. package/src/primitives/canvas/ui_markup_view_tests.zig +80 -6
  38. package/src/primitives/canvas/ui_schema.zig +18 -0
  39. package/src/primitives/canvas/ui_schema_tests.zig +10 -7
  40. package/src/primitives/canvas/ui_tests.zig +40 -0
  41. package/src/primitives/canvas/widget_builtin_tests.zig +513 -13
  42. package/src/primitives/canvas/widget_invalidation.zig +12 -1
  43. package/src/primitives/canvas/widget_layout.zig +260 -21
  44. package/src/primitives/canvas/widget_metrics.zig +47 -6
  45. package/src/primitives/canvas/widget_render.zig +849 -78
  46. package/src/primitives/canvas/widget_render_controls.zig +83 -25
  47. package/src/primitives/canvas/widget_render_style.zig +52 -8
  48. package/src/primitives/canvas/widget_runtime.zig +2 -0
  49. package/src/primitives/canvas/widget_text_input.zig +167 -8
  50. package/src/primitives/canvas/widget_tree.zig +6 -5
  51. package/src/primitives/canvas/widgets.zig +78 -2
  52. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  53. package/src/runtime/canvas_frame.zig +60 -7
  54. package/src/runtime/canvas_frame_invalidation_tests.zig +80 -0
  55. package/src/runtime/canvas_frame_packet_tests.zig +83 -9
  56. package/src/runtime/canvas_frame_patch_tests.zig +16 -0
  57. package/src/runtime/canvas_images.zig +8 -2
  58. package/src/runtime/canvas_limits.zig +13 -6
  59. package/src/runtime/canvas_widget_clipboard_tests.zig +8 -5
  60. package/src/runtime/canvas_widget_context_menu.zig +7 -2
  61. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  62. package/src/runtime/canvas_widget_events.zig +51 -20
  63. package/src/runtime/canvas_widget_runtime.zig +108 -4
  64. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  65. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  66. package/src/runtime/canvas_widget_state.zig +27 -0
  67. package/src/runtime/canvas_widget_text_tests.zig +326 -0
  68. package/src/runtime/core.zig +15 -5
  69. package/src/runtime/effects.zig +6 -0
  70. package/src/runtime/effects_tests.zig +30 -0
  71. package/src/runtime/gpu_surface_events.zig +50 -19
  72. package/src/runtime/media_surface_tests.zig +1 -1
  73. package/src/runtime/runtime_event_tests.zig +74 -0
  74. package/src/runtime/ui_app.zig +10 -8
  75. package/src/runtime/ui_app_tests.zig +67 -0
  76. package/src/runtime/view.zig +60 -16
  77. package/src/runtime/view_canvas.zig +15 -0
  78. package/src/runtime/view_widget_scroll.zig +48 -18
  79. package/src/runtime/view_widget_text.zig +59 -16
  80. package/src/runtime/window_view_runtime.zig +10 -0
  81. package/src/tooling/templates.zig +9 -6
@@ -46,6 +46,10 @@ const textInputClearButtonRect = widget_text_input.textInputClearButtonRect;
46
46
  const widgetButtonTextSize = widget_metrics.widgetButtonTextSize;
47
47
  const widgetBodyTextSize = widget_metrics.widgetBodyTextSize;
48
48
  const widgetLabelTextSize = widget_metrics.widgetLabelTextSize;
49
+ const widgetTabTriggerTextSize = widget_metrics.widgetTabTriggerTextSize;
50
+ const widgetTabTriggerInset = widget_metrics.widgetTabTriggerInset;
51
+ const widgetTabTriggerIconExtent = widget_metrics.widgetTabTriggerIconExtent;
52
+ const widgetTabTriggerIconGap = widget_metrics.widgetTabTriggerIconGap;
49
53
  const widgetTypographySize = widget_metrics.widgetTypographySize;
50
54
  const widgetButtonInset = widget_metrics.widgetButtonInset;
51
55
  const widgetControlInset = widget_metrics.widgetControlInset;
@@ -842,7 +846,7 @@ pub fn emitListItemWidget(builder: *Builder, widget: Widget, tokens: DesignToken
842
846
  if (icon) |resolved| {
843
847
  const icon_extent = widgetRowIconExtent(widget, tokens);
844
848
  const icon_frame = geometry.RectF.init(
845
- widget.frame.x + text_inset,
849
+ text_frame.x + text_inset,
846
850
  widget.frame.y + (widget.frame.height - icon_extent) * 0.5,
847
851
  icon_extent,
848
852
  icon_extent,
@@ -850,9 +854,9 @@ pub fn emitListItemWidget(builder: *Builder, widget: Widget, tokens: DesignToken
850
854
  try emitVectorIcon(builder, widget.id, 4, icon_frame, content_color, resolved);
851
855
  const shift = icon_extent + widgetRowIconGap(widget, tokens);
852
856
  text_frame = geometry.RectF.init(
853
- widget.frame.x + shift,
857
+ text_frame.x + shift,
854
858
  widget.frame.y,
855
- @max(1, widget.frame.width - shift),
859
+ @max(1, text_frame.width - shift),
856
860
  widget.frame.height,
857
861
  );
858
862
  }
@@ -928,32 +932,44 @@ pub fn emitDataCellWidgetChrome(builder: *Builder, widget: Widget, tokens: Desig
928
932
  fn segmentedTriggerRadius(widget: Widget, visual: ControlVisualTokens, tokens: DesignTokens) Radius {
929
933
  if (widget.style.radius) |radius| return Radius.all(@max(0, radius));
930
934
  if (visual.radius) |radius| return Radius.all(@max(0, widgetSizedRadiusValue(widget, radius)));
935
+ // Primary underline triggers are square text hit targets; rounding
936
+ // is part of the house pill register (and the ignored secondary-tab
937
+ // variant), not the Geist primary Tabs shape.
938
+ if (tokens.controls.tabs_indicator == .underline) return Radius.all(0);
931
939
  const container = tokens.controls.tabs.radius orelse tokens.radius.lg;
932
940
  return Radius.all(@max(0, widgetSizedRadiusValue(widget, container) - widget_model.tabs_list_inset));
933
941
  }
934
942
 
935
- /// The selected-tab bar for the `.underline` tab register: a short
936
- /// filled rect hugging the trigger's label (measured width plus a 2px
937
- /// shoulder each side — the label's own breathing room, mirroring the
938
- /// house reference's tab padding), sunk to the BOTTOM of the TabsList
939
- /// container. The trigger sits `tabs_list_inset` above the container's
940
- /// edge, so the bar extends past the trigger frame by exactly that
941
- /// inset and covers the strip hairline — the underline and the track
942
- /// line meet, which is the register's signature. Null in the `.pill`
943
- /// register; shared with invalidation so damage covers the overhang.
943
+ /// The selected-tab bar for the `.underline` tab register: a short filled
944
+ /// rect hugging the trigger's rendered content (text, optional icon and
945
+ /// icon gap, plus the register's side inset), capped to the hit-target
946
+ /// frame. Measuring the content instead of reusing the frame keeps the
947
+ /// marker honest when an author gives triggers fixed widths or grow
948
+ /// factors. It lands on the trigger's bottom edge and covers the TabsList
949
+ /// hairline where they meet. Null in the `.pill` register; shared with
950
+ /// invalidation.
944
951
  pub fn segmentedControlUnderlineRect(widget: Widget, tokens: DesignTokens) ?geometry.RectF {
945
952
  if (tokens.controls.tabs_indicator != .underline) return null;
946
953
  const frame = widget.frame.normalized();
947
954
  if (frame.isEmpty()) return null;
948
955
  const thickness = @min(frame.height, widgetSizedDensityValue(widget, tokens, tokens.metrics.tabs_indicator_thickness));
949
- const text_size = widgetLabelTextSize(widget, tokens);
956
+ const text_size = widgetTabTriggerTextSize(widget, tokens);
950
957
  const text_width = measureTextWidthForFont(tokens.text_measure, tokens.typography.font_id, widget.text, text_size);
951
- // An icon-only or empty trigger underlines its full width — there
952
- // is no label to hug.
953
- const bar_width = if (text_width > 0) @min(frame.width, text_width + 4) else frame.width;
958
+ const icon_width = if (widget.icon.len > 0)
959
+ widgetTabTriggerIconExtent(widget, tokens) + (if (widget.text.len > 0) widgetTabTriggerIconGap(tokens) else 0)
960
+ else
961
+ 0;
962
+ const content_width = icon_width + text_width;
963
+ // A truly empty trigger has no content to hug, so retain the
964
+ // historical full-frame marker instead of collapsing it to an
965
+ // invisible zero-width rect.
966
+ const bar_width = if (content_width > 0)
967
+ @min(frame.width, content_width + widgetTabTriggerInset(widget, tokens) * 2)
968
+ else
969
+ frame.width;
954
970
  return pixelSnapGeometryRect(tokens, geometry.RectF.init(
955
971
  frame.x + (frame.width - bar_width) * 0.5,
956
- frame.maxY() + widget_model.tabs_list_inset - thickness,
972
+ frame.maxY() - thickness,
957
973
  bar_width,
958
974
  thickness,
959
975
  ));
@@ -963,8 +979,8 @@ pub fn emitSegmentedControlWidget(builder: *Builder, widget: Widget, tokens: Des
963
979
  const selected = widget.state.selected or widget.value >= 0.5;
964
980
  const visual = selectionControlVisualTokens(widget, tokens);
965
981
  const radius = segmentedTriggerRadius(widget, visual, tokens);
966
- const text_size = widgetLabelTextSize(widget, tokens);
967
- const text_inset = widgetControlInset(widget, tokens, tokens.spacing.md);
982
+ const text_size = widgetTabTriggerTextSize(widget, tokens);
983
+ const text_inset = widgetTabTriggerInset(widget, tokens);
968
984
  switch (tokens.controls.tabs_indicator) {
969
985
  // The house tab-trigger treatment: the active segment lifts to
970
986
  // the page surface with a hairline border; inactive segments
@@ -1028,14 +1044,56 @@ pub fn emitSegmentedControlWidget(builder: *Builder, widget: Widget, tokens: Des
1028
1044
  // Inactive labels sit in the muted ink; in the underline register a
1029
1045
  // hovered (enabled) trigger previews the active ink — the register
1030
1046
  // has no hover WASH, so the type itself is the hover feedback.
1031
- const active_ink = widgetForegroundColor(widget, tokens, visual.foreground orelse tokens.colors.text);
1047
+ const active_ink = if (tokens.controls.tabs_indicator == .underline)
1048
+ widget.style.foreground orelse visual.foreground orelse tokens.colors.text
1049
+ else
1050
+ widgetForegroundColor(widget, tokens, visual.foreground orelse tokens.colors.text);
1032
1051
  const hover_preview = tokens.controls.tabs_indicator == .underline and widget.state.hovered and !widget.state.disabled;
1052
+ const content_color = if (selected or hover_preview) active_ink else widgetForegroundColor(widget, tokens, visual.foreground orelse tokens.colors.text_muted);
1053
+ // Primary icon tabs use the same regular ink, 16px glyph, and 6px
1054
+ // icon-to-label gap as the Geist reference. Keep this inside the
1055
+ // underline arm so the default theme's pill tabs remain byte-for-byte
1056
+ // unchanged, including their historical text-only command stream.
1057
+ if (tokens.controls.tabs_indicator == .underline and widget.icon.len > 0) {
1058
+ const resolved = icon_model.resolveOrMissing(widget.icon).?;
1059
+ const icon_extent = widgetTabTriggerIconExtent(widget, tokens);
1060
+ const icon_y = widget.frame.y + (widget.frame.height - icon_extent) * 0.5;
1061
+ if (widget.text.len == 0) {
1062
+ try emitVectorIcon(builder, widget.id, 5, geometry.RectF.init(
1063
+ widget.frame.x + (widget.frame.width - icon_extent) * 0.5,
1064
+ icon_y,
1065
+ icon_extent,
1066
+ icon_extent,
1067
+ ), content_color, resolved);
1068
+ return;
1069
+ }
1070
+ const gap = widgetTabTriggerIconGap(tokens);
1071
+ const text_width = measureTextWidthForFont(tokens.text_measure, tokens.typography.font_id, widget.text, text_size);
1072
+ const available = @max(0, widget.frame.width - text_inset * 2);
1073
+ const content_width = @min(available, icon_extent + gap + text_width);
1074
+ const start_x = widget.frame.x + text_inset + @max(0, (available - content_width) * 0.5);
1075
+ const icon_x = if (widget.icon_placement == .trailing) start_x + content_width - icon_extent else start_x;
1076
+ const text_x = if (widget.icon_placement == .trailing) start_x else start_x + icon_extent + gap;
1077
+ const text_max_x = if (widget.icon_placement == .trailing) icon_x - gap else widget.frame.maxX() - text_inset;
1078
+ try emitVectorIcon(builder, widget.id, 5, geometry.RectF.init(icon_x, icon_y, icon_extent, icon_extent), content_color, resolved);
1079
+ const text_frame = geometry.RectF.init(text_x, widget.frame.y, @max(1, text_max_x - text_x), widget.frame.height);
1080
+ try builder.drawText(.{
1081
+ .id = widgetPartId(widget.id, 3),
1082
+ .font_id = tokens.typography.font_id,
1083
+ .size = text_size,
1084
+ .origin = pixelSnapTextPoint(tokens, boundedTextOrigin(text_frame, text_size, 0)),
1085
+ .color = content_color,
1086
+ .text = widget.text,
1087
+ .text_layout = boundedTextLayout(text_frame, text_size, 0, .start, .none, widget.text_overflow, tokens),
1088
+ });
1089
+ return;
1090
+ }
1033
1091
  try builder.drawText(.{
1034
1092
  .id = widgetPartId(widget.id, 3),
1035
1093
  .font_id = tokens.typography.font_id,
1036
1094
  .size = text_size,
1037
1095
  .origin = pixelSnapTextPoint(tokens, boundedTextOrigin(widget.frame, text_size, text_inset)),
1038
- .color = if (selected or hover_preview) active_ink else widgetForegroundColor(widget, tokens, visual.foreground orelse tokens.colors.text_muted),
1096
+ .color = content_color,
1039
1097
  .text = widget.text,
1040
1098
  .text_layout = boundedTextLayout(widget.frame, text_size, text_inset, .center, .none, widget.text_overflow, tokens),
1041
1099
  });
@@ -1479,7 +1537,7 @@ fn widgetIconGlyphScale(widget: Widget) f32 {
1479
1537
  };
1480
1538
  }
1481
1539
 
1482
- fn emitWidgetTextSelectionRects(
1540
+ pub fn emitWidgetTextSelectionRects(
1483
1541
  builder: *Builder,
1484
1542
  widget: Widget,
1485
1543
  text: DrawText,
@@ -1513,7 +1571,7 @@ fn emitWidgetTextSelectionRects(
1513
1571
  /// (re-shaping the selected substring instead would re-kern at the
1514
1572
  /// segment boundary and shift the halves apart). Runs after the base
1515
1573
  /// text so the recolored glyphs paint over their normal-ink twins.
1516
- fn emitWidgetTextSelectedGlyphs(
1574
+ pub fn emitWidgetTextSelectedGlyphs(
1517
1575
  builder: *Builder,
1518
1576
  widget: Widget,
1519
1577
  text: DrawText,
@@ -1550,7 +1608,7 @@ fn textSelectionOverlayCommandId(seed: u64, widget_id: ObjectId, ordinal: usize)
1550
1608
  return if (value == 0) 1 else value;
1551
1609
  }
1552
1610
 
1553
- fn emitWidgetTextCompositionLines(
1611
+ pub fn emitWidgetTextCompositionLines(
1554
1612
  builder: *Builder,
1555
1613
  widget: Widget,
1556
1614
  text: DrawText,
@@ -1587,7 +1645,7 @@ fn widgetTextRangePart(first_part: ObjectId, overflow_first_part: ObjectId, inde
1587
1645
  return overflow_first_part + @as(ObjectId, @intCast(index - 1));
1588
1646
  }
1589
1647
 
1590
- fn emitWidgetTextCaret(
1648
+ pub fn emitWidgetTextCaret(
1591
1649
  builder: *Builder,
1592
1650
  widget: Widget,
1593
1651
  text: DrawText,
@@ -361,17 +361,22 @@ pub fn disabledWash(color: Color, disabled: bool, alpha: f32) Color {
361
361
  }
362
362
 
363
363
  pub fn buttonTextColorForWidget(widget: Widget, tokens: DesignTokens) Color {
364
- // Disabled ink is the variant's own ink at the disabled-wash
365
- // strength, matching the washed fill — the whole control fades as
366
- // one piece (primary keeps knockout text on its washed fill; the
367
- // quiet variants keep their body ink) instead of swapping to the
368
- // shared muted gray, which read as a live-but-secondary label.
369
- // Themes with a stated disabled ink (`disabled_foreground`) take it
370
- // instead.
364
+ // Disabled ink normally keeps the variant's own ink at the
365
+ // disabled-wash strength. A filled primary is the exception: CSS
366
+ // opacity composites its knockout label and fill as ONE layer, so
367
+ // on the matching surface the label lands near the surface color
368
+ // instead of fading a second time through the already-muted fill.
369
+ // Precomposite that ink over the themed page at the requested state
370
+ // strength; this preserves the group-opacity result while still
371
+ // honoring a custom `disabled_alpha`. Themes with a stated disabled
372
+ // ink (`disabled_foreground`) take it instead — Geist uses that for
373
+ // its gray swap register.
371
374
  if (widget.state.disabled) {
372
375
  const visual = buttonControlVisualTokens(widget, tokens);
373
376
  if (visual.disabled_foreground) |color| return color;
374
- return disabledWash(buttonTextColorForWidget(restStateWidget(widget), tokens), true, tokens.states.disabled_alpha);
377
+ const rest = buttonTextColorForWidget(restStateWidget(widget), tokens);
378
+ if (widget.variant == .primary) return compositeOver(rest, tokens.colors.background, tokens.states.disabled_alpha);
379
+ return disabledWash(rest, true, tokens.states.disabled_alpha);
375
380
  }
376
381
  const active = widget.state.pressed or widget.state.selected;
377
382
  const visual = buttonControlVisualTokens(widget, tokens);
@@ -417,9 +422,48 @@ pub fn buttonBorderFill(widget: Widget, tokens: DesignTokens) Fill {
417
422
  else => widgetBorderColor(widget, visual.border orelse tokens.colors.border),
418
423
  };
419
424
  };
425
+ if (widget.state.disabled and widget.style.border == null) {
426
+ const visual = buttonControlVisualTokens(widget, tokens);
427
+ // The shared edge is independently overridable: a custom theme
428
+ // may want a solid disabled outline while keeping the house fill
429
+ // wash instead of also stating a disabled body color.
430
+ switch (widget.variant) {
431
+ .default, .secondary, .outline => if (tokens.controls.button_disabled_border) |disabled_border| return colorFill(disabled_border),
432
+ .primary, .ghost, .destructive => {},
433
+ }
434
+ // shadcn's filled default button has a transparent CSS border.
435
+ // At rest our fallback accent stroke is invisible against the
436
+ // identical opaque fill, but washing both commands separately
437
+ // makes their overlap darker and creates a false outline. Drop
438
+ // that implicit edge in the disabled state. This remains
439
+ // transparent even when a theme states a translucent disabled
440
+ // body: painting that color again as a stroke would double-blend
441
+ // into a false outline. An explicitly themed or locally authored
442
+ // border still follows its own opacity wash.
443
+ if (widget.variant == .primary and visual.border == null) return colorFill(transparentColor());
444
+ }
420
445
  return colorFill(disabledWash(border, widget.state.disabled, tokens.states.disabled_alpha));
421
446
  }
422
447
 
448
+ /// The command color equivalent to painting `foreground` over
449
+ /// `background` at `opacity`. Button fill, border, and text are separate
450
+ /// display-list commands, so the filled-primary disabled label uses this
451
+ /// to reproduce whole-control opacity without blending a translucent
452
+ /// glyph through the already-muted fill a second time.
453
+ fn compositeOver(foreground: Color, background: Color, opacity: f32) Color {
454
+ const foreground_alpha = std.math.clamp(opacity, 0, 1) * std.math.clamp(foreground.a, 0, 1);
455
+ const background_alpha = std.math.clamp(background.a, 0, 1);
456
+ const output_alpha = foreground_alpha + background_alpha * (1 - foreground_alpha);
457
+ if (output_alpha <= 0) return transparentColor();
458
+ const background_weight = background_alpha * (1 - foreground_alpha);
459
+ return Color.rgba(
460
+ (foreground.r * foreground_alpha + background.r * background_weight) / output_alpha,
461
+ (foreground.g * foreground_alpha + background.g * background_weight) / output_alpha,
462
+ (foreground.b * foreground_alpha + background.b * background_weight) / output_alpha,
463
+ output_alpha,
464
+ );
465
+ }
466
+
423
467
  /// Whether this widget is a button-group member rendering in the
424
468
  /// `.detached` chip register: the group walks stamp `group_segment`
425
469
  /// onto every member on the way down, and the register switch decides
@@ -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,113 @@ 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
+ if (widget.hasCodeDiff()) return false;
481
+ return widget.code_content_width_generation == text_measure_cache.textMeasureGeneration() and
482
+ widget.code_content_width_font_id == font_id and
483
+ widget.code_content_width_size_bits == @as(u32, @bitCast(text_size)) and
484
+ widget.code_content_width >= 0 and
485
+ std.math.isFinite(widget.code_content_width);
486
+ }
487
+
488
+ /// Populate the retained longest-line width once per source/font generation.
489
+ /// Runtime reconciliation carries this cache across unchanged app rebuilds;
490
+ /// edits invalidate it before caret scrolling recomputes the new document.
491
+ pub fn cacheTextInputContentWidthForWidget(widget: *Widget, tokens: DesignTokens) void {
492
+ if (widget.kind != .textarea or !widget.code_editor or !widget.text_no_wrap) return;
493
+ if (widget.hasCodeDiff()) return;
494
+ const text_size = widgetTextInputSize(widget.*, tokens);
495
+ const font_id = widgetTextInputFontId(widget.*, tokens);
496
+ if (codeContentWidthCacheCurrent(widget.*, font_id, text_size)) return;
497
+ widget.code_content_width = widestLogicalLineWidth(tokens.text_measure, font_id, widget.text, text_size);
498
+ widget.code_content_width_generation = text_measure_cache.textMeasureGeneration();
499
+ widget.code_content_width_font_id = font_id;
500
+ widget.code_content_width_size_bits = @bitCast(text_size);
501
+ }
502
+
503
+ fn widestLogicalLineWidth(
504
+ measure: ?*const text_model.TextMeasureProvider,
505
+ font_id: FontId,
506
+ text: []const u8,
507
+ text_size: f32,
508
+ ) f32 {
509
+ if (measure) |provider| {
510
+ if (widestLogicalLineWidthBatched(provider, font_id, text, text_size)) |width| return width;
511
+ }
512
+
513
+ var widest: f32 = 0;
514
+ var start: usize = 0;
515
+ while (start <= text.len) {
516
+ const end = std.mem.indexOfScalarPos(u8, text, start, '\n') orelse text.len;
517
+ widest = @max(widest, measureTextWidthForFont(measure, font_id, text[start..end], text_size));
518
+ if (end == text.len) break;
519
+ start = end + 1;
520
+ }
521
+ return widest;
522
+ }
523
+
524
+ /// Measure many ordinary source lines in one provider round-trip. Chunks end
525
+ /// at a newline so shaping never crosses a logical-line boundary. A single
526
+ /// extraordinary line above the batched scratch bound takes one width call;
527
+ /// the common 10,000-line document takes only a handful of batched calls.
528
+ fn widestLogicalLineWidthBatched(
529
+ provider: *const text_model.TextMeasureProvider,
530
+ font_id: FontId,
531
+ text: []const u8,
532
+ text_size: f32,
533
+ ) ?f32 {
534
+ if (provider.measure_advances_fn == null) return null;
535
+ if (text.len == 0) return 0;
536
+
537
+ var widest: f32 = 0;
538
+ var chunk_start: usize = 0;
539
+ while (chunk_start < text.len) {
540
+ const limit_end = @min(
541
+ text.len,
542
+ chunk_start +| text_measure_cache.max_batched_advance_run_bytes,
543
+ );
544
+ var chunk_end = limit_end;
545
+ if (limit_end < text.len) {
546
+ const bounded = text[chunk_start..limit_end];
547
+ if (std.mem.lastIndexOfScalar(u8, bounded, '\n')) |newline| {
548
+ chunk_end = chunk_start + newline + 1;
549
+ } else {
550
+ const line_end = std.mem.indexOfScalarPos(u8, text, chunk_start, '\n') orelse text.len;
551
+ widest = @max(
552
+ widest,
553
+ measureTextWidthForFont(provider, font_id, text[chunk_start..line_end], text_size),
554
+ );
555
+ chunk_start = if (line_end < text.len) line_end + 1 else line_end;
556
+ continue;
557
+ }
558
+ }
559
+
560
+ const chunk = text[chunk_start..chunk_end];
561
+ const advances = text_measure_cache.textRunAdvances(provider, font_id, text_size, chunk) orelse return null;
562
+ var line_start: usize = 0;
563
+ for (chunk, 0..) |byte, index| {
564
+ if (byte != '\n') continue;
565
+ widest = @max(widest, text_measure_cache.advanceSliceWidth(advances, line_start, index));
566
+ line_start = index + 1;
567
+ }
568
+ if (line_start < chunk.len) {
569
+ widest = @max(widest, text_measure_cache.advanceSliceWidth(advances, line_start, chunk.len));
570
+ }
571
+ chunk_start = chunk_end;
572
+ }
573
+ return widest;
574
+ }
575
+
422
576
  fn widgetTextInputTrailingInset(widget: Widget, text_size: f32, inset: f32) f32 {
577
+ // A code editor's leading inset is its line-number gutter, not symmetric
578
+ // field padding. Its source viewport runs all the way to the trailing
579
+ // edge; mirroring the gutter here invents horizontal overflow for lines
580
+ // that visibly fit beside the numbers.
581
+ if (widget.code_editor) return 0;
423
582
  if (widget.kind == .combobox) return inset + @max(8, text_size - 4);
424
583
  // A search field holding text reserves the trailing slot for the
425
584
  // built-in clear affordance so the text never runs under the x.
@@ -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