iced-cube 0.0.4

Themeable application components for the iced GUI library, with Lucide icons
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
//! A menu of actions that opens from a trigger button.
//!
//! [`State`] owns the entries (items, checkbox and radio items, submenus,
//! group labels and separators), whether the menu is open, and which row is
//! highlighted at each open level. Its [`update`](State::update) handles
//! pointer and keyboard navigation and returns an [`Output`] when an item
//! is chosen. The context menu uses the same model and rows.
//!
//! An open menu resolves its own keys through a [`Keymap`] of [`Action`]s,
//! so the app needs no subscription while it is open; see
//! [`default_keymap`]. A closed menu claims no keys: it opens from its
//! trigger, or from a chord the app binds to [`Action::Open`] and routes
//! through [`State::key_event`]. Rows are drawn in the shared
//! [menu look](crate::overlay::menu).

use std::rc::Rc;

use iced::Element;
use iced::keyboard::{Key, key::Named};
use iced::widget::column;

use crate::icon::Glyph;
use crate::keys::{self, Chord, Keymap};
use crate::overlay::anchored::{Align, Placement, Side, anchored};
use crate::overlay::menu::{self, Leading, Parts, RowStatus, Trailing};
use crate::theme::space;

/// Default menu width in logical pixels.
pub const WIDTH: f32 = 224.0;

/// What an item does when chosen.
#[derive(Debug, Clone, PartialEq)]
pub enum Kind<Id> {
    /// A plain action.
    Action,
    /// A check mark that toggles.
    Checkbox(bool),
    /// One choice among the radio items next to it. A run of adjacent
    /// radio items forms one group.
    Radio(bool),
    /// Opens a nested menu.
    Submenu(Vec<Entry<Id>>),
}

/// A row that can be highlighted and chosen.
#[derive(Debug, Clone, PartialEq)]
pub struct Item<Id> {
    pub id: Id,
    pub label: String,
    pub icon: Option<Glyph>,
    /// A hint such as `Ctrl+S`, shown on the right. It does not bind the keys.
    pub shortcut: Option<String>,
    pub disabled: bool,
    pub destructive: bool,
    pub kind: Kind<Id>,
}

impl<Id> Item<Id> {
    fn enabled(&self) -> bool {
        !self.disabled
    }

    fn submenu(&self) -> Option<&[Entry<Id>]> {
        match &self.kind {
            Kind::Submenu(entries) => Some(entries),
            _ => None,
        }
    }
}

/// One line of a menu.
#[derive(Debug, Clone, PartialEq)]
pub enum Entry<Id> {
    Item(Item<Id>),
    /// A heading for the items below it.
    Label(String),
    Separator,
}

fn new_item<Id>(id: Id, label: impl Into<String>, kind: Kind<Id>) -> Entry<Id> {
    Entry::Item(Item {
        id,
        label: label.into(),
        icon: None,
        shortcut: None,
        disabled: false,
        destructive: false,
        kind,
    })
}

/// An action item.
pub fn item<Id>(id: Id, label: impl Into<String>) -> Entry<Id> {
    new_item(id, label, Kind::Action)
}

/// An item with a check mark that toggles when chosen.
pub fn checkbox_item<Id>(id: Id, label: impl Into<String>, checked: bool) -> Entry<Id> {
    new_item(id, label, Kind::Checkbox(checked))
}

/// An item that checks itself and unchecks the radio items next to it.
pub fn radio_item<Id>(id: Id, label: impl Into<String>, checked: bool) -> Entry<Id> {
    new_item(id, label, Kind::Radio(checked))
}

/// An item that opens `entries` in a nested menu to its right.
pub fn submenu<Id>(
    id: Id,
    label: impl Into<String>,
    entries: impl IntoIterator<Item = Entry<Id>>,
) -> Entry<Id> {
    new_item(id, label, Kind::Submenu(entries.into_iter().collect()))
}

/// A heading for a group of items.
pub fn group_label<Id>(label: impl Into<String>) -> Entry<Id> {
    Entry::Label(label.into())
}

/// A line between groups.
pub fn separator<Id>() -> Entry<Id> {
    Entry::Separator
}

impl<Id> Entry<Id> {
    /// Adds a leading Lucide icon. Ignored on labels and separators, as are
    /// the other item settings.
    pub fn icon(self, glyph: Glyph) -> Self {
        self.with_item(|item| item.icon = Some(glyph))
    }

    pub fn shortcut(self, hint: impl Into<String>) -> Self {
        let hint = hint.into();
        self.with_item(|item| item.shortcut = Some(hint))
    }

    pub fn disabled(self, disabled: bool) -> Self {
        self.with_item(|item| item.disabled = disabled)
    }

    /// Draws the item in the destructive colour, for actions such as delete.
    pub fn destructive(self, destructive: bool) -> Self {
        self.with_item(|item| item.destructive = destructive)
    }

    pub fn as_item(&self) -> Option<&Item<Id>> {
        match self {
            Entry::Item(item) => Some(item),
            _ => None,
        }
    }

    fn with_item(mut self, change: impl FnOnce(&mut Item<Id>)) -> Self {
        if let Entry::Item(item) = &mut self {
            change(item);
        }
        self
    }

    fn enabled_item(&self) -> Option<&Item<Id>> {
        self.as_item().filter(|item| item.enabled())
    }
}

/// Changes to a menu.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Event<Id> {
    /// Opens with nothing highlighted, as a pointer does.
    Open,
    Close,
    Toggle,
    /// The pointer moved onto an item. A submenu item opens its submenu.
    Highlight(Id),
    /// Highlights the next enabled item, wrapping. Opens a closed menu on
    /// the first item.
    Next,
    /// Highlights the previous enabled item, wrapping. Opens a closed menu
    /// on the last item.
    Previous,
    First,
    Last,
    /// Opens the highlighted submenu and highlights its first item.
    OpenSubmenu,
    /// Closes the innermost submenu.
    CloseSubmenu,
    /// Chooses an item, as a click does.
    Activate(Id),
    /// Chooses the highlighted item, or opens its submenu. Opens a closed
    /// menu on the first item.
    ActivateHighlighted,
    /// Highlights the next item whose label starts with this character.
    Typeahead(char),
}

/// What choosing an item did. The menu closes afterwards.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Output<Id> {
    /// An action item was chosen.
    Activated(Id),
    /// A checkbox item changed to this value.
    Toggled(Id, bool),
    /// A radio item was chosen.
    Selected(Id),
}

/// The entries of a menu, whether it is open and what is highlighted.
#[derive(Debug, Clone, PartialEq)]
pub struct State<Id> {
    entries: Vec<Entry<Id>>,
    /// One slot per open level: the root menu, then each open submenu.
    levels: Vec<Option<usize>>,
}

