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.

Open/Save: the foreground goes back to the owner's top-level window (Firefox-based browsers)

gelenmedyaa621e9bd2ebe
diff --git a/crates/rf-app/src/picker.rs b/crates/rf-app/src/picker.rs
index 220109f3..dcf802b5 100644
--- a/crates/rf-app/src/picker.rs
+++ b/crates/rf-app/src/picker.rs
@@ -60,7 +60,8 @@ impl PickSession {
fn answer(&self, resp: Response) {
if let Some(mut f) = self.pipe.borrow_mut().take() {
if self.req.owner != 0 {
- rf_win::window::hand_foreground(self.req.owner);
+ let (top, taken) = rf_win::window::hand_foreground(self.req.owner);
+ dialog_log(&format!("app picker: the foreground goes back to {top:#x} (owner {:#x}), taken: {taken}", self.req.owner));
}
if let Err(e) = write_msg(&mut f, &resp) {
log::warn!("picker answer: {e}");
diff --git a/crates/rf-win/src/window.rs b/crates/rf-win/src/window.rs
index 9c9e17cf..ea12e24c 100644
--- a/crates/rf-win/src/window.rs
+++ b/crates/rf-win/src/window.rs
@@ -254,10 +254,16 @@ pub fn enable(hwnd: isize) {
/// The owner is enabled first. The asking app disabled it for the dialog and enables it again only once our answer reaches it,
/// which can be after our window is gone: a disabled owner can't take the foreground, so Windows gave it to another app and the
/// browser fell behind it (Firefox, whose dialogs run in a helper process that can't take the foreground back itself).
-pub fn hand_foreground(owner: isize) {
+///
+/// The foreground goes to the owner's top-level window. WHY (a Discord report, 2026-12-04, Zen browser: "Cancel download file
+/// minimized the browser"; its log named a different owner window for every dialog, never the one in front): a Firefox-based
+/// browser gives a dialog a small hidden CHILD window of its own as the owner, made for that one dialog and destroyed after it.
+/// A child window cannot be the foreground window, so the browser did not get the foreground back and the window behind it came
+/// to the front when ours closed. Returns the window the foreground was handed to and whether Windows took it, for the log.
+pub fn hand_foreground(owner: isize) -> (isize, bool) {
use windows::Win32::Foundation::HWND;
use windows::Win32::UI::Input::KeyboardAndMouse::EnableWindow;
- use windows::Win32::UI::WindowsAndMessaging::{AllowSetForegroundWindow, GetWindowThreadProcessId, SetForegroundWindow};
+ use windows::Win32::UI::WindowsAndMessaging::{AllowSetForegroundWindow, GA_ROOT, GetAncestor, GetWindowThreadProcessId, SetForegroundWindow};
unsafe {
let h = HWND(owner as *mut _);
let mut pid = 0u32;
@@ -266,7 +272,12 @@ pub fn hand_foreground(owner: isize) {
let _ = AllowSetForegroundWindow(pid);
}
let _ = EnableWindow(h, true);
- let _ = SetForegroundWindow(h);
+ let root = GetAncestor(h, GA_ROOT);
+ let top = if root.is_invalid() { h } else { root };
+ if top != h {
+ let _ = EnableWindow(top, true);
+ }
+ (top.0 as isize, SetForegroundWindow(top).as_bool())
}
}