From ac7e4283cd068e0ae716d41e965acf783e55f98a Mon Sep 17 00:00:00 2001 From: Devan <68340659+DevanMetz@users.noreply.github.com> Date: Tue, 27 Oct 2026 22:46:01 -0500 Subject: [PATCH] Follow Linux desktop light and dark themes --- PARITY.md | 2 +- src/gui/mod.rs | 8 ++ src/main.rs | 2 + src/system_theme.rs | 191 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 src/system_theme.rs diff --git a/PARITY.md b/PARITY.md index c61085f..a129e95 100644 --- a/PARITY.md +++ b/PARITY.md @@ -36,7 +36,7 @@ Status of each Windows user-facing feature on Linux in this tree: `working`, `pa | Always-on-top strip on Wayland | not possible | `src/gui/overlay.rs` — xdg-shell has no stacking request. A layer-shell surface would replace the strip. The compositor's keep-above rule is the equivalent | | Strip hover hints kept inside the monitor work area | partial | `src/gui/overlay.rs` — `monitor_work_area` is `None` | | Minimize to the strip, and always show the strip | working | `src/gui/overlay.rs`, `src/gui/settings_tab.rs` | -| System light or dark theme | partial | winit reports no system theme on X11 (`theme` is `None`) and on Wayland only a theme this process already set, so egui stays on its dark fallback | +| System light or dark theme | working | `src/system_theme.rs` — follows the desktop Settings portal live on X11 and Wayland; falls back to GTK color-scheme / gtk-theme via gsettings when unavailable or unspecified | | Screen reader | working | `Cargo.toml` (eframe `accesskit`, `accesskit_unix`) | | One running copy (lock) | working | `src/instance.rs` | | Settings saved across launches | working | `src/config.rs` | diff --git a/src/gui/mod.rs b/src/gui/mod.rs index e040a80..9b78c98 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -272,6 +272,8 @@ pub struct App { window_placement: overlay::Placement, /// The window was minimized last frame (to catch the moment it is). was_minimized: bool, + #[cfg(target_os = "linux")] + system_theme: Option, mic_test: mic_test::MicTest, update_error: Option, } @@ -288,6 +290,10 @@ impl App { ) -> Self { widgets::configure_text_styles(ctx); let mut app = Self::stopped(settings); + #[cfg(target_os = "linux")] + { + app.system_theme = Some(crate::system_theme::start(ctx)); + } app.updated_at = just_updated.then(Instant::now); { // Development builds never replace themselves on their own. @@ -413,6 +419,8 @@ impl App { overlay_on_top: overlay::OnTop::default(), window_placement: overlay::Placement::default(), was_minimized: false, + #[cfg(target_os = "linux")] + system_theme: None, mic_test: mic_test::MicTest::default(), update_error: None, } diff --git a/src/main.rs b/src/main.rs index b1f6938..c65534c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,8 @@ mod portal_shortcuts; mod record; mod resample; mod rt; +#[cfg(target_os = "linux")] +mod system_theme; mod tray; mod update; mod virtual_mic; diff --git a/src/system_theme.rs b/src/system_theme.rs new file mode 100644 index 0000000..006ca2b --- /dev/null +++ b/src/system_theme.rs @@ -0,0 +1,191 @@ +//! Linux desktop appearance, independent of winit's X11/Wayland theme support. +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, +}; +use std::time::Duration; + +use ashpd::desktop::settings::{ColorScheme, Settings}; +use eframe::egui; +use futures_channel::mpsc; +use futures_util::{FutureExt, StreamExt, select}; + +/// Stops the worker when the application closes. All desktop I/O stays off the UI thread. +pub(crate) struct Monitor(Arc); + +impl Drop for Monitor { + fn drop(&mut self) { + self.0.store(true, Ordering::Relaxed); + } +} + +pub(crate) fn start(ctx: &egui::Context) -> Monitor { + let stopped = Arc::new(AtomicBool::new(false)); + let stop = stopped.clone(); + let (mut tx, mut ticks) = mpsc::unbounded(); + // Also bounds portal discovery and lets an idle signal listener shut down. + std::thread::spawn(move || { + while !stop.load(Ordering::Relaxed) { + std::thread::sleep(Duration::from_secs(2)); + if tx.start_send(()).is_err() { + break; + } + } + }); + let ctx = ctx.clone(); + std::thread::spawn(move || { + pollster::block_on(async move { + loop { + let portal = async { + let settings = Settings::new().await?; + // Subscribe before reading so a change during startup is not lost. + let changes = settings.receive_color_scheme_changed().await?; + let scheme = settings.color_scheme().await?; + Ok::<_, ashpd::Error>((settings, changes, scheme)) + } + .fuse(); + futures_util::pin_mut!(portal); + let result = select! { + result = portal => result.ok(), + tick = ticks.next().fuse() => { + if tick.is_none() { return; } + None + } + }; + if let Some((_settings, changes, mut scheme)) = result { + futures_util::pin_mut!(changes); + loop { + apply(&ctx, portal_theme(scheme).or_else(gtk_theme)); + select! { + next = changes.next().fuse() => match next { + Some(next) => scheme = next, + None => break, + }, + tick = ticks.next().fuse() => if tick.is_none() { return; }, + } + } + } else { + apply(&ctx, gtk_theme()); + if ticks.next().await.is_none() { + return; + } + } + } + }) + }); + Monitor(stopped) +} + +fn apply(ctx: &egui::Context, theme: Option) { + if let Some(theme) = theme + && ctx.theme() != theme + { + // Explicit preference prevents winit's absent theme from resetting this next frame. + ctx.set_theme(theme); + ctx.request_repaint(); + } +} + +fn portal_theme(scheme: ColorScheme) -> Option { + match scheme { + ColorScheme::PreferDark => Some(egui::Theme::Dark), + ColorScheme::PreferLight => Some(egui::Theme::Light), + ColorScheme::NoPreference => None, + } +} + +fn gsettings(key: &str) -> Option { + let output = std::process::Command::new("gsettings") + .args(["get", "org.gnome.desktop.interface", key]) + .output() + .ok()?; + output + .status + .success() + .then(|| String::from_utf8(output.stdout).ok()) + .flatten() +} + +fn gtk_theme() -> Option { + gtk_preference(gsettings("color-scheme").as_deref(), None) + .or_else(|| gtk_preference(None, gsettings("gtk-theme").as_deref())) +} + +fn gtk_preference(scheme: Option<&str>, theme: Option<&str>) -> Option { + let unquote = |s: &str| s.trim().trim_matches(['\'', '"']).to_owned(); + match scheme.map(unquote).as_deref() { + Some("prefer-dark") => Some(egui::Theme::Dark), + Some("prefer-light") => Some(egui::Theme::Light), + _ => theme + .map(unquote) + .filter(|name| !name.is_empty()) + .map(|name| { + if name.to_ascii_lowercase().ends_with("-dark") { + egui::Theme::Dark + } else { + egui::Theme::Light + } + }), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn portal_preferences() { + assert_eq!( + portal_theme(ColorScheme::PreferDark), + Some(egui::Theme::Dark) + ); + assert_eq!( + portal_theme(ColorScheme::PreferLight), + Some(egui::Theme::Light) + ); + assert_eq!(portal_theme(ColorScheme::NoPreference), None); + } + + #[test] + fn desktop_changes_survive_missing_winit_theme() { + let ctx = egui::Context::default(); + for theme in [egui::Theme::Light, egui::Theme::Dark, egui::Theme::Light] { + apply(&ctx, Some(theme)); + let _ = ctx.run_ui(egui::RawInput::default(), |ui| { + assert_eq!(ui.ctx().theme(), theme); + }); + } + // Missing desktop settings do not erase the last usable preference. + apply(&ctx, None); + let _ = ctx.run_ui(egui::RawInput::default(), |ui| { + assert_eq!(ui.ctx().theme(), egui::Theme::Light); + }); + } + + #[test] + fn gtk_preferences() { + for (scheme, theme, expected) in [ + ( + Some("'prefer-dark'\n"), + Some("'Adwaita'"), + Some(egui::Theme::Dark), + ), + ( + Some("'prefer-light'"), + Some("'Adwaita-dark'"), + Some(egui::Theme::Light), + ), + ( + Some("'default'"), + Some("'Adwaita-dark'\n"), + Some(egui::Theme::Dark), + ), + (None, Some("\"Breeze-Dark\""), Some(egui::Theme::Dark)), + (None, Some("'Adwaita'"), Some(egui::Theme::Light)), + (Some("unknown"), Some("''"), None), + (None, None, None), + ] { + assert_eq!(gtk_preference(scheme, theme), expected); + } + } +}