impl<Id: Copy + PartialEq> State<Id> {
    /// A closed menu. Item ids must be unique across submenus.
    pub fn new(entries: impl IntoIterator<Item = Entry<Id>>) -> Self {
        Self {
            entries: entries.into_iter().collect(),
            levels: Vec::new(),
        }
    }

    pub fn entries(&self) -> &[Entry<Id>] {
        &self.entries
    }

    pub fn is_open(&self) -> bool {
        !self.levels.is_empty()
    }

    /// How many menus are open: 0 when closed, 1 for the root menu, plus
    /// one per open submenu.
    pub fn depth(&self) -> usize {
        self.levels.len()
    }

    /// The highlighted item in the menu that has keyboard focus.
    pub fn highlighted(&self) -> Option<Id> {
        let path = self.active_path()?;
        self.item_at(&path).map(|item| item.id)
    }

    /// The highlighted row index in the menu `depth` levels deep.
    pub fn highlighted_at(&self, depth: usize) -> Option<usize> {
        self.levels.get(depth).copied().flatten()
    }

    pub fn item(&self, id: Id) -> Option<&Item<Id>> {
        let path = path_of(&self.entries, id)?;
        self.item_at(&path)
    }

    /// Whether a checkbox or radio item is checked.
    pub fn is_checked(&self, id: Id) -> bool {
        self.item(id)
            .is_some_and(|item| matches!(item.kind, Kind::Checkbox(true) | Kind::Radio(true)))
    }

    /// Checks or unchecks an item. Checking a radio item unchecks its group.
    pub fn set_checked(&mut self, id: Id, checked: bool) {
        let Some(path) = path_of(&self.entries, id) else {
            return;
        };
        let Some((&index, parents)) = path.split_last() else {
            return;
        };
        let Some(siblings) = entries_mut(&mut self.entries, parents) else {
            return;
        };
        let is_radio = matches!(
            siblings.get(index),
            Some(Entry::Item(Item {
                kind: Kind::Radio(_),
                ..
            }))
        );
        if is_radio && checked {
            for sibling in radio_group(siblings, index) {
                if let Some(Entry::Item(item)) = siblings.get_mut(sibling) {
                    item.kind = Kind::Radio(false);
                }
            }
        }
        if let Some(Entry::Item(item)) = siblings.get_mut(index) {
            match &mut item.kind {
                Kind::Checkbox(value) | Kind::Radio(value) => *value = checked,
                Kind::Action | Kind::Submenu(_) => {}
            }
        }
    }

    /// Enables or disables an item. A disabled item loses its highlight.
    pub fn set_disabled(&mut self, id: Id, disabled: bool) {
        let Some(path) = path_of(&self.entries, id) else {
            return;
        };
        let Some((&index, parents)) = path.split_last() else {
            return;
        };
        if let Some(Entry::Item(item)) =
            entries_mut(&mut self.entries, parents).and_then(|entries| entries.get_mut(index))
        {
            item.disabled = disabled;
        }
        let highlighted = self
            .levels
            .iter()
            .take(path.len())
            .copied()
            .eq(path.iter().copied().map(Some));
        if disabled && highlighted {
            self.levels.truncate(parents.len() + 1);
            if let Some(level) = self.levels.get_mut(parents.len()) {
                *level = None;
            }
        }
    }

    /// Applies an event and returns what was chosen, if anything.
    pub fn update(&mut self, event: Event<Id>) -> Option<Output<Id>> {
        match event {
            Event::Open => self.open(),
            Event::Close => self.levels.clear(),
            Event::Toggle if self.is_open() => self.levels.clear(),
            Event::Toggle => self.open(),
            Event::Highlight(id) => self.highlight(id),
            Event::Next | Event::First if !self.is_open() => self.open_on(Edge::First),
            Event::Previous | Event::Last if !self.is_open() => self.open_on(Edge::Last),
            Event::Next => self.step(Step::Next),
            Event::Previous => self.step(Step::Previous),
            Event::First => self.step(Step::Edge(Edge::First)),
            Event::Last => self.step(Step::Edge(Edge::Last)),
            Event::OpenSubmenu => self.open_submenu(),
            Event::CloseSubmenu => self.close_submenu(),
            Event::Typeahead(character) => self.typeahead(character),
            Event::Activate(id) => return self.activate(id),
            Event::ActivateHighlighted if !self.is_open() => self.open_on(Edge::First),
            Event::ActivateHighlighted => return self.activate_highlighted(),
        }
        None
    }

    /// Turns a key press into an event: first through `keymap`, then, while
    /// the menu is open, as typeahead for a letter or digit. An open menu
    /// does this itself; an app calls it for a closed menu's
    /// [`Action::Open`] chord.
    pub fn key_event(&self, keymap: &Keymap<Action>, key: &keys::Event) -> Option<Event<Id>> {
        if let Some(action) = keymap.resolve_event(key) {
            return action.event(self);
        }
        if !self.is_open() {
            return None;
        }
        typeahead(key).map(Event::Typeahead)
    }

    fn open(&mut self) {
        if !self.is_open() {
            self.levels.push(None);
        }
    }

    fn open_on(&mut self, edge: Edge) {
        self.levels = vec![edge.find(&self.entries)];
    }

    /// The deepest level with a highlight, or the root.
    fn active_depth(&self) -> usize {
        self.levels.iter().rposition(Option::is_some).unwrap_or(0)
    }

    fn active_path(&self) -> Option<Vec<usize>> {
        let depth = self.active_depth();
        self.levels.get(..=depth)?.iter().copied().collect()
    }

    fn entries_at(&self, depth: usize) -> Option<&[Entry<Id>]> {
        let mut entries = self.entries.as_slice();
        for level in self.levels.get(..depth)? {
            entries = entries.get((*level)?)?.as_item()?.submenu()?;
        }
        Some(entries)
    }

    fn item_at(&self, path: &[usize]) -> Option<&Item<Id>> {
        let (&index, parents) = path.split_last()?;
        let mut entries = self.entries.as_slice();
        for &parent in parents {
            entries = entries.get(parent)?.as_item()?.submenu()?;
        }
        entries.get(index)?.as_item()
    }

    /// Moves the highlight in the active menu, closing deeper submenus.
    fn step(&mut self, step: Step) {
        let depth = self.active_depth();
        let Some(entries) = self.entries_at(depth) else {
            return;
        };
        let current = self.highlighted_at(depth);
        let target = match step {
            Step::Next => next_enabled(entries, current, true, |_| true),
            Step::Previous => next_enabled(entries, current, false, |_| true),
            Step::Edge(edge) => edge.find(entries),
        };
        self.set_highlight(depth, target);
    }

