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.

Clipboard: copied files also carry their Shell IDList Array

Alpha Malick DIAWARAbcb77ca6e9bd
diff --git a/crates/rf-win/src/clipboard.rs b/crates/rf-win/src/clipboard.rs
index 3a8d9a8e..4bf44131 100644
--- a/crates/rf-win/src/clipboard.rs
+++ b/crates/rf-win/src/clipboard.rs
@@ -90,10 +90,82 @@ pub fn set_files(owner: HWND, paths: &[PathBuf], kind: Transfer) -> anyhow::Resu
let fmt = RegisterClipboardFormatW(w!("Preferred DropEffect"));
let h2 = alloc(&effect.to_le_bytes())?;
SetClipboardData(fmt, Some(HANDLE(h2.0)))?;
+ // Without the item list Explorer's menus only offer a plain Paste: no "Paste shortcut" on the desktop or in a folder.
+ // (best effort: a clipboard without it still pastes files)
+ if let Some(cida) = shell_id_list(paths) {
+ let fmt = RegisterClipboardFormatW(w!("Shell IDList Array"));
+ if let Ok(h3) = alloc(&cida) {
+ let _ = SetClipboardData(fmt, Some(HANDLE(h3.0)));
+ }
+ }
}
Ok(())
}
+/// The "Shell IDList Array" (CIDA) of `paths`: the folder they share as an absolute item id list, then each item's own id.
+/// `None` when they do not share a folder or one of them cannot be named to the shell.
+fn shell_id_list(paths: &[PathBuf]) -> Option<Vec<u8>> {
+ use windows::Win32::UI::Shell::Common::ITEMIDLIST;
+ use windows::Win32::UI::Shell::{ILClone, ILFindLastID, ILFree, ILGetSize, ILRemoveLastID, SHParseDisplayName};
+ use windows::core::PCWSTR;
+
+ let parent = paths.first()?.parent()?.to_path_buf();
+ if paths.iter().any(|p| p.parent() != Some(parent.as_path())) {
+ return None;
+ }
+ unsafe fn bytes_of(pidl: *const ITEMIDLIST) -> Vec<u8> {
+ unsafe { std::slice::from_raw_parts(pidl as *const u8, ILGetSize(Some(pidl)) as usize).to_vec() }
+ }
+ unsafe {
+ let mut folder: *mut ITEMIDLIST = std::ptr::null_mut();
+ let first = wide(&paths[0]);
+ SHParseDisplayName(PCWSTR(first.as_ptr()), None, &mut folder, 0, None).ok()?;
+ if folder.is_null() {
+ return None;
+ }
+ let mut parts: Vec<Vec<u8>> = Vec::new();
+ // the parent: the first item's list without its last id
+ let up = ILClone(folder);
+ let mut ok = !up.is_null();
+ if ok {
+ let _ = ILRemoveLastID(Some(up));
+ parts.push(bytes_of(up));
+ ILFree(Some(up));
+ }
+ ILFree(Some(folder));
+ if !ok {
+ return None;
+ }
+ for p in paths {
+ let w = wide(p);
+ let mut item: *mut ITEMIDLIST = std::ptr::null_mut();
+ if SHParseDisplayName(PCWSTR(w.as_ptr()), None, &mut item, 0, None).is_err() || item.is_null() {
+ ok = false;
+ break;
+ }
+ parts.push(bytes_of(ILFindLastID(item)));
+ ILFree(Some(item));
+ }
+ if !ok {
+ return None;
+ }
+ // CIDA: the count of items, then the offsets of the parent and of each item, then the lists
+ let n = paths.len() as u32;
+ let head = 4 + 4 * (n + 1);
+ let mut out = Vec::new();
+ out.extend_from_slice(&n.to_le_bytes());
+ let mut at = head;
+ for part in &parts {
+ out.extend_from_slice(&at.to_le_bytes());
+ at += part.len() as u32;
+ }
+ for part in &parts {
+ out.extend_from_slice(part);
+ }
+ Some(out)
+ }
+}
+
pub fn has_files() -> bool {
unsafe { IsClipboardFormatAvailable(CF_HDROP.0 as u32).is_ok() }
}
@@ -173,3 +245,24 @@ pub fn set_image(owner: HWND, w: u32, h: u32, rgba: &[u8], png: &[u8]) -> anyhow
}
Ok(())
}
+
+#[cfg(test)]
+mod cida_tests {
+ use super::*;
+
+ #[test]
+ fn item_list_names_the_folder_then_each_item() {
+ let dir = std::env::temp_dir();
+ let (a, b) = (dir.join("rf_cida_a.txt"), dir.join("rf_cida_b.txt"));
+ std::fs::write(&a, b"a").unwrap();
+ std::fs::write(&b, b"b").unwrap();
+ let cida = shell_id_list(&[a.clone(), b.clone()]).expect("two files of one folder");
+ let u = |i: usize| u32::from_le_bytes(cida[i..i + 4].try_into().unwrap());
+ assert_eq!(u(0), 2);
+ assert_eq!(u(4) as usize, 16, "the folder's list follows the three offsets");
+ assert!(u(8) > u(4) && u(12) > u(8) && (u(12) as usize) < cida.len());
+ // files of different folders cannot share a list
+ assert!(shell_id_list(&[a.clone(), dir.parent().unwrap().join("x.txt")]).is_none());
+ let _ = (std::fs::remove_file(a), std::fs::remove_file(b));
+ }
+}