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.

Dock bands are registered with Explorer directly, on every monitor, while the taskbar auto-hides

gelenmedyae0c75d9ff06d
diff --git a/crates/rf-win/src/dock/system.rs b/crates/rf-win/src/dock/system.rs
index 57a2d769..fc139fb6 100644
--- a/crates/rf-win/src/dock/system.rs
+++ b/crates/rf-win/src/dock/system.rs
@@ -462,6 +462,146 @@ fn taskbar_created() -> u32 {
*ID.get_or_init(|| unsafe { windows::Win32::UI::WindowsAndMessaging::RegisterWindowMessageW(windows::core::w!("TaskbarCreated")) })
}
+// ---------------------------------------------------------------- the toolbar messages, straight to Explorer
+//
+// WHY NOT `SHAppBarMessage`. It sends to the FIRST window of class `Shell_TrayWnd`, and while the tray host runs that window is ours,
+// in this very process; the host passes every message on to Explorer. For the messages that ask for an answer (`ABM_QUERYPOS`,
+// `ABM_SETPOS`) that did not work from this process: measured on 2026-12-04, they came back at once with the rectangle that was sent and
+// Explorer changed nothing (a band registered above the visible taskbar stayed there when the taskbar hid itself: 48 px of nothing above
+// the dock; a changed thickness was never taken). The same calls from another program through the same host worked, and so did the
+// dock's own when a relay of ANOTHER process stood in front. Why this process's are lost was not found (shell32 allocates the answer's
+// memory in the process that owns the window it found, which here is the sender itself). So the dock does not go through its own
+// window: it builds the request as shell32 does (`tray_wire::appbar`) and sends it to Explorer's own taskbar window, with a time limit
+// (shell32 waits for ever, and this thread must be able to end).
+
+thread_local! {
+ /// Explorer's own taskbar window and its process, as last found (looked up again when the window is no longer that process's).
+ static EXPLORER_TRAY: std::cell::Cell<Option<(isize, u32)>> = const { std::cell::Cell::new(None) };
+}
+
+/// How long one toolbar message may take. Explorer answers in a millisecond or two; one that hangs must not hold the dock's thread.
+const BAR_SEND_MS: u32 = 4000;
+
+fn explorer_tray() -> Option<(isize, u32)> {
+ use windows::Win32::Foundation::HWND;
+ use windows::Win32::UI::WindowsAndMessaging::GetWindowThreadProcessId;
+ if let Some((hwnd, pid)) = EXPLORER_TRAY.with(std::cell::Cell::get) {
+ let mut now = 0u32;
+ // SAFETY: reads the owner of a window handle; a handle that is gone gives 0.
+ if unsafe { GetWindowThreadProcessId(HWND(hwnd as *mut _), Some(&mut now)) } != 0 && now == pid {
+ return Some((hwnd, pid));
+ }
+ }
+ let found = explorer_tray_id().map(|t| (t.hwnd, t.pid));
+ EXPLORER_TRAY.with(|c| c.set(found));
+ found
+}
+
+/// Whether Explorer on this Windows answers a request built here (0: not asked yet, 1: yes, 2: no). The layout of the request is read
+/// from Windows 11; a Windows that does not understand it must not lose the dock's band, so the first use asks a question that
+/// changes nothing (where the taskbar is) and looks at the answer.
+static DIRECT: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
+
+/// A taskbar rectangle that can be one: not empty.
+fn sane_taskbar_rect(rc: &windows::Win32::Foundation::RECT) -> bool {
+ rc.right > rc.left && rc.bottom > rc.top
+}
+
+fn direct_works(tray: isize, pid: u32) -> bool {
+ match DIRECT.load(Ordering::Relaxed) {
+ 1 => return true,
+ 2 => return false,
+ _ => {}
+ }
+ let mut d = windows::Win32::UI::Shell::APPBARDATA { cbSize: size_of::<windows::Win32::UI::Shell::APPBARDATA>() as u32, ..Default::default() };
+ // SAFETY: a read-only question; `d` is ours.
+ let answer = unsafe { send_to_explorer(tray, pid, windows::Win32::UI::Shell::ABM_GETTASKBARPOS, &mut d) };
+ let ok = answer != 0 && sane_taskbar_rect(&d.rc);
+ if !ok {
+ log::warn!("dock: Explorer did not answer a toolbar message sent to it directly on this Windows; shell32's own call is used");
+ }
+ DIRECT.store(if ok { 1 } else { 2 }, Ordering::Relaxed);
+ ok
+}
+
+/// One toolbar message for the window in `d`; `d` takes the answer of the messages that have one. The return value is Explorer's
+/// (0: refused, not delivered in time, or no answer could be read). Sent to Explorer's own taskbar window when there is one and this
+/// Windows answers such a request ([`direct_works`]); otherwise the call is shell32's own, as before.
+unsafe fn bar_message(msg: u32, d: &mut windows::Win32::UI::Shell::APPBARDATA) -> usize {
+ match explorer_tray() {
+ Some((tray, pid)) if direct_works(tray, pid) => unsafe { send_to_explorer(tray, pid, msg, d) },
+ _ => unsafe { windows::Win32::UI::Shell::SHAppBarMessage(msg, d) },
+ }
+}
+
+/// The request as shell32 builds it, to the taskbar window `tray` of process `pid`.
+unsafe fn send_to_explorer(tray: isize, pid: u32, msg: u32, d: &mut windows::Win32::UI::Shell::APPBARDATA) -> usize {
+ use crate::tray_wire::appbar::{ABD_LEN, AbMessage, AppBarLayout, AppBarRequest, reply};
+ use windows::Win32::Foundation::{HWND, LPARAM, WPARAM};
+ use windows::Win32::System::DataExchange::COPYDATASTRUCT;
+ use windows::Win32::UI::Shell::{ABM_GETTASKBARPOS, ABM_QUERYPOS, ABM_SETPOS, SHAllocShared, SHFreeShared, SHLockShared, SHUnlockShared};
+ use windows::Win32::UI::WindowsAndMessaging::{SMTO_ABORTIFHUNG, SendMessageTimeoutW, WM_COPYDATA};
+ let mut req = AppBarRequest {
+ layout: AppBarLayout::V3,
+ abd_cb_size: ABD_LEN as u32,
+ hwnd: d.hWnd.0 as usize as u32,
+ callback_message: d.uCallbackMessage,
+ edge: d.uEdge,
+ rect: crate::tray_wire::Rect { left: d.rc.left, top: d.rc.top, right: d.rc.right, bottom: d.rc.bottom },
+ lparam: d.lParam.0 as i64,
+ message: AbMessage::from_u32(msg),
+ shared_memory: 0,
+ // (the process that owns the tray window, as shell32 writes it: the memory for the answer is allocated there)
+ source_pid: pid,
+ trailing_len: 0,
+ };
+ let wants_answer = matches!(msg, ABM_QUERYPOS | ABM_SETPOS | ABM_GETTASKBARPOS);
+ let shared = if wants_answer {
+ let abd = req.encode();
+ // SAFETY: `abd` holds at least ABD_LEN bytes; the function copies them into a block of Explorer's.
+ let h = unsafe { SHAllocShared(Some(abd.as_ptr() as *const _), ABD_LEN as u32, pid) };
+ if h.is_invalid() {
+ return 0;
+ }
+ req.shared_memory = h.0 as isize as i64;
+ Some(h)
+ } else {
+ None
+ };
+ let block = req.encode();
+ let cds = COPYDATASTRUCT { dwData: crate::tray_wire::DW_APPBAR, cbData: block.len() as u32, lpData: block.as_ptr() as *mut _ };
+ let mut answer = 0usize;
+ // SAFETY: `cds` and `block` live until the call returns; the system copies them for the receiver.
+ let sent = unsafe { SendMessageTimeoutW(HWND(tray as *mut _), WM_COPYDATA, WPARAM(d.hWnd.0 as usize), LPARAM(&cds as *const _ as isize), SMTO_ABORTIFHUNG, BAR_SEND_MS, Some(&mut answer)) };
+ if let Some(h) = shared {
+ if sent.0 != 0 {
+ // SAFETY: the block was made above with ABD_LEN bytes; it is read while locked and given back.
+ unsafe {
+ let p = SHLockShared(h, pid);
+ if p.is_null() {
+ answer = 0;
+ } else {
+ if let Some((edge, rc)) = reply(std::slice::from_raw_parts(p as *const u8, ABD_LEN)) {
+ d.uEdge = edge;
+ d.rc = windows::Win32::Foundation::RECT { left: rc.left, top: rc.top, right: rc.right, bottom: rc.bottom };
+ }
+ let _ = SHUnlockShared(p);
+ }
+ }
+ }
+ // SAFETY: frees the block made above (also when Explorer did not answer: it cannot write into it later).
+ unsafe {
+ let _ = SHFreeShared(h, pid);
+ }
+ }
+ if sent.0 == 0 {
+ log::warn!("dock: Explorer did not take a toolbar message ({}) in {BAR_SEND_MS} ms", AbMessage::from_u32(msg).name());
+ EXPLORER_TRAY.with(|c| c.set(None));
+ return 0;
+ }
+ answer
+}
+
/// The toolbar message block of `SHAppBarMessage`, for this window.
fn bar_data(hwnd: windows::Win32::Foundation::HWND) -> windows::Win32::UI::Shell::APPBARDATA {
windows::Win32::UI::Shell::APPBARDATA { cbSize: size_of::<windows::Win32::UI::Shell::APPBARDATA>() as u32, hWnd: hwnd, uCallbackMessage: WM_BAR, ..Default::default() }
@@ -497,7 +637,7 @@ impl RealBar {
impl AppBarCalls for RealBar {
fn register(&mut self) -> bool {
let mut d = bar_data(self.hwnd);
- if unsafe { windows::Win32::UI::Shell::SHAppBarMessage(windows::Win32::UI::Shell::ABM_NEW, &mut d) } == 0 {
+ if unsafe { bar_message(windows::Win32::UI::Shell::ABM_NEW, &mut d) } == 0 {
return false;
}
self.note(true);
@@ -511,8 +651,7 @@ impl AppBarCalls for RealBar {
DockEdge::Right => windows::Win32::UI::Shell::ABE_RIGHT,
};
d.rc = windows::Win32::Foundation::RECT { left: edge.left, top: edge.top, right: edge.right, bottom: edge.bottom };
- let answer = unsafe { windows::Win32::UI::Shell::SHAppBarMessage(windows::Win32::UI::Shell::ABM_QUERYPOS, &mut d) };
- log::debug!("dock: band window {:#x} asked for {edge:?}, Explorer said {:?} (answer {answer})", self.hwnd.0 as isize, rect_of(d.rc));
+ unsafe { bar_message(windows::Win32::UI::Shell::ABM_QUERYPOS, &mut d) };
rect_of(d.rc)
}
@@ -523,13 +662,12 @@ impl AppBarCalls for RealBar {
DockEdge::Right => windows::Win32::UI::Shell::ABE_RIGHT,
};
d.rc = windows::Win32::Foundation::RECT { left: rc.left, top: rc.top, right: rc.right, bottom: rc.bottom };
- let answer = unsafe { windows::Win32::UI::Shell::SHAppBarMessage(windows::Win32::UI::Shell::ABM_SETPOS, &mut d) };
- log::debug!("dock: band window {:#x} set to {rc:?}, Explorer made it {:?} (answer {answer})", self.hwnd.0 as isize, rect_of(d.rc));
+ unsafe { bar_message(windows::Win32::UI::Shell::ABM_SETPOS, &mut d) };
}
fn remove(&mut self) {
let mut d = bar_data(self.hwnd);
- unsafe { windows::Win32::UI::Shell::SHAppBarMessage(windows::Win32::UI::Shell::ABM_REMOVE, &mut d) };
+ unsafe { bar_message(windows::Win32::UI::Shell::ABM_REMOVE, &mut d) };
self.note(false);
}
}
@@ -958,6 +1096,15 @@ mod tests {
assert!(!appbar_held());
}
+ #[test]
+ fn only_a_rectangle_that_is_one_counts_as_the_taskbars_answer() {
+ use windows::Win32::Foundation::RECT;
+ assert!(sane_taskbar_rect(&RECT { left: 0, top: 1392, right: 2560, bottom: 1440 }));
+ assert!(sane_taskbar_rect(&RECT { left: -1440, top: 2311, right: 0, bottom: 2359 }), "a monitor to the left");
+ assert!(!sane_taskbar_rect(&RECT::default()), "what comes back when nothing was written");
+ assert!(!sane_taskbar_rect(&RECT { left: 0, top: 1440, right: 2560, bottom: 1440 }));
+ }
+
#[test]
fn the_bands_on_other_monitors_come_for_wanted_monitors_that_are_there_and_go_for_the_rest() {
let n = |s: &str| s.to_string();
diff --git a/crates/rf-win/src/tray_wire/appbar.rs b/crates/rf-win/src/tray_wire/appbar.rs
index 0f739077..83c758a6 100644
--- a/crates/rf-win/src/tray_wire/appbar.rs
+++ b/crates/rf-win/src/tray_wire/appbar.rs
@@ -1,8 +1,8 @@
//! `dwData = 0`: `SHAppBarMessage`. shell32 wraps the `APPBARDATA` the program passed in an enhanced block that also carries the
//! message number and a shared-memory handle, so the taskbar can write results back (a `WM_COPYDATA` is one way).
//!
-//! Two layouts are known; which one 26H2 sends is **UNVERIFIED** (the `tray_spy` example settles it). A parse picks by the length of
-//! the block (`cbData`):
+//! Two layouts are known. Windows 11 (build 26300, read with the `tray_spy` example on 2026-12-04) sends V3: 0x40 bytes, the
+//! shared-memory handle as 64 bits at 0x30, the process id at 0x38. A parse picks by the length of the block (`cbData`):
//!
//! ```text
//! APPBARDATAV2 (both layouts) V3 (ManagedShell, 0x40 bytes) Vista (Chappell, 0x38 bytes)
@@ -14,6 +14,12 @@
//! 0x20 8 lParam (i64)
//! ```
//!
+//! WHOSE the shared memory is (read from the same spy log, and tried): shell32 allocates the block for the answer with
+//! `SHAllocShared(.., pid)` IN THE PROCESS THAT OWNS THE TRAY WINDOW IT FOUND and writes that process's id into `dwSourceProcessId`;
+//! the taskbar opens it with `SHLockShared(handle, that id)`. The id is therefore the tray window's process, not the sender's (a
+//! request the dock sent while a spy of another process was the first tray window carried the spy's id). The block that is shared is
+//! the first [`ABD_LEN`] bytes (the `APPBARDATA`), and the taskbar writes the edge and the rectangle into it ([`reply`]).
+//!
//! Unsure, marked in the fields below: the padding words, whether the shared-memory handle is 32 or 64 bits wide in the block, the
//! meaning of `lParam` per message (the `APPBARDATA.lParam` of `ABM_SETAUTOHIDEBAR`, `ABM_SETSTATE`, `ABM_WINDOWPOSCHANGED`), and the
//! shape of the replies (the taskbar writes them into the shared memory, and `ABM_GETTASKBARPOS` fills the rect). A relay forwards the
@@ -146,8 +152,8 @@ pub struct AppBarRequest {
/// `APPBARDATA.lParam`. Meaning per message UNSURE (see the module docs).
pub lparam: i64,
pub message: AbMessage,
- /// The sender's shared-memory handle for the reply, sign-extended when the layout holds only 32 bits. Valid only in the sender's
- /// process (open it with `SHLockShared(handle, source_pid)`).
+ /// The shared-memory handle for the reply, sign-extended when the layout holds only 32 bits. Valid in the process `source_pid`
+ /// names, which is the tray window's own (open it with `SHLockShared(handle, source_pid)`; see the module docs).
pub shared_memory: i64,
pub source_pid: u32,
/// Bytes after the layout's end (opaque).
@@ -231,6 +237,15 @@ impl AppBarRequest {
}
}
+/// What the taskbar wrote into the shared `APPBARDATA` of a request that asks for an answer (`ABM_QUERYPOS`, `ABM_SETPOS`,
+/// `ABM_GETTASKBARPOS`): the edge and the rectangle. `None`: fewer than [`ABD_LEN`] bytes.
+pub fn reply(abd: &[u8]) -> Option<(u32, Rect)> {
+ if abd.len() < ABD_LEN {
+ return None;
+ }
+ Some((u32_at(abd, 0x0C)?, Rect { left: i32_at(abd, 0x10)?, top: i32_at(abd, 0x14)?, right: i32_at(abd, 0x18)?, bottom: i32_at(abd, 0x1C)? }))
+}
+
#[cfg(test)]
pub(crate) mod tests_support {
use super::super::notify::tests_support::put32;
@@ -402,4 +417,32 @@ mod tests {
assert_eq!(r.trailing_len, 500_000);
assert_eq!(r.message, AbMessage::SetPos);
}
+
+ /// A request as shell32 really sent it on Windows 11 build 26300 (the spy's log of 2026-12-04, message 162): the dock's band on a
+ /// second monitor asking for the bottom edge, while a spy of process 7540 was the first tray window.
+ const SEEN: [u8; 0x40] = [
+ 0x28, 0x00, 0x00, 0x00, 0x84, 0x04, 0x59, 0x00, 0x01, 0x80, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, //
+ 0x00, 0x0a, 0x00, 0x00, 0x96, 0xfd, 0xff, 0xff, 0xa0, 0x0f, 0x00, 0x00, 0x96, 0x07, 0x00, 0x00, //
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
+ 0xda, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x1d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ ];
+
+ #[test]
+ fn a_request_seen_on_windows_11_reads_as_v3_and_is_built_again_byte_for_byte() {
+ let r = AppBarRequest::parse(&SEEN).unwrap();
+ assert_eq!(r.layout, AppBarLayout::V3);
+ assert_eq!((r.abd_cb_size, r.hwnd, r.callback_message, r.edge), (0x28, 0x0059_0484, 0x8001, 3));
+ assert_eq!(r.rect, Rect { left: 2560, top: -618, right: 4000, bottom: 1942 });
+ assert_eq!((r.message, r.shared_memory, r.source_pid), (AbMessage::QueryPos, 0xc0da, 7540));
+ assert_eq!(r.encode(), SEEN, "what the dock sends itself is what shell32 sends");
+ }
+
+ #[test]
+ fn the_answer_is_the_edge_and_the_rectangle_of_the_shared_block() {
+ // (the taskbar's answer to a position query on this PC: the bottom edge, the taskbar's strip)
+ let mut abd = SEEN[..ABD_LEN].to_vec();
+ abd[0x10..0x20].copy_from_slice(&[0, 0, 0, 0, 0x70, 0x05, 0, 0, 0x00, 0x0a, 0, 0, 0xa0, 0x05, 0, 0]);
+ assert_eq!(reply(&abd), Some((3, Rect { left: 0, top: 1392, right: 2560, bottom: 1440 })));
+ assert_eq!(reply(&abd[..ABD_LEN - 1]), None);
+ }
}