registry.rs

  1use std::sync::Arc;
  2use std::{fmt::Debug, path::Path};
  3
  4use anyhow::{anyhow, Context, Result};
  5use collections::HashMap;
  6use derive_more::{Deref, DerefMut};
  7use fs::Fs;
  8use futures::StreamExt;
  9use gpui::{AppContext, 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: &AppContext) -> 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 AppContext) -> 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 AppContext) {
 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 icon theme with the specified name.
216    pub fn get_icon_theme(&self, name: &str) -> Result<Arc<IconTheme>> {
217        self.state
218            .read()
219            .icon_themes
220            .get(name)
221            .ok_or_else(|| anyhow!("icon theme not found: {name}"))
222            .cloned()
223    }
224
225    /// Removes the icon themes with the given names from the registry.
226    pub fn remove_icon_themes(&self, icon_themes_to_remove: &[SharedString]) {
227        self.state
228            .write()
229            .icon_themes
230            .retain(|name, _| !icon_themes_to_remove.contains(name))
231    }
232
233    /// Loads the icon theme from the specified path and adds it to the registry.
234    ///
235    /// The `icons_root_dir` parameter indicates the root directory from which
236    /// the relative paths to icons in the theme should be resolved against.
237    pub async fn load_icon_theme(
238        &self,
239        icon_theme_path: &Path,
240        icons_root_dir: &Path,
241        fs: Arc<dyn Fs>,
242    ) -> Result<()> {
243        let icon_theme_family = read_icon_theme(icon_theme_path, fs).await?;
244
245        let mut state = self.state.write();
246        for icon_theme in icon_theme_family.themes {
247            let icon_theme = IconTheme {
248                id: uuid::Uuid::new_v4().to_string(),
249                name: icon_theme.name.into(),
250                appearance: match icon_theme.appearance {
251                    AppearanceContent::Light => Appearance::Light,
252                    AppearanceContent::Dark => Appearance::Dark,
253                },
254                directory_icons: DirectoryIcons {
255                    collapsed: icon_theme.directory_icons.collapsed,
256                    expanded: icon_theme.directory_icons.expanded,
257                },
258                chevron_icons: ChevronIcons {
259                    collapsed: icon_theme.chevron_icons.collapsed,
260                    expanded: icon_theme.chevron_icons.expanded,
261                },
262                file_icons: icon_theme
263                    .file_icons
264                    .into_iter()
265                    .map(|(key, icon)| {
266                        let path = icons_root_dir.join(icon.path.as_ref());
267
268                        (
269                            key,
270                            IconDefinition {
271                                path: path.to_string_lossy().to_string().into(),
272                            },
273                        )
274                    })
275                    .collect(),
276            };
277
278            state
279                .icon_themes
280                .insert(icon_theme.name.clone(), Arc::new(icon_theme));
281        }
282
283        Ok(())
284    }
285}
286
287impl Default for ThemeRegistry {
288    fn default() -> Self {
289        Self::new(Box::new(()))
290    }
291}