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.

22.4 KB
Raw Blame
//! Drag files out of rustyfiles (to Explorer, the desktop, other apps — or back into
//! rustyfiles itself) with the standard shell data object and drag image.

use std::path::{Path, PathBuf};

use std::sync::atomic::{AtomicU32, Ordering};

use windows::Win32::Foundation::{HWND, LPARAM, TRUE};
use windows::core::BOOL;
use windows::Win32::System::Com::{CoTaskMemFree, IDataObject};
use windows::Win32::System::Ole::{DROPEFFECT, DROPEFFECT_COPY, DROPEFFECT_LINK, DROPEFFECT_MOVE};
use windows::Win32::System::Threading::{AttachThreadInput, GetCurrentProcessId, GetCurrentThreadId};
use windows::Win32::UI::WindowsAndMessaging::{EnumWindows, GetClassNameW, GetWindowThreadProcessId, IsWindowVisible, SW_HIDE, ShowWindow};
use windows::Win32::UI::Input::KeyboardAndMouse::{GetAsyncKeyState, VK_LBUTTON, VK_RBUTTON};
use windows::Win32::UI::Shell::Common::ITEMIDLIST;
use windows::Win32::UI::Shell::{ILFindLastID, SHCreateDataObject, SHDoDragDrop, SHParseDisplayName};
use windows::core::PCWSTR;

use std::sync::Mutex;
use std::time::{Duration, Instant};

use crate::fileops::Transfer;

pub(crate) struct Pidl(pub(crate) *mut ITEMIDLIST);
impl Drop for Pidl {
    fn drop(&mut self) {
        if !self.0.is_null() {
            unsafe { CoTaskMemFree(Some(self.0 as *const _)) }
        }
    }
}

pub(crate) fn parse(path: &Path) -> anyhow::Result<Pidl> {
    let w = crate::wide::shell_name(path);
    let mut pidl: *mut ITEMIDLIST = std::ptr::null_mut();
    unsafe { SHParseDisplayName(PCWSTR(w.as_ptr()), None, &mut pidl, 0, None)? };
    Ok(Pidl(pidl))
}

/// Shell data object for items that share one parent folder.
///
/// The folder is bound once and each item is parsed by its name inside it. Parsing each full path from the desktop down
/// (`SHParseDisplayName`) took 1.5 ms an item: 30 s for 20,000 files (issue 674: 38,000 pictures froze the window for seconds and
/// the drag never began, the button was let go before it was ready). Where an item can't be parsed by its name (a shell namespace
/// that only knows full paths), its full path is parsed as before.
pub fn data_object(items: &[PathBuf]) -> anyhow::Result<IDataObject> {
    use windows::Win32::UI::Shell::{IShellFolder, SHGetDesktopFolder};
    anyhow::ensure!(!items.is_empty(), "no items");
    let parent = items[0].parent().ok_or_else(|| anyhow::anyhow!("no parent"))?;
    let folder = parse(parent)?;
    // Same-folder items only (child pidls are relative to `folder`).
    let items: Vec<&PathBuf> = items.iter().filter(|p| p.parent() == Some(parent)).collect();
    let shell_folder: Option<IShellFolder> = unsafe { SHGetDesktopFolder().and_then(|d| d.BindToObject(folder.0, None)).ok() };
    // (each kept whole: relative ones are the child itself, full ones give their last part)
    let mut kept: Vec<(Pidl, bool)> = Vec::with_capacity(items.len());
    for p in &items {
        let by_name = shell_folder.as_ref().zip(p.file_name()).and_then(|(sf, name)| {
            let w: Vec<u16> = name.to_string_lossy().encode_utf16().chain(Some(0)).collect();
            let mut child: *mut ITEMIDLIST = std::ptr::null_mut();
            unsafe { sf.ParseDisplayName(HWND::default(), None, PCWSTR(w.as_ptr()), None, &mut child, std::ptr::null_mut()) }.ok()?;
            (!child.is_null()).then_some(Pidl(child))
        });
        kept.push(match by_name {
            Some(c) => (c, true),
            None => (parse(p)?, false),
        });
    }
    let children: Vec<*const ITEMIDLIST> = kept.iter().map(|(p, relative)| if *relative { p.0 as *const _ } else { unsafe { ILFindLastID(p.0) as *const _ } }).collect();
    Ok(unsafe { SHCreateDataObject(Some(folder.0), Some(&children), None)? })
}

