nsh-core 0.3.0

Platform-independent core: shadow tree, styles, layout and mount instructions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
//! Overlays ([`crate::overlay`]): an `<overlay>` node stays where the app
//! put it (its parent's child for the reactive graph, events, lifetime,
//! inherited text styles), while its native view is in the root view
//! around it (the surface, or the nearest modal), above that root view's
//! own children and the overlays shown before it.
//!
//! Each commit [`Core::sync_overlays`] works out which overlays are shown
//! (every view around them is, and the overlays around them are) and in
//! which root view, and moves their native views there: one opened later
//! goes on top. An overlay is a layout root of its own, laid out in a box
//! the size of its root view (`position: fixed`), and placed against its
//! anchor when it has one ([`crate::overlay::place`]); it follows the
//! anchor as it moves ([`Core::place_overlays`], after the lists and
//! scroll offsets of the commit). The core also routes what dismissing
//! takes: presses outside (`onPressOutside`: the overlays watching them,
//! their anchors and root views listen to touches while they are shown)
//! and Escape / the back button (`onCancel`, to the top overlay listening:
//! a binding of the root's table and the root's back listener while one
//! is shown). Closing is the app's (`nsh-base`'s components).
//!
//! A hint overlay ([`OverlayConfig::hint`]) is shown by the core: its
//! anchor (its parent without one) listens to the pointer coming and going
//! while the overlay is in the view on a desktop; the pointer resting on it
//! for the hint's delay shows the hint (a task of the components' executor
//! wakes the engine then), the pointer leaving or pressing it hides it, as
//! GPUI's tooltips do. The overlay hears what it asks for (`Show`,
//! `Dismiss`), so a component owning it (`nsh-base`'s `Tooltip`) can
//! decide instead ([`OverlayConfig::open`]). An element's `tooltip` is a
//! hint overlay the core makes inside the element ([`Core::set_tooltip`]).

use rustc_hash::FxHashMap;
use taffy::NodeId as LayoutId;

use super::{Core, Key, Kind, NO_KEY, definite};
use crate::{
    EventKind, EventPayload, NodeId, PayloadValue, Prop, ROOT_ID, Tag, TextEngine, TouchEvent,
    TouchPhase,
    element::task::{Action, Task, act_after},
    geometry::{Bounds, Point, Points, Size, px, rect},
    keymap::{KeyBinding, Keystroke, When},
    mount::MountItem,
    ops::{Op, OverlayProp},
    overlay::{
        HINT_DELAY, OverlayAnchor, OverlayConfig, Placed, Placement, TooltipContent, inset, place,
    },
    props::PointerEvents,
};

/// What the overlays have a node listen to (`Node::overlay_watched`).
pub(super) mod watch {
    /// Touches, for an overlay's `onPressOutside`.
    pub(in crate::core) const PRESS: u8 = 1;
    /// The pointer coming and going and touches, for a hint.
    pub(in crate::core) const HINT: u8 = 2;
}

/// The gap between an element and its `tooltip`, and the distance the
/// tooltip keeps from the safe area's edges.
const TOOLTIP_GAP: f32 = 4.0;

/// A hint's wait for the pointer to rest (dropping it cancels it).
pub(super) struct HintTimer(#[allow(dead_code)] Task<()>);

impl std::fmt::Debug for HintTimer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("HintTimer")
    }
}

/// The nodes the core made for an element's `tooltip`.
#[derive(Debug, Clone, Copy)]
pub(super) struct CoreTooltip {
    /// The hint overlay, a child of the element.
    pub(super) overlay: NodeId,
    /// The element shown as the tooltip, inside the overlay (JS's).
    content: Option<NodeId>,
}

/// The binding id of Escape while an overlay listens to `onCancel`: above
/// any JS gives and any view's ([`crate::element::keys::FIRST_ID`] counts up).
pub(super) const ESCAPE_BINDING: u32 = u32::MAX;

/// An overlay's own state.
#[derive(Debug)]
pub(super) struct OverlayState {
    /// How it is placed.
    pub(super) config: OverlayConfig,
    /// A layout node the size of the root view, holding the overlay's as
    /// an absolutely placed child: what its insets and percentages are
    /// against.
    pub(super) wrapper: LayoutId,
    /// The root view its native view is in, while it is shown.
    pub(super) host: Option<NodeId>,
    /// Where it was placed last, as `onPosition` reported it.
    pub(super) placed: Option<(Placed, Bounds<Points>)>,
    /// A hint: the pointer rested on its anchor, and has not left or
    /// pressed it since.
    pub(super) hinted: bool,
    /// A hint: the pointer is on its anchor, resting until it shows.
    pub(super) waiting: Option<HintTimer>,
}

