Files
oak-gpui/crates/theme/src/icon_theme.rs
T
Marshall Bowers f53915c711 Add infrastructure for loading icon themes from extensions (#23203)
This PR adds the supporting infrastructure to support loading icon
themes defined by extensions.

Here's an example icon theme:

```json
{
  "name": "My Icon Theme",
  "author": "Me <me@example.com>",
  "themes": [
    {
      "name": "My Icon Theme",
      "appearance": "dark",
      "file_icons": {
        "gleam": { "path": "./icons/file_type_gleam.svg" },
        "toml": { "path": "./icons/file_type_toml.svg" }
      }
    }
  ]
}
```

The icon paths are resolved relative to the root of the extension
directory.

Release Notes:

- N/A
2025-01-15 23:33:47 +00:00

154 lines
5.5 KiB
Rust

use collections::HashMap;
use gpui::SharedString;
use crate::Appearance;
/// A family of icon themes.
pub struct IconThemeFamily {
/// The unique ID for the icon theme family.
pub id: String,
/// The name of the icon theme family.
pub name: SharedString,
/// The author of the icon theme family.
pub author: SharedString,
/// The list of icon themes in the family.
pub themes: Vec<IconTheme>,
}
/// An icon theme.
#[derive(Debug, PartialEq)]
pub struct IconTheme {
/// The unique ID for the icon theme.
pub id: String,
/// The name of the icon theme.
pub name: SharedString,
/// The appearance of the icon theme (e.g., light or dark).
pub appearance: Appearance,
/// The icons used for directories.
pub directory_icons: DirectoryIcons,
/// The icons used for chevrons.
pub chevron_icons: ChevronIcons,
/// The mapping of file types to icon definitions.
pub file_icons: HashMap<String, IconDefinition>,
}
/// The icons used for directories.
#[derive(Debug, PartialEq)]
pub struct DirectoryIcons {
/// The path to the icon to use for a collapsed directory.
pub collapsed: Option<SharedString>,
/// The path to the icon to use for an expanded directory.
pub expanded: Option<SharedString>,
}
/// The icons used for chevrons.
#[derive(Debug, PartialEq)]
pub struct ChevronIcons {
/// The path to the icon to use for a collapsed chevron.
pub collapsed: Option<SharedString>,
/// The path to the icon to use for an expanded chevron.
pub expanded: Option<SharedString>,
}
/// An icon definition.
#[derive(Debug, PartialEq)]
pub struct IconDefinition {
/// The path to the icon file.
pub path: SharedString,
}
/// A mapping of a file type identifier to its corresponding icon.
const FILE_ICONS: &[(&str, &str)] = &[
("astro", "icons/file_icons/astro.svg"),
("audio", "icons/file_icons/audio.svg"),
("bun", "icons/file_icons/bun.svg"),
("c", "icons/file_icons/c.svg"),
("code", "icons/file_icons/code.svg"),
("coffeescript", "icons/file_icons/coffeescript.svg"),
("cpp", "icons/file_icons/cpp.svg"),
("css", "icons/file_icons/css.svg"),
("dart", "icons/file_icons/dart.svg"),
("default", "icons/file_icons/file.svg"),
("diff", "icons/file_icons/diff.svg"),
("docker", "icons/file_icons/docker.svg"),
("document", "icons/file_icons/book.svg"),
("elixir", "icons/file_icons/elixir.svg"),
("elm", "icons/file_icons/elm.svg"),
("erlang", "icons/file_icons/erlang.svg"),
("eslint", "icons/file_icons/eslint.svg"),
("font", "icons/file_icons/font.svg"),
("fsharp", "icons/file_icons/fsharp.svg"),
("gleam", "icons/file_icons/gleam.svg"),
("go", "icons/file_icons/go.svg"),
("graphql", "icons/file_icons/graphql.svg"),
("haskell", "icons/file_icons/haskell.svg"),
("hcl", "icons/file_icons/hcl.svg"),
("heroku", "icons/file_icons/heroku.svg"),
("image", "icons/file_icons/image.svg"),
("java", "icons/file_icons/java.svg"),
("javascript", "icons/file_icons/javascript.svg"),
("julia", "icons/file_icons/julia.svg"),
("kotlin", "icons/file_icons/kotlin.svg"),
("lock", "icons/file_icons/lock.svg"),
("log", "icons/file_icons/info.svg"),
("lua", "icons/file_icons/lua.svg"),
("metal", "icons/file_icons/metal.svg"),
("nim", "icons/file_icons/nim.svg"),
("nix", "icons/file_icons/nix.svg"),
("ocaml", "icons/file_icons/ocaml.svg"),
("phoenix", "icons/file_icons/phoenix.svg"),
("php", "icons/file_icons/php.svg"),
("prettier", "icons/file_icons/prettier.svg"),
("prisma", "icons/file_icons/prisma.svg"),
("python", "icons/file_icons/python.svg"),
("r", "icons/file_icons/r.svg"),
("react", "icons/file_icons/react.svg"),
("roc", "icons/file_icons/roc.svg"),
("ruby", "icons/file_icons/ruby.svg"),
("rust", "icons/file_icons/rust.svg"),
("sass", "icons/file_icons/sass.svg"),
("scala", "icons/file_icons/scala.svg"),
("settings", "icons/file_icons/settings.svg"),
("storage", "icons/file_icons/database.svg"),
("swift", "icons/file_icons/swift.svg"),
("tcl", "icons/file_icons/tcl.svg"),
("template", "icons/file_icons/html.svg"),
("terminal", "icons/file_icons/terminal.svg"),
("terraform", "icons/file_icons/terraform.svg"),
("toml", "icons/file_icons/toml.svg"),
("typescript", "icons/file_icons/typescript.svg"),
("v", "icons/file_icons/v.svg"),
("vcs", "icons/file_icons/git.svg"),
("video", "icons/file_icons/video.svg"),
("vue", "icons/file_icons/vue.svg"),
("zig", "icons/file_icons/zig.svg"),
];
/// The name of the default icon theme.
pub(crate) const DEFAULT_ICON_THEME_NAME: &str = "Zed (Default)";
/// Returns the default icon theme.
pub fn default_icon_theme() -> IconTheme {
IconTheme {
id: "zed".into(),
name: DEFAULT_ICON_THEME_NAME.into(),
appearance: Appearance::Dark,
directory_icons: DirectoryIcons {
collapsed: Some("icons/file_icons/folder.svg".into()),
expanded: Some("icons/file_icons/folder_open.svg".into()),
},
chevron_icons: ChevronIcons {
collapsed: Some("icons/file_icons/chevron_right.svg".into()),
expanded: Some("icons/file_icons/chevron_down.svg".into()),
},
file_icons: HashMap::from_iter(FILE_ICONS.into_iter().map(|(ty, path)| {
(
ty.to_string(),
IconDefinition {
path: (*path).into(),
},
)
})),
}
}