Skip to content

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

Fix: no crash when a window opens while MDM is minimized; dark mode on Linux (#23)

Ahmed Nefzaoui68f9bc2b3faa
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f2b9b49e..f70de107 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -54,6 +54,14 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
### Fixed
+- MDM closed with an internal error when a window opened while the main window was minimized
+ or hidden, for example Download File Info for a browser download
+ ([#22](https://github.com/anefzaoui/misterdownloadmanager/issues/22)). The cause was in egui,
+ the toolkit MDM is drawn with ([emilk/egui#8470](https://github.com/emilk/egui/issues/8470)),
+ and is fixed in MDM's copy of it.
+- **View → Dark Mode support** did nothing on Linux: MDM asked the window system for the
+ desktop's light or dark setting, and X11 never says. It now asks the desktop itself (the
+ freedesktop portal, which KDE, GNOME and most others answer) and follows it as it changes.
- A second browser download arriving while Download File Info was open replaced the first, which
was lost (the browser had already given it up). They now wait their turn, and those still
waiting when MDM quits are kept in the main queue (Download Later).
diff --git a/Cargo.lock b/Cargo.lock
index 60347539..38a28ae1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2463,7 +2463,7 @@ dependencies = [
[[package]]
name = "ecolor"
version = "0.37.0"
-source = "git+https://github.com/anefzaoui/egui.git?rev=5cfdcfcb55ddb374d9593ece6605a21119cb03c4#5cfdcfcb55ddb374d9593ece6605a21119cb03c4"
+source = "git+https://github.com/anefzaoui/egui.git?rev=60b5751250171425d239f6580aaca7fc13d60c64#60b5751250171425d239f6580aaca7fc13d60c64"
dependencies = [
"bytemuck",
"emath",
@@ -2497,7 +2497,7 @@ dependencies = [
[[package]]
name = "eframe"
version = "0.37.0"
-source = "git+https://github.com/anefzaoui/egui.git?rev=5cfdcfcb55ddb374d9593ece6605a21119cb03c4#5cfdcfcb55ddb374d9593ece6605a21119cb03c4"
+source = "git+https://github.com/anefzaoui/egui.git?rev=60b5751250171425d239f6580aaca7fc13d60c64#60b5751250171425d239f6580aaca7fc13d60c64"
dependencies = [
"ahash",
"bytemuck",
@@ -2531,7 +2531,7 @@ dependencies = [
[[package]]
name = "egui"
version = "0.37.0"
-source = "git+https://github.com/anefzaoui/egui.git?rev=5cfdcfcb55ddb374d9593ece6605a21119cb03c4#5cfdcfcb55ddb374d9593ece6605a21119cb03c4"
+source = "git+https://github.com/anefzaoui/egui.git?rev=60b5751250171425d239f6580aaca7fc13d60c64#60b5751250171425d239f6580aaca7fc13d60c64"
dependencies = [
"accesskit",
"ahash",
@@ -2550,7 +2550,7 @@ dependencies = [
[[package]]
name = "egui-wgpu"
version = "0.37.0"
-source = "git+https://github.com/anefzaoui/egui.git?rev=5cfdcfcb55ddb374d9593ece6605a21119cb03c4#5cfdcfcb55ddb374d9593ece6605a21119cb03c4"
+source = "git+https://github.com/anefzaoui/egui.git?rev=60b5751250171425d239f6580aaca7fc13d60c64#60b5751250171425d239f6580aaca7fc13d60c64"
dependencies = [
"ahash",
"bytemuck",
@@ -2569,7 +2569,7 @@ dependencies = [
[[package]]
name = "egui-winit"
version = "0.37.0"
-source = "git+https://github.com/anefzaoui/egui.git?rev=5cfdcfcb55ddb374d9593ece6605a21119cb03c4#5cfdcfcb55ddb374d9593ece6605a21119cb03c4"
+source = "git+https://github.com/anefzaoui/egui.git?rev=60b5751250171425d239f6580aaca7fc13d60c64#60b5751250171425d239f6580aaca7fc13d60c64"
dependencies = [
"accesskit_winit",
"arboard",
@@ -2594,7 +2594,7 @@ dependencies = [
[[package]]
name = "egui_extras"
version = "0.37.0"
-source = "git+https://github.com/anefzaoui/egui.git?rev=5cfdcfcb55ddb374d9593ece6605a21119cb03c4#5cfdcfcb55ddb374d9593ece6605a21119cb03c4"
+source = "git+https://github.com/anefzaoui/egui.git?rev=60b5751250171425d239f6580aaca7fc13d60c64#60b5751250171425d239f6580aaca7fc13d60c64"
dependencies = [
"ahash",
"egui",
@@ -2610,7 +2610,7 @@ dependencies = [
[[package]]
name = "egui_glow"
version = "0.37.0"
-source = "git+https://github.com/anefzaoui/egui.git?rev=5cfdcfcb55ddb374d9593ece6605a21119cb03c4#5cfdcfcb55ddb374d9593ece6605a21119cb03c4"
+source = "git+https://github.com/anefzaoui/egui.git?rev=60b5751250171425d239f6580aaca7fc13d60c64#60b5751250171425d239f6580aaca7fc13d60c64"
dependencies = [
"bytemuck",
"egui",
@@ -2663,7 +2663,7 @@ dependencies = [
[[package]]
name = "emath"
version = "0.37.0"
-source = "git+https://github.com/anefzaoui/egui.git?rev=5cfdcfcb55ddb374d9593ece6605a21119cb03c4#5cfdcfcb55ddb374d9593ece6605a21119cb03c4"
+source = "git+https://github.com/anefzaoui/egui.git?rev=60b5751250171425d239f6580aaca7fc13d60c64#60b5751250171425d239f6580aaca7fc13d60c64"
dependencies = [
"bytemuck",
]
@@ -2732,7 +2732,7 @@ dependencies = [
[[package]]
name = "epaint"
version = "0.37.0"
-source = "git+https://github.com/anefzaoui/egui.git?rev=5cfdcfcb55ddb374d9593ece6605a21119cb03c4#5cfdcfcb55ddb374d9593ece6605a21119cb03c4"
+source = "git+https://github.com/anefzaoui/egui.git?rev=60b5751250171425d239f6580aaca7fc13d60c64#60b5751250171425d239f6580aaca7fc13d60c64"
dependencies = [
"ahash",
"bytemuck",
@@ -2758,7 +2758,7 @@ dependencies = [
[[package]]
name = "epaint_default_fonts"
version = "0.37.0"
-source = "git+https://github.com/anefzaoui/egui.git?rev=5cfdcfcb55ddb374d9593ece6605a21119cb03c4#5cfdcfcb55ddb374d9593ece6605a21119cb03c4"
+source = "git+https://github.com/anefzaoui/egui.git?rev=60b5751250171425d239f6580aaca7fc13d60c64#60b5751250171425d239f6580aaca7fc13d60c64"
[[package]]
name = "equator"
@@ -5102,6 +5102,7 @@ dependencies = [
"url",
"windows-sys 0.61.2",
"winresource",
+ "zbus",
]
[[package]]
diff --git a/Cargo.toml b/Cargo.toml
index fe95ca4a..e888f667 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -124,18 +124,19 @@ opt-level = 3
[profile.dev.package.crc32fast]
opt-level = 3
-# storytold's egui fork (as storytold/craft-libs, D-003) plus one MDM commit on anefzaoui/egui
+# storytold's egui fork (as storytold/craft-libs, D-003) plus two MDM commits on anefzaoui/egui
# (branch mdm-accesskit-windows): every window created hidden gets an AccessKit adapter, so
-# screen readers read the dialogs and menus. craft-libs can take the same commit to share one
-# egui again.
+# screen readers read the dialogs and menus; and a dialog opened while the main window is
+# minimized no longer panics (emilk/egui#8470). craft-libs can take the same commits to share
+# one egui again.
[patch.crates-io]
-ecolor = { git = "https://github.com/anefzaoui/egui.git", rev = "5cfdcfcb55ddb374d9593ece6605a21119cb03c4" }
-emath = { git = "https://github.com/anefzaoui/egui.git", rev = "5cfdcfcb55ddb374d9593ece6605a21119cb03c4" }
-epaint = { git = "https://github.com/anefzaoui/egui.git", rev = "5cfdcfcb55ddb374d9593ece6605a21119cb03c4" }
-epaint_default_fonts = { git = "https://github.com/anefzaoui/egui.git", rev = "5cfdcfcb55ddb374d9593ece6605a21119cb03c4" }
-egui = { git = "https://github.com/anefzaoui/egui.git", rev = "5cfdcfcb55ddb374d9593ece6605a21119cb03c4" }
-egui-winit = { git = "https://github.com/anefzaoui/egui.git", rev = "5cfdcfcb55ddb374d9593ece6605a21119cb03c4" }
-egui_glow = { git = "https://github.com/anefzaoui/egui.git", rev = "5cfdcfcb55ddb374d9593ece6605a21119cb03c4" }
-egui_extras = { git = "https://github.com/anefzaoui/egui.git", rev = "5cfdcfcb55ddb374d9593ece6605a21119cb03c4" }
-eframe = { git = "https://github.com/anefzaoui/egui.git", rev = "5cfdcfcb55ddb374d9593ece6605a21119cb03c4" }
+ecolor = { git = "https://github.com/anefzaoui/egui.git", rev = "60b5751250171425d239f6580aaca7fc13d60c64" }
+emath = { git = "https://github.com/anefzaoui/egui.git", rev = "60b5751250171425d239f6580aaca7fc13d60c64" }
+epaint = { git = "https://github.com/anefzaoui/egui.git", rev = "60b5751250171425d239f6580aaca7fc13d60c64" }
+epaint_default_fonts = { git = "https://github.com/anefzaoui/egui.git", rev = "60b5751250171425d239f6580aaca7fc13d60c64" }
+egui = { git = "https://github.com/anefzaoui/egui.git", rev = "60b5751250171425d239f6580aaca7fc13d60c64" }
+egui-winit = { git = "https://github.com/anefzaoui/egui.git", rev = "60b5751250171425d239f6580aaca7fc13d60c64" }
+egui_glow = { git = "https://github.com/anefzaoui/egui.git", rev = "60b5751250171425d239f6580aaca7fc13d60c64" }
+egui_extras = { git = "https://github.com/anefzaoui/egui.git", rev = "60b5751250171425d239f6580aaca7fc13d60c64" }
+eframe = { git = "https://github.com/anefzaoui/egui.git", rev = "60b5751250171425d239f6580aaca7fc13d60c64" }
winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" }
diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml
index 7d41fc5c..f2d061b1 100644
--- a/crates/app/Cargo.toml
+++ b/crates/app/Cargo.toml
@@ -56,6 +56,9 @@ tray-icon = "0.26.1"
[target.'cfg(target_os = "linux")'.dependencies]
eframe = { workspace = true, features = ["wayland", "x11"] }
tray-icon = { version = "0.26.1", default-features = false, features = ["ksni"] }
+# The desktop's light or dark preference from the freedesktop portal (unix.rs); already in the
+# tree through the tray (ksni) and AccessKit, with the same features.
+zbus = { version = "5.19.0", default-features = false, features = ["async-io", "blocking-api"] }
# Crash reports on Linux and macOS: thread and process ids, signals (unix.rs).
[target.'cfg(unix)'.dependencies]
diff --git a/crates/app/src/theme.rs b/crates/app/src/theme.rs
index b97148d9..137ff41e 100644
--- a/crates/app/src/theme.rs
+++ b/crates/app/src/theme.rs
@@ -45,15 +45,19 @@ pub fn system_theme(dark: bool) -> egui::SystemTheme {
}
}
-/// Decide the mode from the setting and the system's: Windows' app mode, elsewhere the light or
-/// dark appearance the window system reports (light when it says nothing). True if it changed.
+/// Decide the mode from the setting and the system's: Windows' app mode; on Linux the desktop's
+/// preference (the freedesktop portal), else what the window system reports; on macOS the
+/// appearance the window system reports (light when nothing says). True if it changed.
pub fn update(dark_mode_support: bool, ctx: &egui::Context) -> bool {
#[cfg(windows)]
let system_dark = {
let _ = ctx;
crate::win32::apps_use_dark()
};
- #[cfg(not(windows))]
+ #[cfg(target_os = "linux")]
+ let system_dark = crate::unix::desktop_dark(ctx)
+ .unwrap_or_else(|| ctx.system_theme() == Some(egui::Theme::Dark));
+ #[cfg(not(any(windows, target_os = "linux")))]
let system_dark = ctx.system_theme() == Some(egui::Theme::Dark);
let want = dark_mode_support && system_dark;
DARK.swap(want, Ordering::Relaxed) != want
diff --git a/crates/app/src/unix.rs b/crates/app/src/unix.rs
index f298a3c1..4b5b773b 100644
--- a/crates/app/src/unix.rs
+++ b/crates/app/src/unix.rs
@@ -1,8 +1,9 @@
//! Linux and macOS counterparts of what `win32` does through Windows (experimental builds):
//! starting with the session, the browsers' host registration, sounds, showing a file in its
//! folder, and the torrent link handler. Each runs the system's own tool, so nothing here needs
-//! a library; the crash reports (at the end) are the exception: signals and Mach exceptions
-//! through crash-handler, thread and process ids through libc.
+//! a library; the exceptions are the desktop's dark mode on Linux (D-Bus, through zbus) and the
+//! crash reports (at the end): signals and Mach exceptions through crash-handler, thread and
+//! process ids through libc.
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
@@ -298,6 +299,82 @@ pub fn wheel_lines() -> Option<f32> {
Some(3.0)
}
+/// The desktop's light or dark preference on Linux, as the freedesktop portal tells it
+/// (`org.freedesktop.appearance` `color-scheme`, which KDE, GNOME, Cinnamon, COSMIC and the
+/// others set): `Some(true)` dark, `Some(false)` light, `None` when no portal answers or the
+/// desktop has no preference (then the window system's answer is used). X11 reports no theme to
+/// winit at all, so without this "Dark Mode support" never turns dark there. The first call
+/// starts a thread that reads it and follows its changes (D-Bus can take seconds to answer, or
+/// start the portal); `ctx` is repainted when it changes.
+#[cfg(target_os = "linux")]
+pub fn desktop_dark(ctx: &egui::Context) -> Option<bool> {
+ use std::sync::atomic::{AtomicU8, Ordering};
+ // 0: not known, 1: dark, 2: light.
+ static SCHEME: AtomicU8 = AtomicU8::new(0);
+ static WATCH: std::sync::Once = std::sync::Once::new();
+ WATCH.call_once(|| {
+ let ctx = ctx.clone();
+ let _ = std::thread::Builder::new()
+ .name("desktop-theme".into())
+ .spawn(move || {
+ let set = |v: &zbus::zvariant::Value| {
+ let scheme = match color_scheme(v) {
+ Some(1) => 1,
+ Some(2) => 2,
+ _ => 0,
+ };
+ if SCHEME.swap(scheme, Ordering::Relaxed) != scheme {
+ ctx.request_repaint();
+ }
+ };
+ let watch = || -> zbus::Result<()> {
+ let conn = zbus::blocking::Connection::session()?;
+ let portal = zbus::blocking::Proxy::new(
+ &conn,
+ "org.freedesktop.portal.Desktop",
+ "/org/freedesktop/portal/desktop",
+ "org.freedesktop.portal.Settings",
+ )?;
+ let changes = portal.receive_signal("SettingChanged")?;
+ let key = ("org.freedesktop.appearance", "color-scheme");
+ // ReadOne since version 2 of the portal; Read (the value wrapped once more)
+ // before it.
+ let now: zbus::zvariant::OwnedValue = portal
+ .call("ReadOne", &key)
+ .or_else(|_| portal.call("Read", &key))?;
+ set(&now);
+ for change in changes {
+ let (namespace, name, value): (String, String, zbus::zvariant::OwnedValue) =
+ change.body().deserialize()?;
+ if (namespace.as_str(), name.as_str()) == key {
+ set(&value);
+ }
+ }
+ Ok(())
+ };
+ if let Err(e) = watch() {
+ tracing::info!("desktop theme: no answer from the portal ({e})");
+ }
+ });
+ });
+ match SCHEME.load(Ordering::Relaxed) {
+ 1 => Some(true),
+ 2 => Some(false),
+ _ => None,
+ }
+}
+
+/// The portal's `color-scheme` number (0 no preference, 1 dark, 2 light), unwrapped from the
+/// variants `Read` adds around it.
+#[cfg(target_os = "linux")]
+fn color_scheme(v: &zbus::zvariant::Value) -> Option<u32> {
+ match v {
+ zbus::zvariant::Value::U32(n) => Some(*n),
+ zbus::zvariant::Value::Value(inner) => color_scheme(inner),
+ _ => None,
+ }
+}
+
/// `win32::on_a_screen`: winit has no monitor list before the window exists, so a saved
/// position is trusted (the window manager keeps windows on screen on Linux and macOS).
pub fn on_a_screen(_x: i32, _y: i32) -> bool {
@@ -502,3 +579,19 @@ fn set_xattr(path: &Path, name: &str, value: &str) {
)
};
}
+
+#[cfg(all(test, target_os = "linux"))]
+mod tests {
+ use zbus::zvariant::Value;
+
+ /// `ReadOne` answers the number, `Read` the number wrapped in one more variant.
+ #[test]
+ fn color_scheme_unwraps_the_portals_answers() {
+ assert_eq!(super::color_scheme(&Value::U32(1)), Some(1));
+ assert_eq!(
+ super::color_scheme(&Value::Value(Box::new(Value::U32(2)))),
+ Some(2)
+ );
+ assert_eq!(super::color_scheme(&Value::Str("dark".into())), None);
+ }
+}