@native-sdk/cli 0.9.3 → 0.9.5

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 (84) hide show
  1. package/README.md +3 -3
  2. package/build/app.zig +323 -76
  3. package/build/ts_run.mjs +15 -18
  4. package/build.zig +83 -8
  5. package/package.json +14 -10
  6. package/packages/core/compile-surface/core.ts +1 -0
  7. package/packages/core/package-lock.json +16 -13
  8. package/packages/core/package.json +5 -2
  9. package/packages/core/scripts/compiler_command.mjs +71 -0
  10. package/packages/core/scripts/compiler_typecheck.mjs +4 -4
  11. package/packages/core/scripts/embed_markup_c.mjs +25 -0
  12. package/packages/core/scripts/npm_static_spike.mjs +3 -3
  13. package/packages/core/scripts/run_external_core_compiler.mjs +21 -10
  14. package/packages/core/scripts/run_external_service_compiler.mjs +17 -7
  15. package/packages/core/scripts/vendor_service_packages.mjs +121 -8
  16. package/packages/core/sdk/core.ts +1 -0
  17. package/packages/core/sdk/events.d.ts +36 -4
  18. package/packages/core/sdk/events.ts +49 -6
  19. package/packages/core/src/checker.ts +106 -10
  20. package/packages/core/src/modules.ts +6 -2
  21. package/packages/core/src/sqlite_cli.ts +22 -2
  22. package/packages/core/src/typed_ast.ts +4 -2
  23. package/packages/core/src/types.ts +8 -8
  24. package/schemas/app.schema.json +435 -0
  25. package/skill-data/core/SKILL.md +53 -15
  26. package/skill-data/core/references/project-anatomy.md +6 -6
  27. package/skill-data/native-ui/SKILL.md +28 -6
  28. package/skill-data/ts-core/SKILL.md +9 -6
  29. package/skill-data/ts-services/references/service-surface.md +1 -1
  30. package/skills/native-sdk/SKILL.md +2 -2
  31. package/src/app_runner/ts_core_main.zig +12 -4
  32. package/src/app_runner/ts_core_mobile.zig +6 -2
  33. package/src/automation/snapshot.zig +91 -3
  34. package/src/platform/macos/appkit_host.h +41 -2
  35. package/src/platform/macos/appkit_host.m +198 -9
  36. package/src/platform/macos/cef_host.mm +191 -9
  37. package/src/platform/macos/root.zig +110 -3
  38. package/src/platform/null_platform.zig +50 -1
  39. package/src/platform/null_platform_tests.zig +53 -0
  40. package/src/platform/root.zig +9 -0
  41. package/src/platform/types.zig +58 -0
  42. package/src/platform/windows/root.zig +88 -15
  43. package/src/primitives/canvas/reference.zig +37 -15
  44. package/src/primitives/canvas/reference_tests.zig +63 -0
  45. package/src/primitives/canvas/ui.zig +9 -0
  46. package/src/primitives/canvas/ui_markup.zig +29 -0
  47. package/src/primitives/canvas/ui_markup_compiled.zig +32 -0
  48. package/src/primitives/canvas/ui_markup_compiled_tests.zig +4 -2
  49. package/src/primitives/canvas/ui_markup_contract.zig +7 -0
  50. package/src/primitives/canvas/ui_markup_contract_tests.zig +21 -0
  51. package/src/primitives/canvas/ui_markup_tests.zig +6 -0
  52. package/src/primitives/canvas/ui_markup_view.zig +28 -0
  53. package/src/primitives/canvas/ui_markup_view_tests.zig +32 -3
  54. package/src/primitives/canvas/ui_schema.zig +9 -0
  55. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  56. package/src/primitives/canvas/ui_tests.zig +4 -2
  57. package/src/primitives/canvas/widget_render.zig +7 -2
  58. package/src/primitives/canvas/widget_semantics_tests.zig +3 -1
  59. package/src/root.zig +4 -0
  60. package/src/runtime/automation_snapshot.zig +3 -0
  61. package/src/runtime/flow.zig +1 -1
  62. package/src/runtime/state.zig +14 -0
  63. package/src/runtime/system_services.zig +62 -0
  64. package/src/runtime/ts_ui_app.zig +181 -39
  65. package/src/runtime/ui_app.zig +40 -1
  66. package/src/runtime/ui_app_tests.zig +63 -0
  67. package/src/runtime/ui_app_window_tests.zig +23 -0
  68. package/src/runtime/validation.zig +87 -1
  69. package/src/tooling/android.zig +5 -4
  70. package/src/tooling/buildgraph.zig +24 -8
  71. package/src/tooling/db.zig +1 -1
  72. package/src/tooling/doctor.zig +1 -1
  73. package/src/tooling/ios.zig +5 -4
  74. package/src/tooling/json_to_zon.zig +136 -0
  75. package/src/tooling/manifest.zig +120 -33
  76. package/src/tooling/raw_manifest.zig +4 -0
  77. package/src/tooling/root.zig +1 -0
  78. package/src/tooling/templates.zig +171 -118
  79. package/src/tooling/ts_core.zig +12 -8
  80. package/src/tooling/verbs.zig +233 -5
  81. package/src/tooling/web_engine.zig +17 -1
  82. package/tools/corewire/emit_profile.zig +20 -7
  83. package/tools/corewire/emit_service.zig +49 -24
  84. package/tools/corewire/main.zig +21 -2
@@ -266,6 +266,10 @@ pub const max_tray_tooltip_bytes: usize = 256;
266
266
  pub const max_tray_item_label_bytes: usize = 256;
267
267
  pub const max_tray_item_command_bytes: usize = 128;
268
268
  pub const max_tray_item_detail_bytes: usize = 256;
269
+ pub const max_tray_segment_options: usize = 8;
270
+ pub const max_tray_segment_label_bytes: usize = 64;
271
+ pub const max_tray_chart_values: usize = 32;
272
+ pub const max_tray_chart_text_bytes: usize = 128;
269
273
  pub const max_drop_paths_bytes: usize = 8192;
270
274
  pub const max_drop_paths: usize = max_drop_paths_bytes / 2 + 1;
271
275
  pub const max_window_event_name_bytes: usize = 64;
@@ -1466,6 +1470,13 @@ pub const TrayTone = enum(u8) {
1466
1470
  critical,
1467
1471
  };
1468
1472
 
1473
+ pub const TrayFontWeight = enum(u8) {
1474
+ regular,
1475
+ medium,
1476
+ semibold,
1477
+ bold,
1478
+ };
1479
+
1469
1480
  /// The model-derived part of a status item. Keeping this separate from
1470
1481
  /// icon/tooltip/activation options lets UiApp patch presentation without
1471
1482
  /// recreating the native item.
@@ -1477,6 +1488,9 @@ pub const TrayPresentation = struct {
1477
1488
  tone: TrayTone = .normal,
1478
1489
  icon_opacity: f32 = 1,
1479
1490
  monospaced: bool = false,
1491
+ /// Explicit title size in points; zero keeps the host's menu-bar default.
1492
+ font_size: f32 = 0,
1493
+ font_weight: TrayFontWeight = .regular,
1480
1494
  };
1481
1495
 
1482
1496
  pub const TrayOptions = struct {
@@ -1531,6 +1545,45 @@ pub const TrayItemRole = enum(u8) {
1531
1545
  hero,
1532
1546
  agent,
1533
1547
  context,
1548
+ segmented,
1549
+ chart,
1550
+ };
1551
+
1552
+ /// One choice inside a typed segmented tray row. Its stable id and command
1553
+ /// participate in the same namespace and dispatch route as an ordinary tray
1554
+ /// command row; the containing row itself is display structure, not an
1555
+ /// action. At most one option in a row may be selected.
1556
+ pub const TraySegmentOption = struct {
1557
+ id: TrayItemId,
1558
+ label: []const u8,
1559
+ command: []const u8,
1560
+ selected: bool = false,
1561
+ enabled: bool = true,
1562
+ };
1563
+
1564
+ pub const TraySegmentedRow = struct {
1565
+ options: []const TraySegmentOption = &.{},
1566
+ };
1567
+
1568
+ /// A prominent two-line metric block inside a tray menu. This is semantic
1569
+ /// dropdown content, distinct from the persistent menu-bar title.
1570
+ pub const TrayMetricRow = struct {
1571
+ primary_text: []const u8,
1572
+ secondary_text: []const u8 = "",
1573
+ accessibility_label: []const u8,
1574
+ };
1575
+
1576
+ /// A bounded bar-chart/sparkline readout. Values are finite and must fall in
1577
+ /// the explicit `min_value...max_value` domain. The text fields remain
1578
+ /// semantic data on every host; capable macOS hosts place them around a
1579
+ /// native AppKit-drawn bar chart.
1580
+ pub const TrayChartRow = struct {
1581
+ values: []const f32 = &.{},
1582
+ min_value: f32 = 0,
1583
+ max_value: f32 = 1,
1584
+ leading_caption: []const u8 = "",
1585
+ trailing_summary: []const u8 = "",
1586
+ accessibility_label: []const u8 = "",
1534
1587
  };
1535
1588
 
1536
1589
  pub const TrayMenuItem = struct {
@@ -1543,6 +1596,11 @@ pub const TrayMenuItem = struct {
1543
1596
  role: TrayItemRole = .command,
1544
1597
  key: []const u8 = "",
1545
1598
  modifiers: ShortcutModifiers = .{},
1599
+ /// Typed rich-row payloads. Exactly one is present for its matching role;
1600
+ /// both stay null for every existing command/readout row.
1601
+ segmented: ?TraySegmentedRow = null,
1602
+ metric: ?TrayMetricRow = null,
1603
+ chart: ?TrayChartRow = null,
1546
1604
  };
1547
1605
 
1548
1606
  pub const NativeCommandEvent = struct {
@@ -1519,6 +1519,34 @@ fn showNotification(context: ?*anyopaque, options: platform_mod.NotificationOpti
1519
1519
 
1520
1520
  const max_tray_items: usize = 32;
1521
1521
 
1522
+ const TrayFallbackText = struct {
1523
+ label: []const u8,
1524
+ detail: []const u8,
1525
+ };
1526
+
1527
+ /// Project a rich row onto Win32's plain label/detail menu surface. Chart
1528
+ /// captions are optional, but the accessibility label is required, so a
1529
+ /// captionless chart must still remain visible instead of becoming a blank
1530
+ /// disabled row.
1531
+ fn trayFallbackText(item: platform_mod.TrayMenuItem) TrayFallbackText {
1532
+ if (item.metric) |metric| return .{
1533
+ .label = metric.primary_text,
1534
+ .detail = metric.secondary_text,
1535
+ };
1536
+ if (item.chart) |chart| {
1537
+ if (chart.leading_caption.len > 0) return .{
1538
+ .label = chart.leading_caption,
1539
+ .detail = chart.trailing_summary,
1540
+ };
1541
+ if (chart.trailing_summary.len > 0) return .{
1542
+ .label = chart.trailing_summary,
1543
+ .detail = "",
1544
+ };
1545
+ return .{ .label = chart.accessibility_label, .detail = "" };
1546
+ }
1547
+ return .{ .label = item.label, .detail = item.detail };
1548
+ }
1549
+
1522
1550
  fn createTray(context: ?*anyopaque, status_item_id: platform_mod.StatusItemId, options: platform_mod.TrayOptions) anyerror!void {
1523
1551
  const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
1524
1552
  if (status_item_id != platform_mod.primary_status_item_id) return error.UnsupportedService;
@@ -1545,7 +1573,7 @@ fn updateTrayMenu(context: ?*anyopaque, status_item_id: platform_mod.StatusItemI
1545
1573
  const self: *WindowsPlatform = @ptrCast(@alignCast(context.?));
1546
1574
  if (status_item_id != platform_mod.primary_status_item_id) return error.UnsupportedService;
1547
1575
  if (self.web_engine != .system) return error.UnsupportedService;
1548
- const count = @min(items.len, max_tray_items);
1576
+ var count: usize = 0;
1549
1577
  var ids: [max_tray_items]u32 = undefined;
1550
1578
  var labels: [max_tray_items][*]const u8 = undefined;
1551
1579
  var label_lens: [max_tray_items]usize = undefined;
@@ -1563,23 +1591,44 @@ fn updateTrayMenu(context: ?*anyopaque, status_item_id: platform_mod.StatusItemI
1563
1591
  // returning). Doubling covers the all-ampersands worst case.
1564
1592
  var label_pool: [max_tray_items * (platform_mod.max_tray_item_label_bytes + platform_mod.max_tray_item_detail_bytes) * 2]u8 = undefined;
1565
1593
  var pool_used: usize = 0;
1566
- for (items[0..count], 0..) |item, index| {
1567
- const label = escapeMenuLabelAmpersands(item.label, &label_pool, &pool_used);
1568
- const detail = escapeMenuLabelAmpersands(item.detail, &label_pool, &pool_used);
1569
- ids[index] = item.id;
1570
- labels[index] = label.ptr;
1571
- label_lens[index] = label.len;
1572
- separators[index] = if (item.separator) 1 else 0;
1594
+ for (items) |item| {
1595
+ if (item.segmented) |segmented| {
1596
+ for (segmented.options) |option| {
1597
+ const label = escapeMenuLabelAmpersands(option.label, &label_pool, &pool_used);
1598
+ ids[count] = option.id;
1599
+ labels[count] = label.ptr;
1600
+ label_lens[count] = label.len;
1601
+ separators[count] = 0;
1602
+ enabled_flags[count] = if (option.enabled) 1 else 0;
1603
+ const selected_detail = if (option.selected) "Selected" else "";
1604
+ details[count] = selected_detail.ptr;
1605
+ detail_lens[count] = selected_detail.len;
1606
+ roles[count] = @intFromEnum(platform_mod.TrayItemRole.command);
1607
+ keys[count] = "".ptr;
1608
+ key_lens[count] = 0;
1609
+ modifiers[count] = 0;
1610
+ count += 1;
1611
+ }
1612
+ continue;
1613
+ }
1614
+ const fallback = trayFallbackText(item);
1615
+ const label = escapeMenuLabelAmpersands(fallback.label, &label_pool, &pool_used);
1616
+ const detail = escapeMenuLabelAmpersands(fallback.detail, &label_pool, &pool_used);
1617
+ ids[count] = item.id;
1618
+ labels[count] = label.ptr;
1619
+ label_lens[count] = label.len;
1620
+ separators[count] = if (item.separator) 1 else 0;
1573
1621
  // Windows has no custom status-menu row seam. Preserve semantic
1574
1622
  // readouts as visible detail text, while only command/agent rows can
1575
1623
  // become actions.
1576
- enabled_flags[index] = if (item.enabled and (item.role == .command or item.role == .agent)) 1 else 0;
1577
- details[index] = detail.ptr;
1578
- detail_lens[index] = detail.len;
1579
- roles[index] = @intFromEnum(item.role);
1580
- keys[index] = item.key.ptr;
1581
- key_lens[index] = item.key.len;
1582
- modifiers[index] = shortcutModifierFlags(item.modifiers);
1624
+ enabled_flags[count] = if (item.enabled and (item.role == .command or item.role == .agent)) 1 else 0;
1625
+ details[count] = detail.ptr;
1626
+ detail_lens[count] = detail.len;
1627
+ roles[count] = @intFromEnum(item.role);
1628
+ keys[count] = item.key.ptr;
1629
+ key_lens[count] = item.key.len;
1630
+ modifiers[count] = shortcutModifierFlags(item.modifiers);
1631
+ count += 1;
1583
1632
  }
1584
1633
  if (native_sdk_windows_update_tray_menu(self.host, &ids, &labels, &label_lens, &separators, &enabled_flags, &details, &detail_lens, &roles, &keys, &key_lens, &modifiers, count) == 0) return error.UnsupportedService;
1585
1634
  }
@@ -2183,6 +2232,30 @@ test "windows tray carries lifecycle commands rich rows and key equivalents into
2183
2232
  try std.testing.expect(std.mem.indexOf(u8, host_source, "tray_event == NIN_SELECT || tray_event == NIN_KEYSELECT || tray_event == WM_LBUTTONUP") != null);
2184
2233
  }
2185
2234
 
2235
+ test "windows chart fallback uses accessibility text when captions are absent" {
2236
+ const values = [_]f32{0.5};
2237
+ const fallback = trayFallbackText(.{
2238
+ .role = .chart,
2239
+ .chart = .{
2240
+ .values = &values,
2241
+ .accessibility_label = "CPU history, 50 percent",
2242
+ },
2243
+ });
2244
+ try std.testing.expectEqualStrings("CPU history, 50 percent", fallback.label);
2245
+ try std.testing.expectEqualStrings("", fallback.detail);
2246
+
2247
+ const summarized = trayFallbackText(.{
2248
+ .role = .chart,
2249
+ .chart = .{
2250
+ .values = &values,
2251
+ .trailing_summary = "50%",
2252
+ .accessibility_label = "CPU history, 50 percent",
2253
+ },
2254
+ });
2255
+ try std.testing.expectEqualStrings("50%", summarized.label);
2256
+ try std.testing.expectEqualStrings("", summarized.detail);
2257
+ }
2258
+
2186
2259
  test "windows refuses a tray-less .hide main window at platform init instead of stranding it hidden" {
2187
2260
  // The pre-created MAIN window never passes through the runtime's
2188
2261
  // create gate, so the platform's init gate must hold the same line
@@ -486,6 +486,7 @@ pub const ReferenceRenderSurface = struct {
486
486
  } else return error.ReferenceRenderUnsupportedCommand;
487
487
 
488
488
  const src_rect = referenceImageSourceRect(image, value.src) orelse return;
489
+ const sample_bounds = referenceImageSampleBounds(image, src_rect);
489
490
  const local_dst = referenceImageDestinationRect(value.dst, src_rect, value.fit) orelse return;
490
491
  const dst_rect = command.transform.transformRect(local_dst).normalized();
491
492
  // The rounded mask applies over the REQUESTED destination (the
@@ -510,7 +511,7 @@ pub const ReferenceRenderSurface = struct {
510
511
  // once per (image content, size, phase) and every later repaint
511
512
  // — the cover-loading cascade, a re-opened view, a whole-pixel
512
513
  // move — blends from the panel.
513
- if (self.imageScalePanel(image, value, src_rect, dst_rect, pixel_rect)) |panel| {
514
+ if (self.imageScalePanel(image, value, src_rect, sample_bounds, dst_rect, pixel_rect)) |panel| {
514
515
  const dst_x0: i64 = @intFromFloat(@floor(dst_rect.x));
515
516
  const dst_y0: i64 = @intFromFloat(@floor(dst_rect.y));
516
517
  var y = pixel_rect.y;
@@ -549,7 +550,7 @@ pub const ReferenceRenderSurface = struct {
549
550
  if (has_mask and !referencePointInRoundedRect(point, mask_rect, mask_radius)) continue;
550
551
  const u = std.math.clamp((point.x - dst_rect.x) / dst_rect.width, 0, 1);
551
552
  const v = std.math.clamp((point.y - dst_rect.y) / dst_rect.height, 0, 1);
552
- const sample = referenceSampleImage(image, src_rect, u, v, value.sampling);
553
+ const sample = referenceSampleImage(image, src_rect, sample_bounds, u, v, value.sampling);
553
554
  const index = (y * self.width + x) * 4;
554
555
  const dst = [4]u8{
555
556
  self.pixels[index + 0],
@@ -575,7 +576,7 @@ pub const ReferenceRenderSurface = struct {
575
576
  width: usize,
576
577
  };
577
578
 
578
- fn imageScalePanel(self: ReferenceRenderSurface, image: ReferenceImage, value: DrawImage, src_rect: geometry.RectF, dst_rect: geometry.RectF, pixel_rect: ReferencePixelRect) ?ImageScalePanel {
579
+ fn imageScalePanel(self: ReferenceRenderSurface, image: ReferenceImage, value: DrawImage, src_rect: geometry.RectF, sample_bounds: ReferenceImageSampleBounds, dst_rect: geometry.RectF, pixel_rect: ReferencePixelRect) ?ImageScalePanel {
579
580
  const memo = self.render_memo orelse return null;
580
581
  // Exact-arithmetic bounds: pixel offsets and phases must stay in
581
582
  // f32's exact-integer range for the phase-relative identity
@@ -630,7 +631,7 @@ pub const ReferenceRenderSurface = struct {
630
631
  var column: usize = 0;
631
632
  while (column < panel_width) : (column += 1) {
632
633
  const u = std.math.clamp(((@as(f32, @floatFromInt(column)) + 0.5) - phase_x) / dst_rect.width, 0, 1);
633
- const sample = referenceSampleImage(image, src_rect, u, v, value.sampling);
634
+ const sample = referenceSampleImage(image, src_rect, sample_bounds, u, v, value.sampling);
634
635
  const offset = (row * panel_width + column) * 4;
635
636
  buffer[offset] = sample[0];
636
637
  buffer[offset + 1] = sample[1];
@@ -1372,22 +1373,43 @@ const ReferencePremultipliedLinearColor = struct {
1372
1373
  a: f32 = 0,
1373
1374
  };
1374
1375
 
1375
- fn referenceSampleImage(image: ReferenceImage, src: geometry.RectF, u: f32, v: f32, sampling: ImageSampling) [4]u8 {
1376
+ fn referenceSampleImage(image: ReferenceImage, src: geometry.RectF, bounds: ReferenceImageSampleBounds, u: f32, v: f32, sampling: ImageSampling) [4]u8 {
1376
1377
  return switch (sampling) {
1377
- .nearest => referenceSampleImageNearest(image, src, u, v),
1378
- .linear => referenceSampleImageLinear(image, src, u, v),
1378
+ .nearest => referenceSampleImageNearest(image, src, bounds, u, v),
1379
+ .linear => referenceSampleImageLinear(image, src, bounds, u, v),
1379
1380
  };
1380
1381
  }
1381
1382
 
1382
- fn referenceSampleImageNearest(image: ReferenceImage, src: geometry.RectF, u: f32, v: f32) [4]u8 {
1383
+ const ReferenceImageSampleBounds = struct {
1384
+ min_x: i32,
1385
+ min_y: i32,
1386
+ max_x: i32,
1387
+ max_y: i32,
1388
+ };
1389
+
1390
+ /// Inclusive texel bounds touched by a clipped source rectangle. Native
1391
+ /// image APIs constrain filtering to their source portion; mirror that
1392
+ /// here so scaling a texture-atlas tile never samples an adjacent tile.
1393
+ fn referenceImageSampleBounds(image: ReferenceImage, src: geometry.RectF) ReferenceImageSampleBounds {
1394
+ const image_max_x: i32 = @intCast(image.width - 1);
1395
+ const image_max_y: i32 = @intCast(image.height - 1);
1396
+ return .{
1397
+ .min_x = clampI32(referenceFloor(src.minX()), 0, image_max_x),
1398
+ .min_y = clampI32(referenceFloor(src.minY()), 0, image_max_y),
1399
+ .max_x = clampI32(referenceCeil(src.maxX()) - 1, 0, image_max_x),
1400
+ .max_y = clampI32(referenceCeil(src.maxY()) - 1, 0, image_max_y),
1401
+ };
1402
+ }
1403
+
1404
+ fn referenceSampleImageNearest(image: ReferenceImage, src: geometry.RectF, bounds: ReferenceImageSampleBounds, u: f32, v: f32) [4]u8 {
1383
1405
  const sample_x_f = src.x + std.math.clamp(u, 0, 1) * src.width;
1384
1406
  const sample_y_f = src.y + std.math.clamp(v, 0, 1) * src.height;
1385
- const x = clampI32(referenceFloor(sample_x_f), 0, @intCast(image.width - 1));
1386
- const y = clampI32(referenceFloor(sample_y_f), 0, @intCast(image.height - 1));
1407
+ const x = clampI32(referenceFloor(sample_x_f), bounds.min_x, bounds.max_x);
1408
+ const y = clampI32(referenceFloor(sample_y_f), bounds.min_y, bounds.max_y);
1387
1409
  return referenceImagePixel(image, x, y);
1388
1410
  }
1389
1411
 
1390
- fn referenceSampleImageLinear(image: ReferenceImage, src: geometry.RectF, u: f32, v: f32) [4]u8 {
1412
+ fn referenceSampleImageLinear(image: ReferenceImage, src: geometry.RectF, bounds: ReferenceImageSampleBounds, u: f32, v: f32) [4]u8 {
1391
1413
  // Belt over the renderPass-level fill: direct sampler callers (unit
1392
1414
  // tests, future paths) stay correct. One predictable branch per
1393
1415
  // output pixel — noise next to the twelve pows the table replaces.
@@ -1396,10 +1418,10 @@ fn referenceSampleImageLinear(image: ReferenceImage, src: geometry.RectF, u: f32
1396
1418
  const sample_y_f = src.y + std.math.clamp(v, 0, 1) * src.height - 0.5;
1397
1419
  const x_floor = referenceFloor(sample_x_f);
1398
1420
  const y_floor = referenceFloor(sample_y_f);
1399
- const x0 = clampI32(x_floor, 0, @intCast(image.width - 1));
1400
- const y0 = clampI32(y_floor, 0, @intCast(image.height - 1));
1401
- const x1 = clampI32(x_floor + 1, 0, @intCast(image.width - 1));
1402
- const y1 = clampI32(y_floor + 1, 0, @intCast(image.height - 1));
1421
+ const x0 = clampI32(x_floor, bounds.min_x, bounds.max_x);
1422
+ const y0 = clampI32(y_floor, bounds.min_y, bounds.max_y);
1423
+ const x1 = clampI32(x_floor + 1, bounds.min_x, bounds.max_x);
1424
+ const y1 = clampI32(y_floor + 1, bounds.min_y, bounds.max_y);
1403
1425
  const tx = std.math.clamp(sample_x_f - @as(f32, @floatFromInt(x_floor)), 0, 1);
1404
1426
  const ty = std.math.clamp(sample_y_f - @as(f32, @floatFromInt(y_floor)), 0, 1);
1405
1427
 
@@ -1731,6 +1731,69 @@ test "reference renderer bilinear-filters scaled images" {
1731
1731
  try expectPixelRgba8(.{ 255, 255, 255, 255 }, surface, 3, 3);
1732
1732
  }
1733
1733
 
1734
+ test "reference renderer keeps linear atlas crops inside their source rectangle" {
1735
+ const commands = [_]CanvasCommand{.{ .draw_image = .{
1736
+ .id = 1,
1737
+ .image_id = 42,
1738
+ .src = geometry.RectF.init(1, 1, 2, 2),
1739
+ .dst = geometry.RectF.init(0, 0, 4, 4),
1740
+ } }};
1741
+
1742
+ // A green 2x2 tile surrounded by red atlas neighbors. Scaling the
1743
+ // crop 2x must stay green through its edge pixels; full-image clamp
1744
+ // would blend red into every side through the bilinear taps.
1745
+ var image_pixels: [4 * 4 * 4]u8 = undefined;
1746
+ for (0..16) |index| {
1747
+ image_pixels[index * 4 + 0] = 255;
1748
+ image_pixels[index * 4 + 1] = 0;
1749
+ image_pixels[index * 4 + 2] = 0;
1750
+ image_pixels[index * 4 + 3] = 255;
1751
+ }
1752
+ for (1..3) |y| {
1753
+ for (1..3) |x| {
1754
+ const index = (y * 4 + x) * 4;
1755
+ image_pixels[index + 0] = 0;
1756
+ image_pixels[index + 1] = 255;
1757
+ image_pixels[index + 2] = 0;
1758
+ }
1759
+ }
1760
+ const images = [_]ReferenceImage{.{
1761
+ .id = 42,
1762
+ .width = 4,
1763
+ .height = 4,
1764
+ .pixels = &image_pixels,
1765
+ }};
1766
+
1767
+ var render_commands: [1]RenderCommand = undefined;
1768
+ var render_batches: [1]RenderBatch = undefined;
1769
+ var resources: [1]RenderResource = undefined;
1770
+ var resource_cache_entries: [1]RenderResourceCacheEntry = undefined;
1771
+ var resource_cache_actions: [1]RenderResourceCacheAction = undefined;
1772
+ var glyphs: [0]GlyphAtlasEntry = .{};
1773
+ var changes: [0]DiffChange = .{};
1774
+ const frame = try (DisplayList{ .commands = &commands }).framePlan(null, .{
1775
+ .surface_size = geometry.SizeF.init(4, 4),
1776
+ }, .{
1777
+ .render_commands = &render_commands,
1778
+ .render_batches = &render_batches,
1779
+ .resources = &resources,
1780
+ .resource_cache_entries = &resource_cache_entries,
1781
+ .resource_cache_actions = &resource_cache_actions,
1782
+ .glyph_atlas_entries = &glyphs,
1783
+ .changes = &changes,
1784
+ });
1785
+
1786
+ var pixels: [4 * 4 * 4]u8 = undefined;
1787
+ const surface = (try ReferenceRenderSurface.init(4, 4, &pixels)).withImages(&images);
1788
+ try surface.renderPass(frame.renderPass(), Color.rgb8(0, 0, 0));
1789
+
1790
+ for (0..4) |y| {
1791
+ for (0..4) |x| {
1792
+ try expectPixelRgba8(.{ 0, 255, 0, 255 }, surface, x, y);
1793
+ }
1794
+ }
1795
+ }
1796
+
1734
1797
  test "reference renderer nearest-filters scaled images" {
1735
1798
  const commands = [_]CanvasCommand{.{ .draw_image = .{
1736
1799
  .id = 1,
@@ -579,6 +579,14 @@ pub fn Ui(comptime Msg: type) type {
579
579
  /// shape, one bit of the id space apart (see
580
580
  /// `canvas.media_surface_image_id_bit`).
581
581
  image: canvas.ImageId = 0,
582
+ /// Optional source rectangle in image pixel coordinates for
583
+ /// image-bearing widgets. Null draws the whole registered
584
+ /// image; a rectangle draws only that sub-region, clipped to
585
+ /// the registered image bounds — the texture-atlas path.
586
+ /// Crops use nearest sampling so filtering cannot bleed an
587
+ /// adjacent atlas region. The destination remains the
588
+ /// widget's resolved frame.
589
+ image_src: ?geometry.RectF = null,
582
590
  /// Vector icon name drawn inside icon-bearing controls
583
591
  /// (`button`, `toggle_button`, `icon_button`, `list_item`,
584
592
  /// `menu_item`): a built-in registry name
@@ -3759,6 +3767,7 @@ pub fn Ui(comptime Msg: type) type {
3759
3767
  .autofocus = options.autofocus,
3760
3768
  .submit_on_enter = options.submit_on_enter,
3761
3769
  .image_id = options.image,
3770
+ .image_src = options.image_src,
3762
3771
  .value = options.value,
3763
3772
  .value_x = options.value_x,
3764
3773
  .tree_level = options.tree_level,
@@ -1269,6 +1269,14 @@ pub const tooltip_delay_dependent_attr_message = "tooltip-delay needs anchor on
1269
1269
 
1270
1270
  pub const image_binding_message = "image takes one {binding} to a u64 ImageId the app registered at runtime (Cmd.imageLoad, fx.loadImage, fx.registerImageBytes) - runtime image ids are model data, not markup literals; 0 renders nothing (an avatar falls back to its initials)";
1271
1271
  pub const image_binding_element_message = "image is only supported on avatar and image - the remaining image-bearing widget (icon-button) stays a Zig view (ElementOptions.image)";
1272
+ pub const image_source_element_message = "source-x, source-y, source-width, and source-height are only supported on avatar and image - they crop a runtime-registered image in decoded-image pixel coordinates";
1273
+ pub const image_source_binding_message = "source-x, source-y, source-width, and source-height require the element's image binding - without a registered image the source rectangle is inert";
1274
+ pub const image_source_complete_message = "source-x, source-y, source-width, and source-height must be declared together - they form one source rectangle in decoded-image pixel coordinates";
1275
+ pub const image_source_attr_names = [_][]const u8{ "source-x", "source-y", "source-width", "source-height" };
1276
+
1277
+ pub fn imageSourceAttrName(name: []const u8) bool {
1278
+ return nameInList(name, &image_source_attr_names);
1279
+ }
1272
1280
  pub const image_missing_image_message = "image requires image={binding} naming the u64 ImageId the app registered at runtime - without one the leaf can never draw anything (dead markup, same policy as icon without name)";
1273
1281
  pub const image_children_message = "image is a leaf - it takes no children";
1274
1282
 
@@ -3353,6 +3361,27 @@ fn validateNode(document: MarkupDocument, node: MarkupNode, parent_element: ?[]c
3353
3361
  }
3354
3362
  continue;
3355
3363
  }
3364
+ if (imageSourceAttrName(attribute.name)) {
3365
+ // A registered-image crop is one atomic rectangle in
3366
+ // decoded-image pixel coordinates. Partial declarations
3367
+ // and crops without an image would otherwise become
3368
+ // silently inert data.
3369
+ if (!std.mem.eql(u8, node.name, "avatar") and !std.mem.eql(u8, node.name, "image")) {
3370
+ return attrError(node, attribute, image_source_element_message);
3371
+ }
3372
+ if (node.attr("image") == null) {
3373
+ return attrError(node, attribute, image_source_binding_message);
3374
+ }
3375
+ for (image_source_attr_names) |name| {
3376
+ if (node.attr(name) == null) {
3377
+ return attrError(node, attribute, image_source_complete_message);
3378
+ }
3379
+ }
3380
+ if (attrExpressionError(attribute.value, invalid_expression_message)) |message| {
3381
+ return attrError(node, attribute, message);
3382
+ }
3383
+ continue;
3384
+ }
3356
3385
  if (std.mem.eql(u8, attribute.name, "surface")) {
3357
3386
  // The media-surface producer rendezvous, media-surface
3358
3387
  // scoped: surface ids are model data a producer
@@ -1820,6 +1820,7 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
1820
1820
  // ---------------------------------------------------- attributes
1821
1821
 
1822
1822
  fn applyAttrs(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype, options: *Ui.ElementOptions) void {
1823
+ applyImageSourceAttrs(node, entries, ui, model, scope, options);
1823
1824
  inline for (0..node.attrs.len) |attr_index| {
1824
1825
  const attribute = comptime node.attrs[attr_index];
1825
1826
  if (comptime std.mem.eql(u8, attribute.name, "kind")) {
@@ -1840,6 +1841,8 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
1840
1841
  };
1841
1842
  } else if (comptime std.mem.eql(u8, attribute.name, "image")) {
1842
1843
  applyImageAttr(node, attribute.value, entries, ui, model, scope, options);
1844
+ } else if (comptime markup.imageSourceAttrName(attribute.name)) {
1845
+ // Consumed atomically by applyImageSourceAttrs above.
1843
1846
  } else if (comptime std.mem.eql(u8, attribute.name, "surface")) {
1844
1847
  applySurfaceAttr(node, attribute.value, entries, ui, model, scope, options);
1845
1848
  } else if (comptime std.mem.eql(u8, attribute.name, "pty")) {
@@ -1943,6 +1946,35 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
1943
1946
  };
1944
1947
  }
1945
1948
 
1949
+ /// Comptime mirror of the interpreter's atomic source-rectangle
1950
+ /// construction. Source-known shape mistakes are compile errors;
1951
+ /// bound numeric values evaluate through the shared expression
1952
+ /// path at runtime.
1953
+ fn applyImageSourceAttrs(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype, options: *Ui.ElementOptions) void {
1954
+ const has_any = comptime blk: {
1955
+ for (markup.image_source_attr_names) |name| {
1956
+ if (node.attr(name) != null) break :blk true;
1957
+ }
1958
+ break :blk false;
1959
+ };
1960
+ if (comptime !has_any) return;
1961
+ comptime {
1962
+ if (!std.mem.eql(u8, node.name, "avatar") and !std.mem.eql(u8, node.name, "image")) {
1963
+ fail(node, markup.image_source_element_message);
1964
+ }
1965
+ if (node.attr("image") == null) fail(node, markup.image_source_binding_message);
1966
+ for (markup.image_source_attr_names) |name| {
1967
+ if (node.attr(name) == null) fail(node, markup.image_source_complete_message);
1968
+ }
1969
+ }
1970
+ options.image_src = .{
1971
+ .x = floatAttr(node, entries, comptime node.attr("source-x").?, ui, model, scope),
1972
+ .y = floatAttr(node, entries, comptime node.attr("source-y").?, ui, model, scope),
1973
+ .width = floatAttr(node, entries, comptime node.attr("source-width").?, ui, model, scope),
1974
+ .height = floatAttr(node, entries, comptime node.attr("source-height").?, ui, model, scope),
1975
+ };
1976
+ }
1977
+
1946
1978
  /// Comptime mirror of the interpreter's `applySurfaceAttr`:
1947
1979
  /// `surface="{binding}"` on media-surface resolves to the
1948
1980
  /// model-owned u64 surface id a producer targets —
@@ -1406,12 +1406,14 @@ test "compiled image leaf binding matches the interpreter and the hand-written v
1406
1406
  try expectSameTree(fixture.ImageLeafMsg, hand, interpreted);
1407
1407
  try expectSameTree(fixture.ImageLeafMsg, hand, compiled);
1408
1408
 
1409
- // The field binding and the fn binding both resolve to the
1410
- // widget's image id at comptime-unrolled access.
1409
+ // The field binding, source-coordinate bindings, and id fn all
1410
+ // resolve through comptime-unrolled access.
1411
1411
  const cover = compiled.root.children[0];
1412
1412
  try testing.expectEqual(canvas.WidgetKind.image, cover.kind);
1413
1413
  try testing.expectEqual(@as(canvas.ImageId, 42), cover.image_id);
1414
+ try testing.expectEqualDeep(@as(?geometry.RectF, geometry.RectF.init(4, 8, 32, 24)), cover.image_src);
1414
1415
  try testing.expectEqual(@as(canvas.ImageId, 43), compiled.root.children[1].image_id);
1416
+ try testing.expectEqual(@as(?geometry.RectF, null), compiled.root.children[1].image_src);
1415
1417
 
1416
1418
  // 0 draws nothing in both engines — the not-loaded-yet state.
1417
1419
  const empty_model = fixture.ImageLeafModel{};
@@ -1248,6 +1248,13 @@ const Checker = struct {
1248
1248
  try self.requireAttrKind(node, attribute, resolved.kind, &.{.integer}, markup.image_binding_message);
1249
1249
  continue;
1250
1250
  }
1251
+ if (markup.imageSourceAttrName(attribute.name)) {
1252
+ // Structural validation owns the atomic/scoped rectangle
1253
+ // rules; the model-aware pass verifies every coordinate
1254
+ // binding is numeric and marks it used.
1255
+ try self.checkClassAttr(node, attribute, .number);
1256
+ continue;
1257
+ }
1251
1258
  if (std.mem.eql(u8, attribute.name, "surface")) {
1252
1259
  // Media-surface ids are model integers (engine parity —
1253
1260
  // the runtime-image-id shape exactly).
@@ -795,6 +795,27 @@ test "the image leaf's image binding checks as a model integer" {
795
795
  try testing.expectEqualStrings(markup.image_binding_message, message);
796
796
  }
797
797
 
798
+ test "registered-image source rectangle bindings check as numbers" {
799
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
800
+ defer arena_state.deinit();
801
+ const arena = arena_state.allocator();
802
+
803
+ const good = try parseFixture(arena,
804
+ \\<column>
805
+ \\ <image image="{count}" source-x="{ratio}" source-y="{count}" source-width="32" source-height="16" label="Atlas tile" />
806
+ \\</column>
807
+ );
808
+ try testing.expectEqual(null, try contractMessage(arena, good, null));
809
+
810
+ const wrong = try parseFixture(arena,
811
+ \\<column>
812
+ \\ <image image="{count}" source-x="{name}" source-y="0" source-width="32" source-height="16" label="Atlas tile" />
813
+ \\</column>
814
+ );
815
+ const message = (try contractMessage(arena, wrong, null)).?;
816
+ try testing.expect(std.mem.startsWith(u8, message, "expected a number"));
817
+ }
818
+
798
819
  test "app: icon references check against the contract's registered icon list" {
799
820
  var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
800
821
  defer arena_state.deinit();
@@ -696,6 +696,7 @@ test "the image attribute validates as one binding on avatar and image" {
696
696
  const valid = [_][]const u8{
697
697
  "<row>\n <avatar image=\"{user_image}\">CT</avatar>\n</row>",
698
698
  "<row>\n <image image=\"{cover}\" width=\"120\" height=\"80\" label=\"Cover art\" />\n</row>",
699
+ "<row>\n <image image=\"{atlas}\" source-x=\"0\" source-y=\"32\" source-width=\"16\" source-height=\"16\" label=\"Atlas tile\" />\n</row>",
699
700
  };
700
701
  for (valid) |source| {
701
702
  var parser = markup.Parser.init(arena, source);
@@ -711,6 +712,11 @@ test "the image attribute validates as one binding on avatar and image" {
711
712
  // attribute would be silently inert.
712
713
  .{ .source = "<row>\n <badge image=\"{user_image}\">3</badge>\n</row>", .message = markup.image_binding_element_message },
713
714
  .{ .source = "<column>\n <panel image=\"{user_image}\" />\n</column>", .message = markup.image_binding_element_message },
715
+ // Source rectangles are image/avatar-only, require an image, and
716
+ // are atomic so a missing coordinate never defaults silently.
717
+ .{ .source = "<row>\n <badge source-x=\"0\" source-y=\"0\" source-width=\"16\" source-height=\"16\">3</badge>\n</row>", .message = markup.image_source_element_message },
718
+ .{ .source = "<row>\n <avatar source-x=\"0\" source-y=\"0\" source-width=\"16\" source-height=\"16\">CT</avatar>\n</row>", .message = markup.image_source_binding_message },
719
+ .{ .source = "<row>\n <image image=\"{atlas}\" source-x=\"0\" source-y=\"0\" source-width=\"16\" label=\"Tile\" />\n</row>", .message = markup.image_source_complete_message },
714
720
  // ...and required on the leaf: an unbound image is statically
715
721
  // dead markup (avatar keeps its initials fallback instead).
716
722
  .{ .source = "<row>\n <image label=\"Art\" />\n</row>", .message = markup.image_missing_image_message },