1use std::sync::Arc;
2use std::{fmt::Debug, path::Path};
3
4use anyhow::{anyhow, Context as _, Result};
5use collections::HashMap;
6use derive_more::{Deref, DerefMut};
7use fs::Fs;
8use futures::StreamExt;
9use gpui::{App, AssetSource, Global, SharedString};
10use parking_lot::RwLock;
11use util::ResultExt;
12
13use crate::{
14 read_icon_theme, read_user_theme, refine_theme_family, Appearance, AppearanceContent,
15 ChevronIcons, DirectoryIcons, IconDefinition, IconTheme, Theme, ThemeFamily,
16 ThemeFamilyContent, DEFAULT_ICON_THEME_NAME,
17};
18
19/// The metadata for a theme.
20#[derive(Debug, Clone)]
21pub struct ThemeMeta {
22 /// The name of the theme.
23 pub name: SharedString,
24 /// The appearance of the theme.
25 pub appearance: Appearance,
26}
27
28/// The global [`ThemeRegistry`].
29///
30/// This newtype exists for obtaining a unique [`TypeId`](std::any::TypeId) when
31/// inserting the [`ThemeRegistry`] into the context as a global.
32///
33/// This should not be exposed outside of this module.
34#[derive(Default, Deref, DerefMut)]
35struct GlobalThemeRegistry(Arc<ThemeRegistry>);
36
37impl Global for GlobalThemeRegistry {}
38
39struct ThemeRegistryState {
40 themes: HashMap<SharedString, Arc<Theme>>,
41 icon_themes: HashMap<SharedString, Arc<IconTheme>>,
42}
43
44/// The registry for themes.
45pub struct ThemeRegistry {
46 state: RwLock<ThemeRegistryState>,
47 assets: Box<dyn AssetSource>,
48}
49
50impl ThemeRegistry {
51 /// Returns the global [`ThemeRegistry`].
52 pub fn global(cx: &App) -> Arc<Self> {
53 cx.global::<GlobalThemeRegistry>().0.clone()
54 }
55
56 /// Returns the global [`ThemeRegistry`].
57 ///
58 /// Inserts a default [`ThemeRegistry`] if one does not yet exist.
59 pub fn default_global(cx: &mut App) -> Arc<Self> {
60 cx.default_global::<GlobalThemeRegistry>().0.clone()
61 }
62
63 /// Sets the global [`ThemeRegistry`].
64 pub(crate) fn set_global(assets: Box<dyn AssetSource>, cx: &mut App) {
65 cx.set_global(GlobalThemeRegistry(Arc::new(ThemeRegistry::new(assets))));
66 }
67
68 /// Creates a new [`ThemeRegistry`] with the given [`AssetSource`].
69 pub fn new(assets: Box<dyn AssetSource>) -> Self {
70 let registry = Self {
71 state: RwLock::new(ThemeRegistryState {
72 themes: HashMap::default(),
73 icon_themes: HashMap::default(),
74 }),
75 assets,
76 };
77
78 // We're loading the Zed default theme, as we need a theme to be loaded
79 // for tests.
80 registry.insert_theme_families([crate::fallback_themes::zed_default_themes()]);
81
82 let default_icon_theme = crate::default_icon_theme();
83 registry.state.write().icon_themes.insert(
84 default_icon_theme.name.clone(),
85 Arc::new(default_icon_theme),
86 );
87
88 registry
89 }
90
91 fn insert_theme_families(&self, families: impl IntoIterator<Item = ThemeFamily>) {
92 for family in families.into_iter() {
93 self.insert_themes(family.themes);
94 }
95 }
96
97 fn insert_themes(&self, themes: impl IntoIterator<Item = Theme>) {
98 let mut state = self.state.write();
99 for theme in themes.into_iter() {
100 state.themes.insert(theme.name.clone(), Arc::new(theme));
101 }
102 }
103
104 #[allow(unused)]
105 fn insert_user_theme_families(&self, families: impl IntoIterator<Item = ThemeFamilyContent>) {
106 for family in families.into_iter() {
107 let refined_family = refine_theme_family(family);
108
109 self.insert_themes(refined_family.themes);
110 }
111 }
112
113 /// Removes the themes with the given names from the registry.
114 pub fn remove_user_themes(&self, themes_to_remove: &[SharedString]) {
115 self.state
116 .write()
117 .themes
118 .retain(|name, _| !themes_to_remove.contains(name))
119 }
120
121 /// Removes all themes from the registry.
122 pub fn clear(&self) {
123 self.state.write().themes.clear();
124 }
125
126 /// Returns the names of all themes in the registry.
127 pub fn list_names(&self) -> Vec<SharedString> {
128 let mut names = self.state.read().themes.keys().cloned().collect::<Vec<_>>();
129 names.sort();
130 names
131 }
132
133 /// Returns the metadata of all themes in the registry.
134 pub fn list(&self) -> Vec<ThemeMeta> {
135 self.state
136 .read()
137 .themes
138 .values()
139 .map(|theme| ThemeMeta {
140 name: theme.name.clone(),
141 appearance: theme.appearance(),
142 })
143 .collect()
144 }
145
146 /// Returns the theme with the given name.
147 pub fn get(&self, name: &str) -> Result<Arc<Theme>> {
148 self.state
149 .read()
150 .themes
151 .get(name)
152 .ok_or_else(|| anyhow!("theme not found: {}", name))
153 .cloned()
154 }
155
156 /// Loads the themes bundled with the Zed binary and adds them to the registry.
157 pub fn load_bundled_themes(&self) {
158 let theme_paths = self
159 .assets
160 .list("themes/")
161 .expect("failed to list theme assets")
162 .into_iter()
163 .filter(|path| path.ends_with(".json"));
164
165 for path in theme_paths {
166 let Some(theme) = self.assets.load(&path).log_err().flatten() else {
167 continue;
168 };
169
170 let Some(theme_family) = serde_json::from_slice(&theme)
171 .with_context(|| format!("failed to parse theme at path \"{path}\""))
172 .log_err()
173 else {
174 continue;
175 };
176
177 self.insert_user_theme_families([theme_family]);
178 }
179 }
180
181 /// Loads the user themes from the specified directory and adds them to the registry.
182 pub async fn load_user_themes(&self, themes_path: &Path, fs: Arc<dyn Fs>) -> Result<()> {
183 let mut theme_paths = fs
184 .read_dir(themes_path)
185 .await
186 .with_context(|| format!("reading themes from {themes_path:?}"))?;
187
188 while let Some(theme_path) = theme_paths.next().await {
189 let Some(theme_path) = theme_path.log_err() else {
190 continue;
191 };
192
193 self.load_user_theme(&theme_path, fs.clone())
194 .await
195 .log_err();
196 }
197
198 Ok(())
199 }
200
201 /// Loads the user theme from the specified path and adds it to the registry.
202 pub async fn load_user_theme(&self, theme_path: &Path, fs: Arc<dyn Fs>) -> Result<()> {
203 let theme = read_user_theme(theme_path, fs).await?;
204
205 self.insert_user_theme_families([theme]);
206
207 Ok(())
208 }
209
210 /// Returns the default icon theme.
211 pub fn default_icon_theme(&self) -> Result<Arc<IconTheme>> {
212 self.get_icon_theme(DEFAULT_ICON_THEME_NAME)
213 }
214
215 /// Returns the metadata of all icon themes in the registry.
216 pub fn list_icon_themes(&self) -> Vec<ThemeMeta> {
217 self.state
218 .read()
219 .icon_themes
220 .values()
221 .map(|theme| ThemeMeta {
222 name: theme.name.clone(),
223 appearance: theme.appearance,
224 })
225 .collect()
226 }
227
228 /// Returns the icon theme with the specified name.
229 pub fn get_icon_theme(&self, name: &str) -> Result<Arc<IconTheme>> {
230 self.state
231 .read()
232 .icon_themes
233 .get(name)
234 .ok_or_else(|| anyhow!("icon theme not found: {name}"))
235 .cloned()
236 }
237
238 /// Removes the icon themes with the given names from the registry.
239 pub fn remove_icon_themes(&self, icon_themes_to_remove: &[SharedString]) {
240 self.state
241 .write()
242 .icon_themes
243 .retain(|name, _| !icon_themes_to_remove.contains(name))
244 }
245
246 /// Loads the icon theme from the specified path and adds it to the registry.
247 ///
248 /// The `icons_root_dir` parameter indicates the root directory from which
249 /// the relative paths to icons in the theme should be resolved against.
250 pub async fn load_icon_theme(
251 &self,
252 icon_theme_path: &Path,
253 icons_root_dir: &Path,
254 fs: Arc<dyn Fs>,
255 ) -> Result<()> {
256 let icon_theme_family = read_icon_theme(icon_theme_path, fs).await?;
257
258 let resolve_icon_path = |path: SharedString| {
259 icons_root_dir
260 .join(path.as_ref())
261 .to_string_lossy()
262 .to_string()
263 .into()
264 };
265
266 let mut state = self.state.write();
267 for icon_theme in icon_theme_family.themes {
268 let icon_theme = IconTheme {
269 id: uuid::Uuid::new_v4().to_string(),
270 name: icon_theme.name.into(),
271 appearance: match icon_theme.appearance {
272 AppearanceContent::Light => Appearance::Light,
273 AppearanceContent::Dark => Appearance::Dark,
274 },
275 directory_icons: DirectoryIcons {
276 collapsed: icon_theme.directory_icons.collapsed.map(resolve_icon_path),
277 expanded: icon_theme.directory_icons.expanded.map(resolve_icon_path),
278 },
279 chevron_icons: ChevronIcons {
280 collapsed: icon_theme.chevron_icons.collapsed.map(resolve_icon_path),
281 expanded: icon_theme.chevron_icons.expanded.map(resolve_icon_path),
282 },
283 file_icons: icon_theme
284 .file_icons
285 .into_iter()
286 .map(|(key, icon)| {
287 (
288 key,
289 IconDefinition {
290 path: resolve_icon_path(icon.path),
291 },
292 )
293 })
294 .collect(),
295 };
296
297 state
298 .icon_themes
299 .insert(icon_theme.name.clone(), Arc::new(icon_theme));
300 }
301
302 Ok(())
303 }
304}
305
306impl Default for ThemeRegistry {
307 fn default() -> Self {
308 Self::new(Box::new(()))
309 }
310}