@native-sdk/cli 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/build/app.zig +121 -66
  2. package/build.zig +123 -6
  3. package/package.json +9 -9
  4. package/skill-data/core/SKILL.md +3 -0
  5. package/skill-data/native-ui/SKILL.md +16 -0
  6. package/skill-data/zig/SKILL.md +198 -0
  7. package/skills/native-sdk/SKILL.md +1 -1
  8. package/src/app_runner/root.zig +51 -9
  9. package/src/platform/linux/gtk_host.c +226 -21
  10. package/src/platform/windows/webview2_host.cpp +16 -10
  11. package/src/primitives/app_manifest/root.zig +8 -0
  12. package/src/primitives/app_manifest/types.zig +14 -0
  13. package/src/primitives/app_manifest/validation.zig +18 -0
  14. package/src/primitives/app_manifest/web_layer.zig +366 -0
  15. package/src/primitives/canvas/chart_tests.zig +2 -2
  16. package/src/primitives/canvas/markdown_tests.zig +1 -1
  17. package/src/primitives/canvas/reference.zig +135 -6
  18. package/src/primitives/canvas/reference_tests.zig +171 -6
  19. package/src/primitives/canvas/root.zig +3 -0
  20. package/src/primitives/canvas/test_support.zig +3 -0
  21. package/src/primitives/canvas/widget_builtin_tests.zig +249 -4
  22. package/src/primitives/canvas/widget_render_controls.zig +18 -1
  23. package/src/primitives/canvas/widget_runtime.zig +3 -0
  24. package/src/primitives/canvas/widget_text_input.zig +104 -1
  25. package/src/runtime/api.zig +7 -0
  26. package/src/runtime/bridge_responses.zig +2 -0
  27. package/src/runtime/builtin_bridge.zig +6 -0
  28. package/src/runtime/canvas_widget_runtime.zig +23 -3
  29. package/src/runtime/canvas_widget_text_tests.zig +101 -0
  30. package/src/runtime/flow.zig +8 -0
  31. package/src/runtime/platform_bridge_tests.zig +1 -0
  32. package/src/runtime/session_tests.zig +35 -8
  33. package/src/runtime/view.zig +1 -1
  34. package/src/runtime/view_widget_scroll.zig +12 -1
  35. package/src/runtime/view_widget_text.zig +6 -2
  36. package/src/runtime/web_layer_gate_tests.zig +242 -0
  37. package/src/runtime/window_storage.zig +5 -0
  38. package/src/runtime/window_view_runtime.zig +1 -0
  39. package/src/tooling/buildgraph.zig +119 -3
  40. package/src/tooling/junction.zig +247 -0
  41. package/src/tooling/manifest.zig +267 -1
  42. package/src/tooling/package.zig +598 -6
  43. package/src/tooling/raw_manifest.zig +1 -0
  44. package/src/tooling/root.zig +1 -0
  45. package/src/tooling/templates.zig +288 -61
  46. package/src/tooling/verbs.zig +5 -0
@@ -17,7 +17,9 @@ const TextSelection = text_model.TextSelection;
17
17
  const DesignTokens = token_model.DesignTokens;
18
18
  const Widget = widget_model.Widget;
19
19
  const snapTextSelection = text_model.snapTextSelection;
20
+ const snapTextOffset = text_model.snapTextOffset;
20
21
  const snapTextRange = text_model.snapTextRange;
22
+ const measureTextWidthForFont = text_model.measureTextWidthForFont;
21
23
  const nextTextLineEnd = text_model.nextTextLineEnd;
22
24
  const isTextBreakByte = text_model.isTextBreakByte;
23
25
  const textLineRange = text_model.textLineRange;
@@ -94,6 +96,29 @@ fn widgetTextInputScrollOffset(widget: Widget, tokens: DesignTokens, text_size:
94
96
  return std.math.clamp(widget.value, 0, widgetTextInputMaxScrollOffset(widget, tokens, text_size, text_inset, options));
95
97
  }
96
98
 
