fix: search all base path's hicolor theme, or as a bare path as a last resort

This commit is contained in:
Ashley Wulber 2022-11-22 17:49:56 -05:00 • committed by Paul Delafosse
parent aa175c8812
commit 9b68d41b77
3 changed files with 34 additions and 17 deletions

View file

@ -51,6 +51,8 @@
//! .find(); //! .find();
//! # } //! # }
//! ``` //! ```
use theme::{Theme, BASE_PATHS};
use crate::cache::{CacheEntry, CACHE}; use crate::cache::{CacheEntry, CACHE};
use crate::theme::{try_build_icon_path, THEMES}; use crate::theme::{try_build_icon_path, THEMES};
use std::path::PathBuf; use std::path::PathBuf;
@ -242,15 +244,25 @@ impl<'a> LookupBuilder<'a> {
}) })
}) })
.or_else(|| { .or_else(|| {
THEMES for theme_base_dir in BASE_PATHS.iter() {
.get("hicolor") let theme = Theme::from_path(theme_base_dir.join("hicolor"));
// Fallback to 'hicolor' if let Some(icon) = theme.and_then(|theme| {
.and_then(|hicolor| { theme.try_get_icon(self.name, self.size, self.scale, self.force_svg)
hicolor.try_get_icon(self.name, self.size, self.scale, self.force_svg) }) {
}) return Some(icon);
}
}
None
}) })
// Last chance, try to find the icon in "/usr/share/pixmaps" .or_else(|| try_build_icon_path(self.name, "/usr/share/pixmaps", self.force_svg))
.or_else(|| try_build_icon_path(self.name, "/usr/share/pixmaps", self.force_svg)); .or_else(|| {
let p = PathBuf::from(&self.name);
if let (Some(name), Some(parent)) = (p.file_name(), p.parent()) {
try_build_icon_path(&name.to_string_lossy(), parent, self.force_svg)
} else {
None
}
});
if self.cache { if self.cache {
self.store(self.theme, icon) self.store(self.theme, icon)

View file

@ -2,7 +2,7 @@ use crate::theme::error::ThemeError;
use crate::theme::paths::ThemePath; use crate::theme::paths::ThemePath;
use ini::Ini; use ini::Ini;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use paths::BASE_PATHS; pub(crate) use paths::BASE_PATHS;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -96,6 +96,7 @@ pub(super) fn try_build_icon_path<P: AsRef<Path>>(
fn try_build_svg<P: AsRef<Path>>(name: &str, path: P) -> Option<PathBuf> { fn try_build_svg<P: AsRef<Path>>(name: &str, path: P) -> Option<PathBuf> {
let path = path.as_ref(); let path = path.as_ref();
let svg = path.join(format!("{name}.svg")); let svg = path.join(format!("{name}.svg"));
if svg.exists() { if svg.exists() {
Some(svg) Some(svg)
} else { } else {
@ -106,6 +107,7 @@ fn try_build_svg<P: AsRef<Path>>(name: &str, path: P) -> Option<PathBuf> {
fn try_build_png<P: AsRef<Path>>(name: &str, path: P) -> Option<PathBuf> { fn try_build_png<P: AsRef<Path>>(name: &str, path: P) -> Option<PathBuf> {
let path = path.as_ref(); let path = path.as_ref();
let png = path.join(format!("{name}.png")); let png = path.join(format!("{name}.png"));
if png.exists() { if png.exists() {
Some(png) Some(png)
} else { } else {
@ -139,7 +141,7 @@ pub(super) fn get_all_themes() -> Result<BTreeMap<String, Theme>> {
} }
impl Theme { impl Theme {
fn from_path<P: AsRef<Path>>(path: P) -> Option<Self> { pub(crate) fn from_path<P: AsRef<Path>>(path: P) -> Option<Self> {
let path = path.as_ref(); let path = path.as_ref();
let has_index = path.join("index.theme").exists(); let has_index = path.join("index.theme").exists();

View file

@ -3,8 +3,8 @@ use crate::theme::error::ThemeError;
use dirs::home_dir; use dirs::home_dir;
use ini::Ini; use ini::Ini;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use xdg::BaseDirectories;
use std::path::PathBuf; use std::path::PathBuf;
use xdg::BaseDirectories;
pub(crate) static BASE_PATHS: Lazy<Vec<PathBuf>> = Lazy::new(icon_theme_base_paths); pub(crate) static BASE_PATHS: Lazy<Vec<PathBuf>> = Lazy::new(icon_theme_base_paths);
@ -12,13 +12,16 @@ pub(crate) static BASE_PATHS: Lazy<Vec<PathBuf>> = Lazy::new(icon_theme_base_pat
/// Paths that are not found are filtered out. /// Paths that are not found are filtered out.
fn icon_theme_base_paths() -> Vec<PathBuf> { fn icon_theme_base_paths() -> Vec<PathBuf> {
let home_icon_dir = home_dir().expect("No $HOME directory").join(".icons"); let home_icon_dir = home_dir().expect("No $HOME directory").join(".icons");
let mut data_dirs = BaseDirectories::new().map(|bd| bd.get_data_dirs()).unwrap_or_default(); let mut data_dirs: Vec<_> = BaseDirectories::new()
.map(|bd| {
bd.get_data_dirs()
.into_iter()
.map(|p| p.join("icons"))
.collect()
})
.unwrap_or_default();
data_dirs.push(home_icon_dir); data_dirs.push(home_icon_dir);
data_dirs.into_iter().filter(|p| p.exists()).collect()
data_dirs
.into_iter()
.filter(|p| p.exists())
.collect()
} }
#[derive(Debug)] #[derive(Debug)]