1use std::sync::Arc;
2use std::{path::Path, str};
3
4use collections::HashMap;
5
6use gpui::{App, AssetSource, Global, SharedString};
7use serde_derive::Deserialize;
8use settings::Settings;
9use theme::{IconTheme, ThemeRegistry, ThemeSettings};
10use util::paths::PathExt;
11
12#[derive(Deserialize, Debug)]
13pub struct FileIcons {
14 stems: HashMap<String, String>,
15 suffixes: HashMap<String, String>,
16}
17
18impl Global for FileIcons {}
19
20pub const FILE_TYPES_ASSET: &str = "icons/file_icons/file_types.json";
21
22pub fn init(assets: impl AssetSource, cx: &mut App) {
23 cx.set_global(FileIcons::new(assets))
24}
25
26impl FileIcons {
27 pub fn get(cx: &App) -> &Self {
28 cx.global::<FileIcons>()
29 }
30
31 pub fn new(assets: impl AssetSource) -> Self {
32 assets
33 .load(FILE_TYPES_ASSET)
34 .ok()
35 .flatten()
36 .and_then(|file| serde_json::from_str::<FileIcons>(str::from_utf8(&file).unwrap()).ok())
37 .unwrap_or_else(|| FileIcons {
38 stems: HashMap::default(),
39 suffixes: HashMap::default(),
40 })
41 }
42
43 pub fn get_icon(path: &Path, cx: &App) -> Option<SharedString> {
44 let this = cx.try_global::<Self>()?;
45
46 let get_icon_from_suffix = |suffix: &str| -> Option<SharedString> {
47 this.stems
48 .get(suffix)
49 .or_else(|| this.suffixes.get(suffix))
50 .and_then(|typ| this.get_icon_for_type(typ, cx))
51 };
52 // TODO: Associate a type with the languages and have the file's language
53 // override these associations
54
55 // check if file name is in suffixes
56 // e.g. catch file named `eslint.config.js` instead of `.eslint.config.js`
57 if let Some(typ) = path.file_name().and_then(|typ| typ.to_str()) {
58 let maybe_path = get_icon_from_suffix(typ);
59 if maybe_path.is_some() {
60 return maybe_path;
61 }
62 }
63
64 // primary case: check if the files extension or the hidden file name
65 // matches some icon path
66 if let Some(suffix) = path.extension_or_hidden_file_name() {
67 let maybe_path = get_icon_from_suffix(suffix);
68 if maybe_path.is_some() {
69 return maybe_path;
70 }
71 }
72
73 // this _should_ only happen when the file is hidden (has leading '.')
74 // and is not a "special" file we have an icon (e.g. not `.eslint.config.js`)
75 // that should be caught above. In the remaining cases, we want to check
76 // for a normal supported extension e.g. `.data.json` -> `json`
77 let extension = path.extension().and_then(|ext| ext.to_str());
78 if let Some(extension) = extension {
79 let maybe_path = get_icon_from_suffix(extension);
80 if maybe_path.is_some() {
81 return maybe_path;
82 }
83 }
84 return this.get_icon_for_type("default", cx);
85 }
86
87 fn default_icon_theme(cx: &App) -> Option<Arc<IconTheme>> {
88 let theme_registry = ThemeRegistry::global(cx);
89 theme_registry.default_icon_theme().ok()
90 }
91
92 pub fn get_icon_for_type(&self, typ: &str, cx: &App) -> Option<SharedString> {
93 fn get_icon_for_type(icon_theme: &Arc<IconTheme>, typ: &str) -> Option<SharedString> {
94 icon_theme
95 .file_icons
96 .get(typ)
97 .map(|icon_definition| icon_definition.path.clone())
98 }
99
100 get_icon_for_type(&ThemeSettings::get_global(cx).active_icon_theme, typ).or_else(|| {
101 Self::default_icon_theme(cx).and_then(|icon_theme| get_icon_for_type(&icon_theme, typ))
102 })
103 }
104
105 pub fn get_folder_icon(expanded: bool, cx: &App) -> Option<SharedString> {
106 fn get_folder_icon(icon_theme: &Arc<IconTheme>, expanded: bool) -> Option<SharedString> {
107 if expanded {
108 icon_theme.directory_icons.expanded.clone()
109 } else {
110 icon_theme.directory_icons.collapsed.clone()
111 }
112 }
113
114 get_folder_icon(&ThemeSettings::get_global(cx).active_icon_theme, expanded).or_else(|| {
115 Self::default_icon_theme(cx)
116 .and_then(|icon_theme| get_folder_icon(&icon_theme, expanded))
117 })
118 }
119
120 pub fn get_chevron_icon(expanded: bool, cx: &App) -> Option<SharedString> {
121 fn get_chevron_icon(icon_theme: &Arc<IconTheme>, expanded: bool) -> Option<SharedString> {
122 if expanded {
123 icon_theme.chevron_icons.expanded.clone()
124 } else {
125 icon_theme.chevron_icons.collapsed.clone()
126 }
127 }
128
129 get_chevron_icon(&ThemeSettings::get_global(cx).active_icon_theme, expanded).or_else(|| {
130 Self::default_icon_theme(cx)
131 .and_then(|icon_theme| get_chevron_icon(&icon_theme, expanded))
132 })
133 }
134}