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, cx: &App) -> Option<SharedString> {
97 fn get_folder_icon(icon_theme: &Arc<IconTheme>, expanded: bool) -> Option<SharedString> {
98 if expanded {
99 icon_theme.directory_icons.expanded.clone()
100 } else {
101 icon_theme.directory_icons.collapsed.clone()
102 }
103 }
104
105 get_folder_icon(&ThemeSettings::get_global(cx).active_icon_theme, expanded).or_else(|| {
106 Self::default_icon_theme(cx)
107 .and_then(|icon_theme| get_folder_icon(&icon_theme, expanded))
108 })
109 }
110
111 pub fn get_chevron_icon(expanded: bool, cx: &App) -> Option<SharedString> {
112 fn get_chevron_icon(icon_theme: &Arc<IconTheme>, expanded: bool) -> Option<SharedString> {
113 if expanded {
114 icon_theme.chevron_icons.expanded.clone()
115 } else {
116 icon_theme.chevron_icons.collapsed.clone()
117 }
118 }
119
120 get_chevron_icon(&ThemeSettings::get_global(cx).active_icon_theme, expanded).or_else(|| {
121 Self::default_icon_theme(cx)
122 .and_then(|icon_theme| get_chevron_icon(&icon_theme, expanded))
123 })
124 }
125}