    fn typeahead(&mut self, character: char) {
        let depth = self.active_depth();
        let Some(entries) = self.entries_at(depth) else {
            return;
        };
        let wanted = character.to_lowercase().collect::<String>();
        let target = next_enabled(entries, self.highlighted_at(depth), true, |item| {
            item.label.to_lowercase().starts_with(&wanted)
        });
        self.set_highlight(depth, target);
    }

    fn set_highlight(&mut self, depth: usize, target: Option<usize>) {
        if target.is_none() {
            return;
        }
        self.levels.truncate(depth + 1);
        if let Some(level) = self.levels.get_mut(depth) {
            *level = target;
        }
    }

    fn highlight(&mut self, id: Id) {
        let Some(path) = path_of(&self.entries, id) else {
            return;
        };
        let Some(item) = self.item_at(&path).filter(|item| item.enabled()) else {
            return;
        };
        let opens = item.submenu().is_some();
        let target = some(&path);
        if self.levels.len() > target.len() && self.levels.starts_with(&target) {
            return;
        }
        self.levels = target;
        if opens {
            self.levels.push(None);
        }
    }

    fn open_submenu(&mut self) {
        let depth = self.active_depth();
        let Some(path) = self.active_path() else {
            return;
        };
        let Some(children) = self
            .item_at(&path)
            .filter(|item| item.enabled())
            .and_then(Item::submenu)
        else {
            return;
        };
        let first = Edge::First.find(children);
        self.levels.truncate(depth + 1);
        self.levels.push(first);
    }

    fn close_submenu(&mut self) {
        if self.levels.len() <= 1 {
            return;
        }
        let keep = self.active_depth().max(1);
        self.levels.truncate(keep);
    }

    fn activate(&mut self, id: Id) -> Option<Output<Id>> {
        let path = path_of(&self.entries, id)?;
        let item = self.item_at(&path).filter(|item| item.enabled())?;
        if item.submenu().is_some() {
            self.highlight(id);
            return None;
        }
        self.choose(id)
    }

    fn activate_highlighted(&mut self) -> Option<Output<Id>> {
        let path = self.active_path()?;
        let item = self.item_at(&path).filter(|item| item.enabled())?;
        if item.submenu().is_some() {
            self.open_submenu();
            return None;
        }
        let id = item.id;
        self.choose(id)
    }

    /// Applies a chosen item and closes the menu.
    fn choose(&mut self, id: Id) -> Option<Output<Id>> {
        let output = match self.item(id)?.kind {
            Kind::Action => Output::Activated(id),
            Kind::Checkbox(checked) => {
                self.set_checked(id, !checked);
                Output::Toggled(id, !checked)
            }
            Kind::Radio(_) => {
                self.set_checked(id, true);
                Output::Selected(id)
            }
            Kind::Submenu(_) => return None,
        };
        self.levels.clear();
        Some(output)
    }
}

fn some(path: &[usize]) -> Vec<Option<usize>> {
    path.iter().copied().map(Some).collect()
}

#[derive(Debug, Clone, Copy)]
enum Edge {
    First,
    Last,
}

impl Edge {
    fn find<Id>(self, entries: &[Entry<Id>]) -> Option<usize> {
        let enabled = |(_, entry): &(usize, &Entry<Id>)| entry.enabled_item().is_some();
        match self {
            Edge::First => entries.iter().enumerate().find(enabled),
            Edge::Last => entries.iter().enumerate().rev().find(enabled),
        }
        .map(|(index, _)| index)
    }
}

#[derive(Debug, Clone, Copy)]
enum Step {
    Next,
    Previous,
    Edge(Edge),
}

/// The nearest enabled item after (or before) `from` that `accept`s,
/// wrapping round. With nothing highlighted, the search starts at an end.
fn next_enabled<Id>(
    entries: &[Entry<Id>],
    from: Option<usize>,
    forward: bool,
    accept: impl Fn(&Item<Id>) -> bool,
) -> Option<usize> {
    let len = entries.len();
    if len == 0 {
        return None;
    }
    let start = from.unwrap_or(if forward { len - 1 } else { 0 });
    let offset = if forward { 1 } else { len - 1 };
    (1..=len)
        .map(|n| (start + offset * n) % len)
        .find(|&index| {
            entries
                .get(index)
                .and_then(Entry::enabled_item)
                .is_some_and(&accept)
        })
}

/// The index path to an item, through submenus.
fn path_of<Id: Copy + PartialEq>(entries: &[Entry<Id>], id: Id) -> Option<Vec<usize>> {
    entries.iter().enumerate().find_map(|(index, entry)| {
        let item = entry.as_item()?;
        if item.id == id {
            return Some(vec![index]);
        }
        let mut path = path_of(item.submenu()?, id)?;
        path.insert(0, index);
        Some(path)
    })
}

fn entries_mut<'a, Id>(
    entries: &'a mut Vec<Entry<Id>>,
    parents: &[usize],
) -> Option<&'a mut Vec<Entry<Id>>> {
    let Some((&first, rest)) = parents.split_first() else {
        return Some(entries);
    };
    match entries.get_mut(first)? {
        Entry::Item(Item {
            kind: Kind::Submenu(children),
            ..
        }) => entries_mut(children, rest),
        _ => None,
    }
}

/// The indices of the run of radio items around `index`.
fn radio_group<Id>(entries: &[Entry<Id>], index: usize) -> std::ops::Range<usize> {
    let is_radio = |entry: &Entry<Id>| {
        matches!(
            entry,
            Entry::Item(Item {
                kind: Kind::Radio(_),
                ..
            })
        )
    };
    let start = entries
        .get(..index)
        .and_then(|before| before.iter().rposition(|entry| !is_radio(entry)))
        .map_or(0, |position| position + 1);
    let end = entries
        .get(index..)
        .and_then(|after| after.iter().position(|entry| !is_radio(entry)))
        .map_or(entries.len(), |position| index + position);
    start..end
}

/// The character a key press types into the menu's typeahead: a letter or
/// digit pressed without Ctrl, Alt or the logo key.
pub fn typeahead(key: &keys::Event) -> Option<char> {
    let modifiers = key.modifiers;
    if modifiers.control() || modifiers.alt() || modifiers.logo() {
        return None;
    }
    let Key::Character(text) = &key.key else {
        return None;
    };
    let mut chars = text.chars();
    let character = chars.next()?;
    if chars.next().is_some() || !character.is_alphanumeric() {
        return None;
    }
    Some(character)
}

/// What a menu keyboard shortcut does.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Action {
    /// Opens a closed menu on its first item. Not bound by default.
    Open,
    Next,
    Previous,
    First,
    Last,
    Activate,
    Close,
    OpenSubmenu,
    CloseSubmenu,
}

impl Action {
    /// The [`Event`] this action sends to `state`, or `None` when it does
    /// nothing: only [`Action::Open`] works while the menu is closed, and
    /// only the others while it is open. [`Action::Close`] closes just the
    /// innermost submenu when one is open.
    pub fn event<Id: Copy + PartialEq>(self, state: &State<Id>) -> Option<Event<Id>> {
        if !state.is_open() {
            return (self == Action::Open).then_some(Event::First);
        }
        match self {
            Action::Open => None,
            Action::Next => Some(Event::Next),
            Action::Previous => Some(Event::Previous),
            Action::First => Some(Event::First),
            Action::Last => Some(Event::Last),
            Action::Activate => Some(Event::ActivateHighlighted),
            Action::Close if state.depth() > 1 => Some(Event::CloseSubmenu),
            Action::Close => Some(Event::Close),
            Action::OpenSubmenu => Some(Event::OpenSubmenu),
            Action::CloseSubmenu => Some(Event::CloseSubmenu),
        }
    }
}

impl keys::Action for Action {
    const ALL: &'static [Self] = &[
        Action::Open,
        Action::Next,
        Action::Previous,
        Action::First,
        Action::Last,
        Action::Activate,
        Action::Close,
        Action::OpenSubmenu,
        Action::CloseSubmenu,
    ];

    fn defaults() -> Keymap<Self> {
        default_keymap()
    }

    fn name(self) -> &'static str {
        match self {
            Action::Open => "Open",
            Action::Next => "Next",
            Action::Previous => "Previous",
            Action::First => "First",
            Action::Last => "Last",
            Action::Activate => "Activate",
            Action::Close => "Close",
            Action::OpenSubmenu => "OpenSubmenu",
            Action::CloseSubmenu => "CloseSubmenu",
        }
    }

    fn description(self) -> &'static str {
        match self {
            Action::Open => "Opens the menu on its first item.",
            Action::Next => "Highlights the next enabled item, wrapping.",
            Action::Previous => "Highlights the previous enabled item, wrapping.",
            Action::First => "Highlights the first enabled item.",
            Action::Last => "Highlights the last enabled item.",
            Action::Activate => "Chooses the highlighted item or opens its submenu.",
            Action::Close => "Closes the innermost submenu, or the menu.",
            Action::OpenSubmenu => "Opens the highlighted submenu.",
            Action::CloseSubmenu => "Closes the innermost submenu.",
        }
    }
}

/// The default menu shortcuts:
///
/// | Keys | Action |
/// | --- | --- |
/// | `ArrowDown` | [`Action::Next`] |
/// | `ArrowUp` | [`Action::Previous`] |
/// | `Home` | [`Action::First`] |
/// | `End` | [`Action::Last`] |
/// | `Enter`, `Space` | [`Action::Activate`] |
/// | `Escape` | [`Action::Close`] |
/// | `ArrowRight` | [`Action::OpenSubmenu`] |
/// | `ArrowLeft` | [`Action::CloseSubmenu`] |
///
/// Letters and digits jump to the next item starting with them. The open
/// menu handles all of these itself, before anything underneath sees
/// them; pass a changed keymap with [`DropdownMenu::keymap`]. A closed
/// menu claims none of them. [`Action::Open`] has no default chord
/// because a menu usually belongs to one button; bind one when a menu
/// deserves an app-wide shortcut.
pub fn default_keymap() -> Keymap<Action> {
    Keymap::new()
        .bind(Chord::named(Named::ArrowDown), Action::Next)
        .bind(Chord::named(Named::ArrowUp), Action::Previous)
        .bind(Chord::named(Named::Home), Action::First)
        .bind(Chord::named(Named::End), Action::Last)
        .bind(Chord::named(Named::Enter), Action::Activate)
        .bind(Chord::named(Named::Space), Action::Activate)
        .bind(Chord::named(Named::Escape), Action::Close)
        .bind(Chord::named(Named::ArrowRight), Action::OpenSubmenu)
        .bind(Chord::named(Named::ArrowLeft), Action::CloseSubmenu)
}

/// A dropdown menu builder. Convert it into an [`Element`] to render.
pub struct DropdownMenu<'a, Id, Message> {
    state: &'a State<Id>,
    trigger: Element<'a, Message>,
    placement: Placement,
    width: f32,
    keymap: Keymap<Action>,
    on_event: Option<Box<dyn Fn(Event<Id>) -> Message + 'a>>,
}

impl<Id: std::fmt::Debug, Message> std::fmt::Debug for DropdownMenu<'_, Id, Message> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DropdownMenu")
            .field("state", self.state)
            .field("placement", &self.placement)
            .field("width", &self.width)
            .field("keymap", &self.keymap)
            .finish_non_exhaustive()
    }
}

/// Renders `trigger` with the menu of `state` below it while it is open.
///
/// The trigger opens the menu itself, usually a button whose message
/// carries [`Event::Toggle`]. Without [`on_event`](DropdownMenu::on_event)
/// every item renders disabled.
pub fn dropdown_menu<'a, Id, Message>(
    state: &'a State<Id>,
    trigger: impl Into<Element<'a, Message>>,
) -> DropdownMenu<'a, Id, Message> {
    DropdownMenu {
        state,
        trigger: trigger.into(),
        placement: Placement::new(Side::Bottom, Align::Start),
        width: WIDTH,
        keymap: default_keymap(),
        on_event: None,
    }
}

impl<'a, Id, Message> DropdownMenu<'a, Id, Message> {
    /// Which side of the trigger the menu opens on. Defaults to the bottom.
    pub fn side(mut self, side: Side) -> Self {
        self.placement.side = side;
        self
    }

    /// Defaults to the start, lining the menu up with the trigger's left edge.
    pub fn align(mut self, align: Align) -> Self {
        self.placement.align = align;
        self
    }

    /// Space between the trigger and the menu.
    pub fn gap(mut self, gap: f32) -> Self {
        self.placement.gap = gap;
        self
    }

    /// Width of the menu and its submenus.
    pub fn width(mut self, width: f32) -> Self {
        self.width = width;
        self
    }

    /// Replaces the [`default_keymap`] the open menu resolves its keys with.
    pub fn keymap(mut self, keymap: Keymap<Action>) -> Self {
        self.keymap = keymap;
        self
    }

    pub fn on_event(mut self, on_event: impl Fn(Event<Id>) -> Message + 'a) -> Self {
        self.on_event = Some(Box::new(on_event));
        self
    }
}

impl<'a, Id, Message> From<DropdownMenu<'a, Id, Message>> for Element<'a, Message>
where
    Id: Copy + PartialEq + 'a,
    Message: Clone + 'a,
{
    fn from(menu: DropdownMenu<'a, Id, Message>) -> Self {
        let DropdownMenu {
            state,
            trigger,
            placement,
            width,
            keymap,
            on_event,
        } = menu;
        let on_event: Option<Rc<dyn Fn(Event<Id>) -> Message + 'a>> = on_event.map(Rc::from);
        let panel = state
            .is_open()
            .then(|| panel(state, on_event.as_deref(), width));
        let anchored = anchored(trigger)
            .content(panel)
            .placement(placement)
            .dismiss_keys([]);
        let Some(on_event) = on_event.filter(|_| state.is_open()) else {
            return anchored.into();
        };
        anchored
            .on_dismiss(on_event(Event::Close))
            .on_key(move |key| state.key_event(&keymap, key).map(&*on_event))
            .into()
    }
}

/// The open menu: the root panel, with open submenus floating beside
/// their items.
pub(crate) fn panel<'a, Id, Message>(
    state: &'a State<Id>,
    on_event: Option<&dyn Fn(Event<Id>) -> Message>,
    width: f32,
) -> Element<'a, Message>
where
    Id: Copy + PartialEq + 'a,
    Message: Clone + 'a,
{
    level(state, &state.entries, 0, on_event, width)
}

fn level<'a, Id, Message>(
    state: &'a State<Id>,
    entries: &'a [Entry<Id>],
    depth: usize,
    on_event: Option<&dyn Fn(Event<Id>) -> Message>,
    width: f32,
) -> Element<'a, Message>
where
    Id: Copy + PartialEq + 'a,
    Message: Clone + 'a,
{
    let highlighted = state.highlighted_at(depth);
    let submenu_open = state.depth() > depth + 1;
    let indented = has_marks(entries);
    let slot = reserves_slot(entries);

    let rows = entries
        .iter()
        .enumerate()
        .map(|(index, entry)| match entry {
            Entry::Separator => menu::separator(),
            Entry::Label(label) => menu::group_label(label, indented),
            Entry::Item(item) => {
                let is_highlighted = highlighted == Some(index);
                let row = item_row(item, is_highlighted, slot, on_event);
                let Some(children) = item.submenu().filter(|_| is_highlighted && submenu_open)
                else {
                    return row;
                };
                // The root menu dismisses everything, so a submenu lets
                // outside presses through to it.
                anchored(row)
                    .content(Some(level(state, children, depth + 1, on_event, width)))
                    .placement(
                        Placement::new(Side::Right, Align::Start)
                            .gap(space::XS)
                            .offset(-(space::XS + 1.0)),
                    )
                    .pass_through(true)
                    .into()
            }
        });

    menu::surface(column(rows).width(width)).into()
}

/// Whether a menu level has checkbox or radio items, whose group labels
/// then line up with the item labels.
fn has_marks<Id>(entries: &[Entry<Id>]) -> bool {
    entries.iter().any(|entry| {
        matches!(
            entry.as_item().map(|item| &item.kind),
            Some(Kind::Checkbox(_) | Kind::Radio(_))
        )
    })
}

/// Whether every row of a menu level gets a leading slot, so labels line
/// up: when any item has a check mark, a radio dot or an icon.
fn reserves_slot<Id>(entries: &[Entry<Id>]) -> bool {
    has_marks(entries)
        || entries
            .iter()
            .any(|entry| entry.as_item().is_some_and(|item| item.icon.is_some()))
}

/// What fills an item's leading slot: its mark while checked, otherwise
/// its icon, otherwise nothing.
fn leading<Id>(item: &Item<Id>, slot: bool) -> Leading {
    match (&item.kind, slot) {
        (_, false) => Leading::None,
        (Kind::Checkbox(true), true) => Leading::Check,
        (Kind::Radio(true), true) => Leading::Dot,
        (_, true) => Leading::Empty,
    }
}

fn item_row<'a, Id, Message>(
    item: &'a Item<Id>,
    highlighted: bool,
    slot: bool,
    on_event: Option<&dyn Fn(Event<Id>) -> Message>,
) -> Element<'a, Message>
where
    Id: Copy + 'a,
    Message: Clone + 'a,
{
    let enabled = on_event.filter(|_| item.enabled());
    let status = match (enabled.is_some(), highlighted) {
        (false, _) => RowStatus::Disabled,
        (true, true) => RowStatus::Highlighted,
        (true, false) => RowStatus::Idle,
    };
    let leading = leading(item, slot);
    let trailing = if item.submenu().is_some() {
        Trailing::Chevron
    } else {
        Trailing::None
    };
    let parts = Parts {
        icon: item.icon,
        hint: item.shortcut.as_deref(),
        leading,
        trailing,
        ..Parts::label(&item.label)
    };
    let messages = enabled.map(|on_event| {
        (
            on_event(Event::Highlight(item.id)),
            on_event(Event::Activate(item.id)),
        )
    });
    menu::item(parts, status, item.destructive, messages)
}

#[cfg(test)]
mod tests {
    use super::*;
    use iced::keyboard::Modifiers;
    use iced::widget::text;

    const COPY: u8 = 1;
    const PASTE: u8 = 2;
    const DELETE: u8 = 3;
    const SHARE: u8 = 4;
    const EMAIL: u8 = 5;
    const LINK: u8 = 6;
    const GRID: u8 = 7;
    const SMALL: u8 = 8;
    const LARGE: u8 = 9;
    const PREVIEW: u8 = 10;

    fn state() -> State<u8> {
        State::new([
            group_label("Edit"),
            item(COPY, "Copy").shortcut("Ctrl+C"),
            item(PASTE, "Paste").disabled(true),
            item(DELETE, "Delete").destructive(true),
            separator(),
            submenu(
                SHARE,
                "Share",
                [item(EMAIL, "Email"), item(LINK, "Copy link")],
            ),
            checkbox_item(GRID, "Show grid", true),
            separator(),
            radio_item(SMALL, "Small", true),
            radio_item(LARGE, "Large", false),
            item(PREVIEW, "Preview"),
        ])
    }

    fn open() -> State<u8> {
        let mut state = state();
        let _ = state.update(Event::Open);
        state
    }

