u0ui 0.3.2

Carbon UI: IBM Carbon v11 visuals with a shadcn-style API for GPUI Kit
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
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
//! Menu: Carbon `Menu` (`.cds--menu`) with `MenuItem` (label, shortcut,
//! `kind="danger"`, nested submenus), `MenuItemDivider`, `MenuItemSelectable`
//! (checkbox), `MenuItemRadioGroup` and `MenuItemGroup`, plus a visible
//! heading ([`MenuItem::heading`]) and, on any row, a second line and a
//! leading status dot (u0's popovers, `.pv-pop`). The surface behind
//! DropdownMenu, ContextMenu and Menubar.
//!
//! - **Why not Kit's menu**: Kit's `PopupMenu` is an entity of `Action`s that
//!   paints its own paddings, radius, check column and hover, has no danger
//!   items or radio groups, and keeps its keyboard handling inside that
//!   entity, so its rendering cannot be made Carbon. The menu is drawn here;
//!   anchoring, window-edge snapping and the deferred top layer stay Base's
//!   (`Popup` / `Positioner`), and focus moves use [`super::overlay`].
//! - **Keyboard** (Carbon `Menu`): opening (pointer or keyboard) focuses the
//!   first enabled item, with its focus ring; ↓/↑ move (wrapping, skipping disabled
//!   items), Home/End, Enter/Space activate, → or Enter opens a submenu and
//!   enters it, ←/Escape leaves a submenu, Escape closes the menu (focus
//!   returns to the trigger), typing jumps to the next item that starts
//!   with the typed text (type-ahead, 500ms buffer).
//! - **Leaving** (Carbon `Menu`'s `onKeyDown` / `onBlur`): Tab and Shift+Tab
//!   close the menu and return focus to the element that had it when the
//!   menu opened (Carbon prevents the Tab, focuses that element and calls
//!   `onClose`). They need [`init`]'s bindings, which beat Base's
//!   window-level Tab. A press outside closes it too (focus stays where the
//!   press put it), and so does focus leaving the menu any other way.
//! - **Appearance** ([`MenuLook`], [`MenuGeometry`]): `layer-01` with the
//!   floating shadow, 0.25rem block padding, 10rem–18rem wide (12rem with
//!   icons or selectable items); items `text-secondary` body-compact-01,
//!   1rem inline padding, height per size (24/32/40/48) or `density-row-md`
//!   in compact density; hover `layer-hover` + `text-primary`; danger labels
//!   `text-error` at rest (icon and shortcut stay `text-secondary`); danger hover
//!   and focus `button-danger-primary` + `text-on-color`; disabled
//!   `text-disabled`; keyboard focus a 2px inset `focus` outline; dividers
//!   1px `border-subtle-00` with 0.25rem margins.
//! - **Motion**: item backgrounds `transition: background-color 70ms
//!   cubic-bezier(0.2, 0, 0.38, 0.9)`; the surface shows and hides at once
//!   (`.cds--menu { display: none }` → `.cds--menu--open`, no transition).
use super::overlay::{self, Callback, Translate, rem};
use crate::{
    density::{self, Density},
    icons::{CarbonIcon, Icon, IconSize},
    motion::{self, Curve, Motion, Speed},
    theme::color,
    tokens::{ColorRole, TypeStyle},
    typography::{Phrase, TypeStyled as _},
};
use gpui_kit::{
    Anchor, AnyElement, App, Bounds, CursorStyle, ElementId, Entity, FocusHandle, Hsla,
    InteractiveElement, IntoElement, KeyDownEvent, MouseButton, ParentElement, Pixels, RenderOnce,
    SharedString, StatefulInteractiveElement as _, Styled, Window, canvas, div, point,
    prelude::FluentBuilder as _, px,
};
use gpui_kit::{KeyBinding, TestSupportExt as _, actions};
use std::{
    cell::RefCell,
    collections::HashMap,
    rc::Rc,
    time::{Duration, Instant},
};

actions!(
    u0ui_menu,
    [
        /// Tab or Shift+Tab inside an open menu: close it (Carbon `Menu`).
        TabOut
    ]
);

/// The key context of an open menu's surface.
pub(crate) const KEY_CONTEXT: &str = "U0Menu";

/// Bind Tab and Shift+Tab inside menus to [`TabOut`]. A binding in the
/// menu's own key context is deeper than Base's window-level `root::Tab`,
/// so it wins: without it the root moves focus and the menu stays open.
pub(crate) fn init(cx: &mut App) {
    cx.bind_keys([
        KeyBinding::new("tab", TabOut, Some(KEY_CONTEXT)),
        KeyBinding::new("shift-tab", TabOut, Some(KEY_CONTEXT)),
    ]);
}

/// `.cds--menu-item { transition: background-color 70ms cubic-bezier(0.2, 0, 0.38, 0.9) }`.
pub const ITEM_MOTION: Motion = Motion::new(Curve::ProductiveStandard, Speed::Fast01);
/// How long typed characters accumulate for type-ahead.
pub const TYPEAHEAD: Duration = Duration::from_millis(500);

/// Menu sizes (`.cds--menu--xs/sm/md/lg`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum MenuSize {
    /// `xs`: 1.5rem items.
    Xs,
    /// `sm`: 2rem items (the default).
    #[default]
    Sm,
    /// `md`: 2.5rem items.
    Md,
    /// `lg`: 3rem items.
    Lg,
}

impl MenuSize {
    /// Every size.
    pub const ALL: [MenuSize; 4] = [Self::Xs, Self::Sm, Self::Md, Self::Lg];

    /// The Carbon name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Xs => "xs",
            Self::Sm => "sm",
            Self::Md => "md",
            Self::Lg => "lg",
        }
    }

    /// Item height in px under `density`: `.cds--menu--<size> .cds--menu-item
    /// { block-size }`, or `[data-density="compact"] .cds--menu-item { height:
    /// var(--density-row-md) }`.
    pub fn item_height(self, density: Density) -> f32 {
        match density {
            Density::Compact => crate::tokens::DENSITY_ROW_MD.px_value(),
            Density::Comfortable => match self {
                Self::Xs => 24.,
                Self::Sm => 32.,
                Self::Md => 40.,
                Self::Lg => 48.,
            },
        }
    }
}

/// Menu box geometry in px.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MenuGeometry {
    /// `padding: 0.25rem 0`.
    pub padding_block: f32,
    /// `.cds--menu { min-inline-size: 10rem }`; `.cds--menu--with-icons
    /// { min-inline-size: 12rem }` (selectable items alone keep 10rem).
    pub min_width: f32,
    /// `max-inline-size: 18rem`.
    pub max_width: f32,
    /// `.cds--menu-item { padding-inline: 1rem }`.
    pub item_padding: f32,
    /// `column-gap: 0.5rem`.
    pub column_gap: f32,
    /// `.cds--menu-item-divider { margin-block: 0.25rem }`.
    pub divider_margin: f32,
}

impl MenuGeometry {
    /// Geometry for a menu with icons (or not).
    pub const fn of(with_icons: bool) -> Self {
        Self {
            padding_block: 4.,
            min_width: if with_icons { 192. } else { 160. },
            max_width: 288.,
            item_padding: 16.,
            column_gap: 8.,
            divider_margin: 4.,
        }
    }
}

/// The interaction state of one item row.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ItemState {
    /// Pointer over it (or its submenu is open).
    pub hovered: bool,
    /// Keyboard focus is on it.
    pub focused: bool,
    /// `kind="danger"`.
    pub danger: bool,
    /// Disabled.
    pub disabled: bool,
}

/// The colors of a menu and its items.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MenuLook {
    /// `.cds--menu { background-color: var(--layer) }`.
    pub background: Hsla,
    /// `.cds--menu-item-divider { background-color: var(--border-subtle) }`;
    /// menus portal to the document root, where `--border-subtle` is
    /// `border-subtle-00` (`:root { --border-subtle: var(--border-subtle-00) }`).
    pub divider: Hsla,
}

