Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

Right-click menu: items of several folders keep their own paths (743)

gelenmedyad9d418ac8721
diff --git a/crates/rf-win/src/ctxmenu.rs b/crates/rf-win/src/ctxmenu.rs
index 329dee15..36726275 100644
--- a/crates/rf-win/src/ctxmenu.rs
+++ b/crates/rf-win/src/ctxmenu.rs
@@ -68,15 +68,26 @@ fn folder(path: &Path) -> anyhow::Result<IShellFolder> {
}
fn menu_for_items(owner: HWND, items: &[PathBuf]) -> anyhow::Result<IContextMenu> {
+ ui_object_for_items(owner, items)
+}
+
+/// The shell's object (`IContextMenu`, `IDataObject`, …) for `items`. Items of one folder go through that folder, with their own
+/// names. Items of several folders (search and tag results) go through the desktop folder with their full ID lists, as File
+/// Explorer's own search results do: through the first item's folder, each was taken by its NAME there, so two files of the same
+/// name in two folders became that folder's one file twice, and the others were left out (issue 743: a PDF merge given two same-named
+/// PDFs from two folders got only one).
+fn ui_object_for_items<T: Interface>(owner: HWND, items: &[PathBuf]) -> anyhow::Result<T> {
anyhow::ensure!(!items.is_empty(), "no items");
let parent = items[0].parent().ok_or_else(|| anyhow::anyhow!("no parent"))?;
- // Child pidls are resolved relative to ONE folder: items from other folders (search/tag
- // results) would silently map to the wrong files, so leave them out.
- let items: Vec<&PathBuf> = items.iter().filter(|p| p.parent() == Some(parent)).collect();
- let sf = folder(parent)?;
let abs: Vec<Pidl> = items.iter().map(|p| parse(p)).collect::<anyhow::Result<_>>()?;
- let children: Vec<*const ITEMIDLIST> = abs.iter().map(|p| unsafe { ILFindLastID(p.0) as *const _ }).collect();
- Ok(unsafe { sf.GetUIObjectOf(owner, &children, None)? })
+ if items.iter().all(|p| p.parent() == Some(parent)) {
+ let sf = folder(parent)?;
+ let children: Vec<*const ITEMIDLIST> = abs.iter().map(|p| unsafe { ILFindLastID(p.0) as *const _ }).collect();
+ return Ok(unsafe { sf.GetUIObjectOf(owner, &children, None)? });
+ }
+ let desktop = unsafe { windows::Win32::UI::Shell::SHGetDesktopFolder()? };
+ let full: Vec<*const ITEMIDLIST> = abs.iter().map(|p| p.0 as *const _).collect();
+ Ok(unsafe { desktop.GetUIObjectOf(owner, &full, None)? })
}
/// Bound through the item's real parent in the shell namespace, so drive roots (`C:\`,
@@ -887,3 +898,37 @@ mod tests {
dump(&bg.unwrap(), 0);
}
}
+
+#[cfg(test)]
+mod items_of_several_folders {
+ /// Issue 743: two files of the same name in two folders reach the shell as two files, each with its own folder.
+ #[test]
+ fn same_names_in_two_folders_stay_two_files() {
+ use windows::Win32::System::Com::{DVASPECT_CONTENT, FORMATETC, IDataObject, TYMED_HGLOBAL};
+ use windows::Win32::System::Ole::{CF_HDROP, ReleaseStgMedium};
+ use windows::Win32::UI::Shell::{DragQueryFileW, HDROP};
+ crate::shell::ensure_com();
+ let root = std::env::temp_dir().join(format!("rf-743-{}", std::process::id()));
+ let (a, b) = (root.join("test1").join("same.pdf"), root.join("test2").join("same.pdf"));
+ for p in [&a, &b] {
+ std::fs::create_dir_all(p.parent().unwrap()).unwrap();
+ std::fs::write(p, b"x").unwrap();
+ }
+ let obj: IDataObject = super::ui_object_for_items(windows::Win32::Foundation::HWND::default(), &[a.clone(), b.clone()]).unwrap();
+ let fmt = FORMATETC { cfFormat: CF_HDROP.0, ptd: std::ptr::null_mut(), dwAspect: DVASPECT_CONTENT.0, lindex: -1, tymed: TYMED_HGLOBAL.0 as u32 };
+ let mut medium = unsafe { obj.GetData(&fmt) }.unwrap();
+ let drop = HDROP(unsafe { medium.u.hGlobal.0 });
+ let n = unsafe { DragQueryFileW(drop, u32::MAX, None) };
+ let mut got = Vec::new();
+ for i in 0..n {
+ let mut buf = [0u16; 520];
+ let len = unsafe { DragQueryFileW(drop, i, Some(&mut buf)) } as usize;
+ got.push(std::path::PathBuf::from(String::from_utf16_lossy(&buf[..len])));
+ }
+ unsafe { ReleaseStgMedium(&mut medium) };
+ let _ = std::fs::remove_dir_all(&root);
+ let canon = |p: &std::path::Path| p.to_string_lossy().to_lowercase();
+ assert_eq!(got.iter().map(|p| canon(p)).collect::<Vec<_>>(), vec![canon(&a), canon(&b)], "{got:?}");
+ }
+}
+