Store and synchronize Menu in application::State

This commit is contained in:
Héctor Ramón Jiménez 2021-07-12 22:28:18 +02:00
parent b3ff522c18
commit 31997d255f
No known key found for this signature in database
GPG key ID: 44B88EB52AB1EE8D
5 changed files with 64 additions and 13 deletions

View file

@ -4,11 +4,17 @@ use crate::keyboard::Hotkey;
/// Menu representation.
///
/// This can be used by `shell` implementations to create a menu.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone)]
pub struct Menu<Message> {
entries: Vec<Entry<Message>>,
}
impl<Message> PartialEq for Menu<Message> {
fn eq(&self, other: &Self) -> bool {
self.entries == other.entries
}
}
impl<Message> Menu<Message> {
/// Creates an empty [`Menu`].
pub fn new() -> Self {
@ -27,13 +33,13 @@ impl<Message> Menu<Message> {
}
/// Returns a [`MenuEntry`] iterator.
pub fn iter(self) -> impl Iterator<Item = Entry<Message>> {
self.entries.into_iter()
pub fn iter(&self) -> impl Iterator<Item = &Entry<Message>> {
self.entries.iter()
}
}
/// Represents one of the possible entries used to build a [`Menu`].
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone)]
pub enum Entry<Message> {
/// Item for a [`Menu`]
Item {
@ -82,3 +88,29 @@ impl<Message> Entry<Message> {
Entry::Dropdown { content, submenu }
}
}
impl<Message> PartialEq for Entry<Message> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
Entry::Item {
content, hotkey, ..
},
Entry::Item {
content: other_content,
hotkey: other_hotkey,
..
},
) => content == other_content && hotkey == other_hotkey,
(
Entry::Dropdown { content, submenu },
Entry::Dropdown {
content: other_content,
submenu: other_submenu,
},
) => content == other_content && submenu == other_submenu,
(Entry::Separator, Entry::Separator) => true,
_ => false,
}
}
}