@native-sdk/cli 0.4.3 → 0.5.0

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 (100) hide show
  1. package/build/app.zig +308 -72
  2. package/build/ts_run.mjs +63 -0
  3. package/build.zig +336 -7
  4. package/build.zig.zon +1 -0
  5. package/package.json +15 -10
  6. package/packages/core/package-lock.json +43 -0
  7. package/packages/core/package.json +37 -0
  8. package/packages/core/rt/rt.zig +1579 -0
  9. package/packages/core/sdk/bytes_text_methods.d.ts +88 -0
  10. package/packages/core/sdk/core.ts +694 -0
  11. package/packages/core/sdk/events.ts +137 -0
  12. package/packages/core/sdk/text.ts +519 -0
  13. package/packages/core/src/checker.ts +1869 -0
  14. package/packages/core/src/cli.ts +68 -0
  15. package/packages/core/src/devhost.ts +291 -0
  16. package/packages/core/src/devhost_resolver.mjs +22 -0
  17. package/packages/core/src/diagnostics.ts +417 -0
  18. package/packages/core/src/emitter.ts +8815 -0
  19. package/packages/core/src/infer.ts +1515 -0
  20. package/packages/core/src/modules.ts +173 -0
  21. package/packages/core/src/ownership.ts +740 -0
  22. package/packages/core/src/text_polyfill.ts +337 -0
  23. package/packages/core/src/text_tables.ts +408 -0
  24. package/packages/core/src/transpile.ts +146 -0
  25. package/packages/core/src/typed_ast.ts +536 -0
  26. package/packages/core/src/types.ts +815 -0
  27. package/skill-data/core/SKILL.md +3 -0
  28. package/skill-data/native-ui/SKILL.md +30 -0
  29. package/skill-data/ts-core/SKILL.md +493 -0
  30. package/skill-data/zig/SKILL.md +208 -0
  31. package/skills/native-sdk/SKILL.md +1 -1
  32. package/src/app_runner/root.zig +75 -9
  33. package/src/app_runner/ts_core_main.zig +231 -0
  34. package/src/platform/linux/gtk_host.c +226 -21
  35. package/src/platform/windows/webview2_host.cpp +16 -10
  36. package/src/primitives/app_manifest/comptime_scene.zig +341 -0
  37. package/src/primitives/app_manifest/root.zig +13 -0
  38. package/src/primitives/app_manifest/types.zig +14 -0
  39. package/src/primitives/app_manifest/validation.zig +18 -0
  40. package/src/primitives/app_manifest/web_layer.zig +366 -0
  41. package/src/primitives/canvas/commands.zig +14 -7
  42. package/src/primitives/canvas/lazy_tls.zig +73 -0
  43. package/src/primitives/canvas/render_generic_resources.zig +22 -15
  44. package/src/primitives/canvas/root.zig +6 -0
  45. package/src/primitives/canvas/tests.zig +1 -0
  46. package/src/primitives/canvas/text_atlas.zig +40 -32
  47. package/src/primitives/canvas/text_layout_cache.zig +29 -22
  48. package/src/primitives/canvas/text_measure_cache.zig +56 -41
  49. package/src/primitives/canvas/text_spans.zig +39 -26
  50. package/src/primitives/canvas/tokens.zig +54 -0
  51. package/src/primitives/canvas/ui.zig +125 -0
  52. package/src/primitives/canvas/ui_markup.zig +22 -3
  53. package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
  54. package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
  55. package/src/primitives/canvas/ui_markup_contract.zig +68 -14
  56. package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
  57. package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
  58. package/src/primitives/canvas/ui_markup_view.zig +105 -1
  59. package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
  60. package/src/primitives/canvas/ui_schema.zig +8 -0
  61. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  62. package/src/primitives/canvas/widget_invalidation.zig +14 -7
  63. package/src/primitives/canvas/widget_render.zig +25 -15
  64. package/src/primitives/canvas/widget_render_controls.zig +15 -9
  65. package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
  66. package/src/root.zig +7 -0
  67. package/src/runtime/api.zig +7 -0
  68. package/src/runtime/bridge_responses.zig +2 -0
  69. package/src/runtime/builtin_bridge.zig +6 -0
  70. package/src/runtime/canvas_frame.zig +57 -36
  71. package/src/runtime/canvas_images.zig +9 -3
  72. package/src/runtime/canvas_widget_control_tests.zig +5 -6
  73. package/src/runtime/canvas_widget_runtime.zig +7 -4
  74. package/src/runtime/effects.zig +490 -2
  75. package/src/runtime/effects_host_tests.zig +594 -0
  76. package/src/runtime/flow.zig +8 -0
  77. package/src/runtime/platform_bridge_tests.zig +1 -0
  78. package/src/runtime/root.zig +18 -0
  79. package/src/runtime/session_replay.zig +7 -1
  80. package/src/runtime/session_tests.zig +35 -8
  81. package/src/runtime/tests.zig +2 -0
  82. package/src/runtime/ts_core_host.zig +1505 -0
  83. package/src/runtime/ts_core_host_tests.zig +1408 -0
  84. package/src/runtime/ts_ui_app.zig +469 -0
  85. package/src/runtime/ui_app.zig +58 -0
  86. package/src/runtime/view_canvas.zig +24 -17
  87. package/src/runtime/view_widget_tree.zig +2 -2
  88. package/src/runtime/web_layer_gate_tests.zig +242 -0
  89. package/src/runtime/window_storage.zig +5 -0
  90. package/src/runtime/window_view_runtime.zig +1 -0
  91. package/src/tooling/buildgraph.zig +7 -1
  92. package/src/tooling/codesign.zig +212 -105
  93. package/src/tooling/doctor.zig +78 -0
  94. package/src/tooling/manifest.zig +314 -1
  95. package/src/tooling/package.zig +722 -18
  96. package/src/tooling/raw_manifest.zig +16 -0
  97. package/src/tooling/root.zig +1 -0
  98. package/src/tooling/templates.zig +911 -88
  99. package/src/tooling/ts_core.zig +486 -0
  100. package/src/tooling/verbs.zig +22 -0
