summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/menu/src/main.rs44
-rw-r--r--native/src/lib.rs2
-rw-r--r--native/src/menu.rs75
-rw-r--r--src/lib.rs2
-rw-r--r--winit/src/conversion.rs9
5 files changed, 70 insertions, 62 deletions
diff --git a/examples/menu/src/main.rs b/examples/menu/src/main.rs
index f8f70903..9d4ed71b 100644
--- a/examples/menu/src/main.rs
+++ b/examples/menu/src/main.rs
@@ -1,6 +1,7 @@
+use iced::menu::{self, Menu};
use iced::{
executor, Application, Clipboard, Command, Container, Element, Length,
- Menu, Settings, Text,
+ Settings, Text,
};
use iced_native::keyboard::{Hotkey, KeyCode, Modifiers};
@@ -55,47 +56,50 @@ impl Application for App {
alt: false,
};
- Menu::new()
- .dropdown(
+ Menu::with_entries(vec![
+ menu::Entry::dropdown(
"First",
- Menu::new()
- .item(
+ Menu::with_entries(vec![
+ menu::Entry::item(
"One",
Hotkey::new(alt, KeyCode::F1),
Message::MenuActivated(Entry::One),
- )
- .item(
+ ),
+ menu::Entry::item(
"Two",
Hotkey::new(alt, KeyCode::F2),
Message::MenuActivated(Entry::Two),
- )
- .separator()
- .item(
+ ),
+ menu::Entry::Separator,
+ menu::Entry::item(
"Three",
Hotkey::new(alt, KeyCode::F3),
Message::MenuActivated(Entry::Three),
),
- )
- .dropdown(
+ ]),
+ ),
+ menu::Entry::dropdown(
"Second",
- Menu::new()
- .item(
+ Menu::with_entries(vec![
+ menu::Entry::item(
"A",
Hotkey::new(ctrl_shift, KeyCode::A),
Message::MenuActivated(Entry::A),
- )
- .item(
+ ),
+ menu::Entry::item(
"B",
Hotkey::new(ctrl_shift, KeyCode::B),
Message::MenuActivated(Entry::B),
- )
- .separator()
- .item(
+ ),
+ menu::Entry::Separator,
+ menu::Entry::item(
"C",
Hotkey::new(ctrl_shift, KeyCode::C),
Message::MenuActivated(Entry::C),
),
- )
+ ]),
+ ),
+ ])
}
fn update(
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<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 }
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 50952777..8cd6aa70 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -245,7 +245,7 @@ pub use sandbox::Sandbox;
pub use settings::Settings;
pub use runtime::{
- futures, Align, Background, Clipboard, Color, Command, Font,
+ futures, menu, Align, Background, Clipboard, Color, Command, Font,
HorizontalAlignment, Length, Menu, Point, Rectangle, Size, Subscription,
Vector, VerticalAlignment,
};
diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs
index da09ac9d..02c21c59 100644
--- a/winit/src/conversion.rs
+++ b/winit/src/conversion.rs
@@ -3,10 +3,11 @@
//! [`winit`]: https://github.com/rust-windowing/winit
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
use crate::keyboard;
+use crate::menu::{self, Menu};
use crate::mouse;
use crate::touch;
use crate::window;
-use crate::{Event, Menu, MenuEntry, Mode, Point};
+use crate::{Event, Mode, Point};
/// Converts a winit window event into an iced event.
pub fn window_event(
@@ -181,7 +182,7 @@ pub fn menu<Message>(menu: Menu<Message>) -> winit::window::Menu {
for item in menu.iter() {
match item {
- MenuEntry::Item {
+ menu::Entry::Item {
content, hotkey, ..
} => {
let hotkey: Option<&keyboard::Hotkey> = hotkey.as_ref().into();
@@ -191,10 +192,10 @@ pub fn menu<Message>(menu: Menu<Message>) -> winit::window::Menu {
hotkey.map(|h| self::hotkey(*h)),
);
}
- MenuEntry::Dropdown { content, submenu } => {
+ menu::Entry::Dropdown { content, submenu } => {
converted.add_dropdown(content, self::menu(submenu));
}
- MenuEntry::Separator => converted.add_separator(),
+ menu::Entry::Separator => converted.add_separator(),
}
}