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