feat: add a function to force svg lookup
This commit is contained in:
parent
51eebd05f1
commit
c737c3f6a4
2 changed files with 89 additions and 27 deletions
|
|
@ -23,17 +23,17 @@ pub struct Theme {
|
|||
}
|
||||
|
||||
impl Theme {
|
||||
pub fn try_get_icon(&self, name: &str, size: u16, scale: u16) -> Option<PathBuf> {
|
||||
self.try_get_icon_exact_size(name, size, scale)
|
||||
.or_else(|| self.try_get_icon_closest_size(name, size, scale))
|
||||
pub fn try_get_icon(&self, name: &str, size: u16, 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))
|
||||
}
|
||||
|
||||
fn try_get_icon_exact_size(&self, name: &str, size: u16, scale: u16) -> Option<PathBuf> {
|
||||
fn try_get_icon_exact_size(&self, name: &str, size: u16, scale: u16, force_svg: bool) -> Option<PathBuf> {
|
||||
self.match_size(size, scale)
|
||||
.find_map(|path| try_build_icon_path(name, path))
|
||||
.find_map(|path| try_build_icon_path(name, path, force_svg))
|
||||
}
|
||||
|
||||
fn match_size(&self, size: u16, scale: u16) -> impl Iterator<Item = PathBuf> + '_ {
|
||||
fn match_size(&self, size: u16, scale: u16) -> impl Iterator<Item=PathBuf> + '_ {
|
||||
let dirs = self.get_all_directories();
|
||||
|
||||
dirs.filter(move |directory| directory.match_size(size, scale))
|
||||
|
|
@ -41,10 +41,10 @@ impl Theme {
|
|||
.map(|dir| self.path().join(dir))
|
||||
}
|
||||
|
||||
fn try_get_icon_closest_size(&self, name: &str, size: u16, scale: u16) -> Option<PathBuf> {
|
||||
fn try_get_icon_closest_size(&self, name: &str, size: u16, scale: u16, force_svg: bool) -> Option<PathBuf> {
|
||||
self.closest_match_size(size, scale)
|
||||
.iter()
|
||||
.find_map(|path| try_build_icon_path(name, path))
|
||||
.find_map(|path| try_build_icon_path(name, path, force_svg))
|
||||
}
|
||||
|
||||
fn closest_match_size(&self, size: u16, scale: u16) -> Vec<PathBuf> {
|
||||
|
|
@ -61,26 +61,48 @@ impl Theme {
|
|||
}
|
||||
}
|
||||
|
||||
pub(super) fn try_build_icon_path<P: AsRef<Path>>(name: &str, path: P) -> Option<PathBuf> {
|
||||
pub(super) fn try_build_icon_path<P: AsRef<Path>>(name: &str, path: P, force_svg: bool) -> Option<PathBuf> {
|
||||
if force_svg {
|
||||
try_build_svg(name, path.as_ref())
|
||||
} else {
|
||||
try_build_png(name, path.as_ref())
|
||||
.or(try_build_svg(name, path.as_ref()))
|
||||
.or(try_build_xmp(name, path.as_ref()))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fn try_build_svg<P: AsRef<Path>>(name: &str, path: P) -> Option<PathBuf> {
|
||||
let path = path.as_ref();
|
||||
let svg = path.join(format!("{name}.svg"));
|
||||
if svg.exists() {
|
||||
Some(svg)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn try_build_png<P: AsRef<Path>>(name: &str, path: P) -> Option<PathBuf> {
|
||||
let path = path.as_ref();
|
||||
let png = path.join(format!("{name}.png"));
|
||||
if png.exists() {
|
||||
return Some(png);
|
||||
Some(png)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
let svg = path.join(format!("{name}.svg"));
|
||||
if svg.exists() {
|
||||
return Some(svg);
|
||||
}
|
||||
let xmp = path.join(format!("{name}.xmp"));
|
||||
|
||||
if xmp.exists() {
|
||||
return Some(xmp);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn try_build_xmp<P: AsRef<Path>>(name: &str, path: P) -> Option<PathBuf> {
|
||||
let path = path.as_ref();
|
||||
let xmp = path.join(format!("{name}.xmp"));
|
||||
if xmp.exists() {
|
||||
Some(xmp)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Iter through the base paths and get all theme directories
|
||||
pub(super) fn get_all_themes() -> Result<BTreeMap<String, Theme>> {
|
||||
let mut icon_themes = BTreeMap::new();
|
||||
|
|
@ -126,6 +148,8 @@ impl Debug for Theme {
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::path::PathBuf;
|
||||
use speculoos::prelude::*;
|
||||
use crate::THEMES;
|
||||
|
||||
#[test]
|
||||
|
|
@ -133,7 +157,25 @@ mod test {
|
|||
let theme = THEMES.get("Adwaita").unwrap();
|
||||
println!(
|
||||
"{:?}",
|
||||
theme.try_get_icon_exact_size("edit-delete-symbolic", 24, 1)
|
||||
theme.try_get_icon_exact_size("edit-delete-symbolic", 24, 1, false)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_get_png_first() {
|
||||
let theme = THEMES.get("hicolor").unwrap();
|
||||
let icon = theme.try_get_icon_exact_size("blueman", 24, 1, true);
|
||||
assert_that!(icon)
|
||||
.is_some()
|
||||
.is_equal_to(PathBuf::from("/usr/share/icons/hicolor/scalable/apps/blueman.svg"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_get_svg_first() {
|
||||
let theme = THEMES.get("hicolor").unwrap();
|
||||
let icon = theme.try_get_icon_exact_size("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