summaryrefslogtreecommitdiffstats
path: root/native/src/menu.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2021-07-12 21:38:54 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2021-07-12 21:38:54 +0200
commit1428e9180ae9f4edbf22514bb74c5c7e9df9c712 (patch)
treea180d25cec5934b0216adf783311f9acce8e0cea /native/src/menu.rs
parentc4552a72d43e5f79faa7c64634be539d81f995b9 (diff)
downloadiced-1428e9180ae9f4edbf22514bb74c5c7e9df9c712.tar.gz
iced-1428e9180ae9f4edbf22514bb74c5c7e9df9c712.tar.bz2
iced-1428e9180ae9f4edbf22514bb74c5c7e9df9c712.zip
Make `Menu` API a bit more functional
Diffstat (limited to '')
-rw-r--r--native/src/menu.rs75
1 files changed, 39 insertions, 36 deletions
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<Message> {
- items: Vec<MenuEntry<Message>>,
+ entries: Vec<Entry<Message>>,
}
impl<Message> Menu<Message> {
/// 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<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
+ /// Creates a new [`Menu`] with the given entries.
+ pub fn with_entries(entries: Vec<Entry<Message>>) -> 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<S: Into<String>>(
- mut self,
- content: S,
- submenu: Menu<Message>,
- ) -> 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<Message>) -> Self {
+ self.entries.push(entry);
self
}
/// Returns a [`MenuEntry`] iterator.
- pub fn iter(self) -> std::vec::IntoIter<MenuEntry<Message>> {
- self.items.into_iter()
+ pub fn iter(self) -> impl Iterator<Item = Entry<Message>> {
+ self.entries.into_iter()
}
}
/// Represents one of the possible entries used to build a [`Menu`].
#[derive(Debug, Clone, PartialEq)]
-pub enum MenuEntry<Message> {
+pub enum Entry<Message> {
/// Item for a [`Menu`]
Item {
/// The title of the item
@@ -79,3 +54,31 @@ pub enum MenuEntry<Message> {
/// Separator for a [`Menu`]
Separator,
}
+
+impl<Message> Entry<Message> {
+ /// Creates an [`Entry::Item`].
+ pub fn item<S: Into<String>>(
+ content: S,
+ hotkey: impl Into<Option<Hotkey>>,
+ 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<S: Into<String>>(
+ content: S,
+ submenu: Menu<Message>,
+ ) -> Self {
+ let content = content.into();
+
+ Entry::Dropdown { content, submenu }
+ }
+}