@native-sdk/cli 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/package.json +9 -9
  2. package/packages/core/package-lock.json +2 -2
  3. package/packages/core/package.json +1 -1
  4. package/skill-data/native-ui/SKILL.md +2 -2
  5. package/src/platform/types.zig +9 -0
  6. package/src/primitives/canvas/root.zig +1 -0
  7. package/src/primitives/canvas/tokens.zig +170 -7
  8. package/src/primitives/canvas/ui.zig +27 -5
  9. package/src/primitives/canvas/ui_markup.zig +27 -4
  10. package/src/primitives/canvas/ui_markup_compiled.zig +9 -1
  11. package/src/primitives/canvas/ui_markup_compiled_tests.zig +127 -0
  12. package/src/primitives/canvas/ui_markup_tests.zig +47 -0
  13. package/src/primitives/canvas/ui_markup_view.zig +10 -1
  14. package/src/primitives/canvas/ui_markup_view_tests.zig +78 -0
  15. package/src/primitives/canvas/ui_schema.zig +15 -1
  16. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  17. package/src/primitives/canvas/widget_builtin_tests.zig +130 -0
  18. package/src/primitives/canvas/widget_layout.zig +4 -1
  19. package/src/primitives/canvas/widget_layout_tests.zig +82 -0
  20. package/src/primitives/canvas/widget_tree.zig +14 -2
  21. package/src/primitives/canvas/widgets.zig +12 -0
  22. package/src/runtime/automation_widget_dispatch.zig +95 -28
  23. package/src/runtime/canvas_frame.zig +15 -0
  24. package/src/runtime/canvas_widget_anchored_tests.zig +253 -65
  25. package/src/runtime/canvas_widget_context_menu.zig +3 -2
  26. package/src/runtime/canvas_widget_events.zig +1271 -3
  27. package/src/runtime/canvas_widget_floating_tests.zig +2801 -0
  28. package/src/runtime/canvas_widget_scroll_drivers.zig +1 -1
  29. package/src/runtime/canvas_widget_state.zig +82 -5
  30. package/src/runtime/core.zig +14 -0
  31. package/src/runtime/flow.zig +24 -1
  32. package/src/runtime/gpu_surface_events.zig +44 -5
  33. package/src/runtime/session_journal.zig +25 -13
  34. package/src/runtime/session_record.zig +166 -0
  35. package/src/runtime/session_tests.zig +453 -0
  36. package/src/runtime/ui_app.zig +6 -1
  37. package/src/runtime/ui_app_tests.zig +460 -0
  38. package/src/runtime/view.zig +86 -0
  39. package/src/runtime/view_widget_text.zig +18 -1
  40. package/src/runtime/view_widget_tree.zig +339 -20
  41. package/src/runtime/widget_bridge.zig +26 -0
  42. package/src/runtime/window_storage.zig +61 -15
  43. package/src/runtime/window_view_runtime.zig +13 -0
  44. package/src/runtime/window_views.zig +6 -0
@@ -295,6 +295,14 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
295
295
  self.widget_tokens,
296
296
  );
297
297
 
298
+ // Anchored-tooltip hover intent survives the rebuild the way
299
+ // hover/press ids do: drop state whose tooltip unmounted,
300
+ // then re-stamp runtime-owned visibility BEFORE semantics
301
+ // collect, so the a11y snapshot (and the replay fingerprint
302
+ // riding it) always reflects the intent machine.
303
+ self.pruneCanvasTooltipIntent();
304
+ self.applyCanvasTooltipVisibility();
305
+
298
306
  const semantics = try self.widgetLayoutTree().collectSemantics(&self.widget_semantics_nodes);
299
307
  applyCanvasWidgetSourceScrollSemantics(self.widget_semantics_nodes[0..semantics.len], &index_scratch.semantics);
300
308
  self.widget_semantics_node_count = semantics.len;
@@ -302,9 +310,15 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
302
310
  const return_id = if (focus_return_id != 0 and self.widgetLayoutTree().focusTargetById(focus_return_id) != null) focus_return_id else 0;
303
311
  self.canvas_widget_focused_id = return_id;
304
312
  self.canvas_widget_focus_visible_id = return_id;
313
+ // The focus-return ring is not a keyboard ARRIVAL:
314
+ // reveals fire only on focus-visible transitions (the
315
+ // dismissal seam's rule), so the returned ring never
316
+ // grants adoption-time tooltip reveals either.
317
+ self.canvas_widget_focus_visible_keyboard = false;
305
318
  }
306
319
  if (self.canvas_widget_focus_visible_id != 0 and (self.canvas_widget_focus_visible_id != self.canvas_widget_focused_id or self.widgetLayoutTree().focusTargetById(self.canvas_widget_focus_visible_id) == null)) {
307
320
  self.canvas_widget_focus_visible_id = 0;
321
+ self.canvas_widget_focus_visible_keyboard = false;
308
322
  }
