Skip to main content

iced_cube/overlay/
dropdown_menu.rs

1//! A menu of actions that opens from a trigger button.
2//!
3//! [`State`] owns the entries (items, checkbox and radio items, submenus,
4//! group labels and separators), whether the menu is open, and which row is
5//! highlighted at each open level. Its [`update`](State::update) handles
6//! pointer and keyboard navigation and returns an [`Output`] when an item
7//! is chosen. The context menu uses the same model and rows.
8//!
9//! An open menu resolves its own keys through a [`Keymap`] of [`Action`]s,
10//! so the app needs no subscription while it is open; see
11//! [`default_keymap`]. A closed menu claims no keys: it opens from its
12//! trigger, or from a chord the app binds to [`Action::Open`] and routes
13//! through [`State::key_event`]. Rows are drawn in the shared
14//! [menu look](crate::overlay::menu).
15
16use std::rc::Rc;
17
18use iced::Element;
19use iced::keyboard::{Key, key::Named};
20use iced::widget::column;
21
22use crate::icon::Glyph;
23use crate::keys::{self, Chord, Keymap};
24use crate::overlay::anchored::{Align, Placement, Side, anchored};
25use crate::overlay::menu::{self, Leading, Parts, RowStatus, Trailing};
26use crate::overlay::scrolling::scrolling;
27use crate::theme::space;
28
29/// Default menu width in logical pixels.
30pub const WIDTH: f32 = 224.0;
31
32/// What an item does when chosen.
33#[derive(Debug, Clone, PartialEq)]
34pub enum Kind<Id> {
35    /// A plain action.
36    Action,
37    /// A check mark that toggles.
38    Checkbox(bool),
39    /// One choice among the radio items next to it. A run of adjacent
40    /// radio items forms one group.
41    Radio(bool),
42    /// Opens a nested menu.
43    Submenu(Vec<Entry<Id>>),
44}
45
46/// A row that can be highlighted and chosen.
47#[derive(Debug, Clone, PartialEq)]
48pub struct Item<Id> {
49    pub id: Id,
50    pub label: String,
51    pub icon: Option<Glyph>,
52    /// A hint such as `Ctrl+S`, shown on the right. It does not bind the keys.
53    pub shortcut: Option<String>,
54    pub disabled: bool,
55    pub destructive: bool,
56    pub kind: Kind<Id>,
57}
58
59impl<Id> Item<Id> {
60    fn enabled(&self) -> bool {
61        !self.disabled
62    }
63
64    fn submenu(&self) -> Option<&[Entry<Id>]> {
65        match &self.kind {
66            Kind::Submenu(entries) => Some(entries),
67            _ => None,
68        }
69    }
70}
71
72/// One line of a menu.
73#[derive(Debug, Clone, PartialEq)]
74pub enum Entry<Id> {
75    Item(Item<Id>),
76    /// A heading for the items below it.
77    Label(String),
78    Separator,
79}
80
81fn new_item<Id>(id: Id, label: impl Into<String>, kind: Kind<Id>) -> Entry<Id> {
82    Entry::Item(Item {
83        id,
84        label: label.into(),
85        icon: None,
86        shortcut: None,
87        disabled: false,
88        destructive: false,
89        kind,
90    })
91}
92
93/// An action item.
94pub fn item<Id>(id: Id, label: impl Into<String>) -> Entry<Id> {
95    new_item(id, label, Kind::Action)
96}
97
98/// An item with a check mark that toggles when chosen.
99pub fn checkbox_item<Id>(id: Id, label: impl Into<String>, checked: bool) -> Entry<Id> {
100    new_item(id, label, Kind::Checkbox(checked))
101}
102
103/// An item that checks itself and unchecks the radio items next to it.
104pub fn radio_item<Id>(id: Id, label: impl Into<String>, checked: bool) -> Entry<Id> {
105    new_item(id, label, Kind::Radio(checked))
106}
107
108/// An item that opens `entries` in a nested menu to its right.
109pub fn submenu<Id>(
110    id: Id,
111    label: impl Into<String>,
112    entries: impl IntoIterator<Item = Entry<Id>>,
113) -> Entry<Id> {
114    new_item(id, label, Kind::Submenu(entries.into_iter().collect()))
115}
116
117/// A heading for a group of items.
118pub fn group_label<Id>(label: impl Into<String>) -> Entry<Id> {
119    Entry::Label(label.into())
120}
121
122/// A line between groups.
123pub fn separator<Id>() -> Entry<Id> {
124    Entry::Separator
125}
126
127impl<Id> Entry<Id> {
128    /// Adds a leading Lucide icon. Ignored on labels and separators, as are
129    /// the other item settings.
130    pub fn icon(self, glyph: Glyph) -> Self {
131        self.with_item(|item| item.icon = Some(glyph))
132    }
133
134    pub fn shortcut(self, hint: impl Into<String>) -> Self {
135        let hint = hint.into();
136        self.with_item(|item| item.shortcut = Some(hint))
137    }
138
139    pub fn disabled(self, disabled: bool) -> Self {
140        self.with_item(|item| item.disabled = disabled)
141    }
142
143    /// Draws the item in the destructive colour, for actions such as delete.
144    pub fn destructive(self, destructive: bool) -> Self {
145        self.with_item(|item| item.destructive = destructive)
146    }
147
148    pub fn as_item(&self) -> Option<&Item<Id>> {
149        match self {
150            Entry::Item(item) => Some(item),
151            _ => None,
152        }
153    }
154
155    fn with_item(mut self, change: impl FnOnce(&mut Item<Id>)) -> Self {
156        if let Entry::Item(item) = &mut self {
157            change(item);
158        }
159        self
160    }
161
162    fn enabled_item(&self) -> Option<&Item<Id>> {
163        self.as_item().filter(|item| item.enabled())
164    }
165}
166
167/// Changes to a menu.
168#[derive(Debug, Clone, Copy, PartialEq, Eq)]
169pub enum Event<Id> {
170    /// Opens with nothing highlighted, as a pointer does.
171    Open,
172    Close,
173    Toggle,
174    /// The pointer moved onto an item. A submenu item opens its submenu.
175    Highlight(Id),
176    /// Highlights the next enabled item, wrapping. Opens a closed menu on
177    /// the first item.
178    Next,
179    /// Highlights the previous enabled item, wrapping. Opens a closed menu
180    /// on the last item.
181    Previous,
182    First,
183    Last,
184    /// Opens the highlighted submenu and highlights its first item.
185    OpenSubmenu,
186    /// Closes the innermost submenu.
187    CloseSubmenu,
188    /// Chooses an item, as a click does.
189    Activate(Id),
190    /// Chooses the highlighted item, or opens its submenu. Opens a closed
191    /// menu on the first item.
192    ActivateHighlighted,
193    /// Highlights the next item whose label starts with this character.
194    Typeahead(char),
195}
196
197/// What choosing an item did. The menu closes afterwards.
198#[derive(Debug, Clone, Copy, PartialEq, Eq)]
199pub enum Output<Id> {
200    /// An action item was chosen.
201    Activated(Id),
202    /// A checkbox item changed to this value.
203    Toggled(Id, bool),
204    /// A radio item was chosen.
205    Selected(Id),
206}
207
208/// The entries of a menu, whether it is open and what is highlighted.
209#[derive(Debug, Clone, PartialEq)]
210pub struct State<Id> {
211    entries: Vec<Entry<Id>>,
212    /// One slot per open level: the root menu, then each open submenu.
213    levels: Vec<Option<usize>>,
214}
215
216impl<Id: Copy + PartialEq> State<Id> {
217    /// A closed menu. Item ids must be unique across submenus.
218    pub fn new(entries: impl IntoIterator<Item = Entry<Id>>) -> Self {
219        Self {
220            entries: entries.into_iter().collect(),
221            levels: Vec::new(),
222        }
223    }
224
225    pub fn entries(&self) -> &[Entry<Id>] {
226        &self.entries
227    }
228
229    pub fn is_open(&self) -> bool {
230        !self.levels.is_empty()
231    }
232
233    /// How many menus are open: 0 when closed, 1 for the root menu, plus
234    /// one per open submenu.
235    pub fn depth(&self) -> usize {
236        self.levels.len()
237    }
238
239    /// The highlighted item in the menu that has keyboard focus.
240    pub fn highlighted(&self) -> Option<Id> {
241        let path = self.active_path()?;
242        self.item_at(&path).map(|item| item.id)
243    }
244
245    /// The highlighted row index in the menu `depth` levels deep.
246    pub fn highlighted_at(&self, depth: usize) -> Option<usize> {
247        self.levels.get(depth).copied().flatten()
248    }
249
250    pub fn item(&self, id: Id) -> Option<&Item<Id>> {
251        let path = path_of(&self.entries, id)?;
252        self.item_at(&path)
253    }
254
255    /// Whether a checkbox or radio item is checked.
256    pub fn is_checked(&self, id: Id) -> bool {
257        self.item(id)
258            .is_some_and(|item| matches!(item.kind, Kind::Checkbox(true) | Kind::Radio(true)))
259    }
260
261    /// Checks or unchecks an item. Checking a radio item unchecks its group.
262    pub fn set_checked(&mut self, id: Id, checked: bool) {
263        let Some(path) = path_of(&self.entries, id) else {
264            return;
265        };
266        let Some((&index, parents)) = path.split_last() else {
267            return;
268        };
269        let Some(siblings) = entries_mut(&mut self.entries, parents) else {
270            return;
271        };
272        let is_radio = matches!(
273            siblings.get(index),
274            Some(Entry::Item(Item {
275                kind: Kind::Radio(_),
276                ..
277            }))
278        );
279        if is_radio && checked {
280            for sibling in radio_group(siblings, index) {
281                if let Some(Entry::Item(item)) = siblings.get_mut(sibling) {
282                    item.kind = Kind::Radio(false);
283                }
284            }
285        }
286        if let Some(Entry::Item(item)) = siblings.get_mut(index) {
287            match &mut item.kind {
288                Kind::Checkbox(value) | Kind::Radio(value) => *value = checked,
289                Kind::Action | Kind::Submenu(_) => {}
290            }
291        }
292    }
293
294    /// Enables or disables an item. A disabled item loses its highlight.
295    pub fn set_disabled(&mut self, id: Id, disabled: bool) {
296        let Some(path) = path_of(&self.entries, id) else {
297            return;
298        };
299        let Some((&index, parents)) = path.split_last() else {
300            return;
301        };
302        if let Some(Entry::Item(item)) =
303            entries_mut(&mut self.entries, parents).and_then(|entries| entries.get_mut(index))
304        {
305            item.disabled = disabled;
306        }
307        let highlighted = self
308            .levels
309            .iter()
310            .take(path.len())
311            .copied()
312            .eq(path.iter().copied().map(Some));
313        if disabled && highlighted {
314            self.levels.truncate(parents.len() + 1);
315            if let Some(level) = self.levels.get_mut(parents.len()) {
316                *level = None;
317            }
318        }
319    }
320
321    /// Applies an event and returns what was chosen, if anything.
322    pub fn update(&mut self, event: Event<Id>) -> Option<Output<Id>> {
323        match event {
324            Event::Open => self.open(),
325            Event::Close => self.levels.clear(),
326            Event::Toggle if self.is_open() => self.levels.clear(),
327            Event::Toggle => self.open(),
328            Event::Highlight(id) => self.highlight(id),
329            Event::Next | Event::First if !self.is_open() => self.open_on(Edge::First),
330            Event::Previous | Event::Last if !self.is_open() => self.open_on(Edge::Last),
331            Event::Next => self.step(Step::Next),
332            Event::Previous => self.step(Step::Previous),
333            Event::First => self.step(Step::Edge(Edge::First)),
334            Event::Last => self.step(Step::Edge(Edge::Last)),
335            Event::OpenSubmenu => self.open_submenu(),
336            Event::CloseSubmenu => self.close_submenu(),
337            Event::Typeahead(character) => self.typeahead(character),
338            Event::Activate(id) => return self.activate(id),
339            Event::ActivateHighlighted if !self.is_open() => self.open_on(Edge::First),
340            Event::ActivateHighlighted => return self.activate_highlighted(),
341        }
342        None
343    }
344
345    /// Turns a key press into an event: first through `keymap`, then, while
346    /// the menu is open, as typeahead for a letter or digit. An open menu
347    /// does this itself; an app calls it for a closed menu's
348    /// [`Action::Open`] chord.
349    pub fn key_event(&self, keymap: &Keymap<Action>, key: &keys::Event) -> Option<Event<Id>> {
350        if let Some(action) = keymap.resolve_event(key) {
351            return action.event(self);
352        }
353        if !self.is_open() {
354            return None;
355        }
356        typeahead(key).map(Event::Typeahead)
357    }
358
359    fn open(&mut self) {
360        if !self.is_open() {
361            self.levels.push(None);
362        }
363    }
364
365    fn open_on(&mut self, edge: Edge) {
366        self.levels = vec![edge.find(&self.entries)];
367    }
368
369    /// The deepest level with a highlight, or the root.
370    fn active_depth(&self) -> usize {
371        self.levels.iter().rposition(Option::is_some).unwrap_or(0)
372    }
373
374    fn active_path(&self) -> Option<Vec<usize>> {
375        let depth = self.active_depth();
376        self.levels.get(..=depth)?.iter().copied().collect()
377    }
378
379    fn entries_at(&self, depth: usize) -> Option<&[Entry<Id>]> {
380        let mut entries = self.entries.as_slice();
381        for level in self.levels.get(..depth)? {
382            entries = entries.get((*level)?)?.as_item()?.submenu()?;
383        }
384        Some(entries)
385    }
386
387    fn item_at(&self, path: &[usize]) -> Option<&Item<Id>> {
388        let (&index, parents) = path.split_last()?;
389        let mut entries = self.entries.as_slice();
390        for &parent in parents {
391            entries = entries.get(parent)?.as_item()?.submenu()?;
392        }
393        entries.get(index)?.as_item()
394    }
395
396    /// Moves the highlight in the active menu, closing deeper submenus.
397    fn step(&mut self, step: Step) {
398        let depth = self.active_depth();
399        let Some(entries) = self.entries_at(depth) else {
400            return;
401        };
402        let current = self.highlighted_at(depth);
403        let target = match step {
404            Step::Next => next_enabled(entries, current, true, |_| true),
405            Step::Previous => next_enabled(entries, current, false, |_| true),
406            Step::Edge(edge) => edge.find(entries),
407        };
408        self.set_highlight(depth, target);
409    }
410
411    fn typeahead(&mut self, character: char) {
412        let depth = self.active_depth();
413        let Some(entries) = self.entries_at(depth) else {
414            return;
415        };
416        let wanted = character.to_lowercase().collect::<String>();
417        let target = next_enabled(entries, self.highlighted_at(depth), true, |item| {
418            item.label.to_lowercase().starts_with(&wanted)
419        });
420        self.set_highlight(depth, target);
421    }
422
423    fn set_highlight(&mut self, depth: usize, target: Option<usize>) {
424        if target.is_none() {
425            return;
426        }
427        self.levels.truncate(depth + 1);
428        if let Some(level) = self.levels.get_mut(depth) {
429            *level = target;
430        }
431    }
432
433    fn highlight(&mut self, id: Id) {
434        let Some(path) = path_of(&self.entries, id) else {
435            return;
436        };
437        let Some(item) = self.item_at(&path).filter(|item| item.enabled()) else {
438            return;
439        };
440        let opens = item.submenu().is_some();
441        let target = some(&path);
442        if self.levels.len() > target.len() && self.levels.starts_with(&target) {
443            return;
444        }
445        self.levels = target;
446        if opens {
447            self.levels.push(None);
448        }
449    }
450
451    fn open_submenu(&mut self) {
452        let depth = self.active_depth();
453        let Some(path) = self.active_path() else {
454            return;
455        };
456        let Some(children) = self
457            .item_at(&path)
458            .filter(|item| item.enabled())
459            .and_then(Item::submenu)
460        else {
461            return;
462        };
463        let first = Edge::First.find(children);
464        self.levels.truncate(depth + 1);
465        self.levels.push(first);
466    }
467
468    fn close_submenu(&mut self) {
469        if self.levels.len() <= 1 {
470            return;
471        }
472        let keep = self.active_depth().max(1);
473        self.levels.truncate(keep);
474    }
475
476    fn activate(&mut self, id: Id) -> Option<Output<Id>> {
477        let path = path_of(&self.entries, id)?;
478        let item = self.item_at(&path).filter(|item| item.enabled())?;
479        if item.submenu().is_some() {
480            self.highlight(id);
481            return None;
482        }
483        self.choose(id)
484    }
485
486    fn activate_highlighted(&mut self) -> Option<Output<Id>> {
487        let path = self.active_path()?;
488        let item = self.item_at(&path).filter(|item| item.enabled())?;
489        if item.submenu().is_some() {
490            self.open_submenu();
491            return None;
492        }
493        let id = item.id;
494        self.choose(id)
495    }
496
497    /// Applies a chosen item and closes the menu.
498    fn choose(&mut self, id: Id) -> Option<Output<Id>> {
499        let output = match self.item(id)?.kind {
500            Kind::Action => Output::Activated(id),
501            Kind::Checkbox(checked) => {
502                self.set_checked(id, !checked);
503                Output::Toggled(id, !checked)
504            }
505            Kind::Radio(_) => {
506                self.set_checked(id, true);
507                Output::Selected(id)
508            }
509            Kind::Submenu(_) => return None,
510        };
511        self.levels.clear();
512        Some(output)
513    }
514}
515
516fn some(path: &[usize]) -> Vec<Option<usize>> {
517    path.iter().copied().map(Some).collect()
518}
519
520#[derive(Debug, Clone, Copy)]
521enum Edge {
522    First,
523    Last,
524}
525
526impl Edge {
527    fn find<Id>(self, entries: &[Entry<Id>]) -> Option<usize> {
528        let enabled = |(_, entry): &(usize, &Entry<Id>)| entry.enabled_item().is_some();
529        match self {
530            Edge::First => entries.iter().enumerate().find(enabled),
531            Edge::Last => entries.iter().enumerate().rev().find(enabled),
532        }
533        .map(|(index, _)| index)
534    }
535}
536
537#[derive(Debug, Clone, Copy)]
538enum Step {
539    Next,
540    Previous,
541    Edge(Edge),
542}
543
544/// The nearest enabled item after (or before) `from` that `accept`s,
545/// wrapping round. With nothing highlighted, the search starts at an end.
546fn next_enabled<Id>(
547    entries: &[Entry<Id>],
548    from: Option<usize>,
549    forward: bool,
550    accept: impl Fn(&Item<Id>) -> bool,
551) -> Option<usize> {
552    let len = entries.len();
553    if len == 0 {
554        return None;
555    }
556    let start = from.unwrap_or(if forward { len - 1 } else { 0 });
557    let offset = if forward { 1 } else { len - 1 };
558    (1..=len)
559        .map(|n| (start + offset * n) % len)
560        .find(|&index| {
561            entries
562                .get(index)
563                .and_then(Entry::enabled_item)
564                .is_some_and(&accept)
565        })
566}
567
568/// The index path to an item, through submenus.
569fn path_of<Id: Copy + PartialEq>(entries: &[Entry<Id>], id: Id) -> Option<Vec<usize>> {
570    entries.iter().enumerate().find_map(|(index, entry)| {
571        let item = entry.as_item()?;
572        if item.id == id {
573            return Some(vec![index]);
574        }
575        let mut path = path_of(item.submenu()?, id)?;
576        path.insert(0, index);
577        Some(path)
578    })
579}
580
581fn entries_mut<'a, Id>(
582    entries: &'a mut Vec<Entry<Id>>,
583    parents: &[usize],
584) -> Option<&'a mut Vec<Entry<Id>>> {
585    let Some((&first, rest)) = parents.split_first() else {
586        return Some(entries);
587    };
588    match entries.get_mut(first)? {
589        Entry::Item(Item {
590            kind: Kind::Submenu(children),
591            ..
592        }) => entries_mut(children, rest),
593        _ => None,
594    }
595}
596
597/// The indices of the run of radio items around `index`.
598fn radio_group<Id>(entries: &[Entry<Id>], index: usize) -> std::ops::Range<usize> {
599    let is_radio = |entry: &Entry<Id>| {
600        matches!(
601            entry,
602            Entry::Item(Item {
603                kind: Kind::Radio(_),
604                ..
605            })
606        )
607    };
608    let start = entries
609        .get(..index)
610        .and_then(|before| before.iter().rposition(|entry| !is_radio(entry)))
611        .map_or(0, |position| position + 1);
612    let end = entries
613        .get(index..)
614        .and_then(|after| after.iter().position(|entry| !is_radio(entry)))
615        .map_or(entries.len(), |position| index + position);
616    start..end
617}
618
619/// The character a key press types into the menu's typeahead: a letter or
620/// digit pressed without Ctrl, Alt or the logo key.
621pub fn typeahead(key: &keys::Event) -> Option<char> {
622    let modifiers = key.modifiers;
623    if modifiers.control() || modifiers.alt() || modifiers.logo() {
624        return None;
625    }
626    let Key::Character(text) = &key.key else {
627        return None;
628    };
629    let mut chars = text.chars();
630    let character = chars.next()?;
631    if chars.next().is_some() || !character.is_alphanumeric() {
632        return None;
633    }
634    Some(character)
635}
636
637/// What a menu keyboard shortcut does.
638#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
639pub enum Action {
640    /// Opens a closed menu on its first item. Not bound by default.
641    Open,
642    Next,
643    Previous,
644    First,
645    Last,
646    Activate,
647    Close,
648    OpenSubmenu,
649    CloseSubmenu,
650}
651
652impl Action {
653    /// The [`Event`] this action sends to `state`, or `None` when it does
654    /// nothing: only [`Action::Open`] works while the menu is closed, and
655    /// only the others while it is open. [`Action::Close`] closes just the
656    /// innermost submenu when one is open.
657    pub fn event<Id: Copy + PartialEq>(self, state: &State<Id>) -> Option<Event<Id>> {
658        if !state.is_open() {
659            return (self == Action::Open).then_some(Event::First);
660        }
661        match self {
662            Action::Open => None,
663            Action::Next => Some(Event::Next),
664            Action::Previous => Some(Event::Previous),
665            Action::First => Some(Event::First),
666            Action::Last => Some(Event::Last),
667            Action::Activate => Some(Event::ActivateHighlighted),
668            Action::Close if state.depth() > 1 => Some(Event::CloseSubmenu),
669            Action::Close => Some(Event::Close),
670            Action::OpenSubmenu => Some(Event::OpenSubmenu),
671            Action::CloseSubmenu => Some(Event::CloseSubmenu),
672        }
673    }
674}
675
676impl keys::Action for Action {
677    const ALL: &'static [Self] = &[
678        Action::Open,
679        Action::Next,
680        Action::Previous,
681        Action::First,
682        Action::Last,
683        Action::Activate,
684        Action::Close,
685        Action::OpenSubmenu,
686        Action::CloseSubmenu,
687    ];
688
689    fn defaults() -> Keymap<Self> {
690        default_keymap()
691    }
692
693    fn name(self) -> &'static str {
694        match self {
695            Action::Open => "Open",
696            Action::Next => "Next",
697            Action::Previous => "Previous",
698            Action::First => "First",
699            Action::Last => "Last",
700            Action::Activate => "Activate",
701            Action::Close => "Close",
702            Action::OpenSubmenu => "OpenSubmenu",
703            Action::CloseSubmenu => "CloseSubmenu",
704        }
705    }
706
707    fn description(self) -> &'static str {
708        match self {
709            Action::Open => "Opens the menu on its first item.",
710            Action::Next => "Highlights the next enabled item, wrapping.",
711            Action::Previous => "Highlights the previous enabled item, wrapping.",
712            Action::First => "Highlights the first enabled item.",
713            Action::Last => "Highlights the last enabled item.",
714            Action::Activate => "Chooses the highlighted item or opens its submenu.",
715            Action::Close => "Closes the innermost submenu, or the menu.",
716            Action::OpenSubmenu => "Opens the highlighted submenu.",
717            Action::CloseSubmenu => "Closes the innermost submenu.",
718        }
719    }
720}
721
722/// The default menu shortcuts:
723///
724/// | Keys | Action |
725/// | --- | --- |
726/// | `ArrowDown` | [`Action::Next`] |
727/// | `ArrowUp` | [`Action::Previous`] |
728/// | `Home` | [`Action::First`] |
729/// | `End` | [`Action::Last`] |
730/// | `Enter`, `Space` | [`Action::Activate`] |
731/// | `Escape` | [`Action::Close`] |
732/// | `ArrowRight` | [`Action::OpenSubmenu`] |
733/// | `ArrowLeft` | [`Action::CloseSubmenu`] |
734///
735/// Letters and digits jump to the next item starting with them. The open
736/// menu handles all of these itself, before anything underneath sees
737/// them; pass a changed keymap with [`DropdownMenu::keymap`]. A closed
738/// menu claims none of them. [`Action::Open`] has no default chord
739/// because a menu usually belongs to one button; bind one when a menu
740/// deserves an app-wide shortcut.
741pub fn default_keymap() -> Keymap<Action> {
742    Keymap::new()
743        .bind(Chord::named(Named::ArrowDown), Action::Next)
744        .bind(Chord::named(Named::ArrowUp), Action::Previous)
745        .bind(Chord::named(Named::Home), Action::First)
746        .bind(Chord::named(Named::End), Action::Last)
747        .bind(Chord::named(Named::Enter), Action::Activate)
748        .bind(Chord::named(Named::Space), Action::Activate)
749        .bind(Chord::named(Named::Escape), Action::Close)
750        .bind(Chord::named(Named::ArrowRight), Action::OpenSubmenu)
751        .bind(Chord::named(Named::ArrowLeft), Action::CloseSubmenu)
752}
753
754/// A dropdown menu builder. Convert it into an [`Element`] to render.
755pub struct DropdownMenu<'a, Id, Message> {
756    state: &'a State<Id>,
757    trigger: Element<'a, Message>,
758    placement: Placement,
759    width: f32,
760    keymap: Keymap<Action>,
761    on_event: Option<Box<dyn Fn(Event<Id>) -> Message + 'a>>,
762}
763
764impl<Id: std::fmt::Debug, Message> std::fmt::Debug for DropdownMenu<'_, Id, Message> {
765    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
766        f.debug_struct("DropdownMenu")
767            .field("state", self.state)
768            .field("placement", &self.placement)
769            .field("width", &self.width)
770            .field("keymap", &self.keymap)
771            .finish_non_exhaustive()
772    }
773}
774
775/// Renders `trigger` with the menu of `state` below it while it is open.
776///
777/// The trigger opens the menu itself, usually a button whose message
778/// carries [`Event::Toggle`]. Without [`on_event`](DropdownMenu::on_event)
779/// every item renders disabled.
780pub fn dropdown_menu<'a, Id, Message>(
781    state: &'a State<Id>,
782    trigger: impl Into<Element<'a, Message>>,
783) -> DropdownMenu<'a, Id, Message> {
784    DropdownMenu {
785        state,
786        trigger: trigger.into(),
787        placement: Placement::new(Side::Bottom, Align::Start),
788        width: WIDTH,
789        keymap: default_keymap(),
790        on_event: None,
791    }
792}
793
794impl<'a, Id, Message> DropdownMenu<'a, Id, Message> {
795    /// Which side of the trigger the menu opens on. Defaults to the bottom.
796    pub fn side(mut self, side: Side) -> Self {
797        self.placement.side = side;
798        self
799    }
800
801    /// Defaults to the start, lining the menu up with the trigger's left edge.
802    pub fn align(mut self, align: Align) -> Self {
803        self.placement.align = align;
804        self
805    }
806
807    /// Space between the trigger and the menu.
808    pub fn gap(mut self, gap: f32) -> Self {
809        self.placement.gap = gap;
810        self
811    }
812
813    /// Width of the menu and its submenus.
814    pub fn width(mut self, width: f32) -> Self {
815        self.width = width;
816        self
817    }
818
819    /// Replaces the [`default_keymap`] the open menu resolves its keys with.
820    pub fn keymap(mut self, keymap: Keymap<Action>) -> Self {
821        self.keymap = keymap;
822        self
823    }
824
825    pub fn on_event(mut self, on_event: impl Fn(Event<Id>) -> Message + 'a) -> Self {
826        self.on_event = Some(Box::new(on_event));
827        self
828    }
829}
830
831impl<'a, Id, Message> From<DropdownMenu<'a, Id, Message>> for Element<'a, Message>
832where
833    Id: Copy + PartialEq + 'a,
834    Message: Clone + 'a,
835{
836    fn from(menu: DropdownMenu<'a, Id, Message>) -> Self {
837        let DropdownMenu {
838            state,
839            trigger,
840            placement,
841            width,
842            keymap,
843            on_event,
844        } = menu;
845        let on_event: Option<Rc<dyn Fn(Event<Id>) -> Message + 'a>> = on_event.map(Rc::from);
846        let panel = state
847            .is_open()
848            .then(|| panel(state, on_event.as_deref(), width));
849        let anchored = anchored(trigger)
850            .kind("iced_cube::dropdown_menu")
851            .content(panel)
852            .placement(placement)
853            .fit_height(true)
854            .dismiss_keys([]);
855        let Some(on_event) = on_event.filter(|_| state.is_open()) else {
856            return anchored.into();
857        };
858        anchored
859            .on_dismiss(on_event(Event::Close))
860            .on_key(move |key| state.key_event(&keymap, key).map(&*on_event))
861            .into()
862    }
863}
864
865/// The open menu: the root panel, with open submenus floating beside
866/// their items.
867pub(crate) fn panel<'a, Id, Message>(
868    state: &'a State<Id>,
869    on_event: Option<&dyn Fn(Event<Id>) -> Message>,
870    width: f32,
871) -> Element<'a, Message>
872where
873    Id: Copy + PartialEq + 'a,
874    Message: Clone + 'a,
875{
876    level(state, &state.entries, 0, on_event, width)
877}
878
879fn level<'a, Id, Message>(
880    state: &'a State<Id>,
881    entries: &'a [Entry<Id>],
882    depth: usize,
883    on_event: Option<&dyn Fn(Event<Id>) -> Message>,
884    width: f32,
885) -> Element<'a, Message>
886where
887    Id: Copy + PartialEq + 'a,
888    Message: Clone + 'a,
889{
890    let highlighted = state.highlighted_at(depth);
891    let submenu_open = state.depth() > depth + 1;
892    let indented = has_marks(entries);
893    let slot = reserves_slot(entries);
894
895    let rows = entries
896        .iter()
897        .enumerate()
898        .map(|(index, entry)| match entry {
899            Entry::Separator => menu::separator(),
900            Entry::Label(label) => menu::group_label(label, indented),
901            Entry::Item(item) => {
902                let is_highlighted = highlighted == Some(index);
903                let expanded = is_highlighted && submenu_open;
904                let row = item_row(item, is_highlighted, expanded, slot, on_event);
905                let Some(children) = item.submenu().filter(|_| is_highlighted && submenu_open)
906                else {
907                    return row;
908                };
909                // The root menu dismisses everything, so a submenu lets
910                // outside presses through to it. Without room on either
911                // side of its row, the submenu drops below or above the row.
912                anchored(row)
913                    .kind("iced_cube::dropdown_menu")
914                    .content(Some(level(state, children, depth + 1, on_event, width)))
915                    .placement(
916                        Placement::new(Side::Right, Align::Start)
917                            .gap(space::XS)
918                            .offset(-(space::XS + 1.0)),
919                    )
920                    .fit_height(true)
921                    .pass_through(true)
922                    .into()
923            }
924        });
925
926    // Each level scrolls when it is taller than the room beside its
927    // trigger or row, following the keyboard highlight.
928    let rows = column(rows).width(width);
929    let surface: Element<'a, Message> = menu::surface(scrolling(rows, highlighted)).into();
930    #[cfg(feature = "icedwright")]
931    let surface = icedwright_probe::semantic(surface)
932        .role(icedwright_probe::role::Menu)
933        .component("iced_cube::dropdown_menu")
934        .into();
935    surface
936}
937
938/// Whether a menu level has checkbox or radio items, whose group labels
939/// then line up with the item labels.
940fn has_marks<Id>(entries: &[Entry<Id>]) -> bool {
941    entries.iter().any(|entry| {
942        matches!(
943            entry.as_item().map(|item| &item.kind),
944            Some(Kind::Checkbox(_) | Kind::Radio(_))
945        )
946    })
947}
948
949/// Whether every row of a menu level gets a leading slot, so labels line
950/// up: when any item has a check mark, a radio dot or an icon.
951fn reserves_slot<Id>(entries: &[Entry<Id>]) -> bool {
952    has_marks(entries)
953        || entries
954            .iter()
955            .any(|entry| entry.as_item().is_some_and(|item| item.icon.is_some()))
956}
957
958/// What fills an item's leading slot: its mark while checked, otherwise
959/// its icon, otherwise nothing.
960fn leading<Id>(item: &Item<Id>, slot: bool) -> Leading {
961    match (&item.kind, slot) {
962        (_, false) => Leading::None,
963        (Kind::Checkbox(true), true) => Leading::Check,
964        (Kind::Radio(true), true) => Leading::Dot,
965        (_, true) => Leading::Empty,
966    }
967}
968
969fn item_row<'a, Id, Message>(
970    item: &'a Item<Id>,
971    highlighted: bool,
972    expanded: bool,
973    slot: bool,
974    on_event: Option<&dyn Fn(Event<Id>) -> Message>,
975) -> Element<'a, Message>
976where
977    Id: Copy + 'a,
978    Message: Clone + 'a,
979{
980    #[cfg(not(feature = "icedwright"))]
981    let _ = expanded;
982    #[cfg(feature = "icedwright")]
983    let enabled_row = on_event.is_some() && item.enabled();
984    let row = item_body(item, highlighted, slot, on_event);
985    #[cfg(feature = "icedwright")]
986    let row = {
987        use icedwright_probe::{role, semantic};
988        let name = item.label.as_str();
989        let kind = "iced_cube::dropdown_menu";
990        match &item.kind {
991            Kind::Checkbox(checked) => semantic(row)
992                .role(role::MenuItemCheckBox)
993                .name(name)
994                .checked(*checked)
995                .disabled(!enabled_row)
996                .component(kind)
997                .into(),
998            Kind::Radio(checked) => semantic(row)
999                .role(role::MenuItemRadio)
1000                .name(name)
1001                .checked(*checked)
1002                .disabled(!enabled_row)
1003                .component(kind)
1004                .into(),
1005            _ => {
1006                let mut annotated = semantic(row)
1007                    .role(role::MenuItem)
1008                    .name(name)
1009                    .disabled(!enabled_row)
1010                    .component(kind);
1011                if item.submenu().is_some() {
1012                    annotated = annotated.expanded(expanded);
1013                }
1014                annotated.into()
1015            }
1016        }
1017    };
1018    row
1019}
1020
1021fn item_body<'a, Id, Message>(
1022    item: &'a Item<Id>,
1023    highlighted: bool,
1024    slot: bool,
1025    on_event: Option<&dyn Fn(Event<Id>) -> Message>,
1026) -> Element<'a, Message>
1027where
1028    Id: Copy + 'a,
1029    Message: Clone + 'a,
1030{
1031    let enabled = on_event.filter(|_| item.enabled());
1032    let status = match (enabled.is_some(), highlighted) {
1033        (false, _) => RowStatus::Disabled,
1034        (true, true) => RowStatus::Highlighted,
1035        (true, false) => RowStatus::Idle,
1036    };
1037    let leading = leading(item, slot);
1038    let trailing = if item.submenu().is_some() {
1039        Trailing::Chevron
1040    } else {
1041        Trailing::None
1042    };
1043    let parts = Parts {
1044        icon: item.icon,
1045        hint: item.shortcut.as_deref(),
1046        leading,
1047        trailing,
1048        ..Parts::label(&item.label)
1049    };
1050    let messages = enabled.map(|on_event| {
1051        (
1052            on_event(Event::Highlight(item.id)),
1053            on_event(Event::Activate(item.id)),
1054        )
1055    });
1056    menu::item(parts, status, item.destructive, messages)
1057}
1058
1059#[cfg(test)]
1060mod tests {
1061    use super::*;
1062    use iced::keyboard::Modifiers;
1063    use iced::widget::text;
1064
1065    const COPY: u8 = 1;
1066    const PASTE: u8 = 2;
1067    const DELETE: u8 = 3;
1068    const SHARE: u8 = 4;
1069    const EMAIL: u8 = 5;
1070    const LINK: u8 = 6;
1071    const GRID: u8 = 7;
1072    const SMALL: u8 = 8;
1073    const LARGE: u8 = 9;
1074    const PREVIEW: u8 = 10;
1075
1076    fn state() -> State<u8> {
1077        State::new([
1078            group_label("Edit"),
1079            item(COPY, "Copy").shortcut("Ctrl+C"),
1080            item(PASTE, "Paste").disabled(true),
1081            item(DELETE, "Delete").destructive(true),
1082            separator(),
1083            submenu(
1084                SHARE,
1085                "Share",
1086                [item(EMAIL, "Email"), item(LINK, "Copy link")],
1087            ),
1088            checkbox_item(GRID, "Show grid", true),
1089            separator(),
1090            radio_item(SMALL, "Small", true),
1091            radio_item(LARGE, "Large", false),
1092            item(PREVIEW, "Preview"),
1093        ])
1094    }
1095
1096    fn open() -> State<u8> {
1097        let mut state = state();
1098        let _ = state.update(Event::Open);
1099        state
1100    }
1101
1102    #[test]
1103    fn builders_set_item_fields_and_ignore_labels() {
1104        let entry: Entry<u8> = item(1, "Save")
1105            .icon(crate::lucide!(Save))
1106            .shortcut("Ctrl+S")
1107            .disabled(true)
1108            .destructive(true);
1109        let item = entry.as_item().unwrap();
1110        assert_eq!(item.label, "Save");
1111        assert_eq!(item.icon, Some(crate::lucide!(Save)));
1112        assert_eq!(item.shortcut.as_deref(), Some("Ctrl+S"));
1113        assert!(item.disabled && item.destructive);
1114        assert_eq!(item.kind, Kind::Action);
1115
1116        let label: Entry<u8> = group_label("Group")
1117            .icon(crate::lucide!(Save))
1118            .disabled(true);
1119        assert_eq!(label, Entry::Label("Group".into()));
1120        assert_eq!(separator::<u8>().shortcut("X"), Entry::Separator);
1121    }
1122
1123    #[test]
1124    fn starts_closed_and_opens_without_a_highlight() {
1125        let mut state = state();
1126        assert!(!state.is_open());
1127        assert_eq!(state.depth(), 0);
1128        assert_eq!(state.update(Event::Open), None);
1129        assert!(state.is_open());
1130        assert_eq!(state.highlighted(), None);
1131        let _ = state.update(Event::Open);
1132        assert_eq!(state.depth(), 1);
1133    }
1134
1135    #[test]
1136    fn toggle_and_close() {
1137        let mut state = state();
1138        let _ = state.update(Event::Toggle);
1139        assert!(state.is_open());
1140        let _ = state.update(Event::Toggle);
1141        assert!(!state.is_open());
1142        let _ = state.update(Event::Next);
1143        let _ = state.update(Event::Close);
1144        assert!(!state.is_open());
1145        assert_eq!(state.highlighted(), None);
1146    }
1147
1148    #[test]
1149    fn next_skips_labels_separators_and_disabled_items_and_wraps() {
1150        let mut state = open();
1151        let mut seen = Vec::new();
1152        for _ in 0..8 {
1153            let _ = state.update(Event::Next);
1154            seen.push(state.highlighted().unwrap());
1155        }
1156        assert_eq!(
1157            seen,
1158            [COPY, DELETE, SHARE, GRID, SMALL, LARGE, PREVIEW, COPY]
1159        );
1160    }
1161
1162    #[test]
1163    fn previous_wraps_backwards() {
1164        let mut state = open();
1165        let _ = state.update(Event::Previous);
1166        assert_eq!(state.highlighted(), Some(PREVIEW));
1167        let _ = state.update(Event::First);
1168        let _ = state.update(Event::Previous);
1169        assert_eq!(state.highlighted(), Some(PREVIEW));
1170    }
1171
1172    #[test]
1173    fn first_and_last() {
1174        let mut state = open();
1175        let _ = state.update(Event::Last);
1176        assert_eq!(state.highlighted(), Some(PREVIEW));
1177        let _ = state.update(Event::First);
1178        assert_eq!(state.highlighted(), Some(COPY));
1179    }
1180
1181    #[test]
1182    fn moving_opens_a_closed_menu_on_an_end() {
1183        let mut down = state();
1184        let _ = down.update(Event::Next);
1185        assert!(down.is_open());
1186        assert_eq!(down.highlighted(), Some(COPY));
1187
1188        let mut up = state();
1189        let _ = up.update(Event::Previous);
1190        assert_eq!(up.highlighted(), Some(PREVIEW));
1191
1192        let mut enter = state();
1193        assert_eq!(enter.update(Event::ActivateHighlighted), None);
1194        assert_eq!(enter.highlighted(), Some(COPY));
1195    }
1196
1197    #[test]
1198    fn empty_and_all_disabled_menus_never_highlight() {
1199        let mut empty: State<u8> = State::new([]);
1200        let _ = empty.update(Event::Next);
1201        assert!(empty.is_open());
1202        assert_eq!(empty.highlighted(), None);
1203        assert_eq!(empty.update(Event::ActivateHighlighted), None);
1204
1205        let mut disabled = State::new([item(1u8, "A").disabled(true), separator()]);
1206        let _ = disabled.update(Event::Next);
1207        let _ = disabled.update(Event::Last);
1208        assert_eq!(disabled.highlighted(), None);
1209        assert_eq!(disabled.update(Event::Activate(1)), None);
1210    }
1211
1212    #[test]
1213    fn activating_an_action_returns_it_and_closes() {
1214        let mut state = open();
1215        assert_eq!(
1216            state.update(Event::Activate(COPY)),
1217            Some(Output::Activated(COPY))
1218        );
1219        assert!(!state.is_open());
1220    }
1221
1222    #[test]
1223    fn disabled_and_unknown_items_do_nothing() {
1224        let mut state = open();
1225        assert_eq!(state.update(Event::Activate(PASTE)), None);
1226        assert_eq!(state.update(Event::Activate(99)), None);
1227        let _ = state.update(Event::Highlight(PASTE));
1228        assert_eq!(state.highlighted(), None);
1229        assert!(state.is_open());
1230    }
1231
1232    #[test]
1233    fn enter_activates_the_highlighted_item() {
1234        let mut state = open();
1235        let _ = state.update(Event::Next);
1236        let _ = state.update(Event::Next);
1237        assert_eq!(
1238            state.update(Event::ActivateHighlighted),
1239            Some(Output::Activated(DELETE))
1240        );
1241        let mut nothing = open();
1242        assert_eq!(nothing.update(Event::ActivateHighlighted), None);
1243        assert!(nothing.is_open());
1244    }
1245
1246    #[test]
1247    fn checkbox_items_toggle() {
1248        let mut state = open();
1249        assert!(state.is_checked(GRID));
1250        assert_eq!(
1251            state.update(Event::Activate(GRID)),
1252            Some(Output::Toggled(GRID, false))
1253        );
1254        assert!(!state.is_checked(GRID));
1255        let _ = state.update(Event::Open);
1256        assert_eq!(
1257            state.update(Event::Activate(GRID)),
1258            Some(Output::Toggled(GRID, true))
1259        );
1260    }
1261
1262    #[test]
1263    fn radio_items_check_one_of_their_run() {
1264        let mut state = open();
1265        assert_eq!(
1266            state.update(Event::Activate(LARGE)),
1267            Some(Output::Selected(LARGE))
1268        );
1269        assert!(state.is_checked(LARGE));
1270        assert!(!state.is_checked(SMALL));
1271        state.set_checked(SMALL, true);
1272        assert!(state.is_checked(SMALL));
1273        assert!(!state.is_checked(LARGE));
1274        state.set_checked(SMALL, false);
1275        assert!(!state.is_checked(SMALL));
1276    }
1277
1278    #[test]
1279    fn radio_group_is_the_adjacent_run() {
1280        let entries: Vec<Entry<u8>> = vec![
1281            radio_item(1, "A", false),
1282            separator(),
1283            radio_item(2, "B", false),
1284            radio_item(3, "C", false),
1285            item(4, "D"),
1286            radio_item(5, "E", false),
1287        ];
1288        assert_eq!(radio_group(&entries, 0), 0..1);
1289        assert_eq!(radio_group(&entries, 2), 2..4);
1290        assert_eq!(radio_group(&entries, 3), 2..4);
1291        assert_eq!(radio_group(&entries, 5), 5..6);
1292    }
1293
1294    #[test]
1295    fn set_checked_ignores_actions_and_unknown_ids() {
1296        let mut state = state();
1297        state.set_checked(COPY, true);
1298        state.set_checked(99, true);
1299        assert!(!state.is_checked(COPY));
1300        assert_eq!(state, self::state());
1301    }
1302
1303    #[test]
1304    fn set_disabled_drops_the_highlight() {
1305        let mut state = open();
1306        let _ = state.update(Event::Highlight(DELETE));
1307        state.set_disabled(DELETE, true);
1308        assert_eq!(state.highlighted(), None);
1309        assert!(state.item(DELETE).unwrap().disabled);
1310        state.set_disabled(PASTE, false);
1311        let _ = state.update(Event::First);
1312        let _ = state.update(Event::Next);
1313        assert_eq!(state.highlighted(), Some(PASTE));
1314    }
1315
1316    #[test]
1317    fn disabling_an_item_in_another_submenu_keeps_the_active_highlight() {
1318        let mut state = State::new([
1319            submenu(1, "Share", [item(2, "Email")]),
1320            submenu(3, "Export", [item(4, "PDF")]),
1321        ]);
1322        let _ = state.update(Event::Open);
1323        let _ = state.update(Event::Highlight(2));
1324
1325        state.set_disabled(4, true);
1326
1327        assert_eq!(state.highlighted(), Some(2));
1328        assert_eq!(
1329            state.update(Event::ActivateHighlighted),
1330            Some(Output::Activated(2))
1331        );
1332    }
1333
1334    #[test]
1335    fn pointer_on_a_submenu_opens_it_without_a_highlight() {
1336        let mut state = open();
1337        let _ = state.update(Event::Highlight(SHARE));
1338        assert_eq!(state.depth(), 2);
1339        assert_eq!(state.highlighted(), Some(SHARE));
1340        assert_eq!(state.highlighted_at(1), None);
1341
1342        let _ = state.update(Event::Highlight(LINK));
1343        assert_eq!(state.highlighted(), Some(LINK));
1344        let _ = state.update(Event::Highlight(SHARE));
1345        assert_eq!(
1346            state.highlighted(),
1347            Some(LINK),
1348            "returning to the parent keeps the submenu"
1349        );
1350
1351        let _ = state.update(Event::Highlight(COPY));
1352        assert_eq!(state.depth(), 1, "another item closes the submenu");
1353    }
1354
1355    #[test]
1356    fn keyboard_opens_and_closes_submenus() {
1357        let mut state = open();
1358        let _ = state.update(Event::Highlight(SHARE));
1359        let _ = state.update(Event::CloseSubmenu);
1360        assert_eq!(state.depth(), 1);
1361        assert_eq!(state.highlighted(), Some(SHARE));
1362
1363        let _ = state.update(Event::OpenSubmenu);
1364        assert_eq!(state.highlighted(), Some(EMAIL));
1365        let _ = state.update(Event::Next);
1366        assert_eq!(state.highlighted(), Some(LINK));
1367        let _ = state.update(Event::Next);
1368        assert_eq!(state.highlighted(), Some(EMAIL), "wraps inside the submenu");
1369
1370        let _ = state.update(Event::CloseSubmenu);
1371        assert_eq!(state.depth(), 1);
1372        assert_eq!(state.highlighted(), Some(SHARE));
1373        let _ = state.update(Event::CloseSubmenu);
1374        assert!(state.is_open(), "the root menu stays open");
1375    }
1376
1377    #[test]
1378    fn enter_on_a_submenu_opens_it_and_items_inside_activate() {
1379        let mut state = open();
1380        let _ = state.update(Event::Highlight(SHARE));
1381        let _ = state.update(Event::CloseSubmenu);
1382        assert_eq!(state.update(Event::ActivateHighlighted), None);
1383        assert_eq!(state.highlighted(), Some(EMAIL));
1384        assert_eq!(
1385            state.update(Event::ActivateHighlighted),
1386            Some(Output::Activated(EMAIL))
1387        );
1388        assert!(!state.is_open());
1389    }
1390
1391    #[test]
1392    fn clicking_a_submenu_item_opens_it() {
1393        let mut state = open();
1394        assert_eq!(state.update(Event::Activate(SHARE)), None);
1395        assert_eq!(state.depth(), 2);
1396        assert_eq!(
1397            state.update(Event::Activate(LINK)),
1398            Some(Output::Activated(LINK))
1399        );
1400    }
1401
1402    #[test]
1403    fn open_submenu_ignores_plain_items() {
1404        let mut state = open();
1405        let _ = state.update(Event::First);
1406        let _ = state.update(Event::OpenSubmenu);
1407        assert_eq!(state.depth(), 1);
1408    }
1409
1410    #[test]
1411    fn moving_in_the_parent_closes_the_submenu() {
1412        let mut state = open();
1413        let _ = state.update(Event::Highlight(SHARE));
1414        let _ = state.update(Event::Next);
1415        assert_eq!(state.depth(), 1);
1416        assert_eq!(state.highlighted(), Some(GRID));
1417    }
1418
1419    #[test]
1420    fn typeahead_jumps_to_matching_labels_and_cycles() {
1421        let mut state = open();
1422        let _ = state.update(Event::Typeahead('s'));
1423        assert_eq!(state.highlighted(), Some(SHARE));
1424        let _ = state.update(Event::Typeahead('S'));
1425        assert_eq!(state.highlighted(), Some(GRID));
1426        let _ = state.update(Event::Typeahead('s'));
1427        assert_eq!(state.highlighted(), Some(SMALL));
1428        let _ = state.update(Event::Typeahead('s'));
1429        assert_eq!(state.highlighted(), Some(SHARE));
1430        let _ = state.update(Event::Typeahead('p'));
1431        assert_eq!(
1432            state.highlighted(),
1433            Some(PREVIEW),
1434            "skips the disabled Paste"
1435        );
1436        let _ = state.update(Event::Typeahead('z'));
1437        assert_eq!(state.highlighted(), Some(PREVIEW));
1438    }
1439
1440    #[test]
1441    fn typeahead_starts_at_the_first_match() {
1442        let mut state = open();
1443        let _ = state.update(Event::Typeahead('c'));
1444        assert_eq!(state.highlighted(), Some(COPY));
1445    }
1446
1447    fn key(key: Key, modifiers: Modifiers) -> keys::Event {
1448        keys::Event { key, modifiers }
1449    }
1450
1451    fn character(c: &str) -> Key {
1452        Key::Character(c.into())
1453    }
1454
1455    #[test]
1456    fn typeahead_accepts_plain_letters_and_digits_only() {
1457        let none = Modifiers::empty();
1458        assert_eq!(typeahead(&key(character("a"), none)), Some('a'));
1459        assert_eq!(typeahead(&key(character("A"), Modifiers::SHIFT)), Some('A'));
1460        assert_eq!(typeahead(&key(character("7"), none)), Some('7'));
1461        assert_eq!(typeahead(&key(character("a"), Modifiers::CTRL)), None);
1462        assert_eq!(typeahead(&key(character("a"), Modifiers::ALT)), None);
1463        assert_eq!(typeahead(&key(character(" "), none)), None);
1464        assert_eq!(typeahead(&key(character("/"), none)), None);
1465        assert_eq!(typeahead(&key(character("ab"), none)), None);
1466        assert_eq!(typeahead(&key(Key::Named(Named::Enter), none)), None);
1467    }
1468
1469    #[test]
1470    fn key_event_uses_the_keymap_then_typeahead() {
1471        let keymap = default_keymap();
1472        let closed = state();
1473        let down = key(Key::Named(Named::ArrowDown), Modifiers::empty());
1474        let enter = key(Key::Named(Named::Enter), Modifiers::empty());
1475        let letter = key(character("d"), Modifiers::empty());
1476        assert_eq!(
1477            closed.key_event(&keymap, &down),
1478            None,
1479            "closed menus claim no keys"
1480        );
1481        assert_eq!(closed.key_event(&keymap, &enter), None);
1482        assert_eq!(closed.key_event(&keymap, &letter), None);
1483
1484        let bound = default_keymap().bind(Chord::named(Named::F2), Action::Open);
1485        let f2 = key(Key::Named(Named::F2), Modifiers::empty());
1486        assert_eq!(closed.key_event(&bound, &f2), Some(Event::First));
1487
1488        let open = open();
1489        assert_eq!(
1490            open.key_event(&keymap, &letter),
1491            Some(Event::Typeahead('d'))
1492        );
1493        let escape = key(Key::Named(Named::Escape), Modifiers::empty());
1494        assert_eq!(open.key_event(&keymap, &escape), Some(Event::Close));
1495    }
1496
1497    fn press(keymap: &Keymap<Action>, chord: &str) -> Option<Action> {
1498        let chord: Chord = chord.parse().unwrap();
1499        keymap.resolve(chord.key(), chord.modifiers())
1500    }
1501
1502    #[test]
1503    fn default_keymap_covers_every_action() {
1504        let keymap = default_keymap();
1505        assert_eq!(press(&keymap, "ArrowDown"), Some(Action::Next));
1506        assert_eq!(press(&keymap, "ArrowUp"), Some(Action::Previous));
1507        assert_eq!(press(&keymap, "Home"), Some(Action::First));
1508        assert_eq!(press(&keymap, "End"), Some(Action::Last));
1509        assert_eq!(press(&keymap, "Enter"), Some(Action::Activate));
1510        assert_eq!(press(&keymap, "Space"), Some(Action::Activate));
1511        assert_eq!(press(&keymap, "Escape"), Some(Action::Close));
1512        assert_eq!(press(&keymap, "ArrowRight"), Some(Action::OpenSubmenu));
1513        assert_eq!(press(&keymap, "ArrowLeft"), Some(Action::CloseSubmenu));
1514        assert_eq!(press(&keymap, "Tab"), None);
1515        for action in <Action as keys::Action>::ALL {
1516            let bound = !keymap.chords(action).is_empty();
1517            assert_eq!(bound, *action != Action::Open, "{action:?}");
1518        }
1519        assert_eq!(Keymap::<Action>::defaults(), keymap);
1520    }
1521
1522    #[test]
1523    fn keymap_overrides_and_unbinding() {
1524        let keymap = default_keymap()
1525            .unbind(&"Space".parse().unwrap())
1526            .bind("J".parse().unwrap(), Action::Next);
1527        assert_eq!(press(&keymap, "Space"), None);
1528        assert_eq!(press(&keymap, "j"), Some(Action::Next));
1529        assert_eq!(press(&keymap, "Enter"), Some(Action::Activate));
1530    }
1531
1532    #[test]
1533    fn a_closed_menu_only_answers_open() {
1534        let closed = state();
1535        assert_eq!(Action::Open.event(&closed), Some(Event::First));
1536        for &action in <Action as keys::Action>::ALL {
1537            if action != Action::Open {
1538                assert_eq!(action.event(&closed), None, "{action:?}");
1539            }
1540        }
1541    }
1542
1543    #[test]
1544    fn an_open_menu_maps_every_action_but_open() {
1545        let open = open();
1546        assert_eq!(Action::Open.event(&open), None);
1547        assert_eq!(Action::Next.event(&open), Some(Event::Next));
1548        assert_eq!(Action::Previous.event(&open), Some(Event::Previous));
1549        assert_eq!(Action::First.event(&open), Some(Event::First));
1550        assert_eq!(Action::Last.event(&open), Some(Event::Last));
1551        assert_eq!(
1552            Action::Activate.event(&open),
1553            Some(Event::ActivateHighlighted)
1554        );
1555        assert_eq!(Action::Close.event(&open), Some(Event::Close));
1556        assert_eq!(Action::OpenSubmenu.event(&open), Some(Event::OpenSubmenu));
1557        assert_eq!(Action::CloseSubmenu.event(&open), Some(Event::CloseSubmenu));
1558    }
1559
1560    #[test]
1561    fn close_in_a_submenu_closes_only_the_submenu() {
1562        let mut state = open();
1563        let _ = state.update(Event::Highlight(SHARE));
1564        assert_eq!(Action::Close.event(&state), Some(Event::CloseSubmenu));
1565        let _ = state.update(Event::CloseSubmenu);
1566        assert_eq!(Action::Close.event(&state), Some(Event::Close));
1567    }
1568
1569    #[test]
1570    fn every_action_has_a_name_and_a_sentence() {
1571        for &action in <Action as keys::Action>::ALL {
1572            assert!(!keys::Action::name(action).is_empty());
1573            assert!(keys::Action::description(action).ends_with('.'));
1574        }
1575    }
1576
1577    #[test]
1578    fn builder_defaults() {
1579        let state = state();
1580        let menu: DropdownMenu<'_, u8, ()> = dropdown_menu(&state, text("Open"));
1581        assert_eq!(menu.placement, Placement::new(Side::Bottom, Align::Start));
1582        assert_eq!(menu.width, WIDTH);
1583        assert_eq!(menu.keymap, default_keymap());
1584        assert!(menu.on_event.is_none());
1585
1586        let menu: DropdownMenu<'_, u8, ()> = dropdown_menu(&state, text("Open"))
1587            .side(Side::Top)
1588            .align(Align::End)
1589            .gap(8.0)
1590            .width(300.0)
1591            .keymap(Keymap::new())
1592            .on_event(|_| ());
1593        assert_eq!(menu.placement.side, Side::Top);
1594        assert_eq!(menu.placement.align, Align::End);
1595        assert_eq!(menu.placement.gap, 8.0);
1596        assert_eq!(menu.width, 300.0);
1597        assert!(menu.keymap.is_empty());
1598        assert!(menu.on_event.is_some());
1599    }
1600
1601    #[test]
1602    fn a_slot_is_reserved_for_marks_or_icons() {
1603        let plain: Vec<Entry<u8>> = vec![item(COPY, "Copy"), separator()];
1604        assert!(!reserves_slot(&plain) && !has_marks(&plain));
1605
1606        let icons: Vec<Entry<u8>> = vec![
1607            item(COPY, "Copy").icon(crate::lucide!(Copy)),
1608            item(PASTE, "Paste"),
1609        ];
1610        assert!(reserves_slot(&icons) && !has_marks(&icons));
1611
1612        let marks: Vec<Entry<u8>> = vec![item(COPY, "Copy"), radio_item(SMALL, "Small", false)];
1613        assert!(reserves_slot(&marks) && has_marks(&marks));
1614    }
1615
1616    #[test]
1617    fn the_slot_holds_the_mark_while_checked_and_the_icon_otherwise() {
1618        let entries: Vec<Entry<u8>> = vec![
1619            checkbox_item(GRID, "Grid", true).icon(crate::lucide!(Grid3x3)),
1620            checkbox_item(GRID, "Grid", false).icon(crate::lucide!(Grid3x3)),
1621            radio_item(SMALL, "Small", true),
1622            radio_item(LARGE, "Large", false),
1623            item(COPY, "Copy").icon(crate::lucide!(Copy)),
1624        ];
1625        let slots: Vec<Leading> = entries
1626            .iter()
1627            .filter_map(Entry::as_item)
1628            .map(|item| leading(item, true))
1629            .collect();
1630        assert_eq!(
1631            slots,
1632            [
1633                Leading::Check,
1634                Leading::Empty,
1635                Leading::Dot,
1636                Leading::Empty,
1637                Leading::Empty
1638            ]
1639        );
1640        let unslotted = entries.iter().filter_map(Entry::as_item);
1641        assert!(
1642            unslotted
1643                .map(|item| leading(item, false))
1644                .all(|l| l == Leading::None)
1645        );
1646    }
1647}