From 9f10c5fd9ecf023121bb56502b348d1683e73715 Mon Sep 17 00:00:00 2001 From: Eduardo Flores Date: Mon, 18 Mar 2024 02:49:14 -0700 Subject: [PATCH] docs(menu): update example --- examples/menu/src/main.rs | 50 +++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/examples/menu/src/main.rs b/examples/menu/src/main.rs index d1bc154a..af83e9bf 100644 --- a/examples/menu/src/main.rs +++ b/examples/menu/src/main.rs @@ -3,20 +3,21 @@ //! Application API example +use std::{env, process}; +use std::collections::HashMap; + +use cosmic::{Element, executor}; use cosmic::app::{Command, Core, Settings}; use cosmic::iced::window; +use cosmic::iced_core::{Length, Size}; use cosmic::iced_core::alignment::{Horizontal, Vertical}; use cosmic::iced_core::keyboard::Key; -use cosmic::iced_core::{Length, Size}; +use cosmic::widget::menu::{ItemHeight, ItemWidth, MenuBar, MenuTree}; 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] @@ -43,17 +44,24 @@ fn main() -> Result<(), Box> { pub enum Message { WindowClose, WindowNew, + ToggleHideContent, } /// The [`App`] stores application-specific state. pub struct App { core: Core, + config: Config, key_binds: HashMap, } +pub struct Config { + hide_content: bool, +} + #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum Action { WindowClose, + ToggleHideContent, WindowNew, } @@ -62,6 +70,7 @@ impl MenuAction for Action { fn message(&self, _entity_opt: Option) -> Self::Message { match self { Action::WindowClose => Message::WindowClose, + Action::ToggleHideContent => Message::ToggleHideContent, Action::WindowNew => Message::WindowNew, } } @@ -93,6 +102,7 @@ impl cosmic::Application for App { fn init(core: Core, _input: Self::Flags) -> (Self, Command) { let app = App { core, + config: Config { hide_content: false }, key_binds: key_binds(), }; @@ -100,7 +110,7 @@ impl cosmic::Application for App { } fn header_start(&self) -> Vec> { - vec![menu_bar(&self.key_binds)] + vec![menu_bar(&self.config, &self.key_binds)] } /// Handle application events here. @@ -120,13 +130,18 @@ impl cosmic::Application for App { eprintln!("failed to get current executable path: {}", err); } }, + Message::ToggleHideContent => self.config.hide_content = !self.config.hide_content, } Command::none() } /// Creates a view after each update. fn view(&self) -> Element { - let text = cosmic::widget::text("Menu Example"); + let text = if self.config.hide_content { + cosmic::widget::text("") + } else { + cosmic::widget::text("Menu Example") + }; let centered = cosmic::widget::container(text) .width(Length::Fill) @@ -138,7 +153,7 @@ impl cosmic::Application for App { } } -pub fn menu_bar<'a>(key_binds: &HashMap) -> Element<'a, Message> { +pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap) -> Element<'a, Message> { MenuBar::new(vec![MenuTree::with_children( menu_root("File"), menu_items( @@ -146,14 +161,25 @@ pub fn menu_bar<'a>(key_binds: &HashMap) -> Element<'a, Message vec![ MenuItem::Button("New window", Action::WindowNew), MenuItem::Divider, + MenuItem::Folder( + "View", + vec![ + MenuItem::CheckBox( + "Hide content", + config.hide_content, + Action::ToggleHideContent, + ), + ], + ), + MenuItem::Divider, MenuItem::Button("Quit", Action::WindowClose), ], ), )]) - .item_height(ItemHeight::Dynamic(40)) - .item_width(ItemWidth::Uniform(240)) - .spacing(4.0) - .into() + .item_height(ItemHeight::Dynamic(40)) + .item_width(ItemWidth::Uniform(240)) + .spacing(4.0) + .into() } pub fn key_binds() -> HashMap {