cosmic-edit/src/menu.rs

246 lines
9.5 KiB
Rust
Raw Normal View History

2023-10-26 10:15:09 -06:00
// SPDX-License-Identifier: GPL-3.0-only
use cosmic::widget::menu::key_bind::KeyBind;
2024-04-30 08:43:55 -06:00
use cosmic::widget::menu::{items as menu_items, root as menu_root, Item as MenuItem};
2023-10-26 10:15:09 -06:00
use cosmic::{
iced::{widget::column, Background, Length},
2024-02-05 16:18:36 -05:00
iced_core::Border,
2023-10-26 10:15:09 -06:00
widget::{
self, divider, horizontal_space,
2024-05-30 15:01:29 -04:00
menu::{menu_button, ItemHeight, ItemWidth, MenuBar, Tree as MenuTree},
2023-11-20 11:26:26 -07:00
segmented_button,
2023-10-26 10:15:09 -06:00
},
Element,
};
2024-02-15 12:21:43 -07:00
use std::{collections::HashMap, path::PathBuf};
2023-10-26 10:15:09 -06:00
use crate::{fl, Action, Config, ConfigState, Message};
2023-11-20 11:26:26 -07:00
2024-01-19 11:25:50 -07:00
pub fn context_menu<'a>(
key_binds: &HashMap<KeyBind, Action>,
entity: segmented_button::Entity,
) -> Element<'a, Message> {
let menu_item = |menu_label, menu_action| {
2023-11-20 11:26:26 -07:00
let mut key = String::new();
2024-01-19 11:25:50 -07:00
for (key_bind, key_action) in key_binds.iter() {
if key_action == &menu_action {
2023-11-20 11:26:26 -07:00
key = key_bind.to_string();
break;
}
}
2024-05-30 15:01:29 -04:00
menu_button(vec![
widget::text(menu_label).into(),
2024-10-22 11:55:58 -06:00
horizontal_space().into(),
2024-05-30 15:01:29 -04:00
widget::text(key).into(),
])
.on_press(Message::TabContextAction(entity, menu_action))
2023-11-20 11:26:26 -07:00
};
2023-11-20 11:34:59 -07:00
widget::container(column!(
2023-11-20 11:26:26 -07:00
menu_item(fl!("undo"), Action::Undo),
menu_item(fl!("redo"), Action::Redo),
divider::horizontal::light(),
2023-11-20 11:26:26 -07:00
menu_item(fl!("cut"), Action::Cut),
menu_item(fl!("copy"), Action::Copy),
menu_item(fl!("paste"), Action::Paste),
menu_item(fl!("select-all"), Action::SelectAll),
))
2023-11-20 11:34:59 -07:00
.padding(1)
//TODO: move style to libcosmic
2024-10-22 11:55:58 -06:00
.style(|theme| {
2023-11-20 11:34:59 -07:00
let cosmic = theme.cosmic();
let component = &cosmic.background.component;
2024-10-22 11:55:58 -06:00
widget::container::Style {
2023-11-20 11:34:59 -07:00
icon_color: Some(component.on.into()),
text_color: Some(component.on.into()),
background: Some(Background::Color(component.base.into())),
2024-02-05 16:18:36 -05:00
border: Border {
radius: cosmic.radius_s().map(|x| x + 1.0).into(),
2024-02-05 16:18:36 -05:00
width: 1.0,
color: component.divider.into(),
},
..Default::default()
2023-11-20 11:34:59 -07:00
}
2024-10-22 11:55:58 -06:00
})
2023-11-20 11:26:26 -07:00
.width(Length::Fixed(240.0))
.into()
}
2023-10-26 10:15:09 -06:00
2024-02-15 12:21:43 -07:00
pub fn menu_bar<'a>(
config: &Config,
2024-02-23 09:55:31 -07:00
config_state: &ConfigState,
2024-02-15 12:21:43 -07:00
key_binds: &HashMap<KeyBind, Action>,
projects: &Vec<(String, PathBuf)>,
) -> Element<'a, Message> {
2023-10-26 10:15:09 -06:00
//TODO: port to libcosmic
2023-11-16 08:44:23 -07:00
let menu_tab_width = |tab_width: u16| {
MenuItem::CheckBox(
2023-11-16 08:44:23 -07:00
fl!("tab-width", tab_width = tab_width),
2024-12-02 23:36:27 +01:00
None,
2023-11-16 08:44:23 -07:00
config.tab_width == tab_width,
2024-02-01 19:50:44 -07:00
Action::TabWidth(tab_width),
2023-11-16 08:44:23 -07:00
)
};
let home_dir_opt = dirs::home_dir();
let format_path = |path: &PathBuf| -> String {
if let Some(home_dir) = &home_dir_opt {
if let Ok(part) = path.strip_prefix(home_dir) {
return format!("~/{}", part.display());
}
}
path.display().to_string()
};
2024-02-23 09:55:31 -07:00
let mut recent_files = Vec::with_capacity(config_state.recent_files.len());
for (i, path) in config_state.recent_files.iter().enumerate() {
recent_files.push(MenuItem::Button(
format_path(path),
2024-12-02 23:36:27 +01:00
None,
Action::OpenRecentFile(i),
));
}
2024-02-23 09:55:31 -07:00
let mut recent_projects = Vec::with_capacity(config_state.recent_projects.len());
for (i, path) in config_state.recent_projects.iter().enumerate() {
recent_projects.push(MenuItem::Button(
format_path(path),
2024-12-02 23:36:27 +01:00
None,
Action::OpenRecentProject(i),
));
}
2024-02-15 12:21:43 -07:00
let mut close_projects = Vec::with_capacity(projects.len());
for (project_i, (name, _path)) in projects.iter().enumerate() {
close_projects.push(MenuItem::Button(
name.clone(),
2024-12-02 23:36:27 +01:00
None,
Action::CloseProject(project_i),
));
2024-02-15 12:21:43 -07:00
}
2023-10-26 10:15:09 -06:00
MenuBar::new(vec![
MenuTree::with_children(
2023-10-30 09:34:36 -06:00
menu_root(fl!("file")),
menu_items(
key_binds,
vec![
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("new-file"), None, Action::NewFile),
MenuItem::Button(fl!("new-window"), None, Action::NewWindow),
MenuItem::Divider,
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("open-file"), None, Action::OpenFileDialog),
MenuItem::Folder(fl!("open-recent-file"), recent_files),
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("close-file"), None, Action::CloseFile),
MenuItem::Divider,
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("menu-open-project"), None, Action::OpenProjectDialog),
MenuItem::Folder(fl!("open-recent-project"), recent_projects),
MenuItem::Folder(fl!("close-project"), close_projects),
MenuItem::Divider,
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("save"), None, Action::Save),
MenuItem::Button(fl!("save-as"), None, Action::SaveAsDialog),
MenuItem::Divider,
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("revert-all-changes"), None, Action::Todo),
MenuItem::Divider,
MenuItem::Button(
fl!("menu-document-statistics"),
2024-12-02 23:36:27 +01:00
None,
Action::ToggleDocumentStatistics,
),
//TODO MenuItem::Button(fl!("document-type"), Action::Todo),
//TODO MenuItem::Button(fl!("encoding"), Action::Todo),
2024-12-02 23:36:27 +01:00
MenuItem::Button(
fl!("menu-git-management"),
None,
Action::ToggleGitManagement,
),
//TODO MenuItem::Button(fl!("print"), Action::Todo),
MenuItem::Divider,
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("quit"), None, Action::Quit),
],
),
2023-10-26 10:15:09 -06:00
),
MenuTree::with_children(
2023-10-30 09:34:36 -06:00
menu_root(fl!("edit")),
menu_items(
key_binds,
vec![
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("undo"), None, Action::Undo),
MenuItem::Button(fl!("redo"), None, Action::Redo),
MenuItem::Divider,
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("cut"), None, Action::Cut),
MenuItem::Button(fl!("copy"), None, Action::Copy),
MenuItem::Button(fl!("paste"), None, Action::Paste),
MenuItem::Button(fl!("select-all"), None, Action::SelectAll),
MenuItem::Divider,
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("find"), None, Action::Find),
MenuItem::Button(fl!("replace"), None, Action::FindAndReplace),
MenuItem::Button(fl!("find-in-project"), None, Action::ToggleProjectSearch),
MenuItem::Divider,
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("spell-check"), None, Action::Todo),
],
),
2023-10-26 10:15:09 -06:00
),
MenuTree::with_children(
2023-10-30 09:34:36 -06:00
menu_root(fl!("view")),
menu_items(
key_binds,
vec![
MenuItem::Folder(
fl!("indentation"),
vec![
MenuItem::CheckBox(
fl!("automatic-indentation"),
2024-12-02 23:36:27 +01:00
None,
config.auto_indent,
Action::ToggleAutoIndent,
),
MenuItem::Divider,
menu_tab_width(1),
menu_tab_width(2),
menu_tab_width(3),
menu_tab_width(4),
menu_tab_width(5),
menu_tab_width(6),
menu_tab_width(7),
menu_tab_width(8),
//TODO MenuItem::Divider,
//TODO MenuItem::Button(fl!("convert-indentation-to-spaces"), Action::Todo),
//TODO MenuItem::Button(fl!("convert-indentation-to-tabs"), Action::Todo),
],
),
MenuItem::Divider,
2024-12-02 23:36:27 +01:00
MenuItem::CheckBox(
fl!("word-wrap"),
None,
config.word_wrap,
Action::ToggleWordWrap,
),
MenuItem::CheckBox(
fl!("show-line-numbers"),
2024-12-02 23:36:27 +01:00
None,
config.line_numbers,
Action::ToggleLineNumbers,
),
MenuItem::CheckBox(
fl!("highlight-current-line"),
2024-12-02 23:36:27 +01:00
None,
config.highlight_current_line,
Action::ToggleHighlightCurrentLine,
),
//TODO: MenuItem::CheckBox(fl!("syntax-highlighting"), Action::Todo),
MenuItem::Divider,
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("menu-settings"), None, Action::ToggleSettingsPage),
//TODO MenuItem::Divider,
//TODO MenuItem::Button(fl!("menu-keyboard-shortcuts"), Action::Todo),
MenuItem::Divider,
2024-12-02 23:36:27 +01:00
MenuItem::Button(fl!("menu-about"), None, Action::About),
],
),
2023-10-26 10:15:09 -06:00
),
])
.item_height(ItemHeight::Dynamic(40))
.item_width(ItemWidth::Uniform(320))
2023-10-26 10:15:09 -06:00
.spacing(4.0)
.into()
}