@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
@@ -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{
@@ -436,6 +437,113 @@ test "icon widgets render built-in vector icons as tinted path commands" {
436
437
  }
437
438
  }
438
439
 
440
+ test "checkbox check mark strokes one anti-aliased vector path" {
441
+ const checkbox = Widget{
442
+ .id = 63,
443
+ .kind = WidgetKind.checkbox,
444
+ .frame = geometry.RectF.init(0, 0, 24, 24),
445
+ .value = 1,
446
+ };
447
+ const tokens = DesignTokens{};
448
+ var commands: [8]CanvasCommand = undefined;
449
+ var builder = Builder.init(&commands);
450
+ try emitWidgetTree(&builder, checkbox, tokens);
451
+ const display_list = builder.displayList();
452
+ // The mark is ONE stroked polyline through the vector core — move
453
+ // plus two lines, round caps (and the vector core's round joins at
454
+ // the vee vertex), the same rasterization the built-in stroke icons
455
+ // get — not a pair of hard-edged line commands.
456
+ switch (display_list.findCommandById(widgetPartId(63, 4)).?.command) {
457
+ .stroke_path => |stroke| {
458
+ try std.testing.expectEqual(@as(usize, 3), stroke.elements.len);
459
+ try std.testing.expectEqual(PathVerb.move_to, stroke.elements[0].verb);
460
+ try std.testing.expectEqual(PathVerb.line_to, stroke.elements[1].verb);
461
+ try std.testing.expectEqual(PathVerb.line_to, stroke.elements[2].verb);
462
+ try std.testing.expectEqual(@as(f32, 2), stroke.stroke.width);
463
+ try std.testing.expectEqual(canvas.LineCap.round, stroke.cap);
464
+ },
465
+ else => return error.TestUnexpectedResult,
466
+ }
467
+
468
+ // Rasterized, the diagonal carries partial-coverage edge pixels —
469
+ // anti-aliasing a binary point-in-capsule test can never produce —
470
+ // and the whole render pins byte-identical.
471
+ var render_commands: [8]RenderCommand = undefined;
472
+ const plan = try (DisplayList{ .commands = display_list.commands }).renderPlan(&render_commands);
473
+ var pixels: [24 * 24 * 4]u8 = undefined;
474
+ @memset(&pixels, 0);
475
+ const surface = try ReferenceRenderSurface.init(24, 24, &pixels);
476
+ try surface.renderPass(.{
477
+ .commands = plan.commands,
478
+ .surface_size = geometry.SizeF.init(24, 24),
479
+ .full_repaint = true,
480
+ }, Color.rgb8(255, 255, 255));
481
+ // Sample strictly inside the accent fill (the 16px box spans y 4-20;
482
+ // stay clear of its border ring and corners) and count pixels that
483
+ // blend between the dark fill and the light mark.
484
+ var partial: usize = 0;
485
+ var y: usize = 6;
486
+ while (y < 18) : (y += 1) {
487
+ var x: usize = 2;
488
+ while (x < 14) : (x += 1) {
489
+ const value = pixels[(y * 24 + x) * 4];
490
+ if (value > 60 and value < 200) partial += 1;
491
+ }
492
+ }
493
+ try std.testing.expect(partial >= 4);
494
+ try std.testing.expectEqual(@as(u64, 10271374105851145327), support.referenceSurfaceSignature(&pixels));
495
+ }
496
+
497
+ test "a builder accumulating two widget trees keeps each checkbox mark's own geometry" {
498
+ const tokens = DesignTokens{};
499
+ const first = Widget{
500
+ .id = 63,
501
+ .kind = WidgetKind.checkbox,
502
+ .frame = geometry.RectF.init(0, 0, 24, 24),
503
+ .value = 1,
504
+ };
505
+ const second = Widget{
506
+ .id = 64,
507
+ .kind = WidgetKind.checkbox,
508
+ .frame = geometry.RectF.init(40, 8, 48, 48),
509
+ .value = 1,
510
+ };
511
+
512
+ // Snapshot the first mark's vertices from a solo emit, deep-copied
513
+ // before any other emission runs.
514
+ var solo_commands: [8]CanvasCommand = undefined;
515
+ var solo_builder = Builder.init(&solo_commands);
516
+ try emitWidgetTree(&solo_builder, first, tokens);
517
+ var expected: [3]canvas.PathElement = undefined;
518
+ switch (solo_builder.displayList().findCommandById(widgetPartId(63, 4)).?.command) {
519
+ .stroke_path => |stroke| {
520
+ try std.testing.expectEqual(@as(usize, 3), stroke.elements.len);
521
+ @memcpy(&expected, stroke.elements[0..3]);
522
+ },
523
+ else => return error.TestUnexpectedResult,
524
+ }
525
+
526
+ // One builder accumulating two separately emitted trees: the first
527
+ // tree's mark command must still describe the FIRST checkbox after
528
+ // the second emit — its elements live in builder-owned storage, so
529
+ // no later emission can overwrite them out from under the retained
530
+ // display list.
531
+ var commands: [16]CanvasCommand = undefined;
532
+ var builder = Builder.init(&commands);
533
+ try emitWidgetTree(&builder, first, tokens);
534
+ try emitWidgetTree(&builder, second, tokens);
535
+ switch (builder.displayList().findCommandById(widgetPartId(63, 4)).?.command) {
536
+ .stroke_path => |stroke| {
537
+ try std.testing.expectEqual(@as(usize, 3), stroke.elements.len);
538
+ for (expected, stroke.elements) |expected_element, element| {
539
+ try std.testing.expectEqual(expected_element.verb, element.verb);
540
+ try std.testing.expectEqual(expected_element.points[0], element.points[0]);
541
+ }
542
+ },
543
+ else => return error.TestUnexpectedResult,
544
+ }
545
+ }
546
+
439
547
  test "app-registered icons draw through the widget paths like built-ins" {
440
548
  var buffer = canvas.svg_icon.IconBuffer{};
441
549
  const parsed = try canvas.svg_icon.parse(
@@ -3158,3 +3266,331 @@ test "theme packs resolve by name and compose with every theme axis" {
3158
3266
  // Fields the override left null keep the pack's values.
3159
3267
  try std.testing.expectEqual(geist_light.metrics.control_height_lg, overridden.metrics.control_height_lg);
3160
3268
  }
3269
+
3270
+ test "hairline borders snap to whole device columns with smooth arcs" {
3271
+ const snapHairlineStrokeRect = @import("widget_render_style.zig").snapHairlineStrokeRect;
3272
+ const white = Color{ .r = 1, .g = 1, .b = 1, .a = 1 };
3273
+
3274
+ // Inactive without the geometry token or a usable scale.
3275
+ const unsnapped: StrokeRect = .{ .rect = geometry.RectF.init(4, 3, 24, 14), .radius = Radius.all(6), .stroke = .{ .fill = .{ .color = white }, .width = 1 } };
3276
+ const off = DesignTokens{ .pixel_snap = .{ .geometry = false, .scale = 1 } };
3277
+ try std.testing.expectEqual(unsnapped.rect.x, snapHairlineStrokeRect(off, unsnapped).rect.x);
3278
+
3279
+ // 1x: a 1px border pulls its centerline half a pixel inward so the
3280
+ // band covers exactly one column with its outer edge on the frame.
3281
+ const at_1x = DesignTokens{ .pixel_snap = .{ .geometry = true, .scale = 1 } };
3282
+ const snapped = snapHairlineStrokeRect(at_1x, unsnapped);
3283
+ try std.testing.expectEqual(@as(f32, 4.5), snapped.rect.x);
3284
+ try std.testing.expectEqual(@as(f32, 3.5), snapped.rect.y);
3285
+ try std.testing.expectEqual(@as(f32, 23), snapped.rect.width);
3286
+ try std.testing.expectEqual(@as(f32, 1), snapped.stroke.width);
3287
+ // The outer arc keeps the frame's radius: centerline radius shrinks
3288
+ // by the half-width inset.
3289
+ try std.testing.expectEqual(@as(f32, 5.5), snapped.radius.top_left);
3290
+
3291
+ // Wider borders keep true geometry (device width above the
3292
+ // hairline range).
3293
+ const wide: StrokeRect = .{ .rect = geometry.RectF.init(4, 3, 24, 14), .radius = Radius.all(6), .stroke = .{ .fill = .{ .color = white }, .width = 3 } };
3294
+ try std.testing.expectEqual(@as(f32, 4), snapHairlineStrokeRect(at_1x, wide).rect.x);
3295
+
3296
+ // Fractional scale: snapping happens on the DEVICE grid. At 1.25x a
3297
+ // 1px logical border snaps to one device pixel (0.8 logical) — the
3298
+ // floor of its exact 1.25-device width — and a frame edge at
3299
+ // logical 4 (device 5) pulls the centerline to device 5.5 =
3300
+ // logical 4.4.
3301
+ const at_fractional = DesignTokens{ .pixel_snap = .{ .geometry = true, .scale = 1.25 } };
3302
+ const fractional = snapHairlineStrokeRect(at_fractional, unsnapped);
3303
+ try std.testing.expectEqual(@as(f32, 4.4), fractional.rect.x);
3304
+ try std.testing.expectEqual(@as(f32, 0.8), fractional.stroke.width);
3305
+
3306
+ // At 1.5x the exact device width of a 1px logical border is 1.5 —
3307
+ // between grid widths — and the snap takes the FLOOR: one device
3308
+ // column (2/3 logical), the crisp-and-light choice, never two. The
3309
+ // frame edge at logical 4 (device 6) pulls the centerline to
3310
+ // device 6.5.
3311
+ const at_halfstep = DesignTokens{ .pixel_snap = .{ .geometry = true, .scale = 1.5 } };
3312
+ const halfstep = snapHairlineStrokeRect(at_halfstep, unsnapped);
3313
+ try std.testing.expectEqual(@as(f32, 6.5 / 1.5), halfstep.rect.x);
3314
+ try std.testing.expectEqual(@as(f32, 1.0 / 1.5), halfstep.stroke.width);
3315
+
3316
+ // A stroke whose device width rounds to zero stays unsnapped — the
3317
+ // floor's one-pixel minimum never darkens a sub-half-pixel stroke.
3318
+ var whisper = unsnapped;
3319
+ whisper.stroke.width = 0.25;
3320
+ try std.testing.expectEqual(@as(f32, 0.25), snapHairlineStrokeRect(at_halfstep, whisper).stroke.width);
3321
+ try std.testing.expectEqual(unsnapped.rect.x, snapHairlineStrokeRect(at_halfstep, whisper).rect.x);
3322
+
3323
+ // Rendered at 1x, the straight runs land as exactly one full-alpha
3324
+ // column inside the frame — no half-covered neighbors — while the
3325
+ // corners keep fractional arc coverage from the continuous field.
3326
+ var pixels: [40 * 24 * 4]u8 = undefined;
3327
+ const surface = try ReferenceRenderSurface.init(40, 24, &pixels);
3328
+ const bounds = geometry.RectF.init(0, 0, 40, 24);
3329
+ const command = RenderCommand{
3330
+ .command = .{ .stroke_rect = snapped },
3331
+ .local_bounds = bounds,
3332
+ .bounds = bounds,
3333
+ };
3334
+ try surface.renderPass(.{
3335
+ .surface_size = geometry.SizeF.init(40, 24),
3336
+ .scale = 1,
3337
+ .full_repaint = true,
3338
+ .commands = &.{command},
3339
+ }, Color{ .r = 0, .g = 0, .b = 0, .a = 0 });
3340
+ // Mid-height row: outside column empty, frame column fully inked,
3341
+ // next column inside empty again.
3342
+ try std.testing.expectEqual(@as(u8, 0), surface.pixelRgba8(3, 10)[3]);
3343
+ try std.testing.expectEqual(@as(u8, 255), surface.pixelRgba8(4, 10)[3]);
3344
+ try std.testing.expectEqual(@as(u8, 0), surface.pixelRgba8(5, 10)[3]);
3345
+ // Mid-width column: same on the top run.
3346
+ try std.testing.expectEqual(@as(u8, 0), surface.pixelRgba8(16, 2)[3]);
3347
+ try std.testing.expectEqual(@as(u8, 255), surface.pixelRgba8(16, 3)[3]);
3348
+ try std.testing.expectEqual(@as(u8, 0), surface.pixelRgba8(16, 4)[3]);
3349
+ // Corner pixels carry fractional arc coverage — smooth, not binary.
3350
+ const corner = surface.pixelRgba8(5, 4)[3];
3351
+ try std.testing.expect(corner > 0 and corner < 255);
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
+ }
@@ -87,6 +87,7 @@ const widgetAccentForegroundColor = widget_render_style.widgetAccentForegroundCo
87
87
  const widgetRadius = widget_render_style.widgetRadius;
88
88
  pub const controlRadius = widget_render_style.controlRadius;
89
89
  pub const controlStrokeWidth = widget_render_style.controlStrokeWidth;
90
+ const snapHairlineStrokeRect = widget_render_style.snapHairlineStrokeRect;
90
91
  pub const selectControlVisualTokens = widget_render_style.selectControlVisualTokens;
91
92
  pub const textInputControlVisualTokens = widget_render_style.textInputControlVisualTokens;
92
93
  const alertControlVisualTokens = widget_render_style.alertControlVisualTokens;
@@ -112,29 +113,23 @@ pub const sliderWidgetKnobRect = widget_render_controls.sliderWidgetKnobRect;
112
113
 
113
114
  const max_widget_depth: usize = 32;
114
115
 
115
- /// Frame-lifetime scratch for widget-built path elements: `.chart`
116
- /// widgets build their line/band `PathElement`s here at emit time, and
117
- /// `.spinner` widgets their arc segment (unlike icons, whose elements
118
- /// are comptime-static); emitted commands slice into it. The event loop
119
- /// is single-threaded and the runtime copies the display list into
120
- /// per-view storage within the same emit call stack, so one threadlocal
121
- /// buffer per frame is sound — reset at each emit entry point. Sized to
122
- /// mirror the runtime's per-view path-element budget
123
- /// (`canvas_limits.max_canvas_path_elements_per_view`; a lockstep test
124
- /// keeps them equal), so overflow here fails exactly where the per-view
125
- /// copy would have refused anyway — loudly, by budget name.
126
- threadlocal var frame_path_elements: [chart_model.max_chart_path_elements_per_frame]drawing_model.PathElement = undefined;
127
- threadlocal var frame_path_len: usize = 0;
116
+ // Widget-built path elements (`.chart` lines/bands, the `.spinner` arc
117
+ // and segments, the `.checkbox` check mark — unlike icons, whose
118
+ // elements are comptime-static) allocate from the display-list builder's
119
+ // own store (`Builder.allocPathElements`), so the emitted commands'
120
+ // element slices share the builder's lifetime instead of pointing into
121
+ // shared scratch that a later emission could overwrite.
128
122
 
129
123
  /// Frame-lifetime scratch for formatted chart label text (y tick values
130
- /// and hover-detail rows): `drawText` commands slice into it under the
131
- /// same single-threaded copy-before-return contract as the path
132
- /// elements above. Overflow fails loudly by budget name.
124
+ /// and hover-detail rows): `drawText` commands slice into it. The event
125
+ /// loop is single-threaded and the runtime copies the display list into
126
+ /// per-view storage within the same emit call stack, so one threadlocal
127
+ /// buffer per frame is sound — reset at each emit entry point. Overflow
128
+ /// fails loudly by budget name.
133
129
  threadlocal var frame_label_bytes: [chart_model.max_chart_label_bytes_per_frame]u8 = undefined;
134
130
  threadlocal var frame_label_len: usize = 0;
135
131
 
136
- fn resetFramePathScratch() void {
137
- frame_path_len = 0;
132
+ fn resetFrameLabelScratch() void {
138
133
  frame_label_len = 0;
139
134
  }
140
135
 
@@ -149,21 +144,14 @@ fn allocFrameLabelBytes(text: []const u8) Error![]const u8 {
149
144
  }
150
145
 
151
146
  /// Frame-lifetime scratch (same single-threaded emit contract as the
152
- /// path-element scratch above) holding the root bounds of the tree being
147
+ /// label scratch above) holding the root bounds of the tree being
153
148
  /// emitted: the rect a modal surface's scrim covers. Chrome emission
154
149
  /// happens deep in the recursion where no ancestor frame is in scope, so
155
150
  /// the entry points record it here.
156
151
  threadlocal var scrim_viewport: ?geometry.RectF = null;
157
152
 
158
- fn allocFramePathElements(count: usize) Error![]drawing_model.PathElement {
159
- if (frame_path_len + count > frame_path_elements.len) return error.ChartPathElementListFull;
160
- const start = frame_path_len;
161
- frame_path_len += count;
162
- return frame_path_elements[start .. start + count];
163
- }
164
-
165
153
  pub fn emitWidgetTree(builder: *Builder, widget: Widget, tokens: DesignTokens) Error!void {
166
- resetFramePathScratch();
154
+ resetFrameLabelScratch();
167
155
  scrim_viewport = widget.frame.normalized();
168
156
  try emitWidgetDepth(builder, widget, tokens, 0);
169
157
  }
@@ -173,7 +161,7 @@ pub fn emitWidgetLayout(builder: *Builder, layout: anytype, tokens: DesignTokens
173
161
  }
174
162
 
175
163
  pub fn emitWidgetLayoutWithState(builder: *Builder, layout: anytype, tokens: DesignTokens, state: WidgetRenderState) Error!void {
176
- resetFramePathScratch();
164
+ resetFrameLabelScratch();
177
165
  scrim_viewport = widgetLayoutRootBounds(layout);
178
166
  try emitWidgetLayoutChildren(builder, layout, null, tokens, state);
179
167
  try emitWidgetLayoutAnchored(builder, layout, tokens, state);
@@ -1201,7 +1189,7 @@ fn emitAvatarWidget(builder: *Builder, widget: Widget, tokens: DesignTokens) Err
1201
1189
  });
1202
1190
  }
1203
1191
 
1204
- try builder.strokeRect(.{
1192
+ try builder.strokeRect(snapHairlineStrokeRect(tokens, .{
1205
1193
  .id = widgetPartId(widget.id, 4),
1206
1194
  .rect = widget.frame,
1207
1195
  .radius = radius,
@@ -1209,7 +1197,7 @@ fn emitAvatarWidget(builder: *Builder, widget: Widget, tokens: DesignTokens) Err
1209
1197
  .fill = widgetBorderFill(widget, visual.border orelse tokens.colors.border),
1210
1198
  .width = controlStrokeWidth(widget, visual, tokens.stroke.hairline),
1211
1199
  },
1212
- });
1200
+ }));
1213
1201
  }