    #[test]
    fn builders_set_item_fields_and_ignore_labels() {
        let entry: Entry<u8> = item(1, "Save")
            .icon(crate::lucide!(Save))
            .shortcut("Ctrl+S")
            .disabled(true)
            .destructive(true);
        let item = entry.as_item().unwrap();
        assert_eq!(item.label, "Save");
        assert_eq!(item.icon, Some(crate::lucide!(Save)));
        assert_eq!(item.shortcut.as_deref(), Some("Ctrl+S"));
        assert!(item.disabled && item.destructive);
        assert_eq!(item.kind, Kind::Action);

        let label: Entry<u8> = group_label("Group")
            .icon(crate::lucide!(Save))
            .disabled(true);
        assert_eq!(label, Entry::Label("Group".into()));
        assert_eq!(separator::<u8>().shortcut("X"), Entry::Separator);
    }

    #[test]
    fn starts_closed_and_opens_without_a_highlight() {
        let mut state = state();
        assert!(!state.is_open());
        assert_eq!(state.depth(), 0);
        assert_eq!(state.update(Event::Open), None);
        assert!(state.is_open());
        assert_eq!(state.highlighted(), None);
        let _ = state.update(Event::Open);
        assert_eq!(state.depth(), 1);
    }

    #[test]
    fn toggle_and_close() {
        let mut state = state();
        let _ = state.update(Event::Toggle);
        assert!(state.is_open());
        let _ = state.update(Event::Toggle);
        assert!(!state.is_open());
        let _ = state.update(Event::Next);
        let _ = state.update(Event::Close);
        assert!(!state.is_open());
        assert_eq!(state.highlighted(), None);
    }

    #[test]
    fn next_skips_labels_separators_and_disabled_items_and_wraps() {
        let mut state = open();
        let mut seen = Vec::new();
        for _ in 0..8 {
            let _ = state.update(Event::Next);
            seen.push(state.highlighted().unwrap());
        }
        assert_eq!(
            seen,
            [COPY, DELETE, SHARE, GRID, SMALL, LARGE, PREVIEW, COPY]
        );
    }

    #[test]
    fn previous_wraps_backwards() {
        let mut state = open();
        let _ = state.update(Event::Previous);
        assert_eq!(state.highlighted(), Some(PREVIEW));
        let _ = state.update(Event::First);
        let _ = state.update(Event::Previous);
        assert_eq!(state.highlighted(), Some(PREVIEW));
    }

    #[test]
    fn first_and_last() {
        let mut state = open();
        let _ = state.update(Event::Last);
        assert_eq!(state.highlighted(), Some(PREVIEW));
        let _ = state.update(Event::First);
        assert_eq!(state.highlighted(), Some(COPY));
    }

    #[test]
    fn moving_opens_a_closed_menu_on_an_end() {
        let mut down = state();
        let _ = down.update(Event::Next);
        assert!(down.is_open());
        assert_eq!(down.highlighted(), Some(COPY));

        let mut up = state();
        let _ = up.update(Event::Previous);
        assert_eq!(up.highlighted(), Some(PREVIEW));

        let mut enter = state();
        assert_eq!(enter.update(Event::ActivateHighlighted), None);
        assert_eq!(enter.highlighted(), Some(COPY));
    }

    #[test]
    fn empty_and_all_disabled_menus_never_highlight() {
        let mut empty: State<u8> = State::new([]);
        let _ = empty.update(Event::Next);
        assert!(empty.is_open());
        assert_eq!(empty.highlighted(), None);
        assert_eq!(empty.update(Event::ActivateHighlighted), None);

        let mut disabled = State::new([item(1u8, "A").disabled(true), separator()]);
        let _ = disabled.update(Event::Next);
        let _ = disabled.update(Event::Last);
        assert_eq!(disabled.highlighted(), None);
        assert_eq!(disabled.update(Event::Activate(1)), None);
    }

    #[test]
    fn activating_an_action_returns_it_and_closes() {
        let mut state = open();
        assert_eq!(
            state.update(Event::Activate(COPY)),
            Some(Output::Activated(COPY))
        );
        assert!(!state.is_open());
    }

    #[test]
    fn disabled_and_unknown_items_do_nothing() {
        let mut state = open();
        assert_eq!(state.update(Event::Activate(PASTE)), None);
        assert_eq!(state.update(Event::Activate(99)), None);
        let _ = state.update(Event::Highlight(PASTE));
        assert_eq!(state.highlighted(), None);
        assert!(state.is_open());
    }

    #[test]
    fn enter_activates_the_highlighted_item() {
        let mut state = open();
        let _ = state.update(Event::Next);
        let _ = state.update(Event::Next);
        assert_eq!(
            state.update(Event::ActivateHighlighted),
            Some(Output::Activated(DELETE))
        );
        let mut nothing = open();
        assert_eq!(nothing.update(Event::ActivateHighlighted), None);
        assert!(nothing.is_open());
    }

    #[test]
    fn checkbox_items_toggle() {
        let mut state = open();
        assert!(state.is_checked(GRID));
        assert_eq!(
            state.update(Event::Activate(GRID)),
            Some(Output::Toggled(GRID, false))
        );
        assert!(!state.is_checked(GRID));
        let _ = state.update(Event::Open);
        assert_eq!(
            state.update(Event::Activate(GRID)),
            Some(Output::Toggled(GRID, true))
        );
    }

    #[test]
    fn radio_items_check_one_of_their_run() {
        let mut state = open();
        assert_eq!(
            state.update(Event::Activate(LARGE)),
            Some(Output::Selected(LARGE))
        );
        assert!(state.is_checked(LARGE));
        assert!(!state.is_checked(SMALL));
        state.set_checked(SMALL, true);
        assert!(state.is_checked(SMALL));
        assert!(!state.is_checked(LARGE));
        state.set_checked(SMALL, false);
        assert!(!state.is_checked(SMALL));
    }

    #[test]
    fn radio_group_is_the_adjacent_run() {
        let entries: Vec<Entry<u8>> = vec![
            radio_item(1, "A", false),
            separator(),
            radio_item(2, "B", false),
            radio_item(3, "C", false),
            item(4, "D"),
            radio_item(5, "E", false),
        ];
        assert_eq!(radio_group(&entries, 0), 0..1);
        assert_eq!(radio_group(&entries, 2), 2..4);
        assert_eq!(radio_group(&entries, 3), 2..4);
        assert_eq!(radio_group(&entries, 5), 5..6);
    }

    #[test]
    fn set_checked_ignores_actions_and_unknown_ids() {
        let mut state = state();
        state.set_checked(COPY, true);
        state.set_checked(99, true);
        assert!(!state.is_checked(COPY));
        assert_eq!(state, self::state());
    }

    #[test]
    fn set_disabled_drops_the_highlight() {
        let mut state = open();
        let _ = state.update(Event::Highlight(DELETE));
        state.set_disabled(DELETE, true);
        assert_eq!(state.highlighted(), None);
        assert!(state.item(DELETE).unwrap().disabled);
        state.set_disabled(PASTE, false);
        let _ = state.update(Event::First);
        let _ = state.update(Event::Next);
        assert_eq!(state.highlighted(), Some(PASTE));
    }

    #[test]
    fn disabling_an_item_in_another_submenu_keeps_the_active_highlight() {
        let mut state = State::new([
            submenu(1, "Share", [item(2, "Email")]),
            submenu(3, "Export", [item(4, "PDF")]),
        ]);
        let _ = state.update(Event::Open);
        let _ = state.update(Event::Highlight(2));

        state.set_disabled(4, true);

        assert_eq!(state.highlighted(), Some(2));
        assert_eq!(
            state.update(Event::ActivateHighlighted),
            Some(Output::Activated(2))
        );
    }

    #[test]
    fn pointer_on_a_submenu_opens_it_without_a_highlight() {
        let mut state = open();
        let _ = state.update(Event::Highlight(SHARE));
        assert_eq!(state.depth(), 2);
        assert_eq!(state.highlighted(), Some(SHARE));
        assert_eq!(state.highlighted_at(1), None);

        let _ = state.update(Event::Highlight(LINK));
        assert_eq!(state.highlighted(), Some(LINK));
        let _ = state.update(Event::Highlight(SHARE));
        assert_eq!(
            state.highlighted(),
            Some(LINK),
            "returning to the parent keeps the submenu"
        );

        let _ = state.update(Event::Highlight(COPY));
        assert_eq!(state.depth(), 1, "another item closes the submenu");
    }

    #[test]
    fn keyboard_opens_and_closes_submenus() {
        let mut state = open();
        let _ = state.update(Event::Highlight(SHARE));
        let _ = state.update(Event::CloseSubmenu);
        assert_eq!(state.depth(), 1);
        assert_eq!(state.highlighted(), Some(SHARE));

        let _ = state.update(Event::OpenSubmenu);
        assert_eq!(state.highlighted(), Some(EMAIL));
        let _ = state.update(Event::Next);
        assert_eq!(state.highlighted(), Some(LINK));
        let _ = state.update(Event::Next);
        assert_eq!(state.highlighted(), Some(EMAIL), "wraps inside the submenu");

        let _ = state.update(Event::CloseSubmenu);
        assert_eq!(state.depth(), 1);
        assert_eq!(state.highlighted(), Some(SHARE));
        let _ = state.update(Event::CloseSubmenu);
        assert!(state.is_open(), "the root menu stays open");
    }

    #[test]
    fn enter_on_a_submenu_opens_it_and_items_inside_activate() {
        let mut state = open();
        let _ = state.update(Event::Highlight(SHARE));
        let _ = state.update(Event::CloseSubmenu);
        assert_eq!(state.update(Event::ActivateHighlighted), None);
        assert_eq!(state.highlighted(), Some(EMAIL));
        assert_eq!(
            state.update(Event::ActivateHighlighted),
            Some(Output::Activated(EMAIL))
        );
        assert!(!state.is_open());
    }

    #[test]
    fn clicking_a_submenu_item_opens_it() {
        let mut state = open();
        assert_eq!(state.update(Event::Activate(SHARE)), None);
        assert_eq!(state.depth(), 2);
        assert_eq!(
            state.update(Event::Activate(LINK)),
            Some(Output::Activated(LINK))
        );
    }

    #[test]
    fn open_submenu_ignores_plain_items() {
        let mut state = open();
        let _ = state.update(Event::First);
        let _ = state.update(Event::OpenSubmenu);
        assert_eq!(state.depth(), 1);
    }

    #[test]
    fn moving_in_the_parent_closes_the_submenu() {
        let mut state = open();
        let _ = state.update(Event::Highlight(SHARE));
        let _ = state.update(Event::Next);
        assert_eq!(state.depth(), 1);
        assert_eq!(state.highlighted(), Some(GRID));
    }

    #[test]
    fn typeahead_jumps_to_matching_labels_and_cycles() {
        let mut state = open();
        let _ = state.update(Event::Typeahead('s'));
        assert_eq!(state.highlighted(), Some(SHARE));
        let _ = state.update(Event::Typeahead('S'));
        assert_eq!(state.highlighted(), Some(GRID));
        let _ = state.update(Event::Typeahead('s'));
        assert_eq!(state.highlighted(), Some(SMALL));
        let _ = state.update(Event::Typeahead('s'));
        assert_eq!(state.highlighted(), Some(SHARE));
        let _ = state.update(Event::Typeahead('p'));
        assert_eq!(
            state.highlighted(),
            Some(PREVIEW),
            "skips the disabled Paste"
        );
        let _ = state.update(Event::Typeahead('z'));
        assert_eq!(state.highlighted(), Some(PREVIEW));
    }

    #[test]
    fn typeahead_starts_at_the_first_match() {
        let mut state = open();
        let _ = state.update(Event::Typeahead('c'));
        assert_eq!(state.highlighted(), Some(COPY));
    }

    fn key(key: Key, modifiers: Modifiers) -> keys::Event {
        keys::Event { key, modifiers }
    }

    fn character(c: &str) -> Key {
        Key::Character(c.into())
    }

    #[test]
    fn typeahead_accepts_plain_letters_and_digits_only() {
        let none = Modifiers::empty();
        assert_eq!(typeahead(&key(character("a"), none)), Some('a'));
        assert_eq!(typeahead(&key(character("A"), Modifiers::SHIFT)), Some('A'));
        assert_eq!(typeahead(&key(character("7"), none)), Some('7'));
        assert_eq!(typeahead(&key(character("a"), Modifiers::CTRL)), None);
        assert_eq!(typeahead(&key(character("a"), Modifiers::ALT)), None);
        assert_eq!(typeahead(&key(character(" "), none)), None);
        assert_eq!(typeahead(&key(character("/"), none)), None);
        assert_eq!(typeahead(&key(character("ab"), none)), None);
        assert_eq!(typeahead(&key(Key::Named(Named::Enter), none)), None);
    }

    #[test]
    fn key_event_uses_the_keymap_then_typeahead() {
        let keymap = default_keymap();
        let closed = state();
        let down = key(Key::Named(Named::ArrowDown), Modifiers::empty());
        let enter = key(Key::Named(Named::Enter), Modifiers::empty());
        let letter = key(character("d"), Modifiers::empty());
        assert_eq!(
            closed.key_event(&keymap, &down),
            None,
            "closed menus claim no keys"
        );
        assert_eq!(closed.key_event(&keymap, &enter), None);
        assert_eq!(closed.key_event(&keymap, &letter), None);

        let bound = default_keymap().bind(Chord::named(Named::F2), Action::Open);
        let f2 = key(Key::Named(Named::F2), Modifiers::empty());
        assert_eq!(closed.key_event(&bound, &f2), Some(Event::First));

        let open = open();
        assert_eq!(
            open.key_event(&keymap, &letter),
            Some(Event::Typeahead('d'))
        );
        let escape = key(Key::Named(Named::Escape), Modifiers::empty());
        assert_eq!(open.key_event(&keymap, &escape), Some(Event::Close));
    }

    fn press(keymap: &Keymap<Action>, chord: &str) -> Option<Action> {
        let chord: Chord = chord.parse().unwrap();
        keymap.resolve(chord.key(), chord.modifiers())
    }

    #[test]
    fn default_keymap_covers_every_action() {
        let keymap = default_keymap();
        assert_eq!(press(&keymap, "ArrowDown"), Some(Action::Next));
        assert_eq!(press(&keymap, "ArrowUp"), Some(Action::Previous));
        assert_eq!(press(&keymap, "Home"), Some(Action::First));
        assert_eq!(press(&keymap, "End"), Some(Action::Last));
        assert_eq!(press(&keymap, "Enter"), Some(Action::Activate));
        assert_eq!(press(&keymap, "Space"), Some(Action::Activate));
        assert_eq!(press(&keymap, "Escape"), Some(Action::Close));
        assert_eq!(press(&keymap, "ArrowRight"), Some(Action::OpenSubmenu));
        assert_eq!(press(&keymap, "ArrowLeft"), Some(Action::CloseSubmenu));
        assert_eq!(press(&keymap, "Tab"), None);
        for action in <Action as keys::Action>::ALL {
            let bound = !keymap.chords(action).is_empty();
            assert_eq!(bound, *action != Action::Open, "{action:?}");
        }
        assert_eq!(Keymap::<Action>::defaults(), keymap);
    }

    #[test]
    fn keymap_overrides_and_unbinding() {
        let keymap = default_keymap()
            .unbind(&"Space".parse().unwrap())
            .bind("J".parse().unwrap(), Action::Next);
        assert_eq!(press(&keymap, "Space"), None);
        assert_eq!(press(&keymap, "j"), Some(Action::Next));
        assert_eq!(press(&keymap, "Enter"), Some(Action::Activate));
    }

    #[test]
    fn a_closed_menu_only_answers_open() {
        let closed = state();
        assert_eq!(Action::Open.event(&closed), Some(Event::First));
        for &action in <Action as keys::Action>::ALL {
            if action != Action::Open {
                assert_eq!(action.event(&closed), None, "{action:?}");
            }
        }
    }

    #[test]
    fn an_open_menu_maps_every_action_but_open() {
        let open = open();
        assert_eq!(Action::Open.event(&open), None);
        assert_eq!(Action::Next.event(&open), Some(Event::Next));
        assert_eq!(Action::Previous.event(&open), Some(Event::Previous));
        assert_eq!(Action::First.event(&open), Some(Event::First));
        assert_eq!(Action::Last.event(&open), Some(Event::Last));
        assert_eq!(
            Action::Activate.event(&open),
            Some(Event::ActivateHighlighted)
        );
        assert_eq!(Action::Close.event(&open), Some(Event::Close));
        assert_eq!(Action::OpenSubmenu.event(&open), Some(Event::OpenSubmenu));
        assert_eq!(Action::CloseSubmenu.event(&open), Some(Event::CloseSubmenu));
    }

    #[test]
    fn close_in_a_submenu_closes_only_the_submenu() {
        let mut state = open();
        let _ = state.update(Event::Highlight(SHARE));
        assert_eq!(Action::Close.event(&state), Some(Event::CloseSubmenu));
        let _ = state.update(Event::CloseSubmenu);
        assert_eq!(Action::Close.event(&state), Some(Event::Close));
    }

    #[test]
    fn every_action_has_a_name_and_a_sentence() {
        for &action in <Action as keys::Action>::ALL {
            assert!(!keys::Action::name(action).is_empty());
            assert!(keys::Action::description(action).ends_with('.'));
        }
    }

    #[test]
    fn builder_defaults() {
        let state = state();
        let menu: DropdownMenu<'_, u8, ()> = dropdown_menu(&state, text("Open"));
        assert_eq!(menu.placement, Placement::new(Side::Bottom, Align::Start));
        assert_eq!(menu.width, WIDTH);
        assert_eq!(menu.keymap, default_keymap());
        assert!(menu.on_event.is_none());

        let menu: DropdownMenu<'_, u8, ()> = dropdown_menu(&state, text("Open"))
            .side(Side::Top)
            .align(Align::End)
            .gap(8.0)
            .width(300.0)
            .keymap(Keymap::new())
            .on_event(|_| ());
        assert_eq!(menu.placement.side, Side::Top);
        assert_eq!(menu.placement.align, Align::End);
        assert_eq!(menu.placement.gap, 8.0);
        assert_eq!(menu.width, 300.0);
        assert!(menu.keymap.is_empty());
        assert!(menu.on_event.is_some());
    }

    #[test]
    fn a_slot_is_reserved_for_marks_or_icons() {
        let plain: Vec<Entry<u8>> = vec![item(COPY, "Copy"), separator()];
        assert!(!reserves_slot(&plain) && !has_marks(&plain));

        let icons: Vec<Entry<u8>> = vec![
            item(COPY, "Copy").icon(crate::lucide!(Copy)),
            item(PASTE, "Paste"),
        ];
        assert!(reserves_slot(&icons) && !has_marks(&icons));

        let marks: Vec<Entry<u8>> = vec![item(COPY, "Copy"), radio_item(SMALL, "Small", false)];
        assert!(reserves_slot(&marks) && has_marks(&marks));
    }

    #[test]
    fn the_slot_holds_the_mark_while_checked_and_the_icon_otherwise() {
        let entries: Vec<Entry<u8>> = vec![
            checkbox_item(GRID, "Grid", true).icon(crate::lucide!(Grid3x3)),
            checkbox_item(GRID, "Grid", false).icon(crate::lucide!(Grid3x3)),
            radio_item(SMALL, "Small", true),
            radio_item(LARGE, "Large", false),
            item(COPY, "Copy").icon(crate::lucide!(Copy)),
        ];
        let slots: Vec<Leading> = entries
            .iter()
            .filter_map(Entry::as_item)
            .map(|item| leading(item, true))
            .collect();
        assert_eq!(
            slots,
            [
                Leading::Check,
                Leading::Empty,
                Leading::Dot,
                Leading::Empty,
                Leading::Empty
            ]
        );
        let unslotted = entries.iter().filter_map(Entry::as_item);
        assert!(
            unslotted
                .map(|item| leading(item, false))
                .all(|l| l == Leading::None)
        );
    }
}