@native-sdk/cli 0.6.3 → 0.7.1

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 (64) hide show
  1. package/build.zig +65 -91
  2. package/package.json +9 -9
  3. package/packages/core/package-lock.json +2 -2
  4. package/packages/core/package.json +1 -1
  5. package/packages/core/sdk/text.ts +25 -14
  6. package/packages/core/src/checker.ts +39 -0
  7. package/packages/core/src/emitter.ts +125 -1
  8. package/skill-data/native-ui/SKILL.md +23 -6
  9. package/src/platform/macos/appkit_host.m +25 -1
  10. package/src/platform/macos/cef_host.mm +24 -2
  11. package/src/platform/macos/root.zig +13 -0
  12. package/src/primitives/canvas/code.zig +1008 -0
  13. package/src/primitives/canvas/code_tests.zig +1912 -0
  14. package/src/primitives/canvas/events.zig +111 -1
  15. package/src/primitives/canvas/layout_audit.zig +3 -1
  16. package/src/primitives/canvas/markdown.zig +13 -11
  17. package/src/primitives/canvas/markdown_tests.zig +168 -6
  18. package/src/primitives/canvas/root.zig +8 -2
  19. package/src/primitives/canvas/tests.zig +1 -0
  20. package/src/primitives/canvas/text.zig +6 -0
  21. package/src/primitives/canvas/text_measure_cache.zig +4 -4
  22. package/src/primitives/canvas/text_span_tests.zig +254 -0
  23. package/src/primitives/canvas/text_spans.zig +556 -32
  24. package/src/primitives/canvas/themes/geist.zig +29 -0
  25. package/src/primitives/canvas/tokens.zig +72 -2
  26. package/src/primitives/canvas/ui.zig +400 -22
  27. package/src/primitives/canvas/ui_markup.zig +78 -7
  28. package/src/primitives/canvas/ui_markup_compiled.zig +89 -0
  29. package/src/primitives/canvas/ui_markup_compiled_tests.zig +45 -0
  30. package/src/primitives/canvas/ui_markup_contract.zig +41 -0
  31. package/src/primitives/canvas/ui_markup_view.zig +89 -0
  32. package/src/primitives/canvas/ui_markup_view_tests.zig +154 -3
  33. package/src/primitives/canvas/ui_schema.zig +30 -6
  34. package/src/primitives/canvas/ui_schema_tests.zig +13 -10
  35. package/src/primitives/canvas/ui_tests.zig +26 -0
  36. package/src/primitives/canvas/widget_invalidation.zig +4 -0
  37. package/src/primitives/canvas/widget_layout.zig +57 -8
  38. package/src/primitives/canvas/widget_layout_tests.zig +35 -0
  39. package/src/primitives/canvas/widget_metrics.zig +35 -1
  40. package/src/primitives/canvas/widget_render.zig +1077 -11
  41. package/src/primitives/canvas/widget_render_controls.zig +4 -4
  42. package/src/primitives/canvas/widget_runtime.zig +2 -0
  43. package/src/primitives/canvas/widget_text_input.zig +165 -8
  44. package/src/primitives/canvas/widget_text_select.zig +2 -2
  45. package/src/primitives/canvas/widget_tree.zig +6 -5
  46. package/src/primitives/canvas/widgets.zig +38 -0
  47. package/src/root.zig +1 -0
  48. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  49. package/src/runtime/canvas_limits.zig +13 -6
  50. package/src/runtime/canvas_widget_clipboard_tests.zig +137 -4
  51. package/src/runtime/canvas_widget_context_menu.zig +17 -2
  52. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  53. package/src/runtime/canvas_widget_events.zig +58 -21
  54. package/src/runtime/canvas_widget_runtime.zig +112 -6
  55. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  56. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  57. package/src/runtime/canvas_widget_state.zig +27 -0
  58. package/src/runtime/canvas_widget_text_tests.zig +383 -0
  59. package/src/runtime/core.zig +15 -5
  60. package/src/runtime/gpu_surface_events.zig +12 -12
  61. package/src/runtime/view.zig +61 -19
  62. package/src/runtime/view_widget_scroll.zig +48 -18
  63. package/src/runtime/view_widget_text.zig +248 -17
  64. package/src/runtime/window_view_runtime.zig +9 -0
@@ -30,11 +30,17 @@ const geometry = @import("geometry");
30
30
  const canvas = @import("canvas");
31
31
  const platform = @import("../platform/root.zig");
32
32
  const runtime_api = @import("api.zig");
33
+ const canvas_limits = @import("canvas_limits.zig");
33
34
  const canvas_widget_runtime = @import("canvas_widget_runtime.zig");
34
35
  const runtime_canvas_widget_events = @import("canvas_widget_events.zig");
35
36
 
36
37
  const context_menu_log = std.log.scoped(.zero_context_menu);
37
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
+
38
44
  /// Reserved item ids of the zero-code default menus.
39
45
  pub const default_item_cut: u32 = 1;
40
46
  pub const default_item_copy: u32 = 2;
@@ -225,7 +231,15 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
225
231
 
226
232
  // 4. Static text with a live selection: Copy only.
227
233
  const selected_id = self.views[index].canvas_widget_selected_text_id;
228
- if (selected_id != 0 and selected_id == target.id) {
234
+ const selected_group_target = if (selected_id != 0 and selected_id != target.id) blk: {
235
+ const selected_index = self.views[index].canvasWidgetNodeIndexById(selected_id) orelse break :blk false;
236
+ const target_index = self.views[index].canvasWidgetNodeIndexById(target.id) orelse break :blk false;
237
+ const selected_widget = self.views[index].widget_layout_nodes[selected_index].widget;
238
+ const target_widget = self.views[index].widget_layout_nodes[target_index].widget;
239
+ break :blk selected_widget.static_text_group_id != 0 and
240
+ selected_widget.static_text_group_id == target_widget.static_text_group_id;
241
+ } else false;
242
+ if (selected_id != 0 and (selected_id == target.id or selected_group_target)) {
229
243
  if (!has_presenter) return;
230
244
  items[0] = .{ .id = default_item_copy, .label = "Copy" };
231
245
  _ = try showMenu(self, app, index, .{
@@ -389,7 +403,8 @@ pub fn RuntimeCanvasWidgetContextMenu(comptime Runtime: type) type {
389
403
  .terminal => try applyDefaultTerminalAction(self, app, index, pending.target_id, event.item_id),
390
404
  .static_copy => {
391
405
  if (event.item_id != default_item_copy) return;
392
- const text = self.views[index].canvasWidgetCopyText() 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;
393
408
  self.writeClipboard(text) catch return;
394
409
  },
395
410
  }
@@ -627,7 +627,7 @@ test "only a focused live terminal owns Tab and focus entry suppresses the whole
627
627
  try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
628
628
  try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.target_id);
629
629
  try std.testing.expect(app_state.focus_moved);
630
- try std.testing.expect(harness.runtime.views[0].canvas_widget_terminal_focus_entry_tab_held);
630
+ try std.testing.expect(harness.runtime.views[0].canvas_widget_tab_input_focus_entry_held);
631
631
 
632
632
  // Auto-repeat while Tab is held, then its key-up: neither produces
633
633
  // another widget event at the terminal.
@@ -646,7 +646,7 @@ test "only a focused live terminal owns Tab and focus entry suppresses the whole
646
646
  .modifiers = .{ .shift = true },
647
647
  } });
648
648
  try std.testing.expectEqual(@as(u32, 3), app_state.keyboard_count);
649
- try std.testing.expect(!harness.runtime.views[0].canvas_widget_terminal_focus_entry_tab_held);
649
+ try std.testing.expect(!harness.runtime.views[0].canvas_widget_tab_input_focus_entry_held);
650
650
 
651
651
  // The same nonzero binding after session exit is no longer an input
652
652
  // owner: its published grid says the session cannot accept input,
@@ -2407,12 +2407,14 @@ test "a target-less composition stays target-less when a text field takes focus
2407
2407
  // The composition STARTS target-less (nothing focused): the preedit
2408
2408
  // buffers for the surface's own consumer.
2409
2409
  harness.runtime.views[0].canvas_widget_focused_id = 0;
2410
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2411
- .window_id = 1,
2412
- .label = "canvas",
2413
- .kind = .ime_set_composition,
2414
- .text = "\xe3\x81\x8b", // か
2415
- } });
2410
+ try harness.runtime.dispatchPlatformEvent(app, .{
2411
+ .gpu_surface_input = .{
2412
+ .window_id = 1,
2413
+ .label = "canvas",
2414
+ .kind = .ime_set_composition,
2415
+ .text = "\xe3\x81\x8b", // か
2416
+ },
2417
+ });
2416
2418
  try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
2417
2419
 
2418
2420
  // Focus lands on a text field mid-sequence. The empty commit
@@ -2600,12 +2602,14 @@ test "target-less IME preedit is per-surface and command chords never commit tex
2600
2602
  harness.runtime.views[1].focused = true;
2601
2603
 
2602
2604
  // Surface A composes; the preedit is provisional — nothing commits.
2603
- try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
2604
- .window_id = 1,
2605
- .label = "canvas-a",
2606
- .kind = .ime_set_composition,
2607
- .text = "\xe3\x81\x8b", // か
2608
- } });
2605
+ try harness.runtime.dispatchPlatformEvent(app, .{
2606
+ .gpu_surface_input = .{
2607
+ .window_id = 1,
2608
+ .label = "canvas-a",
2609
+ .kind = .ime_set_composition,
2610
+ .text = "\xe3\x81\x8b", // か
2611
+ },
2612
+ });
2609
2613
  try std.testing.expectEqual(@as(u32, 0), app_state.committed_count);
2610
2614
 
2611
2615
  // THE per-surface pin: surface B's empty commit must NOT insert
@@ -103,6 +103,12 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
103
103
  }
104
104
  if (raw.kind != .text) return press_target;
105
105
  if (self.views[view_index].canvas_widget_selected_text_id != raw.id) return press_target;
106
+ if (self.views[view_index].canvas_widget_selected_text_group_id != 0 and
107
+ self.views[view_index].canvas_widget_selected_text_group_anchor !=
108
+ self.views[view_index].canvas_widget_selected_text_group_focus)
109
+ {
110
+ return null;
111
+ }
106
112
  const node = self.views[view_index].widgetLayoutTree().findById(raw.id) orelse return press_target;
107
113
  const selection = node.widget.text_selection orelse return press_target;
108
114
  if (selection.isCollapsed(node.widget.text.len)) return press_target;
@@ -324,14 +330,27 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
324
330
  // menu path before reaching here).
325
331
  self.views[index].canvas_widget_click_count = 0;
326
332
  self.views[index].canvas_widget_click_timestamp_ns = 0;
333
+ self.views[index].canvas_widget_click_pointer_id = 0;
334
+ self.views[index].canvas_widget_click_target_id = 0;
327
335
  return;
328
336
  }
329
337
  const previous_count = self.views[index].canvas_widget_click_count;
330
338
  const previous_timestamp = self.views[index].canvas_widget_click_timestamp_ns;
331
339
  const previous_point = self.views[index].canvas_widget_click_point;
340
+ const previous_pointer_id = self.views[index].canvas_widget_click_pointer_id;
341
+ const previous_target_id = self.views[index].canvas_widget_click_target_id;
332
342
  const point = pointer_event.pointer.point;
343
+ const pointer_id = pointer_event.pointer.pointer_id;
344
+ const target_id: canvas.ObjectId = if (pointer_event.press_target) |target|
345
+ target.id
346
+ else if (pointer_event.target) |target|
347
+ target.id
348
+ else
349
+ 0;
333
350
  const chained = previous_count != 0 and
334
351
  previous_timestamp != 0 and
352
+ pointer_id == previous_pointer_id and
353
+ target_id == previous_target_id and
335
354
  input_event.timestamp_ns >= previous_timestamp and
336
355
  input_event.timestamp_ns - previous_timestamp <= canvas_widget_multi_click_interval_ns and
337
356
  @abs(point.x - previous_point.x) <= canvas_widget_multi_click_slop and
@@ -342,6 +361,8 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
342
361
  self.views[index].canvas_widget_click_count = count;
343
362
  self.views[index].canvas_widget_click_timestamp_ns = input_event.timestamp_ns;
344
363
  self.views[index].canvas_widget_click_point = point;
364
+ self.views[index].canvas_widget_click_pointer_id = pointer_id;
365
+ self.views[index].canvas_widget_click_target_id = target_id;
345
366
  pointer_event.pointer.click_count = count;
346
367
  },
347
368
  .move, .up => {
@@ -1952,7 +1973,7 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
1952
1973
 
1953
1974
  switch (action) {
1954
1975
  .copy => {
1955
- const text = self.views[index].canvasWidgetCopyText() orelse return;
1976
+ const text = self.views[index].canvasWidgetCopyText(paste_buffer) orelse return;
1956
1977
  self.writeClipboard(text) catch return;
1957
1978
  },
1958
1979
  .cut => {
@@ -2064,6 +2085,12 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
2064
2085
  };
2065
2086
  keyboard_event.keyboard.edit = edit;
2066
2087
 
2088
+ if (self.views[index].canvasWidgetTextEditNeedsLargeStorage(target.id, edit)) {
2089
+ for (self.views[0 .. index + 1]) |*view| {
2090
+ try view.ensureLargeCanvasWidgetTextStorage(self.owned_allocator);
2091
+ }
2092
+ }
2093
+
2067
2094
  const dirty = if (keyboard_event.history_replay)
2068
2095
  try self.views[index].applyCanvasWidgetTextEditWithoutHistory(target.id, edit) orelse return
2069
2096
  else
@@ -2364,17 +2391,17 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
2364
2391
  }
2365
2392
 
2366
2393
  /// Returns true for an auto-repeat or release belonging to the
2367
- /// physical Tab gesture that moved focus into a live terminal.
2394
+ /// physical Tab gesture that moved focus into a Tab-owning editor.
2368
2395
  /// The caller suppresses that transition; the release also
2369
2396
  /// retires the gesture latch.
2370
- pub fn consumeCanvasWidgetTerminalFocusEntryTab(self: *Runtime, input_event: GpuSurfaceInputEvent) bool {
2397
+ pub fn consumeCanvasWidgetTabInputFocusEntry(self: *Runtime, input_event: GpuSurfaceInputEvent) bool {
2371
2398
  if (input_event.kind != .key_down and input_event.kind != .key_up) return false;
2372
2399
  if (!std.ascii.eqlIgnoreCase(input_event.key, "tab")) return false;
2373
2400
  const index = runtimeFindViewIndex(self, input_event.window_id, input_event.label) orelse return false;
2374
2401
  if (self.views[index].kind != .gpu_surface) return false;
2375
- if (!self.views[index].canvas_widget_terminal_focus_entry_tab_held) return false;
2402
+ if (!self.views[index].canvas_widget_tab_input_focus_entry_held) return false;
2376
2403
  if (input_event.kind == .key_up) {
2377
- self.views[index].canvas_widget_terminal_focus_entry_tab_held = false;
2404
+ self.views[index].canvas_widget_tab_input_focus_entry_held = false;
2378
2405
  }
2379
2406
  return true;
2380
2407
  }
@@ -2409,18 +2436,21 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
2409
2436
  const current_id: ?canvas.ObjectId = if (self.views[index].canvas_widget_focused_id == 0) null else self.views[index].canvas_widget_focused_id;
2410
2437
  const layout = self.views[index].widgetLayoutTree();
2411
2438
  if (std.ascii.eqlIgnoreCase(input_event.key, "tab")) {
2412
- // A terminal owns Tab as INPUT (completion, indentation,
2413
- // and TUI navigation), not as focus traversal. Decide
2414
- // against the pre-key focus before choosing the next
2415
- // target: the routed keyboard pass below will therefore
2416
- // address this same terminal and its emulator can encode
2417
- // Tab/Shift+Tab for the pty. Disabled/hidden terminals
2418
- // cannot occupy the focus register because
2419
- // focusTargetById rejects them, so stale focus still
2420
- // falls through to the ordinary traversal repair.
2439
+ // Live terminals own Tab/Shift+Tab as input; editable code
2440
+ // owns plain Tab as indentation while Shift+Tab remains an
2441
+ // accessible way to leave the editor.
2421
2442
  if (current_id) |id| {
2422
2443
  if (layout.focusTargetById(id)) |current| {
2423
2444
  if (canvasWidgetTerminalOwnsTabInput(layout, current)) return false;
2445
+ if (!input_event.modifiers.shift and
2446
+ !input_event.modifiers.primary and
2447
+ !input_event.modifiers.command and
2448
+ !input_event.modifiers.control and
2449
+ !input_event.modifiers.option and
2450
+ canvasWidgetCodeEditorOwnsTabInput(layout, current))
2451
+ {
2452
+ return false;
2453
+ }
2424
2454
  }
2425
2455
  }
2426
2456
  const direction: canvas.WidgetFocusDirection = if (input_event.modifiers.shift) .backward else .forward;
@@ -2429,13 +2459,14 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
2429
2459
  else
2430
2460
  layout.focusTarget(current_id, direction) orelse return false;
2431
2461
  const moved = try setCanvasWidgetFocusFromKeyboardMoved(self, index, current_id, target.id);
2432
- if (moved and canvasWidgetTerminalOwnsTabInput(layout, target)) {
2433
- // The key-down moved focus INTO this live terminal;
2434
- // it is traversal, not terminal input. Hold that
2435
- // classification through auto-repeat and key-up so
2436
- // the same physical gesture cannot leak a Tab press
2437
- // (legacy) or orphan release (kitty) into the pty.
2438
- self.views[index].canvas_widget_terminal_focus_entry_tab_held = true;
2462
+ if (moved and
2463
+ (canvasWidgetTerminalOwnsTabInput(layout, target) or
2464
+ canvasWidgetCodeEditorOwnsTabInput(layout, target)))
2465
+ {
2466
+ // The key-down entered this input owner through focus
2467
+ // traversal. Hold that classification through repeats
2468
+ // and key-up so the gesture never becomes editor input.
2469
+ self.views[index].canvas_widget_tab_input_focus_entry_held = true;
2439
2470
  }
2440
2471
  return moved;
2441
2472
  }
@@ -2581,6 +2612,12 @@ fn canvasWidgetTerminalOwnsTabInput(layout: canvas.WidgetLayoutTree, target: can
2581
2612
  return grid.running;
2582
2613
  }
2583
2614
 
2615
+ fn canvasWidgetCodeEditorOwnsTabInput(layout: canvas.WidgetLayoutTree, target: canvas.WidgetFocusTarget) bool {
2616
+ if (target.kind != .textarea or target.index >= layout.nodes.len) return false;
2617
+ const widget = layout.nodes[target.index].widget;
2618
+ return widget.code_editor and !widget.state.disabled;
2619
+ }
2620
+
2584
2621
  fn canvasDirtyRegionForView(view_frame: geometry.RectF, local_dirty: geometry.RectF) ?geometry.RectF {
2585
2622
  const normalized_view = view_frame.normalized();
2586
2623
  const surface_bounds = geometry.RectF.init(0, 0, normalized_view.width, normalized_view.height);
@@ -149,6 +149,7 @@ pub const CanvasWidgetTextReconcileEntry = struct {
149
149
  id: canvas.ObjectId = 0,
150
150
  kind: canvas.WidgetKind = .text_field,
151
151
  text: []const u8 = &.{},
152
+ static_text_group_id: canvas.ObjectId = 0,
152
153
  source_text_len: usize = 0,
153
154
  source_text_hash: u64 = 0,
154
155
  /// The selection declared by the previous SOURCE tree, distinct from
@@ -160,6 +161,11 @@ pub const CanvasWidgetTextReconcileEntry = struct {
160
161
  text_selection: ?canvas.TextSelection = null,
161
162
  text_composition: ?canvas.TextRange = null,
162
163
  value: f32 = 0,
164
+ value_x: f32 = 0,
165
+ code_content_width: f32 = 0,
166
+ code_content_width_generation: u64 = 0,
167
+ code_content_width_font_id: u64 = 0,
168
+ code_content_width_size_bits: u32 = 0,
163
169
  };
164
170
 
165
171
  pub const CanvasWidgetSourceScrollEntry = struct {
@@ -247,6 +253,54 @@ pub fn restoreCanvasWidgetLayoutScrollOffsets(
247
253
  }
248
254
  }
249
255
 
256
+ fn previousLayoutHasSelectedTab(previous: canvas.WidgetLayoutTree, id: canvas.ObjectId) bool {
257
+ for (previous.nodes) |node| {
258
+ if (node.widget.id == id) return node.widget.semantics.role == .tab and node.widget.state.selected;
259
+ }
260
+ return false;
261
+ }
262
+
263
+ /// A tab activation reveals only the pixels outside its standing horizontal
264
+ /// viewport. This runs after retained scroll restoration, against final layout
265
+ /// frames, so an already-visible tab preserves the exact offset and a newly
266
+ /// mounted native scroll driver never paints a speculative leading-edge jump.
267
+ fn revealNewlySelectedTabs(previous: canvas.WidgetLayoutTree, nodes: []canvas.WidgetLayoutNode) void {
268
+ for (nodes, 0..) |tab_node, tab_index| {
269
+ const tab = tab_node.widget;
270
+ if (tab.semantics.role != .tab or !tab.state.selected) continue;
271
+ if (tab.id != 0 and previousLayoutHasSelectedTab(previous, tab.id)) continue;
272
+
273
+ var ancestor = tab_node.parent_index;
274
+ while (ancestor) |index| {
275
+ if (index >= nodes.len) break;
276
+ const scroll_node = nodes[index];
277
+ if (scroll_node.widget.kind == .scroll_view and
278
+ canvas.widgetScrollsAxis(scroll_node.widget, .horizontal) and
279
+ !scroll_node.widget.layout.virtualized)
280
+ {
281
+ const viewport = scroll_node.frame.inset(scroll_node.widget.layout.padding).normalized();
282
+ const tab_frame = nodes[tab_index].frame.normalized();
283
+ if (viewport.isEmpty() or tab_frame.isEmpty()) break;
284
+
285
+ const delta = if (tab_frame.x < viewport.x)
286
+ tab_frame.x - viewport.x
287
+ else if (tab_frame.maxX() > viewport.maxX())
288
+ tab_frame.maxX() - viewport.maxX()
289
+ else
290
+ 0;
291
+ if (delta == 0) break;
292
+ const current = scroll_node.widget.value_x;
293
+ const next = @max(0, current + delta);
294
+ if (next == current) break;
295
+ nodes[index].widget.value_x = next;
296
+ translateCanvasWidgetLayoutScrollDescendants(nodes, index, .{ .dx = -(next - current) });
297
+ break;
298
+ }
299
+ ancestor = scroll_node.parent_index;
300
+ }
301
+ }
302
+ }
303
+
250
304
  pub const CanvasWidgetSourceTextEntry = struct {
251
305
  id: canvas.ObjectId = 0,
252
306
  kind: canvas.WidgetKind = .text_field,
@@ -604,12 +658,18 @@ pub fn collectCanvasWidgetTextReconcileEntries(
604
658
  .id = node.widget.id,
605
659
  .kind = node.widget.kind,
606
660
  .text = text_storage[text_range.start..text_range.end],
661
+ .static_text_group_id = node.widget.static_text_group_id,
607
662
  .source_text_len = source_text.len,
608
663
  .source_text_hash = source_text.hash,
609
664
  .source_text_selection = if (source_entry) |entry| entry.text_selection else null,
610
665
  .text_selection = node.widget.text_selection,
611
666
  .text_composition = node.widget.text_composition,
612
667
  .value = node.widget.value,
668
+ .value_x = node.widget.value_x,
669
+ .code_content_width = node.widget.code_content_width,
670
+ .code_content_width_generation = node.widget.code_content_width_generation,
671
+ .code_content_width_font_id = node.widget.code_content_width_font_id,
672
+ .code_content_width_size_bits = node.widget.code_content_width_size_bits,
613
673
  };
614
674
  len += 1;
615
675
  }
@@ -783,9 +843,12 @@ pub fn canvasWidgetLayoutNodeWithTextReconcileState(
783
843
 
784
844
  if (copy.widget.kind == .text) {
785
845
  // Static text selections survive rebuilds only while the source
786
- // text is byte-identical; changed text drops the selection.
846
+ // text and its optional grouped document are byte-identical.
787
847
  if (previous.firstWithKind(copy.widget.id, copy.widget.kind)) |entry| {
788
- if (copy.widget.text_selection == null and std.mem.eql(u8, entry.text, copy.widget.text)) {
848
+ if (copy.widget.text_selection == null and
849
+ std.mem.eql(u8, entry.text, copy.widget.text) and
850
+ entry.static_text_group_id == copy.widget.static_text_group_id)
851
+ {
789
852
  copy.widget.text_selection = entry.text_selection;
790
853
  }
791
854
  }
@@ -814,6 +877,13 @@ pub fn canvasWidgetLayoutNodeWithTextReconcileState(
814
877
  // right after, so a resized or re-texted field never keeps a
815
878
  // stale offset).
816
879
  if (canvasWidgetEditableTextKind(copy.widget.kind)) copy.widget.value = entry.value;
880
+ if (copy.widget.code_editor) {
881
+ copy.widget.value_x = entry.value_x;
882
+ copy.widget.code_content_width = entry.code_content_width;
883
+ copy.widget.code_content_width_generation = entry.code_content_width_generation;
884
+ copy.widget.code_content_width_font_id = entry.code_content_width_font_id;
885
+ copy.widget.code_content_width_size_bits = entry.code_content_width_size_bits;
886
+ }
817
887
  if (copy.widget.text_selection == null and copy.widget.text_composition == null) {
818
888
  copy.widget.text_selection = entry.text_selection;
819
889
  copy.widget.text_composition = entry.text_composition;
@@ -963,6 +1033,7 @@ pub fn canvasWidgetLayoutTreeWithRuntimeReconcileState(
963
1033
  // the caller AFTER native scroll drivers are stamped — a rebuild
964
1034
  // mid-rubber-band must not clamp an offset the OS scroller owns.
965
1035
  restoreCanvasWidgetLayoutScrollOffsets(staged_nodes, previous_runtime_offsets, previous_source_scroll_entries);
1036
+ revealNewlySelectedTabs(previous, staged_nodes);
966
1037
 
967
1038
  const index_scratch = canvas_widget_reconcile_index_scratch.get();
968
1039
  index_scratch.controls.build(previous_control_states);
@@ -1097,7 +1168,12 @@ pub fn clampCanvasWidgetLayoutScrollOffsets(nodes: []canvas.WidgetLayoutNode, st
1097
1168
  pub fn clampCanvasWidgetLayoutTextOffsets(nodes: []canvas.WidgetLayoutNode, tokens: canvas.DesignTokens) void {
1098
1169
  for (nodes) |*node| {
1099
1170
  if (node.widget.kind == .textarea) {
1171
+ canvas.cacheTextInputContentWidthForWidget(&node.widget, tokens);
1100
1172
  node.widget.value = canvas.clampedTextInputScrollOffsetForWidget(node.widget, tokens, node.widget.value);
1173
+ node.widget.value_x = if (node.widget.code_editor)
1174
+ canvas.clampedTextInputHorizontalScrollOffsetForWidget(node.widget, tokens, node.widget.value_x)
1175
+ else
1176
+ 0;
1101
1177
  continue;
1102
1178
  }
1103
1179
  if (!canvasWidgetSingleLineTextKind(node.widget.kind)) continue;
@@ -1494,8 +1570,10 @@ fn canvasWidgetTreeRowFocusTarget(layout: canvas.WidgetLayoutTree, node_index: u
1494
1570
  /// the focused row is a leaf or collapsed (an expanded row collapses
1495
1571
  /// instead — no focus move, the routed toggle intent handles it); Right
1496
1572
  /// moves to the FIRST CHILD row when the focused row is expanded (a
1497
- /// collapsed row expands instead). Null = no tree move (the caller
1498
- /// falls through to group/spatial focus).
1573
+ /// collapsed row expands instead). Flat sibling rows may declare a
1574
+ /// one-based `tree_level`; otherwise parent/child relationships derive
1575
+ /// from widget nesting. Null = no tree move (the caller falls through to
1576
+ /// group/spatial focus).
1499
1577
  pub fn canvasWidgetTreeDirectionalFocusTarget(
1500
1578
  layout: canvas.WidgetLayoutTree,
1501
1579
  focused: canvas.WidgetFocusTarget,
@@ -1515,7 +1593,7 @@ pub fn canvasWidgetTreeDirectionalFocusTarget(
1515
1593
  .right => blk: {
1516
1594
  const expanded = layout.nodes[focused.index].widget.state.expanded orelse false;
1517
1595
  if (!expanded) break :blk null; // expand intent (or leaf no-op)
1518
- break :blk canvasWidgetTreeFirstChildRow(layout, focused.index) orelse focused;
1596
+ break :blk canvasWidgetTreeFirstChildRow(layout, tree_index, focused.index) orelse focused;
1519
1597
  },
1520
1598
  .forward, .backward => null,
1521
1599
  };
@@ -1573,6 +1651,20 @@ fn canvasWidgetTreeAdjacentRow(
1573
1651
  }
1574
1652
 
1575
1653
  fn canvasWidgetTreeParentRow(layout: canvas.WidgetLayoutTree, tree_index: usize, focused_index: usize) ?canvas.WidgetFocusTarget {
1654
+ const focused_level = layout.nodes[focused_index].widget.tree_level;
1655
+ if (focused_level > 1) {
1656
+ var index = focused_index;
1657
+ while (index > tree_index + 1) {
1658
+ index -= 1;
1659
+ const widget = layout.nodes[index].widget;
1660
+ if (widget.semantics.role != .treeitem) continue;
1661
+ if (widget.tree_level == focused_level - 1) {
1662
+ return canvasWidgetTreeRowFocusTarget(layout, index);
1663
+ }
1664
+ if (widget.tree_level != 0 and widget.tree_level < focused_level - 1) return null;
1665
+ }
1666
+ return null;
1667
+ }
1576
1668
  var current = layout.nodes[focused_index].parent_index;
1577
1669
  while (current) |index| {
1578
1670
  if (index >= layout.nodes.len or index == tree_index) return null;
@@ -1582,7 +1674,21 @@ fn canvasWidgetTreeParentRow(layout: canvas.WidgetLayoutTree, tree_index: usize,
1582
1674
  return null;
1583
1675
  }
1584
1676
 
1585
- fn canvasWidgetTreeFirstChildRow(layout: canvas.WidgetLayoutTree, focused_index: usize) ?canvas.WidgetFocusTarget {
1677
+ fn canvasWidgetTreeFirstChildRow(layout: canvas.WidgetLayoutTree, tree_index: usize, focused_index: usize) ?canvas.WidgetFocusTarget {
1678
+ const focused_level = layout.nodes[focused_index].widget.tree_level;
1679
+ if (focused_level > 0) {
1680
+ const tree_depth = layout.nodes[tree_index].depth;
1681
+ var index = focused_index + 1;
1682
+ while (index < layout.nodes.len and layout.nodes[index].depth > tree_depth) : (index += 1) {
1683
+ const widget = layout.nodes[index].widget;
1684
+ if (widget.semantics.role != .treeitem) continue;
1685
+ if (@as(u32, widget.tree_level) == @as(u32, focused_level) + 1) {
1686
+ return canvasWidgetTreeRowFocusTarget(layout, index);
1687
+ }
1688
+ return null;
1689
+ }
1690
+ return null;
1691
+ }
1586
1692
  const row_depth = layout.nodes[focused_index].depth;
1587
1693
  var index = focused_index + 1;
1588
1694
  while (index < layout.nodes.len and layout.nodes[index].depth > row_depth) : (index += 1) {
@@ -37,6 +37,21 @@ fn scrollFixtureLayout(nodes: []canvas.WidgetLayoutNode, offset: f32) !canvas.Wi
37
37
  return canvas.layoutWidgetTree(scroll, geometry.RectF.init(0, 0, 180, 72), nodes);
38
38
  }
39
39
 
40
+ fn horizontalTabFixtureLayout(nodes: []canvas.WidgetLayoutNode, selected_id: canvas.ObjectId) !canvas.WidgetLayoutTree {
41
+ const tabs = [_]canvas.Widget{
42
+ .{ .id = 2, .kind = .stack, .frame = geometry.RectF.init(0, 0, 180, 37), .state = .{ .selected = selected_id == 2 }, .semantics = .{ .role = .tab, .label = "One" } },
43
+ .{ .id = 3, .kind = .stack, .frame = geometry.RectF.init(180, 0, 180, 37), .state = .{ .selected = selected_id == 3 }, .semantics = .{ .role = .tab, .label = "Two" } },
44
+ .{ .id = 4, .kind = .stack, .frame = geometry.RectF.init(360, 0, 180, 37), .state = .{ .selected = selected_id == 4 }, .semantics = .{ .role = .tab, .label = "Three" } },
45
+ };
46
+ const strip = canvas.Widget{
47
+ .id = 1,
48
+ .kind = .scroll_view,
49
+ .scroll_axes = .horizontal,
50
+ .children = &tabs,
51
+ };
52
+ return canvas.layoutWidgetTree(strip, geometry.RectF.init(0, 0, 360, 37), nodes);
53
+ }
54
+
40
55
  test "layout install publishes native scroll drivers and suppresses engine scrollbars" {
41
56
  const harness = try TestHarness().create(std.testing.allocator, .{});
42
57
  defer harness.destroy(std.testing.allocator);
@@ -645,6 +660,50 @@ test "a two-axis region's driver carries both content dimensions and follows off
645
660
  try std.testing.expectEqualDeep(geometry.RectF.init(-60, -30, 140, 120), retained.findById(2).?.frame);
646
661
  }
647
662
 
663
+ test "activating a tab minimally reveals it without shifting an already visible tab" {
664
+ const harness = try TestHarness().create(std.testing.allocator, .{});
665
+ defer harness.destroy(std.testing.allocator);
666
+ harness.null_platform.gpu_surfaces = true;
667
+ harness.null_platform.gpu_surface_scroll_drivers = true;
668
+ var app_state: PassiveApp = .{};
669
+ const app = app_state.app();
670
+ try harness.start(app);
671
+
672
+ _ = try harness.runtime.createView(.{
673
+ .window_id = 1,
674
+ .label = "canvas",
675
+ .kind = .gpu_surface,
676
+ .frame = geometry.RectF.init(10, 20, 360, 37),
677
+ });
678
+ var initial_nodes: [4]canvas.WidgetLayoutNode = undefined;
679
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", try horizontalTabFixtureLayout(&initial_nodes, 2));
680
+
681
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_scroll_driver = .{
682
+ .window_id = 1,
683
+ .label = "canvas",
684
+ .driver_id = 1,
685
+ .offset_x = 60,
686
+ .timestamp_ns = 1_000_000_000,
687
+ } });
688
+
689
+ // Tab Two occupies content x=180..360, already wholly inside the
690
+ // standing 60..420 viewport. Activating it preserves the offset exactly.
691
+ var visible_nodes: [4]canvas.WidgetLayoutNode = undefined;
692
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", try horizontalTabFixtureLayout(&visible_nodes, 3));
693
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
694
+ try std.testing.expectEqual(@as(f32, 60), retained.findById(1).?.widget.value_x);
695
+ try std.testing.expectEqualDeep(geometry.RectF.init(120, 0, 180, 37), retained.findById(3).?.frame);
696
+
697
+ // Tab Three ends at x=540, 120 points past that viewport. The reveal
698
+ // moves by only those 120 points instead of snapping it to the leading edge.
699
+ var hidden_nodes: [4]canvas.WidgetLayoutNode = undefined;
700
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", try horizontalTabFixtureLayout(&hidden_nodes, 4));
701
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
702
+ try std.testing.expectEqual(@as(f32, 180), retained.findById(1).?.widget.value_x);
703
+ try std.testing.expectEqualDeep(geometry.RectF.init(180, 0, 180, 37), retained.findById(4).?.frame);
704
+ try std.testing.expectEqual(@as(f32, 180), harness.null_platform.scrollDrivers()[0].offset_x);
705
+ }
706
+
648
707
  test "a horizontal-only region's driver pins its vertical range" {
649
708
  const harness = try TestHarness().create(std.testing.allocator, .{});
650
709
  defer harness.destroy(std.testing.allocator);
@@ -13,6 +13,7 @@ const App = support.App;
13
13
  const Runtime = support.Runtime;
14
14
  const Event = support.Event;
15
15
  const TestHarness = support.TestHarness;
16
+ const canvas_widget_runtime = @import("canvas_widget_runtime.zig");
16
17
 
17
18
  const TestMsg = union(enum) {
18
19
  resized: f32,
@@ -371,6 +372,105 @@ test "tree keymap walks visible rows, collapses, expands, and selects through re
371
372
  try std.testing.expect(!after_enter.findById(12).?.widget.state.selected);
372
373
  }
373
374
 
375
+ test "flat tree levels resolve logical parents and children" {
376
+ const harness = try TestHarness().create(std.testing.allocator, .{});
377
+ defer harness.destroy(std.testing.allocator);
378
+ harness.null_platform.gpu_surfaces = true;
379
+ var app_state: ObservingApp = .{};
380
+ const app = app_state.app();
381
+ try harness.start(app);
382
+
383
+ _ = try harness.runtime.createView(.{
384
+ .window_id = 1,
385
+ .label = "canvas",
386
+ .kind = .gpu_surface,
387
+ .frame = geometry.RectF.init(0, 0, 240, 160),
388
+ });
389
+
390
+ // The rows are layout siblings (the natural output of a declarative
391
+ // `for`), but their one-based levels describe the logical tree:
392
+ // src (expanded)
393
+ // └─ main.zig
394
+ // README.md
395
+ const rows = [_]canvas.Widget{
396
+ blk: {
397
+ var row = treeRowPanel(21, 0, 24, true, &.{});
398
+ row.tree_level = 1;
399
+ break :blk row;
400
+ },
401
+ blk: {
402
+ var row = treeRowPanel(22, 30, 24, null, &.{});
403
+ row.tree_level = 2;
404
+ break :blk row;
405
+ },
406
+ blk: {
407
+ var row = treeRowPanel(23, 60, 24, null, &.{});
408
+ row.tree_level = 1;
409
+ break :blk row;
410
+ },
411
+ };
412
+ var nodes: [6]canvas.WidgetLayoutNode = undefined;
413
+ const layout = try canvas.layoutWidgetTree(.{ .id = 20, .kind = .tree, .children = &rows }, geometry.RectF.init(0, 0, 240, 160), &nodes);
414
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
415
+ const view = &harness.runtime.views[0];
416
+
417
+ // Focus the flat child row, then use the standard tree moves. Left
418
+ // reaches its logical parent; Right on that expanded parent reaches
419
+ // the first logical child even though neither is a widget ancestor.
420
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{ .window_id = 1, .label = "canvas", .kind = .pointer_down, .x = 10, .y = 35, .button = 0 } });
421
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{ .window_id = 1, .label = "canvas", .kind = .pointer_up, .x = 10, .y = 35, .button = 0 } });
422
+ try std.testing.expectEqual(@as(canvas.ObjectId, 22), view.canvas_widget_focused_id);
423
+
424
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{ .window_id = 1, .label = "canvas", .kind = .key_down, .key = "arrowleft" } });
425
+ try std.testing.expectEqual(@as(canvas.ObjectId, 21), view.canvas_widget_focused_id);
426
+ try std.testing.expect(app_state.last_keyboard_focus_moved);
427
+
428
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{ .window_id = 1, .label = "canvas", .kind = .key_down, .key = "arrowright" } });
429
+ try std.testing.expectEqual(@as(canvas.ObjectId, 22), view.canvas_widget_focused_id);
430
+ try std.testing.expect(app_state.last_keyboard_focus_moved);
431
+ }
432
+
433
+ test "flat tree child lookup stays inside its tree scope" {
434
+ var first_row = treeRowPanel(31, 0, 24, true, &.{});
435
+ first_row.tree_level = 1;
436
+ var second_row = treeRowPanel(41, 0, 24, null, &.{});
437
+ second_row.tree_level = 2;
438
+ const first_rows = [_]canvas.Widget{first_row};
439
+ const second_rows = [_]canvas.Widget{second_row};
440
+ const trees = [_]canvas.Widget{
441
+ .{ .id = 30, .kind = .tree, .children = &first_rows },
442
+ .{ .id = 40, .kind = .tree, .children = &second_rows },
443
+ };
444
+ var nodes: [5]canvas.WidgetLayoutNode = undefined;
445
+ const layout = try canvas.layoutWidgetTree(
446
+ .{ .id = 29, .kind = .stack, .children = &trees },
447
+ geometry.RectF.init(0, 0, 240, 100),
448
+ &nodes,
449
+ );
450
+ const focused = layout.focusTargetById(31).?;
451
+
452
+ const target = canvas_widget_runtime.canvasWidgetTreeDirectionalFocusTarget(layout, focused, .right).?;
453
+ try std.testing.expectEqual(@as(canvas.ObjectId, 31), target.id);
454
+ }
455
+
456
+ test "flat tree child lookup handles the maximum logical level" {
457
+ var first_row = treeRowPanel(51, 0, 24, true, &.{});
458
+ first_row.tree_level = std.math.maxInt(u16);
459
+ var second_row = treeRowPanel(52, 30, 24, null, &.{});
460
+ second_row.tree_level = std.math.maxInt(u16);
461
+ const rows = [_]canvas.Widget{ first_row, second_row };
462
+ var nodes: [4]canvas.WidgetLayoutNode = undefined;
463
+ const layout = try canvas.layoutWidgetTree(
464
+ .{ .id = 50, .kind = .tree, .children = &rows },
465
+ geometry.RectF.init(0, 0, 240, 100),
466
+ &nodes,
467
+ );
468
+ const focused = layout.focusTargetById(51).?;
469
+
470
+ const target = canvas_widget_runtime.canvasWidgetTreeDirectionalFocusTarget(layout, focused, .right).?;
471
+ try std.testing.expectEqual(@as(canvas.ObjectId, 51), target.id);
472
+ }
473
+
374
474
  test "a virtual list declaring the tree role scopes the tree keymap over its rows" {
375
475
  const harness = try TestHarness().create(std.testing.allocator, .{});
376
476
  defer harness.destroy(std.testing.allocator);