impl MenuLook {
    /// The look on the active theme.
    pub fn current() -> Self {
        Self {
            background: color(ColorRole::Layer01),
            divider: color(ColorRole::BorderSubtle00),
        }
    }

    /// `(background, text, focus outline)` of an item in `state`.
    pub fn item(state: ItemState) -> (Hsla, Hsla, Option<Hsla>) {
        use ColorRole::*;
        let clear = Hsla::transparent_black();
        if state.disabled {
            // `.cds--menu-item--disabled:hover { background-color: var(--layer); color: text-disabled }`.
            return (clear, color(TextDisabled), None);
        }
        let outline = state.focused.then(|| color(Focus));
        if state.danger && (state.hovered || state.focused) {
            // `.cds--menu-item--danger:hover, :focus`.
            return (color(ButtonDangerPrimary), color(TextOnColor), outline);
        }
        if state.hovered {
            // `.cds--menu-item:hover { background-color: var(--layer-hover); color: text-primary }`.
            return (color(LayerHover01), color(TextPrimary), outline);
        }
        // `.cds--menu-item { color: var(--text-secondary) }`; a danger item's
        // label is red at rest ([`Self::label`]).
        (clear, color(TextSecondary), outline)
    }

    /// The label color of an item in `state`: the item color, except a
    /// danger item at rest, whose label is `text-error` (`t3.css` app-r3 "the
    /// dangerous git menu item reads as dangerous before hover", also in
    /// `bundle.css`: `.cds--menu-item--danger:not(.cds--menu-item--disabled)
    /// .cds--menu-item__label { color: var(--text-error) }`, and `:hover`,
    /// `:focus` labels `text-on-color`). The rule names `text-error`, not
    /// `support-error`, in every theme (G90/G100 `#ffb3b8`/`#ff8389`); no later
    /// u0/t3 rule overrides it. Only the label: the icon and shortcut keep the
    /// item color.
    pub fn label(state: ItemState) -> Hsla {
        let (_, text, _) = Self::item(state);
        if state.danger && !state.disabled && !state.hovered && !state.focused {
            color(ColorRole::TextError)
        } else {
            text
        }
    }

    /// The colour of an item's second line in `state` (`.pv-mi small {
    /// color: text-secondary }`): `text-disabled` when disabled, and the
    /// item's own colour on a danger item's hover or focus fill, where
    /// `text-secondary` would not read.
    pub fn detail(state: ItemState) -> Hsla {
        let (_, text, _) = Self::item(state);
        if state.disabled || (state.danger && (state.hovered || state.focused)) {
            text
        } else {
            color(ColorRole::TextSecondary)
        }
    }
}

type Toggle = Rc<dyn Fn(bool, &mut Window, &mut App)>;
type Pick = Rc<dyn Fn(usize, &mut Window, &mut App)>;
/// A handler for keys a menu does not use; returns true when handled.
pub(crate) type KeyHook = Rc<dyn Fn(&str, &mut Window, &mut App) -> bool>;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Kind {
    Item,
    Divider,
    Checkbox,
    Radio,
    Group,
    Heading,
}

/// One entry of a menu. Build with [`MenuItem::item`], [`MenuItem::divider`],
/// [`MenuItem::checkbox`], [`MenuItem::radio_group`] or [`MenuItem::group`].
#[derive(Clone)]
pub struct MenuItem {
    kind: Kind,
    label: SharedString,
    shortcut: Option<SharedString>,
    icon: Option<Icon>,
    disabled: bool,
    danger: bool,
    on_select: Option<Callback>,
    children: Vec<MenuItem>,
    selected: Option<bool>,
    default_selected: bool,
    on_toggle: Option<Toggle>,
    options: Vec<SharedString>,
    picked: Option<Option<usize>>,
    default_picked: Option<usize>,
    on_pick: Option<Pick>,
    detail: Option<Phrase>,
    dot: Option<ColorRole>,
    option_details: Vec<Phrase>,
    option_dots: Vec<Option<ColorRole>>,
}

impl MenuItem {
    fn base(kind: Kind, label: SharedString) -> Self {
        Self {
            kind,
            label,
            shortcut: None,
            icon: None,
            disabled: false,
            danger: false,
            on_select: None,
            children: Vec::new(),
            selected: None,
            default_selected: false,
            on_toggle: None,
            options: Vec::new(),
            picked: None,
            default_picked: None,
            on_pick: None,
            detail: None,
            dot: None,
            option_details: Vec::new(),
            option_dots: Vec::new(),
        }
    }

    /// `MenuItem`: an action (or, with [`Self::submenu`], a submenu).
    pub fn item(label: impl Into<SharedString>) -> Self {
        Self::base(Kind::Item, label.into())
    }

    /// `MenuItemDivider`.
    pub fn divider() -> Self {
        Self::base(Kind::Divider, SharedString::default())
    }

    /// `MenuItemSelectable`: a checkbox item.
    pub fn checkbox(label: impl Into<SharedString>) -> Self {
        Self::base(Kind::Checkbox, label.into())
    }

    /// `MenuItemRadioGroup`: one item per option, one selected (`label` is
    /// the group's accessible name).
    pub fn radio_group<S: Into<SharedString>>(
        label: impl Into<SharedString>,
        options: impl IntoIterator<Item = S>,
    ) -> Self {
        let mut item = Self::base(Kind::Radio, label.into());
        item.options = options.into_iter().map(Into::into).collect();
        item
    }

    /// `MenuItemGroup`: items under an accessible group name.
    pub fn group(
        label: impl Into<SharedString>,
        items: impl IntoIterator<Item = MenuItem>,
    ) -> Self {
        let mut item = Self::base(Kind::Group, label.into());
        item.children = items.into_iter().collect();
        item
    }

    /// A heading over the items after it, shown, not chosen (`.pv-pop-h`:
    /// the menu's name above its items); the keyboard passes over it.
    pub fn heading(label: impl Into<SharedString>) -> Self {
        Self::base(Kind::Heading, label.into())
    }

    /// Trailing text (`.cds--menu-item__shortcut`): a keyboard shortcut, or
    /// on a submenu what is chosen in it, before its caret (`.pv-mi small`:
    /// `Viewport  Responsive ›`).
    pub fn shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
        self.shortcut = Some(shortcut.into());
        self
    }

    /// Leading icon (`.cds--menu-item__icon`).
    pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// A second line under the label (`.pv-mi small`: 12/16
    /// `text-secondary`, wrapping), its code in mono.
    pub fn detail(mut self, detail: impl Into<Phrase>) -> Self {
        self.detail = Some(detail.into());
        self
    }

    /// A leading 8px status dot in `role` (`.pv-mi .pv-dot`).
    pub fn dot(mut self, role: ColorRole) -> Self {
        self.dot = Some(role);
        self
    }

    /// A radio group's second lines, one per option in order ([`Self::detail`]).
    pub fn option_details<P: Into<Phrase>>(mut self, details: impl IntoIterator<Item = P>) -> Self {
        self.option_details = details.into_iter().map(Into::into).collect();
        self
    }

    /// A radio group's leading dots, one per option in order (`None` for
    /// an option without one; [`Self::dot`]).
    pub fn option_dots(mut self, dots: impl IntoIterator<Item = Option<ColorRole>>) -> Self {
        self.option_dots = dots.into_iter().collect();
        self
    }

    /// Disable the item.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// `kind="danger"`.
    pub fn danger(mut self, danger: bool) -> Self {
        self.danger = danger;
        self
    }

    /// Activation handler (the menu closes afterwards).
    pub fn on_select(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_select = Some(Rc::new(f));
        self
    }

    /// Nested items: this item opens a submenu.
    pub fn submenu(mut self, items: impl IntoIterator<Item = MenuItem>) -> Self {
        self.children = items.into_iter().collect();
        self
    }

    /// Controlled checked state of a checkbox item.
    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = Some(selected);
        self
    }

    /// Initial checked state of an uncontrolled checkbox item (`defaultSelected`).
    pub fn default_selected(mut self, selected: bool) -> Self {
        self.default_selected = selected;
        self
    }

    /// Checkbox change handler.
    pub fn on_change(mut self, f: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
        self.on_toggle = Some(Rc::new(f));
        self
    }

    /// Controlled selected option of a radio group.
    pub fn selected_item(mut self, index: Option<usize>) -> Self {
        self.picked = Some(index);
        self
    }

    /// Initial selected option of an uncontrolled radio group (`defaultSelectedItem`).
    pub fn default_selected_item(mut self, index: usize) -> Self {
        self.default_picked = Some(index);
        self
    }

    /// Radio group change handler.
    pub fn on_select_item(mut self, f: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
        self.on_pick = Some(Rc::new(f));
        self
    }
}

