Replace content with title in menu module

This commit is contained in:
Héctor Ramón Jiménez 2021-07-19 21:01:24 +07:00
parent 3099f36100
commit a2f49a74d0
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 22 additions and 29 deletions

View file

@ -59,7 +59,7 @@ pub enum Entry<Message> {
/// Item for a [`Menu`] /// Item for a [`Menu`]
Item { Item {
/// The title of the item /// The title of the item
content: String, title: String,
/// The [`Hotkey`] to activate the item, if any /// The [`Hotkey`] to activate the item, if any
hotkey: Option<Hotkey>, hotkey: Option<Hotkey>,
/// The message generated when the item is activated /// The message generated when the item is activated
@ -68,7 +68,7 @@ pub enum Entry<Message> {
/// Dropdown for a [`Menu`] /// Dropdown for a [`Menu`]
Dropdown { Dropdown {
/// Title of the dropdown /// Title of the dropdown
content: String, title: String,
/// The submenu of the dropdown /// The submenu of the dropdown
submenu: Menu<Message>, submenu: Menu<Message>,
}, },
@ -79,43 +79,40 @@ pub enum Entry<Message> {
impl<Message> Entry<Message> { impl<Message> Entry<Message> {
/// Creates an [`Entry::Item`]. /// Creates an [`Entry::Item`].
pub fn item<S: Into<String>>( pub fn item<S: Into<String>>(
content: S, title: S,
hotkey: impl Into<Option<Hotkey>>, hotkey: impl Into<Option<Hotkey>>,
on_activation: Message, on_activation: Message,
) -> Self { ) -> Self {
let content = content.into(); let title = title.into();
let hotkey = hotkey.into(); let hotkey = hotkey.into();
Self::Item { Self::Item {
content, title,
hotkey, hotkey,
on_activation, on_activation,
} }
} }
/// Creates an [`Entry::Dropdown`]. /// Creates an [`Entry::Dropdown`].
pub fn dropdown<S: Into<String>>( pub fn dropdown<S: Into<String>>(title: S, submenu: Menu<Message>) -> Self {
content: S, let title = title.into();
submenu: Menu<Message>,
) -> Self {
let content = content.into();
Self::Dropdown { content, submenu } Self::Dropdown { title, submenu }
} }
fn map<B>(self, f: &impl Fn(Message) -> B) -> Entry<B> { fn map<B>(self, f: &impl Fn(Message) -> B) -> Entry<B> {
match self { match self {
Self::Item { Self::Item {
content, title,
hotkey, hotkey,
on_activation, on_activation,
} => Entry::Item { } => Entry::Item {
content, title,
hotkey, hotkey,
on_activation: f(on_activation), on_activation: f(on_activation),
}, },
Self::Dropdown { content, submenu } => Entry::Dropdown { Self::Dropdown { title, submenu } => Entry::Dropdown {
content, title,
submenu: submenu.map(f), submenu: submenu.map(f),
}, },
Self::Separator => Entry::Separator, Self::Separator => Entry::Separator,
@ -127,22 +124,20 @@ impl<Message> PartialEq for Entry<Message> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
match (self, other) { match (self, other) {
( (
Entry::Item { title, hotkey, .. },
Entry::Item { Entry::Item {
content, hotkey, .. title: other_title,
},
Entry::Item {
content: other_content,
hotkey: other_hotkey, hotkey: other_hotkey,
.. ..
}, },
) => content == other_content && hotkey == other_hotkey, ) => title == other_title && hotkey == other_hotkey,
( (
Entry::Dropdown { content, submenu }, Entry::Dropdown { title, submenu },
Entry::Dropdown { Entry::Dropdown {
content: other_content, title: other_title,
submenu: other_submenu, submenu: other_submenu,
}, },
) => content == other_content && submenu == other_submenu, ) => title == other_title && submenu == other_submenu,
(Entry::Separator, Entry::Separator) => true, (Entry::Separator, Entry::Separator) => true,
_ => false, _ => false,
} }

View file

@ -187,19 +187,17 @@ pub fn menu<Message>(menu: &Menu<Message>) -> winit::window::Menu {
for item in menu.iter() { for item in menu.iter() {
match item { match item {
menu::Entry::Item { menu::Entry::Item { title, hotkey, .. } => {
content, hotkey, .. converted.add_item(id, title, hotkey.map(self::hotkey));
} => {
converted.add_item(id, content, hotkey.map(self::hotkey));
id += 1; id += 1;
} }
menu::Entry::Dropdown { content, submenu } => { menu::Entry::Dropdown { title, submenu } => {
let mut converted_submenu = winit::window::Menu::new(); let mut converted_submenu = winit::window::Menu::new();
let n_children = let n_children =
menu_i(&mut converted_submenu, id, submenu); menu_i(&mut converted_submenu, id, submenu);
converted.add_dropdown(content, converted_submenu); converted.add_dropdown(title, converted_submenu);
id += n_children; id += n_children;
} }