diff --git a/crates/ezd-tray/src/ui/system_accent.rs b/crates/ezd-tray/src/ui/system_accent.rs index 0e1d7ae..da5a9e8 100644 --- a/crates/ezd-tray/src/ui/system_accent.rs +++ b/crates/ezd-tray/src/ui/system_accent.rs @@ -46,9 +46,33 @@ fn parse_kdeglobals(text: &str) -> Option<[u8; 3]> { None } +/// The accent of an Ubuntu GTK theme name. GNOME before 47 has no accent setting, and Ubuntu +/// encodes the accent chosen in its settings in the theme: `Yaru`, `Yaru-dark`, `Yaru-purple`, +/// `Yaru-purple-dark`. The colours are the theme's own (`common/accent-colors.scss.in`). +#[cfg(any(target_os = "linux", test))] +fn yaru_accent(theme: &str) -> Option<[u8; 3]> { + let rest = theme.strip_prefix("Yaru")?; + let rest = rest.strip_suffix("-dark").unwrap_or(rest); + Some(match rest { + "" => [0xE9, 0x54, 0x20], + "-bark" => [0x78, 0x78, 0x59], + "-sage" => [0x65, 0x7B, 0x69], + "-olive" => [0x4B, 0x85, 0x01], + "-viridian" => [0x03, 0x87, 0x5B], + "-prussiangreen" => [0x30, 0x82, 0x80], + "-blue" => [0x00, 0x73, 0xE5], + "-purple" => [0x77, 0x64, 0xD8], + "-magenta" => [0xB3, 0x4C, 0xB3], + "-red" => [0xDA, 0x34, 0x50], + "-yellow" => [0xC8, 0x88, 0x00], + "-wartybrown" => [0xB3, 0x91, 0x69], + _ => return None, + }) +} + #[cfg(target_os = "linux")] mod linux { - use super::{parse_kdeglobals, portal_color}; + use super::{parse_kdeglobals, portal_color, yaru_accent}; use std::time::Duration; /// A missing portal must not hold the caller up. @@ -60,29 +84,49 @@ mod linux { Ok(None) => tracing::debug!("the desktop portal has no accent colour"), Err(e) => tracing::debug!("reading the accent colour from the portal failed: {e}"), } + match with_timeout(portal_gtk_theme) { + Ok(theme) => { + if let Some(c) = yaru_accent(&theme) { + return Some(c); + } + tracing::debug!("the GTK theme {theme:?} names no accent colour"); + } + Err(e) => tracing::debug!("reading the GTK theme from the portal failed: {e}"), + } kdeglobals_accent() } - fn portal_accent() -> Result, String> { + fn with_timeout(call: fn() -> zbus::Result) -> Result { let (tx, rx) = std::sync::mpsc::channel(); std::thread::spawn(move || { - let _ = tx.send(portal_call().map_err(|e| e.to_string())); + let _ = tx.send(call().map_err(|e| e.to_string())); }); rx.recv_timeout(PORTAL_TIMEOUT) .map_err(|_| "timed out".to_string())? } - fn portal_call() -> zbus::Result> { - use zbus::zvariant::OwnedValue; + fn read_setting(namespace: &str, key: &str) -> zbus::Result { let conn = zbus::blocking::Connection::session()?; let reply = conn.call_method( Some("org.freedesktop.portal.Desktop"), "/org/freedesktop/portal/desktop", Some("org.freedesktop.portal.Settings"), "ReadOne", - &("org.freedesktop.appearance", "accent-color"), + &(namespace, key), )?; - let value: OwnedValue = reply.body().deserialize()?; + reply.body().deserialize() + } + + fn portal_gtk_theme() -> zbus::Result { + Ok(read_setting("org.gnome.desktop.interface", "gtk-theme")?.try_into()?) + } + + fn portal_accent() -> Result, String> { + with_timeout(portal_call) + } + + fn portal_call() -> zbus::Result> { + let value = read_setting("org.freedesktop.appearance", "accent-color")?; let (r, g, b): (f64, f64, f64) = value.try_into()?; Ok(portal_color(r, g, b)) } @@ -113,6 +157,17 @@ mod tests { assert_eq!(portal_color(f64::NAN, 0.0, 0.0), None); } + #[test] + fn ubuntu_theme_names_carry_the_accent() { + assert_eq!(yaru_accent("Yaru"), Some([0xE9, 0x54, 0x20])); + assert_eq!(yaru_accent("Yaru-dark"), Some([0xE9, 0x54, 0x20])); + assert_eq!(yaru_accent("Yaru-purple"), Some([0x77, 0x64, 0xD8])); + assert_eq!(yaru_accent("Yaru-purple-dark"), Some([0x77, 0x64, 0xD8])); + assert_eq!(yaru_accent("Yaru-unknown"), None); + assert_eq!(yaru_accent("Adwaita"), None); + assert_eq!(yaru_accent("Adwaita-dark"), None); + } + #[test] fn kdeglobals_accent_is_read_from_general() { let text = "[Colors:View]\nAccentColor=1,2,3\n[General]\nName=x\nAccentColor=61, 174,233\n";