@@ -126,21 +126,31 @@ const max_widget_depth: usize = 32;
126
126
  /// per-view storage within the same emit call stack, so one threadlocal
127
127
  /// buffer per frame is sound — reset at each emit entry point. Overflow
128
128
  /// fails loudly by budget name.
129
- threadlocal var frame_label_bytes: [chart_model.max_chart_label_bytes_per_frame]u8 = undefined;
130
- threadlocal var frame_label_len: usize = 0;
129
+ // Lazily heap-allocated per thread (the label pool plus the chart
130
+ // polyline scratch below): init on first emit/chart use of a thread —
131
+ // the emitting thread by construction — so threads that never emit
132
+ // widgets never allocate it. The arrays carry no default and stay
133
+ // uninitialized, exactly like the `= undefined` statics they replace.
134
+ const WidgetRenderScratch = struct {
135
+ frame_label_bytes: [chart_model.max_chart_label_bytes_per_frame]u8,
136
+ frame_label_len: usize = 0,
137
+ chart_polyline_points: [chart_model.max_chart_points_per_series]geometry.PointF,
138
+ };
139
+ const widget_render_scratch = @import("lazy_tls.zig").LazyTls(WidgetRenderScratch);
131
140
 
132
141
  fn resetFrameLabelScratch() void {
133
- frame_label_len = 0;
142
+ widget_render_scratch.get().frame_label_len = 0;
134
143
  }
135
144
 
136
145
  /// Persist a formatted label into the frame scratch so the emitted
137
146
  /// command outlives the local formatting buffer.
138
147
  fn allocFrameLabelBytes(text: []const u8) Error![]const u8 {
139
- if (frame_label_len + text.len > frame_label_bytes.len) return error.ChartLabelBytesFull;
140
- const start = frame_label_len;
141
- frame_label_len += text.len;
142
- @memcpy(frame_label_bytes[start..frame_label_len], text);
143
- return frame_label_bytes[start..frame_label_len];
148
+ const scratch = widget_render_scratch.get();
149
+ if (scratch.frame_label_len + text.len > scratch.frame_label_bytes.len) return error.ChartLabelBytesFull;
150
+ const start = scratch.frame_label_len;
151
+ scratch.frame_label_len += text.len;
152
+ @memcpy(scratch.frame_label_bytes[start..scratch.frame_label_len], text);
153
+ return scratch.frame_label_bytes[start..scratch.frame_label_len];
144
154
  }
145
155
 
146
156
  /// Frame-lifetime scratch (same single-threaded emit contract as the
@@ -2062,27 +2072,27 @@ fn emitChartBand(
2062
2072
  }
2063
2073
 
2064
2074
  /// Map a series into plot-space points, skipping non-finite values.
2065
- /// Returned points live in a threadlocal scratch valid until the next
2066
- /// series maps (each emitter consumes them before returning).
2067
- threadlocal var chart_polyline_points: [chart_model.max_chart_points_per_series]geometry.PointF = undefined;
2068
-
2075
+ /// Returned points live in per-thread scratch (`WidgetRenderScratch`)
2076
+ /// valid until the next series maps (each emitter consumes them before
2077
+ /// returning).
2069
2078
  fn chartPolylinePoints(
2070
2079
  values: []const f32,
2071
2080
  domain: chart_model.ChartDomain,
2072
2081
  plot: geometry.RectF,
2073
2082
  inset: f32,
2074
2083
  ) Error![]const geometry.PointF {
2075
- const count = @min(values.len, chart_polyline_points.len);
2084
+ const points = &widget_render_scratch.get().chart_polyline_points;
2085
+ const count = @min(values.len, points.len);
2076
2086
  var len: usize = 0;
2077
2087
  for (values[0..count], 0..) |value, index| {
2078
2088
  if (!std.math.isFinite(value)) continue;
2079
- chart_polyline_points[len] = geometry.PointF.init(
2089
+ points[len] = geometry.PointF.init(
2080
2090
  chartMapX(index, count, plot, inset),
2081
2091
  chartMapY(value, domain, plot, inset),
2082
2092
  );
2083
2093
  len += 1;
2084
2094
  }
2085
- return chart_polyline_points[0..len];
2095
+ return points[0..len];
2086
2096
  }
2087
2097
 
2088
2098
  const chart_grid_seed: u64 = 0x5eed_c4a8_0000_0001;
@@ -1219,15 +1219,21 @@ pub fn emitSliderWidget(builder: *Builder, widget: Widget, tokens: DesignTokens)
1219
1219
  .fill = colorFill(disabledWash(widgetBackgroundColor(widget, visual.background orelse tokens.colors.surface_subtle), washed, tokens.states.disabled_alpha)),
1220
1220
  });
1221
1221
  const active_rest = widgetAccentColor(widget, visual.active_background orelse tokens.colors.accent);
1222
- try builder.fillRoundedRect(.{
1223
- .id = widgetPartId(widget.id, 2),
1224
- .rect = active,
1225
- .radius = track_radius,
1226
- .fill = colorFill(if (widget.state.disabled)
1227
- visual.disabled_background orelse disabledWash(active_rest, true, tokens.states.disabled_alpha)
1228
- else
1229
- active_rest),
1230
- });
1222
+ // Zero range paints NOTHING — same guard the progress bar wears. A
1223
+ // width-zero pill still owns anti-aliased edge coverage, so an idle
1224
+ // transport slider (value 0, disabled) showed a one-pixel filled
1225
+ // sliver at the rail's start.
1226
+ if (value > 0) {
1227
+ try builder.fillRoundedRect(.{
1228
+ .id = widgetPartId(widget.id, 2),
1229
+ .rect = active,
1230
+ .radius = track_radius,
1231
+ .fill = colorFill(if (widget.state.disabled)
1232
+ visual.disabled_background orelse disabledWash(active_rest, true, tokens.states.disabled_alpha)
1233
+ else
1234
+ active_rest),
1235
+ });
1236
+ }
1231
1237
  // Paper-white in BOTH schemes: the thumb must read against the
1232
1238
  // filled range and the muted rail alike, and the palette carries no
1233
1239
  // scheme-invariant white token — so the emitter states it, and a
@@ -1958,6 +1958,50 @@ test "widget emitter applies selection and range control tokens" {
1958
1958
  }
1959
1959
  }
1960
1960
 
1961
+ test "slider at value zero emits no filled range" {
1962
+ // The same zero guard the progress bar wears: a width-zero pill still
1963
+ // owns anti-aliased edge coverage, so an idle transport slider
1964
+ // (value 0, disabled) painted a one-pixel filled sliver at the rail's
1965
+ // start. Zero range must paint NOTHING — rail, thumb, and hairline
1966
+ // only.
1967
+ const tokens: DesignTokens = .{};
1968
+
1969
+ var commands: [8]CanvasCommand = undefined;
1970
+ var builder = Builder.init(&commands);
1971
+ try emitWidgetTree(&builder, .{
1972
+ .id = 70,
1973
+ .kind = .slider,
1974
+ .frame = geometry.RectF.init(0, 0, 160, 32),
1975
+ .value = 0,
1976
+ .state = .{ .disabled = true },
1977
+ }, tokens);
1978
+ const idle = builder.displayList();
1979
+ for (idle.commands[0..idle.commandCount()]) |command| {
1980
+ try std.testing.expect(command.objectId() != widgetPartId(70, 2));
1981
+ }
1982
+
1983
+ // A nonzero value keeps the filled range exactly as before.
1984
+ var active_commands: [8]CanvasCommand = undefined;
1985
+ var active_builder = Builder.init(&active_commands);
1986
+ try emitWidgetTree(&active_builder, .{
1987
+ .id = 70,
1988
+ .kind = .slider,
1989
+ .frame = geometry.RectF.init(0, 0, 160, 32),
1990
+ .value = 0.25,
1991
+ }, tokens);
1992
+ const active = active_builder.displayList();
1993
+ try std.testing.expectEqual(@as(usize, 1 + idle.commandCount()), active.commandCount());
1994
+ var saw_range = false;
1995
+ for (active.commands[0..active.commandCount()]) |command| {
1996
+ if (command.objectId() == widgetPartId(70, 2)) {
1997
+ saw_range = true;
1998
+ try std.testing.expect(command == .fill_rounded_rect);
1999
+ try std.testing.expectEqual(@as(f32, 160 * 0.25), command.fill_rounded_rect.rect.width);
2000
+ }
2001
+ }
2002
+ try std.testing.expect(saw_range);
2003
+ }
2004
+
1961
2005
  test "widget emitter applies radio control tokens" {
1962
2006
  const tokens = DesignTokens{
1963
2007
  .controls = .{
package/src/root.zig CHANGED
@@ -76,6 +76,13 @@ pub const EffectAudioEventKind = runtime.EffectAudioEventKind;
76
76
  pub const EffectAudioSource = runtime.EffectAudioSource;
77
77
  pub const audioCachePath = runtime.audioCachePath;
78
78
  pub const max_effect_audio_path_bytes = runtime.max_effect_audio_path_bytes;
79
+ pub const EffectHostResult = runtime.EffectHostResult;
80
+ pub const HostCallBinding = runtime.HostCallBinding;
81
+ pub const max_effect_host_name_bytes = runtime.max_effect_host_name_bytes;
82
+ pub const max_effect_host_payload_bytes = runtime.max_effect_host_payload_bytes;
83
+ pub const max_effect_host_result_bytes = runtime.max_effect_host_result_bytes;
84
+ pub const TsCoreHost = runtime.TsCoreHost;
85
+ pub const TsUiApp = runtime.TsUiApp;
79
86
  pub const Clock = runtime.Clock;
80
87
  pub const TestClock = runtime.TestClock;
81
88
  pub const nowMs = runtime.nowMs;
@@ -396,6 +396,13 @@ pub const Options = struct {
396
396
  automation: ?automation.Server = null,
397
397
  window_state_store: ?window_state.Store = null,
398
398
  js_window_api: bool = false,
399
+ /// Whether this build ships the embedded web layer. The app runner
400
+ /// sets it from the build graph's app.zon inference (declare-to-use:
401
+ /// a .frontend block, the "webview" capability, or a .shell webview
402
+ /// view). When false, every webview-creating path fails fast with
403
+ /// `error.WebViewLayerNotBuilt` and its teaching message instead of
404
+ /// reaching a platform host whose web layer was compiled out.
405
+ web_layer: bool = true,
399
406
  gpu_surface_frame_diagnostics: bool = true,
400
407
  /// Pixels-only presentation hosts (the mobile embed host) opt in to
401
408
  /// keeping the view's keyed command mirror alive across PIXEL
@@ -299,6 +299,7 @@ pub fn builtinBridgeErrorMessage(err: anyerror) []const u8 {
299
299
  error.CrossWindowWebViewDenied => "WebView windowId must match the calling window",
300
300
  error.InvalidWebViewOptions => "WebView options are invalid",
301
301
  error.WebViewNotFound => "WebView was not found",
302
+ error.WebViewLayerNotBuilt => "this app was built without the web layer because nothing in app.zon declared web use; add \"webview\" to .capabilities (or a .frontend block / a .shell webview view) and rebuild",
302
303
  error.WebViewLimitReached => "WebView limit reached",
303
304
  error.DuplicateWebViewLabel => "WebView label already exists",
304
305
  error.ReservedWebViewLabel => "WebView label \"main\" is reserved for the startup WebView",
@@ -367,6 +368,7 @@ pub fn builtinBridgeErrorCode(err: anyerror) bridge.ErrorCode {
367
368
  error.CrossWindowWebViewDenied,
368
369
  error.InvalidWebViewOptions,
369
370
  error.WebViewNotFound,
371
+ error.WebViewLayerNotBuilt,
370
372
  error.WebViewLimitReached,
371
373
  error.DuplicateWebViewLabel,
372
374
  error.ReservedWebViewLabel,
@@ -148,6 +148,12 @@ pub fn RuntimeBuiltinBridge(comptime Runtime: type) type {
148
148
  }
149
149
 
150
150
  pub fn dispatchWebViewBridgeCommand(self: *Runtime, request: bridge.Request, source_window_id: platform.WindowId, result_buffer: []u8, response_buffer: []u8) []const u8 {
151
+ // A native-only build has no webviews to command (and no page
152
+ // to command them from); every webview verb answers with the
153
+ // teaching error instead of a misleading not-found.
154
+ if (!self.options.web_layer) {
155
+ return bridge.writeErrorResponse(response_buffer, request.id, builtinBridgeErrorCode(error.WebViewLayerNotBuilt), builtinBridgeErrorMessage(error.WebViewLayerNotBuilt));
156
+ }
151
157
  const result = if (std.mem.eql(u8, request.command, "native-sdk.webview.create"))
152
158
  Self.createWebViewFromJson(self, request.payload, source_window_id, result_buffer) catch |err| return bridge.writeErrorResponse(response_buffer, request.id, builtinBridgeErrorCode(err), builtinBridgeErrorMessage(err))
153
159
  else if (std.mem.eql(u8, request.command, "native-sdk.webview.list"))
@@ -43,10 +43,29 @@ const max_canvas_render_animations_per_view = canvas_limits.max_canvas_render_an
43
43
  const max_canvas_text_layouts_per_view = canvas_limits.max_canvas_text_layouts_per_view;
44
44
  const max_canvas_text_layout_lines_per_view = canvas_limits.max_canvas_text_layout_lines_per_view;
45
45
  const max_canvas_retained_packet_commands_per_view = canvas_limits.max_canvas_retained_packet_commands_per_view;
46
- threadlocal var canvas_frame_text_layout_plans_scratch: [max_canvas_text_layouts_per_view]canvas.TextLayoutPlan = undefined;
47
- threadlocal var canvas_frame_text_layout_lines_scratch: [max_canvas_text_layout_lines_per_view]canvas.TextLine = undefined;
48
- threadlocal var canvas_frame_text_layout_cache_entries_scratch: [max_canvas_text_layouts_per_view]canvas.TextLayoutCacheEntry = undefined;
49
- threadlocal var canvas_frame_text_layout_cache_actions_scratch: [max_canvas_text_layouts_per_view * 2]canvas.TextLayoutCacheAction = undefined;
46
+ // The frame planner's per-thread scratch behind one lazily
47
+ // heap-allocated pointer (~1.8 MiB that would otherwise sit in the
48
+ // static TLS template every OS thread's loader clones): the text-layout
49
+ // planning arrays plus the packet patch-derivation arrays declared with
50
+ // their contract below. Init happens on a thread's first frame plan —
51
+ // the runtime loop thread by construction — so window-host, COM,
52
+ // accessibility, and worker threads never allocate it. Every field is
53
+ // per-use scratch (no cross-frame state), so first-use init cannot
54
+ // change planner output; the arrays carry no default and stay
55
+ // uninitialized, exactly like the `= undefined` statics they replace.
56
+ const CanvasFrameScratch = struct {
57
+ text_layout_plans: [max_canvas_text_layouts_per_view]canvas.TextLayoutPlan,
58
+ text_layout_lines: [max_canvas_text_layout_lines_per_view]canvas.TextLine,
59
+ text_layout_cache_entries: [max_canvas_text_layouts_per_view]canvas.TextLayoutCacheEntry,
60
+ text_layout_cache_actions: [max_canvas_text_layouts_per_view * 2]canvas.TextLayoutCacheAction,
61
+ packet_current: [max_canvas_retained_packet_commands_per_view]CanvasPacketCurrentCommand,
62
+ packet_current_sort: [max_canvas_retained_packet_commands_per_view]u32,
63
+ packet_baseline_sort: [max_canvas_retained_packet_commands_per_view]u32,
64
+ packet_baseline_matched: [max_canvas_retained_packet_commands_per_view]bool,
65
+ packet_baseline_stable: [max_canvas_retained_packet_commands_per_view]bool,
66
+ packet_upsert: [max_canvas_retained_packet_commands_per_view]bool,
67
+ };
68
+ const canvas_frame_scratch = canvas.lazy_tls.LazyTls(CanvasFrameScratch);
50
69
 
51
70
  /// One entry of the frame's CURRENT keyed command list — the full draw
52
71
  /// order the retained packet protocol works on (never the scissor
@@ -62,17 +81,14 @@ const CanvasPacketCurrentCommand = struct {
62
81
  bounds: geometry.RectF,
63
82
  };
64
83
 
65
- // Patch-derivation scratch (threadlocal, same pattern as the text-layout
66
- // scratch above): the current keyed list, a key-sorted index over it for
67
- // duplicate detection, a key-sorted index over the view's retained
68
- // baseline for O(log n) lookups, per-baseline matched flags (unmatched =
69
- // evict), and per-current upsert flags. ~64 KiB per thread total.
70
- threadlocal var canvas_packet_current_scratch: [max_canvas_retained_packet_commands_per_view]CanvasPacketCurrentCommand = undefined;
71
- threadlocal var canvas_packet_current_sort_scratch: [max_canvas_retained_packet_commands_per_view]u32 = undefined;
72
- threadlocal var canvas_packet_baseline_sort_scratch: [max_canvas_retained_packet_commands_per_view]u32 = undefined;
73
- threadlocal var canvas_packet_baseline_matched_scratch: [max_canvas_retained_packet_commands_per_view]bool = undefined;
74
- threadlocal var canvas_packet_baseline_stable_scratch: [max_canvas_retained_packet_commands_per_view]bool = undefined;
75
- threadlocal var canvas_packet_upsert_scratch: [max_canvas_retained_packet_commands_per_view]bool = undefined;
84
+ // Patch-derivation scratch (the `packet_*` fields of
85
+ // `CanvasFrameScratch` above): the current keyed list, a key-sorted
86
+ // index over it for duplicate detection, a key-sorted index over the
87
+ // view's retained baseline for O(log n) lookups, per-baseline matched
88
+ // flags (unmatched = evict), and per-current upsert flags. ~104 KiB per
89
+ // planning thread total. The matched/upsert flags persist between
90
+ // `computeCanvasPacketPatchStats` and `writeCanvasPacketPatchBinary` on
91
+ // the same thread — the same contract the statics carried.
76
92
 
77
93
  const validateViewLabel = validation.validateViewLabel;
78
94
  const canvasRenderAnimationStartNsForView = runtime_view.canvasRenderAnimationStartNsForView;
@@ -1125,6 +1141,7 @@ pub fn RuntimeCanvasFrames(comptime Runtime: type) type {
1125
1141
  }
1126
1142
 
1127
1143
  pub fn canvasFrameScratchStorage(self: *Runtime) canvas.CanvasFrameStorage {
1144
+ const scratch = canvas_frame_scratch.get();
1128
1145
  return .{
1129
1146
  .render_commands = &self.canvas_frame_render_commands,
1130
1147
  .render_batches = &self.canvas_frame_render_batches,
@@ -1148,10 +1165,10 @@ pub fn RuntimeCanvasFrames(comptime Runtime: type) type {
1148
1165
  .glyph_atlas_entries = &self.canvas_frame_glyph_atlas_entries,
1149
1166
  .glyph_atlas_cache_entries = &self.canvas_frame_glyph_atlas_cache_entries,
1150
1167
  .glyph_atlas_cache_actions = &self.canvas_frame_glyph_atlas_cache_actions,
1151
- .text_layout_plans = &canvas_frame_text_layout_plans_scratch,
1152
- .text_layout_lines = &canvas_frame_text_layout_lines_scratch,
1153
- .text_layout_cache_entries = &canvas_frame_text_layout_cache_entries_scratch,
1154
- .text_layout_cache_actions = &canvas_frame_text_layout_cache_actions_scratch,
1168
+ .text_layout_plans = &scratch.text_layout_plans,
1169
+ .text_layout_lines = &scratch.text_layout_lines,
1170
+ .text_layout_cache_entries = &scratch.text_layout_cache_entries,
1171
+ .text_layout_cache_actions = &scratch.text_layout_cache_actions,
1155
1172
  .changes = &self.canvas_frame_changes,
1156
1173
  };
1157
1174
  }
@@ -1261,14 +1278,15 @@ fn gatherCanvasPacketCurrentCommands(canvas_frame: canvas.CanvasFrame) ?[]const
1261
1278
  /// byte-identical output.
1262
1279
  fn gatherCanvasPacketCurrentCommandsFromPlan(render_commands: []const canvas.RenderCommand, surface_size: geometry.SizeF, render_bounds: ?geometry.RectF) ?[]const CanvasPacketCurrentCommand {
1263
1280
  const full_bounds = canvasFullRepaintBounds(surface_size, render_bounds) orelse return null;
1281
+ const scratch = canvas_frame_scratch.get();
1264
1282
  var count: usize = 0;
1265
1283
  for (render_commands, 0..) |command, index| {
1266
1284
  if (!canvas.renderCommandIntersectsDirtyBounds(command, full_bounds)) continue;
1267
1285
  const gpu_command = canvas.canvasGpuCommandFromRenderCommand(command, index);
1268
1286
  if (!gpu_command.supported()) return null;
1269
- if (count >= canvas_packet_current_scratch.len) return null;
1287
+ if (count >= scratch.packet_current.len) return null;
1270
1288
  const fingerprint = canvas.canvasGpuCommandFingerprint(gpu_command);
1271
- canvas_packet_current_scratch[count] = .{
1289
+ scratch.packet_current[count] = .{
1272
1290
  .key = canvas.canvasGpuPacketCommandKey(gpu_command, fingerprint),
1273
1291
  .fingerprint = fingerprint,
1274
1292
  .render_index = @intCast(index),
@@ -1276,14 +1294,14 @@ fn gatherCanvasPacketCurrentCommandsFromPlan(render_commands: []const canvas.Ren
1276
1294
  };
1277
1295
  count += 1;
1278
1296
  }
1279
- const sorted = canvas_packet_current_sort_scratch[0..count];
1297
+ const sorted = scratch.packet_current_sort[0..count];
1280
1298
  for (sorted, 0..) |*slot, index| slot.* = @intCast(index);
1281
- std.sort.pdq(u32, sorted, @as([]const CanvasPacketCurrentCommand, canvas_packet_current_scratch[0..count]), canvasPacketCurrentKeyLessThan);
1299
+ std.sort.pdq(u32, sorted, @as([]const CanvasPacketCurrentCommand, scratch.packet_current[0..count]), canvasPacketCurrentKeyLessThan);
1282
1300
  var index: usize = 1;
1283
1301
  while (index < count) : (index += 1) {
1284
- if (canvas_packet_current_scratch[sorted[index - 1]].key == canvas_packet_current_scratch[sorted[index]].key) return null;
1302
+ if (scratch.packet_current[sorted[index - 1]].key == scratch.packet_current[sorted[index]].key) return null;
1285
1303
  }
1286
- return canvas_packet_current_scratch[0..count];
1304
+ return scratch.packet_current[0..count];
1287
1305
  }
1288
1306
 
1289
1307
  fn canvasPacketCurrentKeyLessThan(current: []const CanvasPacketCurrentCommand, a: u32, b: u32) bool {
@@ -1317,12 +1335,13 @@ fn computeCanvasPacketPatchStats(view: anytype, current: []const CanvasPacketCur
1317
1335
  const baseline_keys = view.canvas_packet_baseline_keys[0..baseline_count];
1318
1336
  const baseline_fingerprints = view.canvas_packet_baseline_fingerprints[0..baseline_count];
1319
1337
 
1320
- const baseline_sorted = canvas_packet_baseline_sort_scratch[0..baseline_count];
1338
+ const scratch = canvas_frame_scratch.get();
1339
+ const baseline_sorted = scratch.packet_baseline_sort[0..baseline_count];
1321
1340
  for (baseline_sorted, 0..) |*slot, index| slot.* = @intCast(index);
1322
1341
  std.sort.pdq(u32, baseline_sorted, @as([]const u64, baseline_keys), canvasPacketBaselineKeyLessThan);
1323
- const matched = canvas_packet_baseline_matched_scratch[0..baseline_count];
1342
+ const matched = scratch.packet_baseline_matched[0..baseline_count];
1324
1343
  @memset(matched, false);
1325
- const upserts = canvas_packet_upsert_scratch[0..current.len];
1344
+ const upserts = scratch.packet_upsert[0..current.len];
1326
1345
 
1327
1346
  var stats = CanvasPacketPatchStats{};
1328
1347
  for (current, 0..) |entry, index| {
@@ -1395,22 +1414,23 @@ fn canvasPacketPatchDirtyBounds(view: anytype, current: []const CanvasPacketCurr
1395
1414
  const baseline_fingerprints = view.canvas_packet_baseline_fingerprints[0..baseline_count];
1396
1415
  const baseline_bounds = view.canvas_packet_baseline_bounds[0..baseline_count];
1397
1416
 
1398
- const baseline_sorted = canvas_packet_baseline_sort_scratch[0..baseline_count];
1417
+ const scratch = canvas_frame_scratch.get();
1418
+ const baseline_sorted = scratch.packet_baseline_sort[0..baseline_count];
1399
1419
  for (baseline_sorted, 0..) |*slot, index| slot.* = @intCast(index);
1400
1420
  std.sort.pdq(u32, baseline_sorted, @as([]const u64, baseline_keys), canvasPacketBaselineKeyLessThan);
1401
- const matched = canvas_packet_baseline_matched_scratch[0..baseline_count];
1402
- const stable = canvas_packet_baseline_stable_scratch[0..baseline_count];
1421
+ const matched = scratch.packet_baseline_matched[0..baseline_count];
1422
+ const stable = scratch.packet_baseline_stable[0..baseline_count];
1403
1423
  @memset(matched, false);
1404
1424
  @memset(stable, false);
1405
1425
 
1406
1426
  var dirty = CanvasPacketPatchDirty{};
1407
1427
  for (current, 0..) |entry, index| {
1408
- canvas_packet_upsert_scratch[index] = true;
1428
+ scratch.packet_upsert[index] = true;
1409
1429
  if (findCanvasPacketBaselineIndex(baseline_keys, baseline_sorted, entry.key)) |baseline_index| {
1410
1430
  matched[baseline_index] = true;
1411
1431
  if (baseline_fingerprints[baseline_index] == entry.fingerprint) {
1412
1432
  stable[baseline_index] = true;
1413
- canvas_packet_upsert_scratch[index] = false;
1433
+ scratch.packet_upsert[index] = false;
1414
1434
  continue;
1415
1435
  }
1416
1436
  dirty.add(baseline_bounds[baseline_index]);
@@ -1426,7 +1446,7 @@ fn canvasPacketPatchDirtyBounds(view: anytype, current: []const CanvasPacketCurr
1426
1446
  // outside the union may change.
1427
1447
  var baseline_walk: usize = 0;
1428
1448
  for (current, 0..) |entry, index| {
1429
- if (canvas_packet_upsert_scratch[index]) continue;
1449
+ if (scratch.packet_upsert[index]) continue;
1430
1450
  while (baseline_walk < baseline_count and !stable[baseline_walk]) baseline_walk += 1;
1431
1451
  if (baseline_walk >= baseline_count or baseline_keys[baseline_walk] != entry.key) return null;
1432
1452
  baseline_walk += 1;
@@ -1453,8 +1473,9 @@ fn writeCanvasPacketPatchBinary(
1453
1473
  ) !void {
1454
1474
  const baseline_count = view.canvas_packet_baseline_count;
1455
1475
  const baseline_keys = view.canvas_packet_baseline_keys[0..baseline_count];
1456
- const matched = canvas_packet_baseline_matched_scratch[0..baseline_count];
1457
- const upserts = canvas_packet_upsert_scratch[0..current.len];
1476
+ const scratch = canvas_frame_scratch.get();
1477
+ const matched = scratch.packet_baseline_matched[0..baseline_count];
1478
+ const upserts = scratch.packet_upsert[0..current.len];
1458
1479
 
1459
1480
  try canvas.writeCanvasGpuPacketBinaryHeader(
1460
1481
  canvas.binary_packet_load_action_patch,
@@ -52,8 +52,14 @@ pub const RegisteredCanvasImage = struct {
52
52
  /// bound because decoders may need in-buffer scratch beyond the tight
53
53
  /// pixel bytes (the null platform's strict PNG parser keeps one filter
54
54
  /// byte per row: raw stream <= pixels + pixels/4 since a row is at least
55
- /// 4 pixel bytes). Loop-thread only, like the frame scratch.
56
- threadlocal var canvas_image_decode_scratch: [max_registered_canvas_image_pixel_bytes + max_registered_canvas_image_pixel_bytes / 4]u8 = undefined;
55
+ /// 4 pixel bytes). Loop-thread only, like the frame scratch — and
56
+ /// lazily heap-allocated per thread (1.25 MiB) on the first decode, so
57
+ /// threads that never register image bytes never carry it in their
58
+ /// static TLS block.
59
+ const CanvasImageDecodeScratch = struct {
60
+ bytes: [max_registered_canvas_image_pixel_bytes + max_registered_canvas_image_pixel_bytes / 4]u8,
61
+ };
62
+ const canvas_image_decode_scratch = canvas.lazy_tls.LazyTls(CanvasImageDecodeScratch);
57
63
 
58
64
  pub fn RuntimeCanvasImages(comptime Runtime: type) type {
59
65
  return struct {
@@ -112,7 +118,7 @@ pub fn RuntimeCanvasImages(comptime Runtime: type) type {
112
118
  /// `error.ImageTooLarge` (decoded pixels over the slot bound).
113
119
  pub fn registerCanvasImageBytes(self: *Runtime, id: canvas.ImageId, bytes: []const u8) anyerror!RegisteredCanvasImage {
114
120
  if (id == 0) return error.InvalidImageId;
115
- const decoded = try self.options.platform.services.decodeImage(bytes, &canvas_image_decode_scratch);
121
+ const decoded = try self.options.platform.services.decodeImage(bytes, &canvas_image_decode_scratch.get().bytes);
116
122
  if (decoded.rgba8.len > max_registered_canvas_image_pixel_bytes) return error.ImageTooLarge;
117
123
  try registerCanvasImage(self, id, decoded.width, decoded.height, decoded.rgba8);
118
124
  return .{ .width = decoded.width, .height = decoded.height };
@@ -188,23 +188,22 @@ test "runtime applies pointer values to canvas controls" {
188
188
  _ = try harness.runtime.emitCanvasWidgetDisplayList(1, "canvas", .{});
189
189
  const display_list = try harness.runtime.canvasDisplayList(1, "canvas");
190
190
  var saw_checkbox_check = false;
191
- var saw_empty_slider_active = false;
192
191
  for (display_list.commands) |command| {
193
192
  switch (command) {
194
193
  .stroke_path => |stroke| {
195
194
  if (stroke.id == testCanvasWidgetPartId(2, 4)) saw_checkbox_check = true;
196
195
  },
197
196
  .fill_rounded_rect => |fill| {
198
- if (fill.id == testCanvasWidgetPartId(4, 2)) {
199
- try std.testing.expectEqual(@as(f32, 0), fill.rect.width);
200
- saw_empty_slider_active = true;
201
- }
197
+ // A slider dragged to zero paints NO active range (the
198
+ // zero guard the progress bar wears): a width-zero pill
199
+ // still owns anti-aliased edge coverage — the idle
200
+ // transport slider's one-pixel sliver.
201
+ try std.testing.expect(fill.id != testCanvasWidgetPartId(4, 2));
202
202
  },
203
203
  else => {},
204
204
  }
205
205
  }
206
206
  try std.testing.expect(saw_checkbox_check);
207
- try std.testing.expect(saw_empty_slider_active);
208
207
  }
209
208
 
210
209
  test "runtime automation widget click dispatches pointer input" {
@@ -114,9 +114,12 @@ pub const CanvasWidgetSemanticsIndex = CanvasWidgetIdIndex(canvas.WidgetSemantic
114
114
  /// Shared per-pass index scratch (~8 KiB of slots per table). The two
115
115
  /// reconcile passes per rebuild (the staged reconcile, then the retained
116
116
  /// copy) run back-to-back on the single-threaded event loop and each
117
- /// rebuilds every table it uses, so one threadlocal set serves both —
118
- /// the same pattern as the planners' probe-table scratch.
119
- pub threadlocal var canvas_widget_reconcile_index_scratch: CanvasWidgetReconcileIndexScratch = .{};
117
+ /// rebuilds every table it uses, so one per-thread set serves both —
118
+ /// the same pattern as the planners' probe-table scratch. Lazily
119
+ /// heap-allocated (~32 KiB) on a thread's first reconcile, so threads
120
+ /// that never reconcile widgets never carry it in their static TLS
121
+ /// block.
122
+ pub const canvas_widget_reconcile_index_scratch = canvas.lazy_tls.LazyTls(CanvasWidgetReconcileIndexScratch);
120
123
 
121
124
  pub const CanvasWidgetReconcileIndexScratch = struct {
122
125
  controls: CanvasWidgetControlEntryIndex = .{},
@@ -825,7 +828,7 @@ pub fn canvasWidgetLayoutTreeWithRuntimeReconcileState(
825
828
  // mid-rubber-band must not clamp an offset the OS scroller owns.
826
829
  restoreCanvasWidgetLayoutScrollOffsets(staged_nodes, previous_runtime_offsets, previous_source_scroll_entries);
827
830
 
828
- const index_scratch = &canvas_widget_reconcile_index_scratch;
831
+ const index_scratch = canvas_widget_reconcile_index_scratch.get();
829
832
  index_scratch.controls.build(previous_control_states);
830
833
  index_scratch.source_controls.build(previous_source_control_entries);
831
834
  index_scratch.texts.build(previous_text_states);