Load icons using mime type

This commit is contained in:
Jeremy Soller 2023-11-13 10:33:26 -07:00
parent 1151c64323
commit f76f83565c
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
6 changed files with 72 additions and 15 deletions

17
src/mime_icon.rs Normal file
View file

@ -0,0 +1,17 @@
use cosmic::widget::icon;
use std::path::Path;
pub const FALLBACK_MIME_ICON: &str = "text-x-generic";
pub fn mime_icon<P: AsRef<Path>>(path: P, size: u16) -> icon::Icon {
let path = path.as_ref();
for mime in mime_guess::from_path(path).iter() {
//TODO: correct some common issues (like application/x-sh not being found)
let icon_name = mime.essence_str().replace("/", "-");
let named = icon::from_name(icon_name).size(size);
if named.clone().path().is_some() {
return named.icon();
}
}
icon::from_name(FALLBACK_MIME_ICON).size(size).icon()
}