Merge pull request #84 from edfloreshz/master
refactor(menu): update menu declaration.
This commit is contained in:
commit
39888d5604
4 changed files with 57 additions and 111 deletions
12
src/app.rs
12
src/app.rs
|
|
@ -1,6 +1,8 @@
|
||||||
// Copyright 2023 System76 <info@system76.com>
|
// Copyright 2023 System76 <info@system76.com>
|
||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
|
use cosmic::widget::menu::action::MenuAction;
|
||||||
|
use cosmic::widget::menu::key_bind::KeyBind;
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
app::{message, Command, Core},
|
app::{message, Command, Core},
|
||||||
cosmic_config, cosmic_theme, executor,
|
cosmic_config, cosmic_theme, executor,
|
||||||
|
|
@ -32,7 +34,7 @@ use std::{
|
||||||
use crate::{
|
use crate::{
|
||||||
config::{AppTheme, Config, IconSizes, TabConfig, CONFIG_VERSION},
|
config::{AppTheme, Config, IconSizes, TabConfig, CONFIG_VERSION},
|
||||||
fl, home_dir,
|
fl, home_dir,
|
||||||
key_bind::{key_binds, KeyBind},
|
key_bind::key_binds,
|
||||||
menu, mime_app,
|
menu, mime_app,
|
||||||
operation::Operation,
|
operation::Operation,
|
||||||
spawn_detached::spawn_detached,
|
spawn_detached::spawn_detached,
|
||||||
|
|
@ -83,8 +85,10 @@ pub enum Action {
|
||||||
WindowNew,
|
WindowNew,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Action {
|
impl MenuAction for Action {
|
||||||
pub fn message(self, entity_opt: Option<Entity>) -> Message {
|
type Message = Message;
|
||||||
|
|
||||||
|
fn message(&self, entity_opt: Option<Entity>) -> Message {
|
||||||
match self {
|
match self {
|
||||||
Action::About => Message::ToggleContextPage(ContextPage::About),
|
Action::About => Message::ToggleContextPage(ContextPage::About),
|
||||||
Action::Copy => Message::Copy(entity_opt),
|
Action::Copy => Message::Copy(entity_opt),
|
||||||
|
|
@ -121,7 +125,7 @@ impl Action {
|
||||||
Message::TabMessage(entity_opt, tab::Message::View(tab::View::List))
|
Message::TabMessage(entity_opt, tab::Message::View(tab::View::List))
|
||||||
}
|
}
|
||||||
Action::ToggleShowHidden => Message::TabMessage(None, tab::Message::ToggleShowHidden),
|
Action::ToggleShowHidden => Message::TabMessage(None, tab::Message::ToggleShowHidden),
|
||||||
Action::ToggleSort(sort) => Message::TabMessage(None, tab::Message::ToggleSort(sort)),
|
Action::ToggleSort(sort) => Message::TabMessage(None, tab::Message::ToggleSort(*sort)),
|
||||||
Action::WindowClose => Message::WindowClose,
|
Action::WindowClose => Message::WindowClose,
|
||||||
Action::WindowNew => Message::WindowNew,
|
Action::WindowNew => Message::WindowNew,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,48 +1,9 @@
|
||||||
use cosmic::{
|
use cosmic::widget::menu::key_bind::KeyBind;
|
||||||
iced::keyboard::{Key, Modifiers},
|
use cosmic::{iced::keyboard::Key, iced_core::keyboard::key::Named};
|
||||||
iced_core::keyboard::key::Named,
|
use std::collections::HashMap;
|
||||||
};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::{collections::HashMap, fmt};
|
|
||||||
|
|
||||||
use crate::app::Action;
|
use crate::app::Action;
|
||||||
|
use cosmic::widget::menu::key_bind::Modifier;
|
||||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
|
||||||
pub enum Modifier {
|
|
||||||
Super,
|
|
||||||
Ctrl,
|
|
||||||
Alt,
|
|
||||||
Shift,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: load from config
|
//TODO: load from config
|
||||||
pub fn key_binds() -> HashMap<KeyBind, Action> {
|
pub fn key_binds() -> HashMap<KeyBind, Action> {
|
||||||
|
|
|
||||||
104
src/menu.rs
104
src/menu.rs
|
|
@ -1,5 +1,7 @@
|
||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
|
use cosmic::widget::menu::key_bind::KeyBind;
|
||||||
|
use cosmic::widget::menu::menu_tree::{menu_items, menu_root, MenuItem};
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
//TODO: export iced::widget::horizontal_rule in cosmic::widget
|
//TODO: export iced::widget::horizontal_rule in cosmic::widget
|
||||||
iced::{widget::horizontal_rule, Alignment, Background, Border, Length},
|
iced::{widget::horizontal_rule, Alignment, Background, Border, Length},
|
||||||
|
|
@ -16,7 +18,6 @@ use crate::{
|
||||||
app::{Action, Message},
|
app::{Action, Message},
|
||||||
config::TabConfig,
|
config::TabConfig,
|
||||||
fl,
|
fl,
|
||||||
key_bind::KeyBind,
|
|
||||||
tab::{self, HeadingOptions, Location, Tab},
|
tab::{self, HeadingOptions, Location, Tab},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -171,75 +172,56 @@ pub fn context_menu<'a>(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn menu_bar<'a>(key_binds: &HashMap<KeyBind, Action>) -> Element<'a, Message> {
|
pub fn menu_bar<'a>(key_binds: &HashMap<KeyBind, Action>) -> Element<'a, Message> {
|
||||||
//TODO: port to libcosmic
|
|
||||||
let menu_root = |label| {
|
|
||||||
widget::button(widget::text(label))
|
|
||||||
.padding([4, 12])
|
|
||||||
.style(theme::Button::MenuRoot)
|
|
||||||
};
|
|
||||||
|
|
||||||
let find_key = |action: &Action| -> String {
|
|
||||||
for (key_bind, key_action) in key_binds.iter() {
|
|
||||||
if action == key_action {
|
|
||||||
return key_bind.to_string();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String::new()
|
|
||||||
};
|
|
||||||
|
|
||||||
let menu_item = |label, action| {
|
|
||||||
let key = find_key(&action);
|
|
||||||
MenuTree::new(
|
|
||||||
menu_button!(
|
|
||||||
widget::text(label),
|
|
||||||
widget::horizontal_space(Length::Fill),
|
|
||||||
widget::text(key)
|
|
||||||
)
|
|
||||||
.on_press(action.message(None)),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
MenuBar::new(vec![
|
MenuBar::new(vec![
|
||||||
MenuTree::with_children(
|
MenuTree::with_children(
|
||||||
menu_root(fl!("file")),
|
menu_root(fl!("file")),
|
||||||
vec![
|
menu_items(
|
||||||
menu_item(fl!("new-tab"), Action::TabNew),
|
key_binds,
|
||||||
menu_item(fl!("new-window"), Action::WindowNew),
|
vec![
|
||||||
menu_item(fl!("new-file"), Action::NewFile),
|
MenuItem::Button(fl!("new-tab"), Action::TabNew),
|
||||||
menu_item(fl!("new-folder"), Action::NewFolder),
|
MenuItem::Button(fl!("new-window"), Action::WindowNew),
|
||||||
menu_item(fl!("open"), Action::Open),
|
MenuItem::Button(fl!("new-file"), Action::NewFile),
|
||||||
MenuTree::new(horizontal_rule(1)),
|
MenuItem::Button(fl!("new-folder"), Action::NewFolder),
|
||||||
menu_item(fl!("rename"), Action::Rename),
|
MenuItem::Button(fl!("open"), Action::Open),
|
||||||
//TOOD: add to sidebar, then divider
|
MenuItem::Divider,
|
||||||
MenuTree::new(horizontal_rule(1)),
|
MenuItem::Button(fl!("rename"), Action::Rename),
|
||||||
menu_item(fl!("move-to-trash"), Action::MoveToTrash),
|
//TOOD: add to sidebar, then divider
|
||||||
MenuTree::new(horizontal_rule(1)),
|
MenuItem::Divider,
|
||||||
menu_item(fl!("close-tab"), Action::TabClose),
|
MenuItem::Button(fl!("move-to-trash"), Action::MoveToTrash),
|
||||||
menu_item(fl!("quit"), Action::WindowClose),
|
MenuItem::Divider,
|
||||||
],
|
MenuItem::Button(fl!("close-tab"), Action::TabClose),
|
||||||
|
MenuItem::Button(fl!("quit"), Action::WindowClose),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
MenuTree::with_children(
|
MenuTree::with_children(
|
||||||
menu_root(fl!("edit")),
|
menu_root(fl!("edit")),
|
||||||
vec![
|
menu_items(
|
||||||
menu_item(fl!("cut"), Action::Cut),
|
key_binds,
|
||||||
menu_item(fl!("copy"), Action::Copy),
|
vec![
|
||||||
menu_item(fl!("paste"), Action::Paste),
|
MenuItem::Button(fl!("cut"), Action::Cut),
|
||||||
menu_item(fl!("select-all"), Action::SelectAll),
|
MenuItem::Button(fl!("copy"), Action::Copy),
|
||||||
MenuTree::new(horizontal_rule(1)),
|
MenuItem::Button(fl!("paste"), Action::Paste),
|
||||||
//TODO: edit history
|
MenuItem::Button(fl!("select-all"), Action::SelectAll),
|
||||||
menu_item(fl!("operations"), Action::Operations),
|
MenuItem::Divider,
|
||||||
],
|
//TODO: edit history
|
||||||
|
MenuItem::Button(fl!("operations"), Action::Operations),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
MenuTree::with_children(
|
MenuTree::with_children(
|
||||||
menu_root(fl!("view")),
|
menu_root(fl!("view")),
|
||||||
vec![
|
menu_items(
|
||||||
menu_item(fl!("grid-view"), Action::TabViewGrid),
|
key_binds,
|
||||||
menu_item(fl!("list-view"), Action::TabViewList),
|
vec![
|
||||||
MenuTree::new(horizontal_rule(1)),
|
MenuItem::Button(fl!("grid-view"), Action::TabViewGrid),
|
||||||
menu_item(fl!("menu-settings"), Action::Settings),
|
MenuItem::Button(fl!("list-view"), Action::TabViewList),
|
||||||
MenuTree::new(horizontal_rule(1)),
|
MenuItem::Divider,
|
||||||
menu_item(fl!("menu-about"), Action::About),
|
MenuItem::Button(fl!("menu-settings"), Action::Settings),
|
||||||
],
|
MenuItem::Divider,
|
||||||
|
MenuItem::Button(fl!("menu-about"), Action::About),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
.item_height(ItemHeight::Dynamic(40))
|
.item_height(ItemHeight::Dynamic(40))
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use cosmic::widget::menu::key_bind::KeyBind;
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
cosmic_theme,
|
cosmic_theme,
|
||||||
iced::{
|
iced::{
|
||||||
|
|
@ -37,9 +38,7 @@ use crate::{
|
||||||
app::{self, Action},
|
app::{self, Action},
|
||||||
config::{IconSizes, TabConfig},
|
config::{IconSizes, TabConfig},
|
||||||
dialog::DialogKind,
|
dialog::DialogKind,
|
||||||
fl,
|
fl, menu,
|
||||||
key_bind::KeyBind,
|
|
||||||
menu,
|
|
||||||
mime_app::{mime_apps, MimeApp},
|
mime_app::{mime_apps, MimeApp},
|
||||||
mime_icon::{mime_for_path, mime_icon},
|
mime_icon::{mime_for_path, mime_icon},
|
||||||
mouse_area,
|
mouse_area,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue