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.
Rust archive-managerdesktop-appdual-paneexplorer-alternativefile-browserfile-explorerfile-managerfluent-designgpu-acceleratedgpuimacosmicaproductivityquick-lookrustrust-guitabswindowswindows-11windows-explorer
//! Native Explorer context menu (IContextMenu), shown from the shell STA thread.
use std::path::{Path, PathBuf};
use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, POINT, WPARAM};
use windows::Win32::System::Com::CoTaskMemFree;
use windows::Win32::UI::Shell::Common::ITEMIDLIST;
use windows::Win32::UI::Shell::{
CMF_CANRENAME, CMF_EXPLORE, CMF_EXTENDEDVERBS, CMF_NORMAL, CMIC_MASK_PTINVOKE,
CMINVOKECOMMANDINFO, CMINVOKECOMMANDINFOEX, GCS_VERBW, IContextMenu, IContextMenu2, IContextMenu3, ILFindLastID,
IShellFolder, SHBindToObject, SHParseDisplayName,
};
use windows::Win32::UI::WindowsAndMessaging::{
CreatePopupMenu, DestroyMenu, InsertMenuW, MF_BYPOSITION, MF_GRAYED, MF_SEPARATOR, MF_STRING, PostMessageW, SW_SHOWNORMAL, SetForegroundWindow, TPM_RETURNCMD, TPM_RIGHTBUTTON,
TrackPopupMenuEx, WM_DRAWITEM, WM_INITMENUPOPUP, WM_MEASUREITEM, WM_MENUCHAR, WM_NULL,
};
use windows::core::{Interface, PCSTR, PCWSTR, PSTR};
use crate::sta::MENU_HOOK;
const CMIC_MASK_UNICODE: u32 = 0x4000;
use crate::wide::wide;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MenuOutcome {
Dismissed,
/// User chose "Rename": the app should start inline rename.
Rename,
/// A shell verb ran (e.g. "delete", "copy", "properties", or a handler without a verb).
Invoked(String),
/// One of the app's own items (see `ExtraItem`) was chosen.
Custom(u32),
}
/// App-provided entries shown above the shell's items. `id` is returned in `MenuOutcome::Custom`.
#[derive(Debug, Clone)]
pub struct ExtraItem {
pub id: u32,
pub label: String,
pub enabled: bool,
}
const EXTRA_BASE: u32 = 0x9000;
struct Pidl(*mut ITEMIDLIST);
impl Drop for Pidl {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe { CoTaskMemFree(Some(self.0 as *const _)) }
}
}
}
fn parse(path: &Path) -> anyhow::Result<Pidl> {
let w = crate::wide::shell_name(path);
let mut pidl: *mut ITEMIDLIST = std::ptr::null_mut();
match unsafe { SHParseDisplayName(PCWSTR(w.as_ptr()), None, &mut pidl, 0, None) } {
Ok(()) => Ok(Pidl(pidl)),
// a phone's item the shell does not parse back (issue 221): found by walking down to it
Err(_) if crate::shellfolder::is_namespace_name(&path.to_string_lossy()) => Ok(Pidl(crate::shellfolder::id_list(&path.to_string_lossy())?)),
Err(e) => Err(e.into()),
}
}
fn folder(path: &Path) -> anyhow::Result<IShellFolder> {
let pidl = parse(path)?;
Ok(unsafe { SHBindToObject::<_, _, IShellFolder>(None, pidl.0, None)? })
}
fn menu_for_items(owner: HWND, items: &[PathBuf]) -> anyhow::Result<IContextMenu> {
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)) {
match ui_object_for_items(owner, items) {
Ok(cm) => return Ok(cm),
// The signed-in user's profile folder (C:\Users\<name>) is parsed into Windows' own "user files" namespace, not as a
// child of C:\Users, and that folder refuses the foreign ID (E_INVALIDARG: no menu at all, issue 801). The item through
// its real parent, or several through the desktop, as below.
Err(e) if items.len() == 1 => {
log::debug!("context menu through the folder: {e}; through the item's own parent");
return menu_for_one(owner, &items[0]);
}
Err(e) => log::debug!("context menu through the folder: {e}; through the desktop"),
}
}
// 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
/// 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"))?;
let abs: Vec<Pidl> = items.iter().map(|p| parse(p)).collect::<anyhow::Result<_>>()?;
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:\`,
/// parent = "This PC") work too.
fn menu_for_one(owner: HWND, item: &Path) -> anyhow::Result<IContextMenu> {
let abs = parse(item)?;
let mut last: *mut ITEMIDLIST = std::ptr::null_mut();
let sf: IShellFolder = unsafe { windows::Win32::UI::Shell::SHBindToParent(abs.0, Some(&mut last))? };
let children = [last as *const ITEMIDLIST];
Ok(unsafe { sf.GetUIObjectOf(owner, &children, None)? })
}
fn menu_for_background(owner: HWND, dir: &Path) -> anyhow::Result<IContextMenu> {
let sf = folder(dir)?;
Ok(unsafe { sf.CreateViewObject(owner)? })
}
/// Must be called on the shell STA thread (see `sta::run`) with its owner window.
pub fn show_for_items(
owner: HWND,
items: &[PathBuf],
screen: (i32, i32),
extended: bool,
extra: &[ExtraItem],
) -> anyhow::Result<MenuOutcome> {
let cm = menu_for_items(owner, items)?;
run_menu(owner, &cm, working_dir(&ShellTarget::Items(items.to_vec())).as_deref(), screen, extended, true, extra)
}
/// Menu for one item (used by the sidebar).
pub fn show_for_one(
owner: HWND,
item: &Path,
screen: (i32, i32),
extended: bool,
extra: &[ExtraItem],
) -> anyhow::Result<MenuOutcome> {
let cm = menu_for_one(owner, item)?;
// no inline rename from the sidebar
run_menu(owner, &cm, working_dir(&ShellTarget::One(item.to_path_buf())).as_deref(), screen, extended, false, extra)
}
/// Background (empty space) menu of a folder.
pub fn show_for_background(
owner: HWND,
dir: &Path,
screen: (i32, i32),
extended: bool,
extra: &[ExtraItem],
) -> anyhow::Result<MenuOutcome> {
let cm = menu_for_background(owner, dir)?;
run_menu(owner, &cm, Some(dir), screen, extended, false, extra)
}
// ---------- the shell menu as data (rendered by the app's own Fluent menu) ----------
/// What a shell menu is built for.
#[derive(Debug, Clone)]
pub enum ShellTarget {
/// Items of one folder (the selection).
Items(Vec<PathBuf>),
/// A single item through its real parent (sidebar entries, drive roots).
One(PathBuf),
/// The empty area of a folder.
Background(PathBuf),
}
/// A menu bitmap as top-down BGRA rows (premultiplied, as the shell hands them out).
///
/// When `monochrome` is true (a 1-bit check-mark bitmap such as 7-Zip's `MenuLogo.bmp`), the
/// bitmap is returned as a mask: 0-bits (foreground ink) are opaque (`alpha = 255`) and 1-bits
/// (background) are transparent (`alpha = 0`), so the menu can tint it in its text colour.
#[derive(Debug, Clone)]
pub struct ShellIcon {
pub width: u32,
pub height: u32,
pub bgra: Vec<u8>,
pub monochrome: bool,
}
#[derive(Debug, Clone)]
pub struct ShellItem {
/// Command id for [`invoke`]; 0 for separators and submenus.
pub id: u32,
pub label: String,
/// Text after a tab in the label (shortcut hint), if any.
pub hint: Option<String>,
/// Lowercased access key from a single `&` in the raw label (issue 659).
pub mnemonic: Option<char>,
/// Canonical verb ("open", "properties", "Windows.ModernShare"…), empty if the handler has none.
pub verb: String,
pub icon: Option<ShellIcon>,
pub enabled: bool,
pub separator: bool,
pub children: Vec<ShellItem>,
/// A submenu [`build_deferring`] left unfilled (the shell fills it when it opens, and that is slow: "Open with" took 70–210 ms):
/// its items come from [`fill_deferred`] under this number.
pub deferred: Option<u8>,
}
/// At most this many submenus are left for later in one menu; any more are filled at once, as before.
pub const MAX_DEFERRED: usize = 4;
struct Session {
id: u64,
cm: IContextMenu,
/// The folder a command runs in (see [`working_dir`]).
dir: Option<PathBuf>,
hmenu: windows::Win32::UI::WindowsAndMessaging::HMENU,
/// The submenus left unfilled, in [`ShellItem::deferred`]'s order: their position in the top level and their menu.
deferred: Vec<(u32, windows::Win32::UI::WindowsAndMessaging::HMENU)>,
}
impl Drop for Session {
fn drop(&mut self) {
unsafe {
let _ = DestroyMenu(self.hmenu);
}
}
}
thread_local! {
/// The live shell menus, newest last (each kept until invoked or released; the oldest goes when there are more than
/// `MAX_SESSIONS`). More than one: a menu built ahead of a right click (rustyfiles prepares one while the pointer rests on a file)
/// must not be thrown away by the next one built.
static SESSIONS: std::cell::RefCell<Vec<Session>> = const { std::cell::RefCell::new(Vec::new()) };
}
const MAX_SESSIONS: usize = 4;
fn keep_session(s: Session) {
SESSIONS.with(|v| {
let mut v = v.borrow_mut();
v.push(s);
while v.len() > MAX_SESSIONS {
v.remove(0);
}
});
}
fn take_session(id: u64) -> Option<Session> {
SESSIONS.with(|v| {
let mut v = v.borrow_mut();
let i = v.iter().position(|s| s.id == id)?;
Some(v.remove(i))
})
}
static NEXT_SESSION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
/// The folder a command of `target`'s menu runs in, as Explorer gives it: the items' folder, or the folder itself for its empty area.
/// Without it a command line that works on the current folder ("Run with PowerShell", a script reading files beside it) ran in
/// rustyfiles' own working folder (issue 290).
fn working_dir(target: &ShellTarget) -> Option<PathBuf> {
match target {
ShellTarget::Items(items) => items.first().and_then(|p| p.parent()).map(Path::to_path_buf),
// (a drive root has no parent: it is its own folder)
ShellTarget::One(item) => Some(item.parent().map_or_else(|| item.clone(), Path::to_path_buf)),
ShellTarget::Background(dir) => Some(dir.clone()),
}
}
fn context_menu_for(owner: HWND, target: &ShellTarget) -> anyhow::Result<(IContextMenu, bool)> {
Ok(match target {
ShellTarget::Items(items) => (menu_for_items(owner, items)?, true),
ShellTarget::One(item) => (menu_for_one(owner, item)?, false),
ShellTarget::Background(dir) => (menu_for_background(owner, dir)?, false),
})
}
/// Builds the shell menu for `target` and returns it as data plus a session id for
/// [`invoke`]. Submenus that fill themselves on demand (Send to, Open with…) are initialised
/// here. Call on the shell STA thread.
pub fn build(owner: HWND, target: &ShellTarget, extended: bool) -> anyhow::Result<(u64, Vec<ShellItem>)> {
build_ex(owner, target, extended, false)
}
/// [`build`], leaving for later the first-level submenus the shell fills only when they open ("Open with", "Cast to device"): they
/// come back with [`ShellItem::deferred`] set and no items, and [`fill_deferred`] fills them. Call on the shell STA thread.
pub fn build_deferring(owner: HWND, target: &ShellTarget) -> anyhow::Result<(u64, Vec<ShellItem>)> {
build_ex(owner, target, false, true)
}
/// Fills the submenus [`build_deferring`] left for later in session `session`: (their [`ShellItem::deferred`] number, their
/// items). Call on the shell STA thread, after `build_deferring`.
pub fn fill_deferred(session: u64) -> anyhow::Result<Vec<(u8, Vec<ShellItem>)>> {
SESSIONS.with(|v| {
let v = v.borrow();
let s = v.iter().find(|s| s.id == session).ok_or_else(|| anyhow::anyhow!("menu session expired"))?;
let cm3: Option<IContextMenu3> = s.cm.cast().ok();
let cm2: Option<IContextMenu2> = s.cm.cast().ok();
let mut out = Vec::new();
for (k, &(pos, sub)) in s.deferred.iter().enumerate() {
unsafe {
init_submenu(cm3.as_ref(), cm2.as_ref(), sub, pos);
let mut children = read_menu(&s.cm, cm3.as_ref(), cm2.as_ref(), sub, 1, None);
trim_separators(&mut children);
out.push((k as u8, children));
}
}
// a submenu filled now may have loaded handlers of its own: kept loaded as well (issue 605)
crate::extpin::pin_loaded();
Ok(out)
})
}
/// Lets a submenu that fills itself when it opens (Send to, Open with…) do so.
unsafe fn init_submenu(
cm3: Option<&IContextMenu3>,
cm2: Option<&IContextMenu2>,
sub: windows::Win32::UI::WindowsAndMessaging::HMENU,
pos: u32,
) {
let (wp, lp) = (WPARAM(sub.0 as usize), LPARAM(pos as isize));
unsafe {
if let Some(c3) = cm3 {
let mut res = LRESULT(0);
let _ = c3.HandleMenuMsg2(WM_INITMENUPOPUP, wp, lp, Some(&mut res));
} else if let Some(c2) = cm2 {
let _ = c2.HandleMenuMsg(WM_INITMENUPOPUP, wp, lp);
}
}
}
fn trim_separators(items: &mut Vec<ShellItem>) {
while items.first().is_some_and(|c| c.separator) {
items.remove(0);
}
while items.last().is_some_and(|c| c.separator) {
items.pop();
}
}
fn build_ex(owner: HWND, target: &ShellTarget, extended: bool, defer: bool) -> anyhow::Result<(u64, Vec<ShellItem>)> {
let (cm, can_rename) = context_menu_for(owner, target)?;
unsafe {
let hmenu = CreatePopupMenu()?;
let mut flags = CMF_NORMAL | CMF_EXPLORE;
if can_rename {
flags |= CMF_CANRENAME;
}
if extended {
flags |= CMF_EXTENDEDVERBS;
}
if let Err(e) = cm.QueryContextMenu(hmenu, 0, 1, 0x7FFF, flags).ok() {
let _ = DestroyMenu(hmenu);
return Err(e.into());
}
let cm3: Option<IContextMenu3> = cm.cast().ok();
let cm2: Option<IContextMenu2> = cm.cast().ok();
let mut deferred = Vec::new();
let items = read_menu(&cm, cm3.as_ref(), cm2.as_ref(), hmenu, 0, defer.then_some(&mut deferred));
// the handlers are loaded now: keep them loaded (a handler unloaded under its own objects ends the process, issue 605)
crate::extpin::pin_loaded();
let id = NEXT_SESSION.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
keep_session(Session { id, cm, dir: working_dir(target), hmenu, deferred });
Ok((id, items))
}
}
/// Runs command `id` of session `session` (from [`build`]). `screen` is where the menu was
/// opened (some handlers place their UI there). Call on the shell STA thread.
pub fn invoke(owner: HWND, session: u64, id: u32, screen: (i32, i32)) -> anyhow::Result<MenuOutcome> {
let Some(s) = take_session(session) else {
anyhow::bail!("menu session expired");
};
let r = invoke_offset(owner, &s.cm, s.dir.as_deref(), id - 1, screen);
drop(s);
crate::extpin::pin_loaded();
r
}
/// Drops the live session `session` (menu closed without choosing a shell command).
pub fn release(session: u64) {
drop(take_session(session));
}
/// Invokes a canonical verb ("properties", "Windows.ModernShare", "openas"…) on `target`
/// without showing a menu. Call on the shell STA thread.
pub fn invoke_verb(owner: HWND, target: &ShellTarget, verb: &str, screen: (i32, i32)) -> anyhow::Result<()> {
let (cm, _) = context_menu_for(owner, target)?;
unsafe {
// handlers only answer verbs after QueryContextMenu
let hmenu = CreatePopupMenu()?;
let _ = cm.QueryContextMenu(hmenu, 0, 1, 0x7FFF, CMF_NORMAL | CMF_EXPLORE);
let verb_a = std::ffi::CString::new(verb).unwrap_or_default();
let verb_w = wide(verb);
let dir = working_dir(target);
let dir_a = dir.as_ref().filter(|d| d.to_string_lossy().is_ascii()).and_then(|d| std::ffi::CString::new(d.to_string_lossy().as_bytes()).ok());
let dir_w = dir.as_ref().map(|d| wide(d));
let info = CMINVOKECOMMANDINFOEX {
cbSize: std::mem::size_of::<CMINVOKECOMMANDINFOEX>() as u32,
fMask: CMIC_MASK_UNICODE | CMIC_MASK_PTINVOKE,
hwnd: owner,
lpDirectory: dir_a.as_ref().map_or(PCSTR::null(), |d| PCSTR(d.as_ptr() as *const u8)),
lpDirectoryW: dir_w.as_ref().map_or(PCWSTR::null(), |d| PCWSTR(d.as_ptr())),
lpVerb: PCSTR(verb_a.as_ptr() as *const u8),
lpVerbW: PCWSTR(verb_w.as_ptr()),
nShow: SW_SHOWNORMAL.0,
ptInvoke: POINT { x: screen.0, y: screen.1 },
..Default::default()
};
let r = cm.InvokeCommand(&info as *const _ as *const CMINVOKECOMMANDINFO);
let _ = DestroyMenu(hmenu);
crate::extpin::pin_loaded();
r.map_err(Into::into)
}
}
fn verb_of(cm: &IContextMenu, offset: usize) -> String {
let mut buf = [0u16; 256];
unsafe {
cm.GetCommandString(offset, GCS_VERBW, None, PSTR(buf.as_mut_ptr() as *mut u8), buf.len() as u32)
.ok()
.map(|_| crate::wide::from_wide(&buf))
.unwrap_or_default()
}
}
fn invoke_offset(owner: HWND, cm: &IContextMenu, dir: Option<&Path>, offset: u32, (x, y): (i32, i32)) -> anyhow::Result<MenuOutcome> {
let verb = verb_of(cm, offset as usize);
if verb.eq_ignore_ascii_case("rename") {
return Ok(MenuOutcome::Rename);
}
// (the ANSI copy only for an ASCII path: other text would reach an ANSI handler as UTF-8 bytes; Unicode handlers read the W one)
let dir_a = dir.filter(|d| d.to_string_lossy().is_ascii()).and_then(|d| std::ffi::CString::new(d.to_string_lossy().as_bytes()).ok());
let dir_w = dir.map(|d| wide(d));
let info = CMINVOKECOMMANDINFOEX {
lpDirectory: dir_a.as_ref().map_or(PCSTR::null(), |d| PCSTR(d.as_ptr() as *const u8)),
lpDirectoryW: dir_w.as_ref().map_or(PCWSTR::null(), |d| PCWSTR(d.as_ptr())),
cbSize: std::mem::size_of::<CMINVOKECOMMANDINFOEX>() as u32,
fMask: CMIC_MASK_UNICODE | CMIC_MASK_PTINVOKE,
hwnd: owner,
lpVerb: PCSTR(offset as usize as *const u8),
lpVerbW: PCWSTR(offset as usize as *const u16),
nShow: SW_SHOWNORMAL.0,
ptInvoke: POINT { x, y },
..Default::default()
};
unsafe { cm.InvokeCommand(&info as *const _ as *const CMINVOKECOMMANDINFO) }
.map(|_| MenuOutcome::Invoked(verb))
.map_err(Into::into)
}
/// Parses a Win32 menu item label such as `"&Open\tCtrl+O"` or `"반디집(&G)"` into
/// `(cleaned_label, shortcut_hint, access_key)`. `"&&"` stays a literal ampersand.
pub fn parse_label(raw: &str) -> (String, Option<String>, Option<char>) {
let (text, hint) = match raw.split_once('\t') {
Some((t, h)) => (t, Some(h.trim().to_string()).filter(|h| !h.is_empty())),
None => (raw, None),
};
let mut out = String::with_capacity(text.len());
let mut mnemonic = None;
let mut chars = text.chars().peekable();
while let Some(c) = chars.next() {
if c == '&' {
if chars.peek() == Some(&'&') {
chars.next();
out.push('&');
} else if mnemonic.is_none()
&& let Some(&next) = chars.peek()
&& next.is_alphanumeric()
{
mnemonic = next.to_lowercase().next();
}
continue;
}
out.push(c);
}
(out.trim().to_string(), hint, mnemonic)
}
/// "&Open\tCtrl+O" → ("Open", Some("Ctrl+O")); "&&" stays a literal ampersand.
pub fn clean_label(raw: &str) -> (String, Option<String>) {
let (label, hint, _) = parse_label(raw);
(label, hint)
}
unsafe fn read_menu(
cm: &IContextMenu,
cm3: Option<&IContextMenu3>,
cm2: Option<&IContextMenu2>,
hmenu: windows::Win32::UI::WindowsAndMessaging::HMENU,
depth: u32,
// (the top level only) where to leave for later the submenus the shell fills when they open: see `build_deferring`
mut defer: Option<&mut Vec<(u32, windows::Win32::UI::WindowsAndMessaging::HMENU)>>,
) -> Vec<ShellItem> {
use windows::Win32::UI::WindowsAndMessaging::{
GetMenuItemCount, GetMenuItemInfoW, MENUITEMINFOW, MFS_DISABLED, MFT_OWNERDRAW, MFT_SEPARATOR, MIIM_BITMAP,
MIIM_CHECKMARKS, MIIM_FTYPE, MIIM_ID, MIIM_STATE, MIIM_STRING, MIIM_SUBMENU,
};
use windows::core::PWSTR;
let mut out = Vec::new();
let n = unsafe { GetMenuItemCount(Some(hmenu)) };
for i in 0..n.max(0) {
let mut buf = [0u16; 512];
let mut mii = MENUITEMINFOW {
cbSize: std::mem::size_of::<MENUITEMINFOW>() as u32,
fMask: MIIM_FTYPE | MIIM_STATE | MIIM_ID | MIIM_SUBMENU | MIIM_STRING | MIIM_BITMAP | MIIM_CHECKMARKS,
dwTypeData: PWSTR(buf.as_mut_ptr()),
cch: (buf.len() - 1) as u32,
..Default::default()
};
if unsafe { GetMenuItemInfoW(hmenu, i as u32, true, &mut mii) }.is_err() {
continue;
}
if mii.fType.contains(MFT_SEPARATOR) {
out.push(ShellItem {
id: 0,
label: String::new(),
hint: None,
mnemonic: None,
verb: String::new(),
icon: None,
enabled: false,
separator: true,
children: Vec::new(),
deferred: None,
});
continue;
}
let raw = String::from_utf16_lossy(&buf[..(mii.cch as usize).min(buf.len())]);
let (label, hint, mnemonic) = parse_label(&raw);
if label.is_empty() || mii.fType.contains(MFT_OWNERDRAW) && label.is_empty() {
continue;
}
let mut children = Vec::new();
if !mii.hSubMenu.is_invalid() {
if depth >= 3 {
continue;
}
// A submenu the shell fills only when it opens holds, until then, one item that repeats its own name ("Open with",
// "Cast to device"). Filling it is what is slow: left for later (`fill_deferred`).
if let Some(list) = defer.as_deref_mut()
&& list.len() < MAX_DEFERRED
&& unsafe { is_placeholder_submenu(mii.hSubMenu, &raw) }
{
let k = list.len() as u8;
list.push((i as u32, mii.hSubMenu));
let verb = if (1..=0x7FFF).contains(&mii.wID) { verb_of(cm, (mii.wID - 1) as usize) } else { String::new() };
out.push(ShellItem {
id: 0,
label,
hint,
mnemonic,
verb,
icon: unsafe { item_icon(&mii) },
enabled: !mii.fState.contains(MFS_DISABLED),
separator: false,
children: Vec::new(),
deferred: Some(k),
});
continue;
}
// let lazily filled submenus (Send to, Open with…) populate themselves
unsafe { init_submenu(cm3, cm2, mii.hSubMenu, i as u32) };
children = unsafe { read_menu(cm, cm3, cm2, mii.hSubMenu, depth + 1, None) };
// trim separators at the ends / empty submenus
trim_separators(&mut children);
if children.is_empty() {
continue;
}
}
let id = if mii.hSubMenu.is_invalid() && (1..=0x7FFF).contains(&mii.wID) { mii.wID } else { 0 };
if id == 0 && children.is_empty() {
continue;
}
let verb = if id > 0 { verb_of(cm, (id - 1) as usize) } else { String::new() };
out.push(ShellItem {
id,
label,
hint,
mnemonic,
verb,
icon: unsafe { item_icon(&mii) },
enabled: !mii.fState.contains(MFS_DISABLED),
separator: false,
children,
deferred: None,
});
}
out
}
/// Whether `sub`, not yet opened, holds only the one item the shell puts in a submenu it fills when it opens: an item with the
/// submenu's own name (`parent_raw`, the parent item's text as the menu has it).
unsafe fn is_placeholder_submenu(sub: windows::Win32::UI::WindowsAndMessaging::HMENU, parent_raw: &str) -> bool {
use windows::Win32::UI::WindowsAndMessaging::{GetMenuItemCount, GetMenuItemInfoW, MENUITEMINFOW, MIIM_STRING};
unsafe {
if GetMenuItemCount(Some(sub)) != 1 {
return false;
}
let mut buf = [0u16; 512];
let mut mii = MENUITEMINFOW {
cbSize: std::mem::size_of::<MENUITEMINFOW>() as u32,
fMask: MIIM_STRING,
dwTypeData: windows::core::PWSTR(buf.as_mut_ptr()),
cch: (buf.len() - 1) as u32,
..Default::default()
};
if GetMenuItemInfoW(sub, 0, true, &mut mii).is_err() {
return false;
}
let text = String::from_utf16_lossy(&buf[..(mii.cch as usize).min(buf.len())]);
!text.is_empty() && text == parent_raw
}
}
/// An item's picture: its item bitmap, or else the check-mark bitmap that some handlers
/// (7-Zip, issue 397) set as their icon, which Explorer shows the same way. Standard Windows
/// check marks leave `hbmpChecked` and `hbmpUnchecked` null; a real bitmap placed there —
/// including 7-Zip's 1-bit `MenuLogo.bmp` — is an icon supplied by the handler.
unsafe fn item_icon(mii: &windows::Win32::UI::WindowsAndMessaging::MENUITEMINFOW) -> Option<ShellIcon> {
use windows::Win32::UI::WindowsAndMessaging::MFS_CHECKED;
let mark = if mii.fState.contains(MFS_CHECKED) { mii.hbmpChecked } else { mii.hbmpUnchecked };
unsafe { bitmap_bgra(mii.hbmpItem).or_else(|| bitmap_bgra(mark)) }
}
/// Reads a menu item bitmap as BGRA. Special `HBMMENU_*` values (owner-drawn callbacks,
/// system glyphs) yield `None`.
unsafe fn bitmap_bgra(hbm: windows::Win32::Graphics::Gdi::HBITMAP) -> Option<ShellIcon> {
use windows::Win32::Graphics::Gdi::{
BI_RGB, BITMAP, BITMAPINFO, BITMAPINFOHEADER, DIB_RGB_COLORS, GetDC, GetDIBits, GetObjectW, HGDIOBJ, ReleaseDC,
};
let raw = hbm.0 as isize;
if (-1..=11).contains(&raw) {
return None;
}
let mut bm = BITMAP::default();
if unsafe { GetObjectW(HGDIOBJ(hbm.0), std::mem::size_of::<BITMAP>() as i32, Some(&mut bm as *mut _ as *mut _)) } == 0 {
return None;
}
let (w, h) = (bm.bmWidth, bm.bmHeight.abs());
if bm.bmBitsPixel == 0 || w <= 0 || h <= 0 || w > 256 || h > 256 {
return None;
}
let mut bi = BITMAPINFO {
bmiHeader: BITMAPINFOHEADER {
biSize: std::mem::size_of::<BITMAPINFOHEADER>() as u32,
biWidth: w,
biHeight: -h,
biPlanes: 1,
biBitCount: 32,
biCompression: BI_RGB.0,
..Default::default()
},
..Default::default()
};
let mut buf = vec![0u8; (w * h * 4) as usize];
let hdc = unsafe { GetDC(None) };
let lines = unsafe { GetDIBits(hdc, hbm, 0, h as u32, Some(buf.as_mut_ptr() as *mut _), &mut bi, DIB_RGB_COLORS) };
unsafe {
ReleaseDC(None, hdc);
}
if lines == 0 {
return None;
}
let monochrome = bm.bmBitsPixel == 1;
if monochrome {
// Windows draws 1-bit check-mark bitmaps (7-Zip's MenuLogo.bmp) using the menu's own
// colours: 0-bits (black) in the text colour and 1-bits (white) as the background.
// Return a mask with transparent white pixels and opaque black pixels so both light and
// dark menus can tint it cleanly in their text colour (issue 397).
for p in buf.chunks_exact_mut(4) {
if p[0] > 127 {
p.copy_from_slice(&[0, 0, 0, 0]);
} else {
p.copy_from_slice(&[0, 0, 0, 255]);
}
}
} else if bm.bmBitsPixel < 32 || buf.chunks_exact(4).all(|p| p[3] == 0) {
// bitmaps without an alpha channel come back with alpha 0 everywhere
for p in buf.chunks_exact_mut(4) {
p[3] = 255;
}
}
Some(ShellIcon { width: w as u32, height: h as u32, bgra: buf, monochrome })
}
fn run_menu(
owner: HWND,
cm: &IContextMenu,
dir: Option<&Path>,
(x, y): (i32, i32),
extended: bool,
can_rename: bool,
extra: &[ExtraItem],
) -> anyhow::Result<MenuOutcome> {
unsafe {
let hmenu = CreatePopupMenu()?;
let mut flags = CMF_NORMAL | CMF_EXPLORE;
if can_rename {
flags |= CMF_CANRENAME;
}
if extended {
flags |= CMF_EXTENDEDVERBS;
}
if let Err(e) = cm.QueryContextMenu(hmenu, 0, 1, 0x7FFF, flags).ok() {
let _ = DestroyMenu(hmenu);
return Err(e.into());
}
insert_extras(hmenu, extra);
crate::extpin::pin_loaded();
let cm2: Option<IContextMenu2> = cm.cast().ok();
let cm3: Option<IContextMenu3> = cm.cast().ok();
MENU_HOOK.with(|h| {
*h.borrow_mut() = Some(Box::new(move |msg: u32, wp: WPARAM, lp: LPARAM| -> Option<LRESULT> {
match msg {
WM_INITMENUPOPUP | WM_DRAWITEM | WM_MEASUREITEM | WM_MENUCHAR => {
if let Some(c3) = &cm3 {
let mut res = LRESULT(0);
if c3.HandleMenuMsg2(msg, wp, lp, Some(&mut res)).is_ok() {
return Some(res);
}
} else if let Some(c2) = &cm2 {
if c2.HandleMenuMsg(msg, wp, lp).is_ok() {
return Some(LRESULT(0));
}
}
None
}
_ => None,
}
}));
});
let _ = SetForegroundWindow(owner);
crate::menu_theme::apply();
let cmd = TrackPopupMenuEx(hmenu, (TPM_RETURNCMD | TPM_RIGHTBUTTON).0, x, y, owner, None);
let _ = PostMessageW(Some(owner), WM_NULL, WPARAM(0), LPARAM(0));
MENU_HOOK.with(|h| *h.borrow_mut() = None);
let id = cmd.0 as u32;
let result = if id == 0 {
Ok(MenuOutcome::Dismissed)
} else if id >= EXTRA_BASE {
Ok(MenuOutcome::Custom(id - EXTRA_BASE))
} else {
invoke_offset(owner, cm, dir, id - 1, (x, y))
};
let _ = DestroyMenu(hmenu);
// (submenus filled while it was open and the command itself may have loaded more)
crate::extpin::pin_loaded();
result
}
}
/// A menu row's words as people read them: no `&` accelerator mark, no `\t` shortcut, any letter case.
pub fn plain_label(s: &str) -> String {
s.split('\t').next().unwrap_or("").replace('&', "").trim().to_lowercase()
}
/// The entries of `extra` that Windows' menu does not already have under the same words (`existing`, as [`plain_label`] gives
/// them). Issue 446: with Windows Terminal installed, its own "Open in Terminal" stood above ours, "Open in terminal".
pub fn extras_not_in(extra: &[ExtraItem], existing: &[String]) -> Vec<ExtraItem> {
extra.iter().filter(|it| !existing.contains(&plain_label(&it.label))).cloned().collect()
}
/// The words of every row of a menu, as [`plain_label`] gives them.
fn labels_of(hmenu: windows::Win32::UI::WindowsAndMessaging::HMENU) -> Vec<String> {
use windows::Win32::UI::WindowsAndMessaging::{GetMenuItemCount, GetMenuStringW, MF_BYPOSITION};
let n = unsafe { GetMenuItemCount(Some(hmenu)) }.max(0);
(0..n)
.map(|i| {
let mut buf = [0u16; 260];
// SAFETY: a live menu of this thread, asked for one row's text into a buffer of the size given
let len = unsafe { GetMenuStringW(hmenu, i as u32, Some(&mut buf), MF_BYPOSITION) }.max(0) as usize;
plain_label(&String::from_utf16_lossy(&buf[..len.min(buf.len())]))
})
.collect()
}
/// Puts our own entries into Windows' menu (see [`extras_place`] for where), less those it already has (see [`extras_not_in`]).
fn insert_extras(hmenu: windows::Win32::UI::WindowsAndMessaging::HMENU, extra: &[ExtraItem]) {
let extra = extras_not_in(extra, &labels_of(hmenu));
if extra.is_empty() {
return;
}
let (at, before, after) = extras_place(&separators_of(hmenu));
let mut i = at as u32;
// SAFETY: `hmenu` is a live menu of this thread, and every label is a zero-ended string that outlives its call
unsafe {
if before {
let _ = InsertMenuW(hmenu, i, MF_BYPOSITION | MF_SEPARATOR, 0, PCWSTR::null());
i += 1;
}
for it in &extra {
let label = crate::wide::wide(&it.label);
let mut f = MF_BYPOSITION | MF_STRING;
if !it.enabled {
f |= MF_GRAYED;
}
let _ = InsertMenuW(hmenu, i, f, (EXTRA_BASE + it.id) as usize, PCWSTR(label.as_ptr()));
i += 1;
}
if after {
let _ = InsertMenuW(hmenu, i, MF_BYPOSITION | MF_SEPARATOR, 0, PCWSTR::null());
}
}
}
/// Which rows of a menu are separators, top to bottom.
fn separators_of(hmenu: windows::Win32::UI::WindowsAndMessaging::HMENU) -> Vec<bool> {
use windows::Win32::UI::WindowsAndMessaging::{GetMenuItemCount, GetMenuItemInfoW, MENUITEMINFOW, MFT_SEPARATOR, MIIM_FTYPE};
let n = unsafe { GetMenuItemCount(Some(hmenu)) }.max(0);
(0..n)
.map(|i| {
let mut mii = MENUITEMINFOW { cbSize: std::mem::size_of::<MENUITEMINFOW>() as u32, fMask: MIIM_FTYPE, ..Default::default() };
// SAFETY: `mii` is a MENUITEMINFOW of the size it states, asked for its type only
unsafe { GetMenuItemInfoW(hmenu, i as u32, true, &mut mii) }.is_ok() && mii.fType.contains(MFT_SEPARATOR)
})
.collect()
}
/// Where our own entries go in Windows' menu: the row they start at, and whether a separator goes before and after them.
///
/// WHY (issue 310): they were put at the very top, so the commands of the file's own type (Install, Mount, Open, Extract All: the
/// menu's first block, the ones people look for first) came second, below Copy path, Compress and Tag. They follow that block now.
fn extras_place(separators: &[bool]) -> (usize, bool, bool) {
match separators.iter().position(|s| *s) {
// (a menu that starts with a separator, or an empty one, has no first block to keep above them)
Some(0) => (0, false, true),
None if separators.is_empty() => (0, false, false),
// below the first block: a separator of our own, and the menu's own one closes them
Some(first) => (first, true, false),
None => (separators.len(), true, false),
}
}
/// A plain popup menu of app items at `screen` (no shell items). Returns the chosen id.
/// Call on the shell STA thread with its owner window.
pub fn show_simple(owner: HWND, screen: (i32, i32), items: &[ExtraItem]) -> Option<u32> {
unsafe {
let hmenu = CreatePopupMenu().ok()?;
for (i, it) in items.iter().enumerate() {
if it.label == "-" {
let _ = InsertMenuW(hmenu, i as u32, MF_BYPOSITION | MF_SEPARATOR, 0, PCWSTR::null());
continue;
}
let label = crate::wide::wide(&it.label);
let mut f = MF_BYPOSITION | MF_STRING;
if !it.enabled {
f |= MF_GRAYED;
}
let _ = InsertMenuW(hmenu, i as u32, f, (EXTRA_BASE + it.id) as usize, PCWSTR(label.as_ptr()));
}
let _ = SetForegroundWindow(owner);
crate::menu_theme::apply();
let cmd = TrackPopupMenuEx(hmenu, (TPM_RETURNCMD | TPM_RIGHTBUTTON).0, screen.0, screen.1, owner, None);
let _ = PostMessageW(Some(owner), WM_NULL, WPARAM(0), LPARAM(0));
let _ = DestroyMenu(hmenu);
let id = cmd.0 as u32;
(id >= EXTRA_BASE).then(|| id - EXTRA_BASE)
}
}
#[cfg(test)]
mod tests {
use super::{ExtraItem, extras_not_in, plain_label};
/// Issue 446: Windows Terminal's "Open in &Terminal" is already in the menu, so ours ("Open in terminal") is not added again;
/// the other rows are.
#[test]
fn a_row_windows_already_has_is_not_added_twice() {
let row = |id, label: &str| ExtraItem { id, label: label.into(), enabled: true };
let ours = [row(1, "Open in terminal"), row(2, "Copy path Ctrl+Shift+C")];
let windows = vec![plain_label("Open in &Terminal"), plain_label("Properties")];
let kept: Vec<u32> = extras_not_in(&ours, &windows).iter().map(|e| e.id).collect();
assert_eq!(kept, vec![2]);
assert_eq!(plain_label("Yolu kopyala Ctrl+Shift+C"), "yolu kopyala");
}
use super::*;
#[test]
fn labels() {
assert_eq!(clean_label("&Open"), ("Open".to_string(), None));
assert_eq!(clean_label("Save && Close\tCtrl+S"), ("Save & Close".to_string(), Some("Ctrl+S".to_string())));
assert_eq!(clean_label(" Gönder "), ("Gönder".to_string(), None));
assert_eq!(parse_label("반디집(&G)"), ("반디집(G)".to_string(), None, Some('g')));
assert_eq!(parse_label("반디뷰로 보기(&1)"), ("반디뷰로 보기(1)".to_string(), None, Some('1')));
assert_eq!(parse_label("새 폴더.7z로 압축(&7)"), ("새 폴더.7z로 압축(7)".to_string(), None, Some('7')));
assert_eq!(parse_label("Save && &Close\tCtrl+S"), ("Save & Close".to_string(), Some("Ctrl+S".to_string()), Some('c')));
assert_eq!(parse_label("Save && Close"), ("Save & Close".to_string(), None, None));
assert_eq!(parse_label("Rock & Roll"), ("Rock Roll".to_string(), None, None));
assert_eq!(parse_label("Trailing&"), ("Trailing".to_string(), None, None));
}
#[test]
fn our_entries_follow_the_first_block_of_windows_menu() {
// Install, Repair, Uninstall | Open with... | Properties: ours start at the first separator, with one of their own before
assert_eq!(extras_place(&[false, false, false, true, false, true, false]), (3, true, false));
// one block only: they close the menu
assert_eq!(extras_place(&[false, false]), (2, true, false));
// nothing above them to keep: at the top, as before
assert_eq!(extras_place(&[true, false, false]), (0, false, true));
assert_eq!(extras_place(&[]), (0, false, false));
}
#[test]
fn our_entries_land_below_the_first_block_of_a_real_menu() {
// a menu shaped like an installer package's: Install, Repair, Uninstall | Open with | Properties
let rows = |hmenu| -> Vec<String> {
use windows::Win32::UI::WindowsAndMessaging::{GetMenuStringW, MF_BYPOSITION};
let separators = separators_of(hmenu);
(0..separators.len())
.map(|i| {
let mut buf = [0u16; 64];
let n = unsafe { GetMenuStringW(hmenu, i as u32, Some(&mut buf), MF_BYPOSITION) }.max(0) as usize;
if separators[i] { "-".to_string() } else { String::from_utf16_lossy(&buf[..n]) }
})
.collect()
};
unsafe {
let hmenu = CreatePopupMenu().unwrap();
for (i, label) in ["Install", "Repair", "Uninstall", "", "Open with", "", "Properties"].iter().enumerate() {
let w = crate::wide::wide(label);
let f = if label.is_empty() { MF_BYPOSITION | MF_SEPARATOR } else { MF_BYPOSITION | MF_STRING };
InsertMenuW(hmenu, i as u32, f, i + 1, PCWSTR(w.as_ptr())).unwrap();
}
let extra = [ExtraItem { id: 1, label: "Copy path".into(), enabled: true }, ExtraItem { id: 2, label: "Tag...".into(), enabled: true }];
insert_extras(hmenu, &extra);
assert_eq!(rows(hmenu), ["Install", "Repair", "Uninstall", "-", "Copy path", "Tag...", "-", "Open with", "-", "Properties"]);
let _ = DestroyMenu(hmenu);
}
}
#[test]
fn an_icon_set_as_the_check_mark_bitmap_is_read() {
use windows::Win32::Graphics::Gdi::{CreateBitmap, DeleteObject, HBITMAP, HGDIOBJ};
use windows::Win32::UI::WindowsAndMessaging::{
GetMenuItemInfoW, InsertMenuItemW, MENUITEMINFOW, MFS_CHECKED, MIIM_BITMAP, MIIM_CHECKMARKS, MIIM_ID, MIIM_STATE, MIIM_STRING,
};
unsafe {
// 1) a 16x16 colour picture in hbmpUnchecked
let px = vec![0x80u8; 16 * 16 * 4];
let colour = CreateBitmap(16, 16, 1, 32, Some(px.as_ptr() as *const _));
// 2) a 13x13 1-bit monochrome bitmap, as 7-Zip loads MenuLogo.bmp (issue 397), with alternating black/white bits
let mono_bits: Vec<u16> = (0..13).map(|row| if row % 2 == 0 { 0xAAAA } else { 0x5555 }).collect();
let mono = CreateBitmap(13, 13, 1, 1, Some(mono_bits.as_ptr() as *const _));
let hmenu = CreatePopupMenu().unwrap();
let insert = |pos: u32, checked: bool, unchecked_bm: HBITMAP, checked_bm: HBITMAP| {
let mut label = crate::wide::wide("7-Zip");
let mii = MENUITEMINFOW {
cbSize: std::mem::size_of::<MENUITEMINFOW>() as u32,
fMask: MIIM_ID | MIIM_STRING | MIIM_STATE | MIIM_CHECKMARKS,
fState: if checked { MFS_CHECKED } else { Default::default() },
wID: pos + 1,
dwTypeData: windows::core::PWSTR(label.as_mut_ptr()),
hbmpUnchecked: unchecked_bm,
hbmpChecked: checked_bm,
..Default::default()
};
InsertMenuItemW(hmenu, pos, true, &mii).unwrap();
};
insert(0, false, colour, HBITMAP::default());
insert(1, false, mono, HBITMAP::default());
// 3) a standard Windows checked item leaves both checkmark handles null -> no icon
insert(2, true, HBITMAP::default(), HBITMAP::default());
// 4) a checked item with an explicit checked bitmap uses hbmpChecked
insert(3, true, HBITMAP::default(), colour);
let read = |i: u32| {
let mut mii = MENUITEMINFOW {
cbSize: std::mem::size_of::<MENUITEMINFOW>() as u32,
fMask: MIIM_STATE | MIIM_BITMAP | MIIM_CHECKMARKS,
..Default::default()
};
GetMenuItemInfoW(hmenu, i, true, &mut mii).unwrap();
item_icon(&mii)
};
let icon = read(0).expect("the colour picture is the icon");
assert_eq!((icon.width, icon.height), (16, 16));
assert!(!icon.monochrome);
let zip_icon = read(1).expect("7-Zip's 1-bit MenuLogo bitmap is read as an icon");
assert_eq!((zip_icon.width, zip_icon.height), (13, 13));
assert!(zip_icon.monochrome, "1-bit bitmap is flagged as monochrome mask");
// Row 0 is 0xAAAA (MSB-first 1,0,1,0...): white (1-bit) pixels are transparent, black (0-bit) pixels are opaque
assert_eq!(&zip_icon.bgra[0..4], &[0, 0, 0, 0], "1-bit white pixel is transparent");
assert_eq!(&zip_icon.bgra[4..8], &[0, 0, 0, 255], "1-bit black pixel is opaque");
assert!(zip_icon.bgra.chunks_exact(4).all(|p| p == [0, 0, 0, 0] || p == [0, 0, 0, 255]));
assert!(read(2).is_none(), "a standard Windows check mark has null handles and yields no icon");
let checked_icon = read(3).expect("checked item reads hbmpChecked");
assert_eq!((checked_icon.width, checked_icon.height), (16, 16));
assert!(!checked_icon.monochrome);
let _ = DestroyMenu(hmenu);
let _ = DeleteObject(HGDIOBJ(colour.0));
let _ = DeleteObject(HGDIOBJ(mono.0));
}
}
fn dump(items: &[ShellItem], depth: usize) {
for i in items {
if i.separator {
println!("{}----", " ".repeat(depth));
continue;
}
println!(
"{}[{}] {:?} verb={:?} icon={} enabled={}",
" ".repeat(depth),
i.id,
i.label,
i.verb,
i.icon.as_ref().map(|c| format!("{}x{}", c.width, c.height)).unwrap_or_default(),
i.enabled
);
dump(&i.children, depth + 1);
}
}
/// Manual: `RF_MENU_DUMP=<path> cargo test -p rf-win dump_shell_menu -- --ignored --nocapture`
#[test]
#[ignore]
fn dump_shell_menu() {
let p = PathBuf::from(std::env::var("RF_MENU_DUMP").unwrap_or_else(|_| std::env::var("USERPROFILE").unwrap()));
let (tx, rx) = std::sync::mpsc::channel();
crate::sta::run(move |owner| {
let t0 = std::time::Instant::now();
let item = build(owner, &ShellTarget::Items(vec![p.clone()]), false).map(|(_, v)| v);
let t1 = t0.elapsed();
let bg = build(owner, &ShellTarget::Background(p.parent().unwrap().to_path_buf()), false).map(|(_, v)| v);
let _ = tx.send((item.map_err(|e| e.to_string()), t1, bg.map_err(|e| e.to_string()), t0.elapsed()));
});
let (item, t1, bg, t2) = rx.recv_timeout(std::time::Duration::from_secs(30)).unwrap();
println!("== item menu ({t1:?})");
dump(&item.unwrap(), 0);
println!("== background menu ({t2:?} total)");
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) };
// (each side in its full long form: the temp folder can come as an 8.3 short path, `RUNNER~1`, on one side and long on the other)
let canon = |p: &std::path::Path| std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf()).to_string_lossy().to_lowercase();
let (got, want): (Vec<String>, Vec<String>) = (got.iter().map(|p| canon(p)).collect(), vec![canon(&a), canon(&b)]);
let _ = std::fs::remove_dir_all(&root);
assert_eq!(got, want);
}
}