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.

Drags: the held buttons are written into the drag thread's key state, a drop with a button down is refused, and the log says where a drag was lost (1067, 1072)

gelenmedya15d032cbefc3
diff --git a/Cargo.lock b/Cargo.lock
index 0dfd0f38..272448b5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3288,7 +3288,7 @@ dependencies = [
[[package]]
name = "gpui-pre-windows"
version = "0.3.11"
-source = "git+https://github.com/gelenmedya/gpui-pre-windows?rev=31ae1ba61fa2b18453fc79f47d0f6ac4dc14e423#31ae1ba61fa2b18453fc79f47d0f6ac4dc14e423"
+source = "git+https://github.com/gelenmedya/gpui-pre-windows?rev=3386e2b5a2c5a85763387885d1ac9756989dc672#3386e2b5a2c5a85763387885d1ac9756989dc672"
dependencies = [
"accesskit",
"accesskit_windows",
diff --git a/Cargo.toml b/Cargo.toml
index a29b00b7..48e78451 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -203,4 +203,4 @@ incremental = true
# Velopack with one change: its downloads trust the Windows certificate store (issue 121), see vendor/velopack/VENDORED.md
velopack = { path = "vendor/velopack" }
# our fork of GPUI's Windows layer (GPU/CPU hot-swap renderer), see its FORK.md
-gpui-pre-windows = { git = "https://github.com/gelenmedya/gpui-pre-windows", rev = "31ae1ba61fa2b18453fc79f47d0f6ac4dc14e423" }
+gpui-pre-windows = { git = "https://github.com/gelenmedya/gpui-pre-windows", rev = "3386e2b5a2c5a85763387885d1ac9756989dc672" }
diff --git a/crates/rf-app/src/folder_view.rs b/crates/rf-app/src/folder_view.rs
index 8cee6891..53ed9c9c 100644
--- a/crates/rf-app/src/folder_view.rs
+++ b/crates/rf-app/src/folder_view.rs
@@ -4805,6 +4805,10 @@ impl FolderView {
fn on_drag_motion(&mut self, ev: &MouseMoveEvent, window: &mut Window, cx: &mut Context<Self>) {
let Some(press) = &self.drag_origin else { return };
if ev.pressed_button != Some(MouseButton::Left) {
+ // (issue 1067, a drag that does not start: a press given up here while the button is really down is one way to lose it)
+ if rf_win::keys::left_button_down() {
+ log::warn!("drag: a press on an item was given up: a mouse move came without the left button ({:?}) while the button is down", ev.pressed_button);
+ }
self.drag_origin = None;
return;
}
diff --git a/crates/rf-app/src/ops.rs b/crates/rf-app/src/ops.rs
index daf07f65..c2444b61 100644
--- a/crates/rf-app/src/ops.rs
+++ b/crates/rf-app/src/ops.rs
@@ -1439,6 +1439,7 @@ pub(crate) fn drag_paths_out(items: Vec<PathBuf>) {
unsafe {
let _ = windows::Win32::UI::Input::KeyboardAndMouse::ReleaseCapture();
}
+ rf_win::dnd::drag_asked();
rf_win::sta::run_drag(move |owner| {
if let Err(e) = rf_win::dnd::drag_out(owner, ui_thread, &items) {
log::warn!("drag: {e:#}");
diff --git a/crates/rf-win/src/dnd.rs b/crates/rf-win/src/dnd.rs
index f4d63579..829574e0 100644
--- a/crates/rf-win/src/dnd.rs
+++ b/crates/rf-win/src/dnd.rs
@@ -120,18 +120,30 @@ fn copy_only_at(sources: &[PathBuf], now: Instant) -> bool {
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);
}
@@ -140,6 +152,59 @@ fn drag_out_allowing(owner: HWND, ui_thread: u32, items: &[PathBuf], allowed: DR
}
}
+/// 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. NOT tried with a real mouse here: the log
+/// lines around the drag say whether a drag is still ended at once.
+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;
+ if held != was {
+ log::warn!("drag: the key state of the drag's thread had key {vk:#04x} {} while it is {}", 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).
diff --git a/crates/rf-win/src/keys.rs b/crates/rf-win/src/keys.rs
index a630c304..2b41dad4 100644
--- a/crates/rf-win/src/keys.rs
+++ b/crates/rf-win/src/keys.rs
@@ -4,6 +4,12 @@
use windows::Win32::UI::Input::KeyboardAndMouse::{MAPVK_VK_TO_VSC, MapVirtualKeyW, ToUnicode, VK_SHIFT};
+/// Whether the left mouse button is down right now (the button itself, not what a message says).
+pub fn left_button_down() -> bool {
+ use windows::Win32::UI::Input::KeyboardAndMouse::{GetAsyncKeyState, VK_LBUTTON};
+ (unsafe { GetAsyncKeyState(VK_LBUTTON.0 as i32) } as u16 & 0x8000) != 0
+}
+
/// Character the current keyboard layout produces for `vk` with Shift held, if printable.
pub fn shifted_char(vk: u16) -> Option<String> {
let scan = unsafe { MapVirtualKeyW(vk as u32, MAPVK_VK_TO_VSC) };