/// What a row does when activated.
#[derive(Clone)]
enum Action {
    Select(Option<Callback>),
    Submenu(Vec<MenuItem>),
    Toggle {
        key: String,
        checked: bool,
        controlled: bool,
        on_change: Option<Toggle>,
    },
    Pick {
        key: String,
        index: usize,
        controlled: bool,
        on_change: Option<Pick>,
    },
}

/// One rendered line of a menu (built per frame; size is irrelevant).
#[derive(Clone)]
#[allow(clippy::large_enum_variant)]
enum Row {
    Divider,
    Heading(SharedString),
    Entry {
        label: SharedString,
        detail: Option<Phrase>,
        dot: Option<ColorRole>,
        shortcut: Option<SharedString>,
        icon: Option<Icon>,
        disabled: bool,
        danger: bool,
        checked: Option<bool>,
        action: Action,
    },
}

impl Row {
    fn enabled(&self) -> bool {
        matches!(
            self,
            Row::Entry {
                disabled: false,
                ..
            }
        )
    }

    fn label(&self) -> &str {
        match self {
            Row::Entry { label, .. } => label,
            Row::Divider | Row::Heading(_) => "",
        }
    }
}

/// Uncontrolled selections, keyed by item path.
pub(crate) type Selections = Entity<HashMap<String, usize>>;

fn flatten(
    items: &[MenuItem],
    prefix: &str,
    selections: &HashMap<String, usize>,
    out: &mut Vec<Row>,
) {
    for (i, item) in items.iter().enumerate() {
        let key = format!("{prefix}{i}");
        match item.kind {
            Kind::Divider => out.push(Row::Divider),
            Kind::Heading => out.push(Row::Heading(item.label.clone())),
            Kind::Group => flatten(&item.children, &format!("{key}."), selections, out),
            Kind::Item => out.push(Row::Entry {
                label: item.label.clone(),
                detail: item.detail.clone(),
                dot: item.dot,
                shortcut: item.shortcut.clone(),
                icon: item.icon.clone(),
                disabled: item.disabled,
                danger: item.danger,
                checked: None,
                action: if item.children.is_empty() {
                    Action::Select(item.on_select.clone())
                } else {
                    Action::Submenu(item.children.clone())
                },
            }),
            Kind::Checkbox => {
                let checked = item.selected.unwrap_or_else(|| {
                    selections
                        .get(&key)
                        .map_or(item.default_selected, |v| *v == 1)
                });
                out.push(Row::Entry {
                    label: item.label.clone(),
                    detail: item.detail.clone(),
                    dot: item.dot,
                    shortcut: item.shortcut.clone(),
                    icon: item.icon.clone(),
                    disabled: item.disabled,
                    danger: false,
                    checked: Some(checked),
                    action: Action::Toggle {
                        key,
                        checked,
                        controlled: item.selected.is_some(),
                        on_change: item.on_toggle.clone(),
                    },
                })
            }
            Kind::Radio => {
                let picked = item
                    .picked
                    .unwrap_or_else(|| selections.get(&key).copied().or(item.default_picked));
                for (index, option) in item.options.iter().enumerate() {
                    out.push(Row::Entry {
                        label: option.clone(),
                        detail: item.option_details.get(index).cloned(),
                        dot: item.option_dots.get(index).copied().flatten(),
                        shortcut: None,
                        icon: None,
                        disabled: item.disabled,
                        danger: false,
                        checked: Some(picked == Some(index)),
                        action: Action::Pick {
                            key: key.clone(),
                            index,
                            controlled: item.picked.is_some(),
                            on_change: item.on_pick.clone(),
                        },
                    });
                }
            }
        }
    }
}

/// Keyboard and pointer state of one menu level.
#[derive(Default)]
struct LevelState {
    /// Keyboard-focused row.
    active: Option<usize>,
    /// Pointer-hovered row.
    hovered: Option<usize>,
    /// Row whose submenu is open.
    submenu: Option<usize>,
    /// Whether the open submenu should take keyboard focus.
    submenu_focus: bool,
    /// Whether the open submenu should highlight its first item; the
    /// submenu clears it once it has (its popup may first paint a frame
    /// later than the request).
    submenu_first: bool,
    typed: String,
    typed_at: Option<Instant>,
}

/// Shared by every level of one open menu: closes the whole chain, holds
/// uncontrolled selections, and collects submenu bounds so a press inside a
/// submenu is not a press outside the root.
#[derive(Clone)]
pub(crate) struct Chain {
    pub close: Callback,
    pub selections: Selections,
    pub bounds: Rc<RefCell<Vec<Bounds<Pixels>>>>,
}

/// The next enabled row after `from` in `direction` (wrapping).
fn step(rows: &[Row], from: Option<usize>, direction: isize) -> Option<usize> {
    let n = rows.len() as isize;
    if n == 0 {
        return None;
    }
    let mut i = match from {
        Some(i) => i as isize,
        None if direction > 0 => -1,
        None => n,
    };
    for _ in 0..n {
        i = (i + direction).rem_euclid(n);
        if rows[i as usize].enabled() {
            return Some(i as usize);
        }
    }
    None
}

/// The row a menu focuses when it opens. Carbon `Menu` focuses its first
/// enabled item (`focusItem()` on open, from the pointer or the keyboard).
/// A menu that is only one radio group is a select (the Pagination pickers):
/// it starts on the picked option, as a native `<select>` does.
fn initial_row(rows: &[Row]) -> Option<usize> {
    // A heading over the group does not make it less of a select.
    let only_picks = rows
        .iter()
        .filter(|r| !matches!(r, Row::Heading(_)))
        .all(|r| {
            matches!(
                r,
                Row::Entry {
                    action: Action::Pick { .. },
                    ..
                }
            )
        });
    let picked = only_picks
        .then(|| {
            rows.iter().position(|r| {
                r.enabled()
                    && matches!(
                        r,
                        Row::Entry {
                            checked: Some(true),
                            ..
                        }
                    )
            })
        })
        .flatten();
    picked.or_else(|| step(rows, None, 1))
}

/// The next enabled row (after `from`) whose label starts with `typed`.
fn find_typed(rows: &[Row], from: Option<usize>, typed: &str) -> Option<usize> {
    let typed = typed.to_lowercase();
    let n = rows.len();
    let start = from.map_or(0, |i| if typed.chars().count() > 1 { i } else { i + 1 });
    (0..n)
        .map(|k| (start + k) % n)
        .find(|&i| rows[i].enabled() && rows[i].label().to_lowercase().starts_with(&typed))
}

/// A Carbon menu surface (one level). Usually created by DropdownMenu,
/// ContextMenu or Menubar, which own placement and open state.
#[derive(IntoElement)]
pub(crate) struct Menu {
    id: ElementId,
    items: Vec<MenuItem>,
    size: MenuSize,
    label: SharedString,
    focus: FocusHandle,
    chain: Chain,
    parent: Option<(FocusHandle, Callback)>,
    min_width: Option<f32>,
    path: String,
    on_key: Option<KeyHook>,
    /// A submenu opened from the keyboard highlights its first enabled item
    /// (Carbon focuses it), then clears the request on its parent level.
    focus_first: Option<Entity<LevelState>>,
    /// Set when the root menu opens and cleared by the first render of the
    /// menu itself (which may come frames later: an anchored menu waits to be
    /// measured): start over on [`initial_row`].
    opening: Option<Entity<bool>>,
}

impl Menu {
    pub fn new(
        id: impl Into<ElementId>,
        items: Vec<MenuItem>,
        focus: FocusHandle,
        chain: Chain,
    ) -> Self {
        Self {
            id: id.into(),
            items,
            size: MenuSize::Sm,
            label: SharedString::default(),
            focus,
            chain,
            parent: None,
            min_width: None,
            path: String::new(),
            on_key: None,
            focus_first: None,
            opening: None,
        }
    }

    pub fn size(mut self, size: MenuSize) -> Self {
        self.size = size;
        self
    }

    pub fn label(mut self, label: SharedString) -> Self {
        self.label = label;
        self
    }

    pub fn min_width(mut self, width: Option<f32>) -> Self {
        self.min_width = width;
        self
    }
}

fn activate(
    row: &Row,
    index: usize,
    state: &Entity<LevelState>,
    chain: &Chain,
    from_keyboard: bool,
    window: &mut Window,
    cx: &mut App,
) {
    let Row::Entry {
        action, disabled, ..
    } = row
    else {
        return;
    };
    if *disabled {
        return;
    }
    match action {
        Action::Submenu(_) => state.update(cx, |s, cx| {
            s.submenu = Some(index);
            s.submenu_focus = from_keyboard;
            s.submenu_first = from_keyboard;
            cx.notify();
        }),
        Action::Select(on_select) => {
            if let Some(f) = on_select {
                f(window, cx);
            }
            (chain.close)(window, cx);
        }
        Action::Toggle {
            key,
            checked,
            controlled,
            on_change,
        } => {
            if !controlled {
                chain.selections.update(cx, |map, cx| {
                    map.insert(key.clone(), usize::from(!checked));
                    cx.notify();
                });
            }
            if let Some(f) = on_change {
                f(!checked, window, cx);
            }
            (chain.close)(window, cx);
        }
        Action::Pick {
            key,
            index,
            controlled,
            on_change,
        } => {
            if !controlled {
                chain.selections.update(cx, |map, cx| {
                    map.insert(key.clone(), *index);
                    cx.notify();
                });
            }
            if let Some(f) = on_change {
                f(*index, window, cx);
            }
            (chain.close)(window, cx);
        }
    }
}

impl RenderOnce for Menu {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let id = self.id.clone();
        let rows = {
            let mut rows = Vec::new();
            flatten(
                &self.items,
                &self.path,
                self.chain.selections.read(cx),
                &mut rows,
            );
            Rc::new(rows)
        };
        let state = window.use_keyed_state((id.clone(), "level"), cx, |_, _| LevelState::default());
        let opening = self.opening.as_ref().is_some_and(|o| *o.read(cx));
        if let Some(flag) = &self.opening
            && opening
        {
            flag.update(cx, |v, _| *v = false);
        }
        if opening {
            // A fresh open forgets the last one (highlight, open submenu,
            // type-ahead) and focuses the first item, keyboard ring included.
            let first = initial_row(&rows);
            state.update(cx, |s, _| {
                *s = LevelState {
                    active: first,
                    ..LevelState::default()
                }
            });
        }
        if let Some(parent) = &self.focus_first {
            let first = step(&rows, None, 1);
            state.update(cx, |s, _| {
                s.active = first;
                s.hovered = None;
            });
            parent.update(cx, |s, _| s.submenu_first = false);
        }
        let sub_focus = window
            .use_keyed_state((id.clone(), "sub-focus"), cx, |_, cx| cx.focus_handle())
            .read(cx)
            .clone();
        let density = density::current();
        let height = self.size.item_height(density);
        let with_icons = rows
            .iter()
            .any(|r| matches!(r, Row::Entry { icon: Some(_), .. }));
        // GPUI sizes a box of wrapping text to its narrowest, not to CSS's
        // max-content: a menu with second lines takes its full width (u0's
        // popovers are fixed-width, `.pv-pop { width: 336px }`) so they
        // wrap only where they must.
        let with_details = rows.iter().any(|r| {
            matches!(
                r,
                Row::Entry {
                    detail: Some(_),
                    ..
                }
            )
        });
        let with_selection = rows.iter().any(|r| {
            matches!(
                r,
                Row::Entry {
                    checked: Some(_),
                    ..
                }
            )
        });
        let geometry = MenuGeometry::of(with_icons);
        let look = MenuLook::current();
        let (active, hovered, submenu, submenu_focus) = {
            let s = state.read(cx);
            (s.active, s.hovered, s.submenu, s.submenu_focus)
        };
        let keyboard_focus = self.focus.is_focused(window);

        let on_key = {
            let rows = rows.clone();
            let state = state.clone();
            let chain = self.chain.clone();
            let parent = self.parent.clone();
            let extra = self.on_key.clone();
            move |event: &KeyDownEvent, window: &mut Window, cx: &mut App| {
                let key = event.keystroke.key.as_str();
                let current = state.read(cx).active.or(state.read(cx).hovered);
                let set_active = |index: Option<usize>, cx: &mut App| {
                    state.update(cx, |s, cx| {
                        s.active = index;
                        s.hovered = None;
                        cx.notify();
                    })
                };
                match key {
                    "down" => set_active(step(&rows, current, 1), cx),
                    "up" => set_active(step(&rows, current, -1), cx),
                    "home" => set_active(step(&rows, None, 1), cx),
                    "end" => set_active(step(&rows, None, -1), cx),
                    "enter" | "space" => {
                        if let Some(i) = current {
                            activate(&rows[i], i, &state, &chain, true, window, cx);
                        }
                    }
                    "right" => {
                        if let Some(i) = current
                            && matches!(
                                &rows[i],
                                Row::Entry {
                                    action: Action::Submenu(_),
                                    disabled: false,
                                    ..
                                }
                            )
                        {
                            activate(&rows[i], i, &state, &chain, true, window, cx);
                        } else if let Some(extra) = &extra {
                            extra(key, window, cx);
                        }
                    }
                    "left" if parent.is_some() => {
                        if let Some((_, close)) = &parent {
                            close(window, cx);
                        }
                    }
                    "escape" => match &parent {
                        Some((_, close)) => close(window, cx),
                        None => (chain.close)(window, cx),
                    },
                    // Reached only without [`init`]'s bindings.
                    "tab" => (chain.close)(window, cx),
                    _ => {
                        if let Some(extra) = &extra
                            && extra(key, window, cx)
                        {
                            cx.stop_propagation();
                            return;
                        }
                        let Some(ch) = event.keystroke.key_char.as_deref() else {
                            return;
                        };
                        if ch.chars().any(|c| c.is_control()) || ch.trim().is_empty() {
                            return;
                        }
                        let now = Instant::now();
                        let typed = state.update(cx, |s, _| {
                            if s.typed_at.is_none_or(|t| now.duration_since(t) > TYPEAHEAD) {
                                s.typed.clear();
                            }
                            s.typed.push_str(ch);
                            s.typed_at = Some(now);
                            s.typed.clone()
                        });
                        if let Some(i) = find_typed(&rows, current, &typed) {
                            set_active(Some(i), cx);
                        }
                    }
                }
                cx.stop_propagation();
            }
        };

