@native-sdk/cli 0.7.0 → 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 (52) 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 +9 -7
  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 +451 -33
  13. package/src/primitives/canvas/code_tests.zig +855 -5
  14. package/src/primitives/canvas/events.zig +111 -1
  15. package/src/primitives/canvas/markdown_tests.zig +17 -18
  16. package/src/primitives/canvas/root.zig +4 -0
  17. package/src/primitives/canvas/text.zig +6 -0
  18. package/src/primitives/canvas/ui.zig +108 -29
  19. package/src/primitives/canvas/ui_markup.zig +23 -9
  20. package/src/primitives/canvas/ui_markup_compiled.zig +13 -0
  21. package/src/primitives/canvas/ui_markup_compiled_tests.zig +12 -0
  22. package/src/primitives/canvas/ui_markup_contract.zig +8 -1
  23. package/src/primitives/canvas/ui_markup_view.zig +16 -0
  24. package/src/primitives/canvas/ui_markup_view_tests.zig +71 -5
  25. package/src/primitives/canvas/ui_schema.zig +13 -0
  26. package/src/primitives/canvas/ui_schema_tests.zig +9 -7
  27. package/src/primitives/canvas/ui_tests.zig +26 -0
  28. package/src/primitives/canvas/widget_invalidation.zig +1 -0
  29. package/src/primitives/canvas/widget_metrics.zig +10 -5
  30. package/src/primitives/canvas/widget_render.zig +660 -39
  31. package/src/primitives/canvas/widget_render_controls.zig +4 -4
  32. package/src/primitives/canvas/widget_runtime.zig +2 -0
  33. package/src/primitives/canvas/widget_text_input.zig +165 -8
  34. package/src/primitives/canvas/widget_tree.zig +6 -5
  35. package/src/primitives/canvas/widgets.zig +25 -0
  36. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  37. package/src/runtime/canvas_limits.zig +13 -6
  38. package/src/runtime/canvas_widget_clipboard_tests.zig +7 -4
  39. package/src/runtime/canvas_widget_context_menu.zig +7 -2
  40. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  41. package/src/runtime/canvas_widget_events.zig +51 -20
  42. package/src/runtime/canvas_widget_runtime.zig +105 -4
  43. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  44. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  45. package/src/runtime/canvas_widget_state.zig +27 -0
  46. package/src/runtime/canvas_widget_text_tests.zig +326 -0
  47. package/src/runtime/core.zig +15 -5
  48. package/src/runtime/gpu_surface_events.zig +12 -12
  49. package/src/runtime/view.zig +54 -16
  50. package/src/runtime/view_widget_scroll.zig +48 -18
  51. package/src/runtime/view_widget_text.zig +54 -13
  52. package/src/runtime/window_view_runtime.zig +9 -0
@@ -330,14 +330,27 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
330
330
  // menu path before reaching here).
331
331
  self.views[index].canvas_widget_click_count = 0;
332
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;
333
335
  return;
334
336
  }
335
337
  const previous_count = self.views[index].canvas_widget_click_count;
336
338
  const previous_timestamp = self.views[index].canvas_widget_click_timestamp_ns;
337
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;
338
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;
339
350
  const chained = previous_count != 0 and
340
351
  previous_timestamp != 0 and
352
+ pointer_id == previous_pointer_id and
353
+ target_id == previous_target_id and
341
354
  input_event.timestamp_ns >= previous_timestamp and
342
355
  input_event.timestamp_ns - previous_timestamp <= canvas_widget_multi_click_interval_ns and
343
356
  @abs(point.x - previous_point.x) <= canvas_widget_multi_click_slop and
@@ -348,6 +361,8 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
348
361
  self.views[index].canvas_widget_click_count = count;
349
362
  self.views[index].canvas_widget_click_timestamp_ns = input_event.timestamp_ns;
350
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;
351
366
  pointer_event.pointer.click_count = count;
352
367
  },
353
368
  .move, .up => {
@@ -2070,6 +2085,12 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
2070
2085
  };
2071
2086
  keyboard_event.keyboard.edit = edit;
2072
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
+
2073
2094
  const dirty = if (keyboard_event.history_replay)
