From 0b47efe1de82d3dfb6abaa30e6be28bab15172f9 Mon Sep 17 00:00:00 2001 From: Eduardo Flores Date: Sat, 16 Mar 2024 15:38:26 -0700 Subject: [PATCH] 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. --- examples/menu/Cargo.toml | 14 +++ examples/menu/src/main.rs | 178 +++++++++++++++++++++++++++++++++++ src/widget/menu.rs | 2 + src/widget/menu/action.rs | 7 ++ src/widget/menu/key_bind.rs | 39 ++++++++ src/widget/menu/menu_tree.rs | 106 +++++++++++++++++++++ 6 files changed, 346 insertions(+) create mode 100644 examples/menu/Cargo.toml create mode 100644 examples/menu/src/main.rs create mode 100644 src/widget/menu/action.rs create mode 100644 src/widget/menu/key_bind.rs diff --git a/examples/menu/Cargo.toml b/examples/menu/Cargo.toml new file mode 100644 index 00000000..44ece16e --- /dev/null +++ b/examples/menu/Cargo.toml @@ -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"] diff --git a/examples/menu/src/main.rs b/examples/menu/src/main.rs new file mode 100644 index 00000000..f0eda340 --- /dev/null +++ b/examples/menu/src/main.rs @@ -0,0 +1,178 @@ +// Copyright 2023 System76 +// 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> { + 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::(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, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum Action { + WindowClose, + WindowNew, +} + +impl MenuAction for Action { + type Message = Message; + fn message(&self, _entity_opt: Option) -> 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) { + let app = App { + core, + key_binds: key_binds(), + }; + + (app, Command::none()) + } + + fn header_start(&self) -> Vec> { + vec![menu_bar(&self.key_binds)] + } + + /// Handle application events here. + fn update(&mut self, message: Self::Message) -> Command { + 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 { + 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) -> 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 { + 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 +} diff --git a/src/widget/menu.rs b/src/widget/menu.rs index 0d786eea..b5ebac9f 100644 --- a/src/widget/menu.rs +++ b/src/widget/menu.rs @@ -54,7 +54,9 @@ //! ``` //! +pub mod action; mod flex; +pub mod key_bind; pub mod menu_bar; mod menu_inner; pub mod menu_tree; diff --git a/src/widget/menu/action.rs b/src/widget/menu/action.rs new file mode 100644 index 00000000..086210ea --- /dev/null +++ b/src/widget/menu/action.rs @@ -0,0 +1,7 @@ +use crate::widget::segmented_button::Entity; + +pub trait MenuAction: Clone + Copy + Eq + PartialEq { + type Message; + + fn message(&self, entity: Option) -> Self::Message; +} diff --git a/src/widget/menu/key_bind.rs b/src/widget/menu/key_bind.rs new file mode 100644 index 00000000..63bfb6b1 --- /dev/null +++ b/src/widget/menu/key_bind.rs @@ -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, + 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), + } + } +} diff --git a/src/widget/menu/menu_tree.rs b/src/widget/menu/menu_tree.rs index e30f331d..595a1235 100644 --- a/src/widget/menu/menu_tree.rs +++ b/src/widget/menu/menu_tree.rs @@ -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>> { + Action(L, A), + Separator, +} + +pub fn menu_root<'a, Message, Renderer: renderer::Renderer>( + label: impl Into> + 'a, +) -> iced::Element<'a, Message, crate::Theme, Renderer> +where + Element<'a, Message, crate::Theme, Renderer>: + From>, +{ + widget::button(widget::text(label)) + .padding([4, 12]) + .style(theme::Button::MenuRoot) + .into() +} + +pub fn menu_items< + 'a, + A: MenuAction, + L: Into> + 'static, + Message: 'a, + Renderer: renderer::Renderer + 'a, +>( + key_binds: &HashMap, + children: Vec>, +) -> Vec> +where + Element<'a, Message, crate::Theme, Renderer>: + From>, +{ + fn find_key(action: &A, key_binds: &HashMap) -> 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::( + 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::::new(menu_button)); + } + MenuItem::Separator => { + if i != size - 1 { + trees.push(MenuTree::::new(horizontal_rule(1))); + } + } + } + trees + }) + .collect() +}