@native-sdk/cli 0.4.1 → 0.4.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 (48) hide show
  1. package/assets/native-sdk.manifest +17 -1
  2. package/build/app.zig +40 -1
  3. package/build.zig +49 -3
  4. package/package.json +10 -9
  5. package/src/platform/macos/appkit_host.m +21 -6
  6. package/src/platform/windows/webview2_host.cpp +377 -72
  7. package/src/primitives/canvas/chart.zig +8 -7
  8. package/src/primitives/canvas/chart_tests.zig +2 -2
  9. package/src/primitives/canvas/commands.zig +27 -0
  10. package/src/primitives/canvas/gpu.zig +8 -0
  11. package/src/primitives/canvas/markdown_tests.zig +1 -1
  12. package/src/primitives/canvas/reference.zig +215 -7
  13. package/src/primitives/canvas/reference_tests.zig +326 -7
  14. package/src/primitives/canvas/root.zig +3 -0
  15. package/src/primitives/canvas/serialization.zig +16 -3
  16. package/src/primitives/canvas/test_support.zig +3 -0
  17. package/src/primitives/canvas/themes/geist.zig +6 -0
  18. package/src/primitives/canvas/tokens.zig +7 -0
  19. package/src/primitives/canvas/widget_builtin_tests.zig +439 -3
  20. package/src/primitives/canvas/widget_render.zig +29 -41
  21. package/src/primitives/canvas/widget_render_controls.zig +73 -43
  22. package/src/primitives/canvas/widget_render_style.zig +53 -0
  23. package/src/primitives/canvas/widget_render_surfaces.zig +19 -18
  24. package/src/primitives/canvas/widget_runtime.zig +3 -0
  25. package/src/primitives/canvas/widget_runtime_tests.zig +20 -21
  26. package/src/primitives/canvas/widget_text_input.zig +104 -1
  27. package/src/runtime/canvas_frame_packet_tests.zig +9 -1
  28. package/src/runtime/canvas_frame_patch_tests.zig +39 -1
  29. package/src/runtime/canvas_widget_control_tests.zig +4 -4
  30. package/src/runtime/canvas_widget_runtime.zig +23 -3
  31. package/src/runtime/canvas_widget_text_tests.zig +101 -0
  32. package/src/runtime/ui_app.zig +78 -10
  33. package/src/runtime/ui_app_tests.zig +109 -0
  34. package/src/runtime/ui_app_window_tests.zig +93 -0
  35. package/src/runtime/view.zig +1 -1
  36. package/src/runtime/view_widget_scroll.zig +12 -1
  37. package/src/runtime/view_widget_text.zig +6 -2
  38. package/src/tooling/buildgraph.zig +112 -2
  39. package/src/tooling/junction.zig +247 -0
  40. package/src/tooling/package.zig +45 -0
  41. package/src/tooling/root.zig +1 -0
  42. package/src/tooling/templates.zig +102 -2
  43. package/third_party/webview2/LICENSE.txt +27 -0
  44. package/third_party/webview2/README.md +19 -0
  45. package/third_party/webview2/arm64/WebView2Loader.dll +0 -0
  46. package/third_party/webview2/include/EventToken.h +12 -0
  47. package/third_party/webview2/include/WebView2.h +60636 -0
  48. package/third_party/webview2/x64/WebView2Loader.dll +0 -0
