
Release Notes: - Added support for showing file icons for files without suffixes. Before: <img width="281" alt="image" src="https://github.com/zed-industries/zed/assets/25414681/ab4c00ed-72c7-458f-8dda-61c68165590f"> After: <img width="242" alt="Screenshot 2024-02-27 at 1 51 20 AM" src="https://github.com/zed-industries/zed/assets/25414681/8f3082c4-9424-4bc3-9100-a527b9adc315"> This screenshot is to show if the file has extension, then the extension takes precedence. <img width="193" alt="image" src="https://github.com/zed-industries/zed/assets/25414681/72fcebd1-361f-444b-8890-f59932963083"> <br> - Added icons for - Docker - https://www.svgrepo.com/svg/473589/docker - License - https://www.svgrepo.com/svg/477704/license-1 - Heroku - https://www.svgrepo.com/svg/341904/heroku - Updated tests
98 lines
2.9 KiB
Rust
98 lines
2.9 KiB
Rust
use std::{path::Path, str, sync::Arc};
|
|
|
|
use collections::HashMap;
|
|
|
|
use gpui::{AppContext, AssetSource, Global};
|
|
use serde_derive::Deserialize;
|
|
use util::{maybe, paths::PathExt};
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
struct TypeConfig {
|
|
icon: Arc<str>,
|
|
}
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
pub struct FileAssociations {
|
|
stems: HashMap<String, String>,
|
|
suffixes: HashMap<String, String>,
|
|
types: HashMap<String, TypeConfig>,
|
|
}
|
|
|
|
impl Global for FileAssociations {}
|
|
|
|
const COLLAPSED_DIRECTORY_TYPE: &'static str = "collapsed_folder";
|
|
const EXPANDED_DIRECTORY_TYPE: &'static str = "expanded_folder";
|
|
const COLLAPSED_CHEVRON_TYPE: &'static str = "collapsed_chevron";
|
|
const EXPANDED_CHEVRON_TYPE: &'static str = "expanded_chevron";
|
|
pub const FILE_TYPES_ASSET: &'static str = "icons/file_icons/file_types.json";
|
|
|
|
pub fn init(assets: impl AssetSource, cx: &mut AppContext) {
|
|
cx.set_global(FileAssociations::new(assets))
|
|
}
|
|
|
|
impl FileAssociations {
|
|
pub fn new(assets: impl AssetSource) -> Self {
|
|
assets
|
|
.load("icons/file_icons/file_types.json")
|
|
.and_then(|file| {
|
|
serde_json::from_str::<FileAssociations>(str::from_utf8(&file).unwrap())
|
|
.map_err(Into::into)
|
|
})
|
|
.unwrap_or_else(|_| FileAssociations {
|
|
stems: HashMap::default(),
|
|
suffixes: HashMap::default(),
|
|
types: HashMap::default(),
|
|
})
|
|
}
|
|
|
|
pub fn get_icon(path: &Path, cx: &AppContext) -> Option<Arc<str>> {
|
|
let this = cx.try_global::<Self>()?;
|
|
|
|
// FIXME: Associate a type with the languages and have the file's language
|
|
// override these associations
|
|
maybe!({
|
|
let suffix = path.icon_stem_or_suffix()?;
|
|
|
|
if let Some(type_str) = this.stems.get(suffix) {
|
|
return this
|
|
.types
|
|
.get(type_str)
|
|
.map(|type_config| type_config.icon.clone());
|
|
}
|
|
|
|
this.suffixes
|
|
.get(suffix)
|
|
.and_then(|type_str| this.types.get(type_str))
|
|
.map(|type_config| type_config.icon.clone())
|
|
})
|
|
.or_else(|| this.types.get("default").map(|config| config.icon.clone()))
|
|
}
|
|
|
|
pub fn get_folder_icon(expanded: bool, cx: &AppContext) -> Option<Arc<str>> {
|
|
let this = cx.try_global::<Self>()?;
|
|
|
|
let key = if expanded {
|
|
EXPANDED_DIRECTORY_TYPE
|
|
} else {
|
|
COLLAPSED_DIRECTORY_TYPE
|
|
};
|
|
|
|
this.types
|
|
.get(key)
|
|
.map(|type_config| type_config.icon.clone())
|
|
}
|
|
|
|
pub fn get_chevron_icon(expanded: bool, cx: &AppContext) -> Option<Arc<str>> {
|
|
let this = cx.try_global::<Self>()?;
|
|
|
|
let key = if expanded {
|
|
EXPANDED_CHEVRON_TYPE
|
|
} else {
|
|
COLLAPSED_CHEVRON_TYPE
|
|
};
|
|
|
|
this.types
|
|
.get(key)
|
|
.map(|type_config| type_config.icon.clone())
|
|
}
|
|
}
|