use crate::model::IconSet;
#[must_use]
pub fn system_icon_set() -> IconSet {
if cfg!(any(target_os = "macos", target_os = "ios")) {
IconSet::SfSymbols
} else if cfg!(target_os = "windows") {
IconSet::SegoeFluent
} else if cfg!(target_os = "linux") {
IconSet::Freedesktop
} else {
IconSet::Material
}
}
pub fn system_icon_theme() -> crate::Result<String> {
crate::detect::system().icon_theme()
}
pub fn detect_icon_theme() -> crate::Result<String> {
detect_icon_theme_outcome().map_err(|failure| failure.to_error())
}
pub(crate) fn detect_icon_theme_outcome() -> Result<String, IconThemeFailure> {
#[cfg(any(target_os = "macos", target_os = "ios"))]
{
Ok("sf-symbols".to_string())
}
#[cfg(target_os = "windows")]
{
Ok("segoe-fluent".to_string())
}
#[cfg(target_os = "linux")]
{
detect_linux_icon_theme()
}
#[cfg(not(any(
target_os = "linux",
target_os = "windows",
target_os = "macos",
target_os = "ios"
)))]
{
Err(IconThemeFailure::Unsupported(std::env::consts::OS))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub(crate) struct SourceFailure {
source: String,
reason: String,
}
impl std::fmt::Display for SourceFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.source, self.reason)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub(crate) struct IconThemeUndetected(Vec<SourceFailure>);
impl std::fmt::Display for IconThemeUndetected {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut sources = self.0.iter();
if let Some(first) = sources.next() {
write!(f, "{first}")?;
}
for failure in sources {
write!(f, "; {failure}")?;
}
Ok(())
}
}
impl std::error::Error for IconThemeUndetected {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum IconThemeFailure {
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
Undetected(IconThemeUndetected),
#[cfg_attr(
any(
target_os = "linux",
target_os = "windows",
target_os = "macos",
target_os = "ios"
),
allow(dead_code)
)]
Unsupported(&'static str),
}
impl IconThemeFailure {
pub(crate) fn to_error(&self) -> crate::Error {
match self {
Self::Undetected(undetected) => crate::Error::ReaderFailed {
reader: crate::error::reader_names::ICON_THEME,
source: Box::new(undetected.clone()),
},
Self::Unsupported(platform) => crate::Error::PlatformUnsupported { platform },
}
}
#[cfg(test)]
pub(crate) fn for_tests(reason: &str) -> Self {
Self::Undetected(IconThemeUndetected(vec![SourceFailure {
source: "test".to_string(),
reason: reason.to_string(),
}]))
}
}
#[cfg(target_os = "linux")]
const GNOME_INTERFACE_SCHEMA: &str = "org.gnome.desktop.interface";
#[cfg(target_os = "linux")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum IconThemeSource {
Kde,
Gsettings(&'static str),
Xfce,
LxQt,
KdeThenGnome,
}
#[cfg(target_os = "linux")]
fn icon_theme_source(desktop: crate::detect::LinuxDesktop) -> IconThemeSource {
use crate::detect::LinuxDesktop;
match desktop {
LinuxDesktop::Kde => IconThemeSource::Kde,
LinuxDesktop::Gnome
| LinuxDesktop::Budgie
| LinuxDesktop::Hyprland
| LinuxDesktop::Sway
| LinuxDesktop::River
| LinuxDesktop::Niri
| LinuxDesktop::Wayfire
| LinuxDesktop::CosmicDe => IconThemeSource::Gsettings(GNOME_INTERFACE_SCHEMA),
LinuxDesktop::Cinnamon => IconThemeSource::Gsettings("org.cinnamon.desktop.interface"),
LinuxDesktop::Xfce => IconThemeSource::Xfce,
LinuxDesktop::Mate => IconThemeSource::Gsettings("org.mate.interface"),
LinuxDesktop::LxQt => IconThemeSource::LxQt,
LinuxDesktop::Unknown => IconThemeSource::KdeThenGnome,
}
}
#[cfg(target_os = "linux")]
fn detect_linux_icon_theme() -> Result<String, IconThemeFailure> {
let desktop = crate::detect::system().refined_linux_desktop_sync();
let found = match icon_theme_source(desktop) {
IconThemeSource::Kde => detect_kde_icon_theme(),
IconThemeSource::Gsettings(schema) => gsettings_icon_theme(schema),
IconThemeSource::Xfce => detect_xfce_icon_theme(),
IconThemeSource::LxQt => detect_lxqt_icon_theme(),
IconThemeSource::KdeThenGnome => {
return unknown_desktop_icon_theme(detect_kde_icon_theme, || {
gsettings_icon_theme(GNOME_INTERFACE_SCHEMA)
});
}
};
found.map_err(|failure| IconThemeFailure::Undetected(IconThemeUndetected(vec![failure])))
}
#[cfg(target_os = "linux")]
fn unknown_desktop_icon_theme(
kde: impl FnOnce() -> Result<String, SourceFailure>,
gnome: impl FnOnce() -> Result<String, SourceFailure>,
) -> Result<String, IconThemeFailure> {
let kde_failure = match kde() {
Ok(theme) => return Ok(theme),
Err(failure) => failure,
};
let gnome_failure = match gnome() {
Ok(theme) => return Ok(theme),
Err(failure) => failure,
};
Err(IconThemeFailure::Undetected(IconThemeUndetected(vec![
kde_failure,
gnome_failure,
])))
}
#[cfg(target_os = "linux")]
fn config_dir_unresolved(source: &str) -> SourceFailure {
SourceFailure {
source: source.to_string(),
reason: crate::detect::CONFIG_HOME_UNRESOLVED.to_string(),
}
}
#[cfg(target_os = "linux")]
const KDE_SOURCE: &str = "KDE kdeglobals";
#[cfg(all(target_os = "linux", feature = "kde"))]
fn detect_kde_icon_theme() -> Result<String, SourceFailure> {
crate::kde::kdeglobals_icon_theme().map_err(|reason| SourceFailure {
source: KDE_SOURCE.to_string(),
reason,
})
}
#[cfg(all(target_os = "linux", not(feature = "kde")))]
fn detect_kde_icon_theme() -> Result<String, SourceFailure> {
kde_icon_theme(crate::detect::xdg_config_home().as_deref(), |path| {
std::fs::read_to_string(path)
})
}
#[cfg(all(target_os = "linux", not(feature = "kde")))]
fn kde_icon_theme(
config_dir: Option<&std::path::Path>,
read: impl Fn(&std::path::Path) -> std::io::Result<String>,
) -> Result<String, SourceFailure> {
let config_dir = config_dir.ok_or_else(|| config_dir_unresolved(KDE_SOURCE))?;
let paths = [
config_dir.join("kdeglobals"),
config_dir.join("kdedefaults").join("kdeglobals"),
];
let mut reasons = Vec::new();
for path in &paths {
match read(path) {
Ok(content) => match crate::icons::ini_value(&content, "Icons", "Theme") {
Some(theme) => return Ok(theme.to_string()),
None => reasons.push(format!(
"{}: `[Icons] Theme` is absent or empty",
path.display()
)),
},
Err(e) => reasons.push(format!("{}: unreadable ({e})", path.display())),
}
}
Err(SourceFailure {
source: KDE_SOURCE.to_string(),
reason: reasons.join(", "),
})
}
#[cfg(target_os = "linux")]
fn gsettings_icon_theme(schema: &str) -> Result<String, SourceFailure> {
gsettings_icon_theme_from(
schema,
crate::detect::run_with_timeout(
"gsettings",
&["get", schema, "icon-theme"],
crate::detect::SUBPROCESS_TIMEOUT,
),
)
}
#[cfg(target_os = "linux")]
fn gsettings_icon_theme_from(
schema: &str,
output: std::io::Result<std::process::Output>,
) -> Result<String, SourceFailure> {
command_icon_theme(
format!("`gsettings get {schema} icon-theme`"),
output,
|printed| printed.trim().trim_matches('\''),
)
}
#[cfg(target_os = "linux")]
const XFCONF_ICON_THEME_ARGS: [&str; 4] = ["-c", "xsettings", "-p", "/Net/IconThemeName"];
#[cfg(target_os = "linux")]
fn detect_xfce_icon_theme() -> Result<String, SourceFailure> {
xfce_icon_theme_from(crate::detect::run_with_timeout(
"xfconf-query",
&XFCONF_ICON_THEME_ARGS,
crate::detect::SUBPROCESS_TIMEOUT,
))
}
#[cfg(target_os = "linux")]
fn xfce_icon_theme_from(
output: std::io::Result<std::process::Output>,
) -> Result<String, SourceFailure> {
command_icon_theme(
format!("`xfconf-query {}`", XFCONF_ICON_THEME_ARGS.join(" ")),
output,
str::trim,
)
}
#[cfg(target_os = "linux")]
fn command_icon_theme(
source: String,
output: std::io::Result<std::process::Output>,
parse: fn(&str) -> &str,
) -> Result<String, SourceFailure> {
let reason = match output {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => "command not found".to_string(),
Err(e) if e.kind() == std::io::ErrorKind::TimedOut => e.to_string(),
Err(e) => format!("could not run ({e})"),
Ok(output) if !output.status.success() => {
let stderr = String::from_utf8_lossy(&output.stderr);
match stderr.trim() {
"" => format!("unsuccessful ({})", output.status),
stderr => format!("unsuccessful ({}): {stderr}", output.status),
}
}
Ok(output) => match String::from_utf8(output.stdout) {
Err(_) => "printed output that is not UTF-8".to_string(),
Ok(printed) => match parse(&printed) {
"" => "printed no theme name".to_string(),
theme => return Ok(theme.to_string()),
},
},
};
Err(SourceFailure { source, reason })
}
#[cfg(target_os = "linux")]
fn detect_lxqt_icon_theme() -> Result<String, SourceFailure> {
lxqt_icon_theme(crate::detect::xdg_config_home().as_deref(), |path| {
std::fs::read_to_string(path)
})
}
#[cfg(target_os = "linux")]
fn lxqt_icon_theme(
config_dir: Option<&std::path::Path>,
read: impl Fn(&std::path::Path) -> std::io::Result<String>,
) -> Result<String, SourceFailure> {
const SOURCE: &str = "LXQt lxqt.conf";
let config_dir = config_dir.ok_or_else(|| config_dir_unresolved(SOURCE))?;
let path = config_dir.join("lxqt").join("lxqt.conf");
let failure = |reason: String| SourceFailure {
source: SOURCE.to_string(),
reason: format!("{}: {reason}", path.display()),
};
let content = read(&path).map_err(|e| failure(format!("unreadable ({e})")))?;
content
.lines()
.filter_map(|line| line.trim().strip_prefix("icon_theme="))
.map(str::trim)
.find(|value| !value.is_empty())
.map(str::to_string)
.ok_or_else(|| failure("`icon_theme` is absent or empty".to_string()))
}
#[cfg(test)]
#[cfg(target_os = "linux")]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
use std::os::unix::process::ExitStatusExt;
use std::path::{Path, PathBuf};
use std::process::{ExitStatus, Output};
#[test]
fn system_icon_set_returns_freedesktop_on_linux() {
assert_eq!(system_icon_set(), IconSet::Freedesktop);
}
fn missing(_: &Path) -> std::io::Result<String> {
Err(std::io::Error::from(std::io::ErrorKind::NotFound))
}
fn printed(stdout: &str) -> std::io::Result<Output> {
Ok(Output {
status: ExitStatus::from_raw(0),
stdout: stdout.as_bytes().to_vec(),
stderr: Vec::new(),
})
}
fn exited_1(stderr: &str) -> std::io::Result<Output> {
Ok(Output {
status: ExitStatus::from_raw(1 << 8),
stdout: Vec::new(),
stderr: stderr.as_bytes().to_vec(),
})
}
fn not_found() -> std::io::Result<Output> {
Err(std::io::Error::from(std::io::ErrorKind::NotFound))
}
fn timed_out() -> std::io::Result<Output> {
Err(std::io::Error::new(
std::io::ErrorKind::TimedOut,
"timed out after 2s",
))
}
#[cfg(not(feature = "kde"))]
#[test]
fn kde_without_a_config_dir_names_kdeglobals_and_the_config_dir() {
let failure = kde_icon_theme(None, missing).unwrap_err().to_string();
assert!(failure.contains("kdeglobals"), "got: {failure}");
assert!(failure.contains("config directory"), "got: {failure}");
assert!(failure.contains("XDG_CONFIG_HOME"), "got: {failure}");
}
#[cfg(not(feature = "kde"))]
#[test]
fn kde_with_no_readable_file_names_both_files() {
let dir = PathBuf::from("/cfg");
let failure = kde_icon_theme(Some(&dir), missing).unwrap_err().to_string();
assert!(failure.contains("/cfg/kdeglobals"), "got: {failure}");
assert!(
failure.contains("/cfg/kdedefaults/kdeglobals"),
"got: {failure}"
);
assert!(failure.contains("unreadable"), "got: {failure}");
}
#[cfg(not(feature = "kde"))]
#[test]
fn kde_with_no_icons_theme_key_says_the_key_is_absent() {
let dir = PathBuf::from("/cfg");
let failure = kde_icon_theme(Some(&dir), |_: &Path| {
Ok("[General]\nTheme=not-this-one\n".to_string())
})
.unwrap_err()
.to_string();
assert!(failure.contains("kdeglobals"), "got: {failure}");
assert!(
failure.contains("`[Icons] Theme` is absent or empty"),
"got: {failure}"
);
}
#[cfg(not(feature = "kde"))]
#[test]
fn kde_takes_the_defaults_file_where_kdeglobals_lacks_the_key() {
let dir = PathBuf::from("/cfg");
let theme = kde_icon_theme(Some(&dir), |path: &Path| {
if path.ends_with("kdedefaults/kdeglobals") {
Ok("[Icons]\nTheme=breeze-dark\n".to_string())
} else {
Ok("[General]\n".to_string())
}
});
assert_eq!(theme, Ok("breeze-dark".to_string()));
}
#[test]
fn gsettings_not_found_names_the_command() {
let failure = gsettings_icon_theme_from("org.gnome.desktop.interface", not_found())
.unwrap_err()
.to_string();
assert!(
failure.contains("gsettings get org.gnome.desktop.interface icon-theme"),
"got: {failure}"
);
assert!(failure.contains("command not found"), "got: {failure}");
}
#[test]
fn gsettings_that_timed_out_says_so() {
let failure = gsettings_icon_theme_from("org.gnome.desktop.interface", timed_out())
.unwrap_err()
.to_string();
assert!(
failure.contains("gsettings get org.gnome.desktop.interface icon-theme"),
"got: {failure}"
);
assert!(failure.contains("timed out after 2s"), "got: {failure}");
assert!(!failure.contains("could not run"), "got: {failure}");
}
#[test]
fn gsettings_nonzero_exit_gives_the_status_and_stderr() {
let failure = gsettings_icon_theme_from(
"org.mate.interface",
exited_1("No such schema “org.mate.interface”\n"),
)
.unwrap_err()
.to_string();
assert!(
failure.contains("gsettings get org.mate.interface icon-theme"),
"got: {failure}"
);
assert!(
failure.contains("unsuccessful (exit status: 1)"),
"got: {failure}"
);
assert!(failure.contains("No such schema"), "got: {failure}");
}
#[test]
fn gsettings_empty_output_says_it_printed_no_name() {
let failure = gsettings_icon_theme_from("org.cinnamon.desktop.interface", printed("''\n"))
.unwrap_err()
.to_string();
assert!(
failure.contains("gsettings get org.cinnamon.desktop.interface icon-theme"),
"got: {failure}"
);
assert!(failure.contains("printed no theme name"), "got: {failure}");
}
#[test]
fn gsettings_output_is_unquoted() {
assert_eq!(
gsettings_icon_theme_from("org.gnome.desktop.interface", printed("'Adwaita'\n")),
Ok("Adwaita".to_string())
);
}
#[test]
fn xfconf_not_found_names_the_command() {
let failure = xfce_icon_theme_from(not_found()).unwrap_err().to_string();
assert!(
failure.contains("xfconf-query -c xsettings -p /Net/IconThemeName"),
"got: {failure}"
);
assert!(failure.contains("command not found"), "got: {failure}");
}
#[test]
fn xfconf_nonzero_exit_gives_the_status() {
let failure = xfce_icon_theme_from(exited_1("")).unwrap_err().to_string();
assert!(failure.contains("xfconf-query"), "got: {failure}");
assert!(
failure.contains("unsuccessful (exit status: 1)"),
"got: {failure}"
);
}
#[test]
fn xfconf_empty_output_says_it_printed_no_name() {
let failure = xfce_icon_theme_from(printed("\n")).unwrap_err().to_string();
assert!(failure.contains("xfconf-query"), "got: {failure}");
assert!(failure.contains("printed no theme name"), "got: {failure}");
}
#[test]
fn xfconf_output_is_trimmed() {
assert_eq!(
xfce_icon_theme_from(printed("elementary\n")),
Ok("elementary".to_string())
);
}
#[test]
fn lxqt_without_a_config_dir_names_lxqt_conf_and_the_config_dir() {
let failure = lxqt_icon_theme(None, missing).unwrap_err().to_string();
assert!(failure.contains("lxqt.conf"), "got: {failure}");
assert!(failure.contains("config directory"), "got: {failure}");
}
#[test]
fn lxqt_unreadable_file_names_the_file() {
let dir = PathBuf::from("/cfg");
let failure = lxqt_icon_theme(Some(&dir), missing)
.unwrap_err()
.to_string();
assert!(failure.contains("/cfg/lxqt/lxqt.conf"), "got: {failure}");
assert!(failure.contains("unreadable"), "got: {failure}");
}
#[test]
fn lxqt_without_the_key_says_the_key_is_absent() {
let dir = PathBuf::from("/cfg");
let failure = lxqt_icon_theme(Some(&dir), |_: &Path| Ok("theme=frost\n".to_string()))
.unwrap_err()
.to_string();
assert!(failure.contains("/cfg/lxqt/lxqt.conf"), "got: {failure}");
assert!(
failure.contains("`icon_theme` is absent or empty"),
"got: {failure}"
);
}
#[test]
fn lxqt_reads_the_icon_theme_key() {
let dir = PathBuf::from("/cfg");
assert_eq!(
lxqt_icon_theme(Some(&dir), |_: &Path| Ok(
"[General]\nicon_theme=Papirus\n".to_string()
)),
Ok("Papirus".to_string())
);
}
fn kde_failed() -> Result<String, SourceFailure> {
Err(SourceFailure {
source: KDE_SOURCE.to_string(),
reason: "no file is readable".to_string(),
})
}
#[test]
fn unknown_desktop_error_names_both_attempts() {
let error = unknown_desktop_icon_theme(kde_failed, || {
gsettings_icon_theme_from("org.gnome.desktop.interface", not_found())
})
.unwrap_err()
.to_error();
let message = error.to_string();
assert!(message.contains("kdeglobals"), "got: {message}");
assert!(
message.contains("gsettings get org.gnome.desktop.interface icon-theme"),
"got: {message}"
);
assert!(
matches!(
error,
crate::Error::ReaderFailed {
reader: crate::error::reader_names::ICON_THEME,
..
}
),
"got: {error:?}"
);
}
#[test]
fn unknown_desktop_takes_kde_first() {
let theme = unknown_desktop_icon_theme(
|| Ok("breeze".to_string()),
|| gsettings_icon_theme_from("org.gnome.desktop.interface", printed("'Adwaita'")),
);
assert_eq!(theme, Ok("breeze".to_string()));
}
#[test]
fn unknown_desktop_takes_gnome_where_kde_fails() {
let theme = unknown_desktop_icon_theme(kde_failed, || {
gsettings_icon_theme_from("org.gnome.desktop.interface", printed("'Adwaita'"))
});
assert_eq!(theme, Ok("Adwaita".to_string()));
}
fn source_of(
portal_backend: Option<crate::detect::LinuxDesktop>,
kdeglobals: bool,
) -> IconThemeSource {
icon_theme_source(crate::detect::refine_linux_desktop(
crate::detect::LinuxDesktop::Unknown,
portal_backend,
|| kdeglobals,
))
}
#[test]
fn an_unknown_desktop_with_a_kde_portal_has_kdes_icon_theme() {
let kde = Some(crate::detect::LinuxDesktop::Kde);
assert_eq!(source_of(kde, false), IconThemeSource::Kde);
assert_eq!(source_of(kde, true), IconThemeSource::Kde);
}
#[test]
fn an_unknown_desktop_with_a_gnome_portal_has_gnomes_icon_theme() {
let gnome = Some(crate::detect::LinuxDesktop::Gnome);
let gsettings = IconThemeSource::Gsettings(GNOME_INTERFACE_SCHEMA);
assert_eq!(source_of(gnome, true), gsettings);
assert_eq!(source_of(gnome, false), gsettings);
}
#[test]
fn an_unknown_desktop_without_a_portal_answer() {
assert_eq!(source_of(None, true), IconThemeSource::Kde);
assert_eq!(source_of(None, false), IconThemeSource::KdeThenGnome);
}
#[test]
fn system_icon_theme_is_a_name_or_the_reason_for_none() {
match system_icon_theme() {
Ok(theme) => assert!(!theme.is_empty(), "detection named an empty theme"),
Err(e) => assert!(!e.to_string().is_empty(), "a failure gave no reason"),
}
}
}