Skip to content

Commit e60123b

Browse files
authoredFeb 15, 2025
Allow icon themes to provide their own file associations (zed-industries#24926)
This PR adds the ability for icon themes to provide their own file associations. The old `file_types.json` that was previously used to make these associations has been removed in favor of storing them on the default theme. Icon themes have two new fields on them: - `file_stems`: A mapping of file stems to icon keys. - `file_suffixes`: A mapping of file suffixes to icon keys. These mappings produce icon keys which can then be used in `file_icons` to associate them to a particular icon: ```json { "file_stems": { "Makefile": "make" }, "file_suffixes": { "idr": "idris" }, "file_icons": { "idris": { "path": "./icons/idris.svg" }, "make": { "path": "./icons/make.svg" } } } ``` When loading an icon theme, the `file_stems` and `file_icons` fields will be merged with the ones from the base icon theme, with the values from the icon theme being loaded overriding ones in the base theme. Release Notes: - Added the ability for icon themes to provide their own file associations.
1 parent f277609 commit e60123b

File tree

16 files changed

+286
-370
lines changed

16 files changed

+286
-370
lines changed
 

‎CONTRIBUTING.md

-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ Zed's default icon theme consists of icons that are hand-designed to fit togethe
4343

4444
We do not accept PRs for file icons that are just an off-the-shelf SVG taken from somewhere else.
4545

46-
### File icon associations
47-
48-
We will happily accept PRs that add new file icon associations to [`file_types.json`](assets/icons/file_icons/file_types.json) to allow them to be targeted by [icon themes](https://zed.dev/docs/extensions/icon-themes).
49-
5046
### Adding new icons to the Zed icon theme
5147

5248
If you would like to add a new icon to the Zed icon theme, [open a Discussion](https://github.com/zed-industries/zed/discussions/new?category=ux-and-design) and we can work with you on getting an icon designed and added to Zed.

‎Cargo.lock

-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎assets/icons/file_icons/file_types.json

-260
This file was deleted.

‎crates/file_icons/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ path = "src/file_icons.rs"
1313
doctest = false
1414

1515
[dependencies]
16-
collections.workspace = true
1716
gpui.workspace = true
1817
serde.workspace = true
19-
serde_derive.workspace = true
20-
serde_json.workspace = true
2118
settings.workspace = true
2219
theme.workspace = true
2320
util.workspace = true

‎crates/file_icons/src/file_icons.rs

+12-31
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,33 @@
11
use std::sync::Arc;
22
use std::{path::Path, str};
33

4-
use collections::HashMap;
5-
6-
use gpui::{App, AssetSource, Global, SharedString};
7-
use serde_derive::Deserialize;
4+
use gpui::{App, SharedString};
85
use settings::Settings;
96
use theme::{IconTheme, ThemeRegistry, ThemeSettings};
107
use util::paths::PathExt;
118

12-
#[derive(Deserialize, Debug)]
9+
#[derive(Debug)]
1310
pub struct FileIcons {
14-
stems: HashMap<String, String>,
15-
suffixes: HashMap<String, String>,
16-
}
17-
18-
impl Global for FileIcons {}
19-
20-
pub const FILE_TYPES_ASSET: &str = "icons/file_icons/file_types.json";
21-
22-
pub fn init(assets: impl AssetSource, cx: &mut App) {
23-
cx.set_global(FileIcons::new(assets))
11+
icon_theme: Arc<IconTheme>,
2412
}
2513

2614
impl FileIcons {
27-
pub fn get(cx: &App) -> &Self {
28-
cx.global::<FileIcons>()
29-
}
15+
pub fn get(cx: &App) -> Self {
16+
let theme_settings = ThemeSettings::get_global(cx);
3017

31-
pub fn new(assets: impl AssetSource) -> Self {
32-
assets
33-
.load(FILE_TYPES_ASSET)
34-
.ok()
35-
.flatten()
36-
.and_then(|file| serde_json::from_str::<FileIcons>(str::from_utf8(&file).unwrap()).ok())
37-
.unwrap_or_else(|| FileIcons {
38-
stems: HashMap::default(),
39-
suffixes: HashMap::default(),
40-
})
18+
Self {
19+
icon_theme: theme_settings.active_icon_theme.clone(),
20+
}
4121
}
4222

4323
pub fn get_icon(path: &Path, cx: &App) -> Option<SharedString> {
44-
let this = cx.try_global::<Self>()?;
24+
let this = Self::get(cx);
4525

4626
let get_icon_from_suffix = |suffix: &str| -> Option<SharedString> {
47-
this.stems
27+
this.icon_theme
28+
.file_stems
4829
.get(suffix)
49-
.or_else(|| this.suffixes.get(suffix))
30+
.or_else(|| this.icon_theme.file_suffixes.get(suffix))
5031
.and_then(|typ| this.get_icon_for_type(typ, cx))
5132
};
5233
// TODO: Associate a type with the languages and have the file's language

0 commit comments

Comments
 (0)
Please sign in to comment.