/// What the overlays add to the native side: the nodes listening to
/// touches for `onPressOutside`, and whether Escape and back are taken.
#[derive(Debug, Default)]
pub(super) struct Overlays {
    /// The shown overlays and the other nodes listening to touches for
    /// them (their anchors and root views), checked by id (keys are
    /// reused).
    pub(super) watched: Vec<(Key, NodeId)>,
    /// A shown overlay listens to `onCancel`: Escape is bound and the back
    /// button listened to.
    pub(super) cancel: bool,
    /// The anchors of the hints in the view listening to the pointer
    /// (desktops), checked by id.
    pub(super) hinting: Vec<(Key, NodeId)>,
    /// The elements with a `tooltip`, by id: the nodes made for it.
    pub(super) tooltips: FxHashMap<NodeId, CoreTooltip>,
}

impl<M: TextEngine> Core<M> {
    /// A new overlay node `key` with layout node `layout`: its wrapper.
    pub(super) fn overlay_made(&mut self, key: Key, layout: LayoutId) {
        let wrapper = self
            .layout
            .new_with_children(taffy::Style::default(), &[layout]);
        if let Some(root) = self.nodes[key as usize].root.as_mut() {
            root.overlay = Some(Box::new(OverlayState {
                config: OverlayConfig::default(),
                wrapper,
                host: None,
                placed: None,
                hinted: false,
                waiting: None,
            }));
        }
        self.overlays.push(key);
    }

    /// The views whose `position: fixed` changed become overlays (laid out
    /// in their root view's box, shown above its children, as CSS places
    /// a fixed box against the viewport), or views in their parents again.
    pub(super) fn sync_fixed(&mut self) {
        if self.fixed_changed.is_empty() {
            return;
        }
        for (key, id) in std::mem::take(&mut self.fixed_changed) {
            let Some(node) = self.nodes.get(key as usize) else {
                continue;
            };
            if node.id != id {
                continue;
            }
            let parent = node.parent;
            let at = (parent != NO_KEY).then(|| {
                let siblings = &self.nodes[parent as usize].children;
                let i = siblings.iter().position(|&c| c == key).expect("child");
                let next = siblings.get(i + 1).map(|&c| self.nodes[c as usize].id);
                (self.nodes[parent as usize].id, next)
            });
            if self.fix_or_unfix(key)
                && let Some((parent, before)) = at
            {
                let _ = self.insert(parent, id, before);
            }
        }
    }

    /// Makes view `key` an overlay when its style says `position: fixed`,
    /// or a view again when it no longer does; whether it changed.
    pub(super) fn fix_or_unfix(&mut self, key: Key) -> bool {
        let node = &self.nodes[key as usize];
        let fixed = node.wants_fixed
            && !node.kind.is_root()
            && (node.layout.is_some() || node.kind == Kind::Text);
        if fixed == node.fixed_view {
            return false;
        }
        if fixed {
            self.fix_view(key);
        } else {
            self.unfix_view(key);
        }
        true
    }

    /// View `key` becomes an overlay of no anchor: out of its parent's
    /// layout and native view, shown by `sync_overlays` in its root view.
    fn fix_view(&mut self, key: Key) {
        if self.nodes[key as usize].parent != NO_KEY {
            self.unlink(key, true);
        }
        self.set_inline(key, false);
        let node = &mut self.nodes[key as usize];
        node.fixed_view = true;
        node.root = Some(Box::default());
        let layout = node.layout.expect("a view has a layout");
        let style = self.layout.style(layout).clone();
        self.nodes[key as usize].base_layout = Some(Box::new(style));
        self.roots.push(key);
        self.overlay_made(key, layout);
        self.sync_layout_style(key);
    }

    /// Overlay `key`, a view `position: fixed` made one, is a view again:
    /// out of its root view, laid out in its parent.
    fn unfix_view(&mut self, key: Key) {
        let (id, created) = (
            self.nodes[key as usize].id,
            self.nodes[key as usize].created,
        );
        let state = self.overlay(key).map(|s| (s.host, s.wrapper));
        if let Some((Some(_), _)) = state
            && created
        {
            self.mount.push(MountItem::Remove { id });
        }
        if self.nodes[key as usize].parent != NO_KEY {
            self.unlink(key, false);
        }
        let layout = self.nodes[key as usize]
            .layout
            .expect("a view has a layout");
        if let Some((_, wrapper)) = state {
            self.layout.remove_child(wrapper, layout);
            self.overlay_released(key, wrapper);
        }
        self.roots.retain(|&k| k != key);
        let node = &mut self.nodes[key as usize];
        node.fixed_view = false;
        node.root = None;
        if let Some(base) = node.base_layout.as_deref() {
            self.layout.set_style(layout, base.clone());
        }
        if !node.kind.derives_layout() {
            node.base_layout = None;
        }
        self.needs_layout = true;
    }