/// Runs a modal OLE drag on the calling thread (must be the drag thread, see `sta::run_drag`).
/// `ui_thread` is the id of the thread that owns the window where the drag started; its input
/// state is attached so this thread can take mouse capture while the button is still down.
pub fn drag_out(owner: HWND, ui_thread: u32, items: &[PathBuf]) -> anyhow::Result<DROPEFFECT> {
    drag_out_allowing(owner, ui_thread, items, DROPEFFECT_COPY | DROPEFFECT_MOVE | DROPEFFECT_LINK)
}

/// [`drag_out`] for files that must not be MOVED by the target: a copy or a link only (the Start panel's recommended files and the
/// copies of its apps' shortcuts). The target still decides between the two by its own rules.
pub fn drag_out_copy_only(owner: HWND, ui_thread: u32, items: &[PathBuf]) -> anyhow::Result<DROPEFFECT> {
    // The allowed-effects mask binds the OLE targets of other programs, but our own windows take a drop as a plain list of paths and ask
    // `default_transfer`, which moves on the same drive. So the paths are also remembered here, and `default_transfer` copies them.
    mark_copy_only(items, DRAG_LIMIT);
    let r = drag_out_allowing(owner, ui_thread, items, DROPEFFECT_COPY | DROPEFFECT_LINK);
    // (the drop is handled a moment after the loop ends: the paths stay marked for that, then they are let go)
    mark_copy_only(items, AFTER_DRAG);
    r
}

/// How long a copy-only drag may last before its paths stop being marked (a drag that never returns must not mark them for good).
const DRAG_LIMIT: Duration = Duration::from_secs(600);
/// How long the paths stay marked after the drag's loop has ended.
const AFTER_DRAG: Duration = Duration::from_secs(5);

/// Paths of drags that must never be moved, each with the time its mark ends.
static COPY_ONLY: Mutex<Vec<(String, Instant)>> = Mutex::new(Vec::new());

fn key(p: &Path) -> String {
    p.to_string_lossy().to_lowercase()
}

/// Marks `items` as the subject of a copy-only drag for `for_how_long` (replacing an earlier mark of the same path).
pub fn mark_copy_only(items: &[PathBuf], for_how_long: Duration) {
    let now = Instant::now();
    let Ok(mut marks) = COPY_ONLY.lock() else { return };
    marks.retain(|(_, until)| *until > now);
    for p in items {
        let k = key(p);
        marks.retain(|(m, _)| *m != k);
        marks.push((k, now + for_how_long));
    }
}

/// Whether every one of `sources` belongs to a copy-only drag that is going on or has just ended at `now`.
fn copy_only_at(sources: &[PathBuf], now: Instant) -> bool {
    let Ok(marks) = COPY_ONLY.lock() else { return false };
    !sources.is_empty() && sources.iter().all(|s| marks.iter().any(|(m, until)| *until > now && *m == key(s)))
}

fn drag_out_allowing(owner: HWND, ui_thread: u32, items: &[PathBuf], allowed: DROPEFFECT) -> anyhow::Result<DROPEFFECT> {
    let data = data_object(items)?;
    let waited = asked_ms_ago();
    unsafe {
        // The button may already be up (very short drags) — don't start a drag then.
        let down = |vk: i32| (GetAsyncKeyState(vk) as u16 & 0x8000) != 0;
        if !down(VK_LBUTTON.0 as i32) && !down(VK_RBUTTON.0 as i32) {
            log::warn!("drag: not begun, the button was up again {} after the drag was asked for", ms(waited));
            return Ok(DROPEFFECT(0));
        }
        if waited.is_some_and(|w| w >= 200) {
            log::warn!("drag: begins {} after it was asked for", ms(waited));
        }
        let me = GetCurrentThreadId();
        let attached = ui_thread != 0 && AttachThreadInput(me, ui_thread, true).as_bool();
        buttons_into_key_state();
        let serial = DRAGS.fetch_add(1, Ordering::SeqCst) + 1;
        DRAGGING.store(true, Ordering::SeqCst);
        let began = Instant::now();
        let r = SHDoDragDrop(Some(owner), &data, None, allowed);
        DRAGGING.store(false, Ordering::SeqCst);
        // (a drag that Windows ends at once never showed: it took the button for up, or could not take the mouse)
        let took = began.elapsed().as_millis();
        if took < 60 {
            log::warn!("drag: Windows ended it after {took} ms ({r:?}); the thread's input was {}", if attached { "joined to the window's" } else { "NOT joined to the window's" });
        }
        if attached {
            let _ = AttachThreadInput(me, ui_thread, false);
        }
        put_away_drag_images(serial);
        Ok(r?)
    }
}

/// Writes the mouse buttons and Ctrl, Shift and Alt, as they are held right now, into this thread's key state.
///
/// WHY. Windows' drag loop, and the shell's drag source that tells it when to drop, read the buttons from the key state of the
/// calling thread's input queue (`GetKeyState`), not from the buttons themselves. Our drags run on a thread of their own, joined to
/// the window's input just before (`AttachThreadInput`), and Windows' documentation says of that call: "key state, which can be
/// ascertained by calls to the GetKeyState or GetKeyboardState function, is reset after a call to AttachThreadInput". A loop that
/// finds no button down drops at once, where the drag began, which looks like a drag that never started (issue 1067: "roughly 1
/// out of every 5 attempts"). So what is really held is written back after the join. Tried with a real mouse on 2026-12-04: the
/// log line below came at the start of each of 26 drags (the left button read "up" while it was held), and none of those drags
/// was lost or ended at once.
/// Whether the line about a wrong key state was written in this run already.
static SAID_KEY_STATE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

fn buttons_into_key_state() {
    use windows::Win32::UI::Input::KeyboardAndMouse::{GetKeyboardState, SetKeyboardState, VK_CONTROL, VK_MENU, VK_SHIFT};
    let mut state = [0u8; 256];
    unsafe {
        if GetKeyboardState(&mut state).is_err() {
            return;
        }
        for vk in [VK_LBUTTON.0, VK_RBUTTON.0, VK_CONTROL.0, VK_SHIFT.0, VK_MENU.0] {
            let held = (GetAsyncKeyState(vk as i32) as u16 & 0x8000) != 0;
            let was = state[vk as usize] & 0x80 != 0;
            // (said once a run: on the first PC it was tried on with a real mouse, 2026-12-04, the left button read "up" at the
            // start of every one of 26 drags, so this is the rule after the join, not an accident worth a line each time)
            if held != was && !SAID_KEY_STATE.swap(true, Ordering::Relaxed) {
                log::warn!("drag: the key state of the drag's thread had key {vk:#04x} {} while it is {} (written back; said once a run)", if was { "down" } else { "up" }, if held { "down" } else { "up" });
            }
            state[vk as usize] = (state[vk as usize] & !0x80) | if held { 0x80 } else { 0 };
        }
        let _ = SetKeyboardState(&state);
    }
}

/// When the window last asked for a drag (`drag_asked`), for the log of a drag that did not begin.
static ASKED: Mutex<Option<Instant>> = Mutex::new(None);

/// Called on the window's thread at the moment a press has become a drag, before the drag is handed to the drag thread.
///
/// WHY (issue 1067: "starting a drag intermittently fails, roughly 1 out of every 5 attempts", still there after three changes that
/// were each made by reasoning: a thread of its own for drags, pointer messages kept while a task runs, the first move followed
/// outside the list). Nothing said where a drag was lost. Now each way it can be lost between here and Windows' own drag loop
/// writes one line to the log: the drag thread still busy with the drag before, the button up again when its turn came, a late
/// start, a drag Windows ended at once.
pub fn drag_asked() {
    if DRAGGING.load(Ordering::SeqCst) {
        log::warn!("drag: asked for while the drag before it has not ended: it waits for that one");
    }
    *ASKED.lock().unwrap_or_else(|p| p.into_inner()) = Some(Instant::now());
}

