@native-sdk/cli 0.4.2 → 0.4.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 (46) hide show
  1. package/build/app.zig +121 -66
  2. package/build.zig +123 -6
  3. package/package.json +9 -9
  4. package/skill-data/core/SKILL.md +3 -0
  5. package/skill-data/native-ui/SKILL.md +16 -0
  6. package/skill-data/zig/SKILL.md +198 -0
  7. package/skills/native-sdk/SKILL.md +1 -1
  8. package/src/app_runner/root.zig +51 -9
  9. package/src/platform/linux/gtk_host.c +226 -21
  10. package/src/platform/windows/webview2_host.cpp +16 -10
  11. package/src/primitives/app_manifest/root.zig +8 -0
  12. package/src/primitives/app_manifest/types.zig +14 -0
  13. package/src/primitives/app_manifest/validation.zig +18 -0
  14. package/src/primitives/app_manifest/web_layer.zig +366 -0
  15. package/src/primitives/canvas/chart_tests.zig +2 -2
  16. package/src/primitives/canvas/markdown_tests.zig +1 -1
  17. package/src/primitives/canvas/reference.zig +135 -6
  18. package/src/primitives/canvas/reference_tests.zig +171 -6
  19. package/src/primitives/canvas/root.zig +3 -0
  20. package/src/primitives/canvas/test_support.zig +3 -0
  21. package/src/primitives/canvas/widget_builtin_tests.zig +249 -4
  22. package/src/primitives/canvas/widget_render_controls.zig +18 -1
  23. package/src/primitives/canvas/widget_runtime.zig +3 -0
  24. package/src/primitives/canvas/widget_text_input.zig +104 -1
  25. package/src/runtime/api.zig +7 -0
  26. package/src/runtime/bridge_responses.zig +2 -0
  27. package/src/runtime/builtin_bridge.zig +6 -0
  28. package/src/runtime/canvas_widget_runtime.zig +23 -3
  29. package/src/runtime/canvas_widget_text_tests.zig +101 -0
  30. package/src/runtime/flow.zig +8 -0
  31. package/src/runtime/platform_bridge_tests.zig +1 -0
  32. package/src/runtime/session_tests.zig +35 -8
  33. package/src/runtime/view.zig +1 -1
  34. package/src/runtime/view_widget_scroll.zig +12 -1
  35. package/src/runtime/view_widget_text.zig +6 -2
  36. package/src/runtime/web_layer_gate_tests.zig +242 -0
  37. package/src/runtime/window_storage.zig +5 -0
  38. package/src/runtime/window_view_runtime.zig +1 -0
  39. package/src/tooling/buildgraph.zig +119 -3
  40. package/src/tooling/junction.zig +247 -0
  41. package/src/tooling/manifest.zig +267 -1
  42. package/src/tooling/package.zig +598 -6
  43. package/src/tooling/raw_manifest.zig +1 -0
  44. package/src/tooling/root.zig +1 -0
  45. package/src/tooling/templates.zig +288 -61
  46. package/src/tooling/verbs.zig +5 -0
@@ -788,6 +788,168 @@ test "rounded-rect coverage matches supersampled ground truth with no silhouette
788
788
  }
789
789
  }
790
790
 
