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
|
|
|
|
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();
|
2025-11-25 06:24:34 +01:00
|
|
|
|
|
|
|
|
let data_dirs = base_dirs
|
2025-11-06 16:08:48 +01:00
|
|
|
.get_data_dirs()
|
|
|
|
|
.into_iter()
|
2025-11-25 06:24:34 +01:00
|
|
|
.flat_map(|p| [p.join("icons"), p.join("pixmaps")]);
|
2025-11-06 16:08:48 +01:00
|
|
|
|
2025-11-25 06:24:34 +01:00
|
|
|
let data_home_dirs = base_dirs
|
|
|
|
|
.get_data_home()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.flat_map(|data_home| [data_home.join("icons"), data_home.join("pixmaps")].into_iter());
|
2025-11-06 16:08:48 +01:00
|
|
|
|
2025-11-25 06:24:34 +01:00
|
|
|
let home_dir = home_dir().into_iter().map(|home| home.join(".icons"));
|
|
|
|
|
|
|
|
|
|
data_dirs
|
|
|
|
|
.chain(data_home_dirs)
|
|
|
|
|
.chain(home_dir)
|
|
|
|
|
.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);
|
|
|
|
|
|
|
|
|
|
#[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();
|
|
|
|
|
}
|
|
|
|
|
}
|