        let mut children: Vec<AnyElement> = Vec::new();
        for (index, row) in rows.iter().enumerate() {
            if let Row::Heading(text) = row {
                children.push(heading(text.clone(), &geometry));
                continue;
            }
            let Row::Entry {
                label,
                detail,
                dot,
                shortcut,
                icon,
                disabled,
                danger,
                checked,
                action,
            } = row
            else {
                children.push(
                    div()
                        .my(rem(geometry.divider_margin))
                        .h(px(1.))
                        .w_full()
                        .bg(look.divider)
                        .into_any_element(),
                );
                continue;
            };
            let is_submenu = matches!(action, Action::Submenu(_));
            let item_state = ItemState {
                hovered: hovered == Some(index) || submenu == Some(index),
                focused: keyboard_focus && active == Some(index),
                danger: *danger,
                disabled: *disabled,
            };
            let (bg, text, outline) = MenuLook::item(item_state);
            let label_color = MenuLook::label(item_state);
            let row_id = ElementId::NamedChild(
                std::sync::Arc::new(id.clone()),
                SharedString::from(format!("row-{index}")),
            );
            let bg = motion::animate_color((row_id.clone(), "bg"), bg, ITEM_MOTION, window, cx);
            let icon_color = text;
            // A row with a second line grows (`.pv-mi { align-items:
            // flex-start; min-height: 32px; padding: 6px 12px }`), its
            // leading and trailing parts level with its first line.
            let two_lines = detail.is_some();
            let cell = || {
                div()
                    .flex_shrink_0()
                    .flex()
                    .items_center()
                    .when(two_lines, |this| this.h(rem(FIRST_LINE)))
            };
            let mut line = div()
                .id(row_id.clone())
                .relative()
                .when(!two_lines, |this| this.h(rem(height)).items_center())
                .when(two_lines, |this| {
                    this.min_h(rem(height))
                        .py(rem(DETAIL_PADDING_BLOCK))
                        .items_start()
                })
                .flex_shrink_0()
                .px(rem(geometry.item_padding))
                .flex()
                .flex_row()
                .gap(rem(geometry.column_gap))
                .bg(bg)
                .text_color(text)
                .type_style(TypeStyle::BodyCompact01)
                .cursor(if *disabled {
                    CursorStyle::OperationNotAllowed
                } else {
                    CursorStyle::PointingHand
                });
            if with_selection {
                line = line.child(cell().child(div().size(rem(16.)).when(
                    checked == &Some(true),
                    |this| {
                        this.child(
                            Icon::carbon(CarbonIcon::Checkmark)
                                .size(IconSize::S16)
                                .color(icon_color),
                        )
                    },
                )));
            }
            if with_icons {
                line = line.child(cell().child(
                    div().size(rem(16.)).when_some(icon.clone(), |this, icon| {
                        this.child(icon.size(IconSize::S16).color(icon_color))
                    }),
                ));
            }
            if let Some(role) = dot {
                // `.pv-dot { width: 8px; height: 8px; border-radius: 50%; flex: none }`.
                line =
                    line.child(cell().child(div().size(rem(DOT)).rounded_full().bg(color(*role))));
            }
            let name = div()
                .overflow_hidden()
                .whitespace_nowrap()
                .text_ellipsis()
                .text_color(label_color)
                .child(label.clone());
            line = line
                .child(match detail {
                    None => name.flex_1().min_w_0(),
                    // `.pv-mi > span:last-child { flex: 1; min-width: 0; display: flex;
                    // flex-direction: column }`, `small { font-size: 12px;
                    // line-height: 16px; color: text-secondary }`, `code { font-family: mono }`.
                    Some(detail) => div()
                        .flex_1()
                        .min_w_0()
                        .flex()
                        .flex_col()
                        .child(name)
                        .child(
                            div()
                                .type_style(TypeStyle::Label01)
                                .child(detail.styled(400, MenuLook::detail(item_state))),
                        ),
                })
                // The trailing `max-content` column (`grid-template-columns:
                // … 1fr max-content; column-gap: 0.5rem`) is there even when
                // empty, so its 8px gap always counts toward the menu's
                // width: the shortcut, or a submenu's caret.
                // A submenu's text (what is chosen in it) stands before its
                // caret, a column gap apart (`.pv-mi { gap: 8px }`).
                .child(
                    cell()
                        .gap(rem(geometry.column_gap))
                        .children(shortcut.clone())
                        .when(is_submenu, |this| {
                            this.child(
                                Icon::carbon(CarbonIcon::CaretRight)
                                    .size(IconSize::S16)
                                    .color(icon_color),
                            )
                        }),
                )
                .when_some(outline, |this, outline| {
                    // `.cds--menu-item:focus { outline: 2px solid var(--focus); outline-offset: -2px }`;
                    // danger adds `box-shadow: inset 0 0 0 0.1875rem var(--background)`.
                    this.child(
                        div()
                            .absolute()
                            .top_0()
                            .left_0()
                            .size_full()
                            .border_2()
                            .border_color(outline)
                            .when(*danger, |this| {
                                this.child(
                                    div()
                                        .size_full()
                                        .border_1()
                                        .border_color(color(ColorRole::Background)),
                                )
                            }),
                    )
                });
            if !*disabled {
                let state_hover = state.clone();
                let state_click = state.clone();
                let chain = self.chain.clone();
                let row_owned = row.clone();
                line = line
                    .on_hover(move |hovering, _, cx| {
                        state_hover.update(cx, |s, cx| {
                            if *hovering {
                                s.hovered = Some(index);
                                s.active = None;
                                // Pointing at another item closes an open submenu;
                                // pointing at a submenu item opens it.
                                s.submenu = is_submenu.then_some(index);
                                s.submenu_focus = false;
                                s.submenu_first = false;
                            } else if s.hovered == Some(index) {
                                s.hovered = None;
                            }
                            cx.notify();
                        })
                    })
                    .on_click(move |_, window, cx| {
                        activate(&row_owned, index, &state_click, &chain, false, window, cx)
                    });
            }
            let line = line.into_any_element();
            // The submenu hangs off its item's right edge, top-aligned with it
            // (`Menu` positions submenus at `[left, right]` of the item).
            if submenu == Some(index)
                && let Action::Submenu(items) = action
            {
                let close_sub = {
                    let state = state.clone();
                    let focus = self.focus.clone();
                    Rc::new(move |window: &mut Window, cx: &mut App| {
                        state.update(cx, |s, cx| {
                            s.submenu = None;
                            s.active = Some(index);
                            cx.notify();
                        });
                        focus.focus(window, cx);
                    }) as Callback
                };
                if submenu_focus {
                    // Opened from the keyboard: the submenu takes focus once.
                    state.update(cx, |s, _| s.submenu_focus = false);
                    let sub_focus = sub_focus.clone();
                    window.defer(cx, move |window, cx| sub_focus.focus(window, cx));
                }
                let mut sub = Menu::new(
                    (row_id.clone(), "sub"),
                    items.clone(),
                    sub_focus.clone(),
                    self.chain.clone(),
                )
                .size(self.size)
                .label(label.clone());
                sub.parent = Some((self.focus.clone(), close_sub));
                sub.focus_first = state.read(cx).submenu_first.then(|| state.clone());
                sub.path = format!("{}{index}.", self.path);
                let bounds = self.chain.bounds.clone();
                let sub = div()
                    .child(sub)
                    .child(
                        canvas(move |b, _, _| bounds.borrow_mut().push(b), |_, _, _, _| {})
                            .absolute()
                            .top_0()
                            .left_0()
                            .size_full(),
                    )
                    .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation());
                children.push(
                    gpui_kit::base::Popup::new((row_id, "popup"), line)
                        .anchor(Anchor::LeftCenter)
                        .content(Translate::new(
                            sub,
                            point(0., 0.5),
                            point(px(0.), px(-height / 2. - geometry.padding_block)),
                        ))
                        .into_any_element(),
                );
            } else {
                children.push(line);
            }
        }

        let tab_out = {
            let chain = self.chain.clone();
            move |_: &TabOut, window: &mut Window, cx: &mut App| {
                (chain.close)(window, cx);
                cx.stop_propagation();
            }
        };
        div()
            .id(id.clone())
            .test_support()
            .key_context(KEY_CONTEXT)
            .track_focus(&self.focus)
            .on_key_down(on_key)
            .on_action(tab_out)
            .flex()
            .flex_col()
            .py(rem(geometry.padding_block))
            .min_w(rem(self.min_width.unwrap_or(0.).max(geometry.min_width)))
            .max_w(rem(geometry.max_width.max(self.min_width.unwrap_or(0.))))
            .when(with_details, |this| {
                this.w(rem(geometry.max_width.max(self.min_width.unwrap_or(0.))))
            })
            .bg(look.background)
            .shadow(overlay::floating_shadow())
            .occlude()
            .children(children)
    }
}

