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

14
examples/menu/Cargo.toml Normal file
View file

@ -0,0 +1,14 @@
[package]
name = "menu"
version = "0.1.0"
edition = "2021"
[dependencies]
tracing = "0.1.37"
tracing-subscriber = "0.3.17"
tracing-log = "0.2.0"
[dependencies.libcosmic]
path = "../../"
default-features = false
features = ["debug", "winit", "tokio", "xdg-portal"]

178
examples/menu/src/main.rs Normal file
View file

@ -0,0 +1,178 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! Application API example
use cosmic::app::{Command, Core, Settings};
use cosmic::iced::window;
use cosmic::iced_core::alignment::{Horizontal, Vertical};
use cosmic::iced_core::keyboard::Key;
use cosmic::iced_core::{Length, Size};
use cosmic::widget::menu::action::MenuAction;
use cosmic::widget::menu::key_bind::KeyBind;
use cosmic::widget::menu::key_bind::Modifier;
use cosmic::widget::menu::menu_tree::{menu_items, menu_root, MenuItem};
use cosmic::widget::menu::{ItemHeight, ItemWidth, MenuBar, MenuTree};
use cosmic::widget::segmented_button::Entity;
use cosmic::{executor, Element};
use std::collections::HashMap;
use std::{env, process};
/// Runs application with these settings
#[rustfmt::skip]
fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let _ = tracing_log::LogTracer::init();
let settings = Settings::default()
.antialiasing(true)
.client_decorations(true)
.debug(false)
.default_icon_theme("Pop")
.default_text_size(16.0)
.scale_factor(1.0)
.size(Size::new(1024., 768.));
cosmic::app::run::<App>(settings, ())?;
Ok(())
}
/// Messages that are used specifically by our [`App`].
#[derive(Clone, Debug)]
pub enum Message {
WindowClose,
WindowNew,
}
/// The [`App`] stores application-specific state.
pub struct App {
core: Core,
key_binds: HashMap<KeyBind, Action>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Action {
WindowClose,
WindowNew,
}
impl MenuAction for Action {
type Message = Message;
fn message(&self, _entity_opt: Option<Entity>) -> Self::Message {
match self {
Action::WindowClose => Message::WindowClose,
Action::WindowNew => Message::WindowNew,
}
}
}
/// Implement [`cosmic::Application`] to integrate with COSMIC.
impl cosmic::Application for App {
/// Default async executor to use with the app.
type Executor = executor::Default;
/// Argument received [`cosmic::Application::new`].
type Flags = ();
/// Message type specific to our [`App`].
type Message = Message;
/// The unique application ID to supply to the window manager.
const APP_ID: &'static str = "org.cosmic.AppDemo";
fn core(&self) -> &Core {
&self.core
}
fn core_mut(&mut self) -> &mut Core {
&mut self.core
}
/// Creates the application, and optionally emits command on initialize.
fn init(core: Core, _input: Self::Flags) -> (Self, Command<Self::Message>) {
let app = App {
core,
key_binds: key_binds(),
};
(app, Command::none())
}
fn header_start(&self) -> Vec<Element<Self::Message>> {
vec![menu_bar(&self.key_binds)]
}
/// Handle application events here.
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
match message {
Message::WindowClose => {
return window::close(window::Id::MAIN);
}
Message::WindowNew => match env::current_exe() {
Ok(exe) => match process::Command::new(&exe).spawn() {
Ok(_child) => {}
Err(err) => {
eprintln!("failed to execute {:?}: {}", exe, err);
}
},
Err(err) => {
eprintln!("failed to get current executable path: {}", err);
}
},
}
Command::none()
}
/// Creates a view after each update.
fn view(&self) -> Element<Self::Message> {
let text = cosmic::widget::text("Menu Example");
let centered = cosmic::widget::container(text)
.width(Length::Fill)
.height(Length::Shrink)
.align_x(Horizontal::Center)
.align_y(Vertical::Center);
Element::from(centered)
}
}
pub fn menu_bar<'a>(key_binds: &HashMap<KeyBind, Action>) -> Element<'a, Message> {
MenuBar::new(vec![MenuTree::with_children(
menu_root("File"),
menu_items(
key_binds,
vec![
MenuItem::Action("New window", Action::WindowNew),
MenuItem::Separator,
MenuItem::Action("Quit", Action::WindowClose),
],
),
)])
.item_height(ItemHeight::Dynamic(40))
.item_width(ItemWidth::Uniform(240))
.spacing(4.0)
.into()
}
pub fn key_binds() -> HashMap<KeyBind, Action> {
let mut key_binds = HashMap::new();
macro_rules! bind {
([$($modifier:ident),* $(,)?], $key:expr, $action:ident) => {{
key_binds.insert(
KeyBind {
modifiers: vec![$(Modifier::$modifier),*],
key: $key,
},
Action::$action,
);
}};
}
bind!([Ctrl], Key::Character("w".into()), WindowClose);
bind!([Ctrl, Shift], Key::Character("n".into()), WindowNew);
key_binds
}

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