icon_theme.rs

  1use std::sync::{Arc, LazyLock};
  2
  3use collections::HashMap;
  4use gpui::SharedString;
  5
  6use crate::Appearance;
  7
  8/// A family of icon themes.
  9pub struct IconThemeFamily {
 10    /// The unique ID for the icon theme family.
 11    pub id: String,
 12    /// The name of the icon theme family.
 13    pub name: SharedString,
 14    /// The author of the icon theme family.
 15    pub author: SharedString,
 16    /// The list of icon themes in the family.
 17    pub themes: Vec<IconTheme>,
 18}
 19
 20/// An icon theme.
 21#[derive(Debug, PartialEq)]
 22pub struct IconTheme {
 23    /// The unique ID for the icon theme.
 24    pub id: String,
 25    /// The name of the icon theme.
 26    pub name: SharedString,
 27    /// The appearance of the icon theme (e.g., light or dark).
 28    pub appearance: Appearance,
 29    /// The icons used for directories.
 30    pub directory_icons: DirectoryIcons,
 31    /// The icons used for chevrons.
 32    pub chevron_icons: ChevronIcons,
 33    /// The mapping of file stems to their associated icon keys.
 34    pub file_stems: HashMap<String, String>,
 35    /// The mapping of file suffixes to their associated icon keys.
 36    pub file_suffixes: HashMap<String, String>,
 37    /// The mapping of icon keys to icon definitions.
 38    pub file_icons: HashMap<String, IconDefinition>,
 39}
 40
 41/// The icons used for directories.
 42#[derive(Debug, PartialEq)]
 43pub struct DirectoryIcons {
 44    /// The path to the icon to use for a collapsed directory.
 45    pub collapsed: Option<SharedString>,
 46    /// The path to the icon to use for an expanded directory.
 47    pub expanded: Option<SharedString>,
 48}
 49
 50/// The icons used for chevrons.
 51#[derive(Debug, PartialEq)]
 52pub struct ChevronIcons {
 53    /// The path to the icon to use for a collapsed chevron.
 54    pub collapsed: Option<SharedString>,
 55    /// The path to the icon to use for an expanded chevron.
 56    pub expanded: Option<SharedString>,
 57}
 58
 59/// An icon definition.
 60#[derive(Debug, PartialEq)]
 61pub struct IconDefinition {
 62    /// The path to the icon file.
 63    pub path: SharedString,
 64}
 65
 66const FILE_STEMS_BY_ICON_KEY: &[(&str, &[&str])] = &[
 67    ("docker", &["Dockerfile"]),
 68    ("ruby", &["Podfile"]),
 69    ("heroku", &["Procfile"]),
 70];
 71
 72const FILE_SUFFIXES_BY_ICON_KEY: &[(&str, &[&str])] = &[
 73    ("astro", &["astro"]),
 74    (
 75        "audio",
 76        &[
 77            "aac", "flac", "m4a", "mka", "mp3", "ogg", "opus", "wav", "wma", "wv",
 78        ],
 79    ),
 80    ("backup", &["bak"]),
 81    ("bicep", &["bicep"]),
 82    ("bun", &["lockb"]),
 83    ("c", &["c", "h"]),
 84    ("code", &["handlebars", "metadata", "rkt", "scm"]),
 85    ("coffeescript", &["coffee"]),
 86    (
 87        "cpp",
 88        &["c++", "cc", "cpp", "cxx", "hh", "hpp", "hxx", "inl", "ixx"],
 89    ),
 90    ("crystal", &["cr", "ecr"]),
 91    ("csharp", &["cs"]),
 92    ("csproj", &["csproj"]),
 93    ("css", &["css", "pcss", "postcss"]),
 94    ("cue", &["cue"]),
 95    ("dart", &["dart"]),
 96    ("diff", &["diff"]),
 97    (
 98        "document",
 99        &[
100            "doc", "docx", "mdx", "odp", "ods", "odt", "pdf", "ppt", "pptx", "rtf", "txt", "xls",
101            "xlsx",
102        ],
103    ),
104    ("elixir", &["eex", "ex", "exs", "heex"]),
105    ("elm", &["elm"]),
106    (
107        "erlang",
108        &[
109            "Emakefile",
110            "app.src",
111            "erl",
112            "escript",
113            "hrl",
114            "rebar.config",
115            "xrl",
116            "yrl",
117        ],
118    ),
119    (
120        "eslint",
121        &[
122            "eslint.config.cjs",
123            "eslint.config.cts",
124            "eslint.config.js",
125            "eslint.config.mjs",
126            "eslint.config.mts",
127            "eslint.config.ts",
128            "eslintrc",
129            "eslintrc.js",
130            "eslintrc.json",
131        ],
132    ),
133    ("font", &["otf", "ttf", "woff", "woff2"]),
134    ("fsharp", &["fs"]),
135    ("fsproj", &["fsproj"]),
136    ("gitlab", &["gitlab-ci.yml"]),
137    ("gleam", &["gleam"]),
138    ("go", &["go", "mod", "work"]),
139    ("graphql", &["gql", "graphql", "graphqls"]),
140    ("haskell", &["hs"]),
141    ("hcl", &["hcl"]),
142    ("html", &["htm", "html"]),
143    (
144        "image",
145        &[
146            "avif", "bmp", "gif", "heic", "heif", "ico", "j2k", "jfif", "jp2", "jpeg", "jpg",
147            "jxl", "png", "psd", "qoi", "svg", "tiff", "webp",
148        ],
149    ),
150    ("java", &["java"]),
151    ("javascript", &["cjs", "js", "mjs"]),
152    ("json", &["json"]),
153    ("julia", &["jl"]),
154    ("kotlin", &["kt"]),
155    ("lock", &["lock"]),
156    ("log", &["log"]),
157    ("lua", &["lua"]),
158    ("luau", &["luau"]),
159    ("markdown", &["markdown", "md"]),
160    ("metal", &["metal"]),
161    ("nim", &["nim"]),
162    ("nix", &["nix"]),
163    ("ocaml", &["ml", "mli"]),
164    ("php", &["php"]),
165    (
166        "prettier",
167        &[
168            "prettier.config.cjs",
169            "prettier.config.js",
170            "prettier.config.mjs",
171            "prettierignore",
172            "prettierrc",
173            "prettierrc.cjs",
174            "prettierrc.js",
175            "prettierrc.json",
176            "prettierrc.json5",
177            "prettierrc.mjs",
178            "prettierrc.toml",
179            "prettierrc.yaml",
180            "prettierrc.yml",
181        ],
182    ),
183    ("prisma", &["prisma"]),
184    ("python", &["py"]),
185    ("r", &["r", "R"]),
186    ("react", &["cjsx", "ctsx", "jsx", "mjsx", "mtsx", "tsx"]),
187    ("roc", &["roc"]),
188    ("ruby", &["rb"]),
189    ("rust", &["rs"]),
190    ("sass", &["sass", "scss"]),
191    ("scala", &["scala", "sc"]),
192    ("settings", &["conf", "ini", "yaml", "yml"]),
193    ("solidity", &["sol"]),
194    (
195        "storage",
196        &[
197            "accdb", "csv", "dat", "db", "dbf", "dll", "fmp", "fp7", "frm", "gdb", "ib", "jsonc",
198            "ldf", "mdb", "mdf", "myd", "myi", "pdb", "RData", "rdata", "sav", "sdf", "sql",
199            "sqlite", "tsv",
200        ],
201    ),
202    (
203        "stylelint",
204        &[
205            "stylelint.config.cjs",
206            "stylelint.config.js",
207            "stylelint.config.mjs",
208            "stylelintignore",
209            "stylelintrc",
210            "stylelintrc.cjs",
211            "stylelintrc.js",
212            "stylelintrc.json",
213            "stylelintrc.mjs",
214            "stylelintrc.yaml",
215            "stylelintrc.yml",
216        ],
217    ),
218    ("svelte", &["svelte"]),
219    ("swift", &["swift"]),
220    ("tcl", &["tcl"]),
221    ("template", &["hbs", "plist", "xml"]),
222    (
223        "terminal",
224        &[
225            "bash",
226            "bash_aliases",
227            "bash_logout",
228            "bash_profile",
229            "bashrc",
230            "fish",
231            "nu",
232            "profile",
233            "ps1",
234            "sh",
235            "zlogin",
236            "zsh",
237            "zsh_aliases",
238            "zsh_histfile",
239            "zsh_profile",
240            "zshenv",
241            "zshrc",
242        ],
243    ),
244    ("terraform", &["tf", "tfvars"]),
245    ("toml", &["toml"]),
246    ("typescript", &["cts", "mts", "ts"]),
247    ("v", &["v", "vsh", "vv"]),
248    (
249        "vcs",
250        &[
251            "COMMIT_EDITMSG",
252            "EDIT_DESCRIPTION",
253            "MERGE_MSG",
254            "NOTES_EDITMSG",
255            "TAG_EDITMSG",
256            "gitattributes",
257            "gitignore",
258            "gitkeep",
259            "gitmodules",
260        ],
261    ),
262    ("vbproj", &["vbproj"]),
263    ("video", &["avi", "m4v", "mkv", "mov", "mp4", "webm", "wmv"]),
264    ("vs_sln", &["sln"]),
265    ("vs_suo", &["suo"]),
266    ("vue", &["vue"]),
267    ("vyper", &["vy", "vyi"]),
268    ("wgsl", &["wgsl"]),
269    ("zig", &["zig"]),
270];
271
272/// A mapping of a file type identifier to its corresponding icon.
273const FILE_ICONS: &[(&str, &str)] = &[
274    ("astro", "icons/file_icons/astro.svg"),
275    ("audio", "icons/file_icons/audio.svg"),
276    ("bicep", "icons/file_icons/file.svg"),
277    ("bun", "icons/file_icons/bun.svg"),
278    ("c", "icons/file_icons/c.svg"),
279    ("code", "icons/file_icons/code.svg"),
280    ("coffeescript", "icons/file_icons/coffeescript.svg"),
281    ("cpp", "icons/file_icons/cpp.svg"),
282    ("crystal", "icons/file_icons/file.svg"),
283    ("csharp", "icons/file_icons/file.svg"),
284    ("csproj", "icons/file_icons/file.svg"),
285    ("css", "icons/file_icons/css.svg"),
286    ("cue", "icons/file_icons/file.svg"),
287    ("dart", "icons/file_icons/dart.svg"),
288    ("default", "icons/file_icons/file.svg"),
289    ("diff", "icons/file_icons/diff.svg"),
290    ("docker", "icons/file_icons/docker.svg"),
291    ("document", "icons/file_icons/book.svg"),
292    ("elixir", "icons/file_icons/elixir.svg"),
293    ("elm", "icons/file_icons/elm.svg"),
294    ("erlang", "icons/file_icons/erlang.svg"),
295    ("eslint", "icons/file_icons/eslint.svg"),
296    ("font", "icons/file_icons/font.svg"),
297    ("fsharp", "icons/file_icons/fsharp.svg"),
298    ("fsproj", "icons/file_icons/file.svg"),
299    ("gitlab", "icons/file_icons/settings.svg"),
300    ("gleam", "icons/file_icons/gleam.svg"),
301    ("go", "icons/file_icons/go.svg"),
302    ("graphql", "icons/file_icons/graphql.svg"),
303    ("haskell", "icons/file_icons/haskell.svg"),
304    ("hcl", "icons/file_icons/hcl.svg"),
305    ("heroku", "icons/file_icons/heroku.svg"),
306    ("html", "icons/file_icons/html.svg"),
307    ("image", "icons/file_icons/image.svg"),
308    ("java", "icons/file_icons/java.svg"),
309    ("javascript", "icons/file_icons/javascript.svg"),
310    ("json", "icons/file_icons/code.svg"),
311    ("julia", "icons/file_icons/julia.svg"),
312    ("kotlin", "icons/file_icons/kotlin.svg"),
313    ("lock", "icons/file_icons/lock.svg"),
314    ("log", "icons/file_icons/info.svg"),
315    ("lua", "icons/file_icons/lua.svg"),
316    ("luau", "icons/file_icons/luau.svg"),
317    ("markdown", "icons/file_icons/book.svg"),
318    ("metal", "icons/file_icons/metal.svg"),
319    ("nim", "icons/file_icons/nim.svg"),
320    ("nix", "icons/file_icons/nix.svg"),
321    ("ocaml", "icons/file_icons/ocaml.svg"),
322    ("phoenix", "icons/file_icons/phoenix.svg"),
323    ("php", "icons/file_icons/php.svg"),
324    ("prettier", "icons/file_icons/prettier.svg"),
325    ("prisma", "icons/file_icons/prisma.svg"),
326    ("python", "icons/file_icons/python.svg"),
327    ("r", "icons/file_icons/r.svg"),
328    ("react", "icons/file_icons/react.svg"),
329    ("roc", "icons/file_icons/roc.svg"),
330    ("ruby", "icons/file_icons/ruby.svg"),
331    ("rust", "icons/file_icons/rust.svg"),
332    ("sass", "icons/file_icons/sass.svg"),
333    ("scala", "icons/file_icons/scala.svg"),
334    ("settings", "icons/file_icons/settings.svg"),
335    ("solidity", "icons/file_icons/file.svg"),
336    ("storage", "icons/file_icons/database.svg"),
337    ("stylelint", "icons/file_icons/javascript.svg"),
338    ("svelte", "icons/file_icons/html.svg"),
339    ("swift", "icons/file_icons/swift.svg"),
340    ("tcl", "icons/file_icons/tcl.svg"),
341    ("template", "icons/file_icons/html.svg"),
342    ("terminal", "icons/file_icons/terminal.svg"),
343    ("terraform", "icons/file_icons/terraform.svg"),
344    ("toml", "icons/file_icons/toml.svg"),
345    ("typescript", "icons/file_icons/typescript.svg"),
346    ("v", "icons/file_icons/v.svg"),
347    ("vbproj", "icons/file_icons/file.svg"),
348    ("vcs", "icons/file_icons/git.svg"),
349    ("video", "icons/file_icons/video.svg"),
350    ("vs_sln", "icons/file_icons/file.svg"),
351    ("vs_suo", "icons/file_icons/file.svg"),
352    ("vue", "icons/file_icons/vue.svg"),
353    ("vyper", "icons/file_icons/vyper.svg"),
354    ("wgsl", "icons/file_icons/wgsl.svg"),
355    ("zig", "icons/file_icons/zig.svg"),
356];
357
358/// Returns a mapping of file associations to icon keys.
359fn icon_keys_by_association(
360    associations_by_icon_key: &[(&str, &[&str])],
361) -> HashMap<String, String> {
362    let mut icon_keys_by_association = HashMap::default();
363    for (icon_key, associations) in associations_by_icon_key {
364        for association in *associations {
365            icon_keys_by_association.insert(association.to_string(), icon_key.to_string());
366        }
367    }
368
369    icon_keys_by_association
370}
371
372/// The name of the default icon theme.
373pub(crate) const DEFAULT_ICON_THEME_NAME: &str = "Zed (Default)";
374
375static DEFAULT_ICON_THEME: LazyLock<Arc<IconTheme>> = LazyLock::new(|| {
376    Arc::new(IconTheme {
377        id: "zed".into(),
378        name: DEFAULT_ICON_THEME_NAME.into(),
379        appearance: Appearance::Dark,
380        directory_icons: DirectoryIcons {
381            collapsed: Some("icons/file_icons/folder.svg".into()),
382            expanded: Some("icons/file_icons/folder_open.svg".into()),
383        },
384        chevron_icons: ChevronIcons {
385            collapsed: Some("icons/file_icons/chevron_right.svg".into()),
386            expanded: Some("icons/file_icons/chevron_down.svg".into()),
387        },
388        file_stems: icon_keys_by_association(FILE_STEMS_BY_ICON_KEY),
389        file_suffixes: icon_keys_by_association(FILE_SUFFIXES_BY_ICON_KEY),
390        file_icons: HashMap::from_iter(FILE_ICONS.into_iter().map(|(ty, path)| {
391            (
392                ty.to_string(),
393                IconDefinition {
394                    path: (*path).into(),
395                },
396            )
397        })),
398    })
399});
400
401/// Returns the default icon theme.
402pub fn default_icon_theme() -> Arc<IconTheme> {
403    DEFAULT_ICON_THEME.clone()
404}