791
+ // ---------------------------------------------------------------------------
792
+ // Coverage blend-space split.
793
+ //
794
+ // Geometry (paths, rounded rects) blends its anti-aliased edge coverage
795
+ // in linear light; glyph coverage blends in sRGB to preserve text
796
+ // weight. These tests pin the split from both sides with independently
797
+ // computed ground truth, so a refactor can never silently swap the two
798
+ // (a swap fails BOTH assertions, loudly).
799
+
800
+ /// The exact sRGB decode the renderer's 256-entry byte table holds.
801
+ fn blendSplitSrgbToLinear(value: f32) f32 {
802
+ const channel = std.math.clamp(value, 0, 1);
803
+ if (channel <= 0.04045) return channel / 12.92;
804
+ return std.math.pow(f32, (channel + 0.055) / 1.055, 2.4);
805
+ }
806
+
807
+ fn blendSplitLinearToSrgb(value: f32) f32 {
808
+ const channel = std.math.clamp(value, 0, 1);
809
+ if (channel <= 0.0031308) return channel * 12.92;
810
+ return 1.055 * std.math.pow(f32, channel, 1.0 / 2.4) - 0.055;
811
+ }
812
+
813
+ /// The renderer's encode-table lookup, replicated: nearest of 4096
814
+ /// evenly spaced entries, then the final byte rounding.
815
+ fn blendSplitEncodeByte(value: f32) u8 {
816
+ const index = @round(std.math.clamp(value, 0, 1) * 4095.0);
817
+ return @intFromFloat(@round(blendSplitLinearToSrgb(index / 4095.0) * 255.0));
818
+ }
819
+
820
+ /// Linear-light coverage blend of an opaque source byte over an opaque
821
+ /// destination byte, from the same LUT math the renderer tabulates.
822
+ fn blendSplitLinearByte(src: u8, dst: u8, coverage: f32) u8 {
823
+ const src_linear = blendSplitSrgbToLinear(@as(f32, @floatFromInt(src)) / 255.0);
824
+ const dst_linear = blendSplitSrgbToLinear(@as(f32, @floatFromInt(dst)) / 255.0);
825
+ return blendSplitEncodeByte(src_linear * coverage + dst_linear * (1 - coverage));
826
+ }
827
+
828
+ /// sRGB-space coverage blend of the same pixel (the historical fold).
829
+ fn blendSplitSrgbByte(src: u8, dst: u8, coverage: f32) u8 {
830
+ const src_f = @as(f32, @floatFromInt(src)) / 255.0;
831
+ const dst_f = @as(f32, @floatFromInt(dst)) / 255.0;
832
+ return @intFromFloat(@round((src_f * coverage + dst_f * (1 - coverage)) * 255.0));
833
+ }
834
+
835
+ fn blendSplitRenderPass(surface: ReferenceRenderSurface, command: RenderCommand, clear: Color, width: usize, height: usize) !void {
836
+ const pass = CanvasRenderPass{
837
+ .surface_size = geometry.SizeF.init(@floatFromInt(width), @floatFromInt(height)),
838
+ .scale = 1,
839
+ .full_repaint = true,
840
+ .commands = &.{command},
841
+ };
842
+ try surface.renderPass(pass, clear);
843
+ }
844
+
845
+ test "geometry edge coverage blends in linear light, computed from the LUT math" {
846
+ // A black path whose right edge splits pixel column 12 exactly in
847
+ // half: the vector core reports coverage 0.5 there, interiors 1.
848
+ const split_width: usize = 24;
849
+ const split_height: usize = 16;
850
+ const white = Color{ .r = 1, .g = 1, .b = 1, .a = 1 };
851
+ const black = Color{ .r = 0, .g = 0, .b = 0, .a = 1 };
852
+ const bounds = geometry.RectF.init(0, 0, split_width, split_height);
853
+ const elements = [_]PathElement{
854
+ .{ .verb = .move_to, .points = .{ geometry.PointF.init(4, 4), geometry.PointF.zero(), geometry.PointF.zero() } },
855
+ .{ .verb = .line_to, .points = .{ geometry.PointF.init(12.5, 4), geometry.PointF.zero(), geometry.PointF.zero() } },
856
+ .{ .verb = .line_to, .points = .{ geometry.PointF.init(12.5, 12), geometry.PointF.zero(), geometry.PointF.zero() } },
857
+ .{ .verb = .line_to, .points = .{ geometry.PointF.init(4, 12), geometry.PointF.zero(), geometry.PointF.zero() } },
858
+ .{ .verb = .close, .points = .{ geometry.PointF.zero(), geometry.PointF.zero(), geometry.PointF.zero() } },
859
+ };
860
+
861
+ const linear_edge = blendSplitLinearByte(0, 255, 0.5);
862
+ const srgb_edge = blendSplitSrgbByte(0, 255, 0.5);
863
+ // The split is only observable if the two spaces disagree here.
864
+ try std.testing.expect(linear_edge != srgb_edge);
865
+
866
+ // fill_path: the vector-core geometry route.
867
+ {
868
+ var pixels: [split_width * split_height * 4]u8 = undefined;
869
+ const surface = try ReferenceRenderSurface.init(split_width, split_height, &pixels);
870
+ try blendSplitRenderPass(surface, .{
871
+ .command = .{ .fill_path = .{ .elements = &elements, .fill = .{ .color = black } } },
872
+ .local_bounds = bounds,
873
+ .bounds = bounds,
874
+ }, white, split_width, split_height);
875
+ // Fully covered interior pixels stay bit-identical to the plain
876
+ // sRGB blend: an opaque source at coverage 1 is a copy in either
877
+ // space.
878
+ try expectPixelRgba8(.{ 0, 0, 0, 255 }, surface, 8, 8);
879
+ // The half-covered edge pixel holds the linear-light value.
880
+ try expectPixelRgba8(.{ linear_edge, linear_edge, linear_edge, 255 }, surface, 12, 8);
881
+ }
882
+
883
+ // fill_rounded_rect: the signed-distance geometry route. Its right
884
+ // edge is a straight segment at the same half-pixel boundary (the
885
+ // radius-2 corners are far from row 8), so coverage is 0.5 again.
886
+ {
887
+ var pixels: [split_width * split_height * 4]u8 = undefined;
888
+ const surface = try ReferenceRenderSurface.init(split_width, split_height, &pixels);
889
+ try blendSplitRenderPass(surface, .{
890
+ .command = .{ .fill_rounded_rect = .{ .rect = geometry.RectF.init(4, 4, 8.5, 8), .radius = Radius.all(2), .fill = .{ .color = black } } },
891
+ .local_bounds = bounds,
892
+ .bounds = bounds,
893
+ }, white, split_width, split_height);
894
+ try expectPixelRgba8(.{ 0, 0, 0, 255 }, surface, 8, 8);
895
+ try expectPixelRgba8(.{ linear_edge, linear_edge, linear_edge, 255 }, surface, 12, 8);
896
+ }
897
+ }
898
+
899
+ test "glyph edge coverage blends in sRGB, not linear light" {
900
+ // The same discrimination from the text side: render one glyph twice
901
+ // — once over transparent (whose alpha channel IS the coverage, in
902
+ // any blend space) and once over white — then check every fringe
903
+ // pixel of the white render against both models. Text must track the
904
+ // sRGB fold and stay far from the linear-light value at mid
905
+ // coverage, so inverting the sink's blend space can never pass.
906
+ const glyph_width: usize = 32;
907
+ const glyph_height: usize = 48;
908
+ const white = Color{ .r = 1, .g = 1, .b = 1, .a = 1 };
909
+ const black = Color{ .r = 0, .g = 0, .b = 0, .a = 1 };
910
+ const clear = Color{ .r = 0, .g = 0, .b = 0, .a = 0 };
911
+ const bounds = geometry.RectF.init(0, 0, glyph_width, glyph_height);
912
+ const command = RenderCommand{
913
+ .command = .{ .draw_text = .{ .size = 32, .origin = geometry.PointF.init(4, 40), .color = black, .text = "o" } },
914
+ .local_bounds = bounds,
915
+ .bounds = bounds,
916
+ };
917
+
918
+ var coverage_pixels: [glyph_width * glyph_height * 4]u8 = undefined;
919
+ const coverage_surface = try ReferenceRenderSurface.init(glyph_width, glyph_height, &coverage_pixels);
920
+ try blendSplitRenderPass(coverage_surface, command, clear, glyph_width, glyph_height);
921
+
922
+ var blended_pixels: [glyph_width * glyph_height * 4]u8 = undefined;
923
+ const blended_surface = try ReferenceRenderSurface.init(glyph_width, glyph_height, &blended_pixels);
924
+ try blendSplitRenderPass(blended_surface, command, white, glyph_width, glyph_height);
925
+
926
+ var mid_coverage_pixels: usize = 0;
927
+ var y: usize = 0;
928
+ while (y < glyph_height) : (y += 1) {
929
+ var x: usize = 0;
930
+ while (x < glyph_width) : (x += 1) {
931
+ const coverage_byte = coverage_surface.pixelRgba8(x, y)[3];
932
+ if (coverage_byte == 0 or coverage_byte == 255) continue;
933
+ const coverage = @as(f32, @floatFromInt(coverage_byte)) / 255.0;
934
+ const rendered: i32 = blended_surface.pixelRgba8(x, y)[0];
935
+ const srgb_expected: i32 = blendSplitSrgbByte(0, 255, coverage);
936
+ // One level of slack: the recovered coverage byte is itself
937
+ // rounded, so the re-derived sRGB fold can sit one step off
938
+ // the value blended from the unrounded coverage.
939
+ try std.testing.expect(@max(rendered - srgb_expected, srgb_expected - rendered) <= 1);
940
+ // Mid-coverage fringes are where the spaces disagree most;
941
+ // hold text a wide margin away from the linear value there.
942
+ if (coverage_byte >= 64 and coverage_byte <= 192) {
943
+ mid_coverage_pixels += 1;
944
+ const linear_expected: i32 = blendSplitLinearByte(0, 255, coverage);
945
+ try std.testing.expect(linear_expected - rendered >= 16);
946
+ }
947
+ }
948
+ }
949
+ // The glyph must actually have exercised the fringe band.
950
+ try std.testing.expect(mid_coverage_pixels >= 4);
951
+ }
952
+
791
953
  test "reference renderer applies clip transform and opacity" {
792
954
  const commands = [_]CanvasCommand{
793
955
  .{ .push_clip = .{ .rect = geometry.RectF.init(1, 1, 2, 2) } },
@@ -965,13 +1127,16 @@ test "reference renderer strokes paths: butt caps end at the segment, round caps
965
1127
  // One horizontal unit-width segment, rendered once per cap shape.
966
1128
  // The end pixels (0,1) and (2,1) are where the caps live: the butt
967
1129
  // cap stops at the endpoint (the segment covers exactly half of each
968
- // end pixel — 128 after coverage rounding), while the round cap
969
- // bulges a half-width semicircle past it (75% coverage per the
970
- // anti-aliased vector core). Interior and off-stroke pixels are
971
- // cap-independent.
1130
+ // end pixel), while the round cap bulges a half-width semicircle
1131
+ // past it (75% coverage per the anti-aliased vector core). Interior
1132
+ // and off-stroke pixels are cap-independent. The expected end-pixel
1133
+ // bytes are the LINEAR-LIGHT encodings of those coverages over black
1134
+ // — geometry edge coverage blends in linear light (see the renderer's
1135
+ // `CoverageBlend`), so 50% coverage of a 255 channel re-encodes to
1136
+ // 188 and 75% to 225, not the 128/191 sRGB-space folds.
972
1137
  const cases = [_]struct { cap: canvas.LineCap, end_coverage: u8 }{
973
- .{ .cap = .butt, .end_coverage = 128 },
974
- .{ .cap = .round, .end_coverage = 191 },
1138
+ .{ .cap = .butt, .end_coverage = 188 },
1139
+ .{ .cap = .round, .end_coverage = 225 },
975
1140
  };
976
1141
  for (cases) |case| {
977
1142
  const elements = [_]PathElement{
@@ -631,6 +631,9 @@ pub const textInputClearButtonHitRect = widget_runtime.textInputClearButtonHitRe
631
631
  pub const textInputContentExtentForWidget = widget_runtime.textInputContentExtentForWidget;
632
632
  pub const textInputMaxScrollOffsetForWidget = widget_runtime.textInputMaxScrollOffsetForWidget;
633
633
  pub const clampedTextInputScrollOffsetForWidget = widget_runtime.clampedTextInputScrollOffsetForWidget;
634
+ pub const textInputMaxHorizontalScrollOffsetForWidget = widget_runtime.textInputMaxHorizontalScrollOffsetForWidget;
635
+ pub const clampedTextInputHorizontalScrollOffsetForWidget = widget_runtime.clampedTextInputHorizontalScrollOffsetForWidget;
636
+ pub const textInputCaretVisibleScrollOffsetForWidget = widget_runtime.textInputCaretVisibleScrollOffsetForWidget;
634
637
  pub const intrinsicWidgetSize = widget_runtime.intrinsicWidgetSize;
635
638
  pub const cursorForWidgetHit = widget_runtime.cursorForWidgetHit;
636
639
  pub const cursorForWidgetTarget = widget_runtime.cursorForWidgetTarget;
@@ -314,6 +314,9 @@ pub const textInputViewportForWidget = canvas.textInputViewportForWidget;
314
314
  pub const textInputContentExtentForWidget = canvas.textInputContentExtentForWidget;
315
315
  pub const textInputMaxScrollOffsetForWidget = canvas.textInputMaxScrollOffsetForWidget;
316
316
  pub const clampedTextInputScrollOffsetForWidget = canvas.clampedTextInputScrollOffsetForWidget;
317
+ pub const textInputMaxHorizontalScrollOffsetForWidget = canvas.textInputMaxHorizontalScrollOffsetForWidget;
318
+ pub const clampedTextInputHorizontalScrollOffsetForWidget = canvas.clampedTextInputHorizontalScrollOffsetForWidget;
319
+ pub const textInputCaretVisibleScrollOffsetForWidget = canvas.textInputCaretVisibleScrollOffsetForWidget;
317
320
  pub const intrinsicWidgetSize = canvas.intrinsicWidgetSize;
318
321
  pub const cursorForWidgetHit = canvas.cursorForWidgetHit;
319
322
  pub const cursorForWidgetTarget = canvas.cursorForWidgetTarget;
@@ -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 {