/// `body-compact-01`'s 18px line: a two-line row's first line, which its
/// leading and trailing parts sit level with.
const FIRST_LINE: f32 = 18.;
/// `.pv-mi { padding: 6px 12px }`: a two-line row's block padding.
pub const DETAIL_PADDING_BLOCK: f32 = 6.;
/// `.pv-dot { width: 8px; height: 8px }`.
pub const DOT: f32 = 8.;
/// `.pv-pop-h { padding: 8px 12px 4px }`: a heading's top and bottom
/// padding; its inline padding is the items' own, so it lines up with them.
pub const HEADING_PADDING: (f32, f32) = (8., 4.);

/// A heading row (`.pv-pop-h { font-size: 12px; line-height: 16px; color:
/// text-secondary }`).
fn heading(text: SharedString, geometry: &MenuGeometry) -> AnyElement {
    div()
        .flex_shrink_0()
        .px(rem(geometry.item_padding))
        .pt(rem(HEADING_PADDING.0))
        .pb(rem(HEADING_PADDING.1))
        .type_style(TypeStyle::Label01)
        .text_color(color(ColorRole::TextSecondary))
        .overflow_hidden()
        .whitespace_nowrap()
        .text_ellipsis()
        .child(text)
        .into_any_element()
}

/// What every open menu needs, whatever places it.
pub(crate) struct MenuSpec {
    pub items: Vec<MenuItem>,
    pub size: MenuSize,
    pub label: SharedString,
    pub open: bool,
    pub set_open: overlay::OpenChange,
    pub min_width: Option<f32>,
    pub on_unhandled_key: Option<KeyHook>,
}

/// The menu surface for `spec` (closing on a press outside it and its
/// submenus), or `None` while closed. Focus moves into it on open
/// and back on close.
pub(crate) fn menu_surface(
    id: &ElementId,
    spec: MenuSpec,
    window: &mut Window,
    cx: &mut App,
) -> Option<AnyElement> {
    menu_surface_returning(id, spec, None, window, cx)
}

/// [`menu_surface`] that returns focus to `return_to` on close (when given)
/// instead of to whatever had focus when it opened.
pub(crate) fn menu_surface_returning(
    id: &ElementId,
    spec: MenuSpec,
    return_to: Option<FocusHandle>,
    window: &mut Window,
    cx: &mut App,
) -> Option<AnyElement> {
    let open = spec.open;
    let focus = overlay::surface_focus_returning(id, open, None, return_to, window, cx);
    let selections = window.use_keyed_state((id.clone(), "selections"), cx, |_, _| HashMap::new());
    let bounds = window
        .use_keyed_state((id.clone(), "sub-bounds"), cx, |_, _| {
            Rc::new(RefCell::new(Vec::<Bounds<Pixels>>::new()))
        })
        .read(cx)
        .clone();
    let was_open = window.use_keyed_state((id.clone(), "was-open"), cx, |_, _| false);
    let opening = open && !*was_open.read(cx);
    if open != *was_open.read(cx) {
        was_open.update(cx, |v, _| *v = open);
    }
    // Whether focus has been inside the open menu (it moves in a frame
    // after the open); once it has, focus leaving closes the menu (Carbon
    // `Menu` `onBlur`). Submenus count as inside: they are its children.
    let had_focus = window.use_keyed_state((id.clone(), "had-focus"), cx, |_, _| false);
    if !open {
        if *had_focus.read(cx) {
            had_focus.update(cx, |v, _| *v = false);
        }
        return None;
    }
    let set_open = spec.set_open;
    // Focus moves in once the surface has painted. Overlay's open-time
    // focus can land before that (a menu open on arrival, or one waiting a
    // frame to be measured), when the handle is in no rendered frame yet and
    // the focus is lost: the first paint takes it again.
    let pending = window.use_keyed_state((id.clone(), "focus-pending"), cx, |_, _| false);
    let fresh = window.use_keyed_state((id.clone(), "fresh"), cx, |_, _| false);
    if opening {
        had_focus.update(cx, |v, _| *v = false);
        pending.update(cx, |v, _| *v = true);
        fresh.update(cx, |v, _| *v = true);
    } else if focus.contains_focused(window, cx) {
        if !*had_focus.read(cx) {
            had_focus.update(cx, |v, _| *v = true);
        }
    } else if *had_focus.read(cx)
        // "Inside" is answered from the last painted frame. A submenu opened
        // from the keyboard takes focus before its popup has painted (the
        // popup waits a frame to measure its item), so for one frame the
        // focused handle is in no frame at all: decide once it is painted.
        && window.focused(cx).is_none_or(|f| f.is_rendered(window))
    {
        had_focus.update(cx, |v, _| *v = false);
        let set_open = set_open.clone();
        window.defer(cx, move |window, cx| set_open(false, window, cx));
    }
    let chain = Chain {
        close: {
            let set_open = set_open.clone();
            Rc::new(move |window, cx| set_open(false, window, cx))
        },
        selections,
        bounds: bounds.clone(),
    };
    let outside = {
        let bounds = bounds.clone();
        move |event: &gpui_kit::MouseDownEvent, window: &mut Window, cx: &mut App| {
            if open && !bounds.borrow().iter().any(|b| b.contains(&event.position)) {
                set_open(false, window, cx);
            }
        }
    };
    // Submenus re-register their bounds while they paint.
    bounds.borrow_mut().clear();
    let mut menu = Menu::new((id.clone(), "menu"), spec.items, focus, chain)
        .size(spec.size)
        .label(spec.label)
        .min_width(spec.min_width);
    menu.on_key = spec.on_unhandled_key;
    menu.opening = Some(fresh);
    let take_focus = {
        let focus = menu.focus.clone();
        canvas(
            |_, _, _| {},
            move |_, _, window, cx| {
                if !*pending.read(cx) {
                    return;
                }
                pending.update(cx, |v, _| *v = false);
                let focus = focus.clone();
                window.defer(cx, move |window, cx| {
                    if !focus.contains_focused(window, cx) {
                        focus.focus(window, cx);
                    }
                });
            },
        )
        .absolute()
        .size_0()
    };
    Some(
        div()
            .id((id.clone(), "surface"))
            .on_mouse_down_out(outside)
            .child(menu)
            .child(take_focus)
            .into_any_element(),
    )
}

/// A menu that hangs from a trigger's start edge (Base's `Popup`): the
/// shared body of DropdownMenu and Menubar menus.
pub(crate) fn anchored_menu(
    id: &ElementId,
    trigger: AnyElement,
    spec: MenuSpec,
    window: &mut Window,
    cx: &mut App,
) -> AnyElement {
    anchored_menu_aligned(id, trigger, spec, false, window, cx)
}

/// [`anchored_menu`], hanging from the trigger's end edge when `end`
/// (`menuAlignment="bottom-end"`).
pub(crate) fn anchored_menu_aligned(
    id: &ElementId,
    trigger: AnyElement,
    spec: MenuSpec,
    end: bool,
    window: &mut Window,
    cx: &mut App,
) -> AnyElement {
    anchored_menu_impl(
        id,
        trigger,
        spec,
        end,
        false,
        None,
        None,
        Shift::NONE,
        window,
        cx,
    )
}

/// [`anchored_menu`] moved `dx` px along the trigger's start edge (only a
/// negative shift, toward the start) and `gap` px away from it: a
/// breadcrumb's overflow menu (`.cds--breadcrumb-menu-options`, placed 13px
/// before its trigger and 10px below it to leave room for its caret).
pub(crate) fn anchored_menu_offset(
    id: &ElementId,
    trigger: AnyElement,
    spec: MenuSpec,
    dx: f32,
    gap: f32,
    window: &mut Window,
    cx: &mut App,
) -> AnyElement {
    anchored_menu_impl(
        id,
        trigger,
        spec,
        false,
        false,
        None,
        None,
        Shift {
            dx: dx.min(0.),
            gap,
            caret: true,
        },
        window,
        cx,
    )
}

/// [`anchored_menu`] whose trigger fills its parent's height (a menubar
/// trigger: `.cu-menubar-trigger { height: 100% }`). Closing returns focus
/// to `trigger_focus`, the trigger whose menu was open.
pub(crate) fn anchored_menu_full_height(
    id: &ElementId,
    trigger: AnyElement,
    trigger_focus: FocusHandle,
    spec: MenuSpec,
    window: &mut Window,
    cx: &mut App,
) -> AnyElement {
    anchored_menu_impl(
        id,
        trigger,
        spec,
        false,
        true,
        Some(trigger_focus),
        None,
        Shift::NONE,
        window,
        cx,
    )
}

/// Carbon `MenuButton`'s menu (`MenuButton` in `bundle.js`): centred under
/// its trigger (`menuAlignment="bottom"`), and as wide as the trigger
/// (floating-ui's `size` middleware sets the menu's `width` to the
/// trigger's) held to `.cds--menu`'s `min-inline-size` / `max-inline-size`
/// ([`button_menu_width`]), so a long label ellipsizes instead of widening
/// the menu. `trigger_width` is in px at the 16px root size.
pub(crate) fn anchored_button_menu(
    id: &ElementId,
    trigger: AnyElement,
    spec: MenuSpec,
    trigger_width: f32,
    window: &mut Window,
    cx: &mut App,
) -> AnyElement {
    let width = button_menu_width(&spec.items, trigger_width);
    anchored_menu_impl(
        id,
        trigger,
        spec,
        false,
        false,
        None,
        Some(width),
        Shift::NONE,
        window,
        cx,
    )
}

/// The width (px at the 16px root size) of a `MenuButton` menu of `items`
/// under a `trigger_width` trigger: the trigger's width between `10rem` (or
/// `12rem` with icons) and `18rem`.
pub fn button_menu_width(items: &[MenuItem], trigger_width: f32) -> f32 {
    let mut rows = Vec::new();
    flatten(items, "", &HashMap::new(), &mut rows);
    let with_icons = rows
        .iter()
        .any(|r| matches!(r, Row::Entry { icon: Some(_), .. }));
    let geometry = MenuGeometry::of(with_icons);
    trigger_width
        .min(geometry.max_width)
        .max(geometry.min_width)
}

/// Where an anchored menu sits against its trigger, beyond the anchor.
#[derive(Clone, Copy)]
struct Shift {
    /// Toward the start edge (px, zero or negative).
    dx: f32,
    /// Away from the trigger (px).
    gap: f32,
    /// Draw the breadcrumb caret.
    caret: bool,
}

impl Shift {
    const NONE: Self = Self {
        dx: 0.,
        gap: 0.,
        caret: false,
    };
}

/// `.cds--breadcrumb-menu-options.cds--overflow-menu-options::after`: a
/// 0.4375rem-tall triangle in `field` standing on the menu's top edge,
/// 0.875rem from its start (`border-block-end: 0.4375rem solid var(--field);
/// border-inline: 0.4375rem solid transparent; inset-block-start: -0.4375rem;
/// inset-inline-start: 0.875rem`).
fn breadcrumb_caret() -> impl IntoElement {
    let fill = color(ColorRole::Field01);
    canvas(
        |_, _, _| {},
        move |bounds, _, window, _| {
            let o = bounds.origin;
            let w = bounds.size.width;
            let h = bounds.size.height;
            let mut path = gpui_kit::PathBuilder::fill();
            path.move_to(gpui_kit::point(o.x, o.y + h));
            path.line_to(gpui_kit::point(o.x + w / 2., o.y));
            path.line_to(gpui_kit::point(o.x + w, o.y + h));
            path.close();
            if let Ok(path) = path.build() {
                window.paint_path(path, fill);
            }
        },
    )
    .absolute()
    .top(rem(-7.))
    .left(rem(14.))
    .w(rem(14.))
    .h(rem(7.))
}

#[allow(clippy::too_many_arguments)]
fn anchored_menu_impl(
    id: &ElementId,
    trigger: AnyElement,
    spec: MenuSpec,
    end: bool,
    full_height: bool,
    return_to: Option<FocusHandle>,
    centered_width: Option<f32>,
    offset: Shift,
    window: &mut Window,
    cx: &mut App,
) -> AnyElement {
    // Below the trigger, or above it when the menu only fits there (Carbon
    // menus `autoAlign`), never over it ([`super::overlay::DropSide`]).
    let anchor = super::overlay::drop_anchor((id.clone(), "drop"), window, cx);
    let trigger = div()
        .relative()
        .when(full_height, |this| this.h_full())
        .child(trigger)
        .child(anchor.measure_trigger());
    let side = anchor.place_measured(window);
    let popup = gpui_kit::base::Popup::new((id.clone(), "popup"), trigger)
        .anchor(if centered_width.is_some() {
            side.anchor_center()
        } else if end {
            side.anchor_end()
        } else {
            side.anchor()
        })
        .when(full_height, |this| this.h_full())
        // A start-edge shift: the measured box reaches `-dx` px before the
        // trigger and pads it back, so the trigger stays put and the surface
        // hangs from the box's start edge.
        .when(offset.dx < 0., |this| {
            this.ml(gpui_kit::px(offset.dx))
                .pl(gpui_kit::px(-offset.dx))
        })
        .offset(gpui_kit::px(offset.gap));
    let surface =
        menu_surface_returning(id, spec, return_to, window, cx).map(
            |surface| match centered_width {
                // The menu stretches to the fixed width (its own min/max agree).
                Some(width) => div().w(rem(width)).child(surface).into_any_element(),
                None => surface,
            },
        );
    // The caret points at the trigger from the menu's top edge; a menu
    // flipped above its trigger has none.
    let surface = surface.map(|surface| {
        if offset.caret && side == super::overlay::DropSide::Below {
            div()
                .relative()
                .child(surface)
                .child(breadcrumb_caret())
                .into_any_element()
        } else {
            surface
        }
    });
    match surface {
        // Not painted until measured on the side it fits (no first-open
        // frame below the trigger before the flip).
        Some(surface) => popup.content(anchor.surface(surface)).into_any_element(),
        None => popup.into_any_element(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tokens::CarbonTheme;

    fn hex(c: Hsla) -> String {
        let c = c.to_rgb();
        let b = |v: f32| (v * 255.).round() as u8;
        format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
    }

    fn rows(items: &[MenuItem]) -> Vec<Row> {
        let mut out = Vec::new();
        flatten(items, "", &HashMap::new(), &mut out);
        out
    }

    #[test]
    fn item_heights_follow_menu_size_and_density() {
        // `.cds--menu--xs/sm/md/lg .cds--menu-item { block-size }`.
        let c = Density::Comfortable;
        assert_eq!(
            MenuSize::ALL.map(|s| s.item_height(c)),
            [24., 32., 40., 48.]
        );
        // `[data-density="compact"] .cds--menu-item { height: var(--density-row-md) }`.
        assert_eq!(MenuSize::Lg.item_height(Density::Compact), 32.);
        let g = MenuGeometry::of(false);
        assert_eq!(
            (g.padding_block, g.min_width, g.max_width),
            (4., 160., 288.)
        );
        assert_eq!(MenuGeometry::of(true).min_width, 192.);
    }

    #[test]
    fn item_colors_follow_menu_item_rules() {
        crate::theme::tests_set(CarbonTheme::White);
        let (bg, text, outline) = MenuLook::item(ItemState::default());
        assert_eq!((bg.a, hex(text), outline), (0., "#525252".into(), None));
        let (bg, text, _) = MenuLook::item(ItemState {
            hovered: true,
            ..Default::default()
        });
        assert_eq!((hex(bg), hex(text)), ("#e8e8e8".into(), "#161616".into()));
        let (bg, text, _) = MenuLook::item(ItemState {
            hovered: true,
            danger: true,
            ..Default::default()
        });
        assert_eq!((hex(bg), hex(text)), ("#da1e28".into(), "#ffffff".into()));
        let (_, _, outline) = MenuLook::item(ItemState {
            focused: true,
            ..Default::default()
        });
        assert_eq!(outline.map(hex).as_deref(), Some("#0f62fe"));
        let (bg, _, _) = MenuLook::item(ItemState {
            hovered: true,
            disabled: true,
            ..Default::default()
        });
        assert_eq!(bg.a, 0.);
        assert_eq!(hex(MenuLook::current().background), "#f4f4f4");
    }

    #[test]
    fn danger_items_are_red_at_rest_and_dividers_use_border_subtle_00() {
        let danger = ItemState {
            danger: true,
            ..Default::default()
        };
        crate::theme::tests_set(CarbonTheme::White);
        let (bg, text, _) = MenuLook::item(danger);
        assert_eq!((bg.a, hex(text)), (0., "#525252".into()));
        assert_eq!(hex(MenuLook::label(danger)), "#da1e28");
        let hovered = ItemState {
            hovered: true,
            ..danger
        };
        assert_eq!(hex(MenuLook::label(hovered)), "#ffffff");
        assert_eq!(hex(MenuLook::current().divider), "#e0e0e0");
        // Disabled wins over danger.
        let (_, text, _) = MenuLook::item(ItemState {
            disabled: true,
            ..danger
        });
        assert_eq!(text, color(ColorRole::TextDisabled));
        crate::theme::tests_set(CarbonTheme::G90);
        // `text-error`, not `support-error` (#ff8389 on G90).
        assert_eq!(hex(MenuLook::label(danger)), "#ffb3b8");
        crate::theme::tests_set(CarbonTheme::G100);
        assert_eq!(hex(MenuLook::label(danger)), "#ff8389");
        crate::theme::tests_set(CarbonTheme::G90);
        assert_eq!(hex(MenuLook::current().divider), "#525252");
        crate::theme::tests_set(CarbonTheme::White);
    }

    #[test]
    fn keyboard_skips_disabled_items_and_dividers_and_wraps() {
        let rows = rows(&[
            MenuItem::item("Restart"),
            MenuItem::item("Resize").disabled(true),
            MenuItem::divider(),
            MenuItem::item("Delete").danger(true),
        ]);
        assert_eq!(step(&rows, None, 1), Some(0));
        assert_eq!(step(&rows, Some(0), 1), Some(3));
        assert_eq!(step(&rows, Some(3), 1), Some(0));
        assert_eq!(step(&rows, None, -1), Some(3));
        assert_eq!(step(&rows, Some(0), -1), Some(3));
    }

    #[test]
    fn typeahead_finds_the_next_match() {
        let rows = rows(&[
            MenuItem::item("Restart"),
            MenuItem::item("Resize"),
            MenuItem::item("Move to"),
        ]);
        assert_eq!(find_typed(&rows, None, "r"), Some(0));
        assert_eq!(find_typed(&rows, Some(0), "r"), Some(1));
        assert_eq!(find_typed(&rows, Some(1), "r"), Some(0));
        assert_eq!(find_typed(&rows, Some(0), "res"), Some(0));
        assert_eq!(find_typed(&rows, Some(0), "resi"), Some(1));
        assert_eq!(find_typed(&rows, None, "m"), Some(2));
    }

    #[test]
    fn selectable_and_radio_rows_carry_their_state() {
        let rows = rows(&[
            MenuItem::checkbox("Show archived").default_selected(true),
            MenuItem::divider(),
            MenuItem::radio_group("Sort", ["Name", "Date"]).default_selected_item(1),
        ]);
        let checked: Vec<_> = rows
            .iter()
            .map(|r| match r {
                Row::Entry { checked, .. } => *checked,
                Row::Divider | Row::Heading(_) => None,
            })
            .collect();
        assert_eq!(checked, [Some(true), None, Some(false), Some(true)]);
    }

    #[test]
    fn radio_options_carry_their_second_line_and_dot_and_a_heading_is_passed_over() {
        let rows = rows(&[
            MenuItem::heading("Dev servers"),
            MenuItem::radio_group("Dev servers", ["dev · pnpm dev", "storybook"])
                .option_details([Phrase::new().code("localhost:5173").text(" · This Mac")])
                .option_dots([Some(ColorRole::SupportSuccess), None])
                .selected_item(Some(1)),
            MenuItem::divider(),
            MenuItem::item("Open an address…").detail("A URL, or a local HTML file"),
        ]);
        let parts: Vec<_> = rows
            .iter()
            .map(|r| match r {
                Row::Entry { detail, dot, .. } => (detail.as_ref().map(Phrase::plain), *dot),
                _ => (None, None),
            })
            .collect();
        assert_eq!(
            parts,
            [
                (None, None),
                (
                    Some("localhost:5173 · This Mac".into()),
                    Some(ColorRole::SupportSuccess)
                ),
                (None, None),
                (None, None),
                (Some("A URL, or a local HTML file".into()), None),
            ]
        );
        // The heading is shown, not chosen: the keyboard starts below it,
        // and a heading over a lone radio group keeps it a select.
        assert!(matches!(rows[0], Row::Heading(_)));
        assert_eq!(step(&rows, None, 1), Some(1));
        assert_eq!(step(&rows, Some(4), 1), Some(1));
        let select = super::tests::rows(&[
            MenuItem::heading("Page"),
            MenuItem::radio_group("Page", ["1", "2"]).selected_item(Some(1)),
        ]);
        assert_eq!(initial_row(&select), Some(2));
    }

    #[test]
    fn a_submenu_row_keeps_its_trailing_text() {
        let rows = rows(&[MenuItem::item("Viewport")
            .shortcut("Responsive")
            .submenu([MenuItem::item("iPad")])]);
        assert!(matches!(
            &rows[0],
            Row::Entry { shortcut: Some(s), action: Action::Submenu(_), .. } if s == "Responsive"
        ));
    }

    #[test]
    fn a_second_line_is_text_secondary_but_disabled_or_on_a_danger_fill() {
        crate::theme::tests_set(CarbonTheme::White);
        assert_eq!(hex(MenuLook::detail(ItemState::default())), "#525252");
        let hovered = ItemState {
            hovered: true,
            ..Default::default()
        };
        assert_eq!(hex(MenuLook::detail(hovered)), "#525252");
        let disabled = ItemState {
            disabled: true,
            ..Default::default()
        };
        assert_eq!(MenuLook::detail(disabled), color(ColorRole::TextDisabled));
        let danger = ItemState {
            danger: true,
            hovered: true,
            ..Default::default()
        };
        assert_eq!(hex(MenuLook::detail(danger)), "#ffffff");
    }

    #[test]
    fn opening_focuses_the_first_enabled_item_or_the_picked_option() {
        // Carbon `Menu`: the first enabled item ("Restart", then ↓ is "Resize").
        let actions = rows(&[
            MenuItem::item("Restart").disabled(true),
            MenuItem::item("Resize"),
            MenuItem::divider(),
            MenuItem::item("Delete").danger(true),
        ]);
        assert_eq!(initial_row(&actions), Some(1));
        // A mixed menu ignores its radio group's pick.
        let mixed = rows(&[
            MenuItem::checkbox("Show archived"),
            MenuItem::divider(),
            MenuItem::radio_group("Sort", ["Name", "Date"]).default_selected_item(1),
        ]);
        assert_eq!(initial_row(&mixed), Some(0));
        // A lone radio group is a select: it starts on the picked option.
        let select = rows(&[MenuItem::radio_group("Page", ["1", "2", "3"]).selected_item(Some(2))]);
        assert_eq!(initial_row(&select), Some(2));
        let none = rows(&[MenuItem::radio_group("Page", ["1", "2"]).selected_item(None)]);
        assert_eq!(initial_row(&none), Some(0));
    }
}