2022-11-22 15:54:14 -05:00
|
|
|
use dirs::home_dir;
|
2025-04-02 17:21:11 +02:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::sync::LazyLock;
|
2022-11-22 17:49:56 -05:00
|
|
|
use xdg::BaseDirectories;
|
2022-05-12 14:41:08 +02:00
|
|
|
|
2024-01-24 16:54:45 +01:00
|
|
|
use crate::theme;
|
|
|
|
|
use crate::theme::error::ThemeError;
|
|
|
|
|
|
2025-04-02 17:21:11 +02:00
|
|
|
pub(crate) static BASE_PATHS: LazyLock<Vec<PathBuf>> = LazyLock::new(icon_theme_base_paths);
|
2022-05-12 14:41:08 +02:00
|
|
|
|
2024-03-18 10:21:33 -07:00
|
|
|
/// Look in $HOME/.icons (for backwards compatibility), in $XDG_DATA_DIRS/icons, in $XDG_DATA_DIRS/pixmaps and in /usr/share/pixmaps (in that order).
|
2022-05-12 14:41:08 +02:00
|
|
|
/// Paths that are not found are filtered out.
|
|
|
|
|
fn icon_theme_base_paths() -> Vec<PathBuf> {
|
2025-11-06 16:08:48 +01:00
|
|
|
let base_dirs = BaseDirectories::new();
|
|
|
|
|
let mut data_dirs: Vec<_> = base_dirs
|
|
|
|
|
.get_data_dirs()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.flat_map(|p| [p.join("icons"), p.join("pixmaps")])
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
if let Some(data_home) = base_dirs.get_data_home() {
|
|
|
|
|
data_dirs.push(data_home.join("icons"));
|
|
|
|
|
data_dirs.push(data_home.join("pixmaps"));
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 11:18:33 +01:00
|
|
|
match home_dir().map(|home| home.join(".icons")) {
|
|
|
|
|
Some(home_icon_dir) => data_dirs.push(home_icon_dir),
|
|
|
|
|
None => tracing::warn!("No $HOME directory found"),
|
|
|
|
|
}
|
2022-11-22 17:49:56 -05:00
|
|
|
data_dirs.into_iter().filter(|p| p.exists()).collect()
|
2022-05-12 14:41:08 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-23 11:18:33 +01:00
|
|
|
#[derive(Clone, Debug)]
|
2022-05-12 14:41:08 +02:00
|
|
|
pub struct ThemePath(pub PathBuf);
|
|
|
|
|
|
|
|
|
|
impl ThemePath {
|
2025-01-23 11:18:33 +01:00
|
|
|
pub(super) fn index(&self) -> theme::Result<PathBuf> {
|
2022-05-12 14:41:08 +02:00
|
|
|
let index = self.0.join("index.theme");
|
|
|
|
|
|
|
|
|
|
if !index.exists() {
|
|
|
|
|
return Err(ThemeError::ThemeIndexNotFound(index));
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 11:18:33 +01:00
|
|
|
Ok(index)
|
2022-05-12 14:41:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use crate::theme::paths::icon_theme_base_paths;
|
2025-11-06 16:08:48 +01:00
|
|
|
use crate::theme::{Theme, get_all_themes};
|
2022-05-12 14:41:08 +02:00
|
|
|
use speculoos::prelude::*;
|
|
|
|
|
|
2022-05-12 15:34:02 +02:00
|
|
|
#[test]
|
|
|
|
|
fn should_get_all_themes() {
|
2025-01-23 11:09:45 +01:00
|
|
|
let themes = get_all_themes();
|
2022-05-12 15:34:02 +02:00
|
|
|
assert_that!(themes.get("hicolor")).is_some();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-12 14:41:08 +02:00
|
|
|
#[test]
|
|
|
|
|
fn should_get_theme_paths_ordered() {
|
|
|
|
|
let base_paths = icon_theme_base_paths();
|
|
|
|
|
assert_that!(base_paths).is_not_empty()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2025-01-23 11:18:33 +01:00
|
|
|
fn should_read_theme_index() {
|
2025-01-23 11:09:45 +01:00
|
|
|
let themes = get_all_themes();
|
2023-01-09 23:54:07 +01:00
|
|
|
let themes: Vec<&Theme> = themes.values().flatten().collect();
|
2022-05-12 14:41:08 +02:00
|
|
|
assert_that!(themes).is_not_empty();
|
|
|
|
|
}
|
|
|
|
|
}
|