diff --git a/i18n/da/cosmic_files.ftl b/i18n/da/cosmic_files.ftl index 2d0e35d..5666fdb 100644 --- a/i18n/da/cosmic_files.ftl +++ b/i18n/da/cosmic_files.ftl @@ -5,7 +5,7 @@ no-results = Fandt ingen resultater filesystem = Filsystem home = Hjem networks = Netværk -notification-in-progress = Fil-operationer er igang. +notification-in-progress = Fil-operationer er igang trash = Papirkurv recents = Seneste undo = Fortryd @@ -69,7 +69,7 @@ name-no-slashes = Navn må ikke indeholde skråstreger ## Open/Save Dialog -cancel = Annuller +cancel = Afbryd create = Opret open = Åbn open-file = Åbn fil @@ -95,7 +95,7 @@ rename-folder = Omdøb mappe ## Replace Dialog replace = Erstat -replace-title = "{ $filename }" eksisterer allerede i denne placering. +replace-title = "{ $filename }" eksisterer allerede i denne placering replace-warning = Ønsker du at erstatte den med den, du er ved at gemme? Erstatning af den vil overskrive dens indhold. replace-warning-operation = Ønsker du at erstatte den? Erstatning af den vil overskrive dens indhold. original-file = Original fil @@ -253,7 +253,7 @@ light = Lyst # Context menu add-to-sidebar = Tilføj til sidebjælke compress = Komprimer… -extract-here = Extract +extract-here = Uddrag new-file = Ny fil... new-folder = Ny mappe... open-in-terminal = Åbn i terminal @@ -332,7 +332,7 @@ deleted = Slettede { $items } { $items -> [one] element *[other] elementer - } fra papirkurven + } fra { trash } removing-from-recents = Fjerner { $items } { $items -> [one] element @@ -360,7 +360,7 @@ remove = Fjern context-action = Konteksthandling context-action-confirm-title = Kør “{ $name }”? context-action-confirm-warning = - This will run on { $items } { $items -> + Dette vil køre på { $items } { $items -> [one] item *[other] items }. diff --git a/i18n/en/cosmic_files.ftl b/i18n/en/cosmic_files.ftl index ea01f7e..2c57b53 100644 --- a/i18n/en/cosmic_files.ftl +++ b/i18n/en/cosmic_files.ftl @@ -118,6 +118,9 @@ rename-file = Rename file rename-folder = Rename folder rename-confirm = Rename +## Change Sidebar Label Dialog +sidebar-label = Sidebar label + ## Replace Dialog replace = Replace replace-title = "{$filename}" already exists in this location @@ -341,6 +344,7 @@ move-to = Move to... move-to-trash = Move to trash restore-from-trash = Restore from trash remove-from-sidebar = Remove from sidebar +change-sidebar-label = Change sidebar label sort-by-name = Sort by name sort-by-modified = Sort by modified sort-by-size = Sort by size diff --git a/i18n/pt-BR/cosmic_files.ftl b/i18n/pt-BR/cosmic_files.ftl index 75b071d..b47214a 100644 --- a/i18n/pt-BR/cosmic_files.ftl +++ b/i18n/pt-BR/cosmic_files.ftl @@ -109,6 +109,10 @@ permanently-delete-warning = Deseja realmente excluir permanentemente { $target rename-file = Renomear arquivo rename-folder = Renomear pasta +## Change Sidebar Label Dialog + +sidebar-label = Rótulo da barra lateral + ## Replace Dialog replace = Substituir @@ -350,7 +354,7 @@ light = Estilo claro ### Type to Search -type-to-search = Digite para pesquisar +type-to-search = Digitar para pesquisar type-to-search-recursive = Pesquisa na pasta atual e em todas as subpastas type-to-search-enter-path = Insere o caminho do diretório ou arquivo # Context menu @@ -365,6 +369,7 @@ open-in-terminal = Abrir no terminal move-to-trash = Mover para a lixeira restore-from-trash = Restaurar da lixeira remove-from-sidebar = Remover da barra lateral +change-sidebar-label = Alterar rótulo na barra lateral sort-by-name = Ordenar por nome sort-by-modified = Ordenar por data de modificação sort-by-size = Ordenar por tamanho diff --git a/src/app.rs b/src/app.rs index afc22f4..6a7bf63 100644 --- a/src/app.rs +++ b/src/app.rs @@ -321,6 +321,7 @@ pub enum NavMenuAction { Preview(segmented_button::Entity), RunContextAction(segmented_button::Entity, usize), RemoveFromSidebar(segmented_button::Entity), + ChangeSidebarLabel(segmented_button::Entity), } impl MenuAction for NavMenuAction { @@ -575,6 +576,10 @@ pub enum DialogPage { DeleteTrash { items: Vec, }, + ChangeSidebarLabel { + entity: Entity, + label: String, + }, RenameItem { from: PathBuf, parent: PathBuf, @@ -1769,15 +1774,9 @@ impl App { for (favorite_i, favorite) in self.config.favorites.iter().enumerate() { if let Some(path) = favorite.path_opt() { - let name = if matches!(favorite, Favorite::Home) { - fl!("home") - } else if let Favorite::Network { name, .. } = favorite { - name.clone() - } else if let Some(file_name) = path.file_name().and_then(|x| x.to_str()) { - file_name.to_string() - } else { - fl!("filesystem") - }; + let name = favorite + .display_name() + .unwrap_or_else(|| fl!("filesystem")); nav_model = nav_model.insert(move |b| { b.text(name.clone()) .icon( @@ -2290,15 +2289,33 @@ impl App { .favorites .iter() .map(|favorite| { - if let Favorite::Path(path) = favorite { - for (from, to) in path_changes.iter().map(|(f, t)| (f.as_ref(), t.as_ref())) { - if path.starts_with(from) - && let Ok(relative) = path.strip_prefix(from) + match favorite { + Favorite::Path(path) => { + for (from, to) in path_changes.iter().map(|(f, t)| (f.as_ref(), t.as_ref())) { - favorites_changed = true; - return Favorite::from_path(to.join(relative)); + if path.starts_with(from) + && let Ok(relative) = path.strip_prefix(from) + { + favorites_changed = true; + return Favorite::from_path(to.join(relative)); + } } } + Favorite::Named { path, name } => { + for (from, to) in path_changes.iter().map(|(f, t)| (f.as_ref(), t.as_ref())) + { + if path.starts_with(from) + && let Ok(relative) = path.strip_prefix(from) + { + favorites_changed = true; + return Favorite::Named { + path: to.join(relative), + name: name.clone(), + }; + } + } + } + _ => {} } favorite.clone() }) @@ -2613,6 +2630,11 @@ impl Application for App { } items.push(cosmic::widget::menu::Item::Divider); if favorite_index_opt.is_some() { + items.push(cosmic::widget::menu::Item::Button( + fl!("change-sidebar-label"), + None, + NavMenuAction::ChangeSidebarLabel(entity), + )); items.push(cosmic::widget::menu::Item::Button( fl!("remove-from-sidebar"), None, @@ -2904,7 +2926,8 @@ impl Application for App { } else { Favorite::from_path(path) }; - if !favorites.contains(&favorite) { + let favorite_path = favorite.path_opt(); + if !favorites.iter().any(|f| f.path_opt() == favorite_path) { favorites.push(favorite); } } @@ -3262,6 +3285,18 @@ impl Application for App { DialogPage::DeleteTrash { items } => { tasks.push(self.operation(Operation::DeleteTrash { items })); } + DialogPage::ChangeSidebarLabel { entity, label } => { + if let Some(FavoriteIndex(favorite_i)) = + self.nav_model.data::(entity) + { + let mut favorites = self.config.favorites.clone(); + if let Some(favorite) = favorites.get_mut(*favorite_i) { + *favorite = favorite.with_label(label.trim()); + config_set!(favorites, favorites); + tasks.push(self.update_config()); + } + } + } DialogPage::RenameItem { from, parent, name, .. } => { @@ -4487,7 +4522,8 @@ impl Application for App { tab::Command::AddToSidebar(path) => { let mut favorites = self.config.favorites.clone(); let favorite = Favorite::from_path(path); - if !favorites.contains(&favorite) { + let favorite_path = favorite.path_opt(); + if !favorites.iter().any(|f| f.path_opt() == favorite_path) { favorites.push(favorite); } config_set!(favorites, favorites); @@ -5271,6 +5307,26 @@ impl Application for App { return self.update_config(); } } + + NavMenuAction::ChangeSidebarLabel(entity) => { + if let Some(favorite) = self + .nav_model + .data::(entity) + .and_then(|FavoriteIndex(favorite_i)| { + self.config.favorites.get(*favorite_i) + }) + { + let label = favorite + .display_name() + .unwrap_or_else(|| fl!("filesystem")); + return Task::batch([ + self.dialog_pages + .push_back(DialogPage::ChangeSidebarLabel { entity, label }), + widget::text_input::focus(self.dialog_text_input.clone()), + widget::text_input::select_all(self.dialog_text_input.clone()), + ]); + } + } }, Message::Recents => { if self.config.show_recents { @@ -6164,6 +6220,40 @@ impl Application for App { target = target ))) } + DialogPage::ChangeSidebarLabel { entity, label } => { + let entity = *entity; + let complete_maybe = if label.trim().is_empty() { + None + } else { + Some(Message::DialogComplete) + }; + + widget::dialog() + .title(fl!("change-sidebar-label")) + .primary_action( + widget::button::suggested(fl!("save")) + .on_press_maybe(complete_maybe.clone()), + ) + .secondary_action( + widget::button::standard(fl!("cancel")).on_press(Message::DialogCancel), + ) + .control( + widget::column::with_children([ + widget::text::body(fl!("sidebar-label")).into(), + widget::text_input("", label.as_str()) + .id(self.dialog_text_input.clone()) + .on_input(move |label| { + Message::DialogUpdate(DialogPage::ChangeSidebarLabel { + entity, + label, + }) + }) + .on_submit_maybe(complete_maybe.map(|maybe| move |_| maybe.clone())) + .into(), + ]) + .spacing(space_xxs), + ) + } DialogPage::RenameItem { from, parent, diff --git a/src/config.rs b/src/config.rs index 2138d83..f064076 100644 --- a/src/config.rs +++ b/src/config.rs @@ -70,6 +70,11 @@ pub enum Favorite { name: String, path: PathBuf, }, + /// A path with a custom name chosen by the user + Named { + path: PathBuf, + name: String, + }, } impl Favorite { @@ -98,6 +103,38 @@ impl Favorite { Self::Videos => dirs::video_dir(), Self::Path(path) => Some(path.clone()), Self::Network { path, .. } => Some(path.clone()), + Self::Named { path, .. } => Some(path.clone()), + } + } + + /// Name shown in the sidebar, or `None` if the path has no usable file name + pub fn display_name(&self) -> Option { + match self { + Self::Home => Some(crate::fl!("home")), + Self::Named { name, .. } | Self::Network { name, .. } => Some(name.clone()), + _ => self + .path_opt()? + .file_name() + .and_then(|x| x.to_str()) + .map(ToString::to_string), + } + } + + /// Return this favorite with a custom sidebar label chosen by the user + pub fn with_label(&self, name: &str) -> Self { + match self { + Self::Network { uri, path, .. } => Self::Network { + uri: uri.clone(), + name: name.to_string(), + path: path.clone(), + }, + other => match other.path_opt() { + Some(path) => Self::Named { + path, + name: name.to_string(), + }, + None => other.clone(), + }, } } } @@ -385,3 +422,66 @@ pub const TIME_CONFIG_ID: &str = "com.system76.CosmicAppletTime"; pub struct TimeConfig { pub military_time: bool, } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn favorite_with_label_converts_path_to_named() { + let favorite = Favorite::Path(PathBuf::from("/some/dir")); + assert_eq!( + favorite.with_label("Custom"), + Favorite::Named { + path: PathBuf::from("/some/dir"), + name: "Custom".to_string(), + } + ); + } + + #[test] + fn favorite_with_label_updates_network_in_place() { + let favorite = Favorite::Network { + uri: "sftp://example.com/".to_string(), + name: "example.com".to_string(), + path: PathBuf::from("/run/mount/example"), + }; + assert_eq!( + favorite.with_label("Custom"), + Favorite::Network { + uri: "sftp://example.com/".to_string(), + name: "Custom".to_string(), + path: PathBuf::from("/run/mount/example"), + } + ); + } + + #[test] + fn favorite_with_label_converts_special_folder_to_named() { + let home = dirs::home_dir().unwrap(); + assert_eq!( + Favorite::Home.with_label("Custom"), + Favorite::Named { + path: home, + name: "Custom".to_string(), + } + ); + } + + #[test] + fn favorite_display_name() { + assert_eq!( + Favorite::Path(PathBuf::from("/some/dir")).display_name(), + Some("dir".to_string()) + ); + assert_eq!(Favorite::Path(PathBuf::from("/")).display_name(), None); + assert_eq!( + Favorite::Named { + path: PathBuf::from("/some/dir"), + name: "Custom".to_string(), + } + .display_name(), + Some("Custom".to_string()) + ); + } +} diff --git a/src/dialog.rs b/src/dialog.rs index b369b73..315691a 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -32,9 +32,7 @@ use std::{env, fmt, fs}; use crate::app::{ Action, ContextPage, Message as AppMessage, PreviewItem, PreviewKind, REPLACE_BUTTON_ID, }; -use crate::config::{ - Config, DialogConfig, Favorite, TIME_CONFIG_ID, ThumbCfg, TimeConfig, TypeToSearch, -}; +use crate::config::{Config, DialogConfig, TIME_CONFIG_ID, ThumbCfg, TimeConfig, TypeToSearch}; use crate::key_bind::key_binds; use crate::localize::LANGUAGE_SORTER; use crate::mounter::{MOUNTERS, MounterItem, MounterItems, MounterKey, MounterMessage}; @@ -889,13 +887,7 @@ impl App { for favorite in &self.flags.config.favorites { if let Some(path) = favorite.path_opt() { - let name = if matches!(favorite, Favorite::Home) { - fl!("home") - } else if let Favorite::Network { name, .. } = favorite { - name.clone() - } else if let Some(file_name) = path.file_name().and_then(|x| x.to_str()) { - file_name.to_string() - } else { + let Some(name) = favorite.display_name() else { continue; }; nav_model = nav_model.insert(move |b| { diff --git a/src/tab.rs b/src/tab.rs index 6de11e7..60d9bf2 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -437,7 +437,9 @@ impl<'a> FormatTime<'a> { impl Display for FormatTime<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let zoned = jiff::Zoned::try_from(self.time).unwrap(); + let Ok(zoned) = jiff::Zoned::try_from(self.time) else { + return Ok(()); + }; let now = jiff::Zoned::now(); let icu_datetime = DateTime::convert_from(zoned.datetime()); if zoned.date() == now.date() { @@ -1931,7 +1933,7 @@ impl ItemMetadata { Self::Path { metadata, .. } => metadata.modified().ok(), #[cfg(feature = "gvfs")] Self::GvfsPath { mtime, .. } => { - Some(SystemTime::UNIX_EPOCH + Duration::from_secs(*mtime)) + SystemTime::UNIX_EPOCH.checked_add(Duration::from_secs(*mtime)) } _ => None, }