From a9daddb1b09e9df8a86173a73178d1cb72a68bb6 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 14 Oct 2024 10:09:57 -0600 Subject: [PATCH] Add to sidebar in breadcrumb context menu, fixes #526, fixes #395 --- src/app.rs | 11 ++++++++++- src/menu.rs | 7 +++++++ src/tab.rs | 36 ++++++++++++++++++++++++------------ 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/app.rs b/src/app.rs index f1ab56b..42ca712 100644 --- a/src/app.rs +++ b/src/app.rs @@ -772,7 +772,7 @@ impl App { } else if let Some(file_name) = path.file_name().and_then(|x| x.to_str()) { file_name.to_string() } else { - continue; + fl!("filesystem") }; nav_model = nav_model.insert(move |b| { b.text(name.clone()) @@ -2509,6 +2509,15 @@ impl Application for App { self.set_show_context(true); self.set_context_title(self.context_page.title()); } + tab::Command::AddToSidebar(path) => { + let mut favorites = self.config.favorites.clone(); + let favorite = Favorite::from_path(path); + if !favorites.iter().any(|f| f == &favorite) { + favorites.push(favorite); + } + config_set!(favorites, favorites); + commands.push(self.update_config()); + } tab::Command::ChangeLocation(tab_title, tab_path, selection_path) => { self.activate_nav_model_location(&tab_path); diff --git a/src/menu.rs b/src/menu.rs index 9160dfe..a47cb29 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -603,6 +603,7 @@ pub fn menu_bar<'a>( } pub fn location_context_menu<'a>(ancestor_index: usize) -> Element<'a, tab::Message> { + //TODO: only add some of these when in App mode let children = vec![ menu_button!(text::body(fl!("open-in-new-tab"))) .on_press(tab::Message::LocationMenuAction( @@ -620,6 +621,12 @@ pub fn location_context_menu<'a>(ancestor_index: usize) -> Element<'a, tab::Mess LocationMenuAction::Preview(ancestor_index), )) .into(), + divider::horizontal::light().into(), + menu_button!(text::body(fl!("add-to-sidebar"))) + .on_press(tab::Message::LocationMenuAction( + LocationMenuAction::AddToSidebar(ancestor_index), + )) + .into(), ]; container(column::with_children(children)) diff --git a/src/tab.rs b/src/tab.rs index 275075d..befeced 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -450,18 +450,18 @@ pub fn item_from_entry( pub fn item_from_path>(path: P, sizes: IconSizes) -> Result { let path = path.into(); - let name_os = path - .file_name() - .ok_or_else(|| format!("failed to get file name from path {:?}", path))?; - let name = name_os - .to_str() - .ok_or_else(|| { - format!( - "failed to parse file name for {:?}: {:?} is not valid UTF-8", - path, name_os - ) - })? - .to_string(); + let name = match path.file_name() { + Some(name_os) => name_os + .to_str() + .ok_or_else(|| { + format!( + "failed to parse file name for {:?}: {:?} is not valid UTF-8", + path, name_os + ) + })? + .to_string(), + None => fl!("filesystem"), + }; let metadata = fs::metadata(&path) .map_err(|err| format!("failed to read metadata for {:?}: {}", path, err))?; Ok(item_from_entry(path, name, metadata, sizes)) @@ -942,6 +942,7 @@ impl Location { pub enum Command { Action(Action), AddNetworkDrive, + AddToSidebar(PathBuf), ChangeLocation(String, Location, Option), DropFiles(PathBuf, ClipboardPaste), EmptyTrash, @@ -1014,6 +1015,7 @@ pub enum LocationMenuAction { OpenInNewTab(usize), OpenInNewWindow(usize), Preview(usize), + AddToSidebar(usize), } impl MenuAction for LocationMenuAction { @@ -2200,6 +2202,16 @@ impl Tab { } } } + LocationMenuAction::AddToSidebar(ancestor_index) => { + if let Some(path) = path_for_index(ancestor_index) { + commands.push(Command::AddToSidebar(path)); + } else { + log::warn!( + "no ancestor {ancestor_index} for location {:?}", + self.location + ); + } + } } } Message::Drag(rect_opt) => match rect_opt {