Skip to content

Commit a9daddb

Browse files
committed
Add to sidebar in breadcrumb context menu, fixes #526, fixes #395
1 parent a6f7a70 commit a9daddb

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

src/app.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ impl App {
772772
} else if let Some(file_name) = path.file_name().and_then(|x| x.to_str()) {
773773
file_name.to_string()
774774
} else {
775-
continue;
775+
fl!("filesystem")
776776
};
777777
nav_model = nav_model.insert(move |b| {
778778
b.text(name.clone())
@@ -2509,6 +2509,15 @@ impl Application for App {
25092509
self.set_show_context(true);
25102510
self.set_context_title(self.context_page.title());
25112511
}
2512+
tab::Command::AddToSidebar(path) => {
2513+
let mut favorites = self.config.favorites.clone();
2514+
let favorite = Favorite::from_path(path);
2515+
if !favorites.iter().any(|f| f == &favorite) {
2516+
favorites.push(favorite);
2517+
}
2518+
config_set!(favorites, favorites);
2519+
commands.push(self.update_config());
2520+
}
25122521
tab::Command::ChangeLocation(tab_title, tab_path, selection_path) => {
25132522
self.activate_nav_model_location(&tab_path);
25142523

src/menu.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ pub fn menu_bar<'a>(
603603
}
604604

605605
pub fn location_context_menu<'a>(ancestor_index: usize) -> Element<'a, tab::Message> {
606+
//TODO: only add some of these when in App mode
606607
let children = vec![
607608
menu_button!(text::body(fl!("open-in-new-tab")))
608609
.on_press(tab::Message::LocationMenuAction(
@@ -620,6 +621,12 @@ pub fn location_context_menu<'a>(ancestor_index: usize) -> Element<'a, tab::Mess
620621
LocationMenuAction::Preview(ancestor_index),
621622
))
622623
.into(),
624+
divider::horizontal::light().into(),
625+
menu_button!(text::body(fl!("add-to-sidebar")))
626+
.on_press(tab::Message::LocationMenuAction(
627+
LocationMenuAction::AddToSidebar(ancestor_index),
628+
))
629+
.into(),
623630
];
624631

625632
container(column::with_children(children))

src/tab.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -450,18 +450,18 @@ pub fn item_from_entry(
450450

451451
pub fn item_from_path<P: Into<PathBuf>>(path: P, sizes: IconSizes) -> Result<Item, String> {
452452
let path = path.into();
453-
let name_os = path
454-
.file_name()
455-
.ok_or_else(|| format!("failed to get file name from path {:?}", path))?;
456-
let name = name_os
457-
.to_str()
458-
.ok_or_else(|| {
459-
format!(
460-
"failed to parse file name for {:?}: {:?} is not valid UTF-8",
461-
path, name_os
462-
)
463-
})?
464-
.to_string();
453+
let name = match path.file_name() {
454+
Some(name_os) => name_os
455+
.to_str()
456+
.ok_or_else(|| {
457+
format!(
458+
"failed to parse file name for {:?}: {:?} is not valid UTF-8",
459+
path, name_os
460+
)
461+
})?
462+
.to_string(),
463+
None => fl!("filesystem"),
464+
};
465465
let metadata = fs::metadata(&path)
466466
.map_err(|err| format!("failed to read metadata for {:?}: {}", path, err))?;
467467
Ok(item_from_entry(path, name, metadata, sizes))
@@ -942,6 +942,7 @@ impl Location {
942942
pub enum Command {
943943
Action(Action),
944944
AddNetworkDrive,
945+
AddToSidebar(PathBuf),
945946
ChangeLocation(String, Location, Option<PathBuf>),
946947
DropFiles(PathBuf, ClipboardPaste),
947948
EmptyTrash,
@@ -1014,6 +1015,7 @@ pub enum LocationMenuAction {
10141015
OpenInNewTab(usize),
10151016
OpenInNewWindow(usize),
10161017
Preview(usize),
1018+
AddToSidebar(usize),
10171019
}
10181020

10191021
impl MenuAction for LocationMenuAction {
@@ -2200,6 +2202,16 @@ impl Tab {
22002202
}
22012203
}
22022204
}
2205+
LocationMenuAction::AddToSidebar(ancestor_index) => {
2206+
if let Some(path) = path_for_index(ancestor_index) {
2207+
commands.push(Command::AddToSidebar(path));
2208+
} else {
2209+
log::warn!(
2210+
"no ancestor {ancestor_index} for location {:?}",
2211+
self.location
2212+
);
2213+
}
2214+
}
22032215
}
22042216
}
22052217
Message::Drag(rect_opt) => match rect_opt {

0 commit comments

Comments
 (0)