1214
1202
 
1215
1203
  fn emitBadgeWidget(builder: *Builder, widget: Widget, tokens: DesignTokens) Error!void {
@@ -1223,7 +1211,7 @@ fn emitBadgeWidget(builder: *Builder, widget: Widget, tokens: DesignTokens) Erro
1223
1211
  .radius = radius,
1224
1212
  .fill = colorFill(badgeBackgroundColor(widget, tokens, visual)),
1225
1213
  });
1226
- try builder.strokeRect(.{
1214
+ try builder.strokeRect(snapHairlineStrokeRect(tokens, .{
1227
1215
  .id = widgetPartId(widget.id, 2),
1228
1216
  .rect = widget.frame,
1229
1217
  .radius = radius,
@@ -1231,7 +1219,7 @@ fn emitBadgeWidget(builder: *Builder, widget: Widget, tokens: DesignTokens) Erro
1231
1219
  .fill = widgetBorderFill(widget, badgeBorderColor(widget, tokens, visual)),
1232
1220
  .width = badgeStrokeWidth(widget, tokens, visual),
1233
1221
  },
1234
- });
1222
+ }));
1235
1223
  const content_color = badgeTextColor(widget, tokens, visual);
1236
1224
  // Inline vector icon: icon-only badges center it (the stepper's
1237
1225
  // completed check, status chips); icon + text draws it before the
@@ -1329,7 +1317,7 @@ fn emitSplitDividerWidget(builder: *Builder, widget: Widget, tokens: DesignToken
1329
1317
  .fill = colorFill(line_color),
1330
1318
  });
1331
1319
  if (widget.state.focused) {
1332
- try builder.strokeRect(.{
1320
+ try builder.strokeRect(snapHairlineStrokeRect(tokens, .{
1333
1321
  .id = widgetPartId(widget.id, 2),
1334
1322
  .rect = pixelSnapGeometryRect(tokens, normalized),
1335
1323
  .radius = Radius.all(tokens.radius.sm),
@@ -1337,7 +1325,7 @@ fn emitSplitDividerWidget(builder: *Builder, widget: Widget, tokens: DesignToken
1337
1325
  .fill = widget_render_style.widgetFocusRingFill(widget, tokens),
1338
1326
  .width = tokens.stroke.focus,
1339
1327
  },
1340
- });
1328
+ }));
1341
1329
  }
1342
1330
  }
1343
1331
 
@@ -1536,7 +1524,7 @@ fn emitSpinnerArc(builder: *Builder, widget: Widget, visual: ControlVisualTokens
1536
1524
  const delta_degrees = spinner_arc_sweep_degrees / @as(f32, @floatFromInt(segment_total));
1537
1525
  const kappa = (4.0 / 3.0) * @tan(std.math.degreesToRadians(delta_degrees) * 0.25) * radius;
1538
1526
 
1539
- const elements = try allocFramePathElements(1 + segment_total);
1527
+ const elements = try builder.allocPathElements(1 + segment_total);
1540
1528
  const start_radians = std.math.degreesToRadians(start_degrees);
1541
1529
  elements[0] = .{ .verb = .move_to, .points = .{
1542
1530
  geometry.PointF.init(center.x + radius * @cos(start_radians), center.y + radius * @sin(start_radians)),
@@ -1622,7 +1610,7 @@ fn emitSpinnerSegments(builder: *Builder, widget: Widget, tokens: DesignTokens,
1622
1610
  const f = geometry.PointF.init(inner.x - cap * v.x, inner.y - cap * v.y);
1623
1611
  const inner_mid = geometry.PointF.init(inner.x - cap * u.x, inner.y - cap * u.y);
1624
1612
 
1625
- const elements = try allocFramePathElements(8);
1613
+ const elements = try builder.allocPathElements(8);
1626
1614
  elements[0] = .{ .verb = .move_to, .points = .{ a, geometry.PointF.zero(), geometry.PointF.zero() } };
1627
1615
  elements[1] = .{ .verb = .line_to, .points = .{ b, geometry.PointF.zero(), geometry.PointF.zero() } };
1628
1616
  elements[2] = .{ .verb = .cubic_to, .points = .{
@@ -1987,7 +1975,7 @@ fn emitChartLine(
1987
1975
 
1988
1976
  if (series.fill) {
1989
1977
  const base_y = chartMapY(chartBaselineValue(domain), domain, plot, 0);
1990
- const elements = try allocFramePathElements(points.len + 3);
1978
+ const elements = try builder.allocPathElements(points.len + 3);
1991
1979
  for (points, 0..) |point, index| {
1992
1980
  elements[index] = .{
1993
1981
  .verb = if (index == 0) .move_to else .line_to,
@@ -2004,7 +1992,7 @@ fn emitChartLine(
2004
1992
  });
2005
1993
  }
2006
1994
 
2007
- const elements = try allocFramePathElements(points.len);
1995
+ const elements = try builder.allocPathElements(points.len);
2008
1996
  for (points, 0..) |point, index| {
2009
1997
  elements[index] = .{
2010
1998
  .verb = if (index == 0) .move_to else .line_to,
@@ -2035,7 +2023,7 @@ fn emitChartBand(
2035
2023
  const base_y = chartMapY(chartBaselineValue(domain), domain, plot, 0);
2036
2024
  const pair_count = @min(series.values.len, series.low.len);
2037
2025
  const lower_count = if (pair_count >= 2) pair_count else 2;
2038
- const elements = try allocFramePathElements(upper.len + lower_count + 1);
2026
+ const elements = try builder.allocPathElements(upper.len + lower_count + 1);
2039
2027
  for (upper, 0..) |point, index| {
2040
2028
  elements[index] = .{
2041
2029
  .verb = if (index == 0) .move_to else .line_to,
@@ -2322,12 +2310,12 @@ fn emitChartHoverDetail(builder: *Builder, widget: Widget, tokens: DesignTokens,
2322
2310
  .radius = radius,
2323
2311
  .fill = colorFill(tokens.colors.surface),
2324
2312
  });
2325
- try builder.strokeRect(.{
2313
+ try builder.strokeRect(snapHairlineStrokeRect(tokens, .{
2326
2314
  .id = chartCommandId(widget.id, chart_hover_seed, 0, 3),
2327
2315
  .rect = card,
2328
2316
  .radius = radius,
2329
2317
  .stroke = .{ .fill = colorFill(tokens.colors.border), .width = hairline },
2330
- });
2318
+ }));
2331
2319
 
2332
2320
  // Title line, then one swatch/name/value row per series holding
2333
2321
  // this sample. Values right-align on the card's inner edge so a