2074
2095
  try self.views[index].applyCanvasWidgetTextEditWithoutHistory(target.id, edit) orelse return
2075
2096
  else
@@ -2370,17 +2391,17 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
2370
2391
  }
2371
2392
 
2372
2393
  /// Returns true for an auto-repeat or release belonging to the
2373
- /// physical Tab gesture that moved focus into a live terminal.
2394
+ /// physical Tab gesture that moved focus into a Tab-owning editor.
2374
2395
  /// The caller suppresses that transition; the release also
2375
2396
  /// retires the gesture latch.
2376
- pub fn consumeCanvasWidgetTerminalFocusEntryTab(self: *Runtime, input_event: GpuSurfaceInputEvent) bool {
2397
+ pub fn consumeCanvasWidgetTabInputFocusEntry(self: *Runtime, input_event: GpuSurfaceInputEvent) bool {
2377
2398
  if (input_event.kind != .key_down and input_event.kind != .key_up) return false;
2378
2399
  if (!std.ascii.eqlIgnoreCase(input_event.key, "tab")) return false;
2379
2400
  const index = runtimeFindViewIndex(self, input_event.window_id, input_event.label) orelse return false;
2380
2401
  if (self.views[index].kind != .gpu_surface) return false;
2381
- 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;
2382
2403
  if (input_event.kind == .key_up) {
2383
- self.views[index].canvas_widget_terminal_focus_entry_tab_held = false;
2404
+ self.views[index].canvas_widget_tab_input_focus_entry_held = false;
2384
2405
  }
2385
2406
  return true;
2386
2407
  }
@@ -2415,18 +2436,21 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
2415
2436
  const current_id: ?canvas.ObjectId = if (self.views[index].canvas_widget_focused_id == 0) null else self.views[index].canvas_widget_focused_id;
2416
2437
  const layout = self.views[index].widgetLayoutTree();
2417
2438
  if (std.ascii.eqlIgnoreCase(input_event.key, "tab")) {
2418
- // A terminal owns Tab as INPUT (completion, indentation,
2419
- // and TUI navigation), not as focus traversal. Decide
2420
- // against the pre-key focus before choosing the next
2421
- // target: the routed keyboard pass below will therefore
2422
- // address this same terminal and its emulator can encode
2423
- // Tab/Shift+Tab for the pty. Disabled/hidden terminals
2424
- // cannot occupy the focus register because
2425
- // focusTargetById rejects them, so stale focus still
2426
- // 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.
2427
2442
  if (current_id) |id| {
2428
2443
  if (layout.focusTargetById(id)) |current| {
2429
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
+ }
2430
2454
  }
2431
2455
  }
2432
2456
  const direction: canvas.WidgetFocusDirection = if (input_event.modifiers.shift) .backward else .forward;
@@ -2435,13 +2459,14 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
2435
2459
  else
2436
2460
  layout.focusTarget(current_id, direction) orelse return false;
2437
2461
  const moved = try setCanvasWidgetFocusFromKeyboardMoved(self, index, current_id, target.id);
2438
- if (moved and canvasWidgetTerminalOwnsTabInput(layout, target)) {
2439
- // The key-down moved focus INTO this live terminal;
2440
- // it is traversal, not terminal input. Hold that
2441
- // classification through auto-repeat and key-up so
2442
- // the same physical gesture cannot leak a Tab press
2443
- // (legacy) or orphan release (kitty) into the pty.
2444
- 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;
2445
2470
  }
2446
2471
  return moved;
2447
2472
  }
@@ -2587,6 +2612,12 @@ fn canvasWidgetTerminalOwnsTabInput(layout: canvas.WidgetLayoutTree, target: can
2587
2612
  return grid.running;
2588
2613
  }
