@native-sdk/cli 0.5.1 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/build.zig +41 -0
  2. package/package.json +9 -9
  3. package/packages/core/package-lock.json +2 -2
  4. package/packages/core/package.json +1 -1
  5. package/packages/core/sdk/events.ts +39 -2
  6. package/packages/core/src/checker.ts +1 -1
  7. package/packages/core/src/emitter.ts +39 -3
  8. package/packages/core/src/infer.ts +53 -7
  9. package/skill-data/automation/SKILL.md +7 -4
  10. package/skill-data/core/references/app-model-runtime.md +3 -0
  11. package/skill-data/native-ui/SKILL.md +3 -2
  12. package/src/app_runner/root.zig +25 -0
  13. package/src/automation/protocol.zig +20 -1
  14. package/src/embed/host.zig +27 -0
  15. package/src/embed/tests.zig +69 -0
  16. package/src/embed/ui_host.zig +4 -0
  17. package/src/platform/macos/appkit_host.h +11 -0
  18. package/src/platform/macos/appkit_host.m +130 -0
  19. package/src/platform/macos/root.zig +34 -0
  20. package/src/platform/null_platform_tests.zig +39 -0
  21. package/src/platform/root.zig +3 -0
  22. package/src/platform/types.zig +118 -4
  23. package/src/primitives/canvas/chart.zig +7 -7
  24. package/src/primitives/canvas/chart_tests.zig +66 -0
  25. package/src/primitives/canvas/commands.zig +48 -0
  26. package/src/primitives/canvas/events.zig +161 -0
  27. package/src/primitives/canvas/font_ttf.zig +155 -8
  28. package/src/primitives/canvas/font_ttf_tests.zig +686 -0
  29. package/src/primitives/canvas/reference.zig +67 -8
  30. package/src/primitives/canvas/render_fingerprints.zig +8 -0
  31. package/src/primitives/canvas/root.zig +26 -0
  32. package/src/primitives/canvas/tokens.zig +170 -7
  33. package/src/primitives/canvas/ui.zig +62 -7
  34. package/src/primitives/canvas/ui_markup.zig +51 -4
  35. package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
  36. package/src/primitives/canvas/ui_markup_compiled.zig +49 -4
  37. package/src/primitives/canvas/ui_markup_compiled_tests.zig +160 -0
  38. package/src/primitives/canvas/ui_markup_contract.zig +9 -0
  39. package/src/primitives/canvas/ui_markup_contract_tests.zig +24 -0
  40. package/src/primitives/canvas/ui_markup_tests.zig +73 -0
  41. package/src/primitives/canvas/ui_markup_view.zig +63 -5
  42. package/src/primitives/canvas/ui_markup_view_tests.zig +151 -2
  43. package/src/primitives/canvas/ui_schema.zig +31 -1
  44. package/src/primitives/canvas/ui_schema_tests.zig +11 -11
  45. package/src/primitives/canvas/ui_tests.zig +31 -0
  46. package/src/primitives/canvas/vector.zig +258 -141
  47. package/src/primitives/canvas/widget_access.zig +1 -1
  48. package/src/primitives/canvas/widget_builtin_tests.zig +259 -0
  49. package/src/primitives/canvas/widget_layout.zig +13 -5
  50. package/src/primitives/canvas/widget_layout_tests.zig +82 -0
  51. package/src/primitives/canvas/widget_render.zig +78 -40
  52. package/src/primitives/canvas/widget_render_controls.zig +15 -4
  53. package/src/primitives/canvas/widget_semantics.zig +6 -1
  54. package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
  55. package/src/primitives/canvas/widget_text_input.zig +118 -10
  56. package/src/primitives/canvas/widget_tree.zig +14 -2
  57. package/src/primitives/canvas/widgets.zig +28 -0
  58. package/src/root.zig +5 -0
  59. package/src/runtime/api.zig +13 -0
  60. package/src/runtime/automation_commands.zig +33 -0
  61. package/src/runtime/automation_widget_dispatch.zig +159 -28
  62. package/src/runtime/canvas_font_tests.zig +272 -3
  63. package/src/runtime/canvas_fonts.zig +60 -20
  64. package/src/runtime/canvas_frame.zig +15 -0
  65. package/src/runtime/canvas_frame_helpers.zig +15 -0
  66. package/src/runtime/canvas_image_tests.zig +26 -0
  67. package/src/runtime/canvas_images.zig +33 -7
  68. package/src/runtime/canvas_limits.zig +44 -8
  69. package/src/runtime/canvas_widget_anchored_tests.zig +253 -65
  70. package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
  71. package/src/runtime/canvas_widget_context_menu.zig +3 -2
  72. package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
  73. package/src/runtime/canvas_widget_events.zig +1295 -7
  74. package/src/runtime/canvas_widget_floating_tests.zig +2801 -0
  75. package/src/runtime/canvas_widget_runtime.zig +24 -7
  76. package/src/runtime/canvas_widget_scroll_drivers.zig +1 -1
  77. package/src/runtime/canvas_widget_state.zig +88 -6
  78. package/src/runtime/canvas_widget_text_tests.zig +277 -0
  79. package/src/runtime/core.zig +112 -6
  80. package/src/runtime/flow.zig +51 -1
  81. package/src/runtime/gpu_surface_events.zig +51 -5
  82. package/src/runtime/media_surface.zig +726 -0
  83. package/src/runtime/media_surface_tests.zig +1476 -0
  84. package/src/runtime/root.zig +9 -0
  85. package/src/runtime/session_journal.zig +52 -13
  86. package/src/runtime/session_record.zig +166 -0
  87. package/src/runtime/session_tests.zig +575 -0
  88. package/src/runtime/tests.zig +1 -0
  89. package/src/runtime/ts_ui_app.zig +56 -0
  90. package/src/runtime/ui_app.zig +64 -1
  91. package/src/runtime/ui_app_tests.zig +794 -0
  92. package/src/runtime/view.zig +86 -0
  93. package/src/runtime/view_widget_text.zig +18 -1
  94. package/src/runtime/view_widget_tree.zig +339 -20
  95. package/src/runtime/widget_bridge.zig +26 -0
  96. package/src/runtime/window_storage.zig +61 -15
  97. package/src/runtime/window_view_runtime.zig +13 -0
  98. package/src/runtime/window_views.zig +6 -0
@@ -1415,6 +1415,136 @@ test "design tokens provide theme and contrast palettes" {
1415
1415
  try std.testing.expectEqual(Easing.linear, reduced_motion.motion.easing);
1416
1416
  }
1417
1417
 
1418
+ test "the accent override desaturates its dark-scheme focus ring" {
1419
+ const accent = Color.rgb8(223, 38, 112);
1420
+
1421
+ // Light keeps the raw accent on every identity slot, ring included.
1422
+ const light = canvas.accentOverrides(accent, .light);
1423
+ try std.testing.expectEqualDeep(accent, light.colors.accent.?);
1424
+ try std.testing.expectEqualDeep(accent, light.colors.focus_ring.?);
1425
+
1426
+ // Dark keeps the accent identity but derives the ring at half the
1427
+ // accent's HSL saturation — a quiet outline instead of a neon glare
1428
+ // — through the exported derivation, so hand-authored token sets
1429
+ // can state the same value.
1430
+ const dark = canvas.accentOverrides(accent, .dark);
1431
+ try std.testing.expectEqualDeep(accent, dark.colors.accent.?);
1432
+ const ring = dark.colors.focus_ring.?;
1433
+ try std.testing.expectEqualDeep(canvas.accentFocusRing(accent, .dark), ring);
1434
+ try std.testing.expect(!std.meta.eql(accent, ring));
1435
+ // Same lightness (HSL max+min preserved), lower chroma: the channel
1436
+ // spread shrinks to half while the hue's ordering holds.
1437
+ const accent_spread = @max(accent.r, @max(accent.g, accent.b)) - @min(accent.r, @min(accent.g, accent.b));
1438
+ const ring_spread = @max(ring.r, @max(ring.g, ring.b)) - @min(ring.r, @min(ring.g, ring.b));
1439
+ try std.testing.expect(ring_spread < accent_spread);
1440
+ try std.testing.expect(ring.r > ring.b and ring.b > ring.g);
1441
+
1442
+ // An achromatic accent has no saturation to halve: the ring passes
1443
+ // through untouched.
1444
+ const gray = Color.rgb8(115, 115, 115);
1445
+ try std.testing.expectEqualDeep(gray, canvas.accentFocusRing(gray, .dark));
1446
+ }
1447
+
1448
+ test "the dark accent focus ring holds the non-text contrast floor across hues" {
1449
+ // Rings draw OUTSIDE controls, so the tones that matter are the
1450
+ // dark containers controls commonly sit on, across BOTH shipped
1451
+ // packs: page background, card/popover surface, and the muted
1452
+ // surface (the tabs-list pill container focusable triggers sit
1453
+ // inside). accentFocusRing floors against the LIGHTEST of these
1454
+ // (house surface_subtle #262626) — a ring clearing 3:1 there
1455
+ // clears every darker tone in the set too; each is asserted below.
1456
+ const house_dark = DesignTokens.theme(.{ .color_scheme = .dark }).colors;
1457
+ const geist_dark = DesignTokens.theme(.{ .pack = .geist, .color_scheme = .dark }).colors;
1458
+ const adjacent_tones = [_]Color{
1459
+ house_dark.background, // #0a0a0a
1460
+ house_dark.surface, // #171717
1461
+ house_dark.surface_subtle, // #262626 — the floor's reference
1462
+ geist_dark.background, // #000000 (Geist surface is the same black)
1463
+ geist_dark.surface_subtle, // #1a1a1a
1464
+ };
1465
+ const reference_tone = house_dark.surface_subtle;
1466
+ const cases = [_]Color{
1467
+ // Green: the input clears 3:1 on the background AND the card
1468
+ // surface; desaturation alone dropped to ~2.6:1 on the
1469
+ // background (worse still on the surface) — the floor lifts it
1470
+ // back over the bar on both. Against the muted reference the
1471
+ // input itself sits just under 3:1 (~2.94), so the ring holds
1472
+ // exactly that there — restored, never invented.
1473
+ Color.rgb8(0, 128, 0),
1474
+ // Deep blue: below 3:1 on its own; the ring must not get worse.
1475
+ Color.rgb8(0, 0, 204),
1476
+ // Dark red: below 3:1 on its own AND desaturation costs more —
1477
+ // the floor restores the accent's own contrast, no further.
1478
+ Color.rgb8(139, 0, 0),
1479
+ // The soundboard pink: clears the bar before and after halving.
1480
+ Color.rgb8(223, 38, 112),
1481
+ // Achromatic gray: nothing to desaturate, passes through.
1482
+ Color.rgb8(115, 115, 115),
1483
+ };
1484
+ for (cases) |accent| {
1485
+ // Light passes the accent through untouched, every hue.
1486
+ try std.testing.expectEqualDeep(accent, canvas.accentFocusRing(accent, .light));
1487
+
1488
+ const ring = canvas.accentFocusRing(accent, .dark);
1489
+ // Dark never raises saturation; chromatic accents lose half.
1490
+ const accent_saturation = testHslSaturation(accent);
1491
+ const ring_saturation = testHslSaturation(ring);
1492
+ if (accent_saturation > 0) {
1493
+ try std.testing.expect(ring_saturation < accent_saturation);
1494
+ } else {
1495
+ try std.testing.expectEqualDeep(accent, ring);
1496
+ }
1497
+ // The floor, tone by tone: >= 3:1 (WCAG non-text) against EVERY
1498
+ // adjacent tone the accent itself cleared it on. Flooring
1499
+ // against the lightest tone guarantees the rest: where the
1500
+ // accent cleared 3:1 anywhere, the ring's luminance lands at or
1501
+ // above the accent's, so no darker tone can regress.
1502
+ for (adjacent_tones) |tone| {
1503
+ if (testContrastRatio(accent, tone) >= 3.0) {
1504
+ try std.testing.expect(testContrastRatio(ring, tone) >= 3.0);
1505
+ }
1506
+ }
1507
+ // The escape hatch, stated against the SAME reference the floor
1508
+ // measures on: when the accent never cleared 3:1 there, the
1509
+ // ring still never lands below the accent's own contrast.
1510
+ const accent_contrast = testContrastRatio(accent, reference_tone);
1511
+ const ring_contrast = testContrastRatio(ring, reference_tone);
1512
+ if (accent_contrast >= 3.0) {
1513
+ try std.testing.expect(ring_contrast >= 3.0);
1514
+ } else {
1515
+ try std.testing.expect(ring_contrast >= accent_contrast);
1516
+ }
1517
+ }
1518
+ }
1519
+
1520
+ /// HSL saturation of a color, for the desaturation assertions.
1521
+ fn testHslSaturation(color: Color) f32 {
1522
+ const max = @max(color.r, @max(color.g, color.b));
1523
+ const min = @min(color.r, @min(color.g, color.b));
1524
+ const delta = max - min;
1525
+ if (delta <= 0.0001) return 0;
1526
+ const lightness = (max + min) / 2;
1527
+ return delta / (1 - @abs(2 * lightness - 1));
1528
+ }
1529
+
1530
+ /// WCAG contrast ratio between two colors, for the floor assertions.
1531
+ fn testContrastRatio(a: Color, b: Color) f32 {
1532
+ const la = testRelativeLuminance(a);
1533
+ const lb = testRelativeLuminance(b);
1534
+ return (@max(la, lb) + 0.05) / (@min(la, lb) + 0.05);
1535
+ }
1536
+
1537
+ fn testRelativeLuminance(color: Color) f32 {
1538
+ return 0.2126 * testSrgbToLinear(color.r) +
1539
+ 0.7152 * testSrgbToLinear(color.g) +
1540
+ 0.0722 * testSrgbToLinear(color.b);
1541
+ }
1542
+
1543
+ fn testSrgbToLinear(channel: f32) f32 {
1544
+ if (channel <= 0.04045) return channel / 12.92;
1545
+ return std.math.pow(f32, (channel + 0.055) / 1.055, 2.4);
1546
+ }
1547
+
1418
1548
  test "built-in component catalog covers house component set" {
1419
1549
  const expected_names = [_][]const u8{
1420
1550
  "Accordion",
@@ -3847,6 +3977,135 @@ test "short single-line values emit no clip and an unshifted origin" {
3847
3977
  }
3848
3978
  }
3849
3979
 
3980
+ test "a single-line value holding a line break renders one line inside the border" {
3981
+ // The defensive containment layer: edits can never insert a line
3982
+ // break into a single-line field (they sanitize at the derivation
3983
+ // seam), but a MODEL-SET value still can — a Zig core can put one
3984
+ // there today, as can an old journal or a host API write. The field
3985
+ // presents `\n`/`\r` as spaces (one line, byte-for-byte offsets) and
3986
+ // force-clips to its content rect, so the value never paints outside
3987
+ // the rounded border — identically in both render walks.
3988
+ const field = Widget{
3989
+ .id = 7,
3990
+ .kind = .input,
3991
+ .frame = geometry.RectF.init(10, 12, 200, 32),
3992
+ .text = "one\ntwo\r\nthree",
3993
+ .semantics = .{ .label = "Name" },
3994
+ };
3995
+
3996
+ var commands: [8]CanvasCommand = undefined;
3997
+ var builder = Builder.init(&commands);
3998
+ try emitWidgetTree(&builder, field, .{});
3999
+ try expectSingleLinePresentedField(builder.displayList(), field);
4000
+
4001
+ // Layout walk: the compiled and interpreted engines both emit
4002
+ // retained layouts through this walk, so containment pins here too.
4003
+ var nodes: [2]WidgetLayoutNode = undefined;
4004
+ const layout = try layoutWidgetTree(field, field.frame, &nodes);
4005
+ var layout_commands: [8]CanvasCommand = undefined;
4006
+ var layout_builder = Builder.init(&layout_commands);
4007
+ try layout.emitDisplayList(&layout_builder, .{});
4008
+ try expectSingleLinePresentedField(layout_builder.displayList(), field);
4009
+
4010
+ // A search field (the other emitter) contains the same value the
4011
+ // same way: presented bytes and a forced clip.
4012
+ var search = field;
4013
+ search.kind = .search_field;
4014
+ var search_commands: [32]CanvasCommand = undefined;
4015
+ var search_builder = Builder.init(&search_commands);
4016
+ try emitWidgetTree(&search_builder, search, .{});
4017
+ const search_list = search_builder.displayList();
4018
+ try std.testing.expect(search_list.findCommandById(widgetPartId(7, 7)) != null);
4019
+ switch (search_list.findCommandById(widgetPartId(7, 9)).?.command) {
4020
+ .draw_text => |text| try std.testing.expectEqualStrings("one two three", text.text),
4021
+ else => return error.TestUnexpectedResult,
4022
+ }
4023
+
4024
+ // A textarea keeps its raw line breaks: it is the one genuinely
4025
+ // multi-line editable kind.
4026
+ var textarea = field;
4027
+ textarea.kind = .textarea;
4028
+ textarea.frame = geometry.RectF.init(10, 12, 200, 120);
4029
+ var area_commands: [8]CanvasCommand = undefined;
4030
+ var area_builder = Builder.init(&area_commands);
4031
+ try emitWidgetTree(&area_builder, textarea, .{});
4032
+ switch (area_builder.displayList().findCommandById(widgetPartId(7, 3)).?.command) {
4033
+ .draw_text => |text| try std.testing.expectEqualStrings("one\ntwo\r\nthree", text.text),
4034
+ else => return error.TestUnexpectedResult,
4035
+ }
4036
+ }
4037
+
4038
+ fn expectSingleLinePresentedField(display_list: DisplayList, field: Widget) !void {
4039
+ // The content-rect clip is FORCED for a value holding a line break,
4040
+ // even though the presented value fits on one line.
4041
+ const viewport = textInputViewportForWidget(field, .{}).?;
4042
+ switch (display_list.findCommandById(widgetPartId(field.id, 16)).?.command) {
4043
+ .push_clip => |clip| try expectRectApprox(viewport, clip.rect),
4044
+ else => return error.TestUnexpectedResult,
4045
+ }
4046
+ switch (display_list.findCommandById(widgetPartId(field.id, 3)).?.command) {
4047
+ .draw_text => |text| {
4048
+ // `\n` and each byte of `\r\n` present as spaces — same
4049
+ // length, so caret/selection offsets address the raw value.
4050
+ try std.testing.expectEqualStrings("one two three", text.text);
4051
+ // The presented value lays out as exactly ONE line.
4052
+ var lines: [4]TextLine = undefined;
4053
+ const run = try layoutTextRun(text, text.text_layout.?, &lines);
4054
+ try std.testing.expectEqual(@as(usize, 1), run.lines.len);
4055
+ // ... and that line's painted bounds sit inside the field.
4056
+ const bounds = run.lines[0].bounds;
4057
+ try std.testing.expect(bounds.maxY() <= field.frame.maxY() + 0.001);
4058
+ try std.testing.expect(bounds.y >= field.frame.y - 0.001);
4059
+ },
4060
+ else => return error.TestUnexpectedResult,
4061
+ }
4062
+ }
4063
+
4064
+ test "presented single-line values survive later emits into any builder" {
4065
+ // The builder contract (the `allocPathElements` rule): a display
4066
+ // list may be HELD while another builder emits, and one builder may
4067
+ // ACCUMULATE several emit calls — either way every emitted command
4068
+ // stays intact. Presented bytes live in the BUILDER's own text
4069
+ // store, so a later presentation can never overwrite an emitted
4070
+ // draw_text the way a per-emit-reset scratch pool would (both
4071
+ // values here present to the same byte length, so a reset pool
4072
+ // would alias the first list's slice exactly).
4073
+ const first = Widget{
4074
+ .id = 7,
4075
+ .kind = .input,
4076
+ .frame = geometry.RectF.init(10, 12, 200, 32),
4077
+ .text = "one\ntwo",
4078
+ };
4079
+ var second = first;
4080
+ second.id = 8;
4081
+ second.text = "AAA\nBBB";
4082
+
4083
+ // Hold the first builder's list across a second builder's emit.
4084
+ var commands: [8]CanvasCommand = undefined;
4085
+ var builder = Builder.init(&commands);
4086
+ try emitWidgetTree(&builder, first, .{});
4087
+ var other_commands: [8]CanvasCommand = undefined;
4088
+ var other_builder = Builder.init(&other_commands);
4089
+ try emitWidgetTree(&other_builder, second, .{});
4090
+ try expectPresentedFieldText(builder.displayList(), 7, "one two");
4091
+ try expectPresentedFieldText(other_builder.displayList(), 8, "AAA BBB");
4092
+
4093
+ // Sequential emits accumulated into ONE builder keep both intact.
4094
+ var accumulated_commands: [16]CanvasCommand = undefined;
4095
+ var accumulated = Builder.init(&accumulated_commands);
4096
+ try emitWidgetTree(&accumulated, first, .{});
4097
+ try emitWidgetTree(&accumulated, second, .{});
4098
+ try expectPresentedFieldText(accumulated.displayList(), 7, "one two");
4099
+ try expectPresentedFieldText(accumulated.displayList(), 8, "AAA BBB");
4100
+ }
4101
+
4102
+ fn expectPresentedFieldText(display_list: DisplayList, id: ObjectId, expected: []const u8) !void {
4103
+ switch (display_list.findCommandById(widgetPartId(id, 3)).?.command) {
4104
+ .draw_text => |text| try std.testing.expectEqualStrings(expected, text.text),
4105
+ else => return error.TestUnexpectedResult,
4106
+ }
4107
+ }
4108
+
3850
4109
  test "search fields clip an overflowing value and keep chrome outside the clip" {
3851
4110
  const long_text = "an overflowing search query that runs past the narrow field";
3852
4111
  const field = Widget{
@@ -144,7 +144,7 @@ pub fn layoutWidgetDepth(
144
144
  // Span paragraphs and span-carrying table cells share the link
145
145
  // hotspot child convention (no spans or no children is a no-op).
146
146
  .text, .data_cell => try layoutTextSpanLinkChildren(widget, content, index, depth, output, len, tokens),
147
- .icon, .image, .avatar, .badge, .button, .toggle_button, .icon_button, .select, .input, .text_field, .search_field, .combobox, .textarea, .tooltip, .menu_item, .status_bar, .segmented_control, .checkbox, .radio, .switch_control, .toggle, .slider, .progress, .separator, .skeleton, .spinner, .chart, .split_divider => {},
147
+ .icon, .image, .avatar, .badge, .button, .toggle_button, .icon_button, .select, .input, .text_field, .search_field, .combobox, .textarea, .tooltip, .menu_item, .status_bar, .segmented_control, .checkbox, .radio, .switch_control, .toggle, .slider, .progress, .separator, .skeleton, .spinner, .chart, .split_divider, .media_surface => {},
148
148
  }
149
149
 
150
150
  // Anchored floating children are excluded from every flow above (they
@@ -284,8 +284,10 @@ fn widgetIsWindowControlContent(widget: Widget) bool {
284
284
  return switch (widget.kind) {
285
285
  // Layout and surface containers: judged through their children.
286
286
  .stack, .row, .column, .grid, .data_grid, .table, .scroll_view, .list, .breadcrumb, .button_group, .pagination, .radio_group, .tabs, .toggle_group, .accordion, .bubble, .resizable, .alert, .card, .dialog, .drawer, .sheet, .panel, .popover, .menu_surface, .dropdown_menu, .list_item, .data_row, .split, .tree, .input_group => false,
287
- // Full-bleed decoration: expected under the cluster.
288
- .separator, .skeleton, .progress, .image, .split_divider => false,
287
+ // Full-bleed decoration: expected under the cluster. Media
288
+ // surfaces join images here — full-bleed video/preview content
289
+ // composites under the OS controls exactly like header cover art.
290
+ .separator, .skeleton, .progress, .image, .split_divider, .media_surface => false,
289
291
  // A text leaf with no bytes and no spans is a hit/semantics
290
292
  // overlay, not readable content.
291
293
  .text => widget.text.len > 0 or widget.spans.len > 0,
@@ -1587,7 +1589,10 @@ fn intrinsicWidgetSizeDepth(widget: Widget, tokens: DesignTokens, depth: usize)
1587
1589
  .split_divider => geometry.SizeF.init(splitDividerExtent(widget), 0),
1588
1590
  // A split fills the space it is given (panes partition it);
1589
1591
  // like scroll viewports it reports no intrinsic size of its own.
1590
- .scroll_view, .image, .split => geometry.SizeF.zero(),
1592
+ // Media surfaces measure like images: the texture is external
1593
+ // content with no natural layout size — definite width/height
1594
+ // (or flex grow) size the surface, exactly like an image leaf.
1595
+ .scroll_view, .image, .split, .media_surface => geometry.SizeF.zero(),
1591
1596
  };
1592
1597
  }
1593
1598
 
@@ -1681,7 +1686,10 @@ fn intrinsicGridChildrenSize(widget: Widget, tokens: DesignTokens, depth: usize)
1681
1686
  cell_height = @max(cell_height, size.height);
1682
1687
  }
1683
1688
  const columns = gridColumnCount(widget.children.len, widget.layout.columns);
1684
- const rows = (widget.children.len + columns - 1) / columns;
1689
+ // `gridRowCount`, not the additive ceil-div: declared columns are
1690
+ // engine input, and `children.len + columns` overflows in safe
1691
+ // builds when a parent intrinsically measures a maxInt-columns grid.
1692
+ const rows = gridRowCount(widget.children.len, columns);
1685
1693
  const gap = nonNegative(widget.layout.gap);
1686
1694
  return paddedIntrinsicSize(widget, geometry.SizeF.init(
1687
1695
  cell_width * @as(f32, @floatFromInt(columns)) + gap * @as(f32, @floatFromInt(columns - 1)),
@@ -935,6 +935,88 @@ test "widget grid layout places children in deterministic cells" {
935
935
  try std.testing.expectEqual(@as(usize, 8), builder.displayList().commandCount());
936
936
  }
937
937
 
938
+ test "widget grid keeps declared column slots when children run short" {
939
+ // Fewer children than the declared columns (a filtered grid): each
940
+ // child keeps the column-slot width the declared count derives —
941
+ // never stretching across the freed row — filling the leading slots
942
+ // and leaving the trailing slots empty.
943
+ const children = [_]Widget{
944
+ .{ .id = 2, .kind = .text, .text = "One" },
945
+ .{ .id = 3, .kind = .text, .text = "Two" },
946
+ };
947
+ const grid = Widget{
948
+ .id = 1,
949
+ .kind = .grid,
950
+ .layout = .{ .gap = 8, .columns = 4 },
951
+ .children = &children,
952
+ };
953
+
954
+ var nodes: [4]WidgetLayoutNode = undefined;
955
+ const layout = try layoutWidgetTree(grid, geometry.RectF.init(0, 0, 424, 40), &nodes);
956
+ try std.testing.expectEqual(@as(usize, 3), layout.nodeCount());
957
+ // Slot width = (424 - 3 gaps of 8) / 4 declared columns = 100.
958
+ try expectLayoutFrame(layout, 2, geometry.RectF.init(0, 0, 100, 40));
959
+ try expectLayoutFrame(layout, 3, geometry.RectF.init(108, 0, 100, 40));
960
+ }
961
+
962
+ test "widget grid row count survives a huge declared column count" {
963
+ const widget_tree = @import("widget_tree.zig");
964
+ // Declared columns are engine input (never validator-bounded): the
965
+ // additive ceil-div `(count + columns - 1) / columns` would overflow
966
+ // in safe builds for a huge but valid usize. A few children under
967
+ // maxInt columns is one row, not a panic.
968
+ const huge = std.math.maxInt(usize);
969
+ try std.testing.expectEqual(@as(usize, 1), widget_tree.gridRowCount(3, huge));
970
+ try std.testing.expectEqual(@as(usize, 1), widget_tree.gridRowCount(1, huge));
971
+ // Zero children (or zero columns) is zero rows in both paths.
972
+ try std.testing.expectEqual(@as(usize, 0), widget_tree.gridRowCount(0, huge));
973
+ try std.testing.expectEqual(@as(usize, 0), widget_tree.gridRowCount(0, 4));
974
+ try std.testing.expectEqual(@as(usize, 0), widget_tree.gridRowCount(5, 0));
975
+ // The ordinary shape still ceil-divides: 5 children over 2 columns
976
+ // is 3 rows.
977
+ try std.testing.expectEqual(@as(usize, 3), widget_tree.gridRowCount(5, 2));
978
+ try std.testing.expectEqual(@as(usize, 2), widget_tree.gridRowCount(4, 2));
979
+ }
980
+
981
+ test "nested widget grid with a huge declared column count measures intrinsically" {
982
+ // The root-grid case above never runs the INTRINSIC path: only a
983
+ // parent that measures its children (a column sizing an unframed
984
+ // child) reaches intrinsicGridChildrenSize, where the row math must
985
+ // be the same overflow-safe helper — the additive ceil-div panicked
986
+ // here while the placement path was already fixed.
987
+ const tiles = [_]Widget{
988
+ .{ .id = 3, .kind = .text, .text = "One" },
989
+ .{ .id = 4, .kind = .text, .text = "Two" },
990
+ .{ .id = 5, .kind = .text, .text = "Three" },
991
+ };
992
+ const grid = Widget{
993
+ .id = 2,
994
+ .kind = .grid,
995
+ .layout = .{ .gap = 8, .columns = std.math.maxInt(usize) },
996
+ .children = &tiles,
997
+ };
998
+ const column = Widget{
999
+ .id = 1,
1000
+ .kind = .column,
1001
+ .children = &[_]Widget{grid},
1002
+ };
1003
+
1004
+ // The direct measurement: three children under maxInt columns is
1005
+ // ONE row, so the grid's intrinsic height is one cell (no row gap).
1006
+ const cell = support.intrinsicWidgetSize(tiles[0], .{});
1007
+ const intrinsic = support.intrinsicWidgetSize(grid, .{});
1008
+ try std.testing.expectEqual(cell.height, intrinsic.height);
1009
+
1010
+ // And the full pass: the column measures the unframed grid child
1011
+ // intrinsically while laying out — no panic, one grid row.
1012
+ var nodes: [8]WidgetLayoutNode = undefined;
1013
+ const layout = try layoutWidgetTree(column, geometry.RectF.init(0, 0, 400, 120), &nodes);
1014
+ try std.testing.expectEqual(@as(usize, 5), layout.nodeCount());
1015
+ const first = layout.findById(3).?;
1016
+ const last = layout.findById(5).?;
1017
+ try std.testing.expectEqual(first.frame.y, last.frame.y);
1018
+ }
1019
+
938
1020
  test "widget virtualized grid lays out visible cells by row" {
939
1021
  const children = [_]Widget{
940
1022
  .{ .id = 2, .kind = .button, .text = "Zero" },
@@ -113,46 +113,28 @@ pub const sliderWidgetKnobRect = widget_render_controls.sliderWidgetKnobRect;
113
113
 
114
114
  const max_widget_depth: usize = 32;
115
115
 
116
- // Widget-built path elements (`.chart` lines/bands, the `.spinner` arc
117
- // and segments, the `.checkbox` check mark — unlike icons, whose
118
- // elements are comptime-static) allocate from the display-list builder's
119
- // own store (`Builder.allocPathElements`), so the emitted commands'
120
- // element slices share the builder's lifetime instead of pointing into
121
- // shared scratch that a later emission could overwrite.
122
-
123
- /// Frame-lifetime scratch for formatted chart label text (y tick values
124
- /// and hover-detail rows): `drawText` commands slice into it. The event
125
- /// loop is single-threaded and the runtime copies the display list into
126
- /// per-view storage within the same emit call stack, so one threadlocal
127
- /// buffer per frame is sound — reset at each emit entry point. Overflow
128
- /// fails loudly by budget name.
129
- // Lazily heap-allocated per thread (the label pool plus the chart
130
- // polyline scratch below): init on first emit/chart use of a thread —
131
- // the emitting thread by construction — so threads that never emit
132
- // widgets never allocate it. The arrays carry no default and stay
133
- // uninitialized, exactly like the `= undefined` statics they replace.
116
+ // Emit-built bytes that emitted commands RETAIN live in the builder,
117
+ // never in per-emit-reset scratch: path elements (`.chart` lines/bands,
118
+ // the `.spinner` arc and segments, the `.checkbox` check mark — unlike
119
+ // icons, whose elements are comptime-static) allocate from
120
+ // `Builder.allocPathElements`, formatted chart labels from
121
+ // `Builder.allocChartLabelBytes`, and presented single-line values from
122
+ // `Builder.allocTextBytes`, so every emitted command's slices share the
123
+ // builder's lifetime — a display list accumulated across several emit
124
+ // calls, or held while another builder emits, stays intact.
125
+
126
+ /// Transient chart scratch, lazily heap-allocated per thread: init on
127
+ /// first chart use of a thread — the emitting thread by construction —
128
+ /// so threads that never emit charts never allocate it. Purely
129
+ /// intra-emit (polyline points are copied into the builder's
130
+ /// path-element store before the emitter returns), so nothing emitted
131
+ /// retains slices into it. The array carries no default and stays
132
+ /// uninitialized, exactly like the `= undefined` static it replaces.
134
133
  const WidgetRenderScratch = struct {
135
- frame_label_bytes: [chart_model.max_chart_label_bytes_per_frame]u8,
136
- frame_label_len: usize = 0,
137
134
  chart_polyline_points: [chart_model.max_chart_points_per_series]geometry.PointF,
138
135
  };
139
136
  const widget_render_scratch = @import("lazy_tls.zig").LazyTls(WidgetRenderScratch);
140
137
 
141
- fn resetFrameLabelScratch() void {
142
- widget_render_scratch.get().frame_label_len = 0;
143
- }
144
-
145
- /// Persist a formatted label into the frame scratch so the emitted
146
- /// command outlives the local formatting buffer.
147
- fn allocFrameLabelBytes(text: []const u8) Error![]const u8 {
148
- const scratch = widget_render_scratch.get();
149
- if (scratch.frame_label_len + text.len > scratch.frame_label_bytes.len) return error.ChartLabelBytesFull;
150
- const start = scratch.frame_label_len;
151
- scratch.frame_label_len += text.len;
152
- @memcpy(scratch.frame_label_bytes[start..scratch.frame_label_len], text);
153
- return scratch.frame_label_bytes[start..scratch.frame_label_len];
154
- }
155
-
156
138
  /// Frame-lifetime scratch (same single-threaded emit contract as the
157
139
  /// label scratch above) holding the root bounds of the tree being
158
140
  /// emitted: the rect a modal surface's scrim covers. Chrome emission
@@ -161,7 +143,6 @@ fn allocFrameLabelBytes(text: []const u8) Error![]const u8 {
161
143
  threadlocal var scrim_viewport: ?geometry.RectF = null;
162
144
 
163
145
  pub fn emitWidgetTree(builder: *Builder, widget: Widget, tokens: DesignTokens) Error!void {
164
- resetFrameLabelScratch();
165
146
  scrim_viewport = widget.frame.normalized();
166
147
  try emitWidgetDepth(builder, widget, tokens, 0);
167
148
  }
@@ -171,7 +152,6 @@ pub fn emitWidgetLayout(builder: *Builder, layout: anytype, tokens: DesignTokens
171
152
  }
172
153
 
173
154
  pub fn emitWidgetLayoutWithState(builder: *Builder, layout: anytype, tokens: DesignTokens, state: WidgetRenderState) Error!void {
174
- resetFrameLabelScratch();
175
155
  scrim_viewport = widgetLayoutRootBounds(layout);
176
156
  try emitWidgetLayoutChildren(builder, layout, null, tokens, state);
177
157
  try emitWidgetLayoutAnchored(builder, layout, tokens, state);
@@ -254,6 +234,7 @@ fn emitWidgetDepthContent(builder: *Builder, widget: Widget, tokens: DesignToken
254
234
  .text => try emitTextWidget(builder, paint_widget, tokens),
255
235
  .icon => try emitIconWidget(builder, paint_widget, tokens),
256
236
  .image => try emitImageWidget(builder, paint_widget),
237
+ .media_surface => try emitMediaSurfaceWidget(builder, paint_widget),
257
238
  .avatar => try emitAvatarWidget(builder, paint_widget, tokens),
258
239
  .badge => try emitBadgeWidget(builder, paint_widget, tokens),
259
240
  .button, .toggle_button, .toggle => try widget_render_controls.emitButtonWidget(builder, paint_widget, tokens),
@@ -588,6 +569,7 @@ fn emitWidgetLayoutNodeContent(
588
569
  .text => try emitTextWidget(builder, paint_widget, tokens),
589
570
  .icon => try emitIconWidget(builder, paint_widget, tokens),
590
571
  .image => try emitImageWidget(builder, paint_widget),
572
+ .media_surface => try emitMediaSurfaceWidget(builder, paint_widget),
591
573
  .avatar => try emitAvatarWidget(builder, paint_widget, tokens),
592
574
  .badge => try emitBadgeWidget(builder, paint_widget, tokens),
593
575
  .button, .toggle_button, .toggle => try widget_render_controls.emitButtonWidget(builder, paint_widget, tokens),
@@ -1150,6 +1132,62 @@ fn emitImageWidget(builder: *Builder, widget: Widget) Error!void {
1150
1132
  if (clips_image) try builder.popClip();
1151
1133
  }
1152
1134
 
1135
+ /// The deterministic placeholder color a media surface paints under its
1136
+ /// texture: a muted flat fill DERIVED FROM THE SURFACE ID alone, so
1137
+ /// every renderer — and every replay, with or without a producer
1138
+ /// attached — derives the identical pixels from the identical display
1139
+ /// list. Channels land in the 0x40..0x7F band: distinct per surface,
1140
+ /// never garish, honest "no frame yet" chrome.
1141
+ pub fn mediaSurfacePlaceholderColor(surface_id: u64) Color {
1142
+ const hash = std.hash.Wyhash.hash(0, std.mem.asBytes(&surface_id));
1143
+ return Color.rgba8(
1144
+ 0x40 + @as(u8, @truncate(hash)) % 0x40,
1145
+ 0x40 + @as(u8, @truncate(hash >> 8)) % 0x40,
1146
+ 0x40 + @as(u8, @truncate(hash >> 16)) % 0x40,
1147
+ 0xff,
1148
+ );
1149
+ }
1150
+
1151
+ /// The media surface: an id-derived placeholder fill with the surface's
1152
+ /// externally produced texture composited over it. ONE display list
1153
+ /// serves both renderers — the texture rides a `draw_image` whose
1154
+ /// resource is marked `presentation_only`, so the deterministic
1155
+ /// reference renderer (goldens, screenshots, replay pixel marks) skips
1156
+ /// the draw and shows the placeholder, while live GPU/packet hosts
1157
+ /// composite the real texture. `image_id` is the SURFACE id (0 = the
1158
+ /// unbound sentinel: placeholder only, like an image leaf with id 0
1159
+ /// draws nothing).
1160
+ fn emitMediaSurfaceWidget(builder: *Builder, widget: Widget) Error!void {
1161
+ if (widget.image_id == 0 or widget.frame.normalized().isEmpty()) return;
1162
+ const radius = Radius.all(widget.style.radius orelse 0);
1163
+ try builder.fillRoundedRect(.{
1164
+ .id = widgetPartId(widget.id, 1),
1165
+ .rect = widget.frame,
1166
+ .radius = radius,
1167
+ .fill = colorFill(mediaSurfacePlaceholderColor(widget.image_id)),
1168
+ });
1169
+ // Cover fit expands the drawn texture past the widget frame on one
1170
+ // axis; the rectangular clip is what crops the overflow — exactly
1171
+ // emitImageWidget's cover clip. The draw's own radius mask below is
1172
+ // NOT a substitute: hosts that only mask radii paint a zero-radius
1173
+ // cover surface outside its bounds, over its siblings.
1174
+ const clips_image = widget.image_fit == .cover;
1175
+ if (clips_image) try builder.pushClip(.{ .id = widgetPartId(widget.id, 3), .rect = widget.frame });
1176
+ try builder.drawImage(.{
1177
+ .id = widgetPartId(widget.id, 2),
1178
+ .image_id = canvas.mediaSurfaceTextureImageId(widget.image_id),
1179
+ .src = widget.image_src,
1180
+ .dst = widget.frame,
1181
+ .opacity = widget.image_opacity,
1182
+ .fit = widget.image_fit,
1183
+ .sampling = widget.image_sampling,
1184
+ // The render plan flattens clip stacks to rects, so a rounded
1185
+ // surface masks on the draw itself (the avatar convention).
1186
+ .radius = radius,
1187
+ });
1188
+ if (clips_image) try builder.popClip();
1189
+ }
1190
+
1153
1191
  fn emitAvatarWidget(builder: *Builder, widget: Widget, tokens: DesignTokens) Error!void {
1154
1192
  const visual = componentControlVisualTokens(widget, tokens);
1155
1193
  const radius = componentPillRadius(widget, visual, widget.frame.height * 0.5);
@@ -1830,7 +1868,7 @@ fn emitChartAxisLabels(builder: *Builder, widget: Widget, tokens: DesignTokens,
1830
1868
  for (0..lattice.count) |ordinal| {
1831
1869
  const value = lattice.value(ordinal);
1832
1870
  const formatted = chart_model.formatChartValue(&buffer, value, lattice.decimals);
1833
- const text = try allocFrameLabelBytes(formatted);
1871
+ const text = try builder.allocChartLabelBytes(formatted);
1834
1872
  const width = measureTextWidthForFont(tokens.text_measure, tokens.typography.font_id, text, size);
1835
1873
  const line_y = chartMapY(value, domain, plot, 0);
1836
1874
  const top = std.math.clamp(line_y - line_height * 0.5, content.y, @max(content.y, content.maxY() - line_height));
@@ -2334,7 +2372,7 @@ fn emitChartHoverDetail(builder: *Builder, widget: Widget, tokens: DesignTokens,
2334
2372
  const line_height = size * 1.25;
2335
2373
  var title_buffer: [chart_model.max_chart_value_label_bytes]u8 = undefined;
2336
2374
  const title = chartHoverDetailTitle(data, detail.index, &title_buffer);
2337
- const title_text = try allocFrameLabelBytes(title);
2375
+ const title_text = try builder.allocChartLabelBytes(title);
2338
2376
  var row_y = card.y + chart_detail_pad_v;
2339
2377
  try builder.drawText(.{
2340
2378
  .id = chartCommandId(widget.id, chart_hover_seed, 0, 4),
@@ -2364,7 +2402,7 @@ fn emitChartHoverDetail(builder: *Builder, widget: Widget, tokens: DesignTokens,
2364
2402
  .text = chartDetailRowName(series),
2365
2403
  });
2366
2404
  var value_buffer: [chart_model.max_chart_value_label_bytes]u8 = undefined;
2367
- const value_text = try allocFrameLabelBytes(chart_model.formatChartValue(&value_buffer, series.values[detail.index], decimals));
2405
+ const value_text = try builder.allocChartLabelBytes(chart_model.formatChartValue(&value_buffer, series.values[detail.index], decimals));
2368
2406
  const value_width = measureTextWidthForFont(tokens.text_measure, tokens.typography.font_id, value_text, size);
2369
2407
  try builder.drawText(.{
2370
2408
  .id = chartCommandId(widget.id, chart_hover_row_seed, series_index, 2),
@@ -463,7 +463,12 @@ pub fn emitTextFieldWidget(builder: *Builder, widget: Widget, tokens: DesignToke
463
463
  const clip_rect = widgetTextInputClipRect(widget, tokens, text_size, text_inset, layout_options);
464
464
  const origin = widgetTextInputOrigin(widget, tokens, text_size, text_inset, layout_options);
465
465
  const text_color = widgetForegroundColor(widget, tokens, visual.foreground orelse tokens.colors.text);
466
- const draw_text = widgetTextInputDrawText(widget, tokens, text_size, origin, text_color, layout_options);
466
+ var draw_text = widgetTextInputDrawText(widget, tokens, text_size, origin, text_color, layout_options);
467
+ // A presented value (single-line kind holding a line break) lives in
468
+ // shared scratch; persist it into the builder so this widget's
469
+ // emitted commands survive the rest of the walk AND any later emit
470
+ // while this display list is held. No-op for the ordinary raw value.
471
+ draw_text.text = widget_text_input.persistWidgetTextInputPresentedText(builder, widget.text, draw_text.text);
467
472
  const selection_range = widgetTextSelectionRange(widget);
468
473
  const composition_range = widgetTextCompositionRange(widget);
469
474
  const has_text_affordances = selection_range != null or composition_range != null;
@@ -497,7 +502,9 @@ pub fn emitTextFieldWidget(builder: *Builder, widget: Widget, tokens: DesignToke
497
502
  }
498
503
  }
499
504
  const placeholder = widgetPlaceholder(widget);
500
- const visible_text = if (widget.text.len > 0) widget.text else placeholder;
505
+ // A non-empty value paints the presented bytes the draw text already
506
+ // carries; only an empty field swaps in the placeholder.
507
+ const visible_text = if (widget.text.len > 0) draw_text.text else placeholder;
501
508
  if (visible_text.len > 0) {
502
509
  var command = draw_text;
503
510
  command.id = widgetPartId(widget.id, if (has_text_affordances) 4 else 3);
@@ -566,7 +573,11 @@ pub fn emitSearchFieldWidget(builder: *Builder, widget: Widget, tokens: DesignTo
566
573
  const selection_range = widgetTextSelectionRange(widget);
567
574
  const composition_range = widgetTextCompositionRange(widget);
568
575
  const text_color = widgetForegroundColor(widget, tokens, visual.foreground orelse tokens.colors.text);
569
- const draw_text = widgetTextInputDrawText(widget, tokens, text_size, origin, text_color, layout_options);
576
+ var draw_text = widgetTextInputDrawText(widget, tokens, text_size, origin, text_color, layout_options);
577
+ // Presented values persist out of the shared scratch into the
578
+ // builder — the text-field emitter's rule (no-op for ordinary raw
579
+ // values).
580
+ draw_text.text = widget_text_input.persistWidgetTextInputPresentedText(builder, widget.text, draw_text.text);
570
581
  // Same overflow contract as the text-field emitter: clip only once
571
582
  // the value (or placeholder) overflows the content rect, so the
572
583
  // horizontally scrolled text and its affordances cut at the border.
@@ -600,7 +611,7 @@ pub fn emitSearchFieldWidget(builder: *Builder, widget: Widget, tokens: DesignTo
600
611
  }
601
612
  }
602
613
  const placeholder = widgetPlaceholder(widget);
603
- const visible_text = if (widget.text.len > 0) widget.text else placeholder;
614
+ const visible_text = if (widget.text.len > 0) draw_text.text else placeholder;
604
615
  if (visible_text.len > 0) {
605
616
  var command = draw_text;
606
617
  command.id = widgetPartId(widget.id, 9);