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.

Items of several folders get their file type's menu rows again (743)

gelenmedyae1232232aa0c
diff --git a/crates/rf-win/examples/menu_rows.rs b/crates/rf-win/examples/menu_rows.rs
new file mode 100644
index 00000000..7015e549
--- /dev/null
+++ b/crates/rf-win/examples/menu_rows.rs
@@ -0,0 +1,29 @@
+//! Prints the rows of the shell's context menu for the files given, as the file list builds it (issue 743: items of two folders
+//! lost the rows their file type adds, such as WPS's "PDF merge" or an image's "Rotate").
+//!
+//! cargo run -p rf-win --example menu_rows -- <file> [<file> ...]
+//!
+//! Read-only: the menu is built and released, nothing is invoked.
+
+use std::path::PathBuf;
+use std::time::Duration;
+
+use rf_win::ctxmenu::{self, ShellTarget};
+
+fn main() {
+ let items: Vec<PathBuf> = std::env::args().skip(1).map(PathBuf::from).collect();
+ let (tx, rx) = std::sync::mpsc::channel();
+ rf_win::sta::run(move |owner| {
+ let out = match ctxmenu::build(owner, &ShellTarget::Items(items), false) {
+ Ok((session, rows)) => {
+ ctxmenu::release(session);
+ rows.iter().filter(|r| !r.label.is_empty()).map(|r| format!("{} [{}]", r.label, r.verb)).collect()
+ }
+ Err(e) => vec![format!("error: {e}")],
+ };
+ let _ = tx.send(out);
+ });
+ for row in rx.recv_timeout(Duration::from_secs(20)).unwrap_or_else(|_| vec!["(no answer)".into()]) {
+ println!("{row}");
+ }
+}
diff --git a/crates/rf-win/src/ctxmenu.rs b/crates/rf-win/src/ctxmenu.rs
index 513052a7..95eacd4c 100644
--- a/crates/rf-win/src/ctxmenu.rs
+++ b/crates/rf-win/src/ctxmenu.rs
@@ -68,7 +68,52 @@ fn folder(path: &Path) -> anyhow::Result<IShellFolder> {
}
fn menu_for_items(owner: HWND, items: &[PathBuf]) -> anyhow::Result<IContextMenu> {
- ui_object_for_items(owner, items)
+ let Some(parent) = items.first().and_then(|p| p.parent()) else { return ui_object_for_items(owner, items) };
+ if items.iter().all(|p| p.parent() == Some(parent)) {
+ return ui_object_for_items(owner, items);
+ }
+ // Items of several folders: the desktop's own menu for full ID lists knows no file type, so it had only the rows every file
+ // gets (issue 743, 1.0.7: two PDFs from two folders lost WPS's "PDF merge"; two pictures lost Rotate and the editors). The
+ // default menu is built here with the FIRST item's associations, as File Explorer's search results do; the handlers still get
+ // every item, through the desktop's data object of the full ID lists.
+ match menu_across_folders(owner, items) {
+ Ok(cm) => Ok(cm),
+ Err(e) => {
+ log::debug!("context menu across folders with associations: {e}; the plain one");
+ ui_object_for_items(owner, items)
+ }
+ }
+}
+
+fn menu_across_folders(owner: HWND, items: &[PathBuf]) -> anyhow::Result<IContextMenu> {
+ use std::mem::ManuallyDrop;
+ use windows::Win32::UI::Shell::{DEFCONTEXTMENU, IQueryAssociations, SHBindToParent, SHCreateDefaultContextMenu, SHGetDesktopFolder};
+ let abs: Vec<Pidl> = items.iter().map(|p| parse(p)).collect::<anyhow::Result<_>>()?;
+ let assoc: IQueryAssociations = unsafe {
+ let mut last: *mut ITEMIDLIST = std::ptr::null_mut();
+ let sf: IShellFolder = SHBindToParent(abs[0].0, Some(&mut last))?;
+ sf.GetUIObjectOf(owner, &[last as *const ITEMIDLIST], None)?
+ };
+ let desktop = unsafe { SHGetDesktopFolder()? };
+ let mut full: Vec<*mut ITEMIDLIST> = abs.iter().map(|p| p.0).collect();
+ let mut dcm = DEFCONTEXTMENU {
+ hwnd: owner,
+ pcmcb: ManuallyDrop::new(None),
+ pidlFolder: std::ptr::null_mut(),
+ psf: ManuallyDrop::new(Some(desktop)),
+ cidl: full.len() as u32,
+ apidl: full.as_mut_ptr(),
+ punkAssociationInfo: ManuallyDrop::new(Some(assoc.cast()?)),
+ cKeys: 0,
+ aKeys: std::ptr::null(),
+ };
+ // SAFETY: `dcm` and the ID lists it points at (`abs`, `full`) live until the call returns; the menu keeps its own references.
+ let cm = unsafe { SHCreateDefaultContextMenu::<IContextMenu>(&dcm) };
+ unsafe {
+ ManuallyDrop::drop(&mut dcm.psf);
+ ManuallyDrop::drop(&mut dcm.punkAssociationInfo);
+ }
+ Ok(cm?)
}
/// The shell's object (`IContextMenu`, `IDataObject`, …) for `items`. Items of one folder go through that folder, with their own