@@ -1907,7 +1907,7 @@ test "widget emitter applies selection and range control tokens" {
1907
1907
  try emitWidgetTree(&builder, .{ .id = 64, .kind = .progress, .frame = geometry.RectF.init(0, 172, 160, 8), .value = 0.5 }, tokens);
1908
1908
 
1909
1909
  const display_list = builder.displayList();
1910
- try std.testing.expectEqual(@as(usize, 18), display_list.commandCount());
1910
+ try std.testing.expectEqual(@as(usize, 17), display_list.commandCount());
1911
1911
  switch (display_list.commands[0]) {
1912
1912
  .fill_rounded_rect => |fill| try expectFillColor(Color.rgb8(30, 50, 70), fill.fill),
1913
1913
  else => return error.TestUnexpectedResult,
@@ -1925,34 +1925,34 @@ test "widget emitter applies selection and range control tokens" {
1925
1925
  else => return error.TestUnexpectedResult,
1926
1926
  }
1927
1927
  switch (display_list.commands[5]) {
1928
- .draw_line => |line| try expectFillColor(Color.rgb8(250, 252, 255), line.stroke.fill),
1928
+ .stroke_path => |stroke| try expectFillColor(Color.rgb8(250, 252, 255), stroke.stroke.fill),
1929
1929
  else => return error.TestUnexpectedResult,
1930
1930
  }
1931
- switch (display_list.commands[8]) {
1931
+ switch (display_list.commands[7]) {
1932
1932
  .fill_rounded_rect => |fill| try expectFillColor(Color.rgb8(34, 70, 108), fill.fill),
1933
1933
  else => return error.TestUnexpectedResult,
1934
1934
  }
1935
- switch (display_list.commands[10]) {
1935
+ switch (display_list.commands[9]) {
1936
1936
  .fill_rounded_rect => |fill| try expectFillColor(Color.rgb8(248, 250, 252), fill.fill),
1937
1937
  else => return error.TestUnexpectedResult,
1938
1938
  }
1939
- switch (display_list.commands[12]) {
1939
+ switch (display_list.commands[11]) {
1940
1940
  .fill_rounded_rect => |fill| try expectFillColor(Color.rgb8(50, 56, 64), fill.fill),
1941
1941
  else => return error.TestUnexpectedResult,
1942
1942
  }
1943
- switch (display_list.commands[13]) {
1943
+ switch (display_list.commands[12]) {
1944
1944
  .fill_rounded_rect => |fill| try expectFillColor(Color.rgb8(38, 76, 114), fill.fill),
1945
1945
  else => return error.TestUnexpectedResult,
1946
1946
  }
1947
- switch (display_list.commands[14]) {
1947
+ switch (display_list.commands[13]) {
1948
1948
  .fill_rounded_rect => |fill| try expectFillColor(Color.rgb8(246, 248, 250), fill.fill),
1949
1949
  else => return error.TestUnexpectedResult,
1950
1950
  }
1951
- switch (display_list.commands[16]) {
1951
+ switch (display_list.commands[15]) {
1952
1952
  .fill_rounded_rect => |fill| try expectFillColor(Color.rgb8(52, 58, 66), fill.fill),
1953
1953
  else => return error.TestUnexpectedResult,
1954
1954
  }
1955
- switch (display_list.commands[17]) {
1955
+ switch (display_list.commands[16]) {
1956
1956
  .fill_rounded_rect => |fill| try expectFillColor(Color.rgb8(40, 80, 120), fill.fill),
1957
1957
  else => return error.TestUnexpectedResult,
1958
1958
  }
@@ -2642,57 +2642,56 @@ test "widget emitter renders checkbox radio switch and slider controls" {
2642
2642
  }, tokens);
2643
2643
 
2644
2644
  const display_list = builder.displayList();
2645
- try std.testing.expectEqual(@as(usize, 19), display_list.commandCount());
2645
+ try std.testing.expectEqual(@as(usize, 18), display_list.commandCount());
2646
2646
  switch (display_list.commands[0]) {
2647
2647
  .fill_rounded_rect => |fill| try expectFillColor(tokens.colors.accent, fill.fill),
2648
2648
  else => return error.TestUnexpectedResult,
2649
2649
  }
2650
- try std.testing.expect(display_list.commands[3] == .draw_line);
2651
- try std.testing.expect(display_list.commands[4] == .draw_line);
2652
- switch (display_list.commands[5]) {
2650
+ try std.testing.expect(display_list.commands[3] == .stroke_path);
2651
+ switch (display_list.commands[4]) {
2653
2652
  .draw_text => |text| try std.testing.expectEqualStrings("Live", text.text),
2654
2653
  else => return error.TestUnexpectedResult,
2655
2654
  }
2656
- switch (display_list.commands[6]) {
2655
+ switch (display_list.commands[5]) {
2657
2656
  .fill_rounded_rect => |fill| try expectFillColor(tokens.colors.surface, fill.fill),
2658
2657
  else => return error.TestUnexpectedResult,
2659
2658
  }
2660
- switch (display_list.commands[9]) {
2659
+ switch (display_list.commands[8]) {
2661
2660
  .fill_rounded_rect => |fill| {
2662
2661
  try std.testing.expectEqual(@as(ObjectId, widgetPartId(11, 4)), fill.id);
2663
2662
  try expectFillColor(tokens.colors.accent, fill.fill);
2664
2663
  },
2665
2664
  else => return error.TestUnexpectedResult,
2666
2665
  }
2667
- switch (display_list.commands[10]) {
2666
+ switch (display_list.commands[9]) {
2668
2667
  .draw_text => |text| try std.testing.expectEqualStrings("Monthly", text.text),
2669
2668
  else => return error.TestUnexpectedResult,
2670
2669
  }
2671
- switch (display_list.commands[11]) {
2670
+ switch (display_list.commands[10]) {
2672
2671
  .fill_rounded_rect => |fill| try expectFillColor(tokens.colors.accent, fill.fill),
2673
2672
  else => return error.TestUnexpectedResult,
2674
2673
  }
2675
2674
  // The switch track is borderless now, so the toggle's label follows
2676
2675
  // its fill and knob directly.
2677
- switch (display_list.commands[13]) {
2676
+ switch (display_list.commands[12]) {
2678
2677
  .draw_text => |text| try std.testing.expectEqualStrings("Mode", text.text),
2679
2678
  else => return error.TestUnexpectedResult,
2680
2679
  }
2681
2680
  // 4px slider rail centered in the 32px row: y = 124 + 14.
2682
- switch (display_list.commands[15]) {
2681
+ switch (display_list.commands[14]) {
2683
2682
  .fill_rounded_rect => |fill| try expectRect(geometry.RectF.init(0, 138, 40, 4), fill.rect),
2684
2683
  else => return error.TestUnexpectedResult,
2685
2684
  }
2686
2685
  // The knob's resting hairline wears the focus-ring neutral; focus
2687
2686
  // adds the offset ring in the same color outside it.
2688
- switch (display_list.commands[17]) {
2687
+ switch (display_list.commands[16]) {
2689
2688
  .stroke_rect => |stroke| {
2690
2689
  try std.testing.expectEqual(@as(ObjectId, widgetPartId(13, 4)), stroke.id);
2691
2690
  try expectFillColor(tokens.colors.focus_ring, stroke.stroke.fill);
2692
2691
  },
2693
2692
  else => return error.TestUnexpectedResult,
2694
2693
  }
2695
- switch (display_list.commands[18]) {
2694
+ switch (display_list.commands[17]) {
2696
2695
  .stroke_rect => |stroke| {
2697
2696
  try std.testing.expectEqual(@as(ObjectId, widgetPartId(13, 5)), stroke.id);
2698
2697
  try std.testing.expectEqual(@as(f32, 3), stroke.stroke.width);
@@ -17,7 +17,9 @@ const TextSelection = text_model.TextSelection;
17
17
  const DesignTokens = token_model.DesignTokens;
18
18
  const Widget = widget_model.Widget;
19
19
  const snapTextSelection = text_model.snapTextSelection;
20
+ const snapTextOffset = text_model.snapTextOffset;
20
21
  const snapTextRange = text_model.snapTextRange;
22
+ const measureTextWidthForFont = text_model.measureTextWidthForFont;
21
23
  const nextTextLineEnd = text_model.nextTextLineEnd;
22
24
  const isTextBreakByte = text_model.isTextBreakByte;
23
25
  const textLineRange = text_model.textLineRange;
@@ -94,6 +96,29 @@ fn widgetTextInputScrollOffset(widget: Widget, tokens: DesignTokens, text_size:
94
96
  return std.math.clamp(widget.value, 0, widgetTextInputMaxScrollOffset(widget, tokens, text_size, text_inset, options));
95
97
  }
96
98
 
99
+ /// Advance the horizontal scroll math reserves past the last glyph so a
100
+ /// caret parked at the end of the value lands inside the clip instead of
101
+ /// exactly on its trailing edge (the caret bar is one point wide).
102
+ const text_input_caret_reserve: f32 = 1;
103
+
104
+ /// The single-line field's horizontal scroll offset: the same retained
105
+ /// `value` channel the textarea scrolls vertically, clamped so the field
106
+ /// never shows trailing emptiness while text could fill the span. Zero
107
+ /// whenever the value fits — a short value renders exactly as an
108
+ /// unscrolled field always has.
109
+ fn widgetTextInputHorizontalScrollOffset(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) f32 {
110
+ if (options.wrap != .none) return 0;
111
+ return std.math.clamp(widget.value, 0, widgetTextInputMaxHorizontalScrollOffset(widget, tokens, text_size, text_inset, options));
112
+ }
113
+
114
+ fn widgetTextInputMaxHorizontalScrollOffset(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) f32 {
115
+ if (options.wrap != .none or widget.text.len == 0) return 0;
116
+ const viewport = widgetTextInputClipRect(widget, tokens, text_size, text_inset, options);
117
+ if (viewport.width <= 0) return 0;
118
+ const text_width = measureTextWidthForFont(options.measure, tokens.typography.font_id, widget.text, text_size);
119
+ return @max(0, text_width + text_input_caret_reserve - viewport.width);
120
+ }
121
+
97
122
  pub fn widgetTextInputOrigin(widget: Widget, tokens: DesignTokens, text_size: f32, inset: f32, options: TextLayoutOptions) geometry.PointF {
98
123
  if (options.wrap != .none) {
99
124
  const scroll_offset = widgetTextInputScrollOffset(widget, tokens, text_size, inset, options);
@@ -102,7 +127,14 @@ pub fn widgetTextInputOrigin(widget: Widget, tokens: DesignTokens, text_size: f3
102
127
  widget.frame.y + widgetTextInputVerticalInset(widget, tokens, text_size, options) + text_size - scroll_offset,
103
128
  );
104
129
  }
105
- return textInputOriginForFrame(widget.frame, text_size, inset);
130
+ // Single-line fields scroll horizontally through the same origin:
131
+ // selection rects, composition underlines, the caret, and hit-testing
132
+ // all derive their geometry from this point, so shifting it moves
133
+ // every text affordance together. The offset stays in logical
134
+ // coordinates here — `widgetTextInputDrawText` snaps the final point.
135
+ const scroll_offset = widgetTextInputHorizontalScrollOffset(widget, tokens, text_size, inset, options);
136
+ const origin = textInputOriginForFrame(widget.frame, text_size, inset);
137
+ return geometry.PointF.init(origin.x - scroll_offset, origin.y);
106
138
  }
107
139
 
108
140
  pub fn widgetTextInputClipRect(widget: Widget, tokens: DesignTokens, text_size: f32, inset: f32, options: TextLayoutOptions) geometry.RectF {
@@ -147,6 +179,77 @@ pub fn clampedTextInputScrollOffsetForWidget(widget: Widget, tokens: DesignToken
147
179
  return std.math.clamp(offset, 0, textInputMaxScrollOffsetForWidget(widget, tokens));
148
180
  }
149
181
 
182
+ /// The furthest a single-line field may scroll horizontally: value width
183
+ /// (plus the caret's own advance) past the visible span, zero when the
184
+ /// value fits or the field wraps.
185
+ pub fn textInputMaxHorizontalScrollOffsetForWidget(widget: Widget, tokens: DesignTokens) f32 {
186
+ if (!widget_access.widgetTextInputKind(widget.kind)) return 0;
187
+ const text_size = widgetTextInputSize(widget, tokens);
188
+ const text_inset = widgetTextInputInset(widget, tokens);
189
+ const options = widgetTextInputLayoutOptions(widget, tokens, text_size, text_inset);
190
+ return widgetTextInputMaxHorizontalScrollOffset(widget, tokens, text_size, text_inset, options);
191
+ }
192
+
193
+ pub fn clampedTextInputHorizontalScrollOffsetForWidget(widget: Widget, tokens: DesignTokens, offset: f32) f32 {
194
+ if (!widget_access.widgetTextInputKind(widget.kind)) return 0;
195
+ return std.math.clamp(offset, 0, textInputMaxHorizontalScrollOffsetForWidget(widget, tokens));
196
+ }
197
+
198
+ /// Keep-visible edge margin for the single-line caret: enough neighbor
199
+ /// glyphs stay in view to give the caret context, shrinking with the
200
+ /// field so tiny spans still fit both margins plus the caret.
201
+ fn textInputCaretVisibleMargin(viewport_width: f32) f32 {
202
+ return @min(8, viewport_width * 0.25);
203
+ }
204
+
205
+ /// The minimally-adjusted horizontal scroll offset that keeps the
206
+ /// selection's focus end visible inside the field's content rect (with a
207
+ /// small edge margin for context). Fields whose value fits, wrapping
208
+ /// fields, and fields without a selection just clamp — the offset only
209
+ /// moves when the caret would otherwise leave the visible span.
210
+ pub fn textInputCaretVisibleScrollOffsetForWidget(widget: Widget, tokens: DesignTokens, offset: f32) f32 {
211
+ const clamped = clampedTextInputHorizontalScrollOffsetForWidget(widget, tokens, offset);
212
+ if (!widget_access.widgetTextInputKind(widget.kind)) return clamped;
213
+ const selection = widget.text_selection orelse return clamped;
214
+ const text_size = widgetTextInputSize(widget, tokens);
215
+ const text_inset = widgetTextInputInset(widget, tokens);
216
+ const options = widgetTextInputLayoutOptions(widget, tokens, text_size, text_inset);
217
+ if (options.wrap != .none) return clamped;
218
+ const viewport = widgetTextInputClipRect(widget, tokens, text_size, text_inset, options);
219
+ if (viewport.width <= 0) return clamped;
220
+
221
+ // Caret x in content (unscrolled) coordinates: the width of the value
222
+ // up to the caret byte, measured on the same seam the line layout and
223
+ // selection rects measure with.
224
+ 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);
226
+ const margin = textInputCaretVisibleMargin(viewport.width);
227
+ var next = clamped;
228
+ const scrolled_in = caret_x + text_input_caret_reserve + margin - viewport.width;
229
+ const scrolled_back = caret_x - margin;
230
+ if (next < scrolled_in) next = scrolled_in;
231
+ if (next > scrolled_back) next = scrolled_back;
232
+ return clampedTextInputHorizontalScrollOffsetForWidget(widget, tokens, next);
233
+ }
234
+
235
+ /// Whether the field's painted text needs the content-rect clip: a
236
+ /// textarea always clips (its overflow scrolls vertically), a single-line
237
+ /// field clips once its value can scroll, and an overflowing placeholder
238
+ /// clips at the same border the value would. Short values emit no clip,
239
+ /// exactly as before fields scrolled.
240
+ pub fn widgetTextInputClipsText(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) bool {
241
+ if (widget.kind == .textarea) return true;
242
+ if (options.wrap != .none) return false;
243
+ if (widgetTextInputMaxHorizontalScrollOffset(widget, tokens, text_size, text_inset, options) > 0) return true;
244
+ if (widget.text.len == 0) {
245
+ const placeholder = widgetPlaceholder(widget);
246
+ if (placeholder.len == 0) return false;
247
+ const viewport = widgetTextInputClipRect(widget, tokens, text_size, text_inset, options);
248
+ return measureTextWidthForFont(options.measure, tokens.typography.font_id, placeholder, text_size) > viewport.width;
249
+ }
250
+ return false;
251
+ }
252
+
150
253
  fn widgetTextInputMaxScrollOffset(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) f32 {
151
254
  const viewport = widgetTextInputClipRect(widget, tokens, text_size, text_inset, options);
152
255
  return @max(0, textInputContentExtentForWidgetWithOptions(widget, tokens.typography.font_id, text_size, options) - viewport.height);
@@ -1317,7 +1317,7 @@ test "every display-list command kind is representable on the packet path" {
1317
1317
  .{ .stroke_rect = .{ .id = 13, .rect = geometry.RectF.init(32, 32, 32, 32), .radius = canvas.Radius.all(4), .stroke = .{ .width = 2, .fill = .{ .color = canvas.Color.rgb8(0, 0, 255) } } } },
1318
1318
  .{ .draw_line = .{ .id = 14, .from = geometry.PointF.init(0, 70), .to = geometry.PointF.init(64, 70), .stroke = .{ .width = 1, .fill = .{ .color = canvas.Color.rgb8(128, 128, 128) } } } },
1319
1319
  .{ .fill_path = .{ .id = 15, .elements = &path_elements, .fill = .{ .color = canvas.Color.rgb8(200, 100, 50) } } },
1320
- .{ .stroke_path = .{ .id = 16, .elements = &path_elements, .stroke = .{ .width = 1.5, .fill = .{ .color = canvas.Color.rgb8(50, 100, 200) } } } },
1320
+ .{ .stroke_path = .{ .id = 16, .elements = &path_elements, .stroke = .{ .width = 1.5, .fill = .{ .color = canvas.Color.rgb8(50, 100, 200) } }, .cap = .round } },
1321
1321
  .{ .draw_image = .{ .id = 17, .image_id = 3, .dst = geometry.RectF.init(80, 80, 24, 24), .radius = canvas.Radius.all(12) } },
1322
1322
  .{ .draw_text = .{ .id = 18, .font_id = 1, .size = 13, .origin = geometry.PointF.init(8, 120), .color = canvas.Color.rgb8(30, 30, 30), .text = "audit", .text_layout = .{ .max_width = 200, .line_height = 16, .wrap = .word } } },
1323
1323
  .{ .shadow = .{ .id = 19, .rect = geometry.RectF.init(10, 140, 40, 20), .radius = canvas.Radius.all(4), .offset = .{ .dx = 0, .dy = 2 }, .blur = 6, .spread = 1, .color = canvas.Color.rgba8(0, 0, 0, 90) } },
@@ -1341,10 +1341,18 @@ test "every display-list command kind is representable on the packet path" {
1341
1341
  // Structural commands never reach the packet: 11 draw commands.
1342
1342
  try std.testing.expectEqual(@as(usize, 11), packet.commandCount());
1343
1343
 
1344
+ // The stroke path's cap channel survives into the packet command —
1345
+ // the field the host reads to shape open subpath ends.
1346
+ for (packet.commands) |packet_command| {
1347
+ const expected_cap: canvas.LineCap = if (packet_command.kind == .stroke_path) .round else .butt;
1348
+ try std.testing.expectEqual(expected_cap, packet_command.cap);
1349
+ }
1350
+
1344
1351
  // Both encodings accept the full kind coverage.
1345
1352
  var json_buffer: [64 * 1024]u8 = undefined;
1346
1353
  var json_writer = std.Io.Writer.fixed(&json_buffer);
1347
1354
  try packet.writeJson(&json_writer);
1355
+ try std.testing.expect(std.mem.indexOf(u8, json_writer.buffered(), "\"cap\":\"round\"") != null);
1348
1356
  var binary_buffer: [64 * 1024]u8 = undefined;
1349
1357
  var binary_writer = std.Io.Writer.fixed(&binary_buffer);
1350
1358
  try packet.writeBinary(&binary_writer);
@@ -180,7 +180,7 @@ const BinaryCursor = struct {
180
180
  const start = self.offset;
181
181
  _ = try self.readU8(); // kind
182
182
  const flags = try self.readU8();
183
- try self.skip(16 + 4 + 4); // bounds + opacity + stroke_width
183
+ try self.skip(16 + 4 + 4 + 1); // bounds + opacity + stroke_width + cap
184
184
  if (flags & 0x01 != 0) try self.skip(8); // id
185
185
  if (flags & 0x02 != 0) try self.skip(16); // clip
186
186
  if (flags & 0x04 != 0) try self.skip(24); // transform
@@ -929,6 +929,9 @@ test "command fingerprints cover every encoded field" {
929
929
  changed.stroke_width = 3;
930
930
  try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
931
931
  changed = base;
932
+ changed.cap = .round;
933
+ try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
934
+ changed = base;
932
935
  changed.id = 42;
933
936
  try std.testing.expect(canvas.canvasGpuCommandFingerprint(changed) != base_fingerprint);
934
937
  changed = base;
@@ -975,6 +978,41 @@ test "command fingerprints cover every encoded field" {
975
978
  try std.testing.expect(canvas.canvasGpuPacketCommandKey(moved, unkeyed_fingerprint) != synthetic);
976
979
  }
977
980
 
981
+ test "the stroke end-cap rides the binary command encoding" {
982
+ // The command model's cap channel must survive the wire: the host
983
+ // strokes the checkbox mark's open polyline with the caps the
984
+ // reference renderer rasterizes, so a dropped byte here is silent
985
+ // cross-engine divergence. The cap sits at a fixed offset — key u64,
986
+ // kind u8, flags u8, bounds f32[4], opacity f32, stroke_width f32 —
987
+ // pinned against the host decoder's layout.
988
+ const elements = [_]canvas.PathElement{
989
+ .{ .verb = .move_to, .points = .{ geometry.PointF.init(4, 8), geometry.PointF.zero(), geometry.PointF.zero() } },
990
+ .{ .verb = .line_to, .points = .{ geometry.PointF.init(7, 11), geometry.PointF.zero(), geometry.PointF.zero() } },
991
+ .{ .verb = .line_to, .points = .{ geometry.PointF.init(12, 5), geometry.PointF.zero(), geometry.PointF.zero() } },
992
+ };
993
+ var command = canvas.CanvasGpuCommand{
994
+ .command_index = 0,
995
+ .id = 7,
996
+ .kind = .stroke_path,
997
+ .bounds = geometry.RectF.init(4, 5, 8, 6),
998
+ .shape = .{ .path = &elements },
999
+ .paint = .{ .color = canvas.Color.rgb8(255, 255, 255) },
1000
+ .stroke_width = 2,
1001
+ .cap = .round,
1002
+ };
1003
+ const cap_offset = 8 + 1 + 1 + 16 + 4 + 4;
1004
+
1005
+ var buffer: [512]u8 = undefined;
1006
+ var writer = std.Io.Writer.fixed(&buffer);
1007
+ try canvas.writeCanvasGpuCommandBinaryKeyed(7, command, &writer);
1008
+ try std.testing.expectEqual(@as(u8, 1), writer.buffered()[cap_offset]);
1009
+
1010
+ command.cap = .butt;
1011
+ var butt_writer = std.Io.Writer.fixed(&buffer);
1012
+ try canvas.writeCanvasGpuCommandBinaryKeyed(7, command, &butt_writer);
1013
+ try std.testing.expectEqual(@as(u8, 0), butt_writer.buffered()[cap_offset]);
1014
+ }
1015
+
978
1016
  test "pixel presents adopt a dirty-refinement baseline only for opted-in hosts and never feed patches" {
979
1017
  // The mobile embed host presents through the CPU pixel path only and
980
1018
  // opts in (`Options.pixel_present_retained_baseline`): pixel presents
@@ -191,8 +191,8 @@ test "runtime applies pointer values to canvas controls" {
191
191
  var saw_empty_slider_active = false;
192
192
  for (display_list.commands) |command| {
193
193
  switch (command) {
194
- .draw_line => |line| {
195
- if (line.id == testCanvasWidgetPartId(2, 4)) saw_checkbox_check = true;
194
+ .stroke_path => |stroke| {
195
+ if (stroke.id == testCanvasWidgetPartId(2, 4)) saw_checkbox_check = true;
196
196
  },
197
197
  .fill_rounded_rect => |fill| {
198
198
  if (fill.id == testCanvasWidgetPartId(4, 2)) {
@@ -1328,8 +1328,8 @@ test "runtime refreshes widget owned display list from canvas input" {
1328
1328
  var saw_checkbox_check = false;
1329
1329
  for (display_list.commands) |command| {
1330
1330
  switch (command) {
1331
- .draw_line => |line| {
1332
- if (line.id == testCanvasWidgetPartId(2, 4)) saw_checkbox_check = true;
1331
+ .stroke_path => |stroke| {
1332
+ if (stroke.id == testCanvasWidgetPartId(2, 4)) saw_checkbox_check = true;
1333
1333
  },
1334
1334
  else => {},
1335
1335
  }
@@ -700,7 +700,13 @@ pub fn canvasWidgetLayoutNodeWithTextReconcileState(
700
700
  const source_matches_runtime_text = std.mem.eql(u8, entry.text, copy.widget.text);
701
701
  if (!source_unchanged and !source_matches_runtime_text) return copy;
702
702
  if (source_unchanged) copy.widget.text = entry.text;
703
- if (copy.widget.kind == .textarea) copy.widget.value = entry.value;
703
+ // The retained scroll offset rides `value` for every editable
704
+ // text kind: the textarea's vertical offset and the single-line
705
+ // field's horizontal one both survive rebuilds through this
706
+ // restore (and re-clamp in `clampCanvasWidgetLayoutTextOffsets`
707
+ // right after, so a resized or re-texted field never keeps a
708
+ // stale offset).
709
+ if (canvasWidgetEditableTextKind(copy.widget.kind)) copy.widget.value = entry.value;
704
710
  if (copy.widget.text_selection == null and copy.widget.text_composition == null) {
705
711
  copy.widget.text_selection = entry.text_selection;
706
712
  copy.widget.text_composition = entry.text_composition;
@@ -903,8 +909,22 @@ pub fn clampCanvasWidgetLayoutScrollOffsets(nodes: []canvas.WidgetLayoutNode, st
903
909
 
904
910
  pub fn clampCanvasWidgetLayoutTextOffsets(nodes: []canvas.WidgetLayoutNode, tokens: canvas.DesignTokens) void {
905
911
  for (nodes) |*node| {
906
- if (node.widget.kind != .textarea) continue;
907
- node.widget.value = canvas.clampedTextInputScrollOffsetForWidget(node.widget, tokens, node.widget.value);
912
+ if (node.widget.kind == .textarea) {
913
+ node.widget.value = canvas.clampedTextInputScrollOffsetForWidget(node.widget, tokens, node.widget.value);
914
+ continue;
915
+ }
916
+ if (!canvasWidgetSingleLineTextKind(node.widget.kind)) continue;
917
+ // Single-line fields clamp their horizontal offset against the
918
+ // just-reconciled text and frame, and a field holding a caret
919
+ // also keeps it inside the visible span — this is where a layout
920
+ // width change (a resized window narrowing the field) is first
921
+ // known, so the caret never strands outside the clip after a
922
+ // resize. Deterministic in retained state: the offset only moves
923
+ // when the clamp or the caret demands it.
924
+ node.widget.value = if (node.widget.text_selection != null)
925
+ canvas.textInputCaretVisibleScrollOffsetForWidget(node.widget, tokens, node.widget.value)
926
+ else
927
+ canvas.clampedTextInputHorizontalScrollOffsetForWidget(node.widget, tokens, node.widget.value);
908
928
  }
909
929
  }
910
930
 
@@ -2102,3 +2102,104 @@ test "word selection feeds the shared selection state: copy and shift-arrow just
2102
2102
  } });
2103
2103
  try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 6 }, try retainedTextSelection(harness, 1));
2104
2104
  }
2105
+
2106
+ test "runtime scrolls single-line fields horizontally to keep the caret visible" {
2107
+ const TestApp = struct {
2108
+ fn app(self: *@This()) App {
2109
+ return .{ .context = self, .name = "gpu-widget-field-scroll", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
2110
+ }
2111
+ };
2112
+
2113
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2114
+ defer harness.destroy(std.testing.allocator);
2115
+ harness.null_platform.gpu_surfaces = true;
2116
+ var app_state: TestApp = .{};
2117
+ const app = app_state.app();
2118
+ try harness.start(app);
2119
+
2120
+ _ = try harness.runtime.createView(.{
2121
+ .window_id = 1,
2122
+ .label = "canvas",
2123
+ .kind = .gpu_surface,
2124
+ .frame = geometry.RectF.init(0, 0, 260, 160),
2125
+ });
2126
+
2127
+ const field = canvas.Widget{
2128
+ .id = 2,
2129
+ .kind = .text_field,
2130
+ .frame = geometry.RectF.init(12, 16, 120, 32),
2131
+ .semantics = .{ .label = "Name" },
2132
+ };
2133
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
2134
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{field} }, geometry.RectF.init(0, 0, 260, 160), &nodes);
2135
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
2136
+
2137
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2138
+ .window_id = 1,
2139
+ .label = "canvas",
2140
+ .kind = .pointer_down,
2141
+ .x = 20,
2142
+ .y = 30,
2143
+ } });
2144
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
2145
+
2146
+ // A short value never scrolls: the offset channel stays at zero.
2147
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .insert_text = "short" });
2148
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2149
+ try std.testing.expectEqual(@as(f32, 0), retained.nodes[1].widget.value);
2150
+
2151
+ // Typing past the field's span scrolls forward so the caret stays
2152
+ // inside the visible span; the emitted stream clips at the border.
2153
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .insert_text = " value far too long for this narrow field to show" });
2154
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2155
+ try std.testing.expect(retained.nodes[1].widget.value > 0);
2156
+ try std.testing.expect(canvas.textInputMaxHorizontalScrollOffsetForWidget(retained.nodes[1].widget, .{}) > 0);
2157
+ const viewport = canvas.textInputViewportForWidget(retained.nodes[1].widget, .{}).?;
2158
+ const scrolled_geometry = try harness.runtime.canvasWidgetTextGeometry(1, "canvas", 2);
2159
+ const scrolled_caret = scrolled_geometry.caret_bounds.?;
2160
+ try std.testing.expect(scrolled_caret.x >= viewport.x - 0.001);
2161
+ try std.testing.expect(scrolled_caret.maxX() <= viewport.maxX() + 0.001);
2162
+
2163
+ _ = try harness.runtime.emitCanvasWidgetDisplayList(1, "canvas", .{});
2164
+ const display_list = try harness.runtime.canvasDisplayList(1, "canvas");
2165
+ var saw_field_clip = false;
2166
+ for (display_list.commands) |command| {
2167
+ switch (command) {
2168
+ .push_clip => |clip| {
2169
+ if (clip.id == testCanvasWidgetPartId(2, 16)) {
2170
+ try std.testing.expectEqualDeep(viewport, clip.rect);
2171
+ saw_field_clip = true;
2172
+ }
2173
+ },
2174
+ else => {},
2175
+ }
2176
+ }
2177
+ try std.testing.expect(saw_field_clip);
2178
+
2179
+ // The retained offset survives a source rebuild (the reconcile pass
2180
+ // restores it alongside the text), and the clamp pass re-checks the
2181
+ // caret against the new geometry.
2182
+ var rebuild_nodes: [2]canvas.WidgetLayoutNode = undefined;
2183
+ const rebuild = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{field} }, geometry.RectF.init(0, 0, 260, 160), &rebuild_nodes);
2184
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", rebuild);
2185
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2186
+ try std.testing.expect(retained.nodes[1].widget.value > 0);
2187
+
2188
+ // Home: the caret returns to byte zero and the field scrolls all the
2189
+ // way back.
2190
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .move_caret = .{ .direction = .start } });
2191
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2192
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(0), retained.nodes[1].widget.text_selection.?);
2193
+ try std.testing.expectEqual(@as(f32, 0), retained.nodes[1].widget.value);
2194
+
2195
+ // Deleting the overflow clamps the offset back to zero — the field
2196
+ // never shows trailing emptiness while text could fill it.
2197
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .move_caret = .{ .direction = .end } });
2198
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2199
+ try std.testing.expect(retained.nodes[1].widget.value > 0);
2200
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .set_selection = .{ .anchor = 5, .focus = 55 } });
2201
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .delete_backward);
2202
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2203
+ try std.testing.expectEqualStrings("short", retained.nodes[1].widget.text);
2204
+ try std.testing.expectEqual(@as(f32, 0), retained.nodes[1].widget.value);
2205
+ }