Skip to content

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

Windows: the tray icon's menu follows MDM's look, dark or light (#682) (#722)

Ahmed Nefzaoui19296802b99c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ba51918e..45785d13 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,7 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
- The window no longer waits for the program that owns the clipboard. A text box's right-click menu (to offer **Paste**) and **Add URL**, **Add batch download**, **Add torrent** and **Download video** (to fill in a copied link) read the clipboard on the window's own thread, so a program that held the clipboard without answering (on Linux, where a read waited up to 4 seconds each time, and while the right-click menu was open at every frame; on Windows, a program that had stopped responding) held up MDM's window too. They now read it beside the window, as the clipboard monitor has since 0.5.1; a link copied just before still fills the box as the window opens (follows [#418](https://github.com/anefzaoui/misterdownloadmanager/issues/418)).
- Linux: freeze and crash reports show MDM's own functions, files and lines again, and the right ones. When MDM's code began a page after the start of the program in memory (as 0.5.0's did), the report read the whole stack at the wrong place: it showed only system libraries (`freeze in libc.so.6+…`), named other functions of MDM once the release's symbols were fetched, and couldn't find an existing issue for the same problem. A `.dmp` from such a report reads right too ([#661](https://github.com/anefzaoui/misterdownloadmanager/issues/661)).
- Windows: a freeze report whose window thread hardly ran and stopped in the C runtime's memory routines (`VCRUNTIME140.dll`, comparing the first bytes of an icon) now says the computer was too busy to run MDM or slow to load it from disk, with the page faults counted meanwhile, instead of a deadlock; those routines never wait ([#663](https://github.com/anefzaoui/misterdownloadmanager/issues/663)).
+- Windows: the notification-area icon's menu (and the window menu of a title bar MDM draws) follows MDM's look: dark in dark mode or with a dark theme, light otherwise, whatever Windows' own mode is. It was always drawn in Windows' light colours, also on Windows 10 ([#682](https://github.com/anefzaoui/misterdownloadmanager/issues/682)).
- Torrents: magnet links without trackers find peers through the DHT again where its first nodes didn't answer. MDM asked only two public routers to join the DHT; where neither answered (one name pointing at an old address, or a network blocking one), the DHT stayed empty and the log said "error in bootstrap" every minute or two. It now asks four together (`dht.libtorrent.org`, `dht.transmissionbt.com`, `router.bittorrent.com`, `router.utorrent.com`), any one that answers is enough, and the others are asked no more once one has. The nodes it knows are kept for the next start, the IPv6 ones too ([#650](https://github.com/anefzaoui/misterdownloadmanager/issues/650)).
- Torrents: with the **Port** box empty in Torrent settings, MDM keeps the port it picked and listens on it again at the next start (another one if a program has taken it), so peers that learned it, and a router's or firewall's rule for it, still reach MDM; it was a new port at each start. MDM's log says where it listens: one socket for IPv4 and IPv6, which Windows' `netstat -p tcp` (IPv4 only) doesn't list ([#650](https://github.com/anefzaoui/misterdownloadmanager/issues/650)).
- macOS: YouTube videos that need yt-dlp's JavaScript runtime load when MDM is opened from Finder or the Dock. MDM found Homebrew's yt-dlp, but the helpers it started didn't get Homebrew's folders on their PATH, so yt-dlp found neither Deno nor ffmpeg ("Requested format is not available"); they now look there too, after the folders of their own PATH ([#659](https://github.com/anefzaoui/misterdownloadmanager/issues/659)).
diff --git a/Cargo.toml b/Cargo.toml
index 03e85201..939a0cbd 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -57,6 +57,7 @@ windows-sys = { version = "0.61.2", features = [
"Win32_System_Console",
"Win32_System_IO",
"Win32_System_Ioctl",
+ "Win32_System_LibraryLoader",
"Win32_System_Power",
"Win32_System_ProcessStatus",
"Win32_System_Registry",
diff --git a/ROADMAP.md b/ROADMAP.md
index e496c478..4c50557a 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -600,6 +600,9 @@ These ship in a later version, when they're ready.
- 🔄 Torrents: a pause landing while a piece was being checked leaving the torrent one piece short
until MDM restarted ([#706](https://github.com/anefzaoui/misterdownloadmanager/issues/706)): the
piece is now downloaded again after the pause; fix in review
+- 🔄 Windows: the tray icon's menu light while MDM is dark
+ ([#682](https://github.com/anefzaoui/misterdownloadmanager/issues/682)): it now follows MDM's
+ look, dark or light, on Windows 10 and 11; fix in review
- 🔄 Torrents: magnet links without trackers finding no peers where the DHT's first nodes didn't
answer ([#650](https://github.com/anefzaoui/misterdownloadmanager/issues/650)): four public
routers asked together, the ones that never answer left alone once one has, the IPv6 nodes kept
diff --git a/crates/app/src/app.rs b/crates/app/src/app.rs
index 85f36254..26a0424c 100644
--- a/crates/app/src/app.rs
+++ b/crates/app/src/app.rs
@@ -9533,7 +9533,12 @@ impl App {
{
self.caption_set =
crate::win32::caption_dark(crate::MAIN_TITLE, crate::theme::dark_surface());
+ // The menus Windows draws (the tray icon's, the window menu) too (#682).
+ crate::win32::menus_dark(crate::theme::dark_surface());
}
+ // Linux: the tray's menu is drawn by the desktop's StatusNotifierItem host (ksni
+ // sends only its items), in the desktop's theme; no app can colour it. macOS: the
+ // menu bar item's menu follows the menu bar's appearance, the system's.
#[cfg(not(windows))]
{
self.caption_set = true;
diff --git a/crates/app/src/win32.rs b/crates/app/src/win32.rs
index 6a4574de..9c27b9e3 100644
--- a/crates/app/src/win32.rs
+++ b/crates/app/src/win32.rs
@@ -2308,6 +2308,97 @@ pub fn caption_dark(title: &str, dark: bool) -> bool {
true
}
+/// The popup menus Windows draws for us — the notification-area icon's (tray-icon's), the
+/// window menu of a drawn title bar — in MDM's mode, not Windows' (#682): dark on a dark surface
+/// (dark mode, a dark theme), light otherwise, whatever Windows' own app mode says. Windows has
+/// no documented way: uxtheme's SetPreferredAppMode (ordinal 135, ForceDark 2 / ForceLight 3)
+/// sets the process's mode and FlushMenuThemes (ordinal 136) makes menus read it again, as the
+/// win32-darkmode project and the apps built on it do. They exist from Windows 10 1903 (build
+/// 18362) on; 1809 has another function at 135 and older builds no dark menus, so there menus
+/// stay as Windows draws them. Called when the mode changes; asks uxtheme only when it is news.
+pub fn menus_dark(dark: bool) {
+ use std::sync::atomic::{AtomicU8, Ordering};
+ static TOLD: AtomicU8 = AtomicU8::new(u8::MAX);
+ let mode: i32 = if dark { 2 } else { 3 };
+ if TOLD.swap(mode as u8, Ordering::Relaxed) == mode as u8 {
+ return;
+ }
+ let Some(f) = menu_mode_fns() else {
+ return;
+ };
+ // SAFETY: the functions behind these ordinals on build 18362 and later (menu_mode_fns),
+ // SetPreferredAppMode(int) -> int and FlushMenuThemes(void); process-wide, any thread.
+ unsafe {
+ (f.set_preferred_app_mode)(mode);
+ (f.flush_menu_themes)();
+ }
+}
+
+/// uxtheme's functions that set the mode of the menus Windows draws ([`menus_dark`]).
+#[derive(Clone, Copy)]
+struct MenuModeFns {
+ set_preferred_app_mode: unsafe extern "system" fn(i32) -> i32,
+ flush_menu_themes: unsafe extern "system" fn(),
+}
+
+/// [`MenuModeFns`], looked up once: None before Windows 10 1903 or if uxtheme lacks them.
+fn menu_mode_fns() -> Option<MenuModeFns> {
+ use windows_sys::Win32::System::LibraryLoader::{
+ GetProcAddress, LOAD_LIBRARY_SEARCH_SYSTEM32, LoadLibraryExW,
+ };
+ use windows_sys::Win32::System::Registry::HKEY_LOCAL_MACHINE;
+ static FNS: std::sync::OnceLock<Option<MenuModeFns>> = std::sync::OnceLock::new();
+ *FNS.get_or_init(|| {
+ let build = reg_string_in(
+ HKEY_LOCAL_MACHINE,
+ r"SOFTWARE\Microsoft\Windows NT\CurrentVersion",
+ "CurrentBuild",
+ )
+ .and_then(|b| b.parse::<u32>().ok())?;
+ if build < 18362 {
+ return None;
+ }
+ let name: Vec<u16> = "uxtheme.dll\0".encode_utf16().collect();
+ // SAFETY: a NUL-terminated name, loaded from System32 only; the module stays loaded for
+ // the process's life (never freed), so the pointers taken from it stay valid. An ordinal
+ // goes as the name pointer's low word (MAKEINTRESOURCEA).
+ unsafe {
+ let module = LoadLibraryExW(
+ name.as_ptr(),
+ std::ptr::null_mut(),
+ LOAD_LIBRARY_SEARCH_SYSTEM32,
+ );
+ if module.is_null() {
+ return None;
+ }
+ let set = GetProcAddress(module, 135usize as *const u8)?;
+ let flush = GetProcAddress(module, 136usize as *const u8)?;
+ Some(MenuModeFns {
+ set_preferred_app_mode: std::mem::transmute::<
+ unsafe extern "system" fn() -> isize,
+ unsafe extern "system" fn(i32) -> i32,
+ >(set),
+ flush_menu_themes: std::mem::transmute::<
+ unsafe extern "system" fn() -> isize,
+ unsafe extern "system" fn(),
+ >(flush),
+ })
+ }
+ })
+}
+
+#[cfg(test)]
+mod menus_dark_tests {
+ /// #682: uxtheme has the menu mode functions on every Windows MDM is tested on (10 1903 and
+ /// later, Server 2022's runner included), and setting the mode either way returns.
+ #[test]
+ fn the_menu_mode_functions_are_found_and_answer() {
+ assert!(super::menu_mode_fns().is_some());
+ super::menus_dark(true);
+ super::menus_dark(false);
+ }
+}
+
/// One of our windows by handle, for crates that take a `raw-window-handle` window.
struct OwnWindow(HWND);