fn asked_ms_ago() -> Option<u128> {
    ASKED.lock().unwrap_or_else(|p| p.into_inner()).take().map(|t| t.elapsed().as_millis())
}

fn ms(waited: Option<u128>) -> String {
    waited.map_or_else(|| "an unknown time".to_string(), |w| format!("{w} ms"))
}

/// The drags started so far (each one's number lets a late clean-up see that a newer drag has begun meanwhile).
static DRAGS: AtomicU32 = AtomicU32::new(0);
/// A drag of ours is running now (its picture belongs on the screen).
static DRAGGING: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Whether a drag of ours is running now (a drop on our own window then comes from us).
pub fn our_drag_running() -> bool {
    DRAGGING.load(Ordering::SeqCst)
}

/// Hides a drag picture left on the screen when no drag of ours runs: the app calls it on a click in its windows and when one comes to
/// the front. Issue 80: switching windows with Alt+Tab in the middle of a drag (into a browser) could leave the picture after every
/// clean-up at the drag's end, until rustyfiles was restarted; now the next click or the next time rustyfiles is in front puts it away.
pub fn sweep_drag_images() {
    if !DRAGGING.load(Ordering::SeqCst) {
        let n = hide_drag_images();
        if n > 0 {
            log::info!("dnd: hid {n} drag picture(s) left on the screen after a drag");
        }
    }
}

/// The picture that follows the pointer in a drag is a window of the shell's (class `SysDragImage`), made in the process that started
/// the drag; the drop target under the pointer is the one that hides it when the drag leaves it or ends. When a target never does
/// (a drag cancelled with Esc over some programs), the picture stays on the screen: here it stayed until rustyfiles was closed, since the
/// thread that owns it lives as long as the program (a Discord report, 2026-11-22: a folder's picture "floats in and is irremovable").
/// So once our drag has ended, any such window of this process still on the screen is hidden: now, and again a moment later (the shell
/// may still be finishing its own animation), unless another drag has begun by then.
fn put_away_drag_images(serial: u32) {
    hide_drag_images();
    // (again a moment later, and later still: after a switch of windows mid-drag the shell may show the picture again once the drop
    // has been handled, issue 80)
    std::thread::spawn(move || {
        for wait in [600u64, 1400, 3000] {
            std::thread::sleep(Duration::from_millis(wait));
            if DRAGS.load(Ordering::SeqCst) != serial || DRAGGING.load(Ordering::SeqCst) {
                return;
            }
            let n = hide_drag_images();
            if n > 0 {
                log::info!("dnd: hid {n} drag picture(s) still on the screen {wait} ms into the clean-up");
            }
        }
    });
}

/// Hides the visible drag pictures (`SysDragImage` windows) of this process; returns how many it hid.
pub fn hide_drag_images() -> usize {
    unsafe extern "system" fn each(hwnd: HWND, lp: LPARAM) -> BOOL {
        unsafe {
            let mut pid = 0u32;
            GetWindowThreadProcessId(hwnd, Some(&mut pid));
            if pid == GetCurrentProcessId() && IsWindowVisible(hwnd).as_bool() {
                let mut buf = [0u16; 32];
                let n = GetClassNameW(hwnd, &mut buf) as usize;
                if String::from_utf16_lossy(&buf[..n]) == "SysDragImage" {
                    let _ = ShowWindow(hwnd, SW_HIDE);
                    *(lp.0 as *mut usize) += 1;
                }
            }
        }
        TRUE
    }
    let mut hidden = 0usize;
    unsafe {
        let _ = EnumWindows(Some(each), LPARAM(&mut hidden as *mut usize as isize));
    }
    hidden
}

/// Explorer's default for a plain drag: same volume → move, otherwise copy.
/// Ctrl forces copy, Shift forces move.
/// What a drag without Ctrl or Shift does (Settings › Files and folders; issue 466: "Choose if a mouse file drag does a move or a copy").
/// 0: as Windows does (a move on the same drive, else a copy), 1: always a move, 2: always a copy. Ctrl and Shift still decide.
static PLAIN_DRAG: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);

