Switched to freedesktop-icons

This commit is contained in:
Nicola Guerrera
2023-05-14 22:49:45 +02:00
parent 70f7e39f05
commit 3c8deadfed
4 changed files with 95 additions and 136 deletions

169
Cargo.lock generated
View File

@@ -23,12 +23,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "ahash"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e"
[[package]] [[package]]
name = "ahash" name = "ahash"
version = "0.7.6" version = "0.7.6"
@@ -547,7 +541,16 @@ version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35"
dependencies = [ dependencies = [
"dirs-sys", "dirs-sys 0.4.1",
]
[[package]]
name = "dirs"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys 0.3.7",
] ]
[[package]] [[package]]
@@ -556,7 +559,18 @@ version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
dependencies = [ dependencies = [
"dirs-sys", "dirs-sys 0.4.1",
]
[[package]]
name = "dirs-sys"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [
"libc",
"redox_users",
"winapi",
] ]
[[package]] [[package]]
@@ -573,12 +587,9 @@ dependencies = [
[[package]] [[package]]
name = "dlv-list" name = "dlv-list"
version = "0.2.3" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68df3f2b690c1b86e65ef7830956aededf3cb0a16f898f79b9a6f421a7b6211b" checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257"
dependencies = [
"rand 0.8.5",
]
[[package]] [[package]]
name = "either" name = "either"
@@ -663,15 +674,6 @@ dependencies = [
"simd-adler32", "simd-adler32",
] ]
[[package]]
name = "file-locker"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91c8a37357ab5d51f1a8053900fe8374283449695c5418b66f45deece50705f"
dependencies = [
"nix 0.25.1",
]
[[package]] [[package]]
name = "flagset" name = "flagset"
version = "0.4.3" version = "0.4.3"
@@ -761,13 +763,16 @@ dependencies = [
] ]
[[package]] [[package]]
name = "freedesktop_entry_parser" name = "freedesktop-icons"
version = "1.3.0" version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db9c27b72f19a99a895f8ca89e2d26e4ef31013376e56fdafef697627306c3e4" checksum = "00e61ac115df4632b592d36b71fda3c259f4c8061c70b7fa429bac145890e880"
dependencies = [ dependencies = [
"nom", "dirs 4.0.0",
"once_cell",
"rust-ini",
"thiserror", "thiserror",
"xdg",
] ]
[[package]] [[package]]
@@ -890,20 +895,14 @@ dependencies = [
"crunchy", "crunchy",
] ]
[[package]]
name = "hashbrown"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
dependencies = [
"ahash 0.4.7",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.12.3" version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
dependencies = [
"ahash",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
@@ -938,6 +937,15 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "home"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb"
dependencies = [
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.56" version = "0.1.56"
@@ -1101,29 +1109,6 @@ version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "linicon"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ee8c5653188a809616c97296180a0547a61dba205bcdcbdd261dbd022a25fd9"
dependencies = [
"file-locker",
"freedesktop_entry_parser",
"linicon-theme",
"memmap2",
"thiserror",
]
[[package]]
name = "linicon-theme"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4f8240c33bb08c5d8b8cdea87b683b05e61037aa76ff26bef40672cc6ecbb80"
dependencies = [
"freedesktop_entry_parser",
"rust-ini",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.3.7" version = "0.3.7"
@@ -1194,15 +1179,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "memoffset"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "memoffset" name = "memoffset"
version = "0.7.1" version = "0.7.1"
@@ -1221,12 +1197,6 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.6.2" version = "0.6.2"
@@ -1285,20 +1255,6 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "nix"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f346ff70e7dbfd675fe90590b92d59ef2de15a8779ae305ebcbfd3f0caf59be4"
dependencies = [
"autocfg",
"bitflags",
"cfg-if",
"libc",
"memoffset 0.6.5",
"pin-utils",
]
[[package]] [[package]]
name = "nix" name = "nix"
version = "0.26.2" version = "0.26.2"
@@ -1313,16 +1269,6 @@ dependencies = [
"static_assertions", "static_assertions",
] ]
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]] [[package]]
name = "nu-ansi-term" name = "nu-ansi-term"
version = "0.46.0" version = "0.46.0"
@@ -1417,12 +1363,12 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]] [[package]]
name = "ordered-multimap" name = "ordered-multimap"
version = "0.3.1" version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c672c7ad9ec066e428c00eb917124a06f08db19e2584de982cc34b1f4c12485" checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a"
dependencies = [ dependencies = [
"dlv-list", "dlv-list",
"hashbrown 0.9.1", "hashbrown 0.12.3",
] ]
[[package]] [[package]]
@@ -1616,15 +1562,15 @@ dependencies = [
"clap", "clap",
"color-eyre", "color-eyre",
"directories", "directories",
"dirs", "dirs 5.0.1",
"ez-pixmap", "ez-pixmap",
"freedesktop-icons",
"glam", "glam",
"image", "image",
"lazy_static", "lazy_static",
"linicon",
"manifest-dir-macros", "manifest-dir-macros",
"mint", "mint",
"nix 0.26.2", "nix",
"regex", "regex",
"resvg", "resvg",
"rustc-hash", "rustc-hash",
@@ -1867,9 +1813,9 @@ dependencies = [
[[package]] [[package]]
name = "rust-ini" name = "rust-ini"
version = "0.17.0" version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63471c4aa97a1cf8332a5f97709a79a4234698de6a1f5087faf66f2dae810e22" checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"ordered-multimap", "ordered-multimap",
@@ -2096,7 +2042,7 @@ dependencies = [
"chrono", "chrono",
"cluFlock", "cluFlock",
"color-rs", "color-rs",
"dirs", "dirs 5.0.1",
"mint", "mint",
"parking_lot 0.12.1", "parking_lot 0.12.1",
"rustc-hash", "rustc-hash",
@@ -2529,7 +2475,7 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "371436099f2980de56dc385b615696d3eabbdac9649a72b85f9d75f68474fa9c" checksum = "371436099f2980de56dc385b615696d3eabbdac9649a72b85f9d75f68474fa9c"
dependencies = [ dependencies = [
"ahash 0.7.6", "ahash",
"byteorder", "byteorder",
"lazy_static", "lazy_static",
"parking_lot 0.11.2", "parking_lot 0.11.2",
@@ -2849,6 +2795,15 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "xdg"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "688597db5a750e9cad4511cb94729a078e274308099a0382b5b8203bbc767fee"
dependencies = [
"home",
]
[[package]] [[package]]
name = "xkbcommon" name = "xkbcommon"
version = "0.5.0" version = "0.5.0"

View File

@@ -10,10 +10,10 @@ color-eyre = "0.6.2"
directories = "5.0.0" directories = "5.0.0"
dirs = "5.0.0" dirs = "5.0.0"
ez-pixmap = "0.2.2" ez-pixmap = "0.2.2"
freedesktop-icons = "0.2.3"
glam = { version = "0.24.0", features = ["mint"] } glam = { version = "0.24.0", features = ["mint"] }
image = "0.24.5" image = "0.24.5"
lazy_static = "1.4.0" lazy_static = "1.4.0"
linicon = "2.3.0"
manifest-dir-macros = "0.1.16" manifest-dir-macros = "0.1.16"
mint = "0.5.9" mint = "0.5.9"
nix = "0.26.1" nix = "0.26.1"

View File

@@ -39,7 +39,7 @@ impl Application {
} }
pub fn icon(&self, preferred_px_size: u16, prefer_3d: bool) -> Option<Icon> { pub fn icon(&self, preferred_px_size: u16, prefer_3d: bool) -> Option<Icon> {
let raw_icons = self.desktop_file.get_raw_icons(preferred_px_size); let raw_icons = self.desktop_file.get_icon(preferred_px_size);
let mut icon = raw_icons.iter().max_by_key(|i| i.size).cloned(); let mut icon = raw_icons.iter().max_by_key(|i| i.size).cloned();
if prefer_3d { if prefer_3d {
icon = raw_icons icon = raw_icons

View File

@@ -1,6 +1,6 @@
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use freedesktop_icons::lookup;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use linicon;
use regex::Regex; use regex::Regex;
use resvg::render; use resvg::render;
use resvg::tiny_skia::{Pixmap, Transform}; use resvg::tiny_skia::{Pixmap, Transform};
@@ -232,40 +232,43 @@ pub struct DesktopFile {
pub icon: Option<String>, pub icon: Option<String>,
pub no_display: bool, pub no_display: bool,
} }
const ICON_SIZES: [u16; 7] = [512, 256, 128, 64, 48, 32, 24];
impl DesktopFile { impl DesktopFile {
pub fn get_raw_icons(&self, preferred_px_size: u16) -> Vec<Icon> { pub fn get_icon(&self, preferred_px_size: u16) -> Option<Icon> {
// Get the name of the icon from the DesktopFile struct // Get the name of the icon from the DesktopFile struct
let Some(icon_name) = self.icon.as_ref() else { return Vec::new(); }; let Some(icon_name) = self.icon.as_ref() else { return None };
let test_icon_path = self.path.join(Path::new(icon_name)); let test_icon_path = self.path.join(Path::new(icon_name));
if test_icon_path.exists() { if test_icon_path.exists() {
if let Some(icon) = Icon::from_path(test_icon_path, preferred_px_size) { if let Some(icon) = Icon::from_path(test_icon_path, preferred_px_size) {
return vec![icon]; return Some(icon);
} }
} }
if let Some(cache_icon_path) = IMAGE_CACHE.lock().unwrap().map.get(icon_name) { if let Some(cache_icon_path) = IMAGE_CACHE.lock().unwrap().map.get(icon_name) {
if cache_icon_path.exists() { if cache_icon_path.exists() {
if let Some(icon) = Icon::from_path(cache_icon_path.to_owned(), preferred_px_size) { if let Some(icon) = Icon::from_path(cache_icon_path.to_owned(), preferred_px_size) {
return vec![icon]; return Some(icon);
} }
} }
} }
let mut icons_iter = linicon::lookup_icon(icon_name) // TODO: handle preferred_theme
.use_fallback_themes(false) if let Some(icon_path) = lookup(icon_name).with_size(preferred_px_size).find() {
.peekable(); if let Some(icon) = Icon::from_path(icon_path, preferred_px_size) {
return Some(icon);
if icons_iter.peek().is_none() { }
//dbg!("No icons found in current theme");
icons_iter = linicon::lookup_icon(icon_name).peekable();
} }
let sized_png: Vec<Icon> = icons_iter for icon_size in ICON_SIZES {
.filter_map(|i| i.ok()) if let Some(icon_path) = lookup(icon_name).with_size(icon_size).find() {
.filter(|i| i.icon_type != linicon::IconType::XMP) //TODO: support XMP if let Some(icon) = Icon::from_path(icon_path, preferred_px_size) {
.map(|i| Icon::from_path(i.path, i.max_size - 2).unwrap()) return Some(icon);
.collect(); }
sized_png }
}
None
} }
} }
@@ -341,17 +344,18 @@ fn test_get_icon_path() {
}; };
// Call the get_icon_path() function with a size argument and store the result // Call the get_icon_path() function with a size argument and store the result
let icon_paths = desktop_file.get_raw_icons(32); let icon = desktop_file.get_icon(32);
dbg!(&icon_paths); dbg!(&icon);
// Assert that the get_icon_path() function returns the expected result // Assert that the get_icon_path() function returns the expected result
assert!(icon_paths.contains( assert!(
&Icon::from_path( icon.unwrap()
PathBuf::from("/usr/share/icons/hicolor/32x32/apps/krita.png"), == Icon::from_path(
32 PathBuf::from("/usr/share/icons/hicolor/32x32/apps/krita.png"),
) 32
.unwrap() )
)); .unwrap()
);
} }
pub fn get_image_cache_dir() -> PathBuf { pub fn get_image_cache_dir() -> PathBuf {