Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T02:02:29.017Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

file_icons.rs

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