sgl-2d 0.4.2

Caller-driven 2D rendering, assets, text, lighting, and immediate UI for SGL games
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
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
//! Overlay-band content: tooltips, overlay panels, dropdown popovers and the
//! confirmation modal.

use super::{UiFrame, UiKey, contains, widget_id};
use crate::canvas::draw::Rect;
use crate::canvas::text::{HAlign, TextStyle, VAlign};
use sgl_core::math::Vec2;

impl UiFrame<'_> {
    /// A hover tooltip for `anchor`: while the pointer is over the (clip- and
    /// blocking-gated) anchor rect, draws `text` in the overlay band just
    /// below it, clamped into `bounds`. Purely cosmetic — never blocks input.
    pub fn tooltip(&mut self, anchor: Rect, bounds: Rect, text: &str, px: f32) {
        if text.is_empty() || !self.hit(&anchor) {
            return;
        }
        self.draw_tooltip(anchor, bounds, text, px);
    }

    /// Tooltip on hover or keyboard focus, including disabled icon buttons.
    /// The anchor must intersect both the viewport and current clip. Popup and
    /// modal focus scopes apply. Text wraps to the viewport; excess height clips.
    pub fn tooltip_for(&mut self, name: &str, anchor: Rect, bounds: Rect, text: &str, px: f32) {
        let Some(visible) = anchor.intersection(&bounds) else {
            return;
        };
        if self
            .clip
            .is_some_and(|clip| visible.intersection(&clip).is_none())
        {
            return;
        }
        let focused = self.ui.focus == Some(widget_id(name))
            && (!self.ui.modal_blocking || self.modal_scope == self.ui.modal_scope)
            && (self.ui.open_popup.is_none() || self.popup_scope == self.ui.open_popup);
        if !text.is_empty() && (focused || self.hit(&anchor)) {
            self.draw_tooltip(anchor, bounds, text, px);
        }
    }

    fn draw_tooltip(&mut self, anchor: Rect, bounds: Rect, text: &str, px: f32) {
        let pad = 6.0;
        if bounds.size().x <= 0.0 || bounds.size().y <= 0.0 {
            return;
        }
        let available = (bounds.size().x - 4.0).max(0.0);
        let text_width = (available - pad * 2.0).max(1.0);
        let mut lines = Vec::new();
        for paragraph in text.split('\n') {
            let mut line = String::new();
            for word in paragraph.split_whitespace() {
                let candidate = if line.is_empty() {
                    word.to_owned()
                } else {
                    format!("{line} {word}")
                };
                if !line.is_empty() && self.text.measure(&candidate, px).x > text_width {
                    lines.push(std::mem::take(&mut line));
                }
                if !line.is_empty() {
                    line.push(' ');
                }
                for ch in word.chars() {
                    let mut candidate = line.clone();
                    candidate.push(ch);
                    if !line.is_empty() && self.text.measure(&candidate, px).x > text_width {
                        lines.push(std::mem::take(&mut line));
                    }
                    line.push(ch);
                }
            }
            lines.push(line);
        }
        let width = lines
            .iter()
            .map(|line| self.text.measure(line, px).x)
            .fold(0.0, f32::max);
        let w = (width + pad * 2.0).min(available);
        let line_h = self.text.line_height(px);
        let h = (line_h * lines.len() as f32 + pad).min(bounds.size().y);
        let x = anchor
            .min
            .x
            .clamp(bounds.min.x, (bounds.max.x - w).max(bounds.min.x));
        let below = anchor.max.y + 4.0;
        let y = if below + h <= bounds.max.y {
            below
        } else {
            anchor.min.y - h - 4.0
        }
        .clamp(bounds.min.y, (bounds.max.y - h).max(bounds.min.y));
        let rect = Rect::new(x, y, w, h);
        let overlay_was = self.overlay;
        let clip_was = self.clip;
        self.overlay = true;
        self.set_clip(Some(bounds));
        self.rect(rect, self.ui.theme.popup);
        self.border(rect, 1.0, self.ui.theme.accent);
        self.set_clip(Some(rect));
        let style = TextStyle::new(px, self.ui.theme.text).with_outline(self.ui.theme.text_outline);
        for (index, line) in lines.iter().enumerate() {
            self.label(
                Rect::new(
                    x + pad,
                    y + pad * 0.5 + index as f32 * line_h,
                    width,
                    line_h,
                ),
                line,
                &style,
                HAlign::Left,
                VAlign::Top,
            );
        }
        self.set_clip(clip_was);
        self.overlay = overlay_was;
    }

    /// Begin a custom overlay panel (R-17/#25: the editor's File panel):
    /// widgets drawn until [`overlay_panel_end`](Self::overlay_panel_end)
    /// go to the overlay band (above ordinary widgets) and stay
    /// interactive, while `panel` becomes an input blocker for the
    /// ordinary widgets underneath on the following frames — the same
    /// semantics as an open dropdown popover, for panels the caller lays
    /// out itself. Draws the panel backdrop + accent border. Like a popover,
    /// only the part inside the current clip blocks. Open popovers and
    /// modals still draw above and block this panel's controls.
    pub fn overlay_panel_begin(&mut self, panel: Rect) {
        self.overlay = true;
        self.new_blocked.extend(
            self.clip
                .map_or(Some(panel), |clip| clip.intersection(&panel)),
        );
        self.rect(panel, self.ui.theme.popup);
        self.border(panel, 2.0, self.ui.theme.accent);
    }

    /// End the current overlay panel (see
    /// [`overlay_panel_begin`](Self::overlay_panel_begin)).
    pub fn overlay_panel_end(&mut self) {
        self.overlay = false;
    }

    /// A custom dropdown (never a native HTML `select`): a button showing
    /// `options[*selected]`; pressing it opens a popover listing every
    /// option in the overlay band (drawn above and input-blocking the
    /// widgets underneath). Selecting an option stores it and closes;
    /// pressing elsewhere closes. The popover opens downward and stays in
    /// the current clip: only its visible part shows, takes presses and
    /// blocks widgets underneath. Place dropdowns where
    /// `rect.max.y + options·row` stays on screen and inside the clip.
    /// Submit it every frame while shown: an open dropdown not submitted in
    /// a frame, or whose button and popover are both outside the clip,
    /// closes at [`end`](Self::end), releasing keyboard capture.
    /// Returns `true` the frame the selection changed.
    pub fn dropdown(
        &mut self,
        name: &str,
        rect: Rect,
        options: &[&str],
        selected: &mut usize,
        px: f32,
    ) -> bool {
        let id = widget_id(name);
        let was_open = self.ui.open_popup == Some(id);

        let (fired, held, hover) = self.button_behavior(name, &rect);
        let bg = if held || was_open {
            self.ui.theme.pressed
        } else if hover {
            self.ui.theme.hovered
        } else {
            self.ui.theme.control
        };
        self.rect(rect, bg);
        let fg = if hover {
            self.ui.theme.text_hovered
        } else {
            self.ui.theme.text
        };
        let style = TextStyle::new(px, fg).with_outline(self.ui.theme.text_outline);
        let pad = 8.0;
        let inner = Rect::new(
            rect.min.x + pad,
            rect.min.y,
            (rect.size().x - 2.0 * pad).max(0.0),
            rect.size().y,
        );
        let current = options.get(*selected).copied().unwrap_or("");
        self.label(inner, current, &style, HAlign::Left, VAlign::Center);
        self.label(
            inner,
            if was_open { "^" } else { "v" },
            &style,
            HAlign::Right,
            VAlign::Center,
        );

        if fired {
            self.ui.open_popup = if was_open { None } else { Some(id) };
        }
        let row_h = rect.size().y;
        let pop = Rect::new(
            rect.min.x,
            rect.max.y + 2.0,
            rect.size().x,
            row_h * options.len() as f32,
        );
        // The popover draws and hit-tests within the current clip, so only
        // its visible part blocks widgets underneath or counts as inside.
        let visible = self.clip.map_or(Some(pop), |clip| clip.intersection(&pop));
        // An open dropdown whose button and popover are both clipped away
        // (scrolled out of view) counts as not submitted and closes at `end`.
        let button_visible = self
            .clip
            .is_none_or(|clip| clip.intersection(&rect).is_some());
        self.popup_seen |= self.ui.open_popup == Some(id) && (button_visible || visible.is_some());
        if !was_open || fired {
            // The popover first draws next frame; shield what lies under it.
            self.shield_open_popover(id, visible);
            return false;
        }

        // Open popover (overlay band).
        let overlay_was = self.overlay;
        self.overlay = true;
        self.popup_scope = Some(id);
        self.rect(pop, self.ui.theme.popup);
        self.border(pop, 1.0, self.ui.theme.accent);
        let mut changed = false;
        for (i, option) in options.iter().enumerate() {
            let row = Rect::new(pop.min.x, pop.min.y + row_h * i as f32, pop.size().x, row_h);
            let row_name = format!("{name}.opt{i}");
            let (row_fired, row_held, row_hover) = self.button_behavior(&row_name, &row);
            if row_held || row_hover {
                self.rect(
                    row,
                    if row_held {
                        self.ui.theme.pressed
                    } else {
                        self.ui.theme.hovered
                    },
                );
            }
            let fg = if i == *selected {
                self.ui.theme.accent
            } else {
                self.ui.theme.text
            };
            let row_style = TextStyle::new(px, fg).with_outline(self.ui.theme.text_outline);
            let row_inner = Rect::new(row.min.x + pad, row.min.y, row.size().x - 2.0 * pad, row_h);
            self.label(row_inner, option, &row_style, HAlign::Left, VAlign::Center);
            if row_fired {
                *selected = i;
                changed = true;
                self.ui.open_popup = None;
                self.ui.focus = Some(id);
            }
        }
        self.popup_scope = None;
        self.overlay = overlay_was;

        // Click-away close (a press neither on the button nor the visible
        // popover).
        let p = self.input.mouse_pos;
        if self.input.mouse_pressed
            && !contains(&rect, p)
            && !visible.is_some_and(|visible| contains(&visible, p))
        {
            self.ui.open_popup = None;
        }
        self.shield_open_popover(id, visible);
        changed
    }

    /// Shield what lies under dropdown `id`'s visible popover next frame
    /// only if it is still open, so a selection or click-away frees that
    /// spot at once.
    fn shield_open_popover(&mut self, id: u64, visible: Option<Rect>) {
        if self.ui.open_popup == Some(id) {
            self.new_popover_blocked = visible.map(|rect| (id, rect));
        }
    }

    /// A modal confirmation dialog in the overlay band: dims `screen`,
    /// draws `panel` with a title, a body line, and CONFIRM/CANCEL buttons.
    /// While shown it draws above and blocks all widgets outside the dialog,
    /// including custom overlay panels. Like a dropdown's popover, it ignores
    /// the pointer press and Escape of the frame that first shows it, so the
    /// input that opened it cannot also resolve it. Returns `Some(true)` on confirm,
    /// `Some(false)` on cancel, `None` otherwise — the **caller** owns the open/closed state.
    #[allow(clippy::too_many_arguments)]
    pub fn confirm_modal(
        &mut self,
        name: &str,
        screen: Rect,
        title: &str,
        body: &str,
        confirm_label: &str,
        cancel_label: &str,
        px: f32,
    ) -> Option<bool> {
        // Shown and left unresolved last frame (see `hit` for the pointer).
        let shown = self.ui.modal_scope == Some(widget_id(name));
        let overlay_was = self.overlay;
        self.overlay = true;
        self.ui.keyboard_captured = true;
        self.ui.open_popup = None;
        let modal_was = self.modal_scope;
        self.modal_scope = Some(widget_id(name));

        let panel_size = Vec2::new(420.0, 160.0);
        let center = (screen.min + screen.max) * 0.5;
        let panel = Rect::new(
            center.x - panel_size.x * 0.5,
            center.y - panel_size.y * 0.5,
            panel_size.x,
            panel_size.y,
        );
        self.rect(screen, self.ui.theme.modal_dim);
        self.rect(panel, self.ui.theme.popup);
        self.border(panel, 2.0, self.ui.theme.accent);

        let title_style = TextStyle::new(px * 1.2, self.ui.theme.text_pressed)
            .with_outline(self.ui.theme.text_outline);
        self.label(
            Rect::new(panel.min.x, panel.min.y + 12.0, panel_size.x, 32.0),
            title,
            &title_style,
            HAlign::Center,
            VAlign::Center,
        );
        let body_style =
            TextStyle::new(px * 0.8, self.ui.theme.text).with_outline(self.ui.theme.text_outline);
        self.label(
            Rect::new(
                panel.min.x + 16.0,
                panel.min.y + 52.0,
                panel_size.x - 32.0,
                32.0,
            ),
            body,
            &body_style,
            HAlign::Center,
            VAlign::Center,
        );

        let btn = Vec2::new(150.0, 44.0);
        let gap = 40.0;
        let y = panel.max.y - btn.y - 16.0;
        let confirm_rect = Rect::new(center.x - btn.x - gap * 0.5, y, btn.x, btn.y);
        let cancel_rect = Rect::new(center.x + gap * 0.5, y, btn.x, btn.y);
        let confirm = self.button(&format!("{name}.confirm"), confirm_rect, confirm_label, px);
        let cancel = self.button(&format!("{name}.cancel"), cancel_rect, cancel_label, px);

        self.overlay = overlay_was;
        self.modal_scope = modal_was;
        let result = if confirm {
            Some(true)
        } else if cancel
            || (shown && self.input.keys.contains(&UiKey::Escape) && !self.popup_dismissed)
        {
            Some(false)
        } else {
            None
        };
        // Blocking arms for the next frame only while the modal stays
        // unresolved: on the frame CONFIRM/CANCEL fires the caller closes
        // the modal, and ordinary widgets must be live again immediately
        // (no one-frame lockout after resolution).
        if result.is_none() {
            self.new_modal = Some(widget_id(name));
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::canvas::draw::DrawList;
    use crate::ui::UiInput;
    use crate::ui::test_ui::{BTN, ROW, fixture, hover_at, press_at};
    use wasm_bindgen_test::wasm_bindgen_test;

    #[wasm_bindgen_test(unsupported = test)]
    fn modal_pointer_scope_excludes_custom_overlay() {
        let (mut ui, mut text, _assets) = fixture();
        let screen = Rect::new(0.0, 0.0, 960.0, 540.0);
        let mut modal_open = true;
        let mut fired = Vec::new();
        for (index, input) in [
            UiInput::default(),
            press_at(50.0, 30.0),   // Under the dim layer, outside the dialog.
            press_at(575.0, 312.0), // The modal's Cancel button.
            press_at(50.0, 30.0),
        ]
        .into_iter()
        .enumerate()
        {
            let mut list = DrawList::new();
            let mut f = ui.begin(&mut text, &mut list, input);
            f.overlay_panel_begin(BTN);
            fired.push(f.button("panel.delete", BTN, "Delete", 20.0));
            f.overlay_panel_end();
            if modal_open {
                let result = f.confirm_modal(
                    "modal",
                    screen,
                    "Delete room",
                    "Discard changes?",
                    "Delete",
                    "Cancel",
                    20.0,
                );
                assert_eq!(result, if index == 2 { Some(false) } else { None });
                modal_open = result.is_none();
            }
            f.end();
        }
        assert_eq!(fired, [false, false, false, true]);
        assert!(
            !modal_open,
            "the modal's own Cancel button remains interactive"
        );
    }

    /// The press or Escape that opens a modal must not also resolve it.
    #[wasm_bindgen_test(unsupported = test)]
    fn modal_ignores_input_on_its_opening_frame() {
        let (mut ui, mut text, _assets) = fixture();
        let screen = Rect::new(0.0, 0.0, 960.0, 540.0);
        // The opener covers the modal's Confirm button (x=310..460, y=290..334).
        let opener = Rect::new(300.0, 280.0, 200.0, 60.0);
        let escape = || UiInput {
            keys: vec![UiKey::Escape],
            ..UiInput::default()
        };
        for (input, expected) in [
            (press_at(385.0, 312.0), Some(true)),
            (escape(), Some(false)),
        ] {
            let mut open = expected == Some(false);
            let mut results = Vec::new();
            for _ in 0..2 {
                let mut list = DrawList::new();
                let mut f = ui.begin(&mut text, &mut list, input.clone());
                if f.button("delete", opener, "Delete", 20.0) {
                    open = true;
                }
                if open {
                    let result = f.confirm_modal(
                        "modal", screen, "Delete", "Sure?", "Delete", "Cancel", 20.0,
                    );
                    open = result.is_none();
                    results.push(result);
                }
                f.end();
            }
            assert_eq!(results, [None, expected]);
        }
    }

    #[wasm_bindgen_test(unsupported = test)]
    fn overlay_dropdown_selection_blocks_buttons_in_either_submission_order() {
        for under_first in [false, true] {
            let (mut ui, mut text, _assets) = fixture();
            let dd = Rect::new(10.0, 10.0, 160.0, 30.0);
            let under = Rect::new(10.0, 72.0, 160.0, 30.0);
            let mut selected = 0;
            let mut fired = Vec::new();
            let mut changed = Vec::new();
            for input in [
                press_at(50.0, 25.0),
                hover_at(50.0, 85.0, false),
                press_at(50.0, 85.0),
                press_at(50.0, 85.0),
            ] {
                let mut list = DrawList::new();
                let mut f = ui.begin(&mut text, &mut list, input);
                f.overlay_panel_begin(Rect::new(0.0, 0.0, 200.0, 160.0));
                if under_first {
                    fired.push(f.button("under", under, "Delete", 20.0));
                }
                changed.push(f.dropdown("menu", dd, &["a", "b", "c"], &mut selected, 20.0));
                if !under_first {
                    fired.push(f.button("under", under, "Delete", 20.0));
                }
                f.overlay_panel_end();
                f.end();
            }
            assert_eq!(fired, [false, false, false, true]);
            assert_eq!(changed, [false, false, true, false]);
            assert_eq!(selected, 1);
            assert!(!ui.any_popup_open());
        }
    }

    /// A modal opened over an open popover closes it, and the next frame its
    /// Cancel takes a press where the popover was.
    #[wasm_bindgen_test(unsupported = test)]
    fn modal_closes_open_popover_and_takes_presses_beneath_it() {
        let (mut ui, mut text, _assets) = fixture();
        let mut selected = 0;
        // The popover occupies y=282..372 over the modal's Cancel button.
        let dd = Rect::new(500.0, 250.0, 160.0, 30.0);
        for input in [press_at(550.0, 265.0), UiInput::default()] {
            let mut list = DrawList::new();
            let mut f = ui.begin(&mut text, &mut list, input);
            f.dropdown("menu", dd, &["a", "b", "c"], &mut selected, 20.0);
            f.end();
        }
        assert!(ui.any_popup_open());
        // The modal opens while the popover is still submitted, then its
        // Cancel takes a press inside the popover's former shield.
        for (input, expected) in [
            (UiInput::default(), None),
            (press_at(575.0, 312.0), Some(false)),
        ] {
            let mut list = DrawList::new();
            let mut f = ui.begin(&mut text, &mut list, input);
            f.dropdown("menu", dd, &["a", "b", "c"], &mut selected, 20.0);
            assert_eq!(
                f.confirm_modal(
                    "modal",
                    Rect::new(0.0, 0.0, 960.0, 540.0),
                    "Delete",
                    "Sure?",
                    "Delete",
                    "Cancel",
                    20.0,
                ),
                expected,
            );
            f.end();
            assert!(!ui.any_popup_open());
        }
    }

    #[wasm_bindgen_test(unsupported = test)]
    fn popover_and_modal_draw_above_later_overlay_controls() {
        let (mut ui, mut text, _assets) = fixture();
        let dd = Rect::new(10.0, 10.0, 160.0, 30.0);
        let under = Rect::new(10.0, 72.0, 160.0, 30.0);
        let mut selected = 0;
        let mut list = DrawList::new();
        let mut f = ui.begin(&mut text, &mut list, press_at(50.0, 25.0));
        f.overlay_panel_begin(Rect::new(0.0, 0.0, 200.0, 160.0));
        f.dropdown("menu", dd, &["a", "b", "c"], &mut selected, 20.0);
        f.overlay_panel_end();
        f.end();

        let mut list = DrawList::new();
        let mut f = ui.begin(&mut text, &mut list, UiInput::default());
        f.overlay_panel_begin(Rect::new(0.0, 0.0, 200.0, 160.0));
        let popup_start = f.list.screen.len();
        f.dropdown("menu", dd, &["a", "b", "c"], &mut selected, 20.0);
        let popup_draws = f.list.screen[popup_start..].to_vec();
        let button_start = f.list.screen.len();
        f.button("under", under, "Delete", 20.0);
        let button_draws = f.list.screen[button_start..].to_vec();
        f.overlay_panel_end();
        f.end();
        // The dropdown button is part of the panel; its popover's backdrop
        // covers y=42..132, including the later control at y=72..102.
        let popover = popup_draws
            .iter()
            .find(|draw| draw.scale == Vec2::new(160.0, 90.0))
            .unwrap();
        assert!(button_draws.iter().all(|draw| draw.z < popover.z));

        let mut list = DrawList::new();
        let mut f = ui.begin(&mut text, &mut list, UiInput::default());
        f.confirm_modal(
            "modal",
            Rect::new(0.0, 0.0, 960.0, 540.0),
            "Delete",
            "Sure?",
            "Delete",
            "Cancel",
            20.0,
        );
        let dim = f.list.screen[0];
        let panel_start = f.list.screen.len();
        f.overlay_panel_begin(BTN);
        f.button("later", BTN, "Background", 20.0);
        f.overlay_panel_end();
        assert!(
            f.list.screen[panel_start..]
                .iter()
                .all(|draw| draw.z < dim.z)
        );
        f.end();
    }

    /// Classify emitted rects and flushed glyphs by the clips supplied to
    /// each scope, and by the dropdown geometry (its button is ordinary).
    fn assert_draw_hierarchy(list: &mut DrawList, clips: [Rect; 4], dropdown: Rect) {
        list.sort();
        let bands: Vec<_> = list
            .screen
            .iter()
            .map(|draw| {
                let scope = clips
                    .iter()
                    .position(|clip| draw.clip == Some(*clip))
                    .unwrap();
                if scope == 2 && draw.pos.y <= dropdown.max.y {
                    0
                } else {
                    scope
                }
            })
            .collect();
        for band in 0..4 {
            assert!(bands.contains(&band), "scope {band} must emit draws");
        }
        assert!(
            bands.windows(2).all(|pair| pair[0] <= pair[1]),
            "ordinary draws, panels, popovers and modals must sort in that order"
        );
    }

    #[wasm_bindgen_test(unsupported = test)]
    fn long_widget_lists_stay_below_open_popover_and_modal() {
        let (mut ui, mut text, mut assets) = fixture();
        let dropdown = Rect::new(10.0, 10.0, 160.0, 30.0);
        let options = ["First", "Second"];
        let mut selected = 0;
        let mut opening = DrawList::new();
        let mut f = ui.begin(&mut text, &mut opening, press_at(50.0, 25.0));
        f.dropdown("menu", dropdown, &options, &mut selected, 16.0);
        f.end();
        text.end_frame(&mut assets, &mut opening);
        assert!(
            ui.any_popup_open(),
            "the pointer press must open the dropdown"
        );

        let clips = [
            Rect::new(0.0, 0.0, 960.0, 540.0),
            Rect::new(0.0, 0.0, 961.0, 540.0),
            Rect::new(0.0, 0.0, 962.0, 540.0),
            Rect::new(0.0, 0.0, 963.0, 540.0),
        ];
        let mut list = DrawList::new();
        let mut f = ui.begin(&mut text, &mut list, UiInput::default());
        f.set_clip(Some(clips[2]));
        f.dropdown("menu", dropdown, &options, &mut selected, 16.0);
        f.set_clip(Some(clips[3]));
        f.confirm_modal(
            "exit", clips[0], "Exit?", "Discard?", "Exit", "Cancel", 16.0,
        );
        // Submit the lower layers later: sorting must put them underneath.
        f.set_clip(Some(clips[1]));
        f.overlay_panel_begin(Rect::new(400.0, 100.0, 160.0, 200.0));
        for _ in 0..6_001 {
            f.rect(Rect::new(420.0, 120.0, 20.0, 20.0), [1.0, 0.0, 0.0, 1.0]);
        }
        f.overlay_panel_end();
        f.set_clip(Some(clips[0]));
        // This also exceeds the former modal band, catching ordinary widgets
        // covering its dim layer as well as its dialog.
        for _ in 0..18_001 {
            f.rect(Rect::new(420.0, 120.0, 20.0, 20.0), [0.0, 1.0, 0.0, 1.0]);
        }
        f.end();
        text.end_frame(&mut assets, &mut list);
        assert_draw_hierarchy(&mut list, clips, dropdown);
    }

    #[wasm_bindgen_test(unsupported = test)]
    fn exhausted_draw_bands_keep_hierarchy_and_stable_push_order() {
        let (mut ui, mut text, mut assets) = fixture();
        let dropdown = Rect::new(10.0, 10.0, 160.0, 30.0);
        let options = ["First", "Second", "Third", "Fourth"];
        let mut selected = 0;
        let mut opening = DrawList::new();
        let mut f = ui.begin(&mut text, &mut opening, press_at(50.0, 25.0));
        f.dropdown("menu", dropdown, &options, &mut selected, 16.0);
        f.end();
        text.end_frame(&mut assets, &mut opening);

        let clips = [
            Rect::new(0.0, 0.0, 960.0, 540.0),
            Rect::new(0.0, 0.0, 961.0, 540.0),
            Rect::new(0.0, 0.0, 962.0, 540.0),
            Rect::new(0.0, 0.0, 963.0, 540.0),
        ];
        let mut list = DrawList::new();
        let mut f = ui.begin(&mut text, &mut list, UiInput::default());
        // Set up the end of a huge frame without allocating millions of
        // earlier draws. The oracle is the resulting draw order, not cursor values.
        f.z = crate::ui::OVERLAY_Z - 2.0;
        // Keep a draw at the start of every higher band, so crossing even
        // its first draw is observable despite the higher band's later draws.
        f.set_clip(Some(clips[1]));
        f.overlay_panel_begin(Rect::new(400.0, 100.0, 160.0, 200.0));
        f.overlay_panel_end();
        f.set_clip(Some(clips[2]));
        f.dropdown("menu", dropdown, &options, &mut selected, 16.0);
        f.z_popover = crate::ui::MODAL_Z - 2.0;
        f.dropdown("menu", dropdown, &options, &mut selected, 16.0);
        f.set_clip(Some(clips[3]));
        f.confirm_modal(
            "exit", clips[0], "Exit?", "Discard?", "Exit", "Cancel", 16.0,
        );
        f.z_modal = 16_777_214.0;
        f.confirm_modal(
            "exit", clips[0], "Exit?", "Discard?", "Exit", "Cancel", 16.0,
        );
        f.z_overlay = crate::ui::POPOVER_Z - 2.0;
        f.set_clip(Some(clips[1]));
        f.overlay_panel_begin(Rect::new(400.0, 100.0, 160.0, 200.0));
        let marker = [0.2, 0.4, 0.6, 1.0];
        for x in [430.0, 410.0, 450.0] {
            f.rect(Rect::new(x, 120.0, 10.0, 10.0), marker);
        }
        f.overlay_panel_end();
        f.set_clip(Some(clips[0]));
        for x in [430.0, 410.0, 450.0] {
            f.rect(Rect::new(x, 120.0, 10.0, 10.0), marker);
        }
        f.end();
        text.end_frame(&mut assets, &mut list);
        assert_draw_hierarchy(&mut list, clips, dropdown);
        for clip in &clips[..2] {
            let positions: Vec<_> = list
                .screen
                .iter()
                .filter(|draw| draw.clip == Some(*clip) && draw.color == marker)
                .map(|draw| draw.pos.x)
                .collect();
            assert_eq!(
                positions,
                [435.0, 415.0, 455.0],
                "saturated rects retain push order"
            );
        }
    }

    #[wasm_bindgen_test(unsupported = test)]
    fn modal_keyboard_scope_excludes_custom_overlay() {
        let (mut ui, mut text, _assets) = fixture();
        for (index, keys) in [vec![], vec![UiKey::Tab], vec![UiKey::Enter]]
            .into_iter()
            .enumerate()
        {
            let mut list = DrawList::new();
            let mut f = ui.begin(
                &mut text,
                &mut list,
                UiInput {
                    keys,
                    shift: true,
                    ..UiInput::default()
                },
            );
            f.overlay_panel_begin(BTN);
            assert!(!f.button("panel.action", BTN, "Background", 20.0));
            f.overlay_panel_end();
            if index == 0 {
                f.focus("modal.confirm");
            }
            let result = f.confirm_modal(
                "modal",
                Rect::new(0.0, 0.0, 960.0, 540.0),
                "Delete room",
                "Discard changes?",
                "Delete room",
                "Keep room",
                20.0,
            );
            if index == 1 {
                assert!(f.is_focused("modal.cancel"));
            }
            assert_eq!(result, if index == 2 { Some(false) } else { None });
            f.end();
        }
    }

    #[wasm_bindgen_test(unsupported = test)]
    fn popup_keyboard_is_exclusive_and_escape_is_consumed() {
        let (mut ui, mut text, _assets) = fixture();
        let mut selected = 0;
        let dd = Rect::new(10.0, 10.0, 160.0, 30.0);
        for (i, keys) in [
            vec![],
            vec![UiKey::Enter],
            vec![],
            vec![UiKey::Tab],
            vec![UiKey::Tab],
            vec![UiKey::Space],
            vec![UiKey::Enter],
            vec![UiKey::Escape],
        ]
        .into_iter()
        .enumerate()
        {
            let mut list = DrawList::new();
            let mut f = ui.begin(
                &mut text,
                &mut list,
                UiInput {
                    keys,
                    ..UiInput::default()
                },
            );
            if i == 0 {
                f.focus("menu");
            }
            assert!(!f.button("under", ROW, "Under", 20.0));
            f.dropdown("menu", dd, &["a", "b"], &mut selected, 20.0);
            f.end();
            assert!(ui.keyboard_captured());
            if i == 4 {
                assert!(ui.is_focused_name("menu.opt1"));
            }
        }
        assert_eq!(selected, 1);
        assert!(!ui.any_popup_open());
        assert!(ui.is_focused_name("menu"));
    }

    /// Dropdown lifecycle: closed by default; press opens; an option press
    /// selects + closes; from the first frame the popover is drawn, its
    /// region blocks ordinary widgets underneath.
    #[wasm_bindgen_test(unsupported = test)]
    fn dropdown_opens_selects_and_blocks_underneath() {
        let (mut ui, mut text, _assets) = fixture();
        let mut list = DrawList::new();
        let dd = Rect::new(10.0, 10.0, 160.0, 30.0);
        let options = ["25%", "50%", "100%"];
        let mut selected = 2usize;

        // Frame 1: press the dropdown button → opens, no change.
        let mut f = ui.begin(&mut text, &mut list, press_at(50.0, 25.0));
        assert!(!f.dropdown("zoom", dd, &options, &mut selected, 16.0));
        f.end();
        assert!(ui.any_popup_open());

        // Frame 2: the popover's first drawn frame. Rows sit below the
        // button: row i occupies y ∈ [42 + 30i, 72 + 30i). An underlying
        // button at the popover position must be blocked while a click
        // there selects option 1 (row y = 72..102).
        let mut f = ui.begin(&mut text, &mut list, press_at(50.0, 85.0));
        let under = f.button("under", Rect::new(10.0, 42.0, 160.0, 90.0), "X", 16.0);
        assert!(!under, "widget under an open popover must not fire");
        let changed = f.dropdown("zoom", dd, &options, &mut selected, 16.0);
        f.end();
        assert!(changed);
        assert_eq!(selected, 1);
        assert!(!ui.any_popup_open(), "selection closes the popover");

        // Reopen, then click far away → closes without selection change.
        let mut f = ui.begin(&mut text, &mut list, press_at(50.0, 25.0));
        f.dropdown("zoom", dd, &options, &mut selected, 16.0);
        f.end();
        assert!(ui.any_popup_open());
        let mut f = ui.begin(&mut text, &mut list, press_at(800.0, 500.0));
        assert!(!f.dropdown("zoom", dd, &options, &mut selected, 16.0));
        f.end();
        assert!(!ui.any_popup_open(), "click-away closes");
        assert_eq!(selected, 1);
    }

    /// #575: once a selection, click-away, Escape or `close_popups` closes
    /// the popover, a press on a widget that lay under it fires.
    #[wasm_bindgen_test(unsupported = test)]
    fn closed_dropdown_frees_widgets_under_its_popover() {
        let (mut ui, mut text, _assets) = fixture();
        let dd = Rect::new(10.0, 10.0, 160.0, 30.0);
        let options = ["25%", "50%", "100%"];
        let mut selected = 0usize;
        // Rows occupy y ∈ [42, 132), over this button.
        let under = Rect::new(10.0, 42.0, 160.0, 90.0);
        let open = press_at(50.0, 25.0);
        let press_under = || press_at(50.0, 85.0);
        let escape_and_press_under = UiInput {
            keys: vec![UiKey::Escape],
            ..press_under()
        };
        let mut fired = Vec::new();
        let mut open_after = Vec::new();
        for (input, close_popups) in [
            (open.clone(), false),
            (press_under(), false), // selects option 1
            (press_under(), false),
            (open.clone(), false),
            (press_at(800.0, 500.0), false), // click-away
            (press_under(), false),
            (open.clone(), false),
            (UiInput::default(), false),
            (escape_and_press_under, false),
            (open, false),
            (UiInput::default(), false),
            (press_under(), true),
        ] {
            if close_popups {
                ui.close_popups();
            }
            let mut list = DrawList::new();
            let mut f = ui.begin(&mut text, &mut list, input);
            fired.push(f.button("under", under, "U", 16.0));
            f.dropdown("zoom", dd, &options, &mut selected, 16.0);
            f.end();
            open_after.push(ui.any_popup_open());
        }
        assert_eq!(
            fired,
            [
                false, false, true, false, false, true, false, false, true, false, false, true
            ]
        );
        assert_eq!(
            open_after,
            [
                true, false, false, true, false, false, true, true, false, true, true, false
            ]
        );
        assert_eq!(selected, 1);
    }

    /// #323: a dropdown opened near the bottom of a scroll area blocks only
    /// its visible options; a press on a widget under the clipped-away part
    /// reaches that widget and closes the popover.
    #[wasm_bindgen_test(unsupported = test)]
    fn clipped_dropdown_blocks_only_its_visible_options() {
        let (mut ui, mut text, _assets) = fixture();
        let mut list = DrawList::new();
        let area = Rect::new(0.0, 0.0, 200.0, 100.0);
        let dd = Rect::new(10.0, 40.0, 160.0, 30.0);
        let options = ["25%", "50%", "100%"];
        let mut selected = 0usize;
        let mut offset = 0.0;
        // Options occupy y ∈ [72, 162); the scroll area shows y < 100.
        let below = Rect::new(10.0, 120.0, 160.0, 30.0);
        let mut fired = Vec::new();
        for input in [
            press_at(50.0, 55.0),
            hover_at(0.0, 0.0, false),
            press_at(50.0, 135.0),
        ] {
            let mut f = ui.begin(&mut text, &mut list, input);
            fired.push(f.button("below", below, "B", 16.0));
            f.scroll_area_begin("list", area, 100.0, &mut offset);
            f.dropdown("zoom", dd, &options, &mut selected, 16.0);
            f.scroll_area_end(&mut offset);
            f.end();
        }
        assert_eq!(fired, [false, false, true]);
        assert_eq!(selected, 0);
        assert!(!ui.any_popup_open(), "the press outside the clip closes it");
    }

    /// #292: an open dropdown that stops being submitted closes, releasing
    /// its keyboard capture, so Tab and Enter reach another button.
    #[wasm_bindgen_test(unsupported = test)]
    fn removed_dropdown_releases_its_popup() {
        let (mut ui, mut text, _assets) = fixture();
        let dd = Rect::new(10.0, 10.0, 160.0, 30.0);
        let options = ["a", "b"];
        let mut selected = 0usize;
        let mut list = DrawList::new();
        let mut f = ui.begin(&mut text, &mut list, press_at(50.0, 25.0));
        f.dropdown("menu", dd, &options, &mut selected, 16.0);
        f.end();
        assert!(ui.any_popup_open());

        let mut fired = Vec::new();
        for keys in [vec![], vec![UiKey::Tab], vec![UiKey::Enter]] {
            let mut list = DrawList::new();
            let mut f = ui.begin(
                &mut text,
                &mut list,
                UiInput {
                    keys,
                    ..UiInput::default()
                },
            );
            fired.push(f.button("other", BTN, "OK", 16.0));
            f.end();
        }
        assert!(!ui.any_popup_open());
        assert_eq!(fired, [false, false, true]);
    }

    /// #434: an open dropdown scrolled fully out of its scroll area closes,
    /// releasing keyboard capture, so Tab and Enter reach another button.
    #[wasm_bindgen_test(unsupported = test)]
    fn dropdown_scrolled_out_of_view_releases_its_popup() {
        let (mut ui, mut text, _assets) = fixture();
        let area = Rect::new(0.0, 200.0, 200.0, 100.0);
        let options = ["a", "b"];
        let mut selected = 0usize;
        let mut offset = 0.0;
        // Wheel over the area beside the open popover (x 10..170), which
        // shields what lies under it.
        let wheel = UiInput {
            mouse_pos: Vec2::new(185.0, 250.0),
            scroll: Vec2::new(0.0, -5.0),
            ..UiInput::default()
        };
        let key = |key| UiInput {
            keys: vec![key],
            ..UiInput::default()
        };
        let mut fired = Vec::new();
        for (i, input) in [
            press_at(50.0, 225.0),
            wheel,
            UiInput::default(),
            UiInput::default(),
            key(UiKey::Tab),
            key(UiKey::Enter),
        ]
        .into_iter()
        .enumerate()
        {
            let mut list = DrawList::new();
            let mut f = ui.begin(&mut text, &mut list, input);
            fired.push(f.button("other", BTN, "OK", 16.0));
            f.scroll_area_begin("list", area, 1000.0, &mut offset);
            let dd = Rect::new(10.0, 210.0 - offset, 160.0, 30.0);
            f.dropdown("menu", dd, &options, &mut selected, 16.0);
            f.scroll_area_end(&mut offset);
            f.end();
            if i == 0 {
                assert!(ui.any_popup_open());
            }
            // The wheel moves the content at the end of its frame, so the
            // dropdown leaves the clip the frame after and closes at its end;
            // that frame began with the popup open and still reports capture.
            if i == 2 {
                assert!(!ui.any_popup_open());
            }
            if i == 3 {
                assert!(!ui.keyboard_captured());
            }
        }
        assert!(offset > 100.0, "the wheel scrolled the dropdown away");
        assert_eq!(fired, [false, false, false, false, false, true]);
    }

    /// A modal blocks every ordinary widget anywhere on screen the frame
    /// after it is shown, while its own buttons stay interactive.
    #[wasm_bindgen_test(unsupported = test)]
    fn modal_blocks_ordinary_widgets() {
        let (mut ui, mut text, _assets) = fixture();
        let mut list = DrawList::new();
        let screen = Rect::new(0.0, 0.0, 960.0, 540.0);

        // Frame 1: modal drawn → blocking arms for the next frame.
        let mut f = ui.begin(&mut text, &mut list, UiInput::default());
        assert_eq!(
            f.confirm_modal("exit", screen, "Exit?", "Sure?", "EXIT", "CANCEL", 24.0),
            None
        );
        f.end();

        // Frame 2: a press on an ordinary button far from the panel fires
        // nothing; the modal's own confirm button (center-left, y ≈ 310..354)
        // resolves Some(true).
        let mut f = ui.begin(&mut text, &mut list, press_at(50.0, 30.0));
        assert!(!f.button("b", BTN, "OK", 24.0), "modal must block widgets");
        assert_eq!(
            f.confirm_modal("exit", screen, "Exit?", "Sure?", "EXIT", "CANCEL", 24.0),
            None
        );
        f.end();
        let confirm_center = Vec2::new(480.0 - 75.0 - 20.0, 480.0 * 0.0 + 332.0);
        let mut f = ui.begin(
            &mut text,
            &mut list,
            press_at(confirm_center.x, confirm_center.y),
        );
        let result = f.confirm_modal("exit", screen, "Exit?", "Sure?", "EXIT", "CANCEL", 24.0);
        f.end();
        assert_eq!(result, Some(true));

        // The frame the modal resolved must NOT arm blocking: the very
        // next frame's press on an ordinary button fires (no one-frame
        // lockout after CONFIRM/CANCEL).
        let mut f = ui.begin(&mut text, &mut list, press_at(50.0, 30.0));
        assert!(
            f.button("b", BTN, "OK", 24.0),
            "widgets must be live on the first frame after resolution"
        );
        f.end();
    }

    /// An overlay panel blocks the ordinary widgets underneath on the
    /// following frame while its own widgets stay interactive, and focus
    /// can be dropped explicitly when the panel closes (R-17/#25).
    #[wasm_bindgen_test(unsupported = test)]
    fn overlay_panel_blocks_underneath_and_focus_clears() {
        let (mut ui, mut text, _assets) = fixture();
        let mut list = DrawList::new();
        let panel = Rect::new(100.0, 100.0, 300.0, 200.0);

        // Frame 1: the panel is drawn → blocking arms for the next frame.
        let mut f = ui.begin(&mut text, &mut list, UiInput::default());
        f.overlay_panel_begin(panel);
        f.button("panel.ok", Rect::new(120.0, 120.0, 100.0, 40.0), "OK", 16.0);
        f.overlay_panel_end();
        f.end();

        // Frame 2: a press inside the panel region fires the panel's own
        // button but never the ordinary widget underneath.
        let mut f = ui.begin(&mut text, &mut list, press_at(150.0, 140.0));
        let under = f.button("under", Rect::new(100.0, 100.0, 300.0, 200.0), "U", 16.0);
        assert!(!under, "ordinary widget under the panel must be blocked");
        f.overlay_panel_begin(panel);
        let ok = f.button("panel.ok", Rect::new(120.0, 120.0, 100.0, 40.0), "OK", 16.0);
        f.overlay_panel_end();
        f.end();
        assert!(ok, "the panel's own widget stays interactive");

        // A focused line-edit inside the panel releases focus explicitly
        // when the caller closes the panel.
        let field = Rect::new(120.0, 180.0, 200.0, 30.0);
        let mut buf = String::new();
        let mut f = ui.begin(&mut text, &mut list, press_at(150.0, 190.0));
        f.overlay_panel_begin(panel);
        f.line_edit("panel.name", field, &mut buf, 50, 16.0);
        f.overlay_panel_end();
        f.end();
        assert!(ui.has_focus());
        ui.clear_focus();
        assert!(!ui.has_focus());
    }

    /// #323: a clipped overlay panel blocks only its visible part, like a
    /// clipped dropdown popover.
    #[wasm_bindgen_test(unsupported = test)]
    fn clipped_overlay_panel_blocks_only_its_visible_part() {
        let (mut ui, mut text, _assets) = fixture();
        let panel = Rect::new(100.0, 100.0, 300.0, 200.0);
        let under = Rect::new(300.0, 100.0, 100.0, 200.0);
        let mut fired = Vec::new();
        for input in [UiInput::default(), press_at(350.0, 150.0)] {
            let mut list = DrawList::new();
            let mut f = ui.begin(&mut text, &mut list, input);
            fired.push(f.button("under", under, "U", 16.0));
            f.set_clip(Some(Rect::new(0.0, 0.0, 250.0, 540.0)));
            f.overlay_panel_begin(panel);
            f.overlay_panel_end();
            f.set_clip(None);
            f.end();
        }
        assert_eq!(fired, [false, true]);
    }

    /// Tooltips draw only while their anchor is hovered, land in the
    /// overlay z band, and never become input blockers.
    #[wasm_bindgen_test(unsupported = test)]
    fn tooltip_draws_on_hover_only_and_never_blocks() {
        let (mut ui, mut text, mut assets) = fixture();
        let screen = Rect::new(0.0, 0.0, 960.0, 540.0);
        let anchor = Rect::new(10.0, 10.0, 100.0, 30.0);

        // Not hovered: nothing is drawn.
        let mut list = DrawList::new();
        let mut f = ui.begin(&mut text, &mut list, hover_at(500.0, 300.0, false));
        f.tooltip(anchor, screen, "Collision overlay", 13.0);
        f.end();
        text.end_frame(&mut assets, &mut list);
        assert!(list.screen.is_empty(), "unhovered tooltip must not draw");

        // Hovered: backdrop + border + glyphs, all in the overlay band.
        let mut list = DrawList::new();
        let mut f = ui.begin(&mut text, &mut list, hover_at(50.0, 25.0, false));
        f.tooltip(anchor, screen, "Collision overlay", 13.0);
        f.end();
        text.end_frame(&mut assets, &mut list);
        assert!(!list.screen.is_empty(), "hovered tooltip draws");
        assert!(
            list.screen.iter().all(|i| i.z > 4_000.0),
            "tooltips live in the overlay z band"
        );

        // A tooltip never blocks: the frame after drawing one, a button
        // underneath its rect still fires.
        let mut list = DrawList::new();
        let mut f = ui.begin(&mut text, &mut list, press_at(50.0, 55.0));
        assert!(
            f.button("under", Rect::new(10.0, 40.0, 100.0, 30.0), "B", 13.0),
            "tooltips must not become input blockers"
        );
        f.end();
    }
}