feat: improve cache, add ScaledDirectories lookup and parent lookup
This commit is contained in:
parent
bc903c1bcb
commit
73702f6049
3 changed files with 133 additions and 37 deletions
|
|
@ -18,8 +18,8 @@ pub static THEMES: Lazy<BTreeMap<String, Theme>> =
|
|||
Lazy::new(|| get_all_themes().expect("Failed to get theme paths"));
|
||||
|
||||
pub struct Theme {
|
||||
path: ThemePath,
|
||||
index: Ini,
|
||||
pub path: ThemePath,
|
||||
pub index: Ini,
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
|
|
@ -30,18 +30,15 @@ impl Theme {
|
|||
|
||||
fn try_get_icon_exact_size(&self, name: &str, size: u16, scale: u16) -> Option<PathBuf> {
|
||||
self.match_size(size, scale)
|
||||
.iter()
|
||||
.find_map(|path| try_build_icon_path(name, path))
|
||||
}
|
||||
|
||||
fn match_size(&self, size: u16, scale: u16) -> Vec<PathBuf> {
|
||||
fn match_size(&self, size: u16, scale: u16) -> impl Iterator<Item = PathBuf> + '_ {
|
||||
let dirs = self.get_all_directories();
|
||||
|
||||
dirs.iter()
|
||||
.filter(|directory| directory.match_size(size, scale))
|
||||
dirs.filter(move |directory| directory.match_size(size, scale))
|
||||
.map(|dir| dir.name)
|
||||
.map(|dir| self.path().join(dir))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn try_get_icon_closest_size(&self, name: &str, size: u16, scale: u16) -> Option<PathBuf> {
|
||||
|
|
@ -53,8 +50,7 @@ impl Theme {
|
|||
fn closest_match_size(&self, size: u16, scale: u16) -> Vec<PathBuf> {
|
||||
let dirs = self.get_all_directories();
|
||||
|
||||
dirs.iter()
|
||||
.filter(|directory| directory.directory_size_distance(size, scale) < i16::MAX)
|
||||
dirs.filter(|directory| directory.directory_size_distance(size, scale) < i16::MAX)
|
||||
.map(|dir| dir.name)
|
||||
.map(|dir| self.path().join(dir))
|
||||
.collect()
|
||||
|
|
@ -100,18 +96,6 @@ pub(super) fn get_all_themes() -> Result<BTreeMap<String, Theme>> {
|
|||
Ok(icon_themes)
|
||||
}
|
||||
|
||||
pub fn theme_names() -> Vec<&'static str> {
|
||||
THEMES
|
||||
.values()
|
||||
.map(|path| &path.index)
|
||||
.filter_map(|index| {
|
||||
index
|
||||
.section(Some("Icon Theme"))
|
||||
.and_then(|section| section.get("Name"))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
fn from_path<P: AsRef<Path>>(path: P) -> Option<Self> {
|
||||
let path = path.as_ref();
|
||||
|
|
|
|||
|
|
@ -3,11 +3,15 @@ use crate::theme::Theme;
|
|||
use ini::Properties;
|
||||
|
||||
impl Theme {
|
||||
pub(super) fn get_all_directories(&self) -> Vec<Directory> {
|
||||
pub(super) fn get_all_directories(&self) -> impl Iterator<Item = Directory> {
|
||||
self.directories()
|
||||
.iter()
|
||||
.into_iter()
|
||||
.filter_map(|name| self.get_directory(name))
|
||||
.collect()
|
||||
.chain(
|
||||
self.scaled_directories()
|
||||
.into_iter()
|
||||
.filter_map(|name| self.get_directory(name)),
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: use me
|
||||
|
|
@ -22,9 +26,11 @@ impl Theme {
|
|||
self.index.section(Some("Icon Theme"))
|
||||
}
|
||||
|
||||
pub fn inherits(&self) -> Option<&str> {
|
||||
pub fn inherits(&self) -> Vec<&str> {
|
||||
self.get_icon_theme_section()
|
||||
.and_then(|props| props.get("Inherits"))
|
||||
.map(|parents| parents.split(',').collect())
|
||||
.unwrap_or(vec![])
|
||||
}
|
||||
|
||||
fn directories(&self) -> Vec<&str> {
|
||||
|
|
@ -69,3 +75,23 @@ impl Theme {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::THEMES;
|
||||
use speculoos::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn should_get_theme_parents() {
|
||||
let theme = THEMES.get("Arc").unwrap();
|
||||
let parents = theme.inherits();
|
||||
assert_that!(parents).is_equal_to(vec![
|
||||
"Moka",
|
||||
"Faba",
|
||||
"elementary",
|
||||
"Adwaita",
|
||||
"gnome",
|
||||
"hicolor",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue