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

@ -54,7 +54,9 @@
//! ```
//!
pub mod action;
mod flex;
pub mod key_bind;
pub mod menu_bar;
mod menu_inner;
pub mod menu_tree;

View file

@ -0,0 +1,7 @@
use crate::widget::segmented_button::Entity;
pub trait MenuAction: Clone + Copy + Eq + PartialEq {
type Message;
fn message(&self, entity: Option<Entity>) -> Self::Message;
}

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),
}
}
}

View file

@ -2,7 +2,14 @@
//! A tree structure for constructing a hierarchical menu
use crate::widget::menu::action::MenuAction;
use crate::widget::menu::key_bind::KeyBind;
use crate::{theme, widget};
use iced_widget::core::{renderer, Element};
use iced_widget::horizontal_rule;
use std::borrow::Cow;
use std::collections::HashMap;
/// Nested menu is essentially a tree of items, a menu is a collection of items
/// a menu itself can also be an item of another menu.
///
@ -129,3 +136,102 @@ where
Self::new(value)
}
}
macro_rules! menu_button {
($($x:expr),+ $(,)?) => (
widget::button(
widget::Row::with_children(
vec![$(Element::from($x)),+]
)
.align_items(Alignment::Center)
.height(Length::Fill)
.width(Length::Fill)
)
.height(Length::Fixed(36.0))
.padding([4, 16])
.width(Length::Fill)
.style(theme::Button::MenuItem)
);
}
pub enum MenuItem<A: MenuAction, L: Into<Cow<'static, str>>> {
Action(L, A),
Separator,
}
pub fn menu_root<'a, Message, Renderer: renderer::Renderer>(
label: impl Into<Cow<'a, str>> + 'a,
) -> iced::Element<'a, Message, crate::Theme, Renderer>
where
Element<'a, Message, crate::Theme, Renderer>:
From<widget::button::Button<'a, Message, crate::Theme, iced::Renderer>>,
{
widget::button(widget::text(label))
.padding([4, 12])
.style(theme::Button::MenuRoot)
.into()
}
pub fn menu_items<
'a,
A: MenuAction<Message = Message>,
L: Into<Cow<'static, str>> + 'static,
Message: 'a,
Renderer: renderer::Renderer + 'a,
>(
key_binds: &HashMap<KeyBind, A>,
children: Vec<MenuItem<A, L>>,
) -> Vec<MenuTree<'a, Message, Renderer>>
where
Element<'a, Message, crate::Theme, Renderer>:
From<widget::button::Button<'a, Message, crate::Theme, iced::Renderer>>,
{
fn find_key<A: MenuAction>(action: &A, key_binds: &HashMap<KeyBind, A>) -> String {
for (key_bind, key_action) in key_binds.iter() {
if action == key_action {
return key_bind.to_string();
}
}
String::new()
}
let size = children.len();
children
.into_iter()
.enumerate()
.flat_map(|(i, item)| {
let mut trees = vec![];
match item {
MenuItem::Action(label, action) => {
let key = find_key(&action, key_binds);
let menu_button: iced::Element<'a, Message, crate::Theme, Renderer> =
widget::button::<Message, crate::Theme>(
widget::Row::with_children(vec![
widget::text(label).into(),
widget::horizontal_space(iced_core::Length::Fill).into(),
widget::text(key).into(),
])
.align_items(iced_core::Alignment::Center)
.height(iced_core::Length::Fill)
.width(iced_core::Length::Fill),
)
.on_press(action.message(None))
.height(iced_core::Length::Fixed(36.0))
.padding([4, 16])
.width(iced_core::Length::Fill)
.style(theme::Button::MenuItem)
.into();
trees.push(MenuTree::<Message, Renderer>::new(menu_button));
}
MenuItem::Separator => {
if i != size - 1 {
trees.push(MenuTree::<Message, Renderer>::new(horizontal_rule(1)));
}
}
}
trees
})
.collect()
}