chore: audit components
This commit is contained in:
@@ -4,7 +4,9 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
use std::sync::{Mutex, OnceLock, PoisonError};
|
||||
|
||||
use gpui::SharedString;
|
||||
|
||||
use crate::IconRole;
|
||||
|
||||
@@ -144,14 +146,33 @@ fn icon_score(path: &Path, requested: u32) -> SizeScore {
|
||||
.unwrap_or(SizeScore::Any)
|
||||
}
|
||||
|
||||
/// The discovered icon file for a role at a size, cached for the process.
|
||||
type IconCache = Mutex<HashMap<(&'static str, u32), Option<PathBuf>>>;
|
||||
/// A resolved icon file and the asset key its bytes are served under.
|
||||
struct CachedIcon {
|
||||
path: PathBuf,
|
||||
asset_key: SharedString,
|
||||
}
|
||||
|
||||
/// The discovered icons, cached for the process.
|
||||
type IconCache = Mutex<HashMap<(&'static str, u32), Option<CachedIcon>>>;
|
||||
|
||||
pub(crate) fn cached_file(role: IconRole, requested: u32) -> Option<PathBuf> {
|
||||
cached_icon(role, requested, |icon| icon.path.clone())
|
||||
}
|
||||
|
||||
/// The asset key a resolved freedesktop file is served under.
|
||||
pub(crate) fn cached_asset_key(role: IconRole, requested: u32) -> Option<SharedString> {
|
||||
cached_icon(role, requested, |icon| icon.asset_key.clone())
|
||||
}
|
||||
|
||||
fn cached_icon<T, P>(role: IconRole, requested: u32, project: P) -> Option<T>
|
||||
where
|
||||
P: FnOnce(&CachedIcon) -> T,
|
||||
{
|
||||
static CACHE: OnceLock<IconCache> = OnceLock::new();
|
||||
|
||||
let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
|
||||
let mut cache = cache.lock().expect("icon cache poisoned");
|
||||
// A poisoned lock means a lookup panicked; its stale data beats a panic.
|
||||
let mut cache = cache.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
|
||||
cache
|
||||
.entry((role.slug(), requested))
|
||||
@@ -162,8 +183,16 @@ pub(crate) fn cached_file(role: IconRole, requested: u32) -> Option<PathBuf> {
|
||||
requested,
|
||||
&base_dirs(),
|
||||
)
|
||||
.map(|path| CachedIcon {
|
||||
asset_key: SharedString::from(format!(
|
||||
"icons/freedesktop/{}-{requested}.svg",
|
||||
role.slug()
|
||||
)),
|
||||
path,
|
||||
})
|
||||
})
|
||||
.clone()
|
||||
.as_ref()
|
||||
.map(project)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
@@ -253,7 +282,12 @@ mod tests {
|
||||
fn scratch(name: &str) -> PathBuf {
|
||||
let root =
|
||||
std::env::temp_dir().join(format!("gpui-native-icons-{name}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
// NotFound is the expected first run; anything else is a setup failure.
|
||||
if let Err(error) = std::fs::remove_dir_all(&root)
|
||||
&& error.kind() != std::io::ErrorKind::NotFound
|
||||
{
|
||||
panic!("failed to clear scratch dir {root:?}: {error}");
|
||||
}
|
||||
std::fs::create_dir_all(&root).expect("scratch dir");
|
||||
root
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user