1use std::{path::Path, str, sync::Arc};
2
3use collections::HashMap;
4
5use gpui::{AppContext, AssetSource, Global};
6use serde_derive::Deserialize;
7use util::{maybe, paths::PathExt};
8
9#[derive(Deserialize, Debug)]
10struct TypeConfig {
11 icon: Arc<str>,
12}
13
14#[derive(Deserialize, Debug)]
15pub struct FileAssociations {
16 stems: HashMap<String, String>,
17 suffixes: HashMap<String, String>,
18 types: HashMap<String, TypeConfig>,
19}
20
21impl Global for FileAssociations {}
22
23const COLLAPSED_DIRECTORY_TYPE: &str = "collapsed_folder";
24const EXPANDED_DIRECTORY_TYPE: &str = "expanded_folder";
25const COLLAPSED_CHEVRON_TYPE: &str = "collapsed_chevron";
26const EXPANDED_CHEVRON_TYPE: &str = "expanded_chevron";
27pub const FILE_TYPES_ASSET: &str = "icons/file_icons/file_types.json";
28
29pub fn init(assets: impl AssetSource, cx: &mut AppContext) {
30 cx.set_global(FileAssociations::new(assets))
31}
32
33impl FileAssociations {
34 pub fn new(assets: impl AssetSource) -> Self {
35 assets
36 .load("icons/file_icons/file_types.json")
37 .and_then(|file| {
38 serde_json::from_str::<FileAssociations>(str::from_utf8(&file).unwrap())
39 .map_err(Into::into)
40 })
41 .unwrap_or_else(|_| FileAssociations {
42 stems: HashMap::default(),
43 suffixes: HashMap::default(),
44 types: HashMap::default(),
45 })
46 }
47
48 pub fn get_icon(path: &Path, cx: &AppContext) -> Option<Arc<str>> {
49 let this = cx.try_global::<Self>()?;
50
51 // FIXME: Associate a type with the languages and have the file's language
52 // override these associations
53 maybe!({
54 let suffix = path.icon_stem_or_suffix()?;
55
56 if let Some(type_str) = this.stems.get(suffix) {
57 return this
58 .types
59 .get(type_str)
60 .map(|type_config| type_config.icon.clone());
61 }
62
63 this.suffixes
64 .get(suffix)
65 .and_then(|type_str| this.types.get(type_str))
66 .map(|type_config| type_config.icon.clone())
67 })
68 .or_else(|| this.types.get("default").map(|config| config.icon.clone()))
69 }
70
71 pub fn get_folder_icon(expanded: bool, cx: &AppContext) -> Option<Arc<str>> {
72 let this = cx.try_global::<Self>()?;
73
74 let key = if expanded {
75 EXPANDED_DIRECTORY_TYPE
76 } else {
77 COLLAPSED_DIRECTORY_TYPE
78 };
79
80 this.types
81 .get(key)
82 .map(|type_config| type_config.icon.clone())
83 }
84
85 pub fn get_chevron_icon(expanded: bool, cx: &AppContext) -> Option<Arc<str>> {
86 let this = cx.try_global::<Self>()?;
87
88 let key = if expanded {
89 EXPANDED_CHEVRON_TYPE
90 } else {
91 COLLAPSED_CHEVRON_TYPE
92 };
93
94 this.types
95 .get(key)
96 .map(|type_config| type_config.icon.clone())
97 }
98}