@native-sdk/cli 0.5.2 → 0.5.4

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 (123) hide show
  1. package/build/app.zig +41 -1
  2. package/build.zig +264 -3
  3. package/native-sdk.d.ts +17 -1
  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/rt/rt.zig +138 -3
  8. package/packages/core/sdk/core.ts +225 -3
  9. package/packages/core/sdk/events.ts +39 -2
  10. package/packages/core/src/checker.ts +1 -1
  11. package/packages/core/src/emitter.ts +2625 -262
  12. package/packages/core/src/infer.ts +53 -7
  13. package/skill-data/automation/SKILL.md +7 -4
  14. package/skill-data/core/references/app-model-runtime.md +3 -0
  15. package/skill-data/native-ui/SKILL.md +4 -2
  16. package/skill-data/ts-core/SKILL.md +9 -1
  17. package/src/app_runner/root.zig +106 -1
  18. package/src/app_runner/ts_core_main.zig +4 -0
  19. package/src/automation/protocol.zig +20 -1
  20. package/src/embed/host.zig +27 -0
  21. package/src/embed/tests.zig +187 -0
  22. package/src/embed/ui_host.zig +4 -0
  23. package/src/platform/linux/gtk_host.c +112 -3
  24. package/src/platform/linux/root.zig +71 -0
  25. package/src/platform/macos/appkit_host.h +59 -2
  26. package/src/platform/macos/appkit_host.m +663 -34
  27. package/src/platform/macos/cef_host.mm +262 -10
  28. package/src/platform/macos/root.zig +126 -4
  29. package/src/platform/null_platform.zig +298 -5
  30. package/src/platform/null_platform_tests.zig +39 -0
  31. package/src/platform/root.zig +36 -0
  32. package/src/platform/types.zig +247 -8
  33. package/src/platform/windows/root.zig +134 -0
  34. package/src/platform/windows/webview2_host.cpp +152 -40
  35. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  36. package/src/primitives/app_manifest/root.zig +1 -0
  37. package/src/primitives/app_manifest/types.zig +32 -0
  38. package/src/primitives/canvas/chart.zig +7 -7
  39. package/src/primitives/canvas/chart_tests.zig +66 -0
  40. package/src/primitives/canvas/commands.zig +48 -0
  41. package/src/primitives/canvas/events.zig +161 -0
  42. package/src/primitives/canvas/font_ttf.zig +155 -8
  43. package/src/primitives/canvas/font_ttf_tests.zig +686 -0
  44. package/src/primitives/canvas/reference.zig +67 -8
  45. package/src/primitives/canvas/render_fingerprints.zig +8 -0
  46. package/src/primitives/canvas/root.zig +25 -0
  47. package/src/primitives/canvas/tests.zig +1 -0
  48. package/src/primitives/canvas/ui.zig +46 -9
  49. package/src/primitives/canvas/ui_markup.zig +82 -34
  50. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  51. package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
  52. package/src/primitives/canvas/ui_markup_compiled.zig +63 -11
  53. package/src/primitives/canvas/ui_markup_compiled_tests.zig +88 -0
  54. package/src/primitives/canvas/ui_markup_contract.zig +10 -1
  55. package/src/primitives/canvas/ui_markup_contract_tests.zig +50 -0
  56. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  57. package/src/primitives/canvas/ui_markup_tests.zig +55 -9
  58. package/src/primitives/canvas/ui_markup_view.zig +81 -14
  59. package/src/primitives/canvas/ui_markup_view_tests.zig +219 -7
  60. package/src/primitives/canvas/ui_schema.zig +29 -0
  61. package/src/primitives/canvas/ui_schema_tests.zig +11 -11
  62. package/src/primitives/canvas/ui_tests.zig +31 -0
  63. package/src/primitives/canvas/vector.zig +258 -141
  64. package/src/primitives/canvas/widget_access.zig +1 -1
  65. package/src/primitives/canvas/widget_builtin_tests.zig +129 -0
  66. package/src/primitives/canvas/widget_layout.zig +9 -4
  67. package/src/primitives/canvas/widget_render.zig +78 -40
  68. package/src/primitives/canvas/widget_render_controls.zig +15 -4
  69. package/src/primitives/canvas/widget_semantics.zig +6 -1
  70. package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
  71. package/src/primitives/canvas/widget_text_input.zig +118 -10
  72. package/src/primitives/canvas/widgets.zig +16 -0
  73. package/src/root.zig +12 -0
  74. package/src/runtime/api.zig +15 -0
  75. package/src/runtime/automation_commands.zig +33 -0
  76. package/src/runtime/automation_widget_dispatch.zig +64 -0
  77. package/src/runtime/bridge_payload.zig +7 -0
  78. package/src/runtime/bridge_responses.zig +5 -0
  79. package/src/runtime/builtin_bridge.zig +9 -4
  80. package/src/runtime/canvas_font_tests.zig +711 -5
  81. package/src/runtime/canvas_fonts.zig +120 -25
  82. package/src/runtime/canvas_frame_helpers.zig +15 -0
  83. package/src/runtime/canvas_image_tests.zig +221 -0
  84. package/src/runtime/canvas_images.zig +78 -19
  85. package/src/runtime/canvas_limits.zig +44 -8
  86. package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
  87. package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
  88. package/src/runtime/canvas_widget_events.zig +24 -4
  89. package/src/runtime/canvas_widget_runtime.zig +24 -7
  90. package/src/runtime/canvas_widget_state.zig +6 -1
  91. package/src/runtime/canvas_widget_text_tests.zig +277 -0
  92. package/src/runtime/core.zig +148 -9
  93. package/src/runtime/effects.zig +1664 -43
  94. package/src/runtime/effects_fetch_tests.zig +89 -1
  95. package/src/runtime/effects_file_tests.zig +41 -1
  96. package/src/runtime/effects_image_tests.zig +1304 -0
  97. package/src/runtime/effects_tests.zig +153 -0
  98. package/src/runtime/flow.zig +44 -0
  99. package/src/runtime/gpu_surface_events.zig +7 -0
  100. package/src/runtime/media_surface.zig +726 -0
  101. package/src/runtime/media_surface_tests.zig +1476 -0
  102. package/src/runtime/root.zig +20 -0
  103. package/src/runtime/session_blobs.zig +689 -0
  104. package/src/runtime/session_journal.zig +111 -20
  105. package/src/runtime/session_record.zig +27 -2
  106. package/src/runtime/session_replay.zig +168 -16
  107. package/src/runtime/session_tests.zig +2118 -4
  108. package/src/runtime/shell_layout.zig +7 -0
  109. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  110. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  111. package/src/runtime/tests.zig +3 -0
  112. package/src/runtime/ts_core_host.zig +439 -4
  113. package/src/runtime/ts_core_host_tests.zig +41 -0
  114. package/src/runtime/ts_ui_app.zig +63 -0
  115. package/src/runtime/ui_app.zig +169 -6
  116. package/src/runtime/ui_app_tests.zig +334 -0
  117. package/src/runtime/ui_app_window_tests.zig +468 -0
  118. package/src/runtime/window_command_bridge_tests.zig +117 -0
  119. package/src/runtime/window_storage.zig +32 -0
  120. package/src/runtime/window_views.zig +61 -1
  121. package/src/tooling/manifest.zig +75 -0
  122. package/src/tooling/raw_manifest.zig +2 -0
  123. package/src/tooling/templates.zig +219 -1
@@ -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
 
@@ -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);
@@ -120,7 +120,12 @@ pub fn semanticRole(widget: Widget) WidgetRole {
120
120
  .menu_surface, .dropdown_menu => .menu,
121
121
  .list => .list,
122
122
  .text, .status_bar => .text,
123
- .icon, .image, .avatar => .image,
123
+ // Media surfaces announce as images: pictorial content whose
124
+ // TEXTURE is presentation chrome — the semantic node carries only
125
+ // role, label, and bounds, never frame-varying producer state, so
126
+ // session fingerprints (hashed over the a11y tree) stay identical
127
+ // whether or not a producer is attached.
128
+ .icon, .image, .avatar, .media_surface => .image,
124
129
  .badge => .text,
125
130
  .button, .toggle_button, .toggle => .button,
126
131
  .icon_button, .select => .button,
@@ -872,6 +872,174 @@ test "widget image emits draw image and exposes image semantics" {
872
872
  try std.testing.expect(display_list.commands[2] == .pop_clip);
873
873
  }
874
874
 
875
+ test "widget media surface cover fit emits the image widget's rectangular clip" {
876
+ // Cover fit expands the drawn texture past the widget frame on one
877
+ // axis; emitImageWidget crops the overflow with a rectangular clip.
878
+ // The media surface draws through the same image pipeline and must
879
+ // carry the SAME clip: its draw-level radius mask is nothing at
880
+ // radius zero, so hosts that only mask radii (the AppKit packet
881
+ // draw) painted a zero-radius cover surface outside its bounds,
882
+ // over its siblings.
883
+ const surface_id: u64 = 5;
884
+ const row_children = [_]Widget{
885
+ .{
886
+ .id = 21,
887
+ .kind = .media_surface,
888
+ .frame = geometry.RectF.init(0, 0, 80, 48),
889
+ .image_id = surface_id,
890
+ .image_fit = .cover,
891
+ },
892
+ .{
893
+ .id = 22,
894
+ .kind = .image,
895
+ .frame = geometry.RectF.init(0, 0, 40, 48),
896
+ .image_id = 42,
897
+ },
898
+ };
899
+ const root = Widget{ .id = 20, .kind = .row, .children = &row_children };
900
+
901
+ var nodes: [3]WidgetLayoutNode = undefined;
902
+ const layout = try layoutWidgetTree(root, geometry.RectF.init(0, 0, 160, 48), &nodes);
903
+ var commands: [8]CanvasCommand = undefined;
904
+ var builder = Builder.init(&commands);
905
+ try layout.emitDisplayList(&builder, .{});
906
+ const display_list = builder.displayList();
907
+ try std.testing.expectEqual(@as(usize, 5), display_list.commandCount());
908
+ const media_frame = geometry.RectF.init(0, 0, 80, 48);
909
+ switch (display_list.commands[0]) {
910
+ .fill_rounded_rect => |fill| {
911
+ try std.testing.expectEqual(@as(ObjectId, widgetPartId(21, 1)), fill.id);
912
+ try expectRect(media_frame, fill.rect);
913
+ },
914
+ else => return error.TestUnexpectedResult,
915
+ }
916
+ switch (display_list.commands[1]) {
917
+ .push_clip => |clip| {
918
+ try std.testing.expectEqual(@as(ObjectId, widgetPartId(21, 3)), clip.id);
919
+ try expectRect(media_frame, clip.rect);
920
+ // A rectangular bounds crop, exactly like the image
921
+ // widget's — rounding stays on the draw's own mask.
922
+ try std.testing.expectEqual(@as(f32, 0), clip.radius.top_left);
923
+ },
924
+ else => return error.TestUnexpectedResult,
925
+ }
926
+ switch (display_list.commands[2]) {
927
+ .draw_image => |draw| {
928
+ try std.testing.expectEqual(@as(ObjectId, widgetPartId(21, 2)), draw.id);
929
+ try std.testing.expectEqual(canvas.mediaSurfaceTextureImageId(surface_id), draw.image_id);
930
+ try std.testing.expectEqual(ImageFit.cover, draw.fit);
931
+ },
932
+ else => return error.TestUnexpectedResult,
933
+ }
934
+ // The clip closes BEFORE the sibling paints: the sibling's own draw
935
+ // is never cropped by the media surface's frame.
936
+ try std.testing.expect(display_list.commands[3] == .pop_clip);
937
+ switch (display_list.commands[4]) {
938
+ .draw_image => |draw| try std.testing.expectEqual(@as(ObjectId, widgetPartId(22, 1)), draw.id),
939
+ else => return error.TestUnexpectedResult,
940
+ }
941
+
942
+ // Non-cover fits expand nothing and emit no clip — unchanged.
943
+ const contained = Widget{
944
+ .id = 21,
945
+ .kind = .media_surface,
946
+ .frame = media_frame,
947
+ .image_id = surface_id,
948
+ .image_fit = .stretch,
949
+ };
950
+ var contained_nodes: [1]WidgetLayoutNode = undefined;
951
+ const contained_layout = try layoutWidgetTree(contained, media_frame, &contained_nodes);
952
+ var contained_commands: [4]CanvasCommand = undefined;
953
+ var contained_builder = Builder.init(&contained_commands);
954
+ try contained_layout.emitDisplayList(&contained_builder, .{});
955
+ try std.testing.expectEqual(@as(usize, 2), contained_builder.displayList().commandCount());
956
+
957
+ // The rounded cover case keeps its draw-level radius mask AND gains
958
+ // the same bounds crop (the mask lies inside the rect, so pixels
959
+ // are unchanged where the radius already masked).
960
+ const rounded = Widget{
961
+ .id = 21,
962
+ .kind = .media_surface,
963
+ .frame = media_frame,
964
+ .image_id = surface_id,
965
+ .image_fit = .cover,
966
+ .style = .{ .radius = 12 },
967
+ };
968
+ var rounded_nodes: [1]WidgetLayoutNode = undefined;
969
+ const rounded_layout = try layoutWidgetTree(rounded, media_frame, &rounded_nodes);
970
+ var rounded_commands: [4]CanvasCommand = undefined;
971
+ var rounded_builder = Builder.init(&rounded_commands);
972
+ try rounded_layout.emitDisplayList(&rounded_builder, .{});
973
+ const rounded_list = rounded_builder.displayList();
974
+ try std.testing.expectEqual(@as(usize, 4), rounded_list.commandCount());
975
+ try std.testing.expect(rounded_list.commands[1] == .push_clip);
976
+ switch (rounded_list.commands[2]) {
977
+ .draw_image => |draw| try std.testing.expectEqual(@as(f32, 12), draw.radius.top_left),
978
+ else => return error.TestUnexpectedResult,
979
+ }
980
+ try std.testing.expect(rounded_list.commands[3] == .pop_clip);
981
+ }
982
+
983
+ test "a composited cover media texture is byte-contained in the reference render" {
984
+ // Containment pin for the deterministic path: the render planner
985
+ // already crops every draw to its requested `dst` (command bounds),
986
+ // so reference renders — goldens, screenshots — were contained
987
+ // before the cover clip and must stay byte-identical with it. The
988
+ // clip COMMAND itself (pinned in the test above) is what protects
989
+ // packet hosts, which replay the display list and expand cover at
990
+ // draw time without the planner's bounds. The reference renderer
991
+ // skips presentation-only media textures by policy; feeding the
992
+ // texture in as an ordinary resource under the media-derived id
993
+ // exercises the draw geometry itself.
994
+ const surface_id: u64 = 5;
995
+ const media_frame = geometry.RectF.init(8, 4, 8, 16);
996
+ const widget = Widget{
997
+ .id = 9,
998
+ .kind = .media_surface,
999
+ .frame = media_frame,
1000
+ .image_id = surface_id,
1001
+ .image_fit = .cover,
1002
+ };
1003
+ var nodes: [1]WidgetLayoutNode = undefined;
1004
+ const layout = try layoutWidgetTree(widget, media_frame, &nodes);
1005
+ var commands: [4]CanvasCommand = undefined;
1006
+ var builder = Builder.init(&commands);
1007
+ try layout.emitDisplayList(&builder, .{});
1008
+
1009
+ // A square texture in a 1:2 frame: cover expands the draw to
1010
+ // (4,4,16,16) — four columns past BOTH vertical edges of the frame,
1011
+ // where a sibling would sit.
1012
+ const texture_pixels = [_]u8{
1013
+ 255, 255, 255, 255, 255, 255, 255, 255,
1014
+ 255, 255, 255, 255, 255, 255, 255, 255,
1015
+ };
1016
+ const images = [_]ReferenceImage{.{
1017
+ .id = canvas.mediaSurfaceTextureImageId(surface_id),
1018
+ .width = 2,
1019
+ .height = 2,
1020
+ .pixels = &texture_pixels,
1021
+ }};
1022
+
1023
+ var render_commands: [8]RenderCommand = undefined;
1024
+ const plan = try builder.displayList().renderPlan(&render_commands);
1025
+ var pixels: [32 * 24 * 4]u8 = undefined;
1026
+ @memset(&pixels, 0);
1027
+ const surface = (try ReferenceRenderSurface.init(32, 24, &pixels)).withImages(&images);
1028
+ try surface.renderPass(.{
1029
+ .commands = plan.commands,
1030
+ .surface_size = geometry.SizeF.init(32, 24),
1031
+ .full_repaint = true,
1032
+ }, Color.rgb8(0, 0, 0));
1033
+
1034
+ // Inside the frame: the texture (cover fills the whole frame).
1035
+ try support.expectPixelRgba8(.{ 255, 255, 255, 255 }, surface, 12, 12);
1036
+ // The fit-expanded bleed columns — left (x in [4,8)) and right
1037
+ // (x in [16,20)) of the frame — stay background: the emitted clip
1038
+ // contains the draw to the widget's bytes.
1039
+ try support.expectPixelRgba8(.{ 0, 0, 0, 255 }, surface, 5, 12);
1040
+ try support.expectPixelRgba8(.{ 0, 0, 0, 255 }, surface, 18, 12);
1041
+ }
1042
+
875
1043
  test "widget text fields expose textbox semantics and render focused chrome" {
876
1044
  const text_field = Widget{
877
1045
  .id = 8,
@@ -8,6 +8,7 @@ const widget_access = @import("widget_access.zig");
8
8
  const widget_metrics = @import("widget_metrics.zig");
9
9
 
10
10
  const FontId = @import("root.zig").FontId;
11
+ const Builder = @import("commands.zig").Builder;
11
12
  const Color = drawing_model.Color;
12
13
  const DrawText = text_model.DrawText;
13
14
  const TextWrap = text_model.TextWrap;
@@ -36,6 +37,96 @@ pub fn widgetPlaceholder(widget: Widget) []const u8 {
36
37
  };
37
38
  }
38
39
 
40
+ // ---------------------------------------------------------------------------
41
+ // Single-line presentation: a single-line field NEVER paints a second line.
42
+ //
43
+ // Edits into single-line kinds are sanitized at the runtime's derivation
44
+ // seam (`sanitizedSingleLineTextInputEvent`), but the retained VALUE can
45
+ // still legitimately hold a line break — a model-set value, an old journal,
46
+ // a host API write. The honest presentation for such a value is the one
47
+ // the paint layers already give whitespace: `\n`/`\r` lay out and measure
48
+ // as a SPACE (nothing inked, one space advance) instead of breaking the
49
+ // line. The substitution is byte-for-byte (1 → 1), so every caret,
50
+ // selection, composition, and hit-test offset computed against the
51
+ // presented bytes addresses the raw value unchanged — and because the
52
+ // presented bytes contain no `\n`, the single-line layout (`wrap = .none`)
53
+ // produces exactly one line on every renderer: the GPU engine, the
54
+ // reference renderer, and packet hosts that draw engine-broken lines
55
+ // verbatim. Copy, semantics, and automation still read the RAW value.
56
+ //
57
+ // The clip is the independent second guard: `widgetTextInputClipsText`
58
+ // forces the content-rect clip whenever the raw value holds a break, so
59
+ // even a path that somehow lays out raw bytes (presentation scratch
60
+ // exhausted) stays inside the field's border.
61
+
62
+ /// The editable kinds that present single-line: every text-input kind but
63
+ /// the textarea (the one genuinely multi-line editor). Deliberately
64
+ /// includes the tall wrapping `text_field` variant — its kind contract is
65
+ /// single-line entry, and `\n`-as-space is consistent under word wrap too.
66
+ fn widgetTextInputPresentsSingleLine(kind: widget_model.WidgetKind) bool {
67
+ return kind != .textarea and widget_access.widgetTextInputKind(kind);
68
+ }
69
+
70
+ fn textContainsLineBreakByte(text: []const u8) bool {
71
+ return std.mem.indexOfAny(u8, text, "\r\n") != null;
72
+ }
73
+
74
+ /// Whether this widget's PAINTED text differs from its raw value (a
75
+ /// single-line kind whose value holds a line break). Render emitters use
76
+ /// it to persist the presented bytes for the display list's lifetime.
77
+ pub fn widgetTextInputTextNeedsPresentation(widget: Widget) bool {
78
+ return widgetTextInputPresentsSingleLine(widget.kind) and textContainsLineBreakByte(widget.text);
79
+ }
80
+
81
+ /// Presentation scratch, lazily heap-allocated per thread (the
82
+ /// `lazy_tls` pattern all canvas scratch uses; the event loop is
83
+ /// single-threaded): `slot` holds ONE presented text at a time — the
84
+ /// transient buffer the geometry seams (caret, selection, hit-test,
85
+ /// scroll measures) substitute into. Every entry point re-derives it,
86
+ /// and within one widget's computation repeated derivations write
87
+ /// 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
+ /// slice into it: render emitters persist presented bytes into the
90
+ /// display-list builder via `persistWidgetTextInputPresentedText`, whose
91
+ /// storage lives exactly as long as the emitted commands do.
92
+ const WidgetTextPresentationScratch = struct {
93
+ slot: [65536]u8,
94
+ };
95
+ const widget_text_presentation_scratch = @import("lazy_tls.zig").LazyTls(WidgetTextPresentationScratch);
96
+
97
+ /// The text a single-line field lays out, measures, and paints: the raw
98
+ /// value with `\n`/`\r` presented as spaces (same byte length). Returns
99
+ /// the raw slice untouched when nothing needs presenting — the
100
+ /// overwhelmingly common case costs one byte scan.
101
+ pub fn widgetTextInputPresentedText(widget: Widget) []const u8 {
102
+ if (!widgetTextInputPresentsSingleLine(widget.kind)) return widget.text;
103
+ return presentedSingleLineText(widget.text);
104
+ }
105
+
106
+ fn presentedSingleLineText(text: []const u8) []const u8 {
107
+ if (!textContainsLineBreakByte(text)) return text;
108
+ const scratch = widget_text_presentation_scratch.get();
109
+ if (text.len > scratch.slot.len) return text;
110
+ for (text, 0..) |byte, index| {
111
+ scratch.slot[index] = if (byte == '\n' or byte == '\r') ' ' else byte;
112
+ }
113
+ return scratch.slot[0..text.len];
114
+ }
115
+
116
+ /// Persist presented bytes into the display-list BUILDER so an emitted
117
+ /// command outlives the shared slot (a later widget's presentation
118
+ /// overwrites it) — builder-owned storage, because the builder contract
119
+ /// lets a display list accumulate across several emit calls or be held
120
+ /// while another builder emits, and the emitted `draw_text` must stay
121
+ /// intact through both. Falls back to the RAW value — stable,
122
+ /// view-owned storage — when the builder's text store is full (only
123
+ /// possible on a frame already over the per-view draw-text budget); the
124
+ /// forced clip contains that fallback inside the field's border.
125
+ pub fn persistWidgetTextInputPresentedText(builder: *Builder, raw: []const u8, presented: []const u8) []const u8 {
126
+ if (presented.ptr == raw.ptr) return raw;
127
+ return builder.allocTextBytes(presented) catch raw;
128
+ }
129
+
39
130
  pub fn textSelectionForWidgetPoint(widget: Widget, point: geometry.PointF, anchor: ?usize, tokens: DesignTokens) ?TextSelection {
40
131
  const offset = textOffsetForWidgetPoint(widget, point, tokens) orelse return null;
41
132
  const selection = if (anchor) |anchor_offset|
@@ -115,7 +206,10 @@ fn widgetTextInputMaxHorizontalScrollOffset(widget: Widget, tokens: DesignTokens
115
206
  if (options.wrap != .none or widget.text.len == 0) return 0;
116
207
  const viewport = widgetTextInputClipRect(widget, tokens, text_size, text_inset, options);
117
208
  if (viewport.width <= 0) return 0;
118
- const text_width = measureTextWidthForFont(options.measure, tokens.typography.font_id, widget.text, text_size);
209
+ // Measure the PRESENTED bytes — the ones the field lays out and
210
+ // paints — so a value holding a line break scrolls by the same
211
+ // single-line extent it draws.
212
+ const text_width = measureTextWidthForFont(options.measure, tokens.typography.font_id, widgetTextInputPresentedText(widget), text_size);
119
213
  return @max(0, text_width + text_input_caret_reserve - viewport.width);
120
214
  }
121
215
 
@@ -220,9 +314,10 @@ pub fn textInputCaretVisibleScrollOffsetForWidget(widget: Widget, tokens: Design
220
314
 
221
315
  // Caret x in content (unscrolled) coordinates: the width of the value
222
316
  // up to the caret byte, measured on the same seam the line layout and
223
- // selection rects measure with.
317
+ // selection rects measure with — the PRESENTED bytes (byte offsets
318
+ // are interchangeable: the presentation substitutes 1:1).
224
319
  const caret_offset = snapTextOffset(widget.text, selection.focus);
225
- const caret_x = measureTextWidthForFont(options.measure, tokens.typography.font_id, widget.text[0..caret_offset], text_size);
320
+ const caret_x = measureTextWidthForFont(options.measure, tokens.typography.font_id, widgetTextInputPresentedText(widget)[0..caret_offset], text_size);
226
321
  const margin = textInputCaretVisibleMargin(viewport.width);
227
322
  var next = clamped;
228
323
  const scrolled_in = caret_x + text_input_caret_reserve + margin - viewport.width;
@@ -239,6 +334,11 @@ pub fn textInputCaretVisibleScrollOffsetForWidget(widget: Widget, tokens: Design
239
334
  /// exactly as before fields scrolled.
240
335
  pub fn widgetTextInputClipsText(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) bool {
241
336
  if (widget.kind == .textarea) return true;
337
+ // A raw value holding a line break ALWAYS clips: presentation lays it
338
+ // out as one line, but the clip is the independent guard that keeps
339
+ // even a raw-bytes fallback (presentation scratch exhausted) from
340
+ // painting a second line outside the field's border.
341
+ if (widgetTextInputTextNeedsPresentation(widget)) return true;
242
342
  if (options.wrap != .none) return false;
243
343
  if (widgetTextInputMaxHorizontalScrollOffset(widget, tokens, text_size, text_inset, options) > 0) return true;
244
344
  if (widget.text.len == 0) {
@@ -260,16 +360,19 @@ fn textInputContentExtentForWidgetWithOptions(widget: Widget, font_id: FontId, t
260
360
  }
261
361
 
262
362
  fn widgetTextInputLineCount(widget: Widget, font_id: FontId, text_size: f32, options: TextLayoutOptions) usize {
263
- if (widget.text.len == 0) return 1;
363
+ // Count lines over the PRESENTED bytes: a single-line field's value
364
+ // holding a `\n` still lays out (and therefore extends) as one line.
365
+ const text = widgetTextInputPresentedText(widget);
366
+ if (text.len == 0) return 1;
264
367
  var count: usize = 0;
265
368
  var start: usize = 0;
266
- while (start <= widget.text.len) {
267
- const end = nextTextLineEnd(widget.text, start, font_id, text_size, options);
369
+ while (start <= text.len) {
370
+ const end = nextTextLineEnd(text, start, font_id, text_size, options);
268
371
  count += 1;
269
- if (end >= widget.text.len) break;
372
+ if (end >= text.len) break;
270
373
  start = end;
271
- if (start < widget.text.len and widget.text[start] == '\n') start += 1;
272
- while (options.wrap == .word and start < widget.text.len and isTextBreakByte(widget.text[start])) start += 1;
374
+ if (start < text.len and text[start] == '\n') start += 1;
375
+ while (options.wrap == .word and start < text.len and isTextBreakByte(text[start])) start += 1;
273
376
  }
274
377
  return @max(1, count);
275
378
  }
@@ -287,7 +390,12 @@ pub fn widgetTextInputDrawText(
287
390
  .size = text_size,
288
391
  .origin = pixelSnapTextPoint(tokens, origin),
289
392
  .color = color,
290
- .text = widget.text,
393
+ // The presented bytes: identical to `widget.text` unless a
394
+ // single-line field's value holds a line break (then `\n`/`\r`
395
+ // present as spaces — one line, same byte offsets). Geometry
396
+ // consumers (caret, selection, hit-test) and the paint emitters
397
+ // both build from this seam, so they can never disagree.
398
+ .text = widgetTextInputPresentedText(widget),
291
399
  .text_layout = options,
292
400
  };
293
401
  }