From 8470bb318361a2931e7b91d0bea5995174e84421 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Mon, 1 Apr 2024 02:56:56 -0400 Subject: [PATCH] feat: Default GTK theme without dependencies The following patch relies on `gsettings`, which should be installed by the GTK suite on most distros, instead of linking with GTK. --- src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 22d84c7..ce05c41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,6 +89,42 @@ pub fn list_themes() -> Vec<&'static str> { themes } +/// Return the default GTK theme if set. +/// +/// ## Example +/// ```rust +/// use freedesktop_icons::default_theme_gtk; +/// +/// let theme = default_theme_gtk(); +/// +/// assert_eq!(Some("Adwaita"), theme); +/// ``` +pub fn default_theme_gtk() -> Option<&'static str> { + // Calling gsettings is the simplest way to retrieve the default icon theme without adding + // GTK as a dependency. There seems to be several ways to set the default GTK theme + // including a file in XDG_CONFIG_HOME as well as an env var. Gsettings is the most + // straightforward method. + let gsettings = std::process::Command::new("gsettings") + .args(["get", "org.gnome.desktop.interface", "icon-theme"]) + .output() + .ok()?; + + // Only return the theme if it's in the cache. + if gsettings.status.success() { + let name = String::from_utf8(gsettings.stdout).ok()?; + let name = name.trim().trim_matches('\''); + THEMES.get(name).and_then(|themes| { + themes.first().and_then(|path| { + path.index + .section(Some("Icon Theme")) + .and_then(|section| section.get("Name")) + }) + }) + } else { + None + } +} + /// The lookup builder struct, holding all the lookup query parameters. pub struct LookupBuilder<'a> { name: &'a str,