file_icons.rs

  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        // handle cases where the file extension is made up of multiple important
 56        // parts (e.g Component.stories.tsx) that refer to an alternative icon style
 57        if let Some(suffix) = path.multiple_extensions() {
 58            let maybe_path = get_icon_from_suffix(suffix.as_str());
 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        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, path: &Path, cx: &App) -> Option<SharedString> {
106        fn get_folder_icon(
107            icon_theme: &Arc<IconTheme>,
108            path: &Path,
109            expanded: bool,
110        ) -> Option<SharedString> {
111            let name = path.file_name()?.to_str()?.trim();
112            if name.is_empty() {
113                return None;
114            }
115
116            let directory_icons = icon_theme.named_directory_icons.get(name)?;
117
118            if expanded {
119                directory_icons.expanded.clone()
120            } else {
121                directory_icons.collapsed.clone()
122            }
123        }
124
125        get_folder_icon(
126            &ThemeSettings::get_global(cx).active_icon_theme,
127            path,
128            expanded,
129        )
130        .or_else(|| {
131            Self::default_icon_theme(cx)
132                .and_then(|icon_theme| get_folder_icon(&icon_theme, path, expanded))
133        })
134        .or_else(|| {
135            // If we can't find a specific folder icon for the folder at the given path, fall back to the generic folder
136            // icon.
137            Self::get_generic_folder_icon(expanded, cx)
138        })
139    }
140
141    fn get_generic_folder_icon(expanded: bool, cx: &App) -> Option<SharedString> {
142        fn get_generic_folder_icon(
143            icon_theme: &Arc<IconTheme>,
144            expanded: bool,
145        ) -> Option<SharedString> {
146            if expanded {
147                icon_theme.directory_icons.expanded.clone()
148            } else {
149                icon_theme.directory_icons.collapsed.clone()
150            }
151        }
152
153        get_generic_folder_icon(&ThemeSettings::get_global(cx).active_icon_theme, expanded).or_else(
154            || {
155                Self::default_icon_theme(cx)
156                    .and_then(|icon_theme| get_generic_folder_icon(&icon_theme, expanded))
157            },
158        )
159    }
160
161    pub fn get_chevron_icon(expanded: bool, cx: &App) -> Option<SharedString> {
162        fn get_chevron_icon(icon_theme: &Arc<IconTheme>, expanded: bool) -> Option<SharedString> {
163            if expanded {
164                icon_theme.chevron_icons.expanded.clone()
165            } else {
166                icon_theme.chevron_icons.collapsed.clone()
167            }
168        }
169
170        get_chevron_icon(&ThemeSettings::get_global(cx).active_icon_theme, expanded).or_else(|| {
171            Self::default_icon_theme(cx)
172                .and_then(|icon_theme| get_chevron_icon(&icon_theme, expanded))
173        })
174    }
175}