/// Sets [`PLAIN_DRAG`] (from the settings).
pub fn set_plain_drag(kind: u8) {
    PLAIN_DRAG.store(kind.min(2), std::sync::atomic::Ordering::Relaxed);
}

pub fn default_transfer(sources: &[PathBuf], dest_dir: &Path, ctrl: bool, shift: bool) -> Transfer {
    transfer_with(sources, dest_dir, ctrl, shift, PLAIN_DRAG.load(std::sync::atomic::Ordering::Relaxed))
}

/// [`default_transfer`] with the plain-drag setting given (`plain`, see [`PLAIN_DRAG`]).
fn transfer_with(sources: &[PathBuf], dest_dir: &Path, ctrl: bool, shift: bool, plain: u8) -> Transfer {
    // (a drag that was started copy-only, the Start panel's recommended files, is copied by our own windows as well: not even Shift moves it)
    if copy_only_at(sources, Instant::now()) {
        return Transfer::Copy;
    }
    if ctrl {
        return Transfer::Copy;
    }
    if shift {
        return Transfer::Move;
    }
    match plain {
        1 => return Transfer::Move,
        2 => return Transfer::Copy,
        _ => {}
    }
    let vol = |p: &Path| p.components().next().map(|c| c.as_os_str().to_string_lossy().to_lowercase());
    match sources.first() {
        Some(s) if vol(s) == vol(dest_dir) => Transfer::Move,
        _ => Transfer::Copy,
    }
}

/// True when dropping `sources` into `dest_dir` makes no sense (onto itself or its own child, or a move into the folder they are in).
pub fn is_invalid_drop(sources: &[PathBuf], dest_dir: &Path) -> bool {
    is_invalid_drop_as(sources, dest_dir, Transfer::Move)
}

