Add to sidebar in breadcrumb context menu, fixes #526, fixes #395

This commit is contained in:
Jeremy Soller 2024-10-14 10:09:57 -06:00
parent a6f7a70f3d
commit a9daddb1b0
No known key found for this signature in database
GPG key ID: D02FD439211AF56F
3 changed files with 41 additions and 13 deletions

View file

@ -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);

View file

@ -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))

View file

@ -450,18 +450,18 @@ pub fn item_from_entry(
pub fn item_from_path<P: Into<PathBuf>>(path: P, sizes: IconSizes) -> Result<Item, String> {
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<PathBuf>),
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 {