@native-sdk/cli 0.4.2 → 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.
@@ -416,9 +416,10 @@ test "icon widgets render built-in vector icons as tinted path commands" {
416
416
  if (pixels[index] < 250) ink += 1;
417
417
  }
418
418
  try std.testing.expect(ink > 20);
419
- // Regenerated for the house default palette: the tint (the text
420
- // token) moved from #09090b to #0a0a0a; same check-mark coverage.
421
- try std.testing.expectEqual(@as(u64, 1722938743772709742), support.referenceSurfaceSignature(&pixels));
419
+ // Regenerated when geometry edge coverage moved to linear-light
420
+ // blending (the icon is a vector path): same coverage, smoother
421
+ // fringe bytes.
422
+ try std.testing.expectEqual(@as(u64, 5692773564953859754), support.referenceSurfaceSignature(&pixels));
422
423
 
423
424
  // A non-registry text keeps the historical glyph rendering.
424
425
  const glyph = Widget{
@@ -490,7 +491,7 @@ test "checkbox check mark strokes one anti-aliased vector path" {
490
491
  }
491
492
  }
492
493
  try std.testing.expect(partial >= 4);
493
- try std.testing.expectEqual(@as(u64, 12485073109273199295), support.referenceSurfaceSignature(&pixels));
494
+ try std.testing.expectEqual(@as(u64, 10271374105851145327), support.referenceSurfaceSignature(&pixels));
494
495
  }
495
496
 
496
497
  test "a builder accumulating two widget trees keeps each checkbox mark's own geometry" {
@@ -3349,3 +3350,247 @@ test "hairline borders snap to whole device columns with smooth arcs" {
3349
3350
  const corner = surface.pixelRgba8(5, 4)[3];
3350
3351
  try std.testing.expect(corner > 0 and corner < 255);
3351
3352
  }
3353
+
3354
+ test "single-line fields clip and horizontally scroll an overflowing value" {
3355
+ const long_text = "a value far too long for a narrow single-line field to show at once";
3356
+ const field = Widget{
3357
+ .id = 7,
3358
+ .kind = .text_field,
3359
+ .frame = geometry.RectF.init(10, 12, 120, 32),
3360
+ .text = long_text,
3361
+ .text_selection = TextSelection.collapsed(long_text.len),
3362
+ .state = .{ .focused = true },
3363
+ .semantics = .{ .label = "Name" },
3364
+ // The retained offset channel: an oversized write clamps to the
3365
+ // farthest the value can scroll, exactly like the textarea's
3366
+ // vertical offset does.
3367
+ .value = 100000,
3368
+ };
3369
+ const max_offset = support.textInputMaxHorizontalScrollOffsetForWidget(field, .{});
3370
+ try std.testing.expect(max_offset > 0);
3371
+ try std.testing.expectEqual(max_offset, support.clampedTextInputHorizontalScrollOffsetForWidget(field, .{}, 100000));
3372
+
3373
+ var zero_offset_field = field;
3374
+ zero_offset_field.value = 0;
3375
+
3376
+ var commands: [8]CanvasCommand = undefined;
3377
+ var builder = Builder.init(&commands);
3378
+ try emitWidgetTree(&builder, field, .{});
3379
+ const display_list = builder.displayList();
3380
+ var zero_commands: [8]CanvasCommand = undefined;
3381
+ var zero_builder = Builder.init(&zero_commands);
3382
+ try emitWidgetTree(&zero_builder, zero_offset_field, .{});
3383
+ const zero_display_list = zero_builder.displayList();
3384
+
3385
+ // Fill, border, offset focus ring, clip, text, caret, pop.
3386
+ try std.testing.expectEqual(@as(usize, 7), display_list.commandCount());
3387
+ const viewport = textInputViewportForWidget(field, .{}).?;
3388
+ switch (display_list.commands[3]) {
3389
+ .push_clip => |clip| {
3390
+ try std.testing.expectEqual(widgetPartId(7, 16), clip.id);
3391
+ try expectRectApprox(viewport, clip.rect);
3392
+ },
3393
+ else => return error.TestUnexpectedResult,
3394
+ }
3395
+ // The draw-text origin shifts left by exactly the clamped offset.
3396
+ const scrolled_origin = switch (display_list.commands[4]) {
3397
+ .draw_text => |text| text.origin,
3398
+ else => return error.TestUnexpectedResult,
3399
+ };
3400
+ const resting_origin = switch (zero_display_list.commands[4]) {
3401
+ .draw_text => |text| text.origin,
3402
+ else => return error.TestUnexpectedResult,
3403
+ };
3404
+ try std.testing.expectApproxEqAbs(resting_origin.x - max_offset, scrolled_origin.x, 0.001);
3405
+ try std.testing.expectEqual(resting_origin.y, scrolled_origin.y);
3406
+ // The end-of-value caret rides the same origin, landing inside the
3407
+ // clip instead of past the field's border.
3408
+ switch (display_list.commands[5]) {
3409
+ .fill_rect => |caret| {
3410
+ try std.testing.expect(caret.rect.x >= viewport.x - 0.001);
3411
+ try std.testing.expect(caret.rect.maxX() <= viewport.maxX() + 0.001);
3412
+ },
3413
+ else => return error.TestUnexpectedResult,
3414
+ }
3415
+ try std.testing.expectEqual(CanvasCommand.pop_clip, display_list.commands[6]);
3416
+ }
3417
+
3418
+ test "the caret keep-visible offset scrolls to the caret and returns home" {
3419
+ const long_text = "a value far too long for a narrow single-line field to show at once";
3420
+ var field = Widget{
3421
+ .id = 7,
3422
+ .kind = .text_field,
3423
+ .frame = geometry.RectF.init(10, 12, 120, 32),
3424
+ .text = long_text,
3425
+ .text_selection = TextSelection.collapsed(long_text.len),
3426
+ .state = .{ .focused = true },
3427
+ .semantics = .{ .label = "Name" },
3428
+ };
3429
+ const max_offset = support.textInputMaxHorizontalScrollOffsetForWidget(field, .{});
3430
+ try std.testing.expect(max_offset > 0);
3431
+
3432
+ // Caret at the end, unscrolled: the recompute scrolls forward far
3433
+ // enough that the caret sits inside the visible span.
3434
+ const end_offset = support.textInputCaretVisibleScrollOffsetForWidget(field, .{}, 0);
3435
+ try std.testing.expect(end_offset > 0);
3436
+ try std.testing.expect(end_offset <= max_offset + 0.001);
3437
+ field.value = end_offset;
3438
+ const viewport = textInputViewportForWidget(field, .{}).?;
3439
+ const end_geometry = textGeometryForWidget(field, .{});
3440
+ const end_caret = end_geometry.caret_bounds.?;
3441
+ try std.testing.expect(end_caret.x >= viewport.x - 0.001);
3442
+ try std.testing.expect(end_caret.maxX() <= viewport.maxX() + 0.001);
3443
+
3444
+ // Home: caret back at byte zero scrolls all the way back — the field
3445
+ // never shows trailing emptiness while text could fill it.
3446
+ field.text_selection = TextSelection.collapsed(0);
3447
+ try std.testing.expectEqual(@as(f32, 0), support.textInputCaretVisibleScrollOffsetForWidget(field, .{}, end_offset));
3448
+
3449
+ // A value that fits never scrolls and never adjusts.
3450
+ var short = field;
3451
+ short.text = "short";
3452
+ short.text_selection = TextSelection.collapsed(5);
3453
+ try std.testing.expectEqual(@as(f32, 0), support.textInputMaxHorizontalScrollOffsetForWidget(short, .{}));
3454
+ try std.testing.expectEqual(@as(f32, 0), support.textInputCaretVisibleScrollOffsetForWidget(short, .{}, 25));
3455
+ }
3456
+
3457
+ test "scrolled single-line selection rects shift with the text origin" {
3458
+ const long_text = "a value far too long for a narrow single-line field to show at once";
3459
+ const scrolled = Widget{
3460
+ .id = 7,
3461
+ .kind = .text_field,
3462
+ .frame = geometry.RectF.init(10, 12, 120, 32),
3463
+ .text = long_text,
3464
+ .text_selection = .{ .anchor = 2, .focus = 9 },
3465
+ .state = .{ .focused = true },
3466
+ .semantics = .{ .label = "Name" },
3467
+ .value = 20,
3468
+ };
3469
+ var resting = scrolled;
3470
+ resting.value = 0;
3471
+
3472
+ var commands: [10]CanvasCommand = undefined;
3473
+ var builder = Builder.init(&commands);
3474
+ try emitWidgetTree(&builder, scrolled, .{});
3475
+ const display_list = builder.displayList();
3476
+ var resting_commands: [10]CanvasCommand = undefined;
3477
+ var resting_builder = Builder.init(&resting_commands);
3478
+ try emitWidgetTree(&resting_builder, resting, .{});
3479
+ const resting_display_list = resting_builder.displayList();
3480
+
3481
+ // Fill, border, focus ring, clip, selection rect, text, selected
3482
+ // glyphs, pop — the same shape at both offsets.
3483
+ try std.testing.expectEqual(display_list.commandCount(), resting_display_list.commandCount());
3484
+ const scrolled_selection = switch (display_list.commands[4]) {
3485
+ .fill_rect => |rect| rect.rect,
3486
+ else => return error.TestUnexpectedResult,
3487
+ };
3488
+ const resting_selection = switch (resting_display_list.commands[4]) {
3489
+ .fill_rect => |rect| rect.rect,
3490
+ else => return error.TestUnexpectedResult,
3491
+ };
3492
+ const scrolled_text = switch (display_list.commands[5]) {
3493
+ .draw_text => |text| text.origin,
3494
+ else => return error.TestUnexpectedResult,
3495
+ };
3496
+ const resting_text = switch (resting_display_list.commands[5]) {
3497
+ .draw_text => |text| text.origin,
3498
+ else => return error.TestUnexpectedResult,
3499
+ };
3500
+ // Selection geometry and the draw-text origin move together, by
3501
+ // exactly the scroll offset.
3502
+ try std.testing.expectApproxEqAbs(@as(f32, 20), resting_selection.x - scrolled_selection.x, 0.001);
3503
+ try std.testing.expectApproxEqAbs(@as(f32, 20), resting_text.x - scrolled_text.x, 0.001);
3504
+ try std.testing.expectApproxEqAbs(resting_selection.width, scrolled_selection.width, 0.001);
3505
+ }
3506
+
3507
+ test "short single-line values emit no clip and an unshifted origin" {
3508
+ const field = Widget{
3509
+ .id = 7,
3510
+ .kind = .text_field,
3511
+ .frame = geometry.RectF.init(10, 12, 120, 32),
3512
+ .text = "short",
3513
+ .text_selection = TextSelection.collapsed(5),
3514
+ .state = .{ .focused = true },
3515
+ .semantics = .{ .label = "Name" },
3516
+ // A stale offset on a value that fits clamps to zero: fitting
3517
+ // fields render exactly as they did before fields scrolled.
3518
+ .value = 40,
3519
+ };
3520
+ var commands: [8]CanvasCommand = undefined;
3521
+ var builder = Builder.init(&commands);
3522
+ try emitWidgetTree(&builder, field, .{});
3523
+ const display_list = builder.displayList();
3524
+ // Fill, border, offset focus ring, text, caret — no clip pair.
3525
+ try std.testing.expectEqual(@as(usize, 5), display_list.commandCount());
3526
+ for (display_list.commands) |command| {
3527
+ try std.testing.expect(command != .push_clip and command != .pop_clip);
3528
+ }
3529
+ var zero = field;
3530
+ zero.value = 0;
3531
+ var zero_commands: [8]CanvasCommand = undefined;
3532
+ var zero_builder = Builder.init(&zero_commands);
3533
+ try emitWidgetTree(&zero_builder, zero, .{});
3534
+ const zero_display_list = zero_builder.displayList();
3535
+ switch (display_list.commands[3]) {
3536
+ .draw_text => |text| {
3537
+ try std.testing.expectEqual(switch (zero_display_list.commands[3]) {
3538
+ .draw_text => |zero_text| zero_text.origin.x,
3539
+ else => return error.TestUnexpectedResult,
3540
+ }, text.origin.x);
3541
+ },
3542
+ else => return error.TestUnexpectedResult,
3543
+ }
3544
+ }
3545
+
3546
+ test "search fields clip an overflowing value and keep chrome outside the clip" {
3547
+ const long_text = "an overflowing search query that runs past the narrow field";
3548
+ const field = Widget{
3549
+ .id = 9,
3550
+ .kind = .search_field,
3551
+ .frame = geometry.RectF.init(10, 12, 140, 32),
3552
+ .text = long_text,
3553
+ .text_selection = TextSelection.collapsed(long_text.len),
3554
+ .state = .{ .focused = true },
3555
+ .semantics = .{ .label = "Search" },
3556
+ .value = 100000,
3557
+ };
3558
+ try std.testing.expect(support.textInputMaxHorizontalScrollOffsetForWidget(field, .{}) > 0);
3559
+
3560
+ var commands: [24]CanvasCommand = undefined;
3561
+ var builder = Builder.init(&commands);
3562
+ try emitWidgetTree(&builder, field, .{});
3563
+ const display_list = builder.displayList();
3564
+
3565
+ var clip_index: ?usize = null;
3566
+ var pop_index: ?usize = null;
3567
+ var text_index: ?usize = null;
3568
+ var caret_index: ?usize = null;
3569
+ var clear_transform_index: ?usize = null;
3570
+ for (display_list.commands, 0..) |command, index| {
3571
+ switch (command) {
3572
+ .push_clip => |clip| {
3573
+ try std.testing.expectEqual(widgetPartId(9, 7), clip.id);
3574
+ try expectRectApprox(textInputViewportForWidget(field, .{}).?, clip.rect);
3575
+ clip_index = index;
3576
+ },
3577
+ .pop_clip => pop_index = index,
3578
+ .draw_text => |text| {
3579
+ if (text.id == widgetPartId(9, 9)) text_index = index;
3580
+ },
3581
+ .fill_rect => |caret| {
3582
+ if (caret.id == widgetPartId(9, 11)) caret_index = index;
3583
+ },
3584
+ .transform => {
3585
+ if (clear_transform_index == null and pop_index != null) clear_transform_index = index;
3586
+ },
3587
+ else => {},
3588
+ }
3589
+ }
3590
+ // Text and caret sit inside the clip pair; the trailing clear
3591
+ // affordance draws after the pop, outside it.
3592
+ try std.testing.expect(clip_index.? < text_index.?);
3593
+ try std.testing.expect(text_index.? < caret_index.?);
3594
+ try std.testing.expect(caret_index.? < pop_index.?);
3595
+ try std.testing.expect(pop_index.? < clear_transform_index.?);
3596
+ }
@@ -41,6 +41,7 @@ const widgetTextInputOrigin = widget_text_input.widgetTextInputOrigin;
41
41
  const widgetTextInputClipRect = widget_text_input.widgetTextInputClipRect;
42
42
  const widgetTextInputDrawText = widget_text_input.widgetTextInputDrawText;
43
43
  const widgetTextInputInset = widget_text_input.widgetTextInputInset;
44
+ const widgetTextInputClipsText = widget_text_input.widgetTextInputClipsText;
44
45
  const textInputClearButtonRect = widget_text_input.textInputClearButtonRect;
45
46
  const widgetButtonTextSize = widget_metrics.widgetButtonTextSize;
46
47
  const widgetBodyTextSize = widget_metrics.widgetBodyTextSize;
@@ -466,7 +467,12 @@ pub fn emitTextFieldWidget(builder: *Builder, widget: Widget, tokens: DesignToke
466
467
  const selection_range = widgetTextSelectionRange(widget);
467
468
  const composition_range = widgetTextCompositionRange(widget);
468
469
  const has_text_affordances = selection_range != null or composition_range != null;
469
- const clips_text = widget.kind == .textarea;
470
+ // Textareas always clip (their overflow scrolls vertically); a
471
+ // single-line field clips once its value overflows the content rect,
472
+ // so the horizontally scrolled text, selection rects, composition
473
+ // underline, and caret all cut at the field's border instead of
474
+ // painting past it. Short values emit no clip — unchanged emission.
475
+ const clips_text = widgetTextInputClipsText(widget, tokens, text_size, text_inset, layout_options);
470
476
 
471
477
  try builder.fillRoundedRect(.{
472
478
  .id = widgetPartId(widget.id, 1),
@@ -555,11 +561,20 @@ pub fn emitSearchFieldWidget(builder: *Builder, widget: Widget, tokens: DesignTo
555
561
  const icon_size = @max(8, text_size - 2);
556
562
  const text_inset = widgetTextInputInset(widget, tokens);
557
563
  const layout_options = widgetTextInputLayoutOptions(widget, tokens, text_size, text_inset);
564
+ const clip_rect = widgetTextInputClipRect(widget, tokens, text_size, text_inset, layout_options);
558
565
  const origin = widgetTextInputOrigin(widget, tokens, text_size, text_inset, layout_options);
559
566
  const selection_range = widgetTextSelectionRange(widget);
560
567
  const composition_range = widgetTextCompositionRange(widget);
561
568
  const text_color = widgetForegroundColor(widget, tokens, visual.foreground orelse tokens.colors.text);
562
569
  const draw_text = widgetTextInputDrawText(widget, tokens, text_size, origin, text_color, layout_options);
570
+ // Same overflow contract as the text-field emitter: clip only once
571
+ // the value (or placeholder) overflows the content rect, so the
572
+ // horizontally scrolled text and its affordances cut at the border.
573
+ // The leading glass, trailing chevron, and clear affordance draw
574
+ // outside the clip — they are chrome, not scrolling content. Slot 7
575
+ // is clear of the field chrome (1..6, 8..13), the focus ring (14),
576
+ // and the clear affordance's shape range (15..).
577
+ const clips_text = widgetTextInputClipsText(widget, tokens, text_size, text_inset, layout_options);
563
578
 
564
579
  try builder.fillRoundedRect(.{
565
580
  .id = widgetPartId(widget.id, 1),
@@ -578,6 +593,7 @@ pub fn emitSearchFieldWidget(builder: *Builder, widget: Widget, tokens: DesignTo
578
593
  }));
579
594
  if (widget.state.focused) try emitWidgetFocusRingForRect(builder, widget, tokens, 14, widget.frame, radius);
580
595
  try emitSearchFieldIcon(builder, widget, tokens, icon_size);
596
+ if (clips_text) try builder.pushClip(.{ .id = widgetPartId(widget.id, 7), .rect = clip_rect, .radius = radius });
581
597
  if (selection_range) |range| {
582
598
  if (!range.isCollapsed(widget.text.len)) {
583
599
  try emitWidgetTextSelectionRects(builder, widget, draw_text, layout_options, range, 8, 0, 1, tokens);
@@ -609,6 +625,7 @@ pub fn emitSearchFieldWidget(builder: *Builder, widget: Widget, tokens: DesignTo
609
625
  }
610
626
  }
611
627
  }
628
+ if (clips_text) try builder.popClip();
612
629
  if (widget.kind == .combobox) {
613
630
  try emitComboboxChevron(builder, widget, tokens, visual);
614
631
  }
@@ -50,6 +50,9 @@ pub const textInputClearButtonHitRect = widget_text_input.textInputClearButtonHi
50
50
  pub const textInputContentExtentForWidget = widget_text_input.textInputContentExtentForWidget;
51
51
  pub const textInputMaxScrollOffsetForWidget = widget_text_input.textInputMaxScrollOffsetForWidget;
52
52
  pub const clampedTextInputScrollOffsetForWidget = widget_text_input.clampedTextInputScrollOffsetForWidget;
53
+ pub const textInputMaxHorizontalScrollOffsetForWidget = widget_text_input.textInputMaxHorizontalScrollOffsetForWidget;
54
+ pub const clampedTextInputHorizontalScrollOffsetForWidget = widget_text_input.clampedTextInputHorizontalScrollOffsetForWidget;
55
+ pub const textInputCaretVisibleScrollOffsetForWidget = widget_text_input.textInputCaretVisibleScrollOffsetForWidget;
53
56
  pub const textGeometryForWidget = widget_text_input.textGeometryForWidget;
54
57
 
55
58
  pub const WidgetLayoutTree = struct {
@@ -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);
@@ -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
+ }
@@ -516,7 +516,7 @@ pub const RuntimeView = struct {
516
516
  pub const stepCanvasWidgetKineticScroll = CanvasWidgetScrollMethods.stepCanvasWidgetKineticScroll;
517
517
  pub const canvasWidgetScrollContentExtent = CanvasWidgetScrollMethods.canvasWidgetScrollContentExtent;
518
518
  pub const translateCanvasWidgetScrollDescendants = CanvasWidgetScrollMethods.translateCanvasWidgetScrollDescendants;
519
- pub const scrollCanvasTextareaCaretIntoView = CanvasWidgetScrollMethods.scrollCanvasTextareaCaretIntoView;
519
+ pub const scrollCanvasTextInputCaretIntoView = CanvasWidgetScrollMethods.scrollCanvasTextInputCaretIntoView;
520
520
 
521
521
  const CanvasWidgetControlMethods = view_widget_control.RuntimeViewCanvasWidgetControl(RuntimeView);
522
522
  pub const canvasWidgetToggleAnimation = CanvasWidgetControlMethods.canvasWidgetToggleAnimation;
@@ -7,6 +7,7 @@ const canvas_widget_runtime = @import("canvas_widget_runtime.zig");
7
7
  const unionRects = canvas_frame_helpers.unionRects;
8
8
  const CanvasWidgetScrollKeyboardTarget = canvas_widget_runtime.CanvasWidgetScrollKeyboardTarget;
9
9
  const canvasWidgetScrollableKind = canvas_widget_runtime.canvasWidgetScrollableKind;
10
+ const canvasWidgetSingleLineTextKind = canvas_widget_runtime.canvasWidgetSingleLineTextKind;
10
11
 
11
12
  pub const CanvasWidgetScrollSource = enum {
12
13
  discrete,
@@ -335,9 +336,19 @@ pub fn RuntimeViewCanvasWidgetScroll(comptime RuntimeView: type) type {
335
336
  }
336
337
  }
337
338
 
338
- pub fn scrollCanvasTextareaCaretIntoView(self: *RuntimeView, index: usize) void {
339
+ /// Keep the editable field's caret inside its visible span after
340
+ /// a text or caret change: textareas scroll vertically, single-
341
+ /// line fields horizontally — both through the widget's retained
342
+ /// `value` offset channel.
343
+ pub fn scrollCanvasTextInputCaretIntoView(self: *RuntimeView, index: usize) void {
339
344
  if (index >= self.widget_layout_node_count) return;
340
345
  var widget = self.widget_layout_nodes[index].widget;
346
+ if (canvasWidgetSingleLineTextKind(widget.kind)) {
347
+ const next_offset = canvas.textInputCaretVisibleScrollOffsetForWidget(widget, self.widget_tokens, widget.value);
348
+ if (next_offset == widget.value) return;
349
+ self.widget_layout_nodes[index].widget.value = next_offset;
350
+ return;
351
+ }
341
352
  if (widget.kind != .textarea) return;
342
353
 
343
354
  const viewport = canvas.textInputViewportForWidget(widget, self.widget_tokens) orelse return;