99
+ /// Advance the horizontal scroll math reserves past the last glyph so a
100
+ /// caret parked at the end of the value lands inside the clip instead of
101
+ /// exactly on its trailing edge (the caret bar is one point wide).
102
+ const text_input_caret_reserve: f32 = 1;
103
+
104
+ /// The single-line field's horizontal scroll offset: the same retained
105
+ /// `value` channel the textarea scrolls vertically, clamped so the field
106
+ /// never shows trailing emptiness while text could fill the span. Zero
107
+ /// whenever the value fits — a short value renders exactly as an
108
+ /// unscrolled field always has.
109
+ fn widgetTextInputHorizontalScrollOffset(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) f32 {
110
+ if (options.wrap != .none) return 0;
111
+ return std.math.clamp(widget.value, 0, widgetTextInputMaxHorizontalScrollOffset(widget, tokens, text_size, text_inset, options));
112
+ }
113
+
114
+ fn widgetTextInputMaxHorizontalScrollOffset(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) f32 {
115
+ if (options.wrap != .none or widget.text.len == 0) return 0;
116
+ const viewport = widgetTextInputClipRect(widget, tokens, text_size, text_inset, options);
117
+ if (viewport.width <= 0) return 0;
118
+ const text_width = measureTextWidthForFont(options.measure, tokens.typography.font_id, widget.text, text_size);
119
+ return @max(0, text_width + text_input_caret_reserve - viewport.width);
120
+ }
121
+
97
122
  pub fn widgetTextInputOrigin(widget: Widget, tokens: DesignTokens, text_size: f32, inset: f32, options: TextLayoutOptions) geometry.PointF {
98
123
  if (options.wrap != .none) {
99
124
  const scroll_offset = widgetTextInputScrollOffset(widget, tokens, text_size, inset, options);
@@ -102,7 +127,14 @@ pub fn widgetTextInputOrigin(widget: Widget, tokens: DesignTokens, text_size: f3
102
127
  widget.frame.y + widgetTextInputVerticalInset(widget, tokens, text_size, options) + text_size - scroll_offset,
103
128
  );
104
129
  }
105
- return textInputOriginForFrame(widget.frame, text_size, inset);
130
+ // Single-line fields scroll horizontally through the same origin:
131
+ // selection rects, composition underlines, the caret, and hit-testing
132
+ // all derive their geometry from this point, so shifting it moves
133
+ // every text affordance together. The offset stays in logical
134
+ // coordinates here — `widgetTextInputDrawText` snaps the final point.
135
+ const scroll_offset = widgetTextInputHorizontalScrollOffset(widget, tokens, text_size, inset, options);
136
+ const origin = textInputOriginForFrame(widget.frame, text_size, inset);
137
+ return geometry.PointF.init(origin.x - scroll_offset, origin.y);
106
138
  }
107
139
 