2589
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
+
2590
2621
  fn canvasDirtyRegionForView(view_frame: geometry.RectF, local_dirty: geometry.RectF) ?geometry.RectF {
2591
2622
  const normalized_view = view_frame.normalized();
2592
2623
  const surface_bounds = geometry.RectF.init(0, 0, normalized_view.width, normalized_view.height);
@@ -161,6 +161,11 @@ pub const CanvasWidgetTextReconcileEntry = struct {
161
161
  text_selection: ?canvas.TextSelection = null,
162
162
  text_composition: ?canvas.TextRange = null,
163
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,
164
169
  };
165
170
 
166
171
  pub const CanvasWidgetSourceScrollEntry = struct {
@@ -248,6 +253,54 @@ pub fn restoreCanvasWidgetLayoutScrollOffsets(
248
253
  }
249
254
  }
250
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
+
251
304
  pub const CanvasWidgetSourceTextEntry = struct {
252
305
  id: canvas.ObjectId = 0,
253
306
  kind: canvas.WidgetKind = .text_field,
@@ -612,6 +665,11 @@ pub fn collectCanvasWidgetTextReconcileEntries(
612
665
  .text_selection = node.widget.text_selection,
613
666
  .text_composition = node.widget.text_composition,
614
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,
615
673
  };
616
674
  len += 1;
617
675
  }
@@ -819,6 +877,13 @@ pub fn canvasWidgetLayoutNodeWithTextReconcileState(
819
877
  // right after, so a resized or re-texted field never keeps a
820
878
  // stale offset).
821
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
+ }
822
887
  if (copy.widget.text_selection == null and copy.widget.text_composition == null) {
823
888
  copy.widget.text_selection = entry.text_selection;
824
889
  copy.widget.text_composition = entry.text_composition;
@@ -968,6 +1033,7 @@ pub fn canvasWidgetLayoutTreeWithRuntimeReconcileState(
968
1033
  // the caller AFTER native scroll drivers are stamped — a rebuild
969
1034
  // mid-rubber-band must not clamp an offset the OS scroller owns.
970
1035
  restoreCanvasWidgetLayoutScrollOffsets(staged_nodes, previous_runtime_offsets, previous_source_scroll_entries);
1036
+ revealNewlySelectedTabs(previous, staged_nodes);
971
1037
 
972
1038
  const index_scratch = canvas_widget_reconcile_index_scratch.get();
973
1039
  index_scratch.controls.build(previous_control_states);
@@ -1102,7 +1168,12 @@ pub fn clampCanvasWidgetLayoutScrollOffsets(nodes: []canvas.WidgetLayoutNode, st
1102
1168
  pub fn clampCanvasWidgetLayoutTextOffsets(nodes: []canvas.WidgetLayoutNode, tokens: canvas.DesignTokens) void {
1103
1169
  for (nodes) |*node| {
1104
1170
  if (node.widget.kind == .textarea) {
1171
+ canvas.cacheTextInputContentWidthForWidget(&node.widget, tokens);
1105
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;
1106
1177
  continue;
1107
1178
  }
1108
1179
  if (!canvasWidgetSingleLineTextKind(node.widget.kind)) continue;
@@ -1499,8 +1570,10 @@ fn canvasWidgetTreeRowFocusTarget(layout: canvas.WidgetLayoutTree, node_index: u
1499
1570
  /// the focused row is a leaf or collapsed (an expanded row collapses
1500
1571
  /// instead — no focus move, the routed toggle intent handles it); Right
1501
1572
  /// moves to the FIRST CHILD row when the focused row is expanded (a
1502
- /// collapsed row expands instead). Null = no tree move (the caller
1503
- /// 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).
1504
1577
  pub fn canvasWidgetTreeDirectionalFocusTarget(
1505
1578
  layout: canvas.WidgetLayoutTree,
1506
1579
  focused: canvas.WidgetFocusTarget,
@@ -1520,7 +1593,7 @@ pub fn canvasWidgetTreeDirectionalFocusTarget(
1520
1593
  .right => blk: {
1521
1594
  const expanded = layout.nodes[focused.index].widget.state.expanded orelse false;
1522
1595
  if (!expanded) break :blk null; // expand intent (or leaf no-op)
1523
- break :blk canvasWidgetTreeFirstChildRow(layout, focused.index) orelse focused;
1596
+ break :blk canvasWidgetTreeFirstChildRow(layout, tree_index, focused.index) orelse focused;
1524
1597
  },
1525
1598
  .forward, .backward => null,
1526
1599
  };
@@ -1578,6 +1651,20 @@ fn canvasWidgetTreeAdjacentRow(
1578
1651
  }
1579
1652
 
1580
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
+ }
1581
1668
  var current = layout.nodes[focused_index].parent_index;
1582
1669
  while (current) |index| {
1583
1670
  if (index >= layout.nodes.len or index == tree_index) return null;
@@ -1587,7 +1674,21 @@ fn canvasWidgetTreeParentRow(layout: canvas.WidgetLayoutTree, tree_index: usize,
1587
1674
  return null;
1588
1675
  }
1589
1676
 
1590
- 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
+ }
1591
1692
  const row_depth = layout.nodes[focused_index].depth;
1592
1693
  var index = focused_index + 1;
1593
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);
@@ -28,6 +28,27 @@ const canvasWidgetAccessibilityActionSupported = widget_bridge.canvasWidgetAcces
28
28
  const platformWidgetAccessibilityActionKindFromCanvas = widget_bridge.platformWidgetAccessibilityActionKindFromCanvas;
29
29
  const canvasWidgetBooleanSelected = canvas_widget_runtime.canvasWidgetBooleanSelected;
30
30
 
31
+ fn canvasWidgetLayoutNeedsLargeTextStorage(layout: canvas.WidgetLayoutTree) bool {
32
+ var text_len: usize = 0;
33
+ for (layout.nodes) |node| {
34
+ const widget = node.widget;
35
+ if (widget.code_editor) return true;
36
+ text_len +|= widget.text.len +| widget.icon.len +| widget.command.len +| widget.semantics.label.len;
37
+ for (widget.spans) |span| text_len +|= span.text.len +| span.link.len;
38
+ for (widget.context_menu) |item| text_len +|= item.label.len;
39
+ for (widget.chart.series) |series| text_len +|= series.label.len;
40
+ for (widget.chart.x_labels) |label| text_len +|= label.len;
41
+ if (text_len > canvas_limits.max_canvas_widget_inline_text_bytes_per_view) return true;
42
+ }
43
+ return false;
44
+ }
45
+
46
+ fn ensureCanvasWidgetLargeTextStorageThrough(runtime: anytype, index: usize) !void {
47
+ for (runtime.views[0 .. index + 1]) |*view| {
48
+ try view.ensureLargeCanvasWidgetTextStorage(runtime.owned_allocator);
49
+ }
50
+ }
51
+
31
52
  pub fn RuntimeCanvasWidgetState(comptime Runtime: type) type {
32
53
  return struct {
33
54
  pub fn setCanvasWidgetLayout(self: *Runtime, window_id: platform.WindowId, label: []const u8, layout: canvas.WidgetLayoutTree) anyerror!platform.ViewInfo {
@@ -85,6 +106,9 @@ pub fn RuntimeCanvasWidgetState(comptime Runtime: type) type {
85
106
  armed_tween_ids[0..armed_tween_count],
86
107
  canvasWidgetPressedSplitId(self, index),
87
108
  );
109
+ if (canvasWidgetLayoutNeedsLargeTextStorage(reconciled_layout)) {
110
+ try ensureCanvasWidgetLargeTextStorageThrough(self, index);
111
+ }
88
112
  // Native scroll drivers: mark natively driven scroll
89
113
  // regions before the copy so rebuild-time clamping and display
90
114
  // emission both see the flag (engine scrollbar + engine clamp
@@ -831,6 +855,9 @@ pub fn RuntimeCanvasWidgetState(comptime Runtime: type) type {
831
855
  // field's editor never accepts a line break, whoever writes.
832
856
  const node = self.views[index].widgetLayoutTree().findById(id) orelse return error.InvalidCommand;
833
857
  const sanitized = canvas.sanitizedSingleLineTextInputEvent(node.widget.kind, edit) orelse return self.views[index].info();
858
+ if (self.views[index].canvasWidgetTextEditNeedsLargeStorage(id, sanitized)) {
859
+ try ensureCanvasWidgetLargeTextStorageThrough(self, index);
860
+ }
834
861
  const dirty = try self.views[index].applyCanvasWidgetTextEdit(id, sanitized) orelse return self.views[index].info();
835
862
  try CanvasWidgetEventMethods(Runtime).invalidateForCanvasWidgetDirty(self, index, dirty);
836
863
  _ = try CanvasWidgetDisplayMethods(Runtime).refreshCanvasWidgetDisplayListIfOwned(self, index);