Make Menu API a bit more functional

This commit is contained in:
Héctor Ramón Jiménez 2021-07-12 21:38:54 +02:00
parent c4552a72d4
commit 1428e9180a
No known key found for this signature in database
GPG key ID: 44B88EB52AB1EE8D
5 changed files with 70 additions and 62 deletions

View file

@ -1,6 +1,7 @@
use iced::menu::{self, Menu};
use iced::{ use iced::{
executor, Application, Clipboard, Command, Container, Element, Length, executor, Application, Clipboard, Command, Container, Element, Length,
Menu, Settings, Text, Settings, Text,
}; };
use iced_native::keyboard::{Hotkey, KeyCode, Modifiers}; use iced_native::keyboard::{Hotkey, KeyCode, Modifiers};
@ -55,47 +56,50 @@ impl Application for App {
alt: false, alt: false,
}; };
Menu::new() Menu::with_entries(vec![
.dropdown( menu::Entry::dropdown(
"First", "First",
Menu::new() Menu::with_entries(vec![
.item( menu::Entry::item(
"One", "One",
Hotkey::new(alt, KeyCode::F1), Hotkey::new(alt, KeyCode::F1),
Message::MenuActivated(Entry::One), Message::MenuActivated(Entry::One),
) ),
.item( menu::Entry::item(
"Two", "Two",
Hotkey::new(alt, KeyCode::F2), Hotkey::new(alt, KeyCode::F2),
Message::MenuActivated(Entry::Two), Message::MenuActivated(Entry::Two),
) ),
.separator() menu::Entry::Separator,
.item( menu::Entry::item(
"Three", "Three",
Hotkey::new(alt, KeyCode::F3), Hotkey::new(alt, KeyCode::F3),
Message::MenuActivated(Entry::Three), Message::MenuActivated(Entry::Three),
), ),
) ]),
.dropdown( ),
menu::Entry::dropdown(
"Second", "Second",
Menu::new() Menu::with_entries(vec![
.item( menu::Entry::item(
"A", "A",
Hotkey::new(ctrl_shift, KeyCode::A), Hotkey::new(ctrl_shift, KeyCode::A),
Message::MenuActivated(Entry::A), Message::MenuActivated(Entry::A),
) ),
.item( menu::Entry::item(
"B", "B",
Hotkey::new(ctrl_shift, KeyCode::B), Hotkey::new(ctrl_shift, KeyCode::B),
Message::MenuActivated(Entry::B), Message::MenuActivated(Entry::B),
) ),
.separator() menu::Entry::Separator,
.item( menu::Entry::item(
"C", "C",
Hotkey::new(ctrl_shift, KeyCode::C), Hotkey::new(ctrl_shift, KeyCode::C),
Message::MenuActivated(Entry::C), Message::MenuActivated(Entry::C),
), ),
) ]),
),
])
} }
fn update( fn update(

View file

@ -76,7 +76,7 @@ pub use element::Element;
pub use event::Event; pub use event::Event;
pub use hasher::Hasher; pub use hasher::Hasher;
pub use layout::Layout; pub use layout::Layout;
pub use menu::{Menu, MenuEntry}; pub use menu::Menu;
pub use overlay::Overlay; pub use overlay::Overlay;
pub use program::Program; pub use program::Program;
pub use renderer::Renderer; pub use renderer::Renderer;

View file

@ -6,60 +6,35 @@ use crate::keyboard::Hotkey;
/// This can be used by `shell` implementations to create a menu. /// This can be used by `shell` implementations to create a menu.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct Menu<Message> { pub struct Menu<Message> {
items: Vec<MenuEntry<Message>>, entries: Vec<Entry<Message>>,
} }
impl<Message> Menu<Message> { impl<Message> Menu<Message> {
/// Creates an empty [`Menu`]. /// Creates an empty [`Menu`].
pub fn new() -> Self { pub fn new() -> Self {
Menu { items: Vec::new() } Self::with_entries(Vec::new())
} }
/// Adds an item to the [`Menu`]. /// Creates a new [`Menu`] with the given entries.
pub fn item<S: Into<String>>( pub fn with_entries(entries: Vec<Entry<Message>>) -> Self {
mut self, Self { entries }
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`]. /// Adds an [`Entry`] to the [`Menu`].
pub fn separator(mut self) -> Self { pub fn push(mut self, entry: Entry<Message>) -> Self {
self.items.push(MenuEntry::Separator); self.entries.push(entry);
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 self
} }
/// Returns a [`MenuEntry`] iterator. /// Returns a [`MenuEntry`] iterator.
pub fn iter(self) -> std::vec::IntoIter<MenuEntry<Message>> { pub fn iter(self) -> impl Iterator<Item = Entry<Message>> {
self.items.into_iter() self.entries.into_iter()
} }
} }
/// Represents one of the possible entries used to build a [`Menu`]. /// Represents one of the possible entries used to build a [`Menu`].
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum MenuEntry<Message> { pub enum Entry<Message> {
/// Item for a [`Menu`] /// Item for a [`Menu`]
Item { Item {
/// The title of the item /// The title of the item
@ -79,3 +54,31 @@ pub enum MenuEntry<Message> {
/// Separator for a [`Menu`] /// Separator for a [`Menu`]
Separator, 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 }
}
}

View file

@ -245,7 +245,7 @@ pub use sandbox::Sandbox;
pub use settings::Settings; pub use settings::Settings;
pub use runtime::{ 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, HorizontalAlignment, Length, Menu, Point, Rectangle, Size, Subscription,
Vector, VerticalAlignment, Vector, VerticalAlignment,
}; };

View file

@ -3,10 +3,11 @@
//! [`winit`]: https://github.com/rust-windowing/winit //! [`winit`]: https://github.com/rust-windowing/winit
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
use crate::keyboard; use crate::keyboard;
use crate::menu::{self, Menu};
use crate::mouse; use crate::mouse;
use crate::touch; use crate::touch;
use crate::window; use crate::window;
use crate::{Event, Menu, MenuEntry, Mode, Point}; use crate::{Event, Mode, Point};
/// Converts a winit window event into an iced event. /// Converts a winit window event into an iced event.
pub fn window_event( pub fn window_event(
@ -181,7 +182,7 @@ pub fn menu<Message>(menu: Menu<Message>) -> winit::window::Menu {
for item in menu.iter() { for item in menu.iter() {
match item { match item {
MenuEntry::Item { menu::Entry::Item {
content, hotkey, .. content, hotkey, ..
} => { } => {
let hotkey: Option<&keyboard::Hotkey> = hotkey.as_ref().into(); 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)), hotkey.map(|h| self::hotkey(*h)),
); );
} }
MenuEntry::Dropdown { content, submenu } => { menu::Entry::Dropdown { content, submenu } => {
converted.add_dropdown(content, self::menu(submenu)); converted.add_dropdown(content, self::menu(submenu));
} }
MenuEntry::Separator => converted.add_separator(), menu::Entry::Separator => converted.add_separator(),
} }
} }