    /// Overlay `key` is released: forgets it and drops its wrapper (its own
    /// layout node is gone already).
    pub(super) fn overlay_released(&mut self, key: Key, wrapper: LayoutId) {
        self.overlays.retain(|&k| k != key);
        self.layout.remove(wrapper);
    }

    /// Node `id` is released: the nodes made for its `tooltip` go with it.
    pub(super) fn tooltip_released(&mut self, id: NodeId) {
        if let Some(made) = self.overlay_state.tooltips.remove(&id) {
            self.release_tooltip(made);
        }
    }

    /// Whether `key` is the overlay of an element's `tooltip`: not one of
    /// its children for its classes (`first:`, `last:`, ...).
    pub(super) fn is_tooltip_overlay(&self, key: Key) -> bool {
        let node = &self.nodes[key as usize];
        node.is_overlay()
            && node.parent != NO_KEY
            && self
                .overlay_state
                .tooltips
                .get(&self.nodes[node.parent as usize].id)
                .is_some_and(|t| t.overlay == node.id)
    }

    pub(super) fn overlay(&self, key: Key) -> Option<&OverlayState> {
        self.nodes[key as usize]
            .root
            .as_ref()
            .and_then(|r| r.overlay.as_deref())
    }

    fn overlay_mut(&mut self, key: Key) -> Option<&mut OverlayState> {
        self.nodes[key as usize]
            .root
            .as_mut()
            .and_then(|r| r.overlay.as_deref_mut())
    }

    /// Changes how overlay `key` is placed.
    pub(super) fn set_overlay_config(&mut self, key: Key, f: impl FnOnce(&mut OverlayConfig)) {
        if let Some(state) = self.overlay_mut(key) {
            let before = state.config;
            f(&mut state.config);
            if state.config != before {
                state.placed = None;
                // Placed again with its size, or laid out again (no anchor:
                // its insets place it).
                if let Some(root) = self.nodes[key as usize].root.as_mut() {
                    root.laid_out = None;
                }
                self.needs_layout = true;
            }
        }
    }

    /// The first overlay shown in root view `parent`: the native sibling
    /// its own children go below.
    pub(super) fn first_overlay_in(&self, parent: Key) -> Option<NodeId> {
        let id = self.nodes[parent as usize].id;
        self.overlays
            .iter()
            .find_map(|&k| (self.overlay(k)?.host == Some(id)).then(|| self.nodes[k as usize].id))
    }

    /// The root view overlay `key` is shown in, or `None` while it is not
    /// shown: a view around it is not (out of the tree, its native view
    /// not made, `display: none`), or the overlay around it is not.
    fn overlay_host(&self, key: Key) -> Option<Key> {
        let mut modal = None;
        let mut k = self.nodes[key as usize].parent;
        while k != NO_KEY {
            if k == self.root {
                return Some(modal.unwrap_or(self.root));
            }
            let n = &self.nodes[k as usize];
            if let Some(layout) = n.layout {
                if !n.created {
                    return None;
                }
                let hidden = self.layout.style(layout).display == taffy::Display::None;
                if hidden && !n.is_overlay() {
                    return None;
                }
            }
            match n.kind {
                Kind::Modal if modal.is_none() => modal = Some(k),
                _ if n.is_overlay() => {
                    // Shown in its root view, which is this one's unless a
                    // modal is between them.
                    let host = self.overlay(k)?.host?;
                    return Some(modal.unwrap_or(self.key(host).ok()?));
                }
                _ => {}
            }
            k = n.parent;
        }
        None
    }

    /// The overlays around `key`.
    fn overlay_depth(&self, key: Key) -> u32 {
        let mut depth = 0;
        let mut k = self.nodes[key as usize].parent;
        while k != NO_KEY {
            if self.nodes[k as usize].is_overlay() {
                depth += 1;
            }
            k = self.nodes[k as usize].parent;
        }
        depth
    }

    /// Shows the overlays whose views around them are shown, in their root
    /// views, and hides the others (and the hints the pointer has not
    /// asked for); then what they listen to (once per commit, before the
    /// dirty views are flushed).
    pub(super) fn sync_overlays(&mut self) {
        if self.overlays.is_empty()
            && self.overlay_state.watched.is_empty()
            && self.overlay_state.hinting.is_empty()
        {
            return;
        }
        // The overlays around one first: theirs say where it goes.
        if !self.overlay_state.tooltips.is_empty() {
            self.adopt_hint_tooltips();
        }
        let mut order: Vec<(u32, Key)> = self
            .overlays
            .iter()
            .map(|&k| (self.overlay_depth(k), k))
            .collect();
        order.sort_by_key(|&(depth, _)| depth);
        let mut opened = Vec::new();
        // The hints in the view, whose anchors listen to the pointer.
        let mut hints = Vec::new();
        for (_, key) in order {
            let mut target = self.overlay_host(key);
            let Some(state) = self.overlay(key) else {
                continue;
            };
            if state.config.hint.is_some() {
                if target.is_some() {
                    hints.push(key);
                } else {
                    // Out of the view: the pointer starts again.
                    self.unhint(key);
                }
                let state = self.overlay(key).expect("overlay");
                if !state.config.open.unwrap_or(state.hinted) {
                    target = None;
                }
            }
            let target_id = target.map(|h| self.nodes[h as usize].id);
            let current = self.overlay(key).expect("overlay").host;
            if current == target_id {
                continue;
            }
            let id = self.nodes[key as usize].id;
            if let Some(old) = current
                && self.key(old).is_ok_and(|h| self.nodes[h as usize].created)
                && self.nodes[key as usize].created
            {
                self.mount.push(MountItem::Remove { id });
            }
            let state = self.overlay_mut(key).expect("overlay");
            state.host = target_id;
            state.placed = None;
            let Some(host) = target_id else {
                continue;
            };
            self.ensure_created(key);
            self.mount.push(MountItem::Insert {
                parent: host,
                child: id,
                before: None,
            });
            if let Some(root) = self.nodes[key as usize].root.as_mut() {
                root.laid_out = None;
            }
            self.needs_layout = true;
            opened.push(key);
        }
        // Opened last, on top: in the order they were shown.
        if !opened.is_empty() {
            self.overlays.retain(|k| !opened.contains(k));
            self.overlays.extend(opened);
        }
        self.sync_overlay_listeners(&hints);
    }

    /// The touches and keys the shown overlays need, and the pointer the
    /// anchors of `hints` (in the view) follow.
    fn sync_overlay_listeners(&mut self, hints: &[Key]) {
        let mut watched: Vec<(Key, NodeId)> = Vec::new();
        let mut cancel = false;
        let watching = self.overlays.iter().any(|&k| {
            self.overlay(k).is_some_and(|s| s.host.is_some())
                && self.nodes[k as usize]
                    .events
                    .contains(EventKind::PressOutside)
        });
        for &key in &self.overlays {
            let Some(state) = self.overlay(key) else {
                continue;
            };
            let Some(host) = state.host else { continue };
            let node = &self.nodes[key as usize];
            cancel |= node.events.contains(EventKind::Cancel);
            if !watching {
                continue;
            }
            // Every shown overlay, so a press in one inside a watching one
            // tells where it is.
            watched.push((key, node.id));
            if node.events.contains(EventKind::PressOutside) {
                for id in [Some(host), anchor_node(state)].into_iter().flatten() {
                    if let Ok(k) = self.key(id) {
                        watched.push((k, self.nodes[k as usize].id));
                    }
                }
            }
        }
        watched.sort_unstable();
        watched.dedup();
        let old = std::mem::take(&mut self.overlay_state.watched);
        self.rewatch(&old, &watched, watch::PRESS);
        self.overlay_state.watched = watched;
        // Hints only follow a pointer (the desktops).
        let mut hinting: Vec<(Key, NodeId)> = Vec::new();
        if self
            .components
            .environment
            .idiom
            .desktop_on(self.components.environment.os)
        {
            for &key in hints {
                if let Some(k) = self.hint_anchor(key) {
                    hinting.push((k, self.nodes[k as usize].id));
                }
            }
            hinting.sort_unstable();
            hinting.dedup();
        }
        let old = std::mem::take(&mut self.overlay_state.hinting);
        self.rewatch(&old, &hinting, watch::HINT);
        self.overlay_state.hinting = hinting;
        if cancel != self.overlay_state.cancel {
            self.overlay_state.cancel = cancel;
            // The root's table and back listener are made again.
            self.components.executor.keys.borrow_mut().changed = true;
            self.components.executor.back.borrow_mut().changed = true;
        }
    }

