Make file types live reload
This commit is contained in:
parent
96ef6ab326
commit
8c855680e7
4 changed files with 68 additions and 33 deletions
|
@ -4,6 +4,7 @@ use collections::HashMap;
|
|||
|
||||
use gpui::{AppContext, AssetSource};
|
||||
use serde_derive::Deserialize;
|
||||
use util::iife;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct TypeConfig {
|
||||
|
@ -16,9 +17,9 @@ pub struct FileAssociations {
|
|||
types: HashMap<String, TypeConfig>,
|
||||
}
|
||||
|
||||
pub const TEXT_FILE_ASSET: &'static str = "icons/file_icons/quill/file.svg";
|
||||
const DIRECTORY_TYPE: &'static str = "directory";
|
||||
const EXPANDED_DIRECTORY_TYPE: &'static str = "expanded_directory";
|
||||
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))
|
||||
|
@ -28,8 +29,9 @@ impl FileAssociations {
|
|||
pub fn new(assets: impl AssetSource) -> Self {
|
||||
assets
|
||||
.load("icons/file_icons/file_types.json")
|
||||
.map(|file| {
|
||||
serde_json::from_str::<FileAssociations>(str::from_utf8(&file).unwrap()).unwrap()
|
||||
.and_then(|file| {
|
||||
serde_json::from_str::<FileAssociations>(str::from_utf8(&file).unwrap())
|
||||
.map_err(Into::into)
|
||||
})
|
||||
.unwrap_or_else(|_| FileAssociations {
|
||||
suffixes: HashMap::default(),
|
||||
|
@ -37,35 +39,37 @@ impl FileAssociations {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn get_icon(path: &Path, cx: &AppContext) -> Option<Arc<str>> {
|
||||
if !cx.has_global::<Self>() {
|
||||
return None;
|
||||
}
|
||||
pub fn get_icon(path: &Path, cx: &AppContext) -> Arc<str> {
|
||||
iife!({
|
||||
let this = cx.has_global::<Self>().then(|| cx.global::<Self>())?;
|
||||
|
||||
let this = cx.global::<Self>();
|
||||
let suffix = path.extension()?.to_str()?;
|
||||
iife!({
|
||||
let suffix = path.extension()?.to_str()?;
|
||||
|
||||
this.suffixes
|
||||
.get(suffix)
|
||||
.and_then(|type_str| 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()))
|
||||
})
|
||||
.unwrap_or_else(|| Arc::from("".to_string()))
|
||||
}
|
||||
|
||||
pub fn get_folder_icon(expanded: bool, cx: &AppContext) -> Option<Arc<str>> {
|
||||
if !cx.has_global::<Self>() {
|
||||
return None;
|
||||
}
|
||||
pub fn get_folder_icon(expanded: bool, cx: &AppContext) -> Arc<str> {
|
||||
iife!({
|
||||
let this = cx.has_global::<Self>().then(|| cx.global::<Self>())?;
|
||||
|
||||
let this = cx.global::<Self>();
|
||||
let key = if expanded {
|
||||
EXPANDED_DIRECTORY_TYPE
|
||||
} else {
|
||||
DIRECTORY_TYPE
|
||||
};
|
||||
|
||||
let key = if expanded {
|
||||
EXPANDED_DIRECTORY_TYPE
|
||||
} else {
|
||||
DIRECTORY_TYPE
|
||||
};
|
||||
|
||||
this.types
|
||||
.get(key)
|
||||
.map(|type_config| type_config.icon.clone())
|
||||
this.types
|
||||
.get(key)
|
||||
.map(|type_config| type_config.icon.clone())
|
||||
})
|
||||
.unwrap_or_else(|| Arc::from("".to_string()))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue