feat: responsive menu

This commit is contained in:
Ashley Wulber 2025-04-16 13:07:03 -04:00 • committed by Michael Murphy
parent 1c2c859eac
commit 9336731c7d
4 changed files with 1028 additions and 272 deletions

1026
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -46,7 +46,7 @@ features = ["syntect", "vi"]
[dependencies.libcosmic] [dependencies.libcosmic]
git = "https://github.com/pop-os/libcosmic.git" git = "https://github.com/pop-os/libcosmic.git"
default-features = false default-features = false
features = ["a11y", "multi-window", "tokio", "winit"] features = ["a11y", "multi-window", "tokio", "winit", "surface-message"]
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
fork = "0.2" fork = "0.2"

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use cosmic::surface;
use cosmic::widget::menu::action::MenuAction; use cosmic::widget::menu::action::MenuAction;
use cosmic::widget::menu::key_bind::KeyBind; use cosmic::widget::menu::key_bind::KeyBind;
use cosmic::widget::segmented_button::Entity; use cosmic::widget::segmented_button::Entity;
@ -389,6 +390,7 @@ pub enum Message {
SaveAsResult(segmented_button::Entity, DialogResult), SaveAsResult(segmented_button::Entity, DialogResult),
Scroll(f32), Scroll(f32),
SelectAll, SelectAll,
Surface(surface::Action),
SystemThemeModeChange(cosmic_theme::ThemeMode), SystemThemeModeChange(cosmic_theme::ThemeMode),
SyntaxTheme(usize, bool), SyntaxTheme(usize, bool),
TabActivate(segmented_button::Entity), TabActivate(segmented_button::Entity),
@ -2444,6 +2446,11 @@ impl Application for App {
}); });
} }
} }
Message::Surface(a) => {
return cosmic::task::message(cosmic::Action::Cosmic(
cosmic::app::Action::Surface(a),
));
}
Message::SystemThemeModeChange(_theme_mode) => { Message::SystemThemeModeChange(_theme_mode) => {
return self.update_config(); return self.update_config();
} }
@ -2757,6 +2764,7 @@ impl Application for App {
fn header_start(&self) -> Vec<Element<Message>> { fn header_start(&self) -> Vec<Element<Message>> {
vec![menu_bar( vec![menu_bar(
&self.core,
&self.config, &self.config,
&self.config_state, &self.config_state,
&self.key_binds, &self.key_binds,

View file

@ -3,19 +3,23 @@
use cosmic::widget::menu::key_bind::KeyBind; use cosmic::widget::menu::key_bind::KeyBind;
use cosmic::widget::menu::{items as menu_items, root as menu_root, Item as MenuItem}; use cosmic::widget::menu::{items as menu_items, root as menu_root, Item as MenuItem};
use cosmic::{ use cosmic::{
app::Core,
iced::{widget::column, Background, Length}, iced::{widget::column, Background, Length},
iced_core::Border, iced_core::Border,
widget::{ widget::{
self, divider, horizontal_space, self, divider, horizontal_space,
menu::{menu_button, ItemHeight, ItemWidth, MenuBar, Tree as MenuTree}, menu::{menu_button, ItemHeight, ItemWidth, MenuBar, Tree as MenuTree},
segmented_button, responsive_menu_bar, segmented_button,
}, },
Element, Element,
}; };
use std::{collections::HashMap, path::PathBuf}; use std::{collections::HashMap, path::PathBuf, sync::LazyLock};
use crate::{fl, Action, Config, ConfigState, Message}; use crate::{fl, Action, Config, ConfigState, Message};
static MENU_ID: LazyLock<cosmic::widget::Id> =
LazyLock::new(|| cosmic::widget::Id::new("responsive-menu"));
pub fn context_menu<'a>( pub fn context_menu<'a>(
key_binds: &HashMap<KeyBind, Action>, key_binds: &HashMap<KeyBind, Action>,
entity: segmented_button::Entity, entity: segmented_button::Entity,
@ -67,6 +71,7 @@ pub fn context_menu<'a>(
} }
pub fn menu_bar<'a>( pub fn menu_bar<'a>(
core: &Core,
config: &Config, config: &Config,
config_state: &ConfigState, config_state: &ConfigState,
key_binds: &HashMap<KeyBind, Action>, key_binds: &HashMap<KeyBind, Action>,
@ -119,11 +124,18 @@ pub fn menu_bar<'a>(
)); ));
} }
MenuBar::new(vec![ responsive_menu_bar()
MenuTree::with_children( .item_height(ItemHeight::Dynamic(40))
menu_root(fl!("file")), .item_width(ItemWidth::Uniform(320))
menu_items( .spacing(4.0)
.into_element(
core,
key_binds, key_binds,
MENU_ID.clone(),
Message::Surface,
vec![
(
(fl!("file")),
vec![ vec![
MenuItem::Button(fl!("new-file"), None, Action::NewFile), MenuItem::Button(fl!("new-file"), None, Action::NewFile),
MenuItem::Button(fl!("new-window"), None, Action::NewWindow), MenuItem::Button(fl!("new-window"), None, Action::NewWindow),
@ -158,11 +170,8 @@ pub fn menu_bar<'a>(
MenuItem::Button(fl!("quit"), None, Action::Quit), MenuItem::Button(fl!("quit"), None, Action::Quit),
], ],
), ),
), (
MenuTree::with_children( (fl!("edit")),
menu_root(fl!("edit")),
menu_items(
key_binds,
vec![ vec![
MenuItem::Button(fl!("undo"), None, Action::Undo), MenuItem::Button(fl!("undo"), None, Action::Undo),
MenuItem::Button(fl!("redo"), None, Action::Redo), MenuItem::Button(fl!("redo"), None, Action::Redo),
@ -181,11 +190,8 @@ pub fn menu_bar<'a>(
*/ */
], ],
), ),
), (
MenuTree::with_children( (fl!("view")),
menu_root(fl!("view")),
menu_items(
key_binds,
vec![ vec![
MenuItem::Folder( MenuItem::Folder(
fl!("indentation"), fl!("indentation"),
@ -242,10 +248,6 @@ pub fn menu_bar<'a>(
MenuItem::Button(fl!("menu-about"), None, Action::About), MenuItem::Button(fl!("menu-about"), None, Action::About),
], ],
), ),
), ],
]) )
.item_height(ItemHeight::Dynamic(40))
.item_width(ItemWidth::Uniform(320))
.spacing(4.0)
.into()
} }