    /// The nodes of `old` not in `new` stop listening for `bit`, those of
    /// `new` not in `old` start.
    fn rewatch(&mut self, old: &[(Key, NodeId)], new: &[(Key, NodeId)], bit: u8) {
        for &(k, id) in old {
            if !new.contains(&(k, id)) && self.nodes.get(k as usize).is_some_and(|n| n.id == id) {
                self.nodes[k as usize].overlay_watched &= !bit;
                self.mark(k, super::dirty::EVENTS);
            }
        }
        for &(k, id) in new {
            if !old.contains(&(k, id)) {
                self.nodes[k as usize].overlay_watched |= bit;
                self.mark(k, super::dirty::EVENTS);
            }
        }
    }

    /// What hint overlay `key` is placed against and whose pointer it
    /// follows: its anchor, else the nearest view around it with a frame
    /// (its parent, unless that is a span of text).
    fn hint_anchor(&self, key: Key) -> Option<Key> {
        match self.overlay(key)?.config.anchor {
            OverlayAnchor::Node(id) | OverlayAnchor::Content(id) => self.key(id).ok(),
            OverlayAnchor::Point(_) | OverlayAnchor::Bounds(_) => None,
            OverlayAnchor::None => {
                let mut k = self.nodes[key as usize].parent;
                while k != NO_KEY {
                    let n = &self.nodes[k as usize];
                    if n.layout.is_some() {
                        return Some(k);
                    }
                    k = n.parent;
                }
                None
            }
        }
    }

    /// What overlay `key` is placed against: a hint without an anchor
    /// against the view it follows ([`Self::hint_anchor`]).
    fn resolved_anchor(&self, key: Key) -> OverlayAnchor {
        let Some(state) = self.overlay(key) else {
            return OverlayAnchor::None;
        };
        match state.config.anchor {
            OverlayAnchor::None if state.config.hint.is_some() => {
                self.hint_anchor(key).map_or(OverlayAnchor::None, |k| {
                    OverlayAnchor::Node(self.nodes[k as usize].id)
                })
            }
            anchor => anchor,
        }
    }

    /// The pointer came over node `id` or left it (`kind`): the hints it
    /// anchors wait for it to rest, or hide.
    pub(super) fn overlay_hover(&mut self, id: NodeId, kind: EventKind) {
        let Ok(anchor) = self.key(id) else {
            return;
        };
        if self.nodes[anchor as usize].overlay_watched & watch::HINT == 0 {
            return;
        }
        let hints: Vec<Key> = self
            .overlays
            .iter()
            .copied()
            .filter(|&k| {
                self.overlay(k).is_some_and(|s| s.config.hint.is_some())
                    && self.hint_anchor(k) == Some(anchor)
            })
            .collect();
        for key in hints {
            match kind {
                EventKind::HoverIn => self.hint_wait(key),
                _ => self.unhint(key),
            }
        }
    }

    /// The pointer is on hint `key`'s anchor: it shows once the pointer
    /// rested there for its delay ([`Self::hint_due`]).
    fn hint_wait(&mut self, key: Key) {
        let executor = self.components.executor.clone();
        let id = self.nodes[key as usize].id;
        let Some(state) = self.overlay_mut(key) else {
            return;
        };
        if state.hinted || state.waiting.is_some() {
            return;
        }
        let delay = state.config.hint.unwrap_or(HINT_DELAY);
        state.waiting = Some(HintTimer(act_after(&executor, id, delay, Action::Hint(id))));
    }

    /// Hint overlay `id`'s delay is over with the pointer on its anchor: it
    /// shows (in the commit).
    pub(super) fn hint_due(&mut self, id: NodeId) {
        let Ok(key) = self.key(id) else {
            return;
        };
        let Some(state) = self.overlay_mut(key) else {
            return;
        };
        if state.waiting.take().is_none() || state.config.hint.is_none() {
            return;
        }
        state.hinted = true;
        self.hint_changed(key, true);
    }

    /// Hint `key` stops waiting, and hides when the pointer had it shown.
    fn unhint(&mut self, key: Key) {
        let Some(state) = self.overlay_mut(key) else {
            return;
        };
        state.waiting = None;
        if std::mem::take(&mut state.hinted) {
            self.hint_changed(key, false);
        }
    }

    /// Tells hint `key` what the pointer asks for: `Show` or `Dismiss`.
    fn hint_changed(&mut self, key: Key, shown: bool) {
        let node = &self.nodes[key as usize];
        let kind = if shown {
            EventKind::Show
        } else {
            EventKind::Dismiss
        };
        if node.events.contains(kind) {
            self.events.push(crate::element::JsEvent::Event(
                node.id,
                kind,
                EventPayload::None,
            ));
        }
    }

    /// A finger or button went down: the hints whose anchors it is on hide
    /// (and wait for the pointer to come again).
    fn hint_touches(&mut self, touch: &TouchEvent) {
        let hints: Vec<Key> = self
            .overlays
            .iter()
            .copied()
            .filter(|&k| {
                self.overlay(k)
                    .is_some_and(|s| s.config.hint.is_some() && (s.hinted || s.waiting.is_some()))
            })
            .collect();
        for key in hints {
            let Some(anchor) = self.hint_anchor(key).map(|k| self.nodes[k as usize].id) else {
                continue;
            };
            let pressed = touch
                .points
                .iter()
                .any(|p| p.changed && p.path.contains(&anchor));
            if pressed {
                self.unhint(key);
            }
        }
    }

    /// Sets the `tooltip` of node `key`: a hint overlay the core makes
    /// inside it, showing the element JS made.
    pub(super) fn set_tooltip(&mut self, key: Key, content: TooltipContent) {
        let id = self.nodes[key as usize].id;
        let mut made = self.overlay_state.tooltips.get(&id).copied();
        // The element was the hint overlay itself: anything else starts
        // again from a hint overlay of the core's.
        if let Some(adopted) = made.filter(|m| m.content == Some(m.overlay)) {
            if content == TooltipContent::Node(adopted.overlay) {
                return;
            }
            self.overlay_state.tooltips.remove(&id);
            self.release_tooltip(adopted);
            made = None;
        }
        let made = match (content, made) {
            (TooltipContent::None, made) => {
                if let Some(made) = made {
                    self.overlay_state.tooltips.remove(&id);
                    self.release_tooltip(made);
                }
                return;
            }
            (TooltipContent::Node(node), made) => {
                let mut made = match made {
                    Some(made) => made,
                    None => match self.make_tooltip(id) {
                        Some(made) => made,
                        None => return,
                    },
                };
                if made.content == Some(node) {
                    return;
                }
                if let Some(old) = made.content.take() {
                    self.detach_tooltip_content(old);
                }
                match self.insert(made.overlay, node, None) {
                    Ok(()) => made.content = Some(node),
                    Err(e) => log::error!(target: "js", "a tooltip's element is not shown: {e:?}"),
                }
                made
            }
        };
        self.overlay_state.tooltips.insert(id, made);
    }

    /// A hint overlay inside element `id`, above it, for its `tooltip`.
    fn make_tooltip(&mut self, id: NodeId) -> Option<CoreTooltip> {
        let overlay = self.components.alloc();
        let gap = px(TOOLTIP_GAP);
        let ops = [
            Op::Create {
                id: overlay,
                tag: Tag::Overlay,
            },
            // Never in the way of the pointer: the element works as without.
            Op::SetProp {
                id: overlay,
                prop: Prop::PointerEvents(PointerEvents::None),
            },
            Op::SetProp {
                id: overlay,
                prop: Prop::Overlay(OverlayProp::Hint(Some(HINT_DELAY))),
            },
            Op::SetProp {
                id: overlay,
                prop: Prop::Overlay(OverlayProp::Placement(Placement::Top)),
            },
            Op::SetProp {
                id: overlay,
                prop: Prop::Overlay(OverlayProp::Offset(gap)),
            },
            Op::SetProp {
                id: overlay,
                prop: Prop::Overlay(OverlayProp::Margin(gap)),
            },
        ];
        for op in ops {
            if let Err(e) = self.apply_op(op) {
                log::error!(target: "js", "a tooltip is not made: {e:?}");
                return None;
            }
        }
        // Into the element itself, wherever a component places its
        // children.
        if let Err(e) = self.insert(id, overlay, None) {
            log::error!(target: "js", "a tooltip is not made: {e:?}");
            let _ = self.release(overlay);
            return None;
        }
        Some(CoreTooltip {
            overlay,
            content: None,
        })
    }

    /// The elements of `tooltip`s that are hint overlays themselves (a
    /// `Tooltip` of `nsh-base`, whose root its view made one) take the place
    /// of the core's: in the element, with their own placement, delay and
    /// look (after the components rendered, before the overlays are shown).
    fn adopt_hint_tooltips(&mut self) {
        let adoptable: Vec<(NodeId, CoreTooltip)> = self
            .overlay_state
            .tooltips
            .iter()
            .filter(|(_, m)| {
                m.content.is_some_and(|c| {
                    c != m.overlay
                        && self.key(c).is_ok_and(|k| {
                            self.nodes[k as usize].is_overlay()
                                && self.overlay(k).is_some_and(|s| s.config.hint.is_some())
                        })
                })
            })
            .map(|(&id, &m)| (id, m))
            .collect();
        for (id, mut made) in adoptable {
            let content = made.content.expect("content");
            if let Err(e) = self.insert(id, content, None) {
                log::error!(target: "js", "a tooltip's element is not shown: {e:?}");
                continue;
            }
            let _ = self.release(made.overlay);
            made.overlay = content;
            self.overlay_state.tooltips.insert(id, made);
        }
    }

    /// The element shown as a tooltip leaves it (it stays JS's).
    fn detach_tooltip_content(&mut self, content: NodeId) {
        if let Ok(k) = self.key(content)
            && self.nodes[k as usize].parent != NO_KEY
        {
            self.detach(k);
        }
    }

    /// Releases the nodes made for a `tooltip`.
    fn release_tooltip(&mut self, made: CoreTooltip) {
        if let Some(content) = made.content {
            self.detach_tooltip_content(content);
            if content == made.overlay {
                // The element's own (a `Tooltip`'s): JS's to release.
                return;
            }
        }
        let _ = self.release(made.overlay);
    }

    /// Escape's binding, while an overlay listens to `onCancel`.
    pub(super) fn overlay_key_binding(&self) -> Option<KeyBinding> {
        if !self.overlay_state.cancel {
            return None;
        }
        let escape = Keystroke::parse_with("escape", false).ok()?;
        Some(KeyBinding::new(ESCAPE_BINDING, escape, When::Always, false))
    }

    /// Escape or back: `onCancel` of the top overlay listening to it;
    /// whether one took it.
    pub(super) fn overlay_cancel(&mut self) -> bool {
        let top = self.overlays.iter().rev().copied().find(|&k| {
            self.overlay(k).is_some_and(|s| s.host.is_some())
                && self.nodes[k as usize].events.contains(EventKind::Cancel)
        });
        let Some(key) = top else {
            return false;
        };
        let id = self.nodes[key as usize].id;
        self.events.push(crate::element::JsEvent::Event(
            id,
            EventKind::Cancel,
            EventPayload::None,
        ));
        true
    }

    /// A touch on the surface: `onPressOutside` of the shown overlays
    /// watching presses that a finger went down outside of.
    pub(super) fn overlay_touches(&mut self, touch: &TouchEvent) {
        if touch.phase != TouchPhase::Start {
            return;
        }
        if !self.overlay_state.hinting.is_empty() {
            self.hint_touches(touch);
        }
        if self.overlay_state.watched.is_empty() {
            return;
        }
        for point in touch.points.iter().filter(|p| p.changed) {
            for i in 0..self.overlays.len() {
                let key = self.overlays[i];
                let node = &self.nodes[key as usize];
                let Some(state) = self.overlay(key) else {
                    continue;
                };
                if state.host.is_none() || !node.events.contains(EventKind::PressOutside) {
                    continue;
                }
                let id = node.id;
                let anchor = anchor_node(state);
                let inside = point.path.iter().any(|&p| {
                    p == id
                        || Some(p) == anchor
                        || self.key(p).is_ok_and(|k| {
                            self.nodes[k as usize].is_overlay() && self.is_ancestor(key, k)
                        })
                });
                if inside {
                    continue;
                }
                self.events.push(crate::element::JsEvent::Event(
                    id,
                    EventKind::PressOutside,
                    EventPayload::Map(vec![("pagePosition", PayloadValue::Point(point.page))]),
                ));
            }
        }
    }

    /// The size of the root view overlay `key` is shown in.
    fn overlay_host_size(&self, host: NodeId) -> Option<(Size<Points>, crate::EdgeInsets)> {
        if host == ROOT_ID {
            return Some((self.viewport, self.safe_area));
        }
        let root = self.nodes[self.key(host).ok()? as usize].root.as_ref()?;
        Some((root.reported.unwrap_or(self.viewport), root.insets))
    }

    /// Lays out shown overlay `key` in a box the size of its root view,
    /// when that size or its content changed, and places it.
    pub(super) fn layout_overlay(&mut self, key: Key) {
        let Some(state) = self.overlay(key) else {
            return;
        };
        let (Some(host), wrapper) = (state.host, state.wrapper) else {
            return;
        };
        let Some((size, _)) = self.overlay_host_size(host) else {
            return;
        };
        let root = self.nodes[key as usize].root.as_ref().expect("root state");
        let changed = root.laid_out != Some((size, 0.0));
        if !changed && !self.layout.dirty(wrapper) {
            return;
        }
        if changed {
            let style = taffy::Style {
                size: taffy::Size {
                    width: taffy::Dimension::length(size.width.0),
                    height: taffy::Dimension::length(size.height.0),
                },
                ..taffy::Style::default()
            };
            self.layout.set_style(wrapper, style);
        }
        self.nodes[key as usize]
            .root
            .as_mut()
            .expect("root state")
            .laid_out = Some((size, 0.0));
        self.layout_root(wrapper, definite(size));
        self.emit_frames(key);
    }