309
323
  if (self.canvas_widget_hovered_id != 0 and !canvasWidgetInteractionTargetExists(self.widgetLayoutTree(), self.canvas_widget_hovered_id)) {
310
324
  self.canvas_widget_hovered_id = 0;
@@ -367,9 +381,11 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
367
381
  if (self.canvas_widget_focused_id != 0 and layout.focusTargetById(self.canvas_widget_focused_id) == null) {
368
382
  self.canvas_widget_focused_id = 0;
369
383
  self.canvas_widget_focus_visible_id = 0;
384
+ self.canvas_widget_focus_visible_keyboard = false;
370
385
  }
371
386
  if (self.canvas_widget_focus_visible_id != 0 and (self.canvas_widget_focus_visible_id != self.canvas_widget_focused_id or layout.focusTargetById(self.canvas_widget_focus_visible_id) == null)) {
372
387
  self.canvas_widget_focus_visible_id = 0;
388
+ self.canvas_widget_focus_visible_keyboard = false;
373
389
  }
374
390
 
375
391
  var next_hovered_id = self.canvas_widget_hovered_id;
@@ -407,6 +423,10 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
407
423
  /// would leave Escape dead. A focused editable with live IME
408
424
  /// composition always wins: Escape cancels the composition and
409
425
  /// never dismisses a surface, not even through the fallback.
426
+ /// With SEVERAL surfaces anchored on one stack (the select
427
+ /// trigger's focus-shown tooltip floating over its open menu),
428
+ /// each Escape peels exactly one, topmost (last-mounted) first —
429
+ /// the tooltip goes, then the menu.
410
430
  pub fn dismissCanvasWidgetSurfaceFromEscape(self: *RuntimeView, focused_id: canvas.ObjectId) anyerror!?CanvasWidgetSurfaceDismissal {
411
431
  if (focused_id != 0) {
412
432
  if (self.canvasWidgetNodeIndexById(focused_id)) |focused_index| {
@@ -424,12 +444,14 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
424
444
  /// transient choice, so moving the keyboard on closes it WITHOUT
425
445
  /// committing, exactly like a click outside. Scoped to menu
426
446
  /// surfaces only: Tab through a persistent popover's form fields
427
- /// must not tear the popover down.
447
+ /// must not tear the popover down. The lookup scans FOR menu
448
+ /// kinds rather than kind-checking whatever floats topmost — a
449
+ /// trigger can anchor a tooltip beside its menu, and the
450
+ /// focus-visible tooltip shadowing the open menu left Tab unable
451
+ /// to close it.
428
452
  pub fn dismissCanvasWidgetMenuSurfaceForFocusDeparture(self: *RuntimeView, focused_id: canvas.ObjectId) anyerror!?CanvasWidgetSurfaceDismissal {
429
453
  const focused_index = self.canvasWidgetNodeIndexById(focused_id) orelse return null;
430
- const surface_index = self.canvasWidgetDismissibleSurfaceIndexForTarget(focused_index) orelse return null;
431
- const kind = self.widget_layout_nodes[surface_index].widget.kind;
432
- if (kind != .menu_surface and kind != .dropdown_menu) return null;
454
+ const surface_index = canvasWidgetSurfaceIndexForTargetInScope(self, focused_index, .menu) orelse return null;
433
455
  return self.dismissCanvasWidgetSurfaceAtIndex(surface_index);
434
456
  }
435
457
 
@@ -443,9 +465,16 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
443
465
  return self.dismissCanvasWidgetSurfaceAtIndex(surface_index);
444
466
  }
445
467
 
468
+ /// Outside-click light dismissal targets INTERACTIVE surfaces
469
+ /// only. A tooltip's whole lifecycle already belongs to the
470
+ /// intent machine's own causes on any outside down — hover
471
+ /// leave, focus moving with the click, the press itself — so
472
+ /// letting the topmost tooltip absorb this gesture would both
473
+ /// double-cover those and leave the menu beneath it floating
474
+ /// after the user clicked away.
446
475
  pub fn dismissCanvasWidgetSurfaceForPointerOutsideFocusedTarget(self: *RuntimeView, focused_id: canvas.ObjectId, route: []const canvas.WidgetEventRouteEntry) anyerror!?CanvasWidgetSurfaceDismissal {
447
476
  const focused_index = self.canvasWidgetNodeIndexById(focused_id) orelse return null;
448
- const surface_index = self.canvasWidgetDismissibleSurfaceIndexForTarget(focused_index) orelse return null;
477
+ const surface_index = canvasWidgetSurfaceIndexForTargetInScope(self, focused_index, .interactive) orelse return null;
449
478
  if (self.canvasWidgetRouteDescendsFromIndex(route, surface_index)) return null;
450
479
  // Clicking the ANCHOR region of an anchored surface (the
451
480
  // trigger, or the stack that wraps trigger + surface) is the
@@ -466,6 +495,46 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
466
495
  if (surface.semantics.hidden) return null;
467
496
  const dirty = self.canvasWidgetDirtyBounds(surface_index, surface.frame) orelse surface.frame;
468
497
  self.widget_layout_nodes[surface_index].widget.semantics.hidden = true;
498
+ // A dismissed anchored tooltip leaves the intent machine too,
499
+ // or the next visibility stamp would undo the dismissal. No
500
+ // warm window: Escape is a deliberate dismissal, not the
501
+ // pointer moving on to the next trigger.
502
+ if (surface.kind == .tooltip) {
503
+ if (self.canvas_tooltip_shown_id == surface.id) {
504
+ // Covers the focus-shown path too: Escape on the
505
+ // focused trigger clears the reason flag with the
506
+ // slot, and focus (still on the trigger) does not
507
+ // re-reveal — reveals fire only on focus-visible
508
+ // TRANSITIONS, so tabbing away and back re-earns it.
509
+ // The one non-transition reveal — the adoption
510
+ // binding-reconcile, which honors a STANDING
511
+ // keyboard ring when a rebuild swaps the tooltip it
512
+ // owns — is blocked by consuming that standing
513
+ // intent here: when the ring rests on the dismissed
514
+ // tooltip's owner, the dismissal spends
515
+ // `canvas_widget_focus_visible_keyboard` (its only
516
+ // readers are the tooltip reveal gates; the ring
517
+ // itself renders from `canvas_widget_focus_visible_id`
518
+ // and stays painted), so a rebuild that rekeys the
519
+ // tooltip cannot resurrect it one frame after
520
+ // Escape. Same design as the keyboard-activation
521
+ // dismissal seam in canvas_widget_events.zig.
522
+ if (self.canvas_widget_focus_visible_id != 0 and
523
+ self.canvas_widget_focus_visible_id == self.canvas_tooltip_shown_owner_id)
524
+ {
525
+ self.canvas_widget_focus_visible_keyboard = false;
526
+ }
527
+ self.canvas_tooltip_shown_id = 0;
528
+ self.canvas_tooltip_shown_owner_id = 0;
529
+ self.canvas_tooltip_transit_deadline_ns = 0;
530
+ self.canvas_tooltip_shown_from_focus = false;
531
+ }
532
+ if (self.canvas_tooltip_armed_id == surface.id) {
533
+ self.canvas_tooltip_armed_id = 0;
534
+ self.canvas_tooltip_armed_owner_id = 0;
535
+ self.canvas_tooltip_deadline_ns = 0;
536
+ }
537
+ }
469
538
  if (self.canvasWidgetIdDescendsFromIndex(self.canvas_widget_focused_id, surface_index)) {
470
539
  // A dismissal that swallows the focus returns it to the
471
540
  // surface's own trigger when the surface is anchored (the
@@ -474,8 +543,15 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
474
543
  const return_id = self.canvasWidgetAnchorTriggerFocusId(surface_index) orelse 0;
475
544
  self.canvas_widget_focused_id = return_id;
476
545
  self.canvas_widget_focus_visible_id = return_id;
546
+ // Same rule as the rebuild's focus return: a returned
547
+ // ring is not a keyboard arrival, so it earns no
548
+ // reveal — here or at a later layout adoption.
549
+ self.canvas_widget_focus_visible_keyboard = false;
550
+ }
551
+ if (self.canvasWidgetIdDescendsFromIndex(self.canvas_widget_focus_visible_id, surface_index)) {
552
+ self.canvas_widget_focus_visible_id = 0;
553
+ self.canvas_widget_focus_visible_keyboard = false;
477
554
  }
478
- if (self.canvasWidgetIdDescendsFromIndex(self.canvas_widget_focus_visible_id, surface_index)) self.canvas_widget_focus_visible_id = 0;
479
555
  if (self.canvasWidgetIdDescendsFromIndex(self.canvas_widget_hovered_id, surface_index)) {
480
556
  self.canvas_widget_hovered_id = 0;
481
557
  self.canvas_widget_cursor = .arrow;
@@ -487,20 +563,59 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
487
563
  return .{ .id = surface.id, .dirty = dirty };
488
564
  }
489
565
 
566
+ /// Which anchored dismissible surfaces a lookup means to see. A
567
+ /// widget stack can anchor SEVERAL surfaces at once (a select
568
+ /// trigger with both its dropdown-menu and a tooltip), so every
569
+ /// consumer names the population it is really after — grabbing
570
+ /// "the anchored child" and kind-checking the winner let a
571
+ /// focus-visible tooltip shadow the open menu mounted before it.
572
+ pub const CanvasWidgetAnchoredSurfaceScope = enum {
573
+ /// Every dismissible surface, tooltips included. Escape and
574
+ /// the automation/accessibility dismiss actions peel
575
+ /// whatever floats TOPMOST, one surface per gesture.
576
+ any,
577
+ /// Surfaces that can hold interaction and keyboard focus —
578
+ /// everything but tooltips, which are hover chrome the
579
+ /// intent machine owns: outside-click dismissal and focus
580
+ /// scoping.
581
+ interactive,
582
+ /// Menu surfaces only (`menu_surface`/`dropdown_menu`): the
583
+ /// open-select keymap and Tab's focus-departure close.
584
+ menu,
585
+ };
586
+
587
+ fn canvasWidgetAnchoredSurfaceKindInScope(kind: canvas.WidgetKind, comptime scope: CanvasWidgetAnchoredSurfaceScope) bool {
588
+ if (!canvasWidgetDismissibleSurfaceKind(kind)) return false;
589
+ return switch (scope) {
590
+ .any => true,
591
+ .interactive => kind != .tooltip,
592
+ .menu => kind == .menu_surface or kind == .dropdown_menu,
593
+ };
594
+ }
595
+
490
596
  pub fn canvasWidgetDismissibleSurfaceIndexForTarget(self: *const RuntimeView, target_index: usize) ?usize {
597
+ return canvasWidgetSurfaceIndexForTargetInScope(self, target_index, .any);
598
+ }
599
+
600
+ /// The nearest in-scope surface up the target's chain: the first
601
+ /// visible dismissible ancestor, or an in-scope anchored surface
602
+ /// HANGING OFF an ancestor (the ancestor is its anchor) — Escape
603
+ /// on the focused trigger, or on the stack wrapping trigger +
604
+ /// surface, closes its own menu even though the surface is a
605
+ /// descendant, not an ancestor, of the focus. A visible ancestor
606
+ /// surface OUTSIDE the scope shields rather than defers: the
607
+ /// keyboard living in a persistent popover must not reach past
608
+ /// it to a menu further out.
609
+ fn canvasWidgetSurfaceIndexForTargetInScope(self: *const RuntimeView, target_index: usize, comptime scope: CanvasWidgetAnchoredSurfaceScope) ?usize {
491
610
  if (target_index >= self.widget_layout_node_count) return null;
492
611
  var current: ?usize = target_index;
493
612
  while (current) |index| {
494
613
  if (index >= self.widget_layout_node_count) return null;
495
614
  const widget = self.widget_layout_nodes[index].widget;
496
- if (canvasWidgetDismissibleSurfaceKind(widget.kind) and !widget.semantics.hidden) return index;
497
- // An anchored dismissible surface HANGING OFF this
498
- // ancestor (the ancestor is its anchor) is the nearest
499
- // floating surface: Escape on the focused trigger — or on
500
- // the stack wrapping trigger + surface — closes its own
501
- // menu even though the surface is a descendant, not an
502
- // ancestor, of the focus.
503
- if (self.canvasWidgetAnchoredDismissibleChildIndex(index)) |surface_index| return surface_index;
615
+ if (canvasWidgetDismissibleSurfaceKind(widget.kind) and !widget.semantics.hidden) {
616
+ return if (canvasWidgetAnchoredSurfaceKindInScope(widget.kind, scope)) index else null;
617
+ }
618
+ if (canvasWidgetAnchoredChildIndexInScope(self, index, scope)) |surface_index| return surface_index;
504
619
  current = self.widget_layout_nodes[index].parent_index;
505
620
  }
506
621
  return null;
@@ -509,11 +624,19 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
509
624
  /// The topmost (last-mounted) visible anchored dismissible surface
510
625
  /// whose anchor is `anchor_index`, or null.
511
626
  pub fn canvasWidgetAnchoredDismissibleChildIndex(self: *const RuntimeView, anchor_index: usize) ?usize {
627
+ return canvasWidgetAnchoredChildIndexInScope(self, anchor_index, .any);
628
+ }
629
+
630
+ /// The topmost visible anchored child of `anchor_index` whose
631
+ /// kind is IN SCOPE — the scope filters DURING the scan, so an
632
+ /// out-of-scope sibling mounted later (the tooltip above the
633
+ /// menu) never masks the surface the caller asked for.
634
+ fn canvasWidgetAnchoredChildIndexInScope(self: *const RuntimeView, anchor_index: usize, comptime scope: CanvasWidgetAnchoredSurfaceScope) ?usize {
512
635
  var found: ?usize = null;
513
636
  for (self.widget_layout_nodes[0..self.widget_layout_node_count], 0..) |node, index| {
514
637
  if (node.parent_index != anchor_index) continue;
515
638
  if (!canvas.widgetIsAnchored(node.widget)) continue;
516
- if (!canvasWidgetDismissibleSurfaceKind(node.widget.kind)) continue;
639
+ if (!canvasWidgetAnchoredSurfaceKindInScope(node.widget.kind, scope)) continue;
517
640
  if (node.widget.semantics.hidden) continue;
518
641
  found = index;
519
642
  }
@@ -533,10 +656,164 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
533
656
  }
534
657
 
535
658
  fn canvasWidgetAnchoredMenuChildIndex(self: *const RuntimeView, anchor_index: usize) ?usize {
536
- const surface_index = self.canvasWidgetAnchoredDismissibleChildIndex(anchor_index) orelse return null;
537
- const kind = self.widget_layout_nodes[surface_index].widget.kind;
538
- if (kind != .menu_surface and kind != .dropdown_menu) return null;
539
- return surface_index;
659
+ return canvasWidgetAnchoredChildIndexInScope(self, anchor_index, .menu);
660
+ }
661
+
662
+ /// The anchored tooltip a trigger owns: the last-mounted anchored
663
+ /// `.tooltip` child of the trigger itself or of its parent — the
664
+ /// stack wrapping trigger + tooltip, mirroring the anchored-menu
665
+ /// ownership shape. Deliberately IGNORES the hidden flag: the
666
+ /// runtime itself stamps non-shown anchored tooltips hidden, and
667
+ /// arming must find them to show them.
668
+ pub fn canvasWidgetOwnedTooltipIndex(self: *const RuntimeView, trigger_index: usize) ?usize {
669
+ return canvasWidgetOwnedTooltipIndexInNodes(self.widget_layout_nodes[0..self.widget_layout_node_count], trigger_index);
670
+ }
671
+
672
+ /// The ID of the anchored tooltip `owner_id` owns in the
673
+ /// CURRENT retained tree, or 0 (no such owner / no owned
674
+ /// tooltip). The layout-adoption reconcile compares this value
675
+ /// across a rebuild — captured against the outgoing tree in
676
+ /// `setCanvasWidgetLayout`, re-read against the adopted one —
677
+ /// to see a binding that changed beneath a STABLE owner: a
678
+ /// tooltip mounted, replaced, rekeyed, or reparented under a
679
+ /// trigger whose own ID survived produces no hover or focus
680
+ /// delta, so only this comparison can arm (or reveal) it.
681
+ pub fn canvasWidgetOwnedTooltipIdForOwner(self: *const RuntimeView, owner_id: canvas.ObjectId) canvas.ObjectId {
682
+ if (owner_id == 0) return 0;
683
+ const owner_index = self.canvasWidgetNodeIndexById(owner_id) orelse return 0;
684
+ const tooltip_index = self.canvasWidgetOwnedTooltipIndex(owner_index) orelse return 0;
685
+ return self.widget_layout_nodes[tooltip_index].widget.id;
686
+ }
687
+
688
+ /// Stamp hover-intent visibility onto every ANCHORED tooltip
689
+ /// node: hidden unless it is the intent machine's shown tooltip.
690
+ /// Anchored tooltips are runtime-owned chrome, so the stamp
691
+ /// overrides authored visibility; static (non-anchored) tooltips
692
+ /// are never touched. Runs at tree adoption (each rebuild) and
693
+ /// after every intent transition.
694
+ pub fn applyCanvasTooltipVisibility(self: *RuntimeView) void {
695
+ self.applyCanvasTooltipVisibilityToNodes(self.widget_layout_nodes[0..self.widget_layout_node_count]);
696
+ }
697
+
698
+ /// The same stamp over an arbitrary node slice, against the
699
+ /// view's LIVE shown register.
700
+ pub fn applyCanvasTooltipVisibilityToNodes(self: *const RuntimeView, nodes: []canvas.WidgetLayoutNode) void {
701
+ applyCanvasTooltipVisibilityToNodesForShownId(self, nodes, self.canvas_tooltip_shown_id);
702
+ }
703
+
704
+ /// The stamp against an EXPLICIT shown id. The rebuild path
705
+ /// normalizes the RECONCILED scratch tree with it BEFORE diffing
706
+ /// against the retained tree (see `setCanvasWidgetLayout`),
707
+ /// passing the PROSPECTIVE prune verdict from
708
+ /// `canvasTooltipShownIdSurvivingLayout` rather than mutating
709
+ /// the live registers first: the retained side carries the
710
+ /// intent machine's hidden stamps while the source declares
711
+ /// authored visibility, so diffing them un-normalized reported
712
+ /// the runtime's own stamp as a spurious visibility
713
+ /// invalidation on every rebuild that contained a hidden
714
+ /// anchored tooltip — and mutating the registers before the
715
+ /// fallible adoption steps left a FAILED adoption with the old
716
+ /// tree stamped visible and cleared registers (an unhideable
717
+ /// tooltip).
718
+ pub fn applyCanvasTooltipVisibilityToNodesForShownId(self: *const RuntimeView, nodes: []canvas.WidgetLayoutNode, shown_id: canvas.ObjectId) void {
719
+ _ = self;
720
+ for (nodes) |*node| {
721
+ if (node.widget.kind != .tooltip) continue;
722
+ if (!canvas.widgetIsAnchored(node.widget)) continue;
723
+ node.widget.semantics.hidden = node.widget.id == 0 or node.widget.id != shown_id;
724
+ }
725
+ }
726
+
727
+ /// Drop intent state whose tooltip OR owning trigger left the
728
+ /// tree on a rebuild — the interaction-id pruning policy
729
+ /// hovered/pressed ids follow, applied to both ends of the
730
+ /// tooltip binding. The tooltip node surviving is not enough:
731
+ /// a trigger that vanished, was rekeyed (a new id is a new
732
+ /// widget), re-parented away from its tooltip, or disabled
733
+ /// (disabled widgets leave both hover and focus routing, so
734
+ /// nothing could ever hide the tooltip again) invalidates the
735
+ /// slot, and the pruned slot re-stamps hidden through the
736
+ /// `applyCanvasTooltipVisibility` call that follows adoption.
737
+ /// The warm window survives rebuilds that keep the bindings
738
+ /// alive (warmth belongs to the pointer, not to any one
739
+ /// widget), but a pruned binding closes it: instant re-shows
740
+ /// earned against widgets the rebuild replaced are not earned
741
+ /// at all.
742
+ pub fn pruneCanvasTooltipIntent(self: *RuntimeView) void {
743
+ self.pruneCanvasTooltipIntentForLayout(self.widgetLayoutTree());
744
+ }
745
+
746
+ /// The same prune against an arbitrary layout. Runs inside
747
+ /// `copyWidgetLayoutTree` against the freshly retained tree —
748
+ /// AFTER every fallible adoption step has succeeded, which is
749
+ /// what keeps the registers transactional: the rebuild path's
750
+ /// pre-diff stamp reads only the VERDICT
751
+ /// (`canvasTooltipShownIdSurvivingLayout`) so a failed adoption
752
+ /// leaves both the old tree and the registers that can hide its
753
+ /// tooltip intact.
754
+ pub fn pruneCanvasTooltipIntentForLayout(self: *RuntimeView, layout: canvas.WidgetLayoutTree) void {
755
+ if (self.canvas_tooltip_armed_id != 0 and !canvasTooltipIntentBindingAlive(layout, self.canvas_tooltip_armed_id, self.canvas_tooltip_armed_owner_id, false)) {
756
+ self.canvas_tooltip_armed_id = 0;
757
+ self.canvas_tooltip_armed_owner_id = 0;
758
+ self.canvas_tooltip_deadline_ns = 0;
759
+ self.canvas_tooltip_warm_until_ns = 0;
760
+ }
761
+ if (self.canvas_tooltip_shown_id != 0 and !canvasTooltipIntentBindingAlive(layout, self.canvas_tooltip_shown_id, self.canvas_tooltip_shown_owner_id, self.canvas_tooltip_shown_from_focus)) {
762
+ self.canvas_tooltip_shown_id = 0;
763
+ self.canvas_tooltip_shown_owner_id = 0;
764
+ self.canvas_tooltip_shown_from_focus = false;
765
+ self.canvas_tooltip_warm_until_ns = 0;
766
+ self.canvas_tooltip_transit_deadline_ns = 0;
767
+ }
768
+ }
769
+
770
+ /// The shown-tooltip prune VERDICT against a prospective
771
+ /// layout, WITHOUT the register mutation: the shown id that
772
+ /// would survive adopting `layout`, or 0 when the rebuild kills
773
+ /// the binding. `setCanvasWidgetLayout` stamps the reconciled
774
+ /// scratch tree with this ahead of the diff (so the diff
775
+ /// reports the hide a binding-breaking rebuild really causes,
776
+ /// and an unchanged rebuild diffs clean) while the live
777
+ /// registers stay untouched until the fallible adoption steps —
778
+ /// the diff itself and the retained-pool validation/copy —
779
+ /// succeed; `copyWidgetLayoutTree`'s own prune then applies the
780
+ /// same verdict for real. A failed adoption therefore leaves
781
+ /// the OLD tree with the registers that own its stamps: the
782
+ /// tooltip that is still painted is still hideable.
783
+ pub fn canvasTooltipShownIdSurvivingLayout(self: *const RuntimeView, layout: canvas.WidgetLayoutTree) canvas.ObjectId {
784
+ if (self.canvas_tooltip_shown_id == 0) return 0;
785
+ if (!canvasTooltipIntentBindingAlive(layout, self.canvas_tooltip_shown_id, self.canvas_tooltip_shown_owner_id, self.canvas_tooltip_shown_from_focus)) return 0;
786
+ return self.canvas_tooltip_shown_id;
787
+ }
788
+
789
+ /// Both ends of a tooltip intent slot are still live: the
790
+ /// tooltip node exists and is anchored, its recorded owner
791
+ /// still exists AND still resolves to this very tooltip
792
+ /// (`canvasWidgetOwnedTooltipIndex`, the inverse of the arming
793
+ /// walk), and the owner remains reachable by the routing that
794
+ /// earned the slot — focus targeting for focus-shown tooltips,
795
+ /// hover/interaction targeting for pointer ones. Both
796
+ /// predicates already reject disabled and hidden widgets, so
797
+ /// "the trigger can no longer be left" implies "the tooltip
798
+ /// must not stay".
799
+ fn canvasTooltipIntentBindingAlive(layout: canvas.WidgetLayoutTree, tooltip_id: canvas.ObjectId, owner_id: canvas.ObjectId, from_focus: bool) bool {
800
+ const tooltip_index = canvasWidgetNodeIndexByIdInNodes(layout.nodes, tooltip_id) orelse return false;
801
+ const tooltip_widget = layout.nodes[tooltip_index].widget;
802
+ if (tooltip_widget.kind != .tooltip or !canvas.widgetIsAnchored(tooltip_widget)) return false;
803
+ const owner_index = canvasWidgetNodeIndexByIdInNodes(layout.nodes, owner_id) orelse return false;
804
+ const owned_index = canvasWidgetOwnedTooltipIndexInNodes(layout.nodes, owner_index) orelse return false;
805
+ if (owned_index != tooltip_index) return false;
806
+ if (from_focus) return layout.focusTargetById(owner_id) != null;
807
+ return canvasWidgetInteractionTargetExists(layout, owner_id);
808
+ }
809
+
810
+ /// True while the intent machine needs presented frames to keep
811
+ /// coming: an armed show delay and a running transit grace both
812
+ /// fire only on a frame timestamp, so the frame pump
813
+ /// re-invalidates until they resolve (the render-animation
814
+ /// pump's policy exactly).
815
+ pub fn canvasTooltipIntentArmed(self: *const RuntimeView) bool {
816
+ return self.canvas_tooltip_armed_id != 0 or self.canvas_tooltip_transit_deadline_ns != 0;
540
817
  }
541
818
 
542
819
  /// Keyboard entry point into an anchored menu surface: the marked
@@ -611,9 +888,14 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
611
888
  return false;
612
889
  }
613
890
 
891
+ /// Tab cycling scopes to the INTERACTIVE surface around the
892
+ /// focus: a tooltip can never hold a focus target, so scoping to
893
+ /// one (the trigger's visible tooltip shadowing its popover or
894
+ /// menu) would always come up empty and spill the keyboard out
895
+ /// of the trap into the page's global walk.
614
896
  pub fn canvasWidgetScopedFocusTarget(self: *const RuntimeView, current_id: canvas.ObjectId, direction: canvas.WidgetFocusDirection) ?canvas.WidgetFocusTarget {
615
897
  const current_index = self.canvasWidgetNodeIndexById(current_id) orelse return null;
616
- const surface_index = self.canvasWidgetDismissibleSurfaceIndexForTarget(current_index) orelse return null;
898
+ const surface_index = canvasWidgetSurfaceIndexForTargetInScope(self, current_index, .interactive) orelse return null;
617
899
  return self.canvasWidgetFocusTargetInScope(surface_index, current_index, direction);
618
900
  }
619
901
 
@@ -841,3 +1123,40 @@ pub fn RuntimeViewCanvasWidgetTree(comptime RuntimeView: type) type {
841
1123
  }
842
1124
  };
843
1125
  }
1126
+
1127
+ /// Node index by widget id over a bare node slice — the retained
1128
+ /// tree's `canvasWidgetNodeIndexById` generalized so the tooltip
1129
+ /// binding checks can run against the RECONCILED scratch tree before
1130
+ /// adoption (the pre-diff visibility normalization) exactly as they
1131
+ /// run against the retained one.
1132
+ fn canvasWidgetNodeIndexByIdInNodes(nodes: []const canvas.WidgetLayoutNode, id: canvas.ObjectId) ?usize {
1133
+ if (id == 0) return null;
1134
+ for (nodes, 0..) |node, index| {
1135
+ if (node.widget.id == id) return index;
1136
+ }
1137
+ return null;
1138
+ }
1139
+
1140
+ /// The anchored tooltip a trigger owns, over a bare node slice: the
1141
+ /// last-mounted anchored `.tooltip` child of the trigger itself or of
1142
+ /// its parent — the stack-wraps-trigger-plus-tooltip ownership shape,
1143
+ /// mirroring the anchored-menu walk. Deliberately IGNORES the hidden
1144
+ /// flag: the runtime itself stamps non-shown anchored tooltips hidden,
1145
+ /// and arming must find them to show them.
1146
+ fn canvasWidgetOwnedTooltipIndexInNodes(nodes: []const canvas.WidgetLayoutNode, trigger_index: usize) ?usize {
1147
+ if (trigger_index >= nodes.len) return null;
1148
+ if (canvasWidgetAnchoredTooltipChildIndexInNodes(nodes, trigger_index)) |tooltip_index| return tooltip_index;
1149
+ const parent_index = nodes[trigger_index].parent_index orelse return null;
1150
+ return canvasWidgetAnchoredTooltipChildIndexInNodes(nodes, parent_index);
1151
+ }
1152
+
1153
+ fn canvasWidgetAnchoredTooltipChildIndexInNodes(nodes: []const canvas.WidgetLayoutNode, anchor_index: usize) ?usize {
1154
+ var found: ?usize = null;
1155
+ for (nodes, 0..) |node, index| {
1156
+ if (node.parent_index != anchor_index) continue;
1157
+ if (node.widget.kind != .tooltip) continue;
1158
+ if (!canvas.widgetIsAnchored(node.widget)) continue;
1159
+ found = index;
1160
+ }
1161
+ return found;
1162
+ }
@@ -223,6 +223,32 @@ pub fn canvasWidgetAccessibilityActionKindFromPlatform(action: platform.WidgetAc
223
223
  .decrement => .decrement,
224
224
  .set_text => .set_text,
225
225
  .set_selection => .set_selection,
226
+ .set_composition => .set_composition,
227
+ .commit_composition => .commit_composition,
228
+ .cancel_composition => .cancel_composition,
229
+ .select => .select,
230
+ .drag => .drag,
231
+ .drop_files => .drop_files,
232
+ .dismiss => .dismiss,
233
+ };
234
+ }
235
+
236
+ /// The inverse of `canvasWidgetAccessibilityActionKindFromPlatform`: the
237
+ /// direct verb surfaces (embed hosts, automation `widget_action`
238
+ /// commands) reach the dispatch without a platform event, and the
239
+ /// session journal needs one to record the verb outer-wins.
240
+ pub fn platformWidgetAccessibilityActionKindFromCanvas(action: CanvasWidgetAccessibilityActionKind) platform.WidgetAccessibilityActionKind {
241
+ return switch (action) {
242
+ .focus => .focus,
243
+ .press => .press,
244
+ .toggle => .toggle,
245
+ .increment => .increment,
246
+ .decrement => .decrement,
247
+ .set_text => .set_text,
248
+ .set_selection => .set_selection,
249
+ .set_composition => .set_composition,
250
+ .commit_composition => .commit_composition,
251
+ .cancel_composition => .cancel_composition,
226
252
  .select => .select,
227
253
  .drag => .drag,
228
254
  .drop_files => .drop_files,
@@ -3,6 +3,7 @@ const geometry = @import("geometry");
3
3
  const validation = @import("validation.zig");
4
4
  const shell_layout = @import("shell_layout.zig");
5
5
  const runtime_state = @import("state.zig");
6
+ const runtime_canvas_widget_events = @import("canvas_widget_events.zig");
6
7
  const app_manifest = @import("app_manifest");
7
8
  const platform = @import("../platform/root.zig");
8
9
 
@@ -37,7 +38,7 @@ pub fn RuntimeWindowStorage(comptime Runtime: type) type {
37
38
  pub fn focusWindow(self: *Runtime, window_id: platform.WindowId) anyerror!void {
38
39
  const index = Self.findWindowIndexById(self, window_id) orelse return error.WindowNotFound;
39
40
  try self.options.platform.services.focusWindow(window_id);
40
- Self.setFocusedIndex(self, index);
41
+ try Self.setFocusedIndex(self, index);
41
42
  self.invalidated = true;
42
43
  }
43
44
 
@@ -78,7 +79,7 @@ pub fn RuntimeWindowStorage(comptime Runtime: type) type {
78
79
  const window_options = options.windowOptions(id, self.windows[index].info.label);
79
80
  const native_info = try self.options.platform.services.createWindow(window_options);
80
81
  native_created = true;
81
- Self.applyNativeInfo(self, index, native_info);
82
+ try Self.applyNativeInfo(self, index, native_info);
82
83
  if (self.windows[index].source) |window_source| {
83
84
  try self.options.platform.services.loadWindowWebView(id, window_source);
84
85
  }
@@ -131,30 +132,32 @@ pub fn RuntimeWindowStorage(comptime Runtime: type) type {
131
132
  return copySourceInto(&self.loaded_source_storage, source);
132
133
  }
133
134
 
134
- pub fn applyNativeInfo(self: *Runtime, index: usize, native_info: platform.WindowInfo) void {
135
+ pub fn applyNativeInfo(self: *Runtime, index: usize, native_info: platform.WindowInfo) anyerror!void {
135
136
  self.windows[index].info.frame = native_info.frame;
136
137
  self.windows[index].info.scale_factor = native_info.scale_factor;
137
138
  self.windows[index].info.open = native_info.open;
138
- self.windows[index].info.focused = native_info.focused;
139
139
  if (!self.windows[index].main_frame_set) {
140
140
  self.windows[index].main_frame = geometry.RectF.init(0, 0, native_info.frame.width, native_info.frame.height);
141
141
  }
142
- if (native_info.focused) Self.setFocusedIndex(self, index);
142
+ if (native_info.focused)
143
+ try Self.setFocusedIndex(self, index)
144
+ else
145
+ try Self.setWindowFocused(self, index, false);
143
146
  }
144
147
 
145
148
  pub fn updateWindowState(self: *Runtime, state: platform.WindowState) !void {
146
149
  const existing_index = Self.findWindowIndexById(self, state.id);
147
150
  const index = existing_index orelse try Self.reserveWindow(self, state.id, state.label, state.title, null, true);
148
- var info = self.windows[index].info;
149
- info.frame = state.frame;
150
- info.scale_factor = state.scale_factor;
151
- info.open = state.open;
152
- info.focused = state.focused;
153
- self.windows[index].info = info;
151
+ self.windows[index].info.frame = state.frame;
152
+ self.windows[index].info.scale_factor = state.scale_factor;
153
+ self.windows[index].info.open = state.open;
154
154
  if (!self.windows[index].main_frame_set) {
155
155
  self.windows[index].main_frame = geometry.RectF.init(0, 0, state.frame.width, state.frame.height);
156
156
  }
157
- if (state.focused) Self.setFocusedIndex(self, index);
157
+ if (state.focused)
158
+ try Self.setFocusedIndex(self, index)
159
+ else
160
+ try Self.setWindowFocused(self, index, false);
158
161
  }
159
162
 
160
163
  pub fn runtimeWindowStateForPersistence(self: *const Runtime, state: platform.WindowState) platform.WindowState {
@@ -224,9 +227,52 @@ pub fn RuntimeWindowStorage(comptime Runtime: type) type {
224
227
  self.shell_layout_count -= 1;
225
228
  }
226
229
 
227
- pub fn setFocusedIndex(self: *Runtime, focused_index: usize) void {
228
- for (self.windows[0..self.window_count], 0..) |*window, index| {
229
- window.info.focused = index == focused_index;
230
+ /// THE window-key seam: every path that moves key-window status
231
+ /// — the platform's `window_focused` event, a frame-change echo
232
+ /// carrying `focused`, the app's own `focusWindow`, and native
233
+ /// adoption at creation — lands here, so the key-LOSS
234
+ /// consequence cannot be skipped by feeding only one ingress.
235
+ /// A window that transitions focused→unfocused drops the
236
+ /// tooltip conversation in all of its canvas views (the
237
+ /// `.view_blur` contract: focus-shown and pointer-owned alike
238
+ /// hide and re-stamp hidden); `view.focused` itself is
239
+ /// deliberately untouched so per-window focus memory survives
240
+ /// and the re-key restores focus where it was without revealing
241
+ /// anything.
242
+ pub fn setFocusedIndex(self: *Runtime, focused_index: usize) anyerror!void {
243
+ for (0..self.window_count) |index| {
244
+ try Self.setWindowFocused(self, index, index == focused_index);
245
+ }
246
+ }
247
+
248
+ /// The ONE writer of a tracked window's `focused` flag: the
249
+ /// key-LOSS consequence fires on the flag's own focused→
250
+ /// unfocused edge, HERE, so it cannot depend on which platform
251
+ /// event carried the loss or in what order. macOS announces a
252
+ /// key change as one GAIN (`window_focused`), and the dethroning
253
+ /// loop in `setFocusedIndex` observes the old window's edge —
254
+ /// but Windows and GTK announce the LOSS first (a state echo
255
+ /// carrying `focused = false` for the window the user left,
256
+ /// before any gain for the next one), and a loss written past
257
+ /// this seam would leave the later gain nothing to observe:
258
+ /// the tooltip stayed painted, and a11y-visible, in the
259
+ /// inactive window. Callers pass the flag they were told;
260
+ /// the transition logic lives only here.
261
+ ///
262
+ /// The two writes that deliberately stay OUTSIDE the seam:
263
+ /// `reserveWindow`'s creation-time init (a fresh slot has no
264
+ /// prior state — no edge exists) and `closeWindow`'s
265
+ /// transactional flip in window_views.zig (its views are
266
+ /// removed with the window on success, so there is no tooltip
267
+ /// left to reset, and its rollback on platform failure must
268
+ /// not have fired one).
269
+ pub fn setWindowFocused(self: *Runtime, index: usize, focused: bool) anyerror!void {
270
+ const CanvasWidgetEventMethods = runtime_canvas_widget_events.RuntimeCanvasWidgetEvents(Runtime);
271
+ const window = &self.windows[index];
272
+ const was_focused = window.info.focused;
273
+ window.info.focused = focused;
274
+ if (was_focused and !focused) {
275
+ try CanvasWidgetEventMethods.resetCanvasTooltipIntentForWindowKeyLoss(self, window.info.id);
230
276
  }
231
277
  }
232
278