Skip to content

misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux

Windows: Download File Info and the progress window open in front of the browser (part of #208) (#301)

Ahmed Nefzaoui946334db5b71
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e51a4550..bb95add9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -688,6 +688,14 @@ choose. And Linux, checked on a real desktop with every change.
resizes, snaps and maximizes as before, with a thin strip of its title bar's colour above it.
Moving them by their title bar, their taskbar button, Alt+Tab and the window menu (Alt+Space,
or a right-click on the title bar) work as before. Windows 10 keeps the frames it had.
+- **Windows: Download File Info and the progress window open in front of the browser**
+ ([#208](https://github.com/anefzaoui/misterdownloadmanager/issues/208)): a download handed over
+ by the browser could open its window behind the browser, and stay there. Windows lets a
+ program in the background take the foreground only with the browser's say-so, which the user's
+ next click or key ends, and a window refused it opens under the program in front. Every dialog
+ and progress window of MDM that opens without the foreground now goes above the other
+ programs' windows, without taking the keyboard from the one being typed in: a click on it does,
+ as with Download complete.
- **A window closed with its title bar's × no longer turns black first**
([#207](https://github.com/anefzaoui/misterdownloadmanager/issues/207)): every dialog and the
progress window turned black for a moment when closed with the × or Alt+F4, rather than with
diff --git a/ROADMAP.md b/ROADMAP.md
index dea8d495..acb74a30 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -102,6 +102,9 @@ Everything below ships together, in version 0.5.0.
- ✅ More file types taken from the browser by default: DMG, PKG, DEB, RPM, APPIMAGE, MSIX, APPX,
CAB, JAR, XZ, ZST, WEBM, FLAC, OPUS and others; a list never edited gains them
([#208](https://github.com/anefzaoui/misterdownloadmanager/issues/208))
+- ✅ Windows: Download File Info and the progress window open in front of the browser, even when
+ Windows doesn't let MDM take the foreground; the keyboard stays where it was until a click
+ ([#208](https://github.com/anefzaoui/misterdownloadmanager/issues/208))
- ✅ The window no longer freezes on Linux when the system's sleep service (logind) or keyring
(Secret Service) is slow or doesn't answer: keeping the computer awake for a woken queue is
asked in the background, a keyring that doesn't answer at start is waited for once instead of
diff --git a/crates/app/src/dialogs.rs b/crates/app/src/dialogs.rs
index 672a0cf9..935837a9 100644
--- a/crates/app/src/dialogs.rs
+++ b/crates/app/src/dialogs.rs
@@ -124,10 +124,12 @@ pub fn keep_theme(
) {
let theme_key = key.with("theme");
let shown_key = key.with("shown");
+ let front_key = key.with("front");
if !open {
ctx.data_mut(|d| {
d.remove::<bool>(theme_key);
d.remove::<bool>(shown_key);
+ d.remove::<u32>(front_key);
});
return;
}
@@ -142,10 +144,40 @@ pub fn keep_theme(
ctx.request_repaint();
}
// The theme went out in an earlier frame: show the window now.
- if sent.is_some() && !ctx.data(|d| d.get_temp::<bool>(shown_key).unwrap_or(false)) {
+ let shown = ctx.data(|d| d.get_temp::<bool>(shown_key).unwrap_or(false));
+ if sent.is_some() && !shown {
ctx.send_viewport_cmd_to(vid, egui::ViewportCommand::Visible(true));
ctx.data_mut(|d| d.insert_temp(shown_key, true));
+ // The next frame sees where it went (below).
+ ctx.request_repaint();
+ }
+ // Shown (in an earlier frame), once: in front of the other programs' windows, where Windows
+ // didn't give it the foreground (#208: Download File Info behind the browser that asked for
+ // it). Not in off-screen test runs, whose windows stay out of the way. Elsewhere the window
+ // manager places a new window and decides on its focus; an app has no say there.
+ // Looked for in a few frames at most (its title may change meanwhile: a progress window's).
+ #[cfg(windows)]
+ {
+ const TRIES: u32 = 30;
+ let tries = ctx.data(|d| d.get_temp::<u32>(front_key).unwrap_or(0));
+ if shown && !crate::offscreen() && tries < TRIES {
+ let tries = match crate::win32::come_forward(title) {
+ Some(lifted) => {
+ if lifted {
+ tracing::info!("window {title:?} shown behind another program's: lifted");
+ }
+ TRIES
+ }
+ None => {
+ ctx.request_repaint();
+ tries + 1
+ }
+ };
+ ctx.data_mut(|d| d.insert_temp(front_key, tries));
+ }
}
+ #[cfg(not(windows))]
+ let _ = front_key;
// Belt and braces: a no-op when the caption is already right.
#[cfg(windows)]
crate::win32::caption_dark(title, dark);
diff --git a/crates/app/src/win32.rs b/crates/app/src/win32.rs
index ed73f7aa..7ccb3bae 100644
--- a/crates/app/src/win32.rs
+++ b/crates/app/src/win32.rs
@@ -141,6 +141,66 @@ pub fn keep_from_foreground() {
}
}
+/// Our window titled `title`, just shown (dialogs::keep_theme), in front of the other programs'
+/// windows if Windows didn't make it the foreground window (#208). Windows gives a program the
+/// foreground only when it may take it (the user's click is in it, or the browser's native host
+/// passed its right on, `AllowSetForegroundWindow`, which the user's next input elsewhere ends).
+/// Refused, the window is placed under the program in front, just above our own windows, and
+/// stays there: `HWND_TOP` and `BringWindowToTop` don't move it either. So a window opened at the
+/// browser's request (Download File Info, a progress window) could open behind the browser. A
+/// round trip through topmost puts it above every window that isn't topmost, as Download
+/// complete is (#77), without taking the keyboard from the program being typed in (a click on
+/// it does). None while it isn't shown yet (ask again in the next frame); else whether it was
+/// lifted.
+pub fn come_forward(title: &str) -> Option<bool> {
+ use windows_sys::Win32::UI::WindowsAndMessaging::{
+ GetForegroundWindow, IsIconic, IsWindowVisible, WS_EX_TOPMOST,
+ };
+ let hwnd = find_own_window(title)?;
+ // SAFETY: plain queries about our own window.
+ let (visible, foreground, topmost, minimized) = unsafe {
+ (
+ IsWindowVisible(hwnd) != 0,
+ GetForegroundWindow() == hwnd,
+ GetWindowLongPtrW(hwnd, GWL_EXSTYLE) as u32 & WS_EX_TOPMOST != 0,
+ IsIconic(hwnd) != 0,
+ )
+ };
+ if !visible {
+ return None;
+ }
+ let lift = lifted_when_shown(foreground, topmost, minimized);
+ if lift {
+ // SAFETY: our own window, on its thread.
+ unsafe { above_others(hwnd) };
+ }
+ Some(lift)
+}
+
+/// Whether a window just shown goes above the others ([`come_forward`]): it isn't the foreground
+/// window, isn't minimized, and isn't topmost already (a question, a notice: lifting it would
+/// end its topmost).
+fn lifted_when_shown(foreground: bool, topmost: bool, minimized: bool) -> bool {
+ !foreground && !topmost && !minimized
+}
+
+/// Window `hwnd` above every window that isn't topmost, not activated, nothing else changed (its
+/// size, place, frame and styles; its owner stays where it is).
+///
+/// # Safety
+/// `hwnd` is a window of this thread.
+unsafe fn above_others(hwnd: HWND) {
+ use windows_sys::Win32::UI::WindowsAndMessaging::{
+ HWND_NOTOPMOST, HWND_TOPMOST, SWP_NOOWNERZORDER, SWP_NOSIZE,
+ };
+ let flags = SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOOWNERZORDER;
+ // SAFETY: (the caller's) our own window; z-order changes only.
+ unsafe {
+ SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, flags);
+ SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, flags);
+ }
+}
+
/// Give our window titled `title` IDM's modal-dialog frame (WS_EX_DLGMODALFRAME, no sizing
/// frame: 3 px borders, 26 px caption) and resize it so the client stays `client` points, at
/// `scale` pixels per point (the display's scaling times any zoom), with the frame measured for
@@ -1304,6 +1364,105 @@ mod caption_less_tests {
}
}
+#[cfg(test)]
+mod come_forward_tests {
+ use super::*;
+ use windows_sys::Win32::UI::WindowsAndMessaging::{
+ CreateWindowExW, DefWindowProcW, DestroyWindow, GW_HWNDNEXT, GetClientRect, GetTopWindow,
+ GetWindow, GetWindowRect, RegisterClassW, WNDCLASSW, WS_EX_TOPMOST,
+ };
+
+ /// Lifted only when it didn't get the foreground, and never a topmost one (a question, a
+ /// notice) or a minimized one.
+ #[test]
+ fn only_a_window_left_behind_is_lifted() {
+ assert!(lifted_when_shown(false, false, false));
+ assert!(!lifted_when_shown(true, false, false));
+ assert!(!lifted_when_shown(false, true, false));
+ assert!(!lifted_when_shown(false, false, true));
+ }
+
+ /// Where `hwnd` is in the z-order of the top-level windows (0: the top).
+ fn z(hwnd: HWND) -> usize {
+ // SAFETY: plain queries walking the desktop's top-level windows.
+ unsafe {
+ let mut w = GetTopWindow(std::ptr::null_mut());
+ let mut i = 0;
+ while !w.is_null() && w != hwnd {
+ w = GetWindow(w, GW_HWNDNEXT);
+ i += 1;
+ }
+ assert!(!w.is_null(), "not a top-level window");
+ i
+ }
+ }
+
+ /// Two real (hidden, off-screen) windows with winit's styles for the progress window, one
+ /// framed by caption_less as on Windows 11: each in turn goes above the other, neither stays
+ /// topmost, and nothing else of it changes (styles, place, size, client: the frame #208's
+ /// first part made stays as it was).
+ #[test]
+ fn above_others_lifts_a_real_window_and_changes_nothing_else() {
+ const WINIT_PROGRESS: u32 = 0x04CE_0000;
+ const WINIT_EX: u32 = 0x0004_0100;
+ let class: Vec<u16> = "mdm-come-forward-test\0".encode_utf16().collect();
+ // SAFETY: a class registered with the system's own procedure, and hidden windows of it on
+ // this thread, destroyed below; the strings are NUL-terminated; plain queries, and
+ // z-order changes of our own windows.
+ unsafe {
+ let mut wc: WNDCLASSW = zeroed();
+ wc.lpfnWndProc = Some(DefWindowProcW);
+ wc.lpszClassName = class.as_ptr();
+ RegisterClassW(&wc);
+ let make = |n: u32| {
+ let title: Vec<u16> = format!("mdm come-forward test {n} {}", std::process::id())
+ .encode_utf16()
+ .chain(Some(0))
+ .collect();
+ let h = CreateWindowExW(
+ WINIT_EX,
+ class.as_ptr(),
+ title.as_ptr(),
+ WINIT_PROGRESS,
+ -20000,
+ -20000,
+ 548,
+ 444,
+ std::ptr::null_mut(),
+ std::ptr::null_mut(),
+ std::ptr::null_mut(),
+ std::ptr::null(),
+ );
+ assert!(!h.is_null());
+ h
+ };
+ let (a, b) = (make(0), make(1));
+ caption_less(a, Drawn::Fixed);
+ let state = |h: HWND| {
+ let (mut w, mut c): (RECT, RECT) = (zeroed(), zeroed());
+ GetWindowRect(h, &mut w);
+ GetClientRect(h, &mut c);
+ (
+ GetWindowLongPtrW(h, GWL_STYLE) as u32,
+ GetWindowLongPtrW(h, GWL_EXSTYLE) as u32,
+ [w.left, w.top, w.right, w.bottom],
+ [c.right, c.bottom],
+ )
+ };
+ for (up, other) in [(a, b), (b, a), (a, b)] {
+ let before = state(up);
+ above_others(up);
+ assert!(z(up) < z(other), "lifted above the other window");
+ let after = state(up);
+ assert_eq!(after.1 & WS_EX_TOPMOST, 0, "left topmost");
+ assert_eq!(before, after, "only its place in the z-order changes");
+ }
+ DestroyWindow(a);
+ DestroyWindow(b);
+ }
+ }
+}
+
/// The main window's "Minimize to the notification area" (#63): its minimize command (title bar,
/// system menu, taskbar) asks the app to hide it instead. The app can't see the window go
/// minimized: an idle app runs no frame while minimized, so it would never know.