cosmic-edit/src/menu.rs

270 lines
9.6 KiB
Rust
Raw Normal View History

2023-10-26 10:15:09 -06:00
// SPDX-License-Identifier: GPL-3.0-only
use cosmic::{
//TODO: export in cosmic::widget
2023-11-20 11:26:26 -07:00
iced::{
widget::{column, horizontal_rule},
2023-11-20 11:34:59 -07:00
Alignment, Background, Length,
2023-11-20 11:26:26 -07:00
},
2024-02-05 16:18:36 -05:00
iced_core::Border,
2023-10-26 10:15:09 -06:00
theme,
widget::{
2023-10-26 11:25:14 -06:00
self, horizontal_space,
2023-10-26 10:15:09 -06:00
menu::{ItemHeight, ItemWidth, MenuBar, 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
2024-02-01 19:50:44 -07:00
use crate::{fl, icon_cache_get, Action, Config, KeyBind, Message};
2023-11-20 11:26:26 -07:00
macro_rules! menu_button {
($($x:expr),+ $(,)?) => (
widget::button(
widget::Row::with_children(
vec![$(Element::from($x)),+]
)
.align_items(Alignment::Center)
)
.height(Length::Fixed(32.0))
.padding([4, 16])
.width(Length::Fill)
.style(theme::Button::MenuItem)
);
}
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;
}
}
menu_button!(
widget::text(menu_label),
2023-11-20 11:26:26 -07:00
horizontal_space(Length::Fill),
widget::text(key)
)
.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),
horizontal_rule(1),
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
.style(theme::Container::custom(|theme| {
let cosmic = theme.cosmic();
let component = &cosmic.background.component;
widget::container::Appearance {
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: 8.0.into(),
width: 1.0,
color: component.divider.into(),
},
..Default::default()
2023-11-20 11:34:59 -07: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,
key_binds: &HashMap<KeyBind, Action>,
projects: &Vec<(String, PathBuf)>,
) -> Element<'a, Message> {
2023-10-26 10:15:09 -06:00
//TODO: port to libcosmic
let menu_root = |label| {
2023-10-30 09:34:36 -06:00
widget::button(widget::text(label))
2023-10-26 10:15:09 -06:00
.padding([4, 12])
.style(theme::Button::MenuRoot)
};
2023-10-30 09:34:36 -06:00
let menu_folder =
|label| menu_button!(widget::text(label), horizontal_space(Length::Fill), ">");
2023-10-26 10:15:09 -06:00
2024-02-01 19:50:44 -07:00
let find_key = |action: &Action| -> String {
for (key_bind, key_action) in key_binds.iter() {
if action == key_action {
2024-02-15 12:21:43 -07:00
return key_bind.to_string();
}
}
2024-02-15 12:21:43 -07:00
if action == &Action::Todo {
return fl!("todo");
}
String::new()
2023-11-01 09:25:46 -06:00
};
2024-02-01 19:50:44 -07:00
let menu_item = |label, action| {
let key = find_key(&action);
2023-11-01 09:25:46 -06:00
MenuTree::new(
menu_button!(
widget::text(label),
horizontal_space(Length::Fill),
widget::text(key)
)
2024-02-01 19:50:44 -07:00
.on_press(action.message()),
2023-11-01 09:25:46 -06:00
)
};
//TODO: support key lookup?
2024-02-01 19:50:44 -07:00
let menu_checkbox = |label, value, action| {
2023-11-01 09:25:46 -06:00
let check: Element<_> = if value {
2023-11-28 13:07:27 -07:00
icon_cache_get("object-select-symbolic", 16).into()
2023-11-01 09:25:46 -06:00
} else {
widget::Space::with_width(Length::Fixed(16.0)).into()
};
2024-02-01 19:50:44 -07:00
let key = find_key(&action);
MenuTree::new(
menu_button!(
2023-11-01 09:25:46 -06:00
check,
widget::Space::with_width(Length::Fixed(8.0)),
widget::text(label),
horizontal_space(Length::Fill),
widget::text(key)
)
2024-02-01 19:50:44 -07:00
.on_press(action.message()),
)
};
2023-10-26 10:15:09 -06:00
2024-02-01 19:50:44 -07:00
let menu_key = |label, key, action: Action| {
2023-10-30 09:34:36 -06:00
MenuTree::new(
menu_button!(widget::text(label), horizontal_space(Length::Fill), key)
2024-02-01 19:50:44 -07:00
.on_press(action.message()),
2023-10-30 09:34:36 -06:00
)
2023-10-26 10:15:09 -06:00
};
2023-11-16 08:44:23 -07:00
let menu_tab_width = |tab_width: u16| {
menu_checkbox(
fl!("tab-width", tab_width = tab_width),
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
)
};
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(menu_item(name.clone(), Action::CloseProject(project_i)));
}
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")),
2023-10-26 10:15:09 -06:00
vec![
2024-02-01 19:50:44 -07:00
menu_item(fl!("new-file"), Action::NewFile),
menu_item(fl!("new-window"), Action::NewWindow),
2023-10-26 10:15:09 -06:00
MenuTree::new(horizontal_rule(1)),
2024-02-01 19:50:44 -07:00
menu_item(fl!("open-file"), Action::OpenFileDialog),
2023-10-26 10:15:09 -06:00
MenuTree::with_children(
menu_folder(fl!("open-recent-file")),
2024-02-01 19:50:44 -07:00
vec![menu_item(fl!("todo"), Action::Todo)],
2023-10-26 10:15:09 -06:00
),
2024-02-01 19:50:44 -07:00
menu_item(fl!("close-file"), Action::CloseFile),
MenuTree::new(horizontal_rule(1)),
2024-02-01 19:50:44 -07:00
menu_item(fl!("menu-open-project"), Action::OpenProjectDialog),
MenuTree::with_children(
menu_folder(fl!("open-recent-project")),
2024-02-01 19:50:44 -07:00
vec![menu_item(fl!("todo"), Action::Todo)],
),
2024-02-15 12:21:43 -07:00
MenuTree::with_children(menu_folder(fl!("close-project")), close_projects),
2023-10-26 10:15:09 -06:00
MenuTree::new(horizontal_rule(1)),
2024-02-01 19:50:44 -07:00
menu_item(fl!("save"), Action::Save),
menu_key(fl!("save-as"), "Ctrl + Shift + S", Action::Todo),
2023-10-26 10:15:09 -06:00
MenuTree::new(horizontal_rule(1)),
2024-02-01 19:50:44 -07:00
menu_item(fl!("revert-all-changes"), Action::Todo),
2023-10-26 10:15:09 -06:00
MenuTree::new(horizontal_rule(1)),
2023-11-01 14:39:17 -06:00
menu_item(
fl!("menu-document-statistics"),
2024-02-01 19:50:44 -07:00
Action::ToggleDocumentStatistics,
2023-12-01 13:19:56 -07:00
),
//TODO menu_item(fl!("document-type"), Action::Todo),
//TODO menu_item(fl!("encoding"), Action::Todo),
2024-02-01 19:50:44 -07:00
menu_item(fl!("menu-git-management"), Action::ToggleGitManagement),
//TODO menu_item(fl!("print"), Action::Todo),
2023-10-26 10:15:09 -06:00
MenuTree::new(horizontal_rule(1)),
2024-02-01 19:50:44 -07:00
menu_item(fl!("quit"), 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")),
2023-10-26 10:15:09 -06:00
vec![
2024-02-01 19:50:44 -07:00
menu_item(fl!("undo"), Action::Undo),
menu_item(fl!("redo"), Action::Redo),
2023-10-26 10:15:09 -06:00
MenuTree::new(horizontal_rule(1)),
2024-02-01 19:50:44 -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-10-26 10:15:09 -06:00
MenuTree::new(horizontal_rule(1)),
2024-02-01 19:50:44 -07:00
menu_item(fl!("find"), Action::Find),
menu_item(fl!("replace"), Action::FindAndReplace),
menu_item(fl!("find-in-project"), Action::ToggleProjectSearch),
2023-10-26 10:15:09 -06:00
MenuTree::new(horizontal_rule(1)),
2024-02-01 19:50:44 -07:00
menu_item(fl!("spell-check"), 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")),
2023-10-26 10:15:09 -06:00
vec![
MenuTree::with_children(
2023-10-30 09:34:36 -06:00
menu_folder(fl!("indentation")),
2023-10-26 10:15:09 -06:00
vec![
2023-11-16 09:00:48 -07:00
menu_checkbox(
fl!("automatic-indentation"),
config.auto_indent,
2024-02-01 19:50:44 -07:00
Action::ToggleAutoIndent,
2023-11-16 09:00:48 -07:00
),
2023-10-26 10:15:09 -06:00
MenuTree::new(horizontal_rule(1)),
2023-11-16 08:44:23 -07:00
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 MenuTree::new(horizontal_rule(1)),
//TODO menu_item(fl!("convert-indentation-to-spaces"), Action::Todo),
//TODO menu_item(fl!("convert-indentation-to-tabs"), Action::Todo),
2023-10-26 10:15:09 -06:00
],
),
MenuTree::new(horizontal_rule(1)),
2024-02-01 19:50:44 -07:00
menu_checkbox(fl!("word-wrap"), config.word_wrap, Action::ToggleWordWrap),
2023-11-30 14:24:58 -07:00
menu_checkbox(
fl!("show-line-numbers"),
config.line_numbers,
2024-02-01 19:50:44 -07:00
Action::ToggleLineNumbers,
2023-11-30 14:24:58 -07:00
),
2024-02-01 19:50:44 -07:00
menu_checkbox(fl!("highlight-current-line"), false, Action::Todo),
//TODO: menu_item(fl!("syntax-highlighting"), Action::Todo),
2023-10-26 10:15:09 -06:00
MenuTree::new(horizontal_rule(1)),
2024-02-01 19:50:44 -07:00
menu_item(fl!("menu-settings"), Action::ToggleSettingsPage),
//TODO MenuTree::new(horizontal_rule(1)),
//TODO menu_item(fl!("menu-keyboard-shortcuts"), Action::Todo),
2023-10-26 10:15:09 -06:00
MenuTree::new(horizontal_rule(1)),
2024-02-01 19:50:44 -07:00
menu_item(fl!("about-cosmic-text-editor"), Action::Todo),
2023-10-26 10:15:09 -06:00
],
),
])
.item_height(ItemHeight::Dynamic(40))
.item_width(ItemWidth::Uniform(240))
.spacing(4.0)
.into()
}