From 1428e9180ae9f4edbf22514bb74c5c7e9df9c712 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 21:38:54 +0200 Subject: Make `Menu` API a bit more functional --- native/src/lib.rs | 2 +- native/src/menu.rs | 75 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 40 insertions(+), 37 deletions(-) (limited to 'native') diff --git a/native/src/lib.rs b/native/src/lib.rs index 56a933f0..564f2514 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -76,7 +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 menu::Menu; pub use overlay::Overlay; pub use program::Program; pub use renderer::Renderer; diff --git a/native/src/menu.rs b/native/src/menu.rs index 6c73cb32..fe770216 100644 --- a/native/src/menu.rs +++ b/native/src/menu.rs @@ -6,60 +6,35 @@ use crate::keyboard::Hotkey; /// This can be used by `shell` implementations to create a menu. #[derive(Debug, Clone, PartialEq)] pub struct Menu { - items: Vec>, + entries: Vec>, } impl Menu { /// Creates an empty [`Menu`]. pub fn new() -> Self { - Menu { items: Vec::new() } + Self::with_entries(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 + /// Creates a new [`Menu`] with the given entries. + pub fn with_entries(entries: Vec>) -> Self { + Self { entries } } - /// 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 }); + /// Adds an [`Entry`] to the [`Menu`]. + pub fn push(mut self, entry: Entry) -> Self { + self.entries.push(entry); self } /// Returns a [`MenuEntry`] iterator. - pub fn iter(self) -> std::vec::IntoIter> { - self.items.into_iter() + pub fn iter(self) -> impl Iterator> { + self.entries.into_iter() } } /// Represents one of the possible entries used to build a [`Menu`]. #[derive(Debug, Clone, PartialEq)] -pub enum MenuEntry { +pub enum Entry { /// Item for a [`Menu`] Item { /// The title of the item @@ -79,3 +54,31 @@ pub enum MenuEntry { /// Separator for a [`Menu`] Separator, } + +impl Entry { + /// Creates an [`Entry::Item`]. + pub fn item>( + content: S, + hotkey: impl Into>, + on_activation: Message, + ) -> Self { + let content = content.into(); + let hotkey = hotkey.into(); + + Entry::Item { + content, + hotkey, + on_activation, + } + } + + /// Creates an [`Entry::Dropdown`]. + pub fn dropdown>( + content: S, + submenu: Menu, + ) -> Self { + let content = content.into(); + + Entry::Dropdown { content, submenu } + } +} -- cgit