/// [`is_invalid_drop`] for a drop that is known to be a `kind`: a COPY into the folder the items are in makes "x - Copy" beside them,
/// as in Explorer (issue 219: Ctrl and a drop in the same folder did nothing).
pub fn is_invalid_drop_as(sources: &[PathBuf], dest_dir: &Path, kind: Transfer) -> bool {
    sources.iter().any(|s| dest_dir == s || dest_dir.starts_with(s))
        || (kind == Transfer::Move && sources.iter().all(|s| s.parent() == Some(dest_dir)))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn transfer_rules() {
        let src = vec![PathBuf::from(r"C:\a\x.txt")];
        assert_eq!(default_transfer(&src, Path::new(r"C:\b"), false, false), Transfer::Move);
        assert_eq!(default_transfer(&src, Path::new(r"D:\b"), false, false), Transfer::Copy);
        assert_eq!(default_transfer(&src, Path::new(r"D:\b"), false, true), Transfer::Move);
        assert_eq!(default_transfer(&src, Path::new(r"C:\b"), true, false), Transfer::Copy);
        // the setting for a plain drag (issue 466); Ctrl and Shift still win
        assert_eq!(transfer_with(&src, Path::new(r"C:"), false, false, 2), Transfer::Copy);
        assert_eq!(transfer_with(&src, Path::new(r"C:"), false, true, 2), Transfer::Move);
        assert_eq!(transfer_with(&src, Path::new(r"D:"), false, false, 1), Transfer::Move);
        assert_eq!(transfer_with(&src, Path::new(r"D:"), true, false, 1), Transfer::Copy);
        assert_eq!(transfer_with(&src, Path::new(r"D:"), false, false, 0), Transfer::Copy);
        assert!(is_invalid_drop(&src, Path::new(r"C:\a")));
        assert!(is_invalid_drop(&[PathBuf::from(r"C:\a")], Path::new(r"C:\a\sub")));
        assert!(!is_invalid_drop(&src, Path::new(r"C:\b")));
        // a copy into the items' own folder is a duplicate; a folder still cannot go into itself or its own child
        assert!(!is_invalid_drop_as(&src, Path::new(r"C:\a"), Transfer::Copy));
        assert!(is_invalid_drop_as(&[PathBuf::from(r"C:\a")], Path::new(r"C:\a"), Transfer::Copy));
        assert!(is_invalid_drop_as(&[PathBuf::from(r"C:\a")], Path::new(r"C:\a\sub"), Transfer::Copy));
    }


    #[test]
    fn a_file_dragged_copy_only_is_copied_by_our_own_windows_even_on_the_same_drive() {
        let mine = PathBuf::from("C:/rf-copy-only-test/mine.txt");
        let other = PathBuf::from("C:/rf-copy-only-test/other.txt");
        let one = |p: &PathBuf, shift: bool| default_transfer(std::slice::from_ref(p), Path::new("C:/rf-copy-only-dest"), false, shift);
        // (before the fix a same-volume drop was a Move whatever the drag allowed)
        assert_eq!(one(&mine, false), Transfer::Move);
        mark_copy_only(std::slice::from_ref(&mine), Duration::from_secs(60));
        assert_eq!(one(&mine, false), Transfer::Copy);
        assert_eq!(one(&mine, true), Transfer::Copy, "Shift does not move it");
        assert_eq!(default_transfer(&[PathBuf::from("c:/RF-COPY-ONLY-TEST/MINE.TXT")], Path::new("C:/rf-copy-only-dest"), false, false), Transfer::Copy, "case does not matter");
        // another file, or a list that is not all marked, keeps the ordinary rule
        assert_eq!(one(&other, false), Transfer::Move);
        assert_eq!(default_transfer(&[mine.clone(), other], Path::new("C:/rf-copy-only-dest"), false, false), Transfer::Move);
        // the mark ends
        assert!(copy_only_at(std::slice::from_ref(&mine), Instant::now()));
        assert!(!copy_only_at(std::slice::from_ref(&mine), Instant::now() + Duration::from_secs(61)));
        mark_copy_only(std::slice::from_ref(&mine), Duration::ZERO);
        assert_eq!(one(&mine, false), Transfer::Move);
    }

    #[test]
    fn a_drag_picture_left_on_the_screen_is_hidden_and_other_windows_are_not() {
        use windows::Win32::System::LibraryLoader::GetModuleHandleW;
        use windows::Win32::UI::WindowsAndMessaging::*;
        use windows::core::{PCWSTR, w};
        unsafe extern "system" fn proc_(h: HWND, m: u32, wp: windows::Win32::Foundation::WPARAM, lp: LPARAM) -> windows::Win32::Foundation::LRESULT {
            unsafe { DefWindowProcW(h, m, wp, lp) }
        }
        unsafe {
            let hinst = GetModuleHandleW(None).unwrap();
            let make = |class: PCWSTR| {
                let wc = WNDCLASSW { lpfnWndProc: Some(proc_), hInstance: hinst.into(), lpszClassName: class, ..Default::default() };
                RegisterClassW(&wc);
                // (1 px, far off every screen, never activated)
                let h = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE, class, w!(""), WS_POPUP, -32000, -32000, 1, 1, None, None, Some(hinst.into()), None).unwrap();
                let _ = ShowWindow(h, SW_SHOWNOACTIVATE);
                h
            };
            let picture = make(w!("SysDragImage"));
            let other = make(w!("rustyfiles.NotADragImage"));
            assert!(IsWindowVisible(picture).as_bool());
            assert!(hide_drag_images() >= 1);
            assert!(!IsWindowVisible(picture).as_bool(), "the drag picture is hidden");
            assert!(IsWindowVisible(other).as_bool(), "another window is left alone");
            assert_eq!(hide_drag_images(), 0, "nothing more to hide");
            let _ = DestroyWindow(picture);
            let _ = DestroyWindow(other);
        }
    }

    #[test]
    fn builds_data_object() {
        let d = std::env::temp_dir().join(format!("rf-dnd-{}", std::process::id()));
        std::fs::create_dir_all(&d).unwrap();
        std::fs::write(d.join("a.txt"), b"x").unwrap();
        crate::shell::ensure_com();
        assert!(data_object(&[d.join("a.txt")]).is_ok());
        std::fs::remove_dir_all(&d).unwrap();
    }
}