misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux
Rust bittorrent-clientdebriddownload-acceleratordownload-managerdownloadmanagereguifile-downloadergallery-dlidmidm-alternativeinternet-download-managerm3u8-downloadermagnetplaylist-downloaderreal-debridrusttorrent-clientvideo-downloaderyoutube-downloaderyt-dlp
Tray click on Windows: in front is what covers the window, not the focus (#640)
Ahmed Nefzaouib488f2b539d9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f23c3ff8..cc46719a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -60,7 +60,7 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see- The **Download video** window remembers the folder you save into, per **Options → Save to**'s "Change folder for this category on last selected", as **Download File Info** does: it no longer goes back to the Video category's folder each time ([#475](https://github.com/anefzaoui/misterdownloadmanager/issues/475)).- X (Twitter) videos sent from the browser's media list are named after the tab, not the random name of their file on X's video server ([#493](https://github.com/anefzaoui/misterdownloadmanager/issues/493)).- Windows: **Open folder** on a download opens the folder with the file manager set up to open folders (Directory Opus, for example), instead of always starting Windows Explorer. With Explorer it still selects the file ([#446](https://github.com/anefzaoui/misterdownloadmanager/issues/446)).-- Left click on the tray icon now toggles the main window: hidden or minimized shows it again, in front hides it to the tray, and behind other windows brings it forward (it only ever showed it, so clicking while the window was visible did nothing).+- Left click on the tray icon now toggles the main window: hidden or minimized shows it again, in front hides it to the tray, and behind other windows brings it forward (it only ever showed it, so clicking while the window was visible did nothing). On Windows it goes by what covers the window, so it works with the icon under the taskbar's ^ too.- A download started in the browser (a video's download button, a link, a magnet or .torrent) now opens only its dialog when the main window is hidden or minimized, instead of always bringing the full window up.diff --git a/ROADMAP.md b/ROADMAP.mdindex 86df3543..25da91e1 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -501,6 +501,9 @@ Bug fixes from the first days of 0.5.0, a Safari extension, and more file types.These ship in the next version.+- ✅ Left click on the tray icon toggles the main window (on Windows also with the icon under+ the taskbar's ^), and browser downloads open only their dialog when the main window is+ hidden or minimized ([#520](https://github.com/anefzaoui/misterdownloadmanager/pull/520))- ✅ Debrid accounts: signing in by code opens only the service's own page- ✅ A speed limit raised or turned off applies at once, to the connections already waitingunder the old one too ([#621](https://github.com/anefzaoui/misterdownloadmanager/issues/621))@@ -678,9 +681,6 @@ These ship in a later version, when they're ready.- 🔄 Crash reports: a monitor that cannot open its socket exits, and an unreachable one iscleaned up instead of staying beside MDM: fix in review([#458](https://github.com/anefzaoui/misterdownloadmanager/pull/458))-- 🔄 Left click on the tray icon toggles the main window, and browser downloads open only- their dialog when the main window is hidden or minimized: fix in review- ([#520](https://github.com/anefzaoui/misterdownloadmanager/pull/520))## Planneddiff --git a/crates/app/src/app.rs b/crates/app/src/app.rsindex 487ad286..d6563d97 100644--- a/crates/app/src/app.rs+++ b/crates/app/src/app.rs@@ -3115,7 +3115,16 @@ impl App {// now still counts as in front (#520).let focus = ctx.input(|i| i.viewport().focused);let since_focused = self.tray_last_focused.map(|t| t.elapsed());- if tray_toggle_hide(self.hidden, minimized, focus, since_focused) {+ // With the icon under the taskbar's ^, the flyout took the focus well+ // before the click: Windows is asked whether anything covers the window.+ #[cfg(windows)]+ let in_front = crate::win32::main_window_in_front() == Some(true);+ // Elsewhere focus is all there is to go by (sway, KDE's tray).+ #[cfg(not(windows))]+ let in_front = false;+ if tray_toggle_hide(self.hidden, minimized, focus, since_focused)+ || (!self.hidden && !minimized && in_front)+ {self.hide_to_tray(ctx);} else {self.show_window(ctx);diff --git a/crates/app/src/win32.rs b/crates/app/src/win32.rsindex 1bf91f0b..96d47d2c 100644--- a/crates/app/src/win32.rs+++ b/crates/app/src/win32.rs@@ -60,6 +60,77 @@ fn own_capture() -> Option<HWND> {/// The main window of this run, once it exists ([`set_main_window`]). 0: not known yet.static MAIN_WINDOW: std::sync::atomic::AtomicIsize = std::sync::atomic::AtomicIsize::new(0);+/// Whether the main window is in front: no visible window of another program covers any part+/// of it from above. A tray click asks (#520): the click itself takes the focus first, and with+/// the icon under the taskbar's ^ the focus went to the flyout well before, so focus alone+/// can't tell. The taskbar and its flyout, always on top, don't count, nor do hidden, minimized+/// or cloaked windows (another virtual desktop, a suspended app) or this program's own (its+/// dialogs over it). None: the main window isn't known yet.+pub fn main_window_in_front() -> Option<bool> {+ use windows_sys::Win32::Graphics::Dwm::{DWMWA_CLOAKED, DwmGetWindowAttribute};+ use windows_sys::Win32::UI::WindowsAndMessaging::{+ GW_HWNDPREV, GetWindow, GetWindowRect, IsIconic, IsWindowVisible, WS_EX_TOPMOST,+ };+ let main = MAIN_WINDOW.load(std::sync::atomic::Ordering::Relaxed) as HWND;+ if main.is_null() {+ return None;+ }+ let rect = |h: HWND| {+ let mut r = RECT {+ left: 0,+ top: 0,+ right: 0,+ bottom: 0,+ };+ // SAFETY: a read into a local; a gone window fails harmlessly.+ (unsafe { GetWindowRect(h, &mut r) } != 0 && r.right > r.left && r.bottom > r.top)+ .then_some(r)+ };+ let m = rect(main)?;+ // SAFETY: plain queries about windows, walking the z-order up from ours.+ let me = unsafe { GetCurrentProcessId() };+ let mut h = unsafe { GetWindow(main, GW_HWNDPREV) };+ // A bound, in case windows come and go under the walk.+ for _ in 0..4096 {+ if h.is_null() {+ break;+ }+ let mut pid = 0;+ // SAFETY: as above.+ let (visible, topmost) = unsafe {+ GetWindowThreadProcessId(h, &mut pid);+ (+ IsWindowVisible(h) != 0 && IsIconic(h) == 0,+ GetWindowLongPtrW(h, GWL_EXSTYLE) as u32 & WS_EX_TOPMOST != 0,+ )+ };+ if visible && !topmost && pid != me {+ let mut cloaked: u32 = 0;+ // SAFETY: a u32 read into a local of that size.+ let known = unsafe {+ DwmGetWindowAttribute(+ h,+ DWMWA_CLOAKED as _,+ (&mut cloaked as *mut u32).cast(),+ std::mem::size_of::<u32>() as u32,+ )+ } >= 0;+ if !(known && cloaked != 0)+ && let Some(r) = rect(h)+ && r.left < m.right+ && m.left < r.right+ && r.top < m.bottom+ && m.top < r.bottom+ {+ return Some(false);+ }+ }+ // SAFETY: as above.+ h = unsafe { GetWindow(h, GW_HWNDPREV) };+ }+ Some(true)+}+/// Remember the window the app was made for: the one that stays. A way of drawing that fails/// (Direct3D with no usable adapter, renderer.rs) leaves its own window behind under the same/// title until the event loop next runs, and a dialog owned by that window goes down with it@@ -1776,7 +1847,6 @@ fn popup_ex_style(ex: u32, no_activate: bool) -> u32 {#[cfg(test)]mod popup_style_tests {use super::*;- use windows_sys::Win32::Foundation::RECT;use windows_sys::Win32::UI::WindowsAndMessaging::{AdjustWindowRectEx, WS_CLIPCHILDREN, WS_CLIPSIBLINGS, WS_EX_APPWINDOW, WS_EX_NOACTIVATE,WS_EX_TOOLWINDOW, WS_EX_TOPMOST, WS_POPUP, WS_VISIBLE,