Use mime icon cache from cosmic-files

This commit is contained in:
Jeremy Soller 2024-03-04 12:34:02 -07:00
parent 8c8e9275a7
commit e337023d8f
No known key found for this signature in database
GPG key ID: D02FD439211AF56F
6 changed files with 239 additions and 914 deletions

1063
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -49,10 +49,6 @@ features = ["multi-window", "tokio", "winit"]
version = "0.2.1"
features = ["serde"]
#TODO: clean up and send changes upstream
[dependencies.systemicons]
git = "https://github.com/jackpot51/systemicons"
[target.'cfg(unix)'.dependencies]
fork = "0.1"

View file

@ -19,7 +19,10 @@ use cosmic::{
widget::{self, button, icon, nav_bar, segmented_button},
Application, ApplicationExt, Apply, Element,
};
use cosmic_files::dialog::{Dialog, DialogKind, DialogMessage, DialogResult};
use cosmic_files::{
dialog::{Dialog, DialogKind, DialogMessage, DialogResult},
mime_icon::{mime_for_path, mime_icon},
};
use cosmic_text::{Cursor, Edit, Family, Selection, SwashCache, SyntaxSystem, ViMode};
use serde::{Deserialize, Serialize};
use std::{
@ -49,9 +52,6 @@ mod line_number;
mod localize;
pub use self::mime_icon::{mime_icon, FALLBACK_MIME_ICON};
mod mime_icon;
use self::menu::menu_bar;
mod menu;
@ -1730,7 +1730,7 @@ impl Application for App {
},
relative_path.display()
);
let icon = mime_icon(&diff.path, 16);
let icon = icon::icon(mime_icon(mime_for_path(&diff.path), 16)).size(16);
let tab = Tab::GitDiff(GitDiffTab { title, diff });
self.tab_model
.insert()

View file

@ -1,58 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-only
use cosmic::widget::icon;
use std::{collections::HashMap, path::Path, sync::Mutex, sync::OnceLock};
pub const FALLBACK_MIME_ICON: &str = "text-x-generic";
#[derive(Debug, Eq, Hash, PartialEq)]
struct MimeIconKey {
path: String,
size: u16,
}
struct MimeIconCache {
cache: HashMap<MimeIconKey, Option<icon::Handle>>,
}
impl MimeIconCache {
pub fn new() -> Self {
Self {
cache: HashMap::new(),
}
}
pub fn get(&mut self, key: MimeIconKey) -> Option<icon::Handle> {
self.cache
.entry(key)
.or_insert_with_key(|key| match systemicons::get_icon(&key.path, key.size) {
Ok(icon_kind) => match icon_kind {
systemicons::Icon::Png(bytes) => Some(icon::from_raster_bytes(bytes)),
systemicons::Icon::Svg(bytes) => Some(icon::from_svg_bytes(bytes)),
},
Err(err) => {
log::warn!("failed to get icon for {:?}: {:?}", key, err);
None
}
})
.clone()
}
}
static MIME_ICON_CACHE: OnceLock<Mutex<MimeIconCache>> = OnceLock::new();
pub fn mime_icon<P: AsRef<Path>>(path: P, size: u16) -> icon::Icon {
MIME_ICON_CACHE.get_or_init(|| Mutex::new(MimeIconCache::new()));
//TODO: smarter path handling
let path = path
.as_ref()
.to_str()
.expect("failed to convert path to UTF-8")
.to_owned();
let mut mime_icon_cache = MIME_ICON_CACHE.get().unwrap().lock().unwrap();
match mime_icon_cache.get(MimeIconKey { path, size }) {
Some(handle) => icon::icon(handle).size(size),
None => icon::from_name(FALLBACK_MIME_ICON).size(size).icon(),
}
}

View file

@ -1,13 +1,14 @@
// SPDX-License-Identifier: GPL-3.0-only
use cosmic::widget::Icon;
use cosmic::widget::icon;
use cosmic_files::mime_icon::{mime_for_path, mime_icon};
use std::{
cmp::Ordering,
fs, io,
path::{Path, PathBuf},
};
use crate::{icon_cache_get, mime_icon};
use crate::icon_cache_get;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ProjectNode {
@ -50,7 +51,7 @@ impl ProjectNode {
})
}
pub fn icon(&self, size: u16) -> Icon {
pub fn icon(&self, size: u16) -> icon::Icon {
match self {
//TODO: different icon for project root?
Self::Folder { open, .. } => {
@ -60,7 +61,7 @@ impl ProjectNode {
icon_cache_get("go-next-symbolic", size)
}
}
Self::File { path, .. } => mime_icon(path, size),
Self::File { path, .. } => icon::icon(mime_icon(mime_for_path(path), size)).size(size),
}
}

View file

@ -2,8 +2,9 @@
use cosmic::{
iced::{advanced::graphics::text::font_system, Point},
widget::{icon, Icon},
widget::icon,
};
use cosmic_files::mime_icon::{mime_for_path, mime_icon, FALLBACK_MIME_ICON};
use cosmic_text::{Attrs, Buffer, Edit, Shaping, SyntaxEditor, ViEditor, Wrap};
use notify::Watcher;
use std::{
@ -12,7 +13,7 @@ use std::{
sync::{Arc, Mutex},
};
use crate::{fl, git::GitDiff, mime_icon, Config, FALLBACK_MIME_ICON, SYNTAX_SYSTEM};
use crate::{fl, git::GitDiff, Config, SYNTAX_SYSTEM};
pub enum Tab {
Editor(EditorTab),
@ -182,9 +183,9 @@ impl EditorTab {
editor.changed()
}
pub fn icon(&self, size: u16) -> Icon {
pub fn icon(&self, size: u16) -> icon::Icon {
match &self.path_opt {
Some(path) => mime_icon(path, size),
Some(path) => icon::icon(mime_icon(mime_for_path(path), size)).size(size),
None => icon::from_name(FALLBACK_MIME_ICON).size(size).icon(),
}
}