From 39408c71c98bbf5047f21fae47e1b12811553790 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Tue, 23 Jan 2024 13:21:49 -0500 Subject: [PATCH] fix: use the first discovered index.theme from base directories for each theme --- src/lib.rs | 14 +++++--------- src/theme/mod.rs | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4c3fe01..70e773a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,7 +51,7 @@ //! .find(); //! # } //! ``` -use theme::{Theme, BASE_PATHS}; +use theme::BASE_PATHS; use crate::cache::{CacheEntry, CACHE}; use crate::theme::{try_build_icon_path, THEMES}; @@ -257,15 +257,11 @@ impl<'a> LookupBuilder<'a> { }) }) .or_else(|| { - for theme_base_dir in BASE_PATHS.iter() { - let theme = Theme::from_path(theme_base_dir.join("hicolor")); - if let Some(icon) = theme.and_then(|theme| { + THEMES.get("hicolor").and_then(|icon_themes| { + icon_themes.iter().find_map(|theme| { theme.try_get_icon(self.name, self.size, self.scale, self.force_svg) - }) { - return Some(icon); - } - } - None + }) + }) }) .or_else(|| { for theme_base_dir in BASE_PATHS.iter() { diff --git a/src/theme/mod.rs b/src/theme/mod.rs index c8373ab..a28a221 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -142,23 +142,43 @@ fn try_build_xmp>(name: &str, path: P) -> Option { // Iter through the base paths and get all theme directories pub(super) fn get_all_themes() -> Result>> { let mut icon_themes = BTreeMap::<_, Vec<_>>::new(); + let mut found_indices = BTreeMap::new(); + let mut to_revisit = Vec::new(); + for theme_base_dir in BASE_PATHS.iter() { for entry in theme_base_dir.read_dir()? { let entry = entry?; - if let Some(theme) = Theme::from_path(entry.path()) { - let name = entry.file_name().to_string_lossy().to_string(); + let name = entry.file_name(); + let fallback_index = found_indices.get(&name); + if let Some(theme) = Theme::from_path(entry.path(), fallback_index) { + if fallback_index.is_none() { + found_indices.insert(name.clone(), theme.index.clone()); + } + let name = name.to_string_lossy().to_string(); icon_themes.entry(name).or_default().push(theme); + } else if entry.path().is_dir() { + to_revisit.push(entry); } } } + + for entry in to_revisit { + let name = entry.file_name(); + let fallback_index = found_indices.get(&name); + if let Some(theme) = Theme::from_path(entry.path(), fallback_index) { + let name = name.to_string_lossy().to_string(); + icon_themes.entry(name).or_default().push(theme); + } + } + Ok(icon_themes) } impl Theme { - pub(crate) fn from_path>(path: P) -> Option { + pub(crate) fn from_path>(path: P, index: Option<&Ini>) -> Option { let path = path.as_ref(); - let has_index = path.join("index.theme").exists(); + let has_index = path.join("index.theme").exists() || index.is_some(); if !has_index || !path.is_dir() { return None; @@ -166,9 +186,13 @@ impl Theme { let path = ThemePath(path.into()); - match path.index() { - Ok(index) => Some(Theme { path, index }), - Err(_) => None, + match (index, path.index()) { + (Some(index), _) => Some(Theme { + path, + index: index.clone(), + }), + (None, Ok(index)) => Some(Theme { path, index }), + _ => None, } } }