cosmic-files/src/menu.rs

262 lines
9.2 KiB
Rust
Raw Normal View History

2024-01-05 11:18:38 -07:00
// SPDX-License-Identifier: GPL-3.0-only
use cosmic::{
2024-01-10 10:15:16 -07:00
//TODO: export iced::widget::horizontal_rule in cosmic::widget
2024-02-09 07:09:51 -07:00
iced::{widget::horizontal_rule, Alignment, Background, Border, Length},
2024-01-05 11:18:38 -07:00
theme,
2024-01-10 11:39:04 -07:00
widget::{
self,
menu::{ItemHeight, ItemWidth, MenuBar, MenuTree},
},
2024-01-05 11:18:38 -07:00
Element,
};
2024-01-29 12:21:54 -07:00
use std::collections::HashMap;
2024-01-05 11:18:38 -07:00
2024-02-01 15:14:14 -07:00
use crate::{
2024-02-13 12:29:50 -07:00
app::{Action, Message},
2024-02-01 15:14:14 -07:00
fl,
key_bind::KeyBind,
2024-03-15 00:34:16 -04:00
tab::{self, HeadingOptions, Location, Tab},
2024-02-01 15:14:14 -07:00
};
2024-01-05 11:18:38 -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)
);
}
pub fn context_menu<'a>(
tab: &Tab,
key_binds: &HashMap<KeyBind, Action>,
) -> Element<'a, tab::Message> {
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);
menu_button!(
widget::text(label),
widget::horizontal_space(Length::Fill),
widget::text(key)
)
.on_press(tab::Message::ContextAction(action))
2024-01-05 11:18:38 -07:00
};
2024-03-04 10:28:16 -07:00
let mut selected_dir = 0;
let mut selected = 0;
tab.items_opt().map(|items| {
for item in items.iter() {
if item.selected {
selected += 1;
if item.metadata.is_dir() {
selected_dir += 1;
}
}
}
});
2024-01-10 10:15:16 -07:00
let mut children: Vec<Element<_>> = Vec::new();
match tab.location {
2024-01-10 09:47:47 -07:00
Location::Path(_) => {
2024-01-10 10:15:16 -07:00
if selected > 0 {
children.push(menu_item(fl!("open"), Action::Open).into());
if selected == 1 {
children.push(menu_item(fl!("open-with"), Action::OpenWith).into());
2024-03-04 10:28:16 -07:00
if selected_dir == 1 {
children
.push(menu_item(fl!("open-in-terminal"), Action::OpenTerminal).into());
}
}
2024-01-10 10:15:16 -07:00
children.push(horizontal_rule(1).into());
children.push(menu_item(fl!("rename"), Action::Rename).into());
children.push(menu_item(fl!("cut"), Action::Cut).into());
children.push(menu_item(fl!("copy"), Action::Copy).into());
//TODO: Print?
children.push(horizontal_rule(1).into());
//TODO: change to Show details
children.push(menu_item(fl!("properties"), Action::Properties).into());
//TODO: Add to sidebar
children.push(horizontal_rule(1).into());
children.push(menu_item(fl!("move-to-trash"), Action::MoveToTrash).into());
} else {
//TODO: need better designs for menu with no selection
//TODO: have things like properties but they apply to the folder?
children.push(menu_item(fl!("new-file"), Action::NewFile).into());
children.push(menu_item(fl!("new-folder"), Action::NewFolder).into());
2024-03-04 10:28:16 -07:00
children.push(menu_item(fl!("open-in-terminal"), Action::OpenTerminal).into());
children.push(horizontal_rule(1).into());
children.push(menu_item(fl!("select-all"), Action::SelectAll).into());
children.push(menu_item(fl!("paste"), Action::Paste).into());
2024-03-15 00:34:16 -04:00
children.push(horizontal_rule(1).into());
children.push(
menu_item(
fl!("sort-by-name"),
Action::ToggleSort(HeadingOptions::Name),
)
.into(),
);
children.push(
menu_item(
fl!("sort-by-modified"),
Action::ToggleSort(HeadingOptions::Modified),
)
.into(),
);
children.push(
menu_item(
fl!("sort-by-size"),
Action::ToggleSort(HeadingOptions::Size),
)
.into(),
);
2024-01-10 10:15:16 -07:00
}
2024-01-10 09:47:47 -07:00
}
Location::Trash => {
children.push(menu_item(fl!("select-all"), Action::SelectAll).into());
2024-01-10 10:15:16 -07:00
if selected > 0 {
children.push(horizontal_rule(1).into());
children.push(menu_item(fl!("properties"), Action::Properties).into());
2024-01-10 10:15:16 -07:00
children.push(horizontal_rule(1).into());
children
.push(menu_item(fl!("restore-from-trash"), Action::RestoreFromTrash).into());
2024-01-10 10:15:16 -07:00
}
2024-03-15 00:34:16 -04:00
children.push(horizontal_rule(1).into());
children.push(
menu_item(
fl!("sort-by-name"),
Action::ToggleSort(HeadingOptions::Name),
)
.into(),
);
children.push(
menu_item(
fl!("sort-by-modified"),
Action::ToggleSort(HeadingOptions::Modified),
)
.into(),
);
children.push(
menu_item(
fl!("sort-by-size"),
Action::ToggleSort(HeadingOptions::Size),
)
.into(),
);
2024-01-05 11:18:38 -07:00
}
2024-01-10 10:15:16 -07:00
}
2024-01-10 09:47:47 -07:00
2024-01-10 10:15:16 -07:00
widget::container(widget::column::with_children(children))
2024-01-10 09:47:47 -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-09 07:09:51 -07:00
border: Border {
radius: 8.0.into(),
width: 1.0,
color: component.divider.into(),
},
..Default::default()
2024-01-10 09:47:47 -07:00
}
}))
.width(Length::Fixed(240.0))
.into()
2024-01-05 11:18:38 -07:00
}
2024-01-10 11:39:04 -07:00
2024-01-29 12:21:54 -07:00
pub fn menu_bar<'a>(key_binds: &HashMap<KeyBind, Action>) -> Element<'a, Message> {
2024-01-10 11:39:04 -07:00
//TODO: port to libcosmic
let menu_root = |label| {
widget::button(widget::text(label))
.padding([4, 12])
.style(theme::Button::MenuRoot)
};
2024-01-29 12:21:54 -07:00
let find_key = |action: &Action| -> String {
for (key_bind, key_action) in key_binds.iter() {
if action == key_action {
return key_bind.to_string();
}
}
2024-01-10 11:39:04 -07:00
String::new()
};
2024-01-29 12:21:54 -07:00
let menu_item = |label, action| {
let key = find_key(&action);
2024-01-10 11:39:04 -07:00
MenuTree::new(
menu_button!(
widget::text(label),
widget::horizontal_space(Length::Fill),
widget::text(key)
)
2024-01-29 12:21:54 -07:00
.on_press(action.message(None)),
2024-01-10 11:39:04 -07:00
)
};
MenuBar::new(vec![
MenuTree::with_children(
menu_root(fl!("file")),
vec![
2024-01-29 12:21:54 -07:00
menu_item(fl!("new-tab"), Action::TabNew),
menu_item(fl!("new-window"), Action::WindowNew),
menu_item(fl!("new-file"), Action::NewFile),
menu_item(fl!("new-folder"), Action::NewFolder),
menu_item(fl!("open"), Action::Open),
2024-01-10 11:39:04 -07:00
MenuTree::new(horizontal_rule(1)),
2024-02-28 15:07:50 -07:00
menu_item(fl!("rename"), Action::Rename),
//TOOD: add to sidebar, then divider
MenuTree::new(horizontal_rule(1)),
menu_item(fl!("move-to-trash"), Action::MoveToTrash),
2024-01-10 11:39:04 -07:00
MenuTree::new(horizontal_rule(1)),
2024-02-28 15:07:50 -07:00
menu_item(fl!("close-tab"), Action::TabClose),
2024-01-29 12:21:54 -07:00
menu_item(fl!("quit"), Action::WindowClose),
2024-01-10 11:39:04 -07:00
],
),
MenuTree::with_children(
menu_root(fl!("edit")),
vec![
2024-01-29 12:21:54 -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),
MenuTree::new(horizontal_rule(1)),
//TODO: edit history
menu_item(fl!("operations"), Action::Operations),
2024-01-10 11:39:04 -07:00
],
),
MenuTree::with_children(
menu_root(fl!("view")),
vec![
menu_item(fl!("grid-view"), Action::TabViewGrid),
menu_item(fl!("list-view"), Action::TabViewList),
2024-01-10 11:39:04 -07:00
MenuTree::new(horizontal_rule(1)),
menu_item(fl!("menu-settings"), Action::Settings),
MenuTree::new(horizontal_rule(1)),
menu_item(fl!("menu-about"), Action::About),
2024-01-10 11:39:04 -07:00
],
),
])
.item_height(ItemHeight::Dynamic(40))
.item_width(ItemWidth::Uniform(240))
.spacing(4.0)
.into()
}