    /// Where overlay `key` of `frame` (as laid out in its root view) goes:
    /// against its anchor when it has one it can find. Reports a new
    /// placement to `onPosition`.
    pub(super) fn overlay_origin(&mut self, key: Key, frame: Bounds<Points>) -> Point<Points> {
        let Some(state) = self.overlay(key) else {
            return frame.origin;
        };
        let (config, Some(host)) = (state.config, state.host) else {
            return frame.origin;
        };
        let anchor = match self.resolved_anchor(key) {
            OverlayAnchor::None => None,
            OverlayAnchor::Point(p) => Some(Bounds {
                origin: p,
                size: Size::default(),
            }),
            OverlayAnchor::Bounds(bounds) => Some(bounds),
            OverlayAnchor::Node(id) => self.measure(id).map(|m| Bounds {
                origin: m.page,
                size: m.frame.size,
            }),
            OverlayAnchor::Content(id) => self.content_bounds(id),
        };
        let Some(anchor) = anchor else {
            return frame.origin;
        };
        let Some((size, insets)) = self.overlay_host_size(host) else {
            return frame.origin;
        };
        let m = config.margin.0;
        let area = inset(
            rect(0.0, 0.0, size.width.0, size.height.0),
            [
                insets.top.0 + m,
                insets.right.0 + m,
                insets.bottom.0 + m,
                insets.left.0 + m,
            ],
        );
        let placed = place(
            anchor,
            frame.size,
            area,
            &config,
            self.nodes[key as usize].rtl,
        );
        let bounds = Bounds {
            origin: placed.origin,
            size: frame.size,
        };
        let node = &self.nodes[key as usize];
        let (id, listens) = (node.id, node.events.contains(EventKind::Position));
        let state = self.overlay_mut(key).expect("overlay");
        if state.placed != Some((placed, bounds)) {
            state.placed = Some((placed, bounds));
            if listens {
                self.events.push(crate::element::JsEvent::Event(
                    id,
                    EventKind::Position,
                    EventPayload::Map(vec![
                        (
                            "placement",
                            PayloadValue::Str(placed.placement.name().into()),
                        ),
                        ("bounds", PayloadValue::Bounds(bounds)),
                        ("available", PayloadValue::Size(placed.available)),
                    ]),
                ));
            }
        }
        placed.origin
    }

    /// The box around what view `id` draws in its root view: around its
    /// children's frames, or its own while it has none (laid out ones only).
    fn content_bounds(&self, id: NodeId) -> Option<Bounds<Points>> {
        let own = self.measure(id)?;
        let key = self.key(id).ok()?;
        let mut out: Option<(f32, f32, f32, f32)> = None;
        for &c in &self.nodes[key as usize].children {
            let n = &self.nodes[c as usize];
            if n.is_overlay() || n.layout.is_none() {
                continue;
            }
            let Some(m) = self.measure(n.id) else {
                continue;
            };
            let (x0, y0) = (m.page.x.0, m.page.y.0);
            let (x1, y1) = (x0 + m.frame.size.width.0, y0 + m.frame.size.height.0);
            out = Some(match out {
                None => (x0, y0, x1, y1),
                Some((a, b, c, d)) => (a.min(x0), b.min(y0), c.max(x1), d.max(y1)),
            });
        }
        Some(match out {
            Some((x0, y0, x1, y1)) => rect(x0, y0, x1 - x0, y1 - y0),
            None => Bounds {
                origin: own.page,
                size: own.frame.size,
            },
        })
    }

    /// Places the shown overlays with an anchor again, at the end of the
    /// commit: their anchors may have moved (the layout, the lists,
    /// scrolling).
    pub(super) fn place_overlays(&mut self) {
        for i in 0..self.overlays.len() {
            let key = self.overlays[i];
            let anchored = self.overlay(key).is_some_and(|s| s.host.is_some())
                && self.resolved_anchor(key) != OverlayAnchor::None;
            let frame = self.nodes[key as usize].frame;
            if !anchored || !self.nodes[key as usize].created || frame.size.width.0.is_nan() {
                continue;
            }
            let origin = self.overlay_origin(key, frame);
            if origin != frame.origin {
                self.set_frame(
                    key,
                    Bounds {
                        origin,
                        size: frame.size,
                    },
                );
            }
        }
    }
}

/// The view an overlay is anchored to.
fn anchor_node(state: &OverlayState) -> Option<NodeId> {
    match state.config.anchor {
        OverlayAnchor::Node(id) | OverlayAnchor::Content(id) => Some(id),
        _ => None,
    }
}