108
140
  pub fn widgetTextInputClipRect(widget: Widget, tokens: DesignTokens, text_size: f32, inset: f32, options: TextLayoutOptions) geometry.RectF {
@@ -147,6 +179,77 @@ pub fn clampedTextInputScrollOffsetForWidget(widget: Widget, tokens: DesignToken
147
179
  return std.math.clamp(offset, 0, textInputMaxScrollOffsetForWidget(widget, tokens));
148
180
  }
149
181
 
182
+ /// The furthest a single-line field may scroll horizontally: value width
183
+ /// (plus the caret's own advance) past the visible span, zero when the
184
+ /// value fits or the field wraps.
185
+ pub fn textInputMaxHorizontalScrollOffsetForWidget(widget: Widget, tokens: DesignTokens) f32 {
186
+ if (!widget_access.widgetTextInputKind(widget.kind)) return 0;
187
+ const text_size = widgetTextInputSize(widget, tokens);
188
+ const text_inset = widgetTextInputInset(widget, tokens);
189
+ const options = widgetTextInputLayoutOptions(widget, tokens, text_size, text_inset);
190
+ return widgetTextInputMaxHorizontalScrollOffset(widget, tokens, text_size, text_inset, options);
191
+ }
192
+
193
+ pub fn clampedTextInputHorizontalScrollOffsetForWidget(widget: Widget, tokens: DesignTokens, offset: f32) f32 {
194
+ if (!widget_access.widgetTextInputKind(widget.kind)) return 0;
195
+ return std.math.clamp(offset, 0, textInputMaxHorizontalScrollOffsetForWidget(widget, tokens));
196
+ }
197
+
198
+ /// Keep-visible edge margin for the single-line caret: enough neighbor
199
+ /// glyphs stay in view to give the caret context, shrinking with the
200
+ /// field so tiny spans still fit both margins plus the caret.
201
+ fn textInputCaretVisibleMargin(viewport_width: f32) f32 {
202
+ return @min(8, viewport_width * 0.25);
203
+ }
204
+
205
+ /// The minimally-adjusted horizontal scroll offset that keeps the
206
+ /// selection's focus end visible inside the field's content rect (with a
207
+ /// small edge margin for context). Fields whose value fits, wrapping
208
+ /// fields, and fields without a selection just clamp — the offset only
209
+ /// moves when the caret would otherwise leave the visible span.
210
+ pub fn textInputCaretVisibleScrollOffsetForWidget(widget: Widget, tokens: DesignTokens, offset: f32) f32 {
211
+ const clamped = clampedTextInputHorizontalScrollOffsetForWidget(widget, tokens, offset);
212
+ if (!widget_access.widgetTextInputKind(widget.kind)) return clamped;
213
+ const selection = widget.text_selection orelse return clamped;
214
+ const text_size = widgetTextInputSize(widget, tokens);
215
+ const text_inset = widgetTextInputInset(widget, tokens);
216
+ const options = widgetTextInputLayoutOptions(widget, tokens, text_size, text_inset);
217
+ if (options.wrap != .none) return clamped;
218
+ const viewport = widgetTextInputClipRect(widget, tokens, text_size, text_inset, options);
219
+ if (viewport.width <= 0) return clamped;
220
+
221
+ // Caret x in content (unscrolled) coordinates: the width of the value
222
+ // up to the caret byte, measured on the same seam the line layout and
223
+ // selection rects measure with.
224
+ const caret_offset = snapTextOffset(widget.text, selection.focus);
225
+ const caret_x = measureTextWidthForFont(options.measure, tokens.typography.font_id, widget.text[0..caret_offset], text_size);
226
+ const margin = textInputCaretVisibleMargin(viewport.width);
227
+ var next = clamped;
228
+ const scrolled_in = caret_x + text_input_caret_reserve + margin - viewport.width;
229
+ const scrolled_back = caret_x - margin;
230
+ if (next < scrolled_in) next = scrolled_in;
231
+ if (next > scrolled_back) next = scrolled_back;
232
+ return clampedTextInputHorizontalScrollOffsetForWidget(widget, tokens, next);
233
+ }
234
+
235
+ /// Whether the field's painted text needs the content-rect clip: a
236
+ /// textarea always clips (its overflow scrolls vertically), a single-line
237
+ /// field clips once its value can scroll, and an overflowing placeholder
238
+ /// clips at the same border the value would. Short values emit no clip,
239
+ /// exactly as before fields scrolled.
240
+ pub fn widgetTextInputClipsText(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) bool {
241
+ if (widget.kind == .textarea) return true;
242
+ if (options.wrap != .none) return false;
243
+ if (widgetTextInputMaxHorizontalScrollOffset(widget, tokens, text_size, text_inset, options) > 0) return true;
244
+ if (widget.text.len == 0) {
245
+ const placeholder = widgetPlaceholder(widget);
246
+ if (placeholder.len == 0) return false;
247
+ const viewport = widgetTextInputClipRect(widget, tokens, text_size, text_inset, options);
248
+ return measureTextWidthForFont(options.measure, tokens.typography.font_id, placeholder, text_size) > viewport.width;
249
+ }
250
+ return false;
251
+ }
252
+
150
253
  fn widgetTextInputMaxScrollOffset(widget: Widget, tokens: DesignTokens, text_size: f32, text_inset: f32, options: TextLayoutOptions) f32 {
151
254
  const viewport = widgetTextInputClipRect(widget, tokens, text_size, text_inset, options);
152
255
  return @max(0, textInputContentExtentForWidgetWithOptions(widget, tokens.typography.font_id, text_size, options) - viewport.height);
@@ -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"))
@@ -700,7 +700,13 @@ pub fn canvasWidgetLayoutNodeWithTextReconcileState(
700
700
  const source_matches_runtime_text = std.mem.eql(u8, entry.text, copy.widget.text);
701
701
  if (!source_unchanged and !source_matches_runtime_text) return copy;
702
702
  if (source_unchanged) copy.widget.text = entry.text;
703
- if (copy.widget.kind == .textarea) copy.widget.value = entry.value;
703
+ // The retained scroll offset rides `value` for every editable
704
+ // text kind: the textarea's vertical offset and the single-line
705
+ // field's horizontal one both survive rebuilds through this
706
+ // restore (and re-clamp in `clampCanvasWidgetLayoutTextOffsets`
707
+ // right after, so a resized or re-texted field never keeps a
708
+ // stale offset).
709
+ if (canvasWidgetEditableTextKind(copy.widget.kind)) copy.widget.value = entry.value;
704
710
  if (copy.widget.text_selection == null and copy.widget.text_composition == null) {
705
711
  copy.widget.text_selection = entry.text_selection;
706
712
  copy.widget.text_composition = entry.text_composition;
@@ -903,8 +909,22 @@ pub fn clampCanvasWidgetLayoutScrollOffsets(nodes: []canvas.WidgetLayoutNode, st
903
909
 
904
910
  pub fn clampCanvasWidgetLayoutTextOffsets(nodes: []canvas.WidgetLayoutNode, tokens: canvas.DesignTokens) void {
905
911
  for (nodes) |*node| {
906
- if (node.widget.kind != .textarea) continue;
907
- node.widget.value = canvas.clampedTextInputScrollOffsetForWidget(node.widget, tokens, node.widget.value);
912
+ if (node.widget.kind == .textarea) {
913
+ node.widget.value = canvas.clampedTextInputScrollOffsetForWidget(node.widget, tokens, node.widget.value);
914
+ continue;
915
+ }
916
+ if (!canvasWidgetSingleLineTextKind(node.widget.kind)) continue;
917
+ // Single-line fields clamp their horizontal offset against the
918
+ // just-reconciled text and frame, and a field holding a caret
919
+ // also keeps it inside the visible span — this is where a layout
920
+ // width change (a resized window narrowing the field) is first
921
+ // known, so the caret never strands outside the clip after a
922
+ // resize. Deterministic in retained state: the offset only moves
923
+ // when the clamp or the caret demands it.
924
+ node.widget.value = if (node.widget.text_selection != null)
925
+ canvas.textInputCaretVisibleScrollOffsetForWidget(node.widget, tokens, node.widget.value)
926
+ else
927
+ canvas.clampedTextInputHorizontalScrollOffsetForWidget(node.widget, tokens, node.widget.value);
908
928
  }
909
929
  }
910
930
 
@@ -2102,3 +2102,104 @@ test "word selection feeds the shared selection state: copy and shift-arrow just
2102
2102
  } });
2103
2103
  try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 6 }, try retainedTextSelection(harness, 1));
