improv: significant memory and cpu usage reduction
This commit is contained in:
parent
4578b5fa25
commit
3dd7647411
7 changed files with 200 additions and 127 deletions
|
|
@ -1,10 +1,8 @@
|
|||
use crate::theme::error::ThemeError;
|
||||
use crate::theme::paths::ThemePath;
|
||||
use ini::Ini;
|
||||
use once_cell::sync::Lazy;
|
||||
pub(crate) use paths::BASE_PATHS;
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
mod directories;
|
||||
|
|
@ -16,9 +14,14 @@ type Result<T> = std::result::Result<T, ThemeError>;
|
|||
|
||||
pub static THEMES: Lazy<BTreeMap<String, Vec<Theme>>> = Lazy::new(get_all_themes);
|
||||
|
||||
pub fn read_ini_theme(path: &Path) -> String {
|
||||
std::fs::read_to_string(path).unwrap_or_default()
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Theme {
|
||||
pub path: ThemePath,
|
||||
pub index: Ini,
|
||||
pub index: PathBuf,
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
|
|
@ -29,23 +32,30 @@ impl Theme {
|
|||
scale: u16,
|
||||
force_svg: bool,
|
||||
) -> Option<PathBuf> {
|
||||
self.try_get_icon_exact_size(name, size, scale, force_svg)
|
||||
.or_else(|| self.try_get_icon_closest_size(name, size, scale, force_svg))
|
||||
let file = read_ini_theme(&self.index);
|
||||
self.try_get_icon_exact_size(file.as_str(), name, size, scale, force_svg)
|
||||
.or_else(|| self.try_get_icon_closest_size(file.as_str(), name, size, scale, force_svg))
|
||||
}
|
||||
|
||||
fn try_get_icon_exact_size(
|
||||
&self,
|
||||
file: &str,
|
||||
name: &str,
|
||||
size: u16,
|
||||
scale: u16,
|
||||
force_svg: bool,
|
||||
) -> Option<PathBuf> {
|
||||
self.match_size(size, scale)
|
||||
self.match_size(file, size, scale)
|
||||
.find_map(|path| try_build_icon_path(name, path, force_svg))
|
||||
}
|
||||
|
||||
fn match_size(&self, size: u16, scale: u16) -> impl Iterator<Item = PathBuf> + '_ {
|
||||
let dirs = self.get_all_directories();
|
||||
fn match_size<'a>(
|
||||
&'a self,
|
||||
file: &'a str,
|
||||
size: u16,
|
||||
scale: u16,
|
||||
) -> impl Iterator<Item = PathBuf> + 'a {
|
||||
let dirs = self.get_all_directories(file);
|
||||
|
||||
dirs.filter(move |directory| directory.match_size(size, scale))
|
||||
.map(|dir| dir.name)
|
||||
|
|
@ -54,18 +64,19 @@ impl Theme {
|
|||
|
||||
fn try_get_icon_closest_size(
|
||||
&self,
|
||||
file: &str,
|
||||
name: &str,
|
||||
size: u16,
|
||||
scale: u16,
|
||||
force_svg: bool,
|
||||
) -> Option<PathBuf> {
|
||||
self.closest_match_size(size, scale)
|
||||
self.closest_match_size(file, size, scale)
|
||||
.iter()
|
||||
.find_map(|path| try_build_icon_path(name, path, force_svg))
|
||||
}
|
||||
|
||||
fn closest_match_size(&self, size: u16, scale: u16) -> Vec<PathBuf> {
|
||||
let dirs = self.get_all_directories();
|
||||
fn closest_match_size(&self, file: &str, size: u16, scale: u16) -> Vec<PathBuf> {
|
||||
let dirs = self.get_all_directories(file);
|
||||
|
||||
let mut dirs: Vec<_> = dirs
|
||||
.filter_map(|directory| {
|
||||
|
|
@ -181,7 +192,7 @@ pub(super) fn get_all_themes() -> BTreeMap<String, Vec<Theme>> {
|
|||
}
|
||||
|
||||
impl Theme {
|
||||
pub(crate) fn from_path<P: AsRef<Path>>(path: P, index: Option<&Ini>) -> Option<Self> {
|
||||
pub(crate) fn from_path<P: AsRef<Path>>(path: P, index: Option<&PathBuf>) -> Option<Self> {
|
||||
let path = path.as_ref();
|
||||
|
||||
let has_index = path.join("index.theme").exists() || index.is_some();
|
||||
|
|
@ -203,15 +214,6 @@ impl Theme {
|
|||
}
|
||||
}
|
||||
|
||||
impl Debug for Theme {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
let mut content = vec![];
|
||||
self.index.write_to(&mut content).expect("Write error");
|
||||
let content = String::from_utf8_lossy(&content);
|
||||
writeln!(f, "ThemeIndex{{path: {:?}, index: {content:?}}}", self.path)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::THEMES;
|
||||
|
|
@ -223,21 +225,20 @@ mod test {
|
|||
let themes = THEMES.get("Adwaita").unwrap();
|
||||
println!(
|
||||
"{:?}",
|
||||
themes.iter().find_map(|t| t.try_get_icon_exact_size(
|
||||
"edit-delete-symbolic",
|
||||
24,
|
||||
1,
|
||||
false
|
||||
))
|
||||
themes.iter().find_map(|t| {
|
||||
let file = crate::theme::read_ini_theme(&t.index);
|
||||
t.try_get_icon_exact_size(file.as_str(), "edit-delete-symbolic", 24, 1, false)
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_get_png_first() {
|
||||
let themes = THEMES.get("hicolor").unwrap();
|
||||
let icon = themes
|
||||
.iter()
|
||||
.find_map(|t| t.try_get_icon_exact_size("blueman", 24, 1, true));
|
||||
let icon = themes.iter().find_map(|t| {
|
||||
let file = crate::theme::read_ini_theme(&t.index);
|
||||
t.try_get_icon_exact_size(file.as_str(), "blueman", 24, 1, true)
|
||||
});
|
||||
assert_that!(icon).is_some().is_equal_to(PathBuf::from(
|
||||
"/usr/share/icons/hicolor/scalable/apps/blueman.svg",
|
||||
));
|
||||
|
|
@ -246,9 +247,10 @@ mod test {
|
|||
#[test]
|
||||
fn should_get_svg_first() {
|
||||
let themes = THEMES.get("hicolor").unwrap();
|
||||
let icon = themes
|
||||
.iter()
|
||||
.find_map(|t| t.try_get_icon_exact_size("blueman", 24, 1, false));
|
||||
let icon = themes.iter().find_map(|t| {
|
||||
let file = crate::theme::read_ini_theme(&t.index);
|
||||
t.try_get_icon_exact_size(file.as_str(), "blueman", 24, 1, false)
|
||||
});
|
||||
assert_that!(icon).is_some().is_equal_to(PathBuf::from(
|
||||
"/usr/share/icons/hicolor/22x22/apps/blueman.png",
|
||||
));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue