@native-sdk/cli 0.5.0 → 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 (60) hide show
  1. package/build/app.zig +198 -13
  2. package/build/ts_run.mjs +58 -19
  3. package/build.zig +42 -0
  4. package/package.json +13 -9
  5. package/packages/core/package-lock.json +4 -3
  6. package/packages/core/package.json +3 -2
  7. package/packages/core/src/typed_ast.ts +11 -1
  8. package/skill-data/native-ui/SKILL.md +2 -2
  9. package/src/platform/types.zig +9 -0
  10. package/src/platform/windows/webview2_host.cpp +24 -0
  11. package/src/primitives/canvas/layout_audit.zig +5 -5
  12. package/src/primitives/canvas/root.zig +6 -0
  13. package/src/primitives/canvas/tokens.zig +185 -7
  14. package/src/primitives/canvas/ui.zig +27 -5
  15. package/src/primitives/canvas/ui_markup.zig +27 -4
  16. package/src/primitives/canvas/ui_markup_compiled.zig +9 -1
  17. package/src/primitives/canvas/ui_markup_compiled_tests.zig +127 -0
  18. package/src/primitives/canvas/ui_markup_tests.zig +47 -0
  19. package/src/primitives/canvas/ui_markup_view.zig +10 -1
  20. package/src/primitives/canvas/ui_markup_view_tests.zig +78 -0
  21. package/src/primitives/canvas/ui_schema.zig +15 -1
  22. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  23. package/src/primitives/canvas/widget_builtin_tests.zig +439 -5
  24. package/src/primitives/canvas/widget_layout.zig +142 -3
  25. package/src/primitives/canvas/widget_layout_tests.zig +82 -0
  26. package/src/primitives/canvas/widget_metrics.zig +16 -8
  27. package/src/primitives/canvas/widget_tree.zig +14 -2
  28. package/src/primitives/canvas/widgets.zig +12 -0
  29. package/src/runtime/automation_widget_dispatch.zig +95 -28
  30. package/src/runtime/canvas_frame.zig +15 -0
  31. package/src/runtime/canvas_widget_anchored_tests.zig +253 -65
  32. package/src/runtime/canvas_widget_context_menu.zig +3 -2
  33. package/src/runtime/canvas_widget_events.zig +1271 -3
  34. package/src/runtime/canvas_widget_floating_tests.zig +2801 -0
  35. package/src/runtime/canvas_widget_scroll_drivers.zig +1 -1
  36. package/src/runtime/canvas_widget_state.zig +82 -5
  37. package/src/runtime/core.zig +30 -0
  38. package/src/runtime/effects.zig +1037 -133
  39. package/src/runtime/effects_fetch_tests.zig +42 -0
  40. package/src/runtime/effects_file_tests.zig +220 -0
  41. package/src/runtime/effects_tests.zig +287 -0
  42. package/src/runtime/flow.zig +24 -1
  43. package/src/runtime/gpu_surface_events.zig +44 -5
  44. package/src/runtime/session_journal.zig +25 -13
  45. package/src/runtime/session_record.zig +166 -0
  46. package/src/runtime/session_tests.zig +453 -0
  47. package/src/runtime/ui_app.zig +137 -39
  48. package/src/runtime/ui_app_tests.zig +681 -0
  49. package/src/runtime/ui_app_window_tests.zig +182 -0
  50. package/src/runtime/view.zig +86 -0
  51. package/src/runtime/view_widget_text.zig +18 -1
  52. package/src/runtime/view_widget_tree.zig +339 -20
  53. package/src/runtime/widget_bridge.zig +26 -0
  54. package/src/runtime/window_storage.zig +61 -15
  55. package/src/runtime/window_view_runtime.zig +13 -0
  56. package/src/runtime/window_views.zig +6 -0
  57. package/src/tooling/package.zig +311 -8
  58. package/src/tooling/templates.zig +97 -9
  59. package/src/tooling/ts_core.zig +626 -13
  60. package/src/tooling/verbs.zig +88 -2
@@ -8,8 +8,11 @@ const canvas_frame_helpers = @import("canvas_frame.zig");
8
8
  const canvas_limits = @import("canvas_limits.zig");
9
9
  const runtime_canvas_widget_display = @import("canvas_widget_display.zig");
10
10
  const canvas_widget_runtime = @import("canvas_widget_runtime.zig");
11
+ const runtime_view = @import("view.zig");
11
12
  const widget_bridge = @import("widget_bridge.zig");
12
13
 
14
+ const canvasRenderAnimationStartNsForView = runtime_view.canvasRenderAnimationStartNsForView;
15
+
13
16
  const GpuSurfaceInputEvent = runtime_api.GpuSurfaceInputEvent;
14
17
  const CanvasWidgetPointerEvent = runtime_api.CanvasWidgetPointerEvent;
15
18
  const CanvasWidgetKeyboardEvent = runtime_api.CanvasWidgetKeyboardEvent;
@@ -334,6 +337,13 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
334
337
  break :blk 0;
335
338
  } else 0;
336
339
 
340
+ // Whatever this down resolves to, focus-visible provenance
341
+ // is now the POINTER's (before the unchanged early-return:
342
+ // a click that re-establishes the same editable's ring
343
+ // converts a keyboard ring into the caret contract, which
344
+ // never reveals tooltips — Base UI's focus-visible guard
345
+ // against click-focus opens).
346
+ self.views[index].canvas_widget_focus_visible_keyboard = false;
337
347
  if (self.views[index].canvas_widget_focused_id == next_focus_id and self.views[index].canvas_widget_focus_visible_id == next_focus_visible_id) return;
338
348
  const previous_state = self.views[index].canvasWidgetRenderState();
339
349
  self.views[index].canvas_widget_focused_id = next_focus_id;
@@ -401,6 +411,20 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
401
411
  canvasChartHoverKey(self, index, next_hovered_id, next_hover_point),
402
412
  );
403
413
 
414
+ // Anchored-tooltip intent steps through the ONE choke point
415
+ // (see reconcileCanvasTooltipIntent): position bookkeeping,
416
+ // cancel's short-circuit, hover transitions, the travel
417
+ // step, the press reset, and the frame-pump obligation all
418
+ // live there — this path only feeds the cause.
419
+ try reconcileCanvasTooltipIntent(self, index, if (pointer_event.pointer.phase == .cancel)
420
+ .pointer_cancel
421
+ else
422
+ .{ .pointer = .{
423
+ .phase = pointer_event.pointer.phase,
424
+ .point = pointer_event.pointer.point,
425
+ .next_hovered_id = next_hovered_id,
426
+ } });
427
+
404
428
  const interaction_changed = self.views[index].canvas_widget_hovered_id != next_hovered_id or
405
429
  self.views[index].canvas_widget_pressed_id != next_pressed_id or
406
430
  hover_detail_changed;
@@ -431,6 +455,1142 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
431
455
  return .{ .id = hovered_id, .index = sample };
432
456
  }
433
457
 
458
+ /// Every way the world can move under (or out from under) the
459
+ /// tooltip intent machine, named. One cause per input path; the
460
+ /// choke point below is the only place that maps causes to
461
+ /// intent transitions, so a new input path added tomorrow has
462
+ /// exactly one obligation: feed its cause.
463
+ pub const CanvasTooltipReconcileCause = union(enum) {
464
+ /// A routed pointer event, any button and phase except
465
+ /// `.cancel` (which is its own cause below). Carries the
466
+ /// hover resolution the interaction walk produced for this
467
+ /// event — the routed target for downs, the fresh hit-test
468
+ /// for hovers/moves/ups — so the transition gate compares
469
+ /// the same ids the wash and cursor will commit.
470
+ pointer: struct {
471
+ phase: canvas.WidgetPointerPhase,
472
+ point: geometry.PointF,
473
+ next_hovered_id: canvas.ObjectId,
474
+ },
475
+ /// A pointer-carrying input CONSUMED before the widget
476
+ /// pipeline — the secondary-button context-menu stream and
477
+ /// window-drag downs. The interaction path early-returns
478
+ /// for these, but the pointer still moved: position
479
+ /// bookkeeping runs for every one of them, and a consumed
480
+ /// down is still a down ("pointer-down dismisses" holds
481
+ /// for every button). Without this cause, a later
482
+ /// point-blind reconcile re-hit-tested a position the
483
+ /// pointer had already left.
484
+ consumed_pointer: struct {
485
+ point: geometry.PointF,
486
+ down: bool,
487
+ },
488
+ /// The pointer LEFT the view (AppKit mouseExited and kin
489
+ /// arrive as pointer_cancel, whichever button carried it).
490
+ /// Clears the stored position — the point-blind paths must
491
+ /// close pointer intent rather than guess — and closes the
492
+ /// pointer's whole conversation; a FOCUS-shown tooltip
493
+ /// survives, the keyboard holds it.
494
+ pointer_cancel,
495
+ /// Wheel scroll with the live pointer position: content
496
+ /// moved under a stationary pointer.
497
+ wheel_scroll: geometry.PointF,
498
+ /// A scroll with no pointer position of its own — kinetic
499
+ /// steps, native drivers, keyboard scrolling. Borrows the
500
+ /// stored position; with none, pointer intent closes.
501
+ point_blind_scroll,
502
+ /// A rebuild adopted a new tree under a stationary pointer
503
+ /// (`setCanvasWidgetLayout`, after pose restores settle the
504
+ /// frames the user actually sees). Carries the OUTGOING
505
+ /// tree's tooltip bindings for the standing hover and
506
+ /// keyboard focus-visible owners — captured before adoption
507
+ /// alongside the transactional prune's verdict, because the
508
+ /// old tree is gone by the time this cause fires.
509
+ layout_adoption: CanvasTooltipAdoptionBindingSnapshot,
510
+ /// Keyboard focus-visible landed on (or left) a widget: the
511
+ /// tooltip's second reveal path.
512
+ focus_visible: canvas.ObjectId,
513
+ /// A programmatic focus move (autofocus, accessibility
514
+ /// `focus`, automation) — the pointer contract: hides a
515
+ /// focus-owned tooltip, reveals nothing.
516
+ programmatic_focus,
517
+ /// The view stopped being focused: the whole conversation
518
+ /// drops, focus-shown included — the keyboard itself left.
519
+ view_blur,
520
+ };
521
+
522
+ /// The pre-adoption tooltip bindings a rebuild must be compared
523
+ /// against: for the pointer-hovered owner and the KEYBOARD
524
+ /// focus-visible owner, the tooltip each one owned in the
525
+ /// OUTGOING tree (0 = none). The transactional prune validates
526
+ /// only EXISTING intent registers (armed/shown), so a rebuild
527
+ /// that mounts, replaces, rekeys, or reparents a tooltip
528
+ /// beneath an owner whose own ID is stable produces no hover
529
+ /// delta, no focus delta, and no stale register — without this
530
+ /// snapshot the new tooltip could never arm until the pointer
531
+ /// left and re-entered (or focus moved away and back).
532
+ pub const CanvasTooltipAdoptionBindingSnapshot = struct {
533
+ /// `canvas_widget_hovered_id` before adoption.
534
+ hovered_owner_id: canvas.ObjectId = 0,
535
+ /// The tooltip that owner owned in the outgoing tree.
536
+ hovered_tooltip_id: canvas.ObjectId = 0,
537
+ /// The standing KEYBOARD focus-visible owner before
538
+ /// adoption — 0 when the ring is pointer/programmatic
539
+ /// provenance (`canvas_widget_focus_visible_keyboard`) or
540
+ /// the view is not focused: those rings never reveal, so
541
+ /// they carry no standing intent across a rebuild either.
542
+ focus_visible_owner_id: canvas.ObjectId = 0,
543
+ /// The tooltip that owner owned in the outgoing tree.
544
+ focus_visible_tooltip_id: canvas.ObjectId = 0,
545
+ };
546
+
547
+ /// Capture the snapshot against the CURRENT retained tree —
548
+ /// `setCanvasWidgetLayout` calls this before any fallible
549
+ /// adoption step (next to the prune verdict it already
550
+ /// computes), because the outgoing tree's bindings are
551
+ /// unreadable once `copyWidgetLayoutTree` replaces it.
552
+ pub fn captureCanvasTooltipAdoptionBindings(self: *const Runtime, view_index: usize) CanvasTooltipAdoptionBindingSnapshot {
553
+ const view = &self.views[view_index];
554
+ const focus_owner_id: canvas.ObjectId =
555
+ if (view.focused and view.canvas_widget_focus_visible_keyboard) view.canvas_widget_focus_visible_id else 0;
556
+ return .{
557
+ .hovered_owner_id = view.canvas_widget_hovered_id,
558
+ .hovered_tooltip_id = view.canvasWidgetOwnedTooltipIdForOwner(view.canvas_widget_hovered_id),
559
+ .focus_visible_owner_id = focus_owner_id,
560
+ .focus_visible_tooltip_id = view.canvasWidgetOwnedTooltipIdForOwner(focus_owner_id),
561
+ };
562
+ }
563
+
564
+ /// Whether tooltip intent may ACT for this view right now: the
565
+ /// app is active AND the view's owning window is key. Every
566
+ /// reveal and arm path checks this — the pointer dwell/warm
567
+ /// show, the keyboard focus-visible reveal, the frame promote,
568
+ /// and (through those leaves) the adoption binding-reconcile —
569
+ /// because intent causes genuinely reach views the user has
570
+ /// left: always-active tracking areas keep hovering non-key
571
+ /// windows, and a rebuild triggered FROM the deactivation
572
+ /// callback adopts while the app is inactive (the deactivation
573
+ /// reset ran first, so anything revealed or armed there would
574
+ /// resurrect, one dispatch later, exactly the state that reset
575
+ /// just cleared). Suppression gates ACTION only: the keyboard
576
+ /// focus-visible provenance register and
577
+ /// `canvas_last_pointer_position` stay preserved — their
578
+ /// persistence is what lets a later ACTIVE adoption or hover
579
+ /// behave normally; ACTING on them while inactive/non-key was
580
+ /// the bug, never their existence. Re-activation and re-key
581
+ /// reveal nothing spontaneously by construction: both reveal
582
+ /// paths are transition-edge-triggered, and regaining active or
583
+ /// key status replays no transition.
584
+ fn canvasTooltipIntentActionAllowed(self: *const Runtime, view_index: usize) bool {
585
+ if (!self.app_active) return false;
586
+ const window_id = self.views[view_index].window_id;
587
+ for (self.windows[0..self.window_count]) |*window| {
588
+ if (window.info.id == window_id) return window.info.focused;
589
+ }
590
+ return false;
591
+ }
592
+
593
+ /// THE tooltip-intent choke point: the one owner of pointer-
594
+ /// position bookkeeping, hover re-hit-testing from the stored
595
+ /// position, content-hold containment against the CURRENT
596
+ /// tooltip frames, corridor/grace state, deadline arming, and
597
+ /// the frame-pump obligation. Review rounds kept finding the
598
+ /// same disease — an input path that fed only part of the
599
+ /// machine (a deadline armed with no pump, a containment check
600
+ /// skipped behind an unchanged-hover early return, a consumed
601
+ /// down that never updated the stored position) — so every
602
+ /// entry point is now a thin caller that names its cause and
603
+ /// delegates here.
604
+ ///
605
+ /// ORDER OF OPERATIONS (fixed; each step may feed the next):
606
+ /// 1. position update — every pointer-carrying cause writes
607
+ /// `canvas_last_pointer_position` (cancel clears it),
608
+ /// BEFORE any transition logic, so even causes whose
609
+ /// interaction handling early-returns leave the store
610
+ /// truthful;
611
+ /// 2. hover re-hit-test — the content-moved causes (scroll,
612
+ /// adoption) re-resolve hover from the stored/live
613
+ /// position against the current tree; pointer causes
614
+ /// carry their walk's resolution in the cause;
615
+ /// 3. containment re-check — a content-held tooltip is
616
+ /// re-tested against its CURRENT frame, independent of
617
+ /// whether the hover id changed (a hold reads
618
+ /// hovered_id == 0 on both sides of a scroll or rebuild,
619
+ /// so a transition gate alone can never see it break);
620
+ /// 4. transition resolution — hide/arm/dismiss with
621
+ /// reason-awareness (focus-shown vs pointer-shown vs
622
+ /// content-held), including reprocessing the current
623
+ /// hover as a fresh transition when a content hold's
624
+ /// release exposed a trigger already under the pointer;
625
+ /// 5. deadline/pump reconciliation — ANY pending deadline
626
+ /// (armed show delay OR transit grace) obligates the
627
+ /// frame pump: the deadlines fire only on presented-frame
628
+ /// timestamps (advanceCanvasTooltipIntentForFrame), and
629
+ /// planCanvasFrameForView re-invalidates per frame while
630
+ /// one is pending, but that pump only runs once a frame
631
+ /// is planned — so the reconcile that ARMS a deadline
632
+ /// must kick the first invalidation itself. Deriving the
633
+ /// kick from "any deadline pending" (never from "did this
634
+ /// event repaint something") is what closes the idle-app
635
+ /// hole: a transit grace armed by a 0→0 hover move
636
+ /// changes no interaction state and would otherwise wait
637
+ /// forever for a frame that nothing requested.
638
+ pub fn reconcileCanvasTooltipIntent(self: *Runtime, view_index: usize, cause: CanvasTooltipReconcileCause) anyerror!void {
639
+ if (view_index >= self.view_count) return;
640
+ if (self.views[view_index].kind != .gpu_surface) return;
641
+ const view = &self.views[view_index];
642
+
643
+ // 1. Pointer-position bookkeeping. The point-blind paths
644
+ // re-hit-test this stored position; a cleared store tells
645
+ // them the pointer is gone and they must close tooltip
646
+ // intent instead of guessing.
647
+ switch (cause) {
648
+ .pointer => |event| view.canvas_last_pointer_position = event.point,
649
+ .consumed_pointer => |event| view.canvas_last_pointer_position = event.point,
650
+ .pointer_cancel => view.canvas_last_pointer_position = null,
651
+ .wheel_scroll, .point_blind_scroll, .layout_adoption, .focus_visible, .programmatic_focus, .view_blur => {},
652
+ }
653
+
654
+ // 2–4. Hover re-hit-test, containment re-check, and
655
+ // transition resolution, per cause.
656
+ switch (cause) {
657
+ .pointer => |event| {
658
+ const had_pointer_shown = view.canvas_tooltip_shown_id != 0 and !view.canvas_tooltip_shown_from_focus;
659
+ // Hover transitions step the machine (a pointer
660
+ // gliding within one trigger is free); the armed
661
+ // delay itself fires on presented-frame timestamps
662
+ // in advanceCanvasTooltipIntentForFrame.
663
+ if (view.canvas_widget_hovered_id != event.next_hovered_id) {
664
+ try updateCanvasTooltipIntentForHoverChange(self, view_index, event.next_hovered_id, event.point);
665
+ }
666
+ // While a pointer-shown tooltip is up, EVERY move
667
+ // also steps the travel check: crossing the anchor
668
+ // gap or gliding over the tooltip's own frame
669
+ // usually changes no hover target (the tooltip is
670
+ // deliberately not hit-tested), so the transition
671
+ // gate above cannot see it.
672
+ if (event.phase == .hover or event.phase == .move) {
673
+ try updateCanvasTooltipIntentForPointerTravel(self, view_index, event.next_hovered_id, event.point);
674
+ // Content-hold release reprocessing: when this
675
+ // move hid the held tooltip (travel left both
676
+ // regions, or the transition's containment
677
+ // broke), the CURRENT hover — a trigger the
678
+ // pointer was already recorded on THROUGH the
679
+ // tooltip's frame — is reprocessed as a fresh
680
+ // transition so it arms (or warm-shows) per the
681
+ // normal rules. "No transition happened" was
682
+ // measured against stale ownership (hover
683
+ // reached through a floating frame belonged to
684
+ // the tooltip), not against user intent: the
685
+ // pointer is honestly on that trigger NOW.
686
+ // Idempotent when the transition above already
687
+ // armed it.
688
+ if (had_pointer_shown and view.canvas_tooltip_shown_id == 0 and event.next_hovered_id != 0) {
689
+ try updateCanvasTooltipIntentForHoverChange(self, view_index, event.next_hovered_id, event.point);
690
+ }
691
+ }
692
+ // A press ends the tooltip conversation outright —
693
+ // pending reveal, shown tooltip, and warm window
694
+ // alike (after the hover step above, so a down that
695
+ // also moved the hover cannot re-arm or warm past
696
+ // the press).
697
+ if (event.phase == .down) {
698
+ try updateCanvasTooltipIntentForPress(self, view_index);
699
+ }
700
+ },
701
+ .consumed_pointer => |event| {
702
+ // Position bookkeeping (step 1) is the point for
703
+ // moves/ups of a consumed stream; a consumed down
704
+ // still dismisses ("pointer-down dismisses" is
705
+ // documented for ANY down — shadcn's Base UI-backed
706
+ // default; macOS help tags vanish on any click).
707
+ if (event.down) try updateCanvasTooltipIntentForPress(self, view_index);
708
+ },
709
+ // `.pointer_cancel` — the pointer LEAVING the view —
710
+ // short-circuits ahead of any transition gate: its
711
+ // immediate-hide semantics must not depend on a hover
712
+ // TRANSITION existing, and for a tooltip held open by
713
+ // its own hovered content there is none (the tooltip is
714
+ // not a hit target, so the hold already reads
715
+ // hovered_id == 0 and a cancel-to-0 is no transition).
716
+ // The close resets every pointer-owned slot; the
717
+ // FOCUS-shown tooltip survives — the keyboard holds it
718
+ // and the pointer's departure says nothing about it.
719
+ // (View blur is the KEYBOARD leaving, which is why
720
+ // `.view_blur` clears focus-shown too while this arm
721
+ // deliberately does not.)
722
+ .pointer_cancel => try closeCanvasTooltipPointerIntent(self, view_index),
723
+ .wheel_scroll => |point| try reconcileCanvasTooltipIntentForMovedContent(self, view_index, point, .scroll),
724
+ .point_blind_scroll => try reconcileCanvasTooltipIntentForMovedContent(self, view_index, null, .scroll),
725
+ .layout_adoption => |bindings| try reconcileCanvasTooltipIntentForMovedContent(self, view_index, null, .{ .adoption = bindings }),
726
+ .focus_visible => |focus_visible_id| try updateCanvasTooltipIntentForFocusVisibleChange(self, view_index, focus_visible_id),
727
+ // A programmatic focus move writes focus on the pointer
728
+ // contract, not the keyboard one, so it never REVEALS
729
+ // the new target's tooltip (Base UI's focus-visible
730
+ // gate against click-focus opens). But a focus-OWNED
731
+ // tooltip whose keyboard hold the move just broke must
732
+ // not stay painted (the blur-hides contract), and
733
+ // hiding it opens no warm window: warmth is a
734
+ // pointer-sweep courtesy, and nothing swept.
735
+ // Pointer-owned tooltips are untouched — hover holds
736
+ // them, and a focus move says nothing about the pointer.
737
+ .programmatic_focus => {
738
+ if (view.canvas_tooltip_shown_id != 0 and view.canvas_tooltip_shown_from_focus) {
739
+ view.canvas_tooltip_shown_id = 0;
740
+ view.canvas_tooltip_shown_owner_id = 0;
741
+ view.canvas_tooltip_shown_from_focus = false;
742
+ try commitCanvasTooltipVisibility(self, view_index);
743
+ }
744
+ },
745
+ // A view that stops being FOCUSED drops its whole
746
+ // tooltip conversation — armed delay, shown tooltip
747
+ // (focus-owned AND pointer-owned), warm window, transit
748
+ // grace — and re-stamps the tooltip hidden. The widget
749
+ // focus bookkeeping survives a view switch so focus can
750
+ // return where it was, but a tooltip is transient
751
+ // explanation for the interaction the user just left:
752
+ // keeping one painted (or letting a warm window
753
+ // smolder) in a view that no longer hears the keyboard
754
+ // is a stale affordance, and its semantics node would
755
+ // keep claiming visible in the a11y tree.
756
+ .view_blur => {
757
+ const had_shown = view.canvas_tooltip_shown_id != 0;
758
+ view.canvas_tooltip_armed_id = 0;
759
+ view.canvas_tooltip_armed_owner_id = 0;
760
+ view.canvas_tooltip_deadline_ns = 0;
761
+ view.canvas_tooltip_warm_until_ns = 0;
762
+ view.canvas_tooltip_transit_deadline_ns = 0;
763
+ view.canvas_tooltip_shown_id = 0;
764
+ view.canvas_tooltip_shown_owner_id = 0;
765
+ view.canvas_tooltip_shown_from_focus = false;
766
+ if (had_shown) try commitCanvasTooltipVisibility(self, view_index);
767
+ },
768
+ }
769
+
770
+ // 5. Deadline/pump reconciliation: any pending deadline —
771
+ // armed show delay OR transit grace, exactly what
772
+ // canvasTooltipIntentArmed derives — obligates a frame, so
773
+ // the deadline can resolve on the recorded frame clock in
774
+ // an app that receives no further input. The per-frame pump
775
+ // in planCanvasFrameForView takes over from the first
776
+ // planned frame.
777
+ if (view.canvasTooltipIntentArmed()) {
778
+ self.invalidateFor(.state, view.frame);
779
+ }
780
+ }
781
+
782
+ /// How a content-moved reconcile treats the transition step:
783
+ /// scrolls are deliberately corridor-free (immediate-hide
784
+ /// semantics — the pointer did not move, the content did, and a
785
+ /// scroll is the reader moving on; Base UI closes open tooltips
786
+ /// on scroll for the same reason), so their transitions run
787
+ /// point-blind and the corridor apex re-seeds afterward.
788
+ /// Adoption passes the stored point through: its containment
789
+ /// pre-check below already resolved the shown slot, and the
790
+ /// point lets the arm/warm-show paths seed the apex in one step.
791
+ /// Adoption also carries the pre-adoption binding snapshot, so
792
+ /// the binding-change step can see a tooltip that mounted or
793
+ /// swapped beneath a stable owner.
794
+ const CanvasTooltipMovedContentCause = union(enum) {
795
+ scroll,
796
+ adoption: CanvasTooltipAdoptionBindingSnapshot,
797
+ };
798
+
799
+ /// Steps 2–4 of the choke point for the content-moved causes
800
+ /// (scroll and layout adoption): re-hit-test the stored (or
801
+ /// live) pointer position against the current tree, re-check
802
+ /// the content hold against the tooltip's CURRENT frame, then
803
+ /// resolve transitions. Hover ownership follows the content
804
+ /// honestly — a trigger moved out from under the pointer
805
+ /// releases (armed disarms, shown hides with the usual
806
+ /// pointer-hide warmth), one moved under it arms the normal
807
+ /// delay or warm-shows — and with no trustworthy position
808
+ /// (a keyboard-only session, or the pointer left the view) the
809
+ /// pointer's whole conversation CLOSES rather than guesses.
810
+ /// Focus-shown tooltips survive every arm of this: the keyboard
811
+ /// holds them.
812
+ fn reconcileCanvasTooltipIntentForMovedContent(self: *Runtime, view_index: usize, live_point: ?geometry.PointF, cause: CanvasTooltipMovedContentCause) anyerror!void {
813
+ const view = &self.views[view_index];
814
+ // 2. Hover re-hit-test from the stored position (the wheel
815
+ // passes its live point; whichever is used is the truth the
816
+ // rest of this reconcile measures against).
817
+ const effective_point = live_point orelse view.canvas_last_pointer_position orelse {
818
+ view.reconcileCanvasWidgetRenderStateAfterScroll(null);
819
+ try closeCanvasTooltipPointerIntent(self, view_index);
820
+ // A keyboard-only session has no pointer to place, but
821
+ // the KEYBOARD's standing intent is not pointer intent:
822
+ // an adoption that swaps the focus-visible owner's
823
+ // tooltip must still reveal it (below), exactly like
824
+ // the focus-shown tooltip survives the close above.
825
+ switch (cause) {
826
+ .adoption => |bindings| try reconcileCanvasTooltipIntentForAdoptedFocusBinding(self, view_index, bindings),
827
+ .scroll => {},
828
+ }
829
+ return;
830
+ };
831
+ const previous_hovered_id = view.canvas_widget_hovered_id;
832
+ view.reconcileCanvasWidgetRenderStateAfterScroll(effective_point);
833
+ const next_hovered_id = view.canvas_widget_hovered_id;
834
+
835
+ // 3. Content-hold containment re-check, BEFORE the
836
+ // transition step and INDEPENDENT of whether the hover id
837
+ // changed: a content-held tooltip reads hovered_id == 0 (or
838
+ // the id of whatever sits beneath its frame) on both sides
839
+ // of a scroll or rebuild, so gating this on a hover
840
+ // TRANSITION let content slide out from under the hold
841
+ // while the tooltip stayed pinned to a pointer that no
842
+ // longer touched it. Still inside the current frame — or
843
+ // still on the owner — keeps the hold and re-seeds the
844
+ // corridor apex; moved out breaks it on this reconcile
845
+ // itself, with the usual pointer-hide warmth and
846
+ // deliberately NO transit corridor: the content moved, not
847
+ // the pointer, and a corridor hold here would let
848
+ // continuous scrolls or rebuilds re-arm the bounded grace
849
+ // forever under a stationary pointer.
850
+ var held_released = false;
851
+ if (view.canvas_tooltip_shown_id != 0 and !view.canvas_tooltip_shown_from_focus) {
852
+ const on_owner = next_hovered_id != 0 and next_hovered_id == view.canvas_tooltip_shown_owner_id;
853
+ if (on_owner or canvasTooltipShownContentContains(view, effective_point)) {
854
+ view.canvas_tooltip_pointer_from = effective_point;
855
+ view.canvas_tooltip_transit_deadline_ns = 0;
856
+ } else {
857
+ hideShownCanvasTooltipWithWarmth(view, canvasRenderAnimationStartNsForView(view));
858
+ try commitCanvasTooltipVisibility(self, view_index);
859
+ held_released = true;
860
+ }
861
+ }
862
+
863
+ // 4. Transition resolution. Scroll transitions run
864
+ // point-blind (no corridor — see the mode doc above);
865
+ // adoption's pass the stored point through.
866
+ const transition_point: ?geometry.PointF = switch (cause) {
867
+ .scroll => null,
868
+ .adoption => effective_point,
869
+ };
870
+ if (next_hovered_id != previous_hovered_id) {
871
+ try updateCanvasTooltipIntentForHoverChange(self, view_index, next_hovered_id, transition_point);
872
+ } else if (held_released and next_hovered_id != 0) {
873
+ // Content-hold release reprocessing, the content-moved
874
+ // shape: the hold broke while the hover id stayed put
875
+ // on a trigger the pointer had reached THROUGH the
876
+ // tooltip's frame. That id was recorded under stale
877
+ // ownership — the frame claimed the hover — so it never
878
+ // stepped the machine; reprocess it as a fresh
879
+ // transition so the trigger under the pointer arms (or
880
+ // warm-shows) per the normal rules.
881
+ try updateCanvasTooltipIntentForHoverChange(self, view_index, next_hovered_id, transition_point);
882
+ }
883
+ // Binding-change step, adoption only: the transition gate
884
+ // above sees HOVER deltas and the prune sees dead
885
+ // REGISTERS, so a rebuild that mounts, replaces, rekeys, or
886
+ // reparents a tooltip beneath an owner whose own ID stayed
887
+ // put slips past both — the new tooltip could never arm
888
+ // until the pointer left and re-entered. Compare the
889
+ // pre-adoption snapshot's owned-tooltip IDs against the
890
+ // adopted tree and step the machine for a changed binding.
891
+ //
892
+ // The matrix (mount/replace/rekey x hovered/focused x
893
+ // armed/shown):
894
+ // - HOVERED owner, tooltip MOUNTED (owned none before):
895
+ // no register existed, so the prune had nothing to do —
896
+ // arm a FRESH dwell here. Mounting mid-hover must not
897
+ // insta-show: the dwell IS the intent filter, and a
898
+ // rebuild proves nothing about the user.
899
+ // - HOVERED owner, tooltip REPLACED/REKEYED/REPARENTED
900
+ // while ARMED or SHOWN: the transactional prune already
901
+ // reset the old register (its binding died — owner no
902
+ // longer resolves to that tooltip) and closed the warm
903
+ // window with it; the pre-diff stamp reported a shown
904
+ // tooltip's hide honestly. This step only re-earns the
905
+ // NEW tooltip per the hover rule: a fresh dwell.
906
+ // - HOVERED owner, tooltip UNMOUNTED: entirely the
907
+ // prune's case (armed disarms, shown hides); the
908
+ // zero-ID guard below leaves nothing to arm.
909
+ // - FOCUS-VISIBLE owner (keyboard provenance only): a
910
+ // newly bound tooltip reveals IMMEDIATELY — focus is
911
+ // the user's standing intent, and the focus reveal
912
+ // exists precisely so keyboard users never race a
913
+ // pointer timing (shadcn's Base UI-backed default:
914
+ // instant open on focus-visible). Replace/rekey under
915
+ // a focus-SHOWN tooltip: the prune hid the old, this
916
+ // reveals the new.
917
+ // - Both standing on ONE owner: hover arms first, then
918
+ // the focus step reveals and clears the now-redundant
919
+ // dwell (focus re-affirms; blur owns the hide).
920
+ //
921
+ // Warm window: a binding change is not a pointer sweep, so
922
+ // no NEW warmth is minted here. Delegating to the normal
923
+ // hover-change step follows the existing warm semantics
924
+ // exactly: replace/rekey never warm-shows (the prune
925
+ // closed the window when it killed the old binding), while
926
+ // a plain MOUNT under warmth genuinely earned by prior
927
+ // pointer activity keeps the instant-show courtesy.
928
+ switch (cause) {
929
+ .adoption => |bindings| {
930
+ if (bindings.hovered_owner_id != 0 and next_hovered_id == bindings.hovered_owner_id) {
931
+ const adopted_tooltip_id = view.canvasWidgetOwnedTooltipIdForOwner(next_hovered_id);
932
+ if (adopted_tooltip_id != 0 and adopted_tooltip_id != bindings.hovered_tooltip_id) {
933
+ try updateCanvasTooltipIntentForHoverChange(self, view_index, next_hovered_id, transition_point);
934
+ }
935
+ }
936
+ try reconcileCanvasTooltipIntentForAdoptedFocusBinding(self, view_index, bindings);
937
+ },
938
+ .scroll => {},
939
+ }
940
+ // Whichever position drove this reconcile re-seeds the
941
+ // corridor apex for whatever the step armed or warm-showed,
942
+ // so a later leave fans out from the truth.
943
+ if (next_hovered_id != 0 and (view.canvas_tooltip_armed_owner_id == next_hovered_id or
944
+ (view.canvas_tooltip_shown_owner_id == next_hovered_id and !view.canvas_tooltip_shown_from_focus)))
945
+ {
946
+ view.canvas_tooltip_pointer_from = effective_point;
947
+ }
948
+ }
949
+
950
+ /// The focus half of the adoption binding-change step: when
951
+ /// the standing KEYBOARD focus-visible owner survived the
952
+ /// rebuild and now owns a DIFFERENT tooltip than it did in the
953
+ /// outgoing tree, reveal the new one immediately through the
954
+ /// normal focus-visible transition. Focus is the user's
955
+ /// standing declared intent — the focus reveal path exists so
956
+ /// keyboard users get hover-revealed content without pointer
957
+ /// timing (shadcn's Base UI-backed instant focus-visible open;
958
+ /// WCAG 1.4.13) — so a tooltip the rebuild bound beneath that
959
+ /// focus inherits the reveal instead of waiting for focus to
960
+ /// leave and return. Guards, in order: no standing keyboard
961
+ /// ring captured (pointer/programmatic provenance never
962
+ /// reveals — the click-focus exclusion), the view lost focus,
963
+ /// the ring moved or was pruned during adoption, or the
964
+ /// binding did not actually change (the byte-identical
965
+ /// rebuild path must stay silent).
966
+ fn reconcileCanvasTooltipIntentForAdoptedFocusBinding(self: *Runtime, view_index: usize, bindings: CanvasTooltipAdoptionBindingSnapshot) anyerror!void {
967
+ const view = &self.views[view_index];
968
+ if (bindings.focus_visible_owner_id == 0) return;
969
+ if (!view.focused or !view.canvas_widget_focus_visible_keyboard) return;
970
+ if (view.canvas_widget_focus_visible_id != bindings.focus_visible_owner_id) return;
971
+ const adopted_tooltip_id = view.canvasWidgetOwnedTooltipIdForOwner(bindings.focus_visible_owner_id);
972
+ if (adopted_tooltip_id == 0 or adopted_tooltip_id == bindings.focus_visible_tooltip_id) return;
973
+ try updateCanvasTooltipIntentForFocusVisibleChange(self, view_index, bindings.focus_visible_owner_id);
974
+ }
975
+
976
+ /// The anchored-tooltip hover-intent state machine, stepped on
977
+ /// every hover-target transition. Anchored tooltips are
978
+ /// runtime-owned presentation chrome — the model never hears
979
+ /// hover:
980
+ /// - the pointer reaches a tooltip's trigger: arm the show
981
+ /// delay (`tooltip-delay`, default the
982
+ /// `tooltip_show_delay_ms` token) on the recorded clock —
983
+ /// unless the shared warm window is open or the delay is 0,
984
+ /// which show immediately;
985
+ /// - the pointer leaves before the delay fires: disarm, show
986
+ /// nothing (sweeping a toolbar flashes no tooltips);
987
+ /// - a SHOWN tooltip's trigger loses the pointer: hide it and
988
+ /// open the warm window (`tooltip_warm_window_ms`), so the
989
+ /// neighboring trigger explains itself instantly.
990
+ /// `now` is `canvasRenderAnimationStartNsForView` — the freshest
991
+ /// journaled input/frame timestamp, never a wall clock — so a
992
+ /// recorded sweep replays every show/hide frame byte-identically.
993
+ ///
994
+ /// `point` is the pointer position that produced this
995
+ /// transition, when one exists: it lets a SHOWN tooltip hold
996
+ /// through a move into its own frame or across the anchor gap
997
+ /// (see `updateCanvasTooltipIntentForPointerTravel`). Pass null
998
+ /// for point-blind steps — the scroll paths, which are
999
+ /// deliberately corridor-free — to keep the classic
1000
+ /// immediate-hide semantics. (Pointer `.cancel` never reaches
1001
+ /// here: it closes the whole pointer conversation through
1002
+ /// `closeCanvasTooltipPointerIntent` ahead of the transition
1003
+ /// gate.)
1004
+ fn updateCanvasTooltipIntentForHoverChange(self: *Runtime, view_index: usize, next_hovered_id: canvas.ObjectId, point: ?geometry.PointF) anyerror!void {
1005
+ const view = &self.views[view_index];
1006
+ const now_ns = canvasRenderAnimationStartNsForView(view);
1007
+ const tooltip_index: ?usize = blk: {
1008
+ if (next_hovered_id == 0) break :blk null;
1009
+ const hovered_index = view.canvasWidgetNodeIndexById(next_hovered_id) orelse break :blk null;
1010
+ break :blk view.canvasWidgetOwnedTooltipIndex(hovered_index);
1011
+ };
1012
+ const tooltip_id: canvas.ObjectId = if (tooltip_index) |node_index| view.widget_layout_nodes[node_index].widget.id else 0;
1013
+
1014
+ var shown_changed = false;
1015
+ // The pointer sitting inside the shown tooltip's own frame
1016
+ // holds it open (WCAG 1.4.13: hover-revealed content must be
1017
+ // hoverable; Base UI tooltips default `hoverable`). The
1018
+ // tooltip stays OUT of hit-testing — it is presentation
1019
+ // chrome, and claiming hover or presses would put a
1020
+ // non-interactive surface into interaction routing and the
1021
+ // a11y tree's hover story — so the hold is a geometric test
1022
+ // against the shown frame here in the intent machine.
1023
+ const held_by_content = canvasTooltipShownContentContains(view, point);
1024
+ // A FOCUS-shown tooltip is held by the keyboard, not the
1025
+ // pointer: hover leaving some other widget must not tear it
1026
+ // down (the shadcn/Base UI focus-open holds through pointer
1027
+ // traffic). Only the focus path — or a completed pointer
1028
+ // intent below, which takes over the single shown slot —
1029
+ // moves it.
1030
+ if (view.canvas_tooltip_shown_id != 0 and view.canvas_tooltip_shown_id != tooltip_id and !view.canvas_tooltip_shown_from_focus) {
1031
+ // Crossing the anchor gap: a leave whose pointer is still
1032
+ // inside the transit corridor keeps the tooltip up (the
1033
+ // travel step opens the bounded grace); one that owns a
1034
+ // DIFFERENT tooltip transfers immediately (the warm-window
1035
+ // sweep), and everything else hides on the spot.
1036
+ const held_in_transit = tooltip_id == 0 and canvasTooltipTravelRegionContains(view, point);
1037
+ if (!held_by_content and !held_in_transit) {
1038
+ hideShownCanvasTooltipWithWarmth(view, now_ns);
1039
+ shown_changed = true;
1040
+ }
1041
+ }
1042
+ if (view.canvas_tooltip_armed_id != 0 and view.canvas_tooltip_armed_id != tooltip_id) {
1043
+ view.canvas_tooltip_armed_id = 0;
1044
+ view.canvas_tooltip_armed_owner_id = 0;
1045
+ view.canvas_tooltip_deadline_ns = 0;
1046
+ }
1047
+ if (tooltip_index) |node_index| {
1048
+ // Hover reached through the shown tooltip's frame belongs
1049
+ // to the tooltip, not to whatever sits beneath it: never
1050
+ // arm (or warm-show) a trigger the pointer is not
1051
+ // actually on. And an inactive app or a non-key window
1052
+ // arms and reveals nothing at all (see
1053
+ // canvasTooltipIntentActionAllowed) — hover reaches
1054
+ // non-key windows on always-active-tracking hosts, and
1055
+ // the adoption binding-reconcile funnels through here
1056
+ // even mid-deactivation; the hide/disarm arms above
1057
+ // deliberately stay ungated.
1058
+ if (view.canvas_tooltip_shown_id != tooltip_id and tooltip_id != 0 and !held_by_content and
1059
+ canvasTooltipIntentActionAllowed(self, view_index))
1060
+ {
1061
+ const delay_ns = canvasTooltipShowDelayNs(view.widget_layout_nodes[node_index].widget, view.widget_tokens);
1062
+ if (delay_ns == 0 or now_ns < view.canvas_tooltip_warm_until_ns) {
1063
+ view.canvas_tooltip_shown_id = tooltip_id;
1064
+ view.canvas_tooltip_shown_owner_id = next_hovered_id;
1065
+ view.canvas_tooltip_shown_from_focus = false;
1066
+ view.canvas_tooltip_armed_id = 0;
1067
+ view.canvas_tooltip_armed_owner_id = 0;
1068
+ view.canvas_tooltip_deadline_ns = 0;
1069
+ view.canvas_tooltip_transit_deadline_ns = 0;
1070
+ if (point) |value| view.canvas_tooltip_pointer_from = value;
1071
+ shown_changed = true;
1072
+ } else if (view.canvas_tooltip_armed_id != tooltip_id) {
1073
+ view.canvas_tooltip_armed_id = tooltip_id;
1074
+ view.canvas_tooltip_armed_owner_id = next_hovered_id;
1075
+ view.canvas_tooltip_deadline_ns = now_ns + delay_ns;
1076
+ // Seed the transit apex at arm time: a dwell that
1077
+ // completes on the frame clock has no pointer
1078
+ // position of its own, and the corridor for the
1079
+ // eventual leave must fan out from a point that
1080
+ // was really on the trigger.
1081
+ if (point) |value| view.canvas_tooltip_pointer_from = value;
1082
+ }
1083
+ }
1084
+ }
1085
+ if (shown_changed) try commitCanvasTooltipVisibility(self, view_index);
1086
+ }
1087
+
1088
+ /// The travel half of hoverable tooltip content, stepped on
1089
+ /// EVERY pointer move while a pointer-shown tooltip is up (the
1090
+ /// hover-change step alone cannot see moves that stay on one
1091
+ /// hover target while crossing the anchor gap or the tooltip's
1092
+ /// frame, because the tooltip is deliberately not hit-tested):
1093
+ /// - on the owning trigger or inside the shown tooltip's
1094
+ /// frame: held — remember the position as the corridor apex
1095
+ /// and close any running transit;
1096
+ /// - outside both but inside the transit corridor (the convex
1097
+ /// fan from the apex to the trigger and tooltip frames —
1098
+ /// Base UI's safe-polygon shape): keep the tooltip up and
1099
+ /// re-arm the bounded deadline, so a slow deliberate
1100
+ /// crossing never races a timer (WCAG 1.4.13) while a
1101
+ /// pointer that parks in the gap still resolves on the
1102
+ /// frame clock, replay-deterministically;
1103
+ /// - outside the corridor: hide with the usual pointer-hide
1104
+ /// warm window.
1105
+ /// The safe-polygon corridor was chosen over a pure transit
1106
+ /// time window because it keeps every motion AWAY from the
1107
+ /// tooltip hiding on the move itself — the pre-hoverable
1108
+ /// semantics tests and recorded sessions pin — and holds only
1109
+ /// motion that is honestly en route to the content; the
1110
+ /// deadline bounds it so replay and long-idle behavior stay
1111
+ /// deterministic (Base UI's safe polygon is unbounded).
1112
+ fn updateCanvasTooltipIntentForPointerTravel(self: *Runtime, view_index: usize, next_hovered_id: canvas.ObjectId, point: geometry.PointF) anyerror!void {
1113
+ const view = &self.views[view_index];
1114
+ if (view.canvas_tooltip_shown_id == 0 or view.canvas_tooltip_shown_from_focus) {
1115
+ view.canvas_tooltip_transit_deadline_ns = 0;
1116
+ return;
1117
+ }
1118
+ const now_ns = canvasRenderAnimationStartNsForView(view);
1119
+ const on_owner = next_hovered_id != 0 and next_hovered_id == view.canvas_tooltip_shown_owner_id;
1120
+ if (on_owner or canvasTooltipShownContentContains(view, point)) {
1121
+ view.canvas_tooltip_pointer_from = point;
1122
+ view.canvas_tooltip_transit_deadline_ns = 0;
1123
+ return;
1124
+ }
1125
+ if (canvasTooltipTravelRegionContains(view, point)) {
1126
+ view.canvas_tooltip_transit_deadline_ns = now_ns + tooltip_transit_grace_ms * std.time.ns_per_ms;
1127
+ return;
1128
+ }
1129
+ hideShownCanvasTooltipWithWarmth(view, now_ns);
1130
+ try commitCanvasTooltipVisibility(self, view_index);
1131
+ }
1132
+
1133
+ /// Hide the pointer-shown tooltip and open the shared warm
1134
+ /// window — the one pointer-hide shape every path shares. Any
1135
+ /// in-flight transit grace dies with the tooltip it was holding.
1136
+ fn hideShownCanvasTooltipWithWarmth(view: anytype, now_ns: u64) void {
1137
+ view.canvas_tooltip_shown_id = 0;
1138
+ view.canvas_tooltip_shown_owner_id = 0;
1139
+ view.canvas_tooltip_shown_from_focus = false;
1140
+ view.canvas_tooltip_warm_until_ns = now_ns + canvasTooltipWarmWindowNs(view.widget_tokens);
1141
+ view.canvas_tooltip_transit_deadline_ns = 0;
1142
+ }
1143
+
1144
+ /// Whether `point` sits inside the SHOWN tooltip's own frame —
1145
+ /// the hoverable-content test. Null points (cancel, point-blind
1146
+ /// scroll steps) never hold.
1147
+ fn canvasTooltipShownContentContains(view: anytype, point: ?geometry.PointF) bool {
1148
+ const value = point orelse return false;
1149
+ if (view.canvas_tooltip_shown_id == 0 or view.canvas_tooltip_shown_from_focus) return false;
1150
+ const node_index = view.canvasWidgetNodeIndexById(view.canvas_tooltip_shown_id) orelse return false;
1151
+ return view.widget_layout_nodes[node_index].frame.containsPoint(value);
1152
+ }
1153
+
1154
+ /// Whether `point` sits inside the transit corridor between the
1155
+ /// pointer's last held position and the shown tooltip: the
1156
+ /// convex fan from the apex to the tooltip's frame plus the fan
1157
+ /// back to the owning trigger's frame (so content-to-trigger
1158
+ /// returns cross the same gap). This is Base UI's safe-polygon
1159
+ /// shape, evaluated against journaled pointer positions only.
1160
+ fn canvasTooltipTravelRegionContains(view: anytype, point: ?geometry.PointF) bool {
1161
+ const value = point orelse return false;
1162
+ if (view.canvas_tooltip_shown_id == 0 or view.canvas_tooltip_shown_from_focus) return false;
1163
+ const apex = view.canvas_tooltip_pointer_from;
1164
+ if (view.canvasWidgetNodeIndexById(view.canvas_tooltip_shown_id)) |node_index| {
1165
+ if (canvasTooltipTravelFanContains(apex, view.widget_layout_nodes[node_index].frame, value)) return true;
1166
+ }
1167
+ if (view.canvasWidgetNodeIndexById(view.canvas_tooltip_shown_owner_id)) |node_index| {
1168
+ if (canvasTooltipTravelFanContains(apex, view.widget_layout_nodes[node_index].frame, value)) return true;
1169
+ }
1170
+ return false;
1171
+ }
1172
+
1173
+ /// Point-in-convex-hull for the fan from `apex` over `rect`:
1174
+ /// the rect itself plus the four apex-to-adjacent-corner
1175
+ /// triangles cover exactly the hull of {apex} ∪ rect.
1176
+ fn canvasTooltipTravelFanContains(apex: geometry.PointF, rect: geometry.RectF, point: geometry.PointF) bool {
1177
+ if (rect.containsPoint(point)) return true;
1178
+ const corners = [4]geometry.PointF{
1179
+ .{ .x = rect.x, .y = rect.y },
1180
+ .{ .x = rect.maxX(), .y = rect.y },
1181
+ .{ .x = rect.maxX(), .y = rect.maxY() },
1182
+ .{ .x = rect.x, .y = rect.maxY() },
1183
+ };
1184
+ inline for (0..4) |index| {
1185
+ if (canvasTooltipPointInTriangle(point, apex, corners[index], corners[(index + 1) % 4])) return true;
1186
+ }
1187
+ return false;
1188
+ }
1189
+
1190
+ /// Degeneracy threshold for the corridor fan's triangles, in
1191
+ /// CANVAS POINTS — the same logical-point scale every corridor
1192
+ /// coordinate (pointer positions, widget frames) is measured
1193
+ /// in, so the tolerance means the same thing at every display
1194
+ /// scale factor. A triangle whose apex sits within half a
1195
+ /// point of the line through its other two vertices is a
1196
+ /// sliver no pointer path could meaningfully travel through:
1197
+ /// treating it as degenerate loses no honest corridor area,
1198
+ /// while the exact-collinear case (the apex parked precisely
1199
+ /// on a tooltip edge's line) stops reading as "contains the
1200
+ /// whole infinite line".
1201
+ const tooltip_corridor_epsilon: f32 = 0.5;
1202
+
1203
+ /// Sign-consistency point-in-triangle (boundary counts as
1204
+ /// inside). Degenerate triangles get an explicit guard: when
1205
+ /// the apex lands on (or within the epsilon of) the line
1206
+ /// through the rect edge, ALL THREE cross products are ~0 for
1207
+ /// every point collinear with the collapsed triangle, so the
1208
+ /// bare sign test held the corridor open along the entire
1209
+ /// infinite line — motion arbitrarily far along it kept
1210
+ /// re-arming the transit grace and the tooltip never closed. A
1211
+ /// collapsed triangle contains only its boundary SEGMENTS
1212
+ /// (between its actual vertices), never the line beyond them.
1213
+ fn canvasTooltipPointInTriangle(p: geometry.PointF, a: geometry.PointF, b: geometry.PointF, c: geometry.PointF) bool {
1214
+ // |cross(c, a, b)| is twice the triangle's area — base
1215
+ // edge times the apex's height over it. Comparing its
1216
+ // square against epsilon² · longest-edge² asks "is the
1217
+ // triangle thinner than the epsilon over its LONGEST
1218
+ // base?", which is scale-honest for slivers of any length
1219
+ // (a flat epsilon on the area would misclassify long thin
1220
+ // fans whose area grows with the base).
1221
+ const area_x2 = canvasTooltipCross(c, a, b);
1222
+ const longest_sq = @max(
1223
+ canvasTooltipDistanceSq(a, b),
1224
+ @max(canvasTooltipDistanceSq(b, c), canvasTooltipDistanceSq(c, a)),
1225
+ );
1226
+ if (area_x2 * area_x2 <= tooltip_corridor_epsilon * tooltip_corridor_epsilon * longest_sq) {
1227
+ return canvasTooltipPointOnSegment(p, a, b) or
1228
+ canvasTooltipPointOnSegment(p, b, c) or
1229
+ canvasTooltipPointOnSegment(p, c, a);
1230
+ }
1231
+ const d1 = canvasTooltipCross(p, a, b);
1232
+ const d2 = canvasTooltipCross(p, b, c);
1233
+ const d3 = canvasTooltipCross(p, c, a);
1234
+ const has_neg = d1 < 0 or d2 < 0 or d3 < 0;
1235
+ const has_pos = d1 > 0 or d2 > 0 or d3 > 0;
1236
+ return !(has_neg and has_pos);
1237
+ }
1238
+
1239
+ /// Whether `p` lies on the segment [a, b] — between the actual
1240
+ /// endpoints, within the corridor epsilon perpendicular to it —
1241
+ /// the only containment a collapsed fan triangle keeps.
1242
+ fn canvasTooltipPointOnSegment(p: geometry.PointF, a: geometry.PointF, b: geometry.PointF) bool {
1243
+ const dx = b.x - a.x;
1244
+ const dy = b.y - a.y;
1245
+ const len_sq = dx * dx + dy * dy;
1246
+ if (len_sq == 0) {
1247
+ // The segment itself collapsed to a point.
1248
+ return canvasTooltipDistanceSq(p, a) <= tooltip_corridor_epsilon * tooltip_corridor_epsilon;
1249
+ }
1250
+ // Perpendicular distance: |cross| = distance · |b - a|.
1251
+ const cross = canvasTooltipCross(p, a, b);
1252
+ if (cross * cross > tooltip_corridor_epsilon * tooltip_corridor_epsilon * len_sq) return false;
1253
+ // Projection onto the segment stays within its endpoints.
1254
+ const dot = (p.x - a.x) * dx + (p.y - a.y) * dy;
1255
+ return dot >= 0 and dot <= len_sq;
1256
+ }
1257
+
1258
+ fn canvasTooltipDistanceSq(a: geometry.PointF, b: geometry.PointF) f32 {
1259
+ const dx = b.x - a.x;
1260
+ const dy = b.y - a.y;
1261
+ return dx * dx + dy * dy;
1262
+ }
1263
+
1264
+ fn canvasTooltipCross(p: geometry.PointF, a: geometry.PointF, b: geometry.PointF) f32 {
1265
+ return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
1266
+ }
1267
+
1268
+ /// The armed show delay fires on the presented frame's RECORDED
1269
+ /// timestamp (never a wall clock) — the layout-tween discipline —
1270
+ /// so session replay reproduces the exact frame a dwell's tooltip
1271
+ /// appears on. The frame pump in planCanvasFrameForView keeps
1272
+ /// frames coming while a delay is armed.
1273
+ pub fn advanceCanvasTooltipIntentForFrame(self: *Runtime, view_index: usize, timestamp_ns: u64) anyerror!void {
1274
+ const view = &self.views[view_index];
1275
+ // A suppressed view promotes nothing: with the arm paths
1276
+ // gated on the same predicate and both suppression edges
1277
+ // (app deactivation, window key-loss) resetting the armed
1278
+ // registers, a pending dwell here should be unreachable —
1279
+ // but the promote IS a reveal, so it honors the same gate,
1280
+ // and it DISARMS rather than defers (a deferred deadline in
1281
+ // the past would fire on the first frame after re-key: a
1282
+ // spontaneous reveal, which re-activation must never make).
1283
+ if (!canvasTooltipIntentActionAllowed(self, view_index)) {
1284
+ view.canvas_tooltip_armed_id = 0;
1285
+ view.canvas_tooltip_armed_owner_id = 0;
1286
+ view.canvas_tooltip_deadline_ns = 0;
1287
+ view.canvas_tooltip_transit_deadline_ns = 0;
1288
+ return;
1289
+ }
1290
+ // A transit grace that ran out resolves here, on the same
1291
+ // recorded frame clock the show delay fires on: the pointer
1292
+ // parked in the corridor without arriving, so the tooltip
1293
+ // hides with the usual pointer-hide warmth — a
1294
+ // deterministic frame in replay, exactly like the show.
1295
+ if (view.canvas_tooltip_shown_id != 0 and !view.canvas_tooltip_shown_from_focus and
1296
+ view.canvas_tooltip_transit_deadline_ns != 0 and timestamp_ns >= view.canvas_tooltip_transit_deadline_ns)
1297
+ {
1298
+ hideShownCanvasTooltipWithWarmth(view, timestamp_ns);
1299
+ try commitCanvasTooltipVisibility(self, view_index);
1300
+ }
1301
+ if (view.canvas_tooltip_armed_id == 0) return;
1302
+ if (timestamp_ns < view.canvas_tooltip_deadline_ns) return;
1303
+ view.canvas_tooltip_shown_id = view.canvas_tooltip_armed_id;
1304
+ view.canvas_tooltip_shown_owner_id = view.canvas_tooltip_armed_owner_id;
1305
+ view.canvas_tooltip_shown_from_focus = false;
1306
+ view.canvas_tooltip_armed_id = 0;
1307
+ view.canvas_tooltip_armed_owner_id = 0;
1308
+ view.canvas_tooltip_deadline_ns = 0;
1309
+ view.canvas_tooltip_transit_deadline_ns = 0;
1310
+ try commitCanvasTooltipVisibility(self, view_index);
1311
+ }
1312
+
1313
+ /// Pointer-down resets the WHOLE intent machine: a press on the
1314
+ /// armed trigger cancels the pending reveal (no tooltip mid- or
1315
+ /// post-activation), a press on a shown tooltip's trigger
1316
+ /// dismisses it, and the warm window closes with it — an
1317
+ /// activated control has explained itself, so the post-click
1318
+ /// hover must earn the full delay again instead of instantly
1319
+ /// re-showing. This is shadcn's Base UI-backed default (a
1320
+ /// trigger press closes its tooltip) and the platform norm —
1321
+ /// macOS help tags vanish on any click. Any-press semantics are
1322
+ /// deliberate: a down elsewhere has already moved hover or is
1323
+ /// moving focus off the trigger, so the broad reset never fights
1324
+ /// the narrower paths, and it keeps window-drag and capture
1325
+ /// corners honest.
1326
+ /// The view-blur seam: a view that stops being FOCUSED drops
1327
+ /// its whole tooltip conversation (see the `.view_blur` arm of
1328
+ /// the choke point — the focus-shown tooltip's blur-hides
1329
+ /// contract, shadcn's Base UI-backed default, extended to the
1330
+ /// VIEW). Both focus seams call this: per-view focus moves
1331
+ /// (setFocusedView, from input and from the focus commands) and
1332
+ /// window-level focus loss (clearFocusedView).
1333
+ pub fn resetCanvasTooltipIntentForViewBlur(self: *Runtime, view_index: usize) anyerror!void {
1334
+ try reconcileCanvasTooltipIntent(self, view_index, .view_blur);
1335
+ }
1336
+
1337
+ /// The window key-loss seam: a window that stops being the
1338
+ /// focused (key) window drops the tooltip conversation in EVERY
1339
+ /// one of its views — the same `.view_blur` semantics as a
1340
+ /// per-view focus move, WITHOUT touching `view.focused` (the
1341
+ /// per-window focus memory survives key-loss so focus returns
1342
+ /// where it was when the window re-keys; macOS keeps a
1343
+ /// non-key window's first responder the same way). A tooltip is
1344
+ /// transient explanation for a conversation the user just left
1345
+ /// with the whole window: keeping one painted would also keep
1346
+ /// its semantics node claiming visible in the a11y tree of a
1347
+ /// window that no longer hears the keyboard.
1348
+ /// `canvas_last_pointer_position` deliberately survives, like
1349
+ /// every `.view_blur`: pointer truth belongs to the pointer
1350
+ /// channel. A host whose hover delivery is key-window-scoped
1351
+ /// (the macOS tracking areas are) announces the stream's end
1352
+ /// with its own pointer cancel, which clears the store through
1353
+ /// the existing cause; a host that keeps hovering non-key
1354
+ /// windows keeps the store truthful for point-blind
1355
+ /// re-hit-tests. Re-keying reveals nothing by construction —
1356
+ /// both reveal paths are transition-edge-triggered (hover
1357
+ /// change, focus-visible arrival) and a key gain replays
1358
+ /// neither.
1359
+ pub fn resetCanvasTooltipIntentForWindowKeyLoss(self: *Runtime, window_id: platform.WindowId) anyerror!void {
1360
+ for (0..self.view_count) |view_index| {
1361
+ if (self.views[view_index].window_id != window_id) continue;
1362
+ try reconcileCanvasTooltipIntent(self, view_index, .view_blur);
1363
+ }
1364
+ }
1365
+
1366
+ /// The app-deactivation seam: the whole app resigning active is
1367
+ /// key-loss for every window at once — every canvas view in
1368
+ /// every window drops its tooltip conversation (focus-shown and
1369
+ /// pointer-owned alike; both the keyboard and the pointer
1370
+ /// conversation ended with the app). Reactivation reveals
1371
+ /// nothing for the same edge-trigger reason as re-keying above.
1372
+ pub fn resetCanvasTooltipIntentForAppDeactivation(self: *Runtime) anyerror!void {
1373
+ for (0..self.view_count) |view_index| {
1374
+ try reconcileCanvasTooltipIntent(self, view_index, .view_blur);
1375
+ }
1376
+ }
1377
+
1378
+ /// The consumed-pointer seam, keyed by the raw input's view
1379
+ /// identity: pointer-carrying inputs that never reach the
1380
+ /// widget interaction pipeline — the secondary-button stream
1381
+ /// consumed by the context-menu gesture and primary downs
1382
+ /// consumed by a window-drag region. EVERY such event still
1383
+ /// feeds the choke point: position bookkeeping happens even
1384
+ /// when the interaction path early-returned (a later
1385
+ /// point-blind reconcile must hit-test where the pointer really
1386
+ /// is), a consumed down still dismisses ("pointer-down
1387
+ /// dismisses" is documented for ANY down), and a consumed
1388
+ /// cancel is still the pointer leaving the view.
1389
+ pub fn reconcileCanvasTooltipIntentForConsumedPointerInput(self: *Runtime, input_event: GpuSurfaceInputEvent) anyerror!void {
1390
+ const index = runtimeFindViewIndex(self, input_event.window_id, input_event.label) orelse return;
1391
+ if (self.views[index].kind != .gpu_surface) return;
1392
+ const cause: CanvasTooltipReconcileCause = switch (input_event.kind) {
1393
+ .pointer_cancel => .pointer_cancel,
1394
+ .pointer_down, .pointer_up, .pointer_move, .pointer_drag => .{ .consumed_pointer = .{
1395
+ .point = geometry.PointF.init(input_event.x, input_event.y),
1396
+ .down = input_event.kind == .pointer_down,
1397
+ } },
1398
+ else => return,
1399
+ };
1400
+ try reconcileCanvasTooltipIntent(self, index, cause);
1401
+ }
1402
+
1403
+ fn updateCanvasTooltipIntentForPress(self: *Runtime, view_index: usize) anyerror!void {
1404
+ const view = &self.views[view_index];
1405
+ view.canvas_tooltip_armed_id = 0;
1406
+ view.canvas_tooltip_armed_owner_id = 0;
1407
+ view.canvas_tooltip_deadline_ns = 0;
1408
+ view.canvas_tooltip_warm_until_ns = 0;
1409
+ view.canvas_tooltip_transit_deadline_ns = 0;
1410
+ if (view.canvas_tooltip_shown_id == 0) return;
1411
+ view.canvas_tooltip_shown_id = 0;
1412
+ view.canvas_tooltip_shown_owner_id = 0;
1413
+ view.canvas_tooltip_shown_from_focus = false;
1414
+ try commitCanvasTooltipVisibility(self, view_index);
1415
+ }
1416
+
1417
+ /// Keyboard FOCUS-VISIBLE reaching a tooltip-owning trigger
1418
+ /// reveals its tooltip IMMEDIATELY — keyboard navigation is
1419
+ /// deliberate, so there is no dwell to prove intent (shadcn's
1420
+ /// Base UI-backed default: tooltips open instantly on keyboard
1421
+ /// focus, and WCAG 1.4.13 wants hover/focus-revealed content
1422
+ /// reachable without pointer timing). Focus moving on (or
1423
+ /// clearing) hides a focus-shown tooltip just as immediately,
1424
+ /// and deliberately WITHOUT opening the pointer's warm window:
1425
+ /// warmth is a pointer-sweep courtesy, and tabbing through a
1426
+ /// toolbar should not make later hovers instant. Only the
1427
+ /// keyboard focus path calls this — pointer-established
1428
+ /// focus-visible (an editable's caret contract) keeps tooltips
1429
+ /// on the hover-intent path, mirroring Base UI's focus-visible
1430
+ /// guard against click-focus opens.
1431
+ fn updateCanvasTooltipIntentForFocusVisibleChange(self: *Runtime, view_index: usize, focus_visible_id: canvas.ObjectId) anyerror!void {
1432
+ const view = &self.views[view_index];
1433
+ const tooltip_index: ?usize = blk: {
1434
+ if (focus_visible_id == 0) break :blk null;
1435
+ const focused_index = view.canvasWidgetNodeIndexById(focus_visible_id) orelse break :blk null;
1436
+ break :blk view.canvasWidgetOwnedTooltipIndex(focused_index);
1437
+ };
1438
+ const tooltip_id: canvas.ObjectId = if (tooltip_index) |node_index| view.widget_layout_nodes[node_index].widget.id else 0;
1439
+
1440
+ var shown_changed = false;
1441
+ if (view.canvas_tooltip_shown_id != 0 and view.canvas_tooltip_shown_from_focus and view.canvas_tooltip_shown_id != tooltip_id) {
1442
+ view.canvas_tooltip_shown_id = 0;
1443
+ view.canvas_tooltip_shown_owner_id = 0;
1444
+ view.canvas_tooltip_shown_from_focus = false;
1445
+ shown_changed = true;
1446
+ }
1447
+ // The focus reveal is gated exactly like the hover arm: an
1448
+ // inactive app or a non-key window reveals nothing (the
1449
+ // adoption focus-binding reconcile funnels through here, so
1450
+ // a rebuild FROM the deactivation callback that binds a
1451
+ // tooltip beneath the retained keyboard ring stays silent).
1452
+ // The PROVENANCE register itself is untouched — it must
1453
+ // survive so a genuine binding change under a later ACTIVE
1454
+ // adoption still reveals; the hide half above also stays
1455
+ // ungated.
1456
+ if (tooltip_id != 0 and canvasTooltipIntentActionAllowed(self, view_index)) {
1457
+ if (view.canvas_tooltip_shown_id != tooltip_id) {
1458
+ view.canvas_tooltip_shown_id = tooltip_id;
1459
+ shown_changed = true;
1460
+ }
1461
+ // Focus re-affirms an already pointer-shown tooltip too:
1462
+ // the keyboard now holds it, so pointer leave (which
1463
+ // opens the warm window) no longer hides it — blur will.
1464
+ // Any pointer transit grace dies with the handover.
1465
+ view.canvas_tooltip_shown_owner_id = focus_visible_id;
1466
+ view.canvas_tooltip_shown_from_focus = true;
1467
+ view.canvas_tooltip_transit_deadline_ns = 0;
1468
+ // A pending pointer dwell for the SAME tooltip is
1469
+ // redundant now; a dwell on another trigger keeps
1470
+ // running (the pointer's own intent may still complete
1471
+ // and take over the shown slot — last intent wins).
1472
+ if (view.canvas_tooltip_armed_id == tooltip_id) {
1473
+ view.canvas_tooltip_armed_id = 0;
1474
+ view.canvas_tooltip_armed_owner_id = 0;
1475
+ view.canvas_tooltip_deadline_ns = 0;
1476
+ }
1477
+ }
1478
+ if (shown_changed) try commitCanvasTooltipVisibility(self, view_index);
1479
+ }
1480
+
1481
+ /// The programmatic-focus seam — window-default autofocus, the
1482
+ /// accessibility `focus` action, automation focus (all funnel
1483
+ /// through `focusAutomationCanvasWidget`). Programmatic focus
1484
+ /// writes on the pointer contract, so it never reveals and only
1485
+ /// releases a focus-owned hold (see the `.programmatic_focus`
1486
+ /// arm of the choke point).
1487
+ pub fn updateCanvasTooltipIntentForProgrammaticFocusMove(self: *Runtime, view_index: usize) anyerror!void {
1488
+ try reconcileCanvasTooltipIntent(self, view_index, .programmatic_focus);
1489
+ }
1490
+
1491
+ /// Keyboard activation (Space/Enter on the focused trigger)
1492
+ /// dismisses that trigger's armed or shown tooltip and closes
1493
+ /// the warm window — the keyboard mirror of the pointer-down
1494
+ /// reset above, per shadcn's Base UI-backed default that
1495
+ /// keyboard activation counts as a press for close-on-click.
1496
+ /// Scoped to the activated trigger's OWN tooltip, and skipped
1497
+ /// for editable text targets, where Space/Enter type instead of
1498
+ /// activating.
1499
+ pub fn updateCanvasTooltipIntentForKeyboardActivation(self: *Runtime, keyboard_event: CanvasWidgetKeyboardEvent) anyerror!void {
1500
+ if (keyboard_event.keyboard.phase != .key_down or keyboard_event.keyboard.modifiers.hasNavigationModifier()) return;
1501
+ if (!canvas.isWidgetActivationKey(keyboard_event.keyboard.key)) return;
1502
+ const target = keyboard_event.target orelse return;
1503
+ if (canvas_widget_runtime.canvasWidgetEditableTextKind(target.kind)) return;
1504
+ const index = runtimeFindViewIndex(self, keyboard_event.window_id, keyboard_event.view_label) orelse return;
1505
+ if (self.views[index].kind != .gpu_surface) return;
1506
+ const view = &self.views[index];
1507
+ const tooltip_id: canvas.ObjectId = blk: {
1508
+ const target_index = view.canvasWidgetNodeIndexById(target.id) orelse break :blk 0;
1509
+ const tooltip_index = view.canvasWidgetOwnedTooltipIndex(target_index) orelse break :blk 0;
1510
+ break :blk view.widget_layout_nodes[tooltip_index].widget.id;
1511
+ };
1512
+ if (tooltip_id == 0) return;
1513
+ var matched = false;
1514
+ if (view.canvas_tooltip_armed_id == tooltip_id) {
1515
+ view.canvas_tooltip_armed_id = 0;
1516
+ view.canvas_tooltip_armed_owner_id = 0;
1517
+ view.canvas_tooltip_deadline_ns = 0;
1518
+ matched = true;
1519
+ }
1520
+ const dismissed_shown = view.canvas_tooltip_shown_id == tooltip_id;
1521
+ if (dismissed_shown) {
1522
+ view.canvas_tooltip_shown_id = 0;
1523
+ view.canvas_tooltip_shown_owner_id = 0;
1524
+ view.canvas_tooltip_shown_from_focus = false;
1525
+ view.canvas_tooltip_transit_deadline_ns = 0;
1526
+ matched = true;
1527
+ }
1528
+ if (matched) view.canvas_tooltip_warm_until_ns = 0;
1529
+ // Explicit dismissal CONSUMES the standing keyboard reveal
1530
+ // intent, not just the visible slot. The provenance
1531
+ // register (`canvas_widget_focus_visible_keyboard`) exists
1532
+ // for exactly one contract — "only rings placed by the
1533
+ // keyboard carry the standing reveal intent a later layout
1534
+ // adoption may honor" — and its only readers are the two
1535
+ // tooltip reveal gates (the adoption binding snapshot's
1536
+ // capture and `reconcileCanvasTooltipIntentForAdoptedFocusBinding`).
1537
+ // The focus RING renders from `canvas_widget_focus_visible_id`
1538
+ // alone, so clearing the register keeps the trigger visibly
1539
+ // focus-visible while stopping the activation's own model
1540
+ // rebuild — which routinely replaces or rekeys the tooltip —
1541
+ // from resurrecting it one frame after this dismissal
1542
+ // ("stays down while focus rests on the trigger"). A fresh
1543
+ // keyboard ARRIVAL (Tab away and back) re-grants the
1544
+ // contract at the one provenance write; the pointer path is
1545
+ // untouched — a post-dismissal hover re-earns its dwell.
1546
+ if (matched and view.canvas_widget_focus_visible_id == target.id) {
1547
+ view.canvas_widget_focus_visible_keyboard = false;
1548
+ }
1549
+ if (dismissed_shown) try commitCanvasTooltipVisibility(self, index);
1550
+ }
1551
+
1552
+ /// Re-stamp anchored-tooltip visibility after an intent
1553
+ /// transition and repaint the affected tooltip frames — the
1554
+ /// dismissal echo's invalidation shape.
1555
+ fn commitCanvasTooltipVisibility(self: *Runtime, view_index: usize) anyerror!void {
1556
+ const view = &self.views[view_index];
1557
+ var dirty: ?geometry.RectF = null;
1558
+ for (view.widget_layout_nodes[0..view.widget_layout_node_count], 0..) |node, node_index| {
1559
+ if (node.widget.kind != .tooltip) continue;
1560
+ if (!canvas.widgetIsAnchored(node.widget)) continue;
1561
+ const hidden = node.widget.id == 0 or node.widget.id != view.canvas_tooltip_shown_id;
1562
+ if (node.widget.semantics.hidden == hidden) continue;
1563
+ const bounds = view.canvasWidgetDirtyBounds(node_index, node.frame) orelse node.frame;
1564
+ dirty = if (dirty) |current| current.unionWith(bounds) else bounds;
1565
+ }
1566
+ view.applyCanvasTooltipVisibility();
1567
+ try view.refreshCanvasWidgetSemantics();
1568
+ view.widget_revision += 1;
1569
+ try invalidateForCanvasWidgetDirty(self, view_index, dirty orelse view.frame);
1570
+ }
1571
+
1572
+ /// Bound on the anchor-gap transit grace, in milliseconds. Each
1573
+ /// in-corridor pointer move re-arms it, so it only ever fires
1574
+ /// for a pointer PARKED between trigger and tooltip — 400ms of
1575
+ /// stillness in a gap a few points wide is abandonment, not
1576
+ /// transit. Deliberately a constant, not a theme token: the
1577
+ /// corridor's timing is interaction mechanics (like
1578
+ /// double-click windows), and Base UI exposes no knob for its
1579
+ /// safe polygon either.
1580
+ const tooltip_transit_grace_ms: u64 = 400;
1581
+
1582
+ fn canvasTooltipShowDelayNs(widget: canvas.Widget, tokens: canvas.DesignTokens) u64 {
1583
+ const delay_ms: u64 = if (widget.tooltip_delay_ms >= 0)
1584
+ @intCast(widget.tooltip_delay_ms)
1585
+ else
1586
+ tokens.metrics.tooltip_show_delay_ms;
1587
+ return delay_ms * std.time.ns_per_ms;
1588
+ }
1589
+
1590
+ fn canvasTooltipWarmWindowNs(tokens: canvas.DesignTokens) u64 {
1591
+ return @as(u64, tokens.metrics.tooltip_warm_window_ms) * std.time.ns_per_ms;
1592
+ }
1593
+
434
1594
  fn canvasChartHoverIndexForId(self: *Runtime, view_index: usize, hovered_id: canvas.ObjectId, point: geometry.PointF) ?usize {
435
1595
  if (hovered_id == 0) return null;
436
1596
  const layout = self.views[view_index].widgetLayoutTree();
@@ -531,6 +1691,85 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
531
1691
  return b == null;
532
1692
  }
533
1693
 
1694
+ /// The scroll seam: content moved under a (possibly stationary)
1695
+ /// pointer — the wheel passes its live position, and the paths
1696
+ /// without one (kinetic steps, native drivers, keyboard
1697
+ /// scrolling) pass null to borrow the view's last JOURNALED
1698
+ /// pointer position. The choke point re-hit-tests that
1699
+ /// position, re-checks the content hold against the tooltip's
1700
+ /// post-scroll frame, and steps the transitions point-blind
1701
+ /// (immediate-hide semantics, no transit corridor: the pointer
1702
+ /// did not move, the content did — Base UI closes open tooltips
1703
+ /// on scroll for the same reason; we do strictly better exactly
1704
+ /// where the re-hit-test is sound, and CLOSE pointer intent
1705
+ /// where it is not).
1706
+ pub fn reconcileCanvasWidgetRenderStateAfterScrollWithTooltipIntent(self: *Runtime, view_index: usize, point: ?geometry.PointF) anyerror!void {
1707
+ try reconcileCanvasTooltipIntent(self, view_index, if (point) |value| .{ .wheel_scroll = value } else .point_blind_scroll);
1708
+ }
1709
+
1710
+ /// Close the POINTER's whole tooltip conversation — armed
1711
+ /// reveal, pointer-shown tooltip, warm window, transit grace —
1712
+ /// while a focus-SHOWN tooltip stays: the keyboard holds it,
1713
+ /// and blur, focus moves, and activation own its lifecycle.
1714
+ /// Two callers share this shape, both places where the pointer
1715
+ /// cannot be placed on the tree:
1716
+ /// - the point-blind scroll reconcile's staleness arm (no
1717
+ /// trustworthy position to re-hit-test — Base UI's
1718
+ /// close-on-scroll, applied exactly where the re-hit-test
1719
+ /// upgrade is unsound);
1720
+ /// - pointer `.cancel` (the pointer LEFT the view).
1721
+ /// No warmth survives either one: an instant re-show is a
1722
+ /// courtesy earned by a pointer we cannot place.
1723
+ fn closeCanvasTooltipPointerIntent(self: *Runtime, view_index: usize) anyerror!void {
1724
+ const view = &self.views[view_index];
1725
+ view.canvas_tooltip_armed_id = 0;
1726
+ view.canvas_tooltip_armed_owner_id = 0;
1727
+ view.canvas_tooltip_deadline_ns = 0;
1728
+ view.canvas_tooltip_warm_until_ns = 0;
1729
+ view.canvas_tooltip_transit_deadline_ns = 0;
1730
+ if (view.canvas_tooltip_shown_id == 0 or view.canvas_tooltip_shown_from_focus) return;
1731
+ view.canvas_tooltip_shown_id = 0;
1732
+ view.canvas_tooltip_shown_owner_id = 0;
1733
+ try commitCanvasTooltipVisibility(self, view_index);
1734
+ }
1735
+
1736
+ /// The adoption seam: a rebuild adopted a new tree under a
1737
+ /// (possibly stationary) pointer — called by
1738
+ /// `setCanvasWidgetLayout` after adoption (copy, tween pose
1739
+ /// restores) settles the frames the user actually sees. The
1740
+ /// adoption prune validates only tooltip/owner IDENTITY and
1741
+ /// hover survives by ID, so without this step a rebuild that
1742
+ /// MOVED the same-ID trigger away from the stationary pointer
1743
+ /// left armed intent able to fire — and a shown tooltip
1744
+ /// visible — until the next real pointer event. The choke point
1745
+ /// re-hit-tests the stored position, re-checks the content hold
1746
+ /// against the tooltip's ADOPTED frame, and steps transitions
1747
+ /// per the normal rules (or closes pointer intent when no
1748
+ /// trustworthy position exists); this thin caller only wraps it
1749
+ /// in the repaint echo a re-hit-tested wash or cursor owes.
1750
+ /// `bindings` is the pre-adoption snapshot from
1751
+ /// `captureCanvasTooltipAdoptionBindings` — the caller captures
1752
+ /// it against the OUTGOING tree, which no longer exists here.
1753
+ pub fn reconcileCanvasWidgetInteractionAfterLayoutAdoption(self: *Runtime, view_index: usize, bindings: CanvasTooltipAdoptionBindingSnapshot) anyerror!void {
1754
+ const view = &self.views[view_index];
1755
+ const previous_state = view.canvasWidgetRenderState();
1756
+ const previous_cursor = view.canvas_widget_cursor;
1757
+ try reconcileCanvasTooltipIntent(self, view_index, .{ .layout_adoption = bindings });
1758
+ try invalidateForCanvasWidgetAdoptionReconcile(self, view_index, previous_state, previous_cursor);
1759
+ }
1760
+
1761
+ /// The adoption reconcile's repaint echo: a wash or cursor the
1762
+ /// re-hit-test moved must publish even when the rebuild's own
1763
+ /// diff never touched those frames. No-ops entirely on the
1764
+ /// unchanged-rebuild path, so a clean rebuild stays clean.
1765
+ fn invalidateForCanvasWidgetAdoptionReconcile(self: *Runtime, view_index: usize, previous_state: canvas.WidgetRenderState, previous_cursor: platform.Cursor) anyerror!void {
1766
+ const view = &self.views[view_index];
1767
+ if (previous_cursor != view.canvas_widget_cursor) try syncCanvasWidgetCursorForView(self, view_index);
1768
+ if (!canvasWidgetRenderStatesEqual(previous_state, view.canvasWidgetRenderState())) {
1769
+ try invalidateForCanvasWidgetRenderStateChange(self, view_index, previous_state, view.canvasWidgetRenderState());
1770
+ }
1771
+ }
1772
+
534
1773
  pub fn updateCanvasWidgetScrollFromPointer(self: *Runtime, pointer_event: CanvasWidgetPointerEvent) anyerror!void {
535
1774
  if (pointer_event.pointer.phase != .wheel) return;
536
1775
  const index = runtimeFindViewIndex(self, pointer_event.window_id, pointer_event.view_label) orelse return;
@@ -538,7 +1777,7 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
538
1777
 
539
1778
  const dirty = try self.views[index].applyCanvasWidgetScrollRoute(pointer_event.route, pointer_event.pointer.delta.dy, .wheel) orelse return;
540
1779
  const previous_cursor = self.views[index].canvas_widget_cursor;
541
- self.views[index].reconcileCanvasWidgetRenderStateAfterScroll(pointer_event.pointer.point);
1780
+ try reconcileCanvasWidgetRenderStateAfterScrollWithTooltipIntent(self, index, pointer_event.pointer.point);
542
1781
  if (previous_cursor != self.views[index].canvas_widget_cursor) try syncCanvasWidgetCursorForView(self, index);
543
1782
  if (canvasDirtyRegionForView(self.views[index].frame, dirty)) |dirty_region| {
544
1783
  self.invalidateFor(.state, dirty_region);
@@ -619,11 +1858,27 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
619
1858
  return widget.text[range.start..range.end];
620
1859
  }
621
1860
 
622
- pub fn updateCanvasWidgetTextFromKeyboard(self: *Runtime, keyboard_event: CanvasWidgetKeyboardEvent) anyerror!void {
1861
+ /// The ONE derivation seam for keyboard-driven editor mutations:
1862
+ /// `canvasWidgetKeyboardTextEdit` maps the routed key to the edit
1863
+ /// the retained editor applies, and that same edit is STAMPED
1864
+ /// onto the event the app dispatch consumes — so the model's
1865
+ /// `on_input` hears exactly what the editor did (the cut/paste
1866
+ /// and clear-button stamping precedent, made the rule). Before
1867
+ /// the stamp, edits that only THIS derivation produced — Escape's
1868
+ /// search-field clear, Escape's composition cancel, the
1869
+ /// single-line ArrowUp/Down caret jumps — mutated the editor
1870
+ /// while the app-side dispatch re-derived the key on its own and
1871
+ /// heard nothing: the field visibly cleared while `model.query`
1872
+ /// kept the stale term, and the next keystroke dispatched
1873
+ /// against it. Stamping happens even when applying changes
1874
+ /// nothing (an Escape in an already-empty field), so a model
1875
+ /// whose mirror diverged still hears the clear and resyncs.
1876
+ pub fn updateCanvasWidgetTextFromKeyboard(self: *Runtime, keyboard_event: *CanvasWidgetKeyboardEvent) anyerror!void {
623
1877
  const index = runtimeFindViewIndex(self, keyboard_event.window_id, keyboard_event.view_label) orelse return;
624
1878
  if (self.views[index].kind != .gpu_surface) return;
625
1879
  const target = keyboard_event.target orelse return;
626
1880
  const edit = self.views[index].canvasWidgetKeyboardTextEdit(target, keyboard_event.keyboard) orelse return;
1881
+ keyboard_event.keyboard.edit = edit;
627
1882
 
628
1883
  const dirty = try self.views[index].applyCanvasWidgetTextEdit(target.id, edit) orelse return;
629
1884
  if (canvasDirtyRegionForView(self.views[index].frame, dirty)) |dirty_region| {
@@ -739,7 +1994,7 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
739
1994
  const dirty = try self.views[index].applyCanvasWidgetControlKeyboard(target.id, keyboard_event.keyboard) orelse return;
740
1995
  if (toggle_animation) |animation| try runtime_canvas_widget_display.RuntimeCanvasWidgetDisplay(Runtime).scheduleCanvasWidgetToggleAnimation(self, index, animation);
741
1996
  const previous_cursor = self.views[index].canvas_widget_cursor;
742
- if (target.kind == .scroll_view) self.views[index].reconcileCanvasWidgetRenderStateAfterScroll(null);
1997
+ if (target.kind == .scroll_view) try reconcileCanvasWidgetRenderStateAfterScrollWithTooltipIntent(self, index, null);
743
1998
  if (previous_cursor != self.views[index].canvas_widget_cursor) try syncCanvasWidgetCursorForView(self, index);
744
1999
  if (canvasDirtyRegionForView(self.views[index].frame, dirty)) |dirty_region| {
745
2000
  self.invalidateFor(.state, dirty_region);
@@ -967,6 +2222,19 @@ pub fn RuntimeCanvasWidgetEvents(comptime Runtime: type) type {
967
2222
  const previous_state = self.views[view_index].canvasWidgetRenderState();
968
2223
  self.views[view_index].canvas_widget_focused_id = target_id;
969
2224
  self.views[view_index].canvas_widget_focus_visible_id = target_id;
2225
+ // The one provenance write that grants the keyboard
2226
+ // contract: only rings placed HERE carry the standing
2227
+ // reveal intent a later layout adoption may honor.
2228
+ // Explicit dismissal (keyboard activation, Escape) SPENDS
2229
+ // that intent where it dismisses — the register clears
2230
+ // there and only a fresh arrival through this write
2231
+ // re-grants it.
2232
+ self.views[view_index].canvas_widget_focus_visible_keyboard = target_id != 0;
2233
+ // Keyboard focus-visible is the tooltip's second reveal
2234
+ // path: landing on a tooltip-owning trigger shows it
2235
+ // immediately, moving on hides the previous one (the
2236
+ // `.focus_visible` cause of the intent choke point).
2237
+ try reconcileCanvasTooltipIntent(self, view_index, .{ .focus_visible = target_id });
970
2238
  // Keyboard focus landing on an editable establishes a caret:
971
2239
  // without a selection the emitters draw no caret line, so a
972
2240
  // tabbed-into field would render its ring but no insertion