//! Build menus for your application. use crate::keyboard::Hotkey; /// Menu representation. /// /// This can be used by `shell` implementations to create a menu. #[derive(Debug, Clone, PartialEq)] pub struct Menu { items: Vec>, } impl Menu { /// Creates an empty [`Menu`]. pub fn new() -> Self { Menu { items: Vec::new() } } /// Adds an item to the [`Menu`]. pub fn item>( mut self, content: S, hotkey: impl Into>, on_activation: Message, ) -> Self { let content = content.into(); let hotkey = hotkey.into(); self.items.push(MenuEntry::Item { on_activation, content, hotkey, }); self } /// Adds a separator to the [`Menu`]. pub fn separator(mut self) -> Self { self.items.push(MenuEntry::Separator); self } /// Adds a dropdown to the [`Menu`]. pub fn dropdown>( mut self, content: S, submenu: Menu, ) -> Self { let content = content.into(); self.items.push(MenuEntry::Dropdown { content, submenu }); self } /// Returns a [`MenuEntry`] iterator. pub fn iter(self) -> std::vec::IntoIter> { self.items.into_iter() } } /// Represents one of the possible entries used to build a [`Menu`]. #[derive(Debug, Clone, PartialEq)] pub enum MenuEntry { /// Item for a [`Menu`] Item { /// The title of the item content: String, /// The [`Hotkey`] to activate the item, if any hotkey: Option, /// The message generated when the item is activated on_activation: Message, }, /// Dropdown for a [`Menu`] Dropdown { /// Title of the dropdown content: String, /// The submenu of the dropdown submenu: Menu, }, /// Separator for a [`Menu`] Separator, }