improv(menu): simplify menu construction.

- Added `MenuAction` trait to call the `message` method on button press.
- Added two new methods to construct a MenuTree.
- Added MenuItem enum to represent an action or a separator in a MenuTree.
- Added menu example.
- Moved Modifier enum and KeyBind struct to libcosmic.
- Moved menu_button macro to libcosmic.
This commit is contained in:
Eduardo Flores 2024-03-16 15:38:26 -07:00 committed by Jeremy Soller
parent 9e6d94c7eb
commit 0b47efe1de
6 changed files with 346 additions and 0 deletions

View file

@ -0,0 +1,39 @@
use iced_core::keyboard::{Key, Modifiers};
use std::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Modifier {
Super,
Ctrl,
Alt,
Shift,
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct KeyBind {
pub modifiers: Vec<Modifier>,
pub key: Key,
}
impl KeyBind {
pub fn matches(&self, modifiers: Modifiers, key: &Key) -> bool {
key == &self.key
&& modifiers.logo() == self.modifiers.contains(&Modifier::Super)
&& modifiers.control() == self.modifiers.contains(&Modifier::Ctrl)
&& modifiers.alt() == self.modifiers.contains(&Modifier::Alt)
&& modifiers.shift() == self.modifiers.contains(&Modifier::Shift)
}
}
impl fmt::Display for KeyBind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for modifier in self.modifiers.iter() {
write!(f, "{:?} + ", modifier)?;
}
match &self.key {
Key::Character(c) => write!(f, "{}", c.to_uppercase()),
Key::Named(named) => write!(f, "{:?}", named),
other => write!(f, "{:?}", other),
}
}
}