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) => {
992                let annotated = semantic(row)
993                    .role(role::MenuItemCheckBox)
994                    .name(name)
995                    .checked(*checked)
996                    .component(kind);
997                if enabled_row {
998                    annotated.into()
999                } else {
1000                    annotated.disabled(true).into()
1001                }
1002            }
1003            Kind::Radio(checked) => {
1004                let annotated = semantic(row)
1005                    .role(role::MenuItemRadio)
1006                    .name(name)
1007                    .checked(*checked)
1008                    .component(kind);
1009                if enabled_row {
1010                    annotated.into()
1011                } else {
1012                    annotated.disabled(true).into()
1013                }
1014            }
1015            _ => {
1016                let mut annotated = semantic(row)
1017                    .role(role::MenuItem)
1018                    .name(name)
1019                    .component(kind);
1020                if item.submenu().is_some() {
1021                    annotated = annotated.expanded(expanded);
1022                }
1023                if !enabled_row {
1024                    annotated = annotated.disabled(true);
1025                }
1026                annotated.into()
1027            }
1028        }
1029    };
1030    row
1031}
1032
1033fn item_body<'a, Id, Message>(
1034    item: &'a Item<Id>,
1035    highlighted: bool,
1036    slot: bool,
1037    on_event: Option<&dyn Fn(Event<Id>) -> Message>,
1038) -> Element<'a, Message>
1039where
1040    Id: Copy + 'a,
1041    Message: Clone + 'a,
1042{
1043    let enabled = on_event.filter(|_| item.enabled());
1044    let status = match (enabled.is_some(), highlighted) {
1045        (false, _) => RowStatus::Disabled,
1046        (true, true) => RowStatus::Highlighted,
1047        (true, false) => RowStatus::Idle,
1048    };
1049    let leading = leading(item, slot);
1050    let trailing = if item.submenu().is_some() {
1051        Trailing::Chevron
1052    } else {
1053        Trailing::None
1054    };
1055    let parts = Parts {
1056        icon: item.icon,
1057        hint: item.shortcut.as_deref(),
1058        leading,
1059        trailing,
1060        ..Parts::label(&item.label)
1061    };
1062    let messages = enabled.map(|on_event| {
1063        (
1064            on_event(Event::Highlight(item.id)),
1065            on_event(Event::Activate(item.id)),
1066        )
1067    });
1068    menu::item(parts, status, item.destructive, messages)
1069}
1070
1071#[cfg(test)]
1072mod tests {
1073    use super::*;
1074    use iced::keyboard::Modifiers;
1075    use iced::widget::text;
1076
1077    const COPY: u8 = 1;
1078    const PASTE: u8 = 2;
1079    const DELETE: u8 = 3;
1080    const SHARE: u8 = 4;
1081    const EMAIL: u8 = 5;
1082    const LINK: u8 = 6;
1083    const GRID: u8 = 7;
1084    const SMALL: u8 = 8;
1085    const LARGE: u8 = 9;
1086    const PREVIEW: u8 = 10;
1087
1088    fn state() -> State<u8> {
1089        State::new([
1090            group_label("Edit"),
1091            item(COPY, "Copy").shortcut("Ctrl+C"),
1092            item(PASTE, "Paste").disabled(true),
1093            item(DELETE, "Delete").destructive(true),
1094            separator(),
1095            submenu(
1096                SHARE,
1097                "Share",
1098                [item(EMAIL, "Email"), item(LINK, "Copy link")],
1099            ),
1100            checkbox_item(GRID, "Show grid", true),
1101            separator(),
1102            radio_item(SMALL, "Small", true),
1103            radio_item(LARGE, "Large", false),
1104            item(PREVIEW, "Preview"),
1105        ])
1106    }
1107
1108    fn open() -> State<u8> {
1109        let mut state = state();
1110        let _ = state.update(Event::Open);
1111        state
1112    }
1113
1114    #[test]
1115    fn builders_set_item_fields_and_ignore_labels() {
1116        let entry: Entry<u8> = item(1, "Save")
1117            .icon(crate::lucide!(Save))
1118            .shortcut("Ctrl+S")
1119            .disabled(true)
1120            .destructive(true);
1121        let item = entry.as_item().unwrap();
1122        assert_eq!(item.label, "Save");
1123        assert_eq!(item.icon, Some(crate::lucide!(Save)));
1124        assert_eq!(item.shortcut.as_deref(), Some("Ctrl+S"));
1125        assert!(item.disabled && item.destructive);
1126        assert_eq!(item.kind, Kind::Action);
1127
1128        let label: Entry<u8> = group_label("Group")
1129            .icon(crate::lucide!(Save))
1130            .disabled(true);
1131        assert_eq!(label, Entry::Label("Group".into()));
1132        assert_eq!(separator::<u8>().shortcut("X"), Entry::Separator);
1133    }
1134
1135    #[test]
1136    fn starts_closed_and_opens_without_a_highlight() {
1137        let mut state = state();
1138        assert!(!state.is_open());
1139        assert_eq!(state.depth(), 0);
1140        assert_eq!(state.update(Event::Open), None);
1141        assert!(state.is_open());
1142        assert_eq!(state.highlighted(), None);
1143        let _ = state.update(Event::Open);
1144        assert_eq!(state.depth(), 1);
1145    }
1146
1147    #[test]
1148    fn toggle_and_close() {
1149        let mut state = state();
1150        let _ = state.update(Event::Toggle);
1151        assert!(state.is_open());
1152        let _ = state.update(Event::Toggle);
1153        assert!(!state.is_open());
1154        let _ = state.update(Event::Next);
1155        let _ = state.update(Event::Close);
1156        assert!(!state.is_open());
1157        assert_eq!(state.highlighted(), None);
1158    }
1159
1160    #[test]
1161    fn next_skips_labels_separators_and_disabled_items_and_wraps() {
1162        let mut state = open();
1163        let mut seen = Vec::new();
1164        for _ in 0..8 {
1165            let _ = state.update(Event::Next);
1166            seen.push(state.highlighted().unwrap());
1167        }
1168        assert_eq!(
1169            seen,
1170            [COPY, DELETE, SHARE, GRID, SMALL, LARGE, PREVIEW, COPY]
1171        );
1172    }
1173
1174    #[test]
1175    fn previous_wraps_backwards() {
1176        let mut state = open();
1177        let _ = state.update(Event::Previous);
1178        assert_eq!(state.highlighted(), Some(PREVIEW));
1179        let _ = state.update(Event::First);
1180        let _ = state.update(Event::Previous);
1181        assert_eq!(state.highlighted(), Some(PREVIEW));
1182    }
1183
1184    #[test]
1185    fn first_and_last() {
1186        let mut state = open();
1187        let _ = state.update(Event::Last);
1188        assert_eq!(state.highlighted(), Some(PREVIEW));
1189        let _ = state.update(Event::First);
1190        assert_eq!(state.highlighted(), Some(COPY));
1191    }
1192
1193    #[test]
1194    fn moving_opens_a_closed_menu_on_an_end() {
1195        let mut down = state();
1196        let _ = down.update(Event::Next);
1197        assert!(down.is_open());
1198        assert_eq!(down.highlighted(), Some(COPY));
1199
1200        let mut up = state();
1201        let _ = up.update(Event::Previous);
1202        assert_eq!(up.highlighted(), Some(PREVIEW));
1203
1204        let mut enter = state();
1205        assert_eq!(enter.update(Event::ActivateHighlighted), None);
1206        assert_eq!(enter.highlighted(), Some(COPY));
1207    }
1208
1209    #[test]
1210    fn empty_and_all_disabled_menus_never_highlight() {
1211        let mut empty: State<u8> = State::new([]);
1212        let _ = empty.update(Event::Next);
1213        assert!(empty.is_open());
1214        assert_eq!(empty.highlighted(), None);
1215        assert_eq!(empty.update(Event::ActivateHighlighted), None);
1216
1217        let mut disabled = State::new([item(1u8, "A").disabled(true), separator()]);
1218        let _ = disabled.update(Event::Next);
1219        let _ = disabled.update(Event::Last);
1220        assert_eq!(disabled.highlighted(), None);
1221        assert_eq!(disabled.update(Event::Activate(1)), None);
1222    }
1223
1224    #[test]
1225    fn activating_an_action_returns_it_and_closes() {
1226        let mut state = open();
1227        assert_eq!(
1228            state.update(Event::Activate(COPY)),
1229            Some(Output::Activated(COPY))
1230        );
1231        assert!(!state.is_open());
1232    }
1233
1234    #[test]
1235    fn disabled_and_unknown_items_do_nothing() {
1236        let mut state = open();
1237        assert_eq!(state.update(Event::Activate(PASTE)), None);
1238        assert_eq!(state.update(Event::Activate(99)), None);
1239        let _ = state.update(Event::Highlight(PASTE));
1240        assert_eq!(state.highlighted(), None);
1241        assert!(state.is_open());
1242    }
1243
1244    #[test]
1245    fn enter_activates_the_highlighted_item() {
1246        let mut state = open();
1247        let _ = state.update(Event::Next);
1248        let _ = state.update(Event::Next);
1249        assert_eq!(
1250            state.update(Event::ActivateHighlighted),
1251            Some(Output::Activated(DELETE))
1252        );
1253        let mut nothing = open();
1254        assert_eq!(nothing.update(Event::ActivateHighlighted), None);
1255        assert!(nothing.is_open());
1256    }
1257
1258    #[test]
1259    fn checkbox_items_toggle() {
1260        let mut state = open();
1261        assert!(state.is_checked(GRID));
1262        assert_eq!(
1263            state.update(Event::Activate(GRID)),
1264            Some(Output::Toggled(GRID, false))
1265        );
1266        assert!(!state.is_checked(GRID));
1267        let _ = state.update(Event::Open);
1268        assert_eq!(
1269            state.update(Event::Activate(GRID)),
1270            Some(Output::Toggled(GRID, true))
1271        );
1272    }
1273
1274    #[test]
1275    fn radio_items_check_one_of_their_run() {
1276        let mut state = open();
1277        assert_eq!(
1278            state.update(Event::Activate(LARGE)),
1279            Some(Output::Selected(LARGE))
1280        );
1281        assert!(state.is_checked(LARGE));
1282        assert!(!state.is_checked(SMALL));
1283        state.set_checked(SMALL, true);
1284        assert!(state.is_checked(SMALL));
1285        assert!(!state.is_checked(LARGE));
1286        state.set_checked(SMALL, false);
1287        assert!(!state.is_checked(SMALL));
1288    }
1289
1290    #[test]
1291    fn radio_group_is_the_adjacent_run() {
1292        let entries: Vec<Entry<u8>> = vec![
1293            radio_item(1, "A", false),
1294            separator(),
1295            radio_item(2, "B", false),
1296            radio_item(3, "C", false),
1297            item(4, "D"),
1298            radio_item(5, "E", false),
1299        ];
1300        assert_eq!(radio_group(&entries, 0), 0..1);
1301        assert_eq!(radio_group(&entries, 2), 2..4);
1302        assert_eq!(radio_group(&entries, 3), 2..4);
1303        assert_eq!(radio_group(&entries, 5), 5..6);
1304    }
1305
1306    #[test]
1307    fn set_checked_ignores_actions_and_unknown_ids() {
1308        let mut state = state();
1309        state.set_checked(COPY, true);
1310        state.set_checked(99, true);
1311        assert!(!state.is_checked(COPY));
1312        assert_eq!(state, self::state());
1313    }
1314
1315    #[test]
1316    fn set_disabled_drops_the_highlight() {
1317        let mut state = open();
1318        let _ = state.update(Event::Highlight(DELETE));
1319        state.set_disabled(DELETE, true);
1320        assert_eq!(state.highlighted(), None);
1321        assert!(state.item(DELETE).unwrap().disabled);
1322        state.set_disabled(PASTE, false);
1323        let _ = state.update(Event::First);
1324        let _ = state.update(Event::Next);
1325        assert_eq!(state.highlighted(), Some(PASTE));
1326    }
1327
1328    #[test]
1329    fn disabling_an_item_in_another_submenu_keeps_the_active_highlight() {
1330        let mut state = State::new([
1331            submenu(1, "Share", [item(2, "Email")]),
1332            submenu(3, "Export", [item(4, "PDF")]),
1333        ]);
1334        let _ = state.update(Event::Open);
1335        let _ = state.update(Event::Highlight(2));
1336
1337        state.set_disabled(4, true);
1338
1339        assert_eq!(state.highlighted(), Some(2));
1340        assert_eq!(
1341            state.update(Event::ActivateHighlighted),
1342            Some(Output::Activated(2))
1343        );
1344    }
1345
1346    #[test]
1347    fn pointer_on_a_submenu_opens_it_without_a_highlight() {
1348        let mut state = open();
1349        let _ = state.update(Event::Highlight(SHARE));
1350        assert_eq!(state.depth(), 2);
1351        assert_eq!(state.highlighted(), Some(SHARE));
1352        assert_eq!(state.highlighted_at(1), None);
1353
1354        let _ = state.update(Event::Highlight(LINK));
1355        assert_eq!(state.highlighted(), Some(LINK));
1356        let _ = state.update(Event::Highlight(SHARE));
1357        assert_eq!(
1358            state.highlighted(),
1359            Some(LINK),
1360            "returning to the parent keeps the submenu"
1361        );
1362
1363        let _ = state.update(Event::Highlight(COPY));
1364        assert_eq!(state.depth(), 1, "another item closes the submenu");
1365    }
1366
1367    #[test]
1368    fn keyboard_opens_and_closes_submenus() {
1369        let mut state = open();
1370        let _ = state.update(Event::Highlight(SHARE));
1371        let _ = state.update(Event::CloseSubmenu);
1372        assert_eq!(state.depth(), 1);
1373        assert_eq!(state.highlighted(), Some(SHARE));
1374
1375        let _ = state.update(Event::OpenSubmenu);
1376        assert_eq!(state.highlighted(), Some(EMAIL));
1377        let _ = state.update(Event::Next);
1378        assert_eq!(state.highlighted(), Some(LINK));
1379        let _ = state.update(Event::Next);
1380        assert_eq!(state.highlighted(), Some(EMAIL), "wraps inside the submenu");
1381
1382        let _ = state.update(Event::CloseSubmenu);
1383        assert_eq!(state.depth(), 1);
1384        assert_eq!(state.highlighted(), Some(SHARE));
1385        let _ = state.update(Event::CloseSubmenu);
1386        assert!(state.is_open(), "the root menu stays open");
1387    }
1388
1389    #[test]
1390    fn enter_on_a_submenu_opens_it_and_items_inside_activate() {
1391        let mut state = open();
1392        let _ = state.update(Event::Highlight(SHARE));
1393        let _ = state.update(Event::CloseSubmenu);
1394        assert_eq!(state.update(Event::ActivateHighlighted), None);
1395        assert_eq!(state.highlighted(), Some(EMAIL));
1396        assert_eq!(
1397            state.update(Event::ActivateHighlighted),
1398            Some(Output::Activated(EMAIL))
1399        );
1400        assert!(!state.is_open());
1401    }
1402
1403    #[test]
1404    fn clicking_a_submenu_item_opens_it() {
1405        let mut state = open();
1406        assert_eq!(state.update(Event::Activate(SHARE)), None);
1407        assert_eq!(state.depth(), 2);
1408        assert_eq!(
1409            state.update(Event::Activate(LINK)),
1410            Some(Output::Activated(LINK))
1411        );
1412    }
1413
1414    #[test]
1415    fn open_submenu_ignores_plain_items() {
1416        let mut state = open();
1417        let _ = state.update(Event::First);
1418        let _ = state.update(Event::OpenSubmenu);
1419        assert_eq!(state.depth(), 1);
1420    }
1421
1422    #[test]
1423    fn moving_in_the_parent_closes_the_submenu() {
1424        let mut state = open();
1425        let _ = state.update(Event::Highlight(SHARE));
1426        let _ = state.update(Event::Next);
1427        assert_eq!(state.depth(), 1);
1428        assert_eq!(state.highlighted(), Some(GRID));
1429    }
1430
1431    #[test]
1432    fn typeahead_jumps_to_matching_labels_and_cycles() {
1433        let mut state = open();
1434        let _ = state.update(Event::Typeahead('s'));
1435        assert_eq!(state.highlighted(), Some(SHARE));
1436        let _ = state.update(Event::Typeahead('S'));
1437        assert_eq!(state.highlighted(), Some(GRID));
1438        let _ = state.update(Event::Typeahead('s'));
1439        assert_eq!(state.highlighted(), Some(SMALL));
1440        let _ = state.update(Event::Typeahead('s'));
1441        assert_eq!(state.highlighted(), Some(SHARE));
1442        let _ = state.update(Event::Typeahead('p'));
1443        assert_eq!(
1444            state.highlighted(),
1445            Some(PREVIEW),
1446            "skips the disabled Paste"
1447        );
1448        let _ = state.update(Event::Typeahead('z'));
1449        assert_eq!(state.highlighted(), Some(PREVIEW));
1450    }
1451
1452    #[test]
1453    fn typeahead_starts_at_the_first_match() {
1454        let mut state = open();
1455        let _ = state.update(Event::Typeahead('c'));
1456        assert_eq!(state.highlighted(), Some(COPY));
1457    }
1458
1459    fn key(key: Key, modifiers: Modifiers) -> keys::Event {
1460        keys::Event { key, modifiers }
1461    }
1462
1463    fn character(c: &str) -> Key {
1464        Key::Character(c.into())
1465    }
1466
1467    #[test]
1468    fn typeahead_accepts_plain_letters_and_digits_only() {
1469        let none = Modifiers::empty();
1470        assert_eq!(typeahead(&key(character("a"), none)), Some('a'));
1471        assert_eq!(typeahead(&key(character("A"), Modifiers::SHIFT)), Some('A'));
1472        assert_eq!(typeahead(&key(character("7"), none)), Some('7'));
1473        assert_eq!(typeahead(&key(character("a"), Modifiers::CTRL)), None);
1474        assert_eq!(typeahead(&key(character("a"), Modifiers::ALT)), None);
1475        assert_eq!(typeahead(&key(character(" "), none)), None);
1476        assert_eq!(typeahead(&key(character("/"), none)), None);
1477        assert_eq!(typeahead(&key(character("ab"), none)), None);
1478        assert_eq!(typeahead(&key(Key::Named(Named::Enter), none)), None);
1479    }
1480
1481    #[test]
1482    fn key_event_uses_the_keymap_then_typeahead() {
1483        let keymap = default_keymap();
1484        let closed = state();
1485        let down = key(Key::Named(Named::ArrowDown), Modifiers::empty());
1486        let enter = key(Key::Named(Named::Enter), Modifiers::empty());
1487        let letter = key(character("d"), Modifiers::empty());
1488        assert_eq!(
1489            closed.key_event(&keymap, &down),
1490            None,
1491            "closed menus claim no keys"
1492        );
1493        assert_eq!(closed.key_event(&keymap, &enter), None);
1494        assert_eq!(closed.key_event(&keymap, &letter), None);
1495
1496        let bound = default_keymap().bind(Chord::named(Named::F2), Action::Open);
1497        let f2 = key(Key::Named(Named::F2), Modifiers::empty());
1498        assert_eq!(closed.key_event(&bound, &f2), Some(Event::First));
1499
1500        let open = open();
1501        assert_eq!(
1502            open.key_event(&keymap, &letter),
1503            Some(Event::Typeahead('d'))
1504        );
1505        let escape = key(Key::Named(Named::Escape), Modifiers::empty());
1506        assert_eq!(open.key_event(&keymap, &escape), Some(Event::Close));
1507    }
1508
1509    fn press(keymap: &Keymap<Action>, chord: &str) -> Option<Action> {
1510        let chord: Chord = chord.parse().unwrap();
1511        keymap.resolve(chord.key(), chord.modifiers())
1512    }
1513
1514    #[test]
1515    fn default_keymap_covers_every_action() {
1516        let keymap = default_keymap();
1517        assert_eq!(press(&keymap, "ArrowDown"), Some(Action::Next));
1518        assert_eq!(press(&keymap, "ArrowUp"), Some(Action::Previous));
1519        assert_eq!(press(&keymap, "Home"), Some(Action::First));
1520        assert_eq!(press(&keymap, "End"), Some(Action::Last));
1521        assert_eq!(press(&keymap, "Enter"), Some(Action::Activate));
1522        assert_eq!(press(&keymap, "Space"), Some(Action::Activate));
1523        assert_eq!(press(&keymap, "Escape"), Some(Action::Close));
1524        assert_eq!(press(&keymap, "ArrowRight"), Some(Action::OpenSubmenu));
1525        assert_eq!(press(&keymap, "ArrowLeft"), Some(Action::CloseSubmenu));
1526        assert_eq!(press(&keymap, "Tab"), None);
1527        for action in <Action as keys::Action>::ALL {
1528            let bound = !keymap.chords(action).is_empty();
1529            assert_eq!(bound, *action != Action::Open, "{action:?}");
1530        }
1531        assert_eq!(Keymap::<Action>::defaults(), keymap);
1532    }
1533
1534    #[test]
1535    fn keymap_overrides_and_unbinding() {
1536        let keymap = default_keymap()
1537            .unbind(&"Space".parse().unwrap())
1538            .bind("J".parse().unwrap(), Action::Next);
1539        assert_eq!(press(&keymap, "Space"), None);
1540        assert_eq!(press(&keymap, "j"), Some(Action::Next));
1541        assert_eq!(press(&keymap, "Enter"), Some(Action::Activate));
1542    }
1543
1544    #[test]
1545    fn a_closed_menu_only_answers_open() {
1546        let closed = state();
1547        assert_eq!(Action::Open.event(&closed), Some(Event::First));
1548        for &action in <Action as keys::Action>::ALL {
1549            if action != Action::Open {
1550                assert_eq!(action.event(&closed), None, "{action:?}");
1551            }
1552        }
1553    }
1554
1555    #[test]
1556    fn an_open_menu_maps_every_action_but_open() {
1557        let open = open();
1558        assert_eq!(Action::Open.event(&open), None);
1559        assert_eq!(Action::Next.event(&open), Some(Event::Next));
1560        assert_eq!(Action::Previous.event(&open), Some(Event::Previous));
1561        assert_eq!(Action::First.event(&open), Some(Event::First));
1562        assert_eq!(Action::Last.event(&open), Some(Event::Last));
1563        assert_eq!(
1564            Action::Activate.event(&open),
1565            Some(Event::ActivateHighlighted)
1566        );
1567        assert_eq!(Action::Close.event(&open), Some(Event::Close));
1568        assert_eq!(Action::OpenSubmenu.event(&open), Some(Event::OpenSubmenu));
1569        assert_eq!(Action::CloseSubmenu.event(&open), Some(Event::CloseSubmenu));
1570    }
1571
1572    #[test]
1573    fn close_in_a_submenu_closes_only_the_submenu() {
1574        let mut state = open();
1575        let _ = state.update(Event::Highlight(SHARE));
1576        assert_eq!(Action::Close.event(&state), Some(Event::CloseSubmenu));
1577        let _ = state.update(Event::CloseSubmenu);
1578        assert_eq!(Action::Close.event(&state), Some(Event::Close));
1579    }
1580
1581    #[test]
1582    fn every_action_has_a_name_and_a_sentence() {
1583        for &action in <Action as keys::Action>::ALL {
1584            assert!(!keys::Action::name(action).is_empty());
1585            assert!(keys::Action::description(action).ends_with('.'));
1586        }
1587    }
1588
1589    #[test]
1590    fn builder_defaults() {
1591        let state = state();
1592        let menu: DropdownMenu<'_, u8, ()> = dropdown_menu(&state, text("Open"));
1593        assert_eq!(menu.placement, Placement::new(Side::Bottom, Align::Start));
1594        assert_eq!(menu.width, WIDTH);
1595        assert_eq!(menu.keymap, default_keymap());
1596        assert!(menu.on_event.is_none());
1597
1598        let menu: DropdownMenu<'_, u8, ()> = dropdown_menu(&state, text("Open"))
1599            .side(Side::Top)
1600            .align(Align::End)
1601            .gap(8.0)
1602            .width(300.0)
1603            .keymap(Keymap::new())
1604            .on_event(|_| ());
1605        assert_eq!(menu.placement.side, Side::Top);
1606        assert_eq!(menu.placement.align, Align::End);
1607        assert_eq!(menu.placement.gap, 8.0);
1608        assert_eq!(menu.width, 300.0);
1609        assert!(menu.keymap.is_empty());
1610        assert!(menu.on_event.is_some());
1611    }
1612
1613    #[test]
1614    fn a_slot_is_reserved_for_marks_or_icons() {
1615        let plain: Vec<Entry<u8>> = vec![item(COPY, "Copy"), separator()];
1616        assert!(!reserves_slot(&plain) && !has_marks(&plain));
1617
1618        let icons: Vec<Entry<u8>> = vec![
1619            item(COPY, "Copy").icon(crate::lucide!(Copy)),
1620            item(PASTE, "Paste"),
1621        ];
1622        assert!(reserves_slot(&icons) && !has_marks(&icons));
1623
1624        let marks: Vec<Entry<u8>> = vec![item(COPY, "Copy"), radio_item(SMALL, "Small", false)];
1625        assert!(reserves_slot(&marks) && has_marks(&marks));
1626    }
1627
1628    #[test]
1629    fn the_slot_holds_the_mark_while_checked_and_the_icon_otherwise() {
1630        let entries: Vec<Entry<u8>> = vec![
1631            checkbox_item(GRID, "Grid", true).icon(crate::lucide!(Grid3x3)),
1632            checkbox_item(GRID, "Grid", false).icon(crate::lucide!(Grid3x3)),
1633            radio_item(SMALL, "Small", true),
1634            radio_item(LARGE, "Large", false),
1635            item(COPY, "Copy").icon(crate::lucide!(Copy)),
1636        ];
1637        let slots: Vec<Leading> = entries
1638            .iter()
1639            .filter_map(Entry::as_item)
1640            .map(|item| leading(item, true))
1641            .collect();
1642        assert_eq!(
1643            slots,
1644            [
1645                Leading::Check,
1646                Leading::Empty,
1647                Leading::Dot,
1648                Leading::Empty,
1649                Leading::Empty
1650            ]
1651        );
1652        let unslotted = entries.iter().filter_map(Entry::as_item);
1653        assert!(
1654            unslotted
1655                .map(|item| leading(item, false))
1656                .all(|l| l == Leading::None)
1657        );
1658    }
1659}