1use crate::{Theme, ThemeMeta};
2use anyhow::{Context, Result};
3use gpui::{fonts, AssetSource, FontCache};
4use parking_lot::Mutex;
5use serde::Deserialize;
6use serde_json::Value;
7use std::{collections::HashMap, sync::Arc};
8
9pub struct ThemeRegistry {
10 assets: Box<dyn AssetSource>,
11 themes: Mutex<HashMap<String, Arc<Theme>>>,
12 theme_data: Mutex<HashMap<String, Arc<Value>>>,
13 font_cache: Arc<FontCache>,
14}
15
16impl ThemeRegistry {
17 pub fn new(source: impl AssetSource, font_cache: Arc<FontCache>) -> Arc<Self> {
18 Arc::new(Self {
19 assets: Box::new(source),
20 themes: Default::default(),
21 theme_data: Default::default(),
22 font_cache,
23 })
24 }
25
26 pub fn list(&self, staff: bool) -> impl Iterator<Item = ThemeMeta> + '_ {
27 let mut dirs = self.assets.list("themes/");
28
29 if !staff {
30 dirs = dirs
31 .into_iter()
32 .filter(|path| !path.starts_with("themes/staff"))
33 .collect()
34 }
35
36 dirs.into_iter().filter_map(|path| {
37 let filename = path.strip_prefix("themes/")?;
38 let theme_name = filename.strip_suffix(".json")?;
39 self.get(theme_name).ok().map(|theme| theme.meta.clone())
40 })
41 }
42
43 pub fn clear(&self) {
44 self.theme_data.lock().clear();
45 self.themes.lock().clear();
46 }
47
48 pub fn get(&self, name: &str) -> Result<Arc<Theme>> {
49 if let Some(theme) = self.themes.lock().get(name) {
50 return Ok(theme.clone());
51 }
52
53 let asset_path = format!("themes/{}.json", name);
54 let theme_json = self
55 .assets
56 .load(&asset_path)
57 .with_context(|| format!("failed to load theme file {}", asset_path))?;
58
59 // Allocate into the heap directly, the Theme struct is too large to fit in the stack.
60 let mut theme = fonts::with_font_cache(self.font_cache.clone(), || {
61 let mut theme = Box::new(Theme::default());
62 let mut deserializer = serde_json::Deserializer::from_slice(&theme_json);
63 let result = Theme::deserialize_in_place(&mut deserializer, &mut theme);
64 result.map(|_| theme)
65 })?;
66
67 // Reset name to be the file path, so that we can use it to access the stored themes
68 theme.meta.name = name.into();
69 let theme: Arc<Theme> = theme.into();
70 self.themes.lock().insert(name.to_string(), theme.clone());
71 Ok(theme)
72 }
73}