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