2104
2104
  }
2105
+
2106
+ test "runtime scrolls single-line fields horizontally to keep the caret visible" {
2107
+ const TestApp = struct {
2108
+ fn app(self: *@This()) App {
2109
+ return .{ .context = self, .name = "gpu-widget-field-scroll", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
2110
+ }
2111
+ };
2112
+
2113
+ const harness = try TestHarness().create(std.testing.allocator, .{});
2114
+ defer harness.destroy(std.testing.allocator);
2115
+ harness.null_platform.gpu_surfaces = true;
2116
+ var app_state: TestApp = .{};
2117
+ const app = app_state.app();
2118
+ try harness.start(app);
2119
+
2120
+ _ = try harness.runtime.createView(.{
2121
+ .window_id = 1,
2122
+ .label = "canvas",
2123
+ .kind = .gpu_surface,
2124
+ .frame = geometry.RectF.init(0, 0, 260, 160),
2125
+ });
2126
+
2127
+ const field = canvas.Widget{
2128
+ .id = 2,
2129
+ .kind = .text_field,
2130
+ .frame = geometry.RectF.init(12, 16, 120, 32),
2131
+ .semantics = .{ .label = "Name" },
2132
+ };
2133
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
2134
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{field} }, geometry.RectF.init(0, 0, 260, 160), &nodes);
2135
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
2136
+
2137
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2138
+ .window_id = 1,
2139
+ .label = "canvas",
2140
+ .kind = .pointer_down,
2141
+ .x = 20,
2142
+ .y = 30,
2143
+ } });
2144
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
2145
+
2146
+ // A short value never scrolls: the offset channel stays at zero.
2147
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .insert_text = "short" });
2148
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2149
+ try std.testing.expectEqual(@as(f32, 0), retained.nodes[1].widget.value);
2150
+
2151
+ // Typing past the field's span scrolls forward so the caret stays
2152
+ // inside the visible span; the emitted stream clips at the border.
2153
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .insert_text = " value far too long for this narrow field to show" });
2154
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2155
+ try std.testing.expect(retained.nodes[1].widget.value > 0);
2156
+ try std.testing.expect(canvas.textInputMaxHorizontalScrollOffsetForWidget(retained.nodes[1].widget, .{}) > 0);
2157
+ const viewport = canvas.textInputViewportForWidget(retained.nodes[1].widget, .{}).?;
2158
+ const scrolled_geometry = try harness.runtime.canvasWidgetTextGeometry(1, "canvas", 2);
2159
+ const scrolled_caret = scrolled_geometry.caret_bounds.?;
2160
+ try std.testing.expect(scrolled_caret.x >= viewport.x - 0.001);
2161
+ try std.testing.expect(scrolled_caret.maxX() <= viewport.maxX() + 0.001);
2162
+
2163
+ _ = try harness.runtime.emitCanvasWidgetDisplayList(1, "canvas", .{});
2164
+ const display_list = try harness.runtime.canvasDisplayList(1, "canvas");
2165
+ var saw_field_clip = false;
2166
+ for (display_list.commands) |command| {
2167
+ switch (command) {
2168
+ .push_clip => |clip| {
2169
+ if (clip.id == testCanvasWidgetPartId(2, 16)) {
2170
+ try std.testing.expectEqualDeep(viewport, clip.rect);
2171
+ saw_field_clip = true;
2172
+ }
2173
+ },
2174
+ else => {},
2175
+ }
2176
+ }
2177
+ try std.testing.expect(saw_field_clip);
2178
+
2179
+ // The retained offset survives a source rebuild (the reconcile pass
2180
+ // restores it alongside the text), and the clamp pass re-checks the
2181
+ // caret against the new geometry.
2182
+ var rebuild_nodes: [2]canvas.WidgetLayoutNode = undefined;
2183
+ const rebuild = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{field} }, geometry.RectF.init(0, 0, 260, 160), &rebuild_nodes);
2184
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", rebuild);
2185
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2186
+ try std.testing.expect(retained.nodes[1].widget.value > 0);
2187
+
2188
+ // Home: the caret returns to byte zero and the field scrolls all the
2189
+ // way back.
2190
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .move_caret = .{ .direction = .start } });
2191
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2192
+ try std.testing.expectEqualDeep(canvas.TextSelection.collapsed(0), retained.nodes[1].widget.text_selection.?);
2193
+ try std.testing.expectEqual(@as(f32, 0), retained.nodes[1].widget.value);
2194
+
2195
+ // Deleting the overflow clamps the offset back to zero — the field
2196
+ // never shows trailing emptiness while text could fill it.
2197
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .move_caret = .{ .direction = .end } });
2198
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2199
+ try std.testing.expect(retained.nodes[1].widget.value > 0);
2200
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .{ .set_selection = .{ .anchor = 5, .focus = 55 } });
2201
+ _ = try harness.runtime.editCanvasWidgetText(1, "canvas", 2, .delete_backward);
2202
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
2203
+ try std.testing.expectEqualStrings("short", retained.nodes[1].widget.text);
2204
+ try std.testing.expectEqual(@as(f32, 0), retained.nodes[1].widget.value);
2205
+ }
@@ -551,6 +551,9 @@ pub fn RuntimeFlow(comptime Runtime: type) type {
551
551
  }
552
552
 
553
553
  fn loadStartupWindows(self: *Runtime, app: App) anyerror!void {
554
+ // Scene-less startup always materializes the app's webview
555
+ // source into the main window; a native-only build cannot.
556
+ if (!self.options.web_layer) return error.WebViewLayerNotBuilt;
554
557
  const source = try WindowViewMethods().copyLoadedSource(self, try app.webViewSource());
555
558
  self.loaded_source = source;
556
559
  const app_info = self.options.platform.app_info;
@@ -592,6 +595,7 @@ pub fn RuntimeFlow(comptime Runtime: type) type {
592
595
  try WindowViewMethods().copyLoadedSource(self, try app.webViewSource())
593
596
  else
594
597
  null;
598
+ if (source != null and !self.options.web_layer) return error.WebViewLayerNotBuilt;
595
599
  self.loaded_source = source;
596
600
 
597
601
  try loadStartupSceneWindow(self, scene.windows[0], source);
@@ -661,12 +665,16 @@ pub fn RuntimeFlow(comptime Runtime: type) type {
661
665
  }
662
666
 
663
667
  fn loadWebView(self: *Runtime, app: App) anyerror!void {
668
+ if (!self.options.web_layer) return error.WebViewLayerNotBuilt;
664
669
  const source = try WindowViewMethods().copyLoadedSource(self, try app.webViewSource());
665
670
  self.loaded_source = source;
666
671
  try self.options.platform.services.loadWindowWebView(1, source);
667
672
  }
668
673
 
669
674
  pub fn reloadWindows(self: *Runtime, app: App) anyerror!void {
675
+ // Reload re-materializes webview sources; native-only builds
676
+ // have none to reload.
677
+ if (!self.options.web_layer) return error.WebViewLayerNotBuilt;
670
678
  const source = try WindowViewMethods().copyLoadedSource(self, try app.webViewSource());
671
679
  self.loaded_source = source;
672
680
  if (self.window_count == 0) {
@@ -4,5 +4,6 @@ test {
4
4
  _ = @import("window_command_bridge_tests.zig");
5
5
  _ = @import("native_command_event_tests.zig");
6
6
  _ = @import("webview_view_bridge_tests.zig");
7
+ _ = @import("web_layer_gate_tests.zig");
7
8
  _ = @import("bridge_permission_tests.zig");
8
9
  }
@@ -168,8 +168,10 @@ const RecordedSession = struct {
168
168
  /// a fetch and a spawn with results fed through the fake executor, an
169
169
  /// fx-timer fire via its platform timer event, and per-frame
170
170
  /// checkpoints. Returns the final model and fingerprint for the replay
171
- /// side to match.
172
- fn recordReferenceSession(gpa: std.mem.Allocator, buffer: *JournalBuffer) !RecordedSession {
171
+ /// side to match. `web_layer` mirrors the build's inference: the
172
+ /// reference session is a pure canvas app, so it must record (and
173
+ /// replay, below) identically in a native-only build.
174
+ fn recordReferenceSession(gpa: std.mem.Allocator, buffer: *JournalBuffer, web_layer: bool) !RecordedSession {
173
175
  const recorder = try std.heap.page_allocator.create(session_record.SessionRecorder);
174
176
  defer std.heap.page_allocator.destroy(recorder);
175
177
  recorder.* = session_record.SessionRecorder.init(buffer.sink());
@@ -178,6 +180,7 @@ fn recordReferenceSession(gpa: std.mem.Allocator, buffer: *JournalBuffer) !Recor
178
180
  const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
179
181
  defer harness.destroy(gpa);
180
182
  harness.null_platform.gpu_surfaces = true;
183
+ harness.runtime.options.web_layer = web_layer;
181
184
  harness.runtime.options.session_recorder = recorder;
182
185
 
183
186
  const app_state = try gpa.create(SessionApp);
@@ -248,7 +251,7 @@ fn recordReferenceSession(gpa: std.mem.Allocator, buffer: *JournalBuffer) !Recor
248
251
  };
249
252
  }
250
253
 
251
- fn replayIntoFreshApp(gpa: std.mem.Allocator, journal_bytes: []const u8) !struct {
254
+ fn replayIntoFreshApp(gpa: std.mem.Allocator, journal_bytes: []const u8, web_layer: bool) !struct {
252
255
  report: session_replay.ReplayReport,
253
256
  model: SessionModel,
254
257
  fingerprint: u64,
@@ -256,6 +259,7 @@ fn replayIntoFreshApp(gpa: std.mem.Allocator, journal_bytes: []const u8) !struct
256
259
  const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
257
260
  defer harness.destroy(gpa);
258
261
  harness.null_platform.gpu_surfaces = true;
262
+ harness.runtime.options.web_layer = web_layer;
259
263
 
260
264
  const app_state = try gpa.create(SessionApp);
261
265
  defer gpa.destroy(app_state);
@@ -278,7 +282,7 @@ test "a recorded session replays to identical model state and fingerprints" {
278
282
  const buffer = try std.heap.page_allocator.create(JournalBuffer);
279
283
  defer std.heap.page_allocator.destroy(buffer);
280
284
  buffer.len = 0;
281
- const recorded = try recordReferenceSession(gpa, buffer);
285
+ const recorded = try recordReferenceSession(gpa, buffer, true);
282
286
 
283
287
  // The recording captured real state.
284
288
  try std.testing.expectEqual(@as(u32, 2), recorded.model.count);
@@ -291,7 +295,7 @@ test "a recorded session replays to identical model state and fingerprints" {
291
295
  try std.testing.expectEqual(@as(u32, 1), recorded.model.spectrum_count);
292
296
  try std.testing.expect(recorded.model.band_checksum != 0);
293
297
 
294
- const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes());
298
+ const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes(), true);
295
299
  try std.testing.expect(replayed.report.ok());
296
300
  try std.testing.expect(replayed.report.events_replayed > 0);
297
301
  try std.testing.expectEqual(@as(u64, 5), replayed.report.effects_fed);
@@ -300,12 +304,35 @@ test "a recorded session replays to identical model state and fingerprints" {
300
304
  try std.testing.expectEqual(recorded.fingerprint, replayed.fingerprint);
301
305
  }
302
306
 
307
+ test "a native-only session records and replays like a web-layer one" {
308
+ // The whole reference session is canvas-only, so a native-only
309
+ // runtime (web_layer = false, the app-runner inference for an
310
+ // app.zon with no web declaration) must journal and replay it
311
+ // byte-for-byte equivalently: same final model, same fingerprint,
312
+ // same checkpoint verification. Record/replay is part of the
313
+ // native-only contract, not a web-layer feature.
314
+ const gpa = std.testing.allocator;
315
+ const buffer = try std.heap.page_allocator.create(JournalBuffer);
316
+ defer std.heap.page_allocator.destroy(buffer);
317
+ buffer.len = 0;
318
+ const recorded = try recordReferenceSession(gpa, buffer, false);
319
+ try std.testing.expectEqual(@as(u32, 2), recorded.model.count);
320
+ try std.testing.expectEqualStrings("hello-from-the-network", recorded.model.bodyText());
321
+
322
+ const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes(), false);
323
+ try std.testing.expect(replayed.report.ok());
324
+ try std.testing.expect(replayed.report.events_replayed > 0);
325
+ try std.testing.expect(replayed.report.checkpoints_verified > 0);
326
+ try std.testing.expectEqualDeep(recorded.model, replayed.model);
327
+ try std.testing.expectEqual(recorded.fingerprint, replayed.fingerprint);
328
+ }
329
+
303
330
  test "a truncated journal is refused loudly" {
304
331
  const gpa = std.testing.allocator;
305
332
  const buffer = try std.heap.page_allocator.create(JournalBuffer);
306
333
  defer std.heap.page_allocator.destroy(buffer);
307
334
  buffer.len = 0;
308
- _ = try recordReferenceSession(gpa, buffer);
335
+ _ = try recordReferenceSession(gpa, buffer, true);
309
336
  const whole = buffer.journalBytes();
310
337
 
311
338
  const harness = try core.TestHarness().create(gpa, .{ .size = geometry.SizeF.init(400, 300) });
@@ -328,7 +355,7 @@ test "a tampered effect payload fails verification loudly" {
328
355
  const buffer = try std.heap.page_allocator.create(JournalBuffer);
329
356
  defer std.heap.page_allocator.destroy(buffer);
330
357
  buffer.len = 0;
331
- _ = try recordReferenceSession(gpa, buffer);
358
+ _ = try recordReferenceSession(gpa, buffer, true);
332
359
 
333
360
  // Flip one byte inside the journaled fetch body: framing stays
334
361
  // valid, so the tamper is only detectable semantically — the
@@ -337,7 +364,7 @@ test "a tampered effect payload fails verification loudly" {
337
364
  const at = std.mem.indexOf(u8, bytes, "hello-from-the-network") orelse unreachable;
338
365
  bytes[at] ^= 0x20;
339
366
 
340
- const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes());
367
+ const replayed = try replayIntoFreshApp(gpa, buffer.journalBytes(), true);
341
368
  try std.testing.expect(!replayed.report.ok());
342
369
  try std.testing.expect(replayed.report.mismatch_count >= 1);
343
370
  try std.testing.expectEqual(session_replay.ReplayMismatchKind.fingerprint, replayed.report.mismatches[0].kind);
@@ -516,7 +516,7 @@ pub const RuntimeView = struct {
516
516
  pub const stepCanvasWidgetKineticScroll = CanvasWidgetScrollMethods.stepCanvasWidgetKineticScroll;
517
517
  pub const canvasWidgetScrollContentExtent = CanvasWidgetScrollMethods.canvasWidgetScrollContentExtent;
518
518
  pub const translateCanvasWidgetScrollDescendants = CanvasWidgetScrollMethods.translateCanvasWidgetScrollDescendants;
519
- pub const scrollCanvasTextareaCaretIntoView = CanvasWidgetScrollMethods.scrollCanvasTextareaCaretIntoView;
519
+ pub const scrollCanvasTextInputCaretIntoView = CanvasWidgetScrollMethods.scrollCanvasTextInputCaretIntoView;
520
520
 
521
521
  const CanvasWidgetControlMethods = view_widget_control.RuntimeViewCanvasWidgetControl(RuntimeView);
522
522
  pub const canvasWidgetToggleAnimation = CanvasWidgetControlMethods.canvasWidgetToggleAnimation;
@@ -7,6 +7,7 @@ const canvas_widget_runtime = @import("canvas_widget_runtime.zig");
7
7
  const unionRects = canvas_frame_helpers.unionRects;
8
8
  const CanvasWidgetScrollKeyboardTarget = canvas_widget_runtime.CanvasWidgetScrollKeyboardTarget;
9
9
  const canvasWidgetScrollableKind = canvas_widget_runtime.canvasWidgetScrollableKind;
10
+ const canvasWidgetSingleLineTextKind = canvas_widget_runtime.canvasWidgetSingleLineTextKind;
10
11
 
11
12
  pub const CanvasWidgetScrollSource = enum {
12
13
  discrete,
@@ -335,9 +336,19 @@ pub fn RuntimeViewCanvasWidgetScroll(comptime RuntimeView: type) type {
335
336
  }
336
337
  }
337
338
 
338
- pub fn scrollCanvasTextareaCaretIntoView(self: *RuntimeView, index: usize) void {
339
+ /// Keep the editable field's caret inside its visible span after
340
+ /// a text or caret change: textareas scroll vertically, single-
341
+ /// line fields horizontally — both through the widget's retained
342
+ /// `value` offset channel.
343
+ pub fn scrollCanvasTextInputCaretIntoView(self: *RuntimeView, index: usize) void {
339
344
  if (index >= self.widget_layout_node_count) return;
340
345
  var widget = self.widget_layout_nodes[index].widget;
346
+ if (canvasWidgetSingleLineTextKind(widget.kind)) {
347
+ const next_offset = canvas.textInputCaretVisibleScrollOffsetForWidget(widget, self.widget_tokens, widget.value);
348
+ if (next_offset == widget.value) return;
349
+ self.widget_layout_nodes[index].widget.value = next_offset;
350
+ return;
351
+ }
341
352
  if (widget.kind != .textarea) return;
342
353
 
343
354
  const viewport = canvas.textInputViewportForWidget(widget, self.widget_tokens) orelse return;
@@ -37,7 +37,7 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
37
37
  if (canvasWidgetTextEditUnchanged(current_state, next_state)) return null;
38
38
 
39
39
  try self.rewriteCanvasWidgetTextStorage(index, next_state);
40
- self.scrollCanvasTextareaCaretIntoView(index);
40
+ self.scrollCanvasTextInputCaretIntoView(index);
41
41
  const semantics = try self.widgetLayoutTree().collectSemantics(&self.widget_semantics_nodes);
42
42
  self.widget_semantics_node_count = semantics.len;
43
43
  self.widget_revision += 1;
@@ -98,6 +98,10 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
98
98
 
99
99
  self.widget_layout_nodes[index].widget.text_selection = next_selection;
100
100
  self.widget_layout_nodes[index].widget.text_composition = null;
101
+ // A pointer-placed caret is a caret change like any other: a
102
+ // drag past a scrolled single-line field's edge lands on an
103
+ // off-screen offset, and the field follows it.
104
+ if (canvasWidgetSingleLineTextKind(widget.kind)) self.scrollCanvasTextInputCaretIntoView(index);
101
105
  try self.refreshCanvasWidgetSemantics();
102
106
  self.widget_revision += 1;
103
107
  return self.canvasWidgetDirtyBounds(index, widget.frame);
@@ -266,7 +270,7 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
266
270
  .selection = canvas.TextSelection.collapsed(text.len),
267
271
  .composition = null,
268
272
  });
269
- self.scrollCanvasTextareaCaretIntoView(index);
273
+ self.scrollCanvasTextInputCaretIntoView(index);
270
274
  try self.refreshCanvasWidgetSemantics();
271
275
  self.widget_revision += 1;
272
276
  return self.canvasWidgetDirtyBounds(index, self.widget_layout_nodes[index].frame);