1use std::sync::Arc;
2use std::{path::Path, str};
3
4use gpui::{App, SharedString};
5use settings::Settings;
6use theme::{IconTheme, ThemeRegistry, ThemeSettings};
7use util::paths::PathExt;
8
9#[derive(Debug)]
10pub struct FileIcons {
11 icon_theme: Arc<IconTheme>,
12}
13
14impl FileIcons {
15 pub fn get(cx: &App) -> Self {
16 let theme_settings = ThemeSettings::get_global(cx);
17
18 Self {
19 icon_theme: theme_settings.active_icon_theme.clone(),
20 }
21 }
22
23 pub fn get_icon(path: &Path, cx: &App) -> Option<SharedString> {
24 let this = Self::get(cx);
25
26 let get_icon_from_suffix = |suffix: &str| -> Option<SharedString> {
27 this.icon_theme
28 .file_stems
29 .get(suffix)
30 .or_else(|| this.icon_theme.file_suffixes.get(suffix))
31 .and_then(|typ| this.get_icon_for_type(typ, cx))
32 };
33 // TODO: Associate a type with the languages and have the file's language
34 // override these associations
35
36 if let Some(mut typ) = path.file_name().and_then(|typ| typ.to_str()) {
37 // check if file name is in suffixes
38 // e.g. catch file named `eslint.config.js` instead of `.eslint.config.js`
39 let maybe_path = get_icon_from_suffix(typ);
40 if maybe_path.is_some() {
41 return maybe_path;
42 }
43
44 // check if suffix based on first dot is in suffixes
45 // e.g. consider `module.js` as suffix to angular's module file named `auth.module.js`
46 while let Some((_, suffix)) = typ.split_once('.') {
47 let maybe_path = get_icon_from_suffix(suffix);
48 if maybe_path.is_some() {
49 return maybe_path;
50 }
51 typ = suffix;
52 }
53 }
54
55 // primary case: check if the files extension or the hidden file name
56 // matches some icon path
57 if let Some(suffix) = path.extension_or_hidden_file_name() {
58 let maybe_path = get_icon_from_suffix(suffix);
59 if maybe_path.is_some() {
60 return maybe_path;
61 }
62 }
63
64 // this _should_ only happen when the file is hidden (has leading '.')
65 // and is not a "special" file we have an icon (e.g. not `.eslint.config.js`)
66 // that should be caught above. In the remaining cases, we want to check
67 // for a normal supported extension e.g. `.data.json` -> `json`
68 let extension = path.extension().and_then(|ext| ext.to_str());
69 if let Some(extension) = extension {
70 let maybe_path = get_icon_from_suffix(extension);
71 if maybe_path.is_some() {
72 return maybe_path;
73 }
74 }
75 this.get_icon_for_type("default", cx)
76 }
77
78 fn default_icon_theme(cx: &App) -> Option<Arc<IconTheme>> {
79 let theme_registry = ThemeRegistry::global(cx);
80 theme_registry.default_icon_theme().ok()
81 }
82
83 pub fn get_icon_for_type(&self, typ: &str, cx: &App) -> Option<SharedString> {
84 fn get_icon_for_type(icon_theme: &Arc<IconTheme>, typ: &str) -> Option<SharedString> {
85 icon_theme
86 .file_icons
87 .get(typ)
88 .map(|icon_definition| icon_definition.path.clone())
89 }
90
91 get_icon_for_type(&ThemeSettings::get_global(cx).active_icon_theme, typ).or_else(|| {
92 Self::default_icon_theme(cx).and_then(|icon_theme| get_icon_for_type(&icon_theme, typ))
93 })
94 }
95
96 pub fn get_folder_icon(expanded: bool, path: &Path, cx: &App) -> Option<SharedString> {
97 fn get_folder_icon(
98 icon_theme: &Arc<IconTheme>,
99 path: &Path,
100 expanded: bool,
101 ) -> Option<SharedString> {
102 let name = path.file_name()?.to_str()?.trim();
103 if name.is_empty() {
104 return None;
105 }
106
107 let directory_icons = icon_theme.named_directory_icons.get(name)?;
108
109 if expanded {
110 directory_icons.expanded.clone()
111 } else {
112 directory_icons.collapsed.clone()
113 }
114 }
115
116 get_folder_icon(
117 &ThemeSettings::get_global(cx).active_icon_theme,
118 path,
119 expanded,
120 )
121 .or_else(|| {
122 Self::default_icon_theme(cx)
123 .and_then(|icon_theme| get_folder_icon(&icon_theme, path, expanded))
124 })
125 .or_else(|| {
126 // If we can't find a specific folder icon for the folder at the given path, fall back to the generic folder
127 // icon.
128 Self::get_generic_folder_icon(expanded, cx)
129 })
130 }
131
132 fn get_generic_folder_icon(expanded: bool, cx: &App) -> Option<SharedString> {
133 fn get_generic_folder_icon(
134 icon_theme: &Arc<IconTheme>,
135 expanded: bool,
136 ) -> Option<SharedString> {
137 if expanded {
138 icon_theme.directory_icons.expanded.clone()
139 } else {
140 icon_theme.directory_icons.collapsed.clone()
141 }
142 }
143
144 get_generic_folder_icon(&ThemeSettings::get_global(cx).active_icon_theme, expanded).or_else(
145 || {
146 Self::default_icon_theme(cx)
147 .and_then(|icon_theme| get_generic_folder_icon(&icon_theme, expanded))
148 },
149 )
150 }
151
152 pub fn get_chevron_icon(expanded: bool, cx: &App) -> Option<SharedString> {
153 fn get_chevron_icon(icon_theme: &Arc<IconTheme>, expanded: bool) -> Option<SharedString> {
154 if expanded {
155 icon_theme.chevron_icons.expanded.clone()
156 } else {
157 icon_theme.chevron_icons.collapsed.clone()
158 }
159 }
160
161 get_chevron_icon(&ThemeSettings::get_global(cx).active_icon_theme, expanded).or_else(|| {
162 Self::default_icon_theme(cx)
163 .and_then(|icon_theme| get_chevron_icon(&icon_theme, expanded))
164 })
165 }
166}