diff options
author | 2021-07-05 16:23:44 -0300 | |
---|---|---|
committer | 2021-07-05 16:24:10 -0300 | |
commit | 9fc5ad23edca93553137100d167de7b69e88f785 (patch) | |
tree | b9244861c70ff0f1718aabd5fa6e9b835b3857d9 /native | |
parent | 4994d34abab3222f9a8fd7a9a3e63f969ca97ffc (diff) | |
download | iced-9fc5ad23edca93553137100d167de7b69e88f785.tar.gz iced-9fc5ad23edca93553137100d167de7b69e88f785.tar.bz2 iced-9fc5ad23edca93553137100d167de7b69e88f785.zip |
Initial menu implementation
Diffstat (limited to 'native')
-rw-r--r-- | native/src/lib.rs | 2 | ||||
-rw-r--r-- | native/src/menu.rs | 81 |
2 files changed, 83 insertions, 0 deletions
diff --git a/native/src/lib.rs b/native/src/lib.rs index cd214e36..56a933f0 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -37,6 +37,7 @@ pub mod clipboard; pub mod event; pub mod keyboard; pub mod layout; +pub mod menu; pub mod mouse; pub mod overlay; pub mod program; @@ -75,6 +76,7 @@ pub use element::Element; pub use event::Event; pub use hasher::Hasher; pub use layout::Layout; +pub use menu::{Menu, MenuEntry}; pub use overlay::Overlay; pub use program::Program; pub use renderer::Renderer; diff --git a/native/src/menu.rs b/native/src/menu.rs new file mode 100644 index 00000000..6c73cb32 --- /dev/null +++ b/native/src/menu.rs @@ -0,0 +1,81 @@ +//! 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<Message> { + items: Vec<MenuEntry<Message>>, +} + +impl<Message> Menu<Message> { + /// Creates an empty [`Menu`]. + pub fn new() -> Self { + Menu { items: Vec::new() } + } + + /// Adds an item to the [`Menu`]. + pub fn item<S: Into<String>>( + mut self, + content: S, + hotkey: impl Into<Option<Hotkey>>, + 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<S: Into<String>>( + mut self, + content: S, + submenu: Menu<Message>, + ) -> Self { + let content = content.into(); + + self.items.push(MenuEntry::Dropdown { content, submenu }); + self + } + + /// Returns a [`MenuEntry`] iterator. + pub fn iter(self) -> std::vec::IntoIter<MenuEntry<Message>> { + self.items.into_iter() + } +} + +/// Represents one of the possible entries used to build a [`Menu`]. +#[derive(Debug, Clone, PartialEq)] +pub enum MenuEntry<Message> { + /// Item for a [`Menu`] + Item { + /// The title of the item + content: String, + /// The [`Hotkey`] to activate the item, if any + hotkey: Option<Hotkey>, + /// 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<Message>, + }, + /// Separator for a [`Menu`] + Separator, +} |