@native-sdk/cli 0.7.0 → 0.7.2

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 (81) hide show
  1. package/build/app.zig +8 -0
  2. package/build.zig +114 -103
  3. package/native-sdk.d.ts +6 -3
  4. package/package.json +9 -9
  5. package/packages/core/package-lock.json +2 -2
  6. package/packages/core/package.json +1 -1
  7. package/packages/core/sdk/text.ts +25 -14
  8. package/packages/core/src/checker.ts +39 -0
  9. package/packages/core/src/emitter.ts +125 -1
  10. package/skill-data/native-ui/SKILL.md +11 -7
  11. package/src/platform/macos/appkit_host.m +76 -4
  12. package/src/platform/macos/cef_host.mm +24 -2
  13. package/src/platform/macos/root.zig +13 -0
  14. package/src/platform/types.zig +4 -0
  15. package/src/platform/windows/gpu_surface_renderer.cpp +2280 -0
  16. package/src/platform/windows/gpu_surface_renderer.h +86 -0
  17. package/src/platform/windows/root.zig +298 -20
  18. package/src/platform/windows/webview2_host.cpp +343 -33
  19. package/src/primitives/canvas/code.zig +501 -33
  20. package/src/primitives/canvas/code_tests.zig +1029 -6
  21. package/src/primitives/canvas/events.zig +111 -1
  22. package/src/primitives/canvas/frame.zig +45 -1
  23. package/src/primitives/canvas/gpu.zig +6 -0
  24. package/src/primitives/canvas/markdown_tests.zig +17 -18
  25. package/src/primitives/canvas/render_tests.zig +93 -0
  26. package/src/primitives/canvas/root.zig +6 -0
  27. package/src/primitives/canvas/serialization.zig +150 -21
  28. package/src/primitives/canvas/text.zig +6 -0
  29. package/src/primitives/canvas/themes/geist.zig +51 -5
  30. package/src/primitives/canvas/tokens.zig +33 -4
  31. package/src/primitives/canvas/ui.zig +160 -36
  32. package/src/primitives/canvas/ui_markup.zig +60 -9
  33. package/src/primitives/canvas/ui_markup_compiled.zig +41 -0
  34. package/src/primitives/canvas/ui_markup_compiled_tests.zig +12 -0
  35. package/src/primitives/canvas/ui_markup_contract.zig +17 -1
  36. package/src/primitives/canvas/ui_markup_view.zig +44 -0
  37. package/src/primitives/canvas/ui_markup_view_tests.zig +80 -6
  38. package/src/primitives/canvas/ui_schema.zig +18 -0
  39. package/src/primitives/canvas/ui_schema_tests.zig +10 -7
  40. package/src/primitives/canvas/ui_tests.zig +40 -0
  41. package/src/primitives/canvas/widget_builtin_tests.zig +513 -13
  42. package/src/primitives/canvas/widget_invalidation.zig +12 -1
  43. package/src/primitives/canvas/widget_layout.zig +260 -21
  44. package/src/primitives/canvas/widget_metrics.zig +47 -6
  45. package/src/primitives/canvas/widget_render.zig +849 -78
  46. package/src/primitives/canvas/widget_render_controls.zig +83 -25
  47. package/src/primitives/canvas/widget_render_style.zig +52 -8
  48. package/src/primitives/canvas/widget_runtime.zig +2 -0
  49. package/src/primitives/canvas/widget_text_input.zig +167 -8
  50. package/src/primitives/canvas/widget_tree.zig +6 -5
  51. package/src/primitives/canvas/widgets.zig +78 -2
  52. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  53. package/src/runtime/canvas_frame.zig +60 -7
  54. package/src/runtime/canvas_frame_invalidation_tests.zig +80 -0
  55. package/src/runtime/canvas_frame_packet_tests.zig +83 -9
  56. package/src/runtime/canvas_frame_patch_tests.zig +16 -0
  57. package/src/runtime/canvas_images.zig +8 -2
  58. package/src/runtime/canvas_limits.zig +13 -6
  59. package/src/runtime/canvas_widget_clipboard_tests.zig +8 -5
  60. package/src/runtime/canvas_widget_context_menu.zig +7 -2
  61. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  62. package/src/runtime/canvas_widget_events.zig +51 -20
  63. package/src/runtime/canvas_widget_runtime.zig +108 -4
  64. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  65. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  66. package/src/runtime/canvas_widget_state.zig +27 -0
  67. package/src/runtime/canvas_widget_text_tests.zig +326 -0
  68. package/src/runtime/core.zig +15 -5
  69. package/src/runtime/effects.zig +6 -0
  70. package/src/runtime/effects_tests.zig +30 -0
  71. package/src/runtime/gpu_surface_events.zig +50 -19
  72. package/src/runtime/media_surface_tests.zig +1 -1
  73. package/src/runtime/runtime_event_tests.zig +74 -0
  74. package/src/runtime/ui_app.zig +10 -8
  75. package/src/runtime/ui_app_tests.zig +67 -0
  76. package/src/runtime/view.zig +60 -16
  77. package/src/runtime/view_canvas.zig +15 -0
  78. package/src/runtime/view_widget_scroll.zig +48 -18
  79. package/src/runtime/view_widget_text.zig +59 -16
  80. package/src/runtime/window_view_runtime.zig +10 -0
  81. package/src/tooling/templates.zig +9 -6
@@ -3,6 +3,7 @@ const geometry = @import("geometry");
3
3
  const canvas = @import("root.zig");
4
4
  const text_model = @import("text.zig");
5
5
  const text_spans_model = @import("text_spans.zig");
6
+ const code_model = @import("code.zig");
6
7
  const token_model = @import("tokens.zig");
7
8
  const chart_model = @import("chart.zig");
8
9
  const terminal_grid_model = @import("terminal_grid.zig");
@@ -263,6 +264,13 @@ pub const WidgetState = struct {
263
264
  invalid: bool = false,
264
265
  };
265
266
 
267
+ /// Two 128-line masks for code-only diff presentation. `Widget` packs them
268
+ /// into fields dormant on decorated code so ordinary widgets do not grow.
269
+ pub const CodeDiffLines = struct {
270
+ added: u128,
271
+ removed: u128,
272
+ };
273
+
266
274
  pub const WidgetRenderState = struct {
267
275
  /// Whether the app is active and this widget tree's window is key.
268
276
  /// Runtime focus ids stay retained while false so focus-visible
@@ -358,6 +366,11 @@ pub const WidgetAnchor = struct {
358
366
 
359
367
  pub const WidgetLayoutStyle = struct {
360
368
  padding: geometry.InsetsF = .{},
369
+ /// True when the widget-kind defaults supplied `padding`. Layout
370
+ /// resolution uses this provenance to translate a house default onto
371
+ /// another theme register without mistaking an equal author value for
372
+ /// the default. Author-created styles leave this false.
373
+ padding_is_kind_default: bool = false,
361
374
  gap: f32 = 0,
362
375
  grow: f32 = 0,
363
376
  main_alignment: WidgetMainAlignment = .start,
@@ -869,13 +882,40 @@ pub const Widget = struct {
869
882
  /// retained text, so selection/copy remains the exact source bytes.
870
883
  /// Stamped only by `Ui.code`; there is no generic builder/markup
871
884
  /// channel for turning arbitrary paragraphs into numbered code.
885
+ /// Bit 7 is an internal diff-metadata tag; `codeLineNumberDigits`
886
+ /// exposes only the authored digit count.
872
887
  code_line_number_digits: u8 = 0,
888
+ /// Optional one-based logical depth for a flat sequence of tree rows.
889
+ /// Zero keeps the structural nesting contract. A nonzero value lets a
890
+ /// `<for>` render visible rows as siblings while Left/Right still find
891
+ /// the logical parent and first child from their declared levels.
892
+ tree_level: u16 = 0,
893
+ /// Internal marker for an editable `Ui.code` surface. The widget keeps
894
+ /// the `.textarea` kind so the existing editor, IME, clipboard, undo,
895
+ /// selection, and accessibility paths all apply; this bit only swaps
896
+ /// the textarea's visual/geometry policy to bare monospace highlighted
897
+ /// code (no control fill, border, radius, focus ring, or inset).
898
+ code_editor: bool = false,
899
+ /// Syntax grammar for an editable `Ui.code` surface. Editable code
900
+ /// retains one plain source span and tokenizes only visible logical
901
+ /// lines during paint, so large documents do not consume the view's
902
+ /// bounded span pool on offscreen syntax runs.
903
+ code_language: code_model.Language = .plain,
904
+ /// Runtime-owned longest-line measurement for an editable no-wrap code
905
+ /// surface. The signature keeps the value honest across font/appearance
906
+ /// changes; source edits invalidate it before caret and scroll geometry
907
+ /// read it. These fields are renderer state, not builder options.
908
+ code_content_width: f32 = 0,
909
+ code_content_width_generation: u64 = 0,
910
+ code_content_width_font_id: u64 = 0,
911
+ code_content_width_size_bits: u32 = 0,
873
912
  /// Nonzero on bounded paragraph chunks that together present one
874
913
  /// selectable source-code document. The group id is the structural id
875
914
  /// of their internal parent; offsets order each chunk's exact source
876
- /// bytes without duplicating the full source in retained storage.
915
+ /// bytes without duplicating the full source in retained storage. Fixed
916
+ /// width keeps the code-diff metadata union intact on 32-bit targets.
877
917
  static_text_group_id: ObjectId = 0,
878
- static_text_group_offset: usize = 0,
918
+ static_text_group_offset: u64 = 0,
879
919
  /// What a single-line text run does with content that does not fit
880
920
  /// its frame (`ElementOptions.overflow` / markup `overflow=` on
881
921
  /// text leaves): `.ellipsis` (default) elides the tail behind a
@@ -1037,6 +1077,41 @@ pub const Widget = struct {
1037
1077
  /// serialization, or equality decisions.
1038
1078
  group_segment: WidgetGroupSegment = .none,
1039
1079
  children: []const Widget = &.{},
1080
+
1081
+ pub fn codeLineNumberDigits(self: Widget) u8 {
1082
+ return self.code_line_number_digits & 0x7f;
1083
+ }
1084
+
1085
+ pub fn hasCodeDiff(self: Widget) bool {
1086
+ return self.code_line_number_digits & 0x80 != 0;
1087
+ }
1088
+
1089
+ /// Code-only metadata occupies the otherwise unused no-wrap width-cache
1090
+ /// words plus the ungrouped static-text offset. The high bit above tags
1091
+ /// the union. This keeps Widget's common footprint unchanged; a diff
1092
+ /// editor simply measures its longest line on demand instead of caching.
1093
+ pub fn codeDiffLines(self: Widget) ?CodeDiffLines {
1094
+ if (!self.hasCodeDiff()) return null;
1095
+ const removed_low = @as(u64, @as(u32, @bitCast(self.code_content_width))) |
1096
+ (@as(u64, self.code_content_width_size_bits) << 32);
1097
+ return .{
1098
+ .added = @as(u128, self.code_content_width_generation) |
1099
+ (@as(u128, self.code_content_width_font_id) << 64),
1100
+ .removed = @as(u128, removed_low) |
1101
+ (@as(u128, self.static_text_group_offset) << 64),
1102
+ };
1103
+ }
1104
+
1105
+ pub fn setCodeDiffLines(self: *Widget, lines: CodeDiffLines) void {
1106
+ std.debug.assert(self.static_text_group_id == 0);
1107
+ self.code_line_number_digits |= 0x80;
1108
+ self.code_content_width_generation = @truncate(lines.added);
1109
+ self.code_content_width_font_id = @truncate(lines.added >> 64);
1110
+ const removed_low: u64 = @truncate(lines.removed);
1111
+ self.code_content_width = @bitCast(@as(u32, @truncate(removed_low)));
1112
+ self.code_content_width_size_bits = @truncate(removed_low >> 32);
1113
+ self.static_text_group_offset = @truncate(lines.removed >> 64);
1114
+ }
1040
1115
  };
1041
1116
 
1042
1117
  pub const BuiltinComponentOptions = struct {
@@ -1483,6 +1558,7 @@ fn mergeLayoutDefaults(explicit: WidgetLayoutStyle, defaults: WidgetLayoutStyle)
1483
1558
  explicit.padding.bottom == 0 and explicit.padding.left == 0)
1484
1559
  {
1485
1560
  merged.padding = defaults.padding;
1561
+ merged.padding_is_kind_default = true;
1486
1562
  }
1487
1563
  if (explicit.gap == 0) merged.gap = defaults.gap;
1488
1564
  if (explicit.cross_alignment == .stretch) merged.cross_alignment = defaults.cross_alignment;
@@ -67,8 +67,9 @@ fn stressUpdate(model: *StressModel, msg: StressMsg) void {
67
67
  }
68
68
 
69
69
  // Static payload storage: tests are single-threaded and the view only
70
- // reads these.
71
- var text_payload: [96 * 1024]u8 = undefined;
70
+ // reads these. Size it from the live retained-text edge so raising that
71
+ // budget cannot make this test fixture fail before the runtime does.
72
+ var text_payload: [canvas_limits.max_canvas_widget_text_bytes_per_view]u8 = undefined;
72
73
  var chart_values: [256]f32 = undefined;
73
74
  var chart_low: [256]f32 = undefined;
74
75
 
@@ -33,6 +33,16 @@ pub const canvasRenderAnimationActive = canvas_frame_helpers.canvasRenderAnimati
33
33
  pub const platformCanvasFrameProfileRisk = canvas_frame_helpers.platformCanvasFrameProfileRisk;
34
34
  pub const gpuSurfaceFrameEventFromGpuFrame = canvas_frame_helpers.gpuSurfaceFrameEventFromGpuFrame;
35
35
 
36
+ /// Whether presentation for this view should use the GPU packet path. A
37
+ /// missing view stays eligible so the presentation entry point can report
38
+ /// its normal validation error instead of masking it as a backend choice.
39
+ pub fn canvasGpuPacketPresentationRequested(runtime: anytype, window_id: platform.WindowId, label: []const u8) bool {
40
+ return if (runtimeFindViewIndex(runtime, window_id, label)) |index|
41
+ runtime.views[index].gpu_requested_backend != .software
42
+ else
43
+ true;
44
+ }
45
+
36
46
  const runtime_api = @import("api.zig");
37
47
  const runtime_clock = @import("clock.zig");
38
48
  const validation = @import("validation.zig");
@@ -254,7 +264,16 @@ pub fn RuntimeCanvasFrames(comptime Runtime: type) type {
254
264
  packet_json_buffer: []u8,
255
265
  packet_scale: ?f32,
256
266
  ) anyerror!canvas.CanvasGpuPacket {
257
- var canvas_frame = try self.nextCanvasFrame(window_id, label, options, storage);
267
+ // An explicit software request is a presentation policy, not a
268
+ // host refusal. Stop before planning or encoding a packet (and
269
+ // before uploading any packet image resources); callers that
270
+ // offer a pixel fallback handle UnsupportedService as usual.
271
+ try validateRuntimeViewParent(self, window_id);
272
+ try validateViewLabel(label);
273
+ const view_index = runtimeFindViewIndex(self, window_id, label) orelse return error.ViewNotFound;
274
+ if (self.views[view_index].kind != .gpu_surface) return error.InvalidViewOptions;
275
+ if (self.views[view_index].gpu_requested_backend == .software) return error.UnsupportedService;
276
+ var canvas_frame = try planCanvasFrameForView(self, view_index, options, storage, true);
258
277
  recordCanvasClearColor(self, window_id, label, clear_color);
259
278
  const presentation_scale = normalizedCanvasPresentationScale(packet_scale, canvas_frame.scale);
260
279
  // Widen BEFORE the packet build so the scissor-culled
@@ -318,9 +337,11 @@ pub fn RuntimeCanvasFrames(comptime Runtime: type) type {
318
337
  widenCanvasFrameDirtyForPresentationScale(&canvas_frame, presentation_scale);
319
338
 
320
339
  const services = self.options.platform.services;
321
- const packet_service_available = services.present_gpu_surface_packet_fn != null or
322
- services.present_gpu_surface_packet_binary_fn != null;
323
- if (gpu_commands.len > 0 and packet_json_buffer.len > 0) {
340
+ const packet_requested = canvasGpuPacketPresentationRequested(self, window_id, label);
341
+ const packet_service_available = packet_requested and
342
+ (services.present_gpu_surface_packet_fn != null or
343
+ services.present_gpu_surface_packet_binary_fn != null);
344
+ if (packet_requested and gpu_commands.len > 0 and packet_json_buffer.len > 0) {
324
345
  if (packet_service_available) {
325
346
  var packet = try canvas_frame.gpuPacket(gpu_commands);
326
347
  packet.scale = presentation_scale;
@@ -1029,17 +1050,17 @@ pub fn RuntimeCanvasFrames(comptime Runtime: type) type {
1029
1050
  storage.text_layout_cache_actions,
1030
1051
  );
1031
1052
 
1032
- const full_repaint = frame_options.full_repaint or
1053
+ var full_repaint = frame_options.full_repaint or
1033
1054
  !self.views[index].presented_canvas_valid or
1034
1055
  canvas_surface_changed or
1035
1056
  (canvas_changed and (self.views[index].presented_canvas_has_unkeyed or self.views[index].currentCanvasHasUnkeyed()));
1036
- const changes = if (full_repaint)
1057
+ var changes: []const canvas.DiffChange = if (full_repaint)
1037
1058
  storage.changes[0..0]
1038
1059
  else
1039
1060
  try self.views[index].diffPresentedCanvasSummary(storage.changes);
1040
1061
  var dirty_rects: [canvas.max_canvas_frame_dirty_rects]geometry.RectF = undefined;
1041
1062
  var dirty_rect_count: usize = 0;
1042
- const dirty_bounds = if (full_repaint)
1063
+ var dirty_bounds: ?geometry.RectF = if (full_repaint)
1043
1064
  canvasFullRepaintBounds(frame_options.surface_size, render_plan.bounds)
1044
1065
  else dirty: {
1045
1066
  // Every incremental dirty rect leaves here through
@@ -1109,6 +1130,23 @@ pub fn RuntimeCanvasFrames(comptime Runtime: type) type {
1109
1130
  }
1110
1131
  break :dirty bleedAlignedCanvasDirtyBounds(unionRects(canvasDirtyBoundsFromChanges(changes), overrides_dirty), frame_options.scale, 1, frame_options.surface_size);
1111
1132
  };
1133
+ if (!full_repaint and incrementalCanvasDamageIntersectsBackdropBlur(
1134
+ render_plan.commands,
1135
+ dirty_bounds,
1136
+ dirty_rects[0..dirty_rect_count],
1137
+ )) {
1138
+ // A blur's apron must be reconstructed from the scene as
1139
+ // it existed before that command. Retained pixels beyond
1140
+ // an incremental scissor are already fully composited,
1141
+ // so widening only the output rect would still sample
1142
+ // stale blur/later-command pixels at its edge. Replay the
1143
+ // whole ordered list when damage reaches the blur's read
1144
+ // footprint; damage elsewhere remains incremental.
1145
+ full_repaint = true;
1146
+ changes = storage.changes[0..0];
1147
+ dirty_bounds = canvasFullRepaintBounds(frame_options.surface_size, render_plan.bounds);
1148
+ dirty_rect_count = 0;
1149
+ }
1112
1150
 
1113
1151
  const canvas_frame = canvas.CanvasFrame{
1114
1152
  .frame_index = frame_options.frame_index,
@@ -1271,6 +1309,21 @@ pub fn RuntimeCanvasFrames(comptime Runtime: type) type {
1271
1309
  };
1272
1310
  }
1273
1311
 
1312
+ /// Refined patch damage is a set, not its bounding box: two distant edits
1313
+ /// can straddle a blur without either edit reaching the blur's read apron.
1314
+ /// When refinement is unavailable, retain the conservative union check.
1315
+ fn incrementalCanvasDamageIntersectsBackdropBlur(
1316
+ commands: []const canvas.RenderCommand,
1317
+ dirty_bounds: ?geometry.RectF,
1318
+ dirty_rects: []const geometry.RectF,
1319
+ ) bool {
1320
+ if (dirty_rects.len == 0) return canvas.incrementalDamageIntersectsBackdropBlur(commands, dirty_bounds);
1321
+ for (dirty_rects) |dirty_rect| {
1322
+ if (canvas.incrementalDamageIntersectsBackdropBlur(commands, dirty_rect)) return true;
1323
+ }
1324
+ return false;
1325
+ }
1326
+
1274
1327
  /// Rework a planned frame's incremental damage for the present's
1275
1328
  /// EFFECTIVE scale. Two obligations when the scales differ:
1276
1329
  /// - A COARSER presentation scale carries a larger device pixel than
@@ -431,6 +431,86 @@ test "runtime invalidates canvas display list dirty regions" {
431
431
  try std.testing.expectEqual(@as(usize, 0), harness.runtime.pendingDirtyRegions().len);
432
432
  }
433
433
 
434
+ test "backdrop blur promotes intersecting damage to a full repaint" {
435
+ const TestApp = struct {
436
+ fn app(self: *@This()) App {
437
+ return .{ .context = self, .name = "gpu-canvas-blur-damage", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
438
+ }
439
+ };
440
+
441
+ const surface = geometry.SizeF.init(100, 40);
442
+ const harness = try TestHarness().create(std.testing.allocator, .{});
443
+ defer harness.destroy(std.testing.allocator);
444
+ harness.null_platform.gpu_surfaces = true;
445
+ harness.runtime.options.pixel_present_retained_baseline = true;
446
+ var app_state: TestApp = .{};
447
+ try harness.start(app_state.app());
448
+
449
+ _ = try harness.runtime.createView(.{
450
+ .window_id = 1,
451
+ .label = "canvas",
452
+ .kind = .gpu_surface,
453
+ .frame = geometry.RectF.init(0, 0, surface.width, surface.height),
454
+ });
455
+ var pixels: [100 * 40 * 4]u8 = undefined;
456
+ var scratch: [100 * 40 * 4]u8 = undefined;
457
+ const clear = canvas.Color.rgb8(0, 0, 0);
458
+
459
+ const initial = [_]canvas.CanvasCommand{
460
+ .{ .fill_rect = .{ .id = 1, .rect = geometry.RectF.init(4, 4, 4, 4), .fill = .{ .color = canvas.Color.rgb8(255, 0, 0) } } },
461
+ .{ .fill_rect = .{ .id = 2, .rect = geometry.RectF.init(44, 15, 4, 4), .fill = .{ .color = canvas.Color.rgb8(255, 0, 0) } } },
462
+ .{ .fill_rect = .{ .id = 5, .rect = geometry.RectF.init(90, 4, 4, 4), .fill = .{ .color = canvas.Color.rgb8(255, 0, 0) } } },
463
+ .{ .push_clip = .{ .id = 3, .rect = geometry.RectF.init(50, 10, 20, 20) } },
464
+ .{ .blur = .{ .id = 4, .rect = geometry.RectF.init(50, 10, 20, 20), .radius = 8 } },
465
+ .pop_clip,
466
+ };
467
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &initial });
468
+ const first = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
469
+ .frame_index = 1,
470
+ .surface_size = surface,
471
+ }, canvasFrameScratchStorage(&harness.runtime), &pixels, &scratch, clear);
472
+ try std.testing.expect(first.full_repaint);
473
+
474
+ // Two damage islands well outside the blur's radius keep the retained
475
+ // path even though their bounding union crosses the blur apron.
476
+ const far_changed = [_]canvas.CanvasCommand{
477
+ .{ .fill_rect = .{ .id = 1, .rect = geometry.RectF.init(4, 4, 4, 4), .fill = .{ .color = canvas.Color.rgb8(0, 255, 0) } } },
478
+ initial[1],
479
+ .{ .fill_rect = .{ .id = 5, .rect = geometry.RectF.init(90, 4, 4, 4), .fill = .{ .color = canvas.Color.rgb8(0, 255, 0) } } },
480
+ initial[3],
481
+ initial[4],
482
+ initial[5],
483
+ };
484
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &far_changed });
485
+ const incremental = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
486
+ .frame_index = 2,
487
+ .surface_size = surface,
488
+ }, canvasFrameScratchStorage(&harness.runtime), &pixels, &scratch, clear);
489
+ try std.testing.expect(!incremental.full_repaint);
490
+ try std.testing.expect(incremental.dirty_bounds != null);
491
+ try std.testing.expectEqual(@as(usize, 2), incremental.dirtyRects().len);
492
+
493
+ // This changed rect stops two points before the blur output, but it
494
+ // lies inside the radius-8 sampling apron. A scissored replay would
495
+ // sample the prior composited frame outside its dirty boundary.
496
+ const apron_changed = [_]canvas.CanvasCommand{
497
+ far_changed[0],
498
+ .{ .fill_rect = .{ .id = 2, .rect = geometry.RectF.init(44, 15, 4, 4), .fill = .{ .color = canvas.Color.rgb8(0, 0, 255) } } },
499
+ far_changed[2],
500
+ initial[3],
501
+ initial[4],
502
+ initial[5],
503
+ };
504
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &apron_changed });
505
+ const promoted = try harness.runtime.presentNextCanvasFramePixels(1, "canvas", .{
506
+ .frame_index = 3,
507
+ .surface_size = surface,
508
+ }, canvasFrameScratchStorage(&harness.runtime), &pixels, &scratch, clear);
509
+ try std.testing.expect(promoted.full_repaint);
510
+ try std.testing.expectEqual(@as(usize, 0), promoted.changes.len);
511
+ try std.testing.expectEqualDeep(geometry.RectF.init(0, 0, surface.width, surface.height), promoted.dirty_bounds.?);
512
+ }
513
+
434
514
  test "runtime requests gpu surface frames for retained canvas changes" {
435
515
  const TestApp = struct {
436
516
  fn app(self: *@This()) App {
@@ -474,6 +474,68 @@ test "runtime presents next canvas frame through packet presenter when available
474
474
  try std.testing.expect(!presented_frame.canvas_frame_requires_render);
475
475
  }
476
476
 
477
+ test "runtime explicit software canvas bypasses packet encoding and image upload" {
478
+ const TestApp = struct {
479
+ fn app(self: *@This()) App {
480
+ return .{ .context = self, .name = "gpu-canvas-explicit-software", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
481
+ }
482
+ };
483
+
484
+ const harness = try TestHarness().create(std.testing.allocator, .{});
485
+ defer harness.destroy(std.testing.allocator);
486
+ harness.null_platform.gpu_surfaces = true;
487
+ var app_state: TestApp = .{};
488
+ try harness.start(app_state.app());
489
+
490
+ _ = try harness.runtime.createView(.{
491
+ .window_id = 1,
492
+ .label = "canvas",
493
+ .kind = .gpu_surface,
494
+ .frame = geometry.RectF.init(0, 0, 4, 4),
495
+ .gpu_surface = .{ .backend = .software },
496
+ });
497
+
498
+ const red = [4]u8{ 255, 0, 0, 255 };
499
+ try harness.runtime.registerCanvasImage(7, 1, 1, &red);
500
+ const commands = [_]canvas.CanvasCommand{.{ .draw_image = .{
501
+ .id = 1,
502
+ .image_id = 7,
503
+ .dst = geometry.RectF.init(0, 0, 4, 4),
504
+ } }};
505
+ _ = try harness.runtime.setCanvasDisplayList(1, "canvas", .{ .commands = &commands });
506
+
507
+ var gpu_commands: [max_canvas_commands_per_view]canvas.CanvasGpuCommand = undefined;
508
+ var packet_json_buffer: [16 * 1024]u8 = undefined;
509
+ var pixels: [4 * 4 * 4]u8 = undefined;
510
+ var scratch: [4 * 4 * 4]u8 = undefined;
511
+
512
+ // The direct packet-present entry point enforces the same policy as
513
+ // automatic presentation and exits before packet image upload.
514
+ try std.testing.expectError(error.UnsupportedService, harness.runtime.presentNextCanvasGpuPacket(1, "canvas", .{
515
+ .frame_index = 21,
516
+ .timestamp_ns = 88_000,
517
+ .surface_size = geometry.SizeF.init(4, 4),
518
+ .scale = 1,
519
+ }, canvasFrameScratchStorage(&harness.runtime), canvas.Color.rgb8(0, 0, 0), &gpu_commands, &packet_json_buffer));
520
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.gpu_surface_packet_present_count);
521
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.gpu_surface_image_upload_count);
522
+ try std.testing.expectEqual(platform.GpuPresentFallbackReason.none, harness.runtime.views[0].info().gpu_present_fallback_reason);
523
+
524
+ const result = try harness.runtime.presentNextCanvasFrame(1, "canvas", .{
525
+ .frame_index = 22,
526
+ .timestamp_ns = 89_000,
527
+ .surface_size = geometry.SizeF.init(4, 4),
528
+ .scale = 1,
529
+ }, canvasFrameScratchStorage(&harness.runtime), &gpu_commands, &packet_json_buffer, &pixels, &scratch, canvas.Color.rgb8(0, 0, 0), null);
530
+
531
+ try std.testing.expectEqual(platform.GpuSurfaceBackend.software, harness.runtime.views[0].gpu_requested_backend);
532
+ try std.testing.expectEqual(CanvasPresentationMode.pixels, result.mode);
533
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.gpu_surface_packet_present_count);
534
+ try std.testing.expectEqual(@as(usize, 0), harness.null_platform.gpu_surface_image_upload_count);
535
+ try std.testing.expectEqual(@as(usize, 1), harness.null_platform.gpu_surface_present_count);
536
+ try std.testing.expectEqual(platform.GpuPresentFallbackReason.none, harness.runtime.views[0].info().gpu_present_fallback_reason);
537
+ }
538
+
477
539
  test "runtime auto-present packet honors presentation scale without invalidating retained frame" {
478
540
  const TestApp = struct {
479
541
  fn app(self: *@This()) App {
@@ -829,19 +891,31 @@ test "runtime re-registration and unregister drive the image upload side-channel
829
891
  try std.testing.expectEqual(@as(usize, 2), harness.null_platform.gpu_surface_image_upload_count);
830
892
  try std.testing.expectEqualDeep([4]u8{ 0, 0, 255, 255 }, harness.null_platform.gpuSurfaceImage(9).?.sample_rgba);
831
893
 
832
- // Unregister drops the platform-side entry; the next frame (stale
833
- // tree still referencing the id) stays on the packet path with the
834
- // absent-image skip and uploads nothing new.
894
+ // Unregister drops the platform-side entry. Re-registering the SAME
895
+ // content before another frame must still upload: the platform
896
+ // resource is gone even though the image fingerprint matches the
897
+ // last presented cache key.
835
898
  try std.testing.expect(harness.runtime.unregisterCanvasImage(9));
836
899
  try std.testing.expectEqual(@as(usize, 1), harness.null_platform.gpu_surface_image_remove_count);
837
900
  try std.testing.expect(harness.null_platform.gpuSurfaceImage(9) == null);
838
- const after = try present.frame(harness, &gpu_commands, &packet_json_buffer, &pixels, &scratch, 43);
901
+ try harness.runtime.registerCanvasImage(9, 1, 1, &blue);
902
+ const restored = try present.frame(harness, &gpu_commands, &packet_json_buffer, &pixels, &scratch, 43);
903
+ try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, restored.mode);
904
+ try std.testing.expectEqual(@as(usize, 3), harness.null_platform.gpu_surface_image_upload_count);
905
+ try std.testing.expectEqualDeep([4]u8{ 0, 0, 255, 255 }, harness.null_platform.gpuSurfaceImage(9).?.sample_rgba);
906
+
907
+ // A later unregister with no replacement keeps the stale tree on
908
+ // the packet path with the absent-image skip and no new upload.
909
+ try std.testing.expect(harness.runtime.unregisterCanvasImage(9));
910
+ try std.testing.expectEqual(@as(usize, 2), harness.null_platform.gpu_surface_image_remove_count);
911
+ try std.testing.expect(harness.null_platform.gpuSurfaceImage(9) == null);
912
+ const after = try present.frame(harness, &gpu_commands, &packet_json_buffer, &pixels, &scratch, 44);
839
913
  try std.testing.expectEqual(CanvasPresentationMode.gpu_packet, after.mode);
840
- try std.testing.expectEqual(@as(usize, 2), harness.null_platform.gpu_surface_image_upload_count);
914
+ try std.testing.expectEqual(@as(usize, 3), harness.null_platform.gpu_surface_image_upload_count);
841
915
 
842
916
  // Unregistering an absent id never reaches the platform.
843
917
  try std.testing.expect(!harness.runtime.unregisterCanvasImage(9));
844
- try std.testing.expectEqual(@as(usize, 1), harness.null_platform.gpu_surface_image_remove_count);
918
+ try std.testing.expectEqual(@as(usize, 2), harness.null_platform.gpu_surface_image_remove_count);
845
919
  }
846
920
 
847
921
  test "runtime falls back to pixels when the platform lacks the image upload seam" {
@@ -1193,9 +1267,9 @@ test "chat-transcript-shaped heavy frame stays on the packet path through the bi
1193
1267
  const binary_len = harness.null_platform.gpu_surface_packet_present_binary_len;
1194
1268
  try std.testing.expect(binary_len > 0);
1195
1269
  try std.testing.expect(binary_len <= platform.max_gpu_surface_packet_binary_bytes);
1196
- // The size win that makes the ceiling real: >=5x denser than the
1197
- // JSON encoding on this text-heavy frame.
1198
- try std.testing.expect(binary_len * 5 <= json_needed_bytes);
1270
+ // Positioned glyphs now ride the correctness-preserving v5 wire, while
1271
+ // compact fields still keep it >=3x denser than JSON on this shaped frame.
1272
+ try std.testing.expect(binary_len * 3 <= json_needed_bytes);
1199
1273
  try std.testing.expectEqual(platform.GpuPresentPath.packet, harness.runtime.views[0].gpu_present_path);
1200
1274
  try std.testing.expectEqual(platform.GpuPresentFallbackReason.none, harness.runtime.views[0].info().gpu_present_fallback_reason);
1201
1275
 
@@ -228,6 +228,22 @@ const BinaryCursor = struct {
228
228
  try self.skip(8 + 4 + 8 + 16); // font + size + origin + color
229
229
  const text_len = try self.readU32();
230
230
  try self.skip(text_len);
231
+ const has_positioned_glyphs = try self.readU8();
232
+ if (has_positioned_glyphs != 0) {
233
+ const glyph_count = try self.readU32();
234
+ for (0..glyph_count) |_| {
235
+ try self.skip(2); // glyph id
236
+ const glyph_flags = try self.readU8();
237
+ if (glyph_flags & 0x01 != 0) try self.skip(8); // font override
238
+ try self.skip(12); // x + baseline + advance
239
+ }
240
+ const fragment_count = try self.readU32();
241
+ for (0..fragment_count) |_| {
242
+ try self.skip(8); // x + baseline
243
+ const fragment_len = try self.readU32();
244
+ try self.skip(fragment_len);
245
+ }
246
+ }
231
247
  const has_layout = try self.readU8();
232
248
  if (has_layout != 0) {
233
249
  try self.skip(4 + 4 + 1 + 1); // max width + line height + wrap + align
@@ -192,8 +192,14 @@ pub fn RuntimeCanvasImages(comptime Runtime: type) type {
192
192
  // Best-effort drop of the platform-side texture: platforms
193
193
  // without the upload seam report UnsupportedService, and a
194
194
  // failed removal only costs the host a stale (unreferenced)
195
- // texture until the id is re-uploaded — never a wrong frame,
196
- // since packets key uploads by content fingerprint.
195
+ // texture until the id is re-uploaded. Invalidate the
196
+ // planner's per-view mirror FIRST: the platform resource's
197
+ // lifetime is independent of the fingerprint key, so an
198
+ // unregister followed by an identical registration must
199
+ // plan `.upload`, never `.retain` against a removed texture.
200
+ for (self.views[0..self.view_count]) |*view| {
201
+ view.removeCanvasFrameImageCacheId(id);
202
+ }
197
203
  self.options.platform.services.removeGpuSurfaceImage(id) catch {};
198
204
  noteCanvasImagesChanged(self);
199
205
  return true;
@@ -1,3 +1,5 @@
1
+ const canvas = @import("canvas");
2
+
1
3
  // Per-view canvas budgets. Fixed, documented, loud: capacities are compile
2
4
  // time constants sized for a dense desktop view, overflow errors name the
3
5
  // budget, and the automation snapshot reports headroom (widget_nodes=N/MAX).
@@ -169,12 +171,16 @@ pub const max_registered_canvas_font_bytes: usize = 24 * 1024 * 1024;
169
171
  // lockstep.
170
172
  pub const max_canvas_widget_nodes_per_view: usize = 1024;
171
173
  pub const max_canvas_widget_semantics_per_view: usize = 1024;
172
- // Raised from 2048 with the inline-span/markdown work: a rendered document
173
- // retains its full plain text (paragraph bytes are stored once; span slices
174
- // rebase into them) plus link payloads, and 2048 bytes could not hold a
175
- // README-sized document. Raised again with the node-budget raise:
176
- // a 1024-node view retains proportionally more text.
177
- pub const max_canvas_widget_text_bytes_per_view: usize = 65536;
174
+ // Raised from 2048 with the inline-span/markdown work, then from 64 KiB for
175
+ // editable code surfaces: a simple editor must retain a practical source
176
+ // file (roughly 10k ordinary code lines) plus the surrounding view chrome.
177
+ // Syntax paint remains viewport-bounded, so this larger SOURCE budget does
178
+ // not enlarge the display list or retained span pools.
179
+ pub const max_canvas_widget_text_bytes_per_view: usize = canvas.max_widget_text_bytes_per_view;
180
+ /// Ordinary views retain the historical 64 KiB pools inline. A view upgrades
181
+ /// to the practical source-file ceiling above only when its adopted layout
182
+ /// actually needs it, so empty/ordinary view creation remains allocation-free.
183
+ pub const max_canvas_widget_inline_text_bytes_per_view: usize = 64 * 1024;
178
184
  pub const max_canvas_widget_source_text_entries_per_view: usize = 256;
179
185
  /// Retained editor undo/redo is delta-based: changed bytes share this
180
186
  /// per-view pool and metadata stays bounded independently. Matching the
@@ -182,6 +188,7 @@ pub const max_canvas_widget_source_text_entries_per_view: usize = 256;
182
188
  /// be undone; an unusually large whole-document replacement whose removed
183
189
  /// plus inserted bytes exceed the pool simply starts a fresh history.
184
190
  pub const max_canvas_widget_text_history_bytes_per_view: usize = max_canvas_widget_text_bytes_per_view;
191
+ pub const max_canvas_widget_inline_text_history_bytes_per_view: usize = 64 * 1024;
185
192
  pub const max_canvas_widget_text_history_entries_per_view: usize = 128;
186
193
  // Inline styled runs retained across all `.text` widgets of a view. Each
187
194
  // span is a small struct (style flags + slices into the widget text
@@ -350,16 +350,19 @@ test "paste clamps to view text capacity and flags truncation loudly" {
350
350
  const used = harness.runtime.views[0].widget_text_len;
351
351
  const available = canvas_limits.max_canvas_widget_text_bytes_per_view - used;
352
352
 
353
- // A clipboard payload over the view's REMAINING shared text storage
354
- // (the semantics label already consumed a few bytes, and the widget
355
- // text capacity now matches the clipboard bound exactly).
353
+ // Fill the view with repeated maximum clipboard payloads. The retained
354
+ // source budget is intentionally larger than one clipboard transfer;
355
+ // only the final paste clamps against the remaining shared storage.
356
356
  const big = try std.testing.allocator.alloc(u8, platform.max_clipboard_data_bytes);
357
357
  defer std.testing.allocator.free(big);
358
358
  @memset(big, 'a');
359
359
  try harness.runtime.writeClipboard(big);
360
360
 
361
361
  try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_down, 100, 30));
362
- try harness.runtime.dispatchPlatformEvent(app, keyInput("v", cmd));
362
+ const paste_count = std.math.divCeil(usize, available, big.len) catch unreachable;
363
+ for (0..paste_count) |_| {
364
+ try harness.runtime.dispatchPlatformEvent(app, keyInput("v", cmd));
365
+ }
363
366
 
364
367
  const retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
365
368
  try std.testing.expectEqual(available, retained.nodes[1].widget.text.len);
@@ -537,7 +540,7 @@ test "grouped code paragraphs select and copy across chunk boundaries" {
537
540
  .text = second,
538
541
  .spans = &second_spans,
539
542
  .static_text_group_id = group_id,
540
- .static_text_group_offset = first.len,
543
+ .static_text_group_offset = @intCast(first.len),
541
544
  },
542
545
  .{
543
546
  .id = 4,
@@ -36,6 +36,11 @@ const runtime_canvas_widget_events = @import("canvas_widget_events.zig");
36
36
 
37
37
  const context_menu_log = std.log.scoped(.zero_context_menu);
38
38
 
39
+ const CanvasWidgetContextMenuTextScratch = struct {
40
+ group_buffer: [canvas_limits.max_canvas_widget_text_bytes_per_view]u8,
41
+ };
42
+ const canvas_widget_context_menu_text_scratch = canvas.lazy_tls.LazyTls(CanvasWidgetContextMenuTextScratch);
43
+
39
44
  /// Reserved item ids of the zero-code default menus.
40
45
  pub const default_item_cut: u32 = 1;
41
46
  pub const default_item_copy: u32 = 2;
@@ -398,8 +403,8 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
398
403
  .terminal => try applyDefaultTerminalAction(self, app, index, pending.target_id, event.item_id),
399
404
  .static_copy => {
400
405
  if (event.item_id != default_item_copy) return;
401
- var group_buffer: [canvas_limits.max_canvas_widget_text_bytes_per_view]u8 = undefined;
402
- const text = self.views[index].canvasWidgetCopyText(&group_buffer) orelse return;
406
+ const group_buffer = &canvas_widget_context_menu_text_scratch.get().group_buffer;
407
+ const text = self.views[index].canvasWidgetCopyText(group_buffer) orelse return;
403
408
  self.writeClipboard(text) catch return;
404
409
  },
405
410
  }