parent
0169cccfa2
commit
841816e8d1
8 changed files with 557 additions and 59 deletions
44
src/menu.rs
44
src/menu.rs
|
|
@ -5,7 +5,7 @@ use cosmic::{
|
|||
widget::menu::{self, key_bind::KeyBind, ItemHeight, ItemWidth, MenuBar},
|
||||
Element,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::{collections::HashMap, path::PathBuf};
|
||||
|
||||
use crate::{fl, Action, Config, ConfigState, Message};
|
||||
|
||||
|
|
@ -13,18 +13,20 @@ pub fn menu_bar<'a>(
|
|||
config: &Config,
|
||||
config_state: &ConfigState,
|
||||
key_binds: &HashMap<KeyBind, Action>,
|
||||
projects: &Vec<(String, PathBuf)>,
|
||||
) -> Element<'a, Message> {
|
||||
let home_dir_opt = dirs::home_dir();
|
||||
let format_path = |url: &url::Url| -> String {
|
||||
match url.to_file_path() {
|
||||
Ok(path) => {
|
||||
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()
|
||||
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()
|
||||
};
|
||||
let format_url = |url: &url::Url| -> String {
|
||||
match url.to_file_path() {
|
||||
Ok(path) => format_path(&path),
|
||||
Err(()) => url.to_string(),
|
||||
}
|
||||
};
|
||||
|
|
@ -32,19 +34,27 @@ pub fn menu_bar<'a>(
|
|||
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(menu::Item::Button(
|
||||
format_path(path),
|
||||
format_url(path),
|
||||
Action::FileOpenRecent(i),
|
||||
));
|
||||
}
|
||||
|
||||
let mut recent_folders = Vec::with_capacity(config_state.recent_folders.len());
|
||||
for (i, path) in config_state.recent_folders.iter().enumerate() {
|
||||
recent_folders.push(menu::Item::Button(
|
||||
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(menu::Item::Button(
|
||||
format_path(path),
|
||||
Action::FolderOpenRecent(i),
|
||||
));
|
||||
}
|
||||
|
||||
let mut close_projects = Vec::with_capacity(projects.len());
|
||||
for (folder_i, (name, _path)) in projects.iter().enumerate() {
|
||||
close_projects.push(menu::Item::Button(
|
||||
name.clone(),
|
||||
Action::FolderClose(folder_i),
|
||||
));
|
||||
}
|
||||
|
||||
MenuBar::new(vec![menu::Tree::with_children(
|
||||
menu::root(fl!("file")),
|
||||
menu::items(
|
||||
|
|
@ -54,12 +64,10 @@ pub fn menu_bar<'a>(
|
|||
menu::Item::Folder(fl!("open-recent-media"), recent_files),
|
||||
menu::Item::Button(fl!("close-file"), Action::FileClose),
|
||||
menu::Item::Divider,
|
||||
/*TODO: folders
|
||||
menu::Item::Button(fl!("open-media-folder"), Action::FolderOpen),
|
||||
menu::Item::Folder(fl!("open-recent-media-folder"), recent_folders),
|
||||
menu::Item::Folder(fl!("close-media-folder"), close_folders),
|
||||
menu::Item::Folder(fl!("open-recent-media-folder"), recent_projects),
|
||||
menu::Item::Folder(fl!("close-media-folder"), close_projects),
|
||||
menu::Item::Divider,
|
||||
*/
|
||||
menu::Item::Button(fl!("quit"), Action::WindowClose),
|
||||
],
|
||||
),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue