theme_registry.rs

 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::{
 8    collections::HashMap,
 9    sync::{
10        atomic::{AtomicUsize, Ordering::SeqCst},
11        Arc,
12    },
13};
14
15pub struct ThemeRegistry {
16    assets: Box<dyn AssetSource>,
17    themes: Mutex<HashMap<String, Arc<Theme>>>,
18    theme_data: Mutex<HashMap<String, Arc<Value>>>,
19    font_cache: Arc<FontCache>,
20    next_theme_id: AtomicUsize,
21}
22
23impl ThemeRegistry {
24    pub fn new(source: impl AssetSource, font_cache: Arc<FontCache>) -> Arc<Self> {
25        let this = Arc::new(Self {
26            assets: Box::new(source),
27            themes: Default::default(),
28            theme_data: Default::default(),
29            next_theme_id: Default::default(),
30            font_cache,
31        });
32
33        #[cfg(any(test, feature = "test-support"))]
34        this.themes.lock().insert(
35            settings::EMPTY_THEME_NAME.to_string(),
36            gpui::fonts::with_font_cache(this.font_cache.clone(), || {
37                let mut theme = Theme::default();
38                theme.meta.id = this.next_theme_id.fetch_add(1, SeqCst);
39                theme.meta.name = settings::EMPTY_THEME_NAME.into();
40                Arc::new(theme)
41            }),
42        );
43
44        this
45    }
46
47    pub fn list(&self, staff: bool) -> impl Iterator<Item = ThemeMeta> + '_ {
48        let mut dirs = self.assets.list("themes/");
49
50        if !staff {
51            dirs = dirs
52                .into_iter()
53                .filter(|path| !path.starts_with("themes/staff"))
54                .collect()
55        }
56
57        dirs.into_iter().filter_map(|path| {
58            let filename = path.strip_prefix("themes/")?;
59            let theme_name = filename.strip_suffix(".json")?;
60            self.get(theme_name).ok().map(|theme| theme.meta.clone())
61        })
62    }
63
64    pub fn clear(&self) {
65        self.theme_data.lock().clear();
66        self.themes.lock().clear();
67    }
68
69    pub fn get(&self, name: &str) -> Result<Arc<Theme>> {
70        if let Some(theme) = self.themes.lock().get(name) {
71            return Ok(theme.clone());
72        }
73
74        let asset_path = format!("themes/{}.json", name);
75        let theme_json = self
76            .assets
77            .load(&asset_path)
78            .with_context(|| format!("failed to load theme file {}", asset_path))?;
79
80        // Allocate into the heap directly, the Theme struct is too large to fit in the stack.
81        let mut theme = fonts::with_font_cache(self.font_cache.clone(), || {
82            let mut theme = Box::new(Theme::default());
83            let mut deserializer = serde_json::Deserializer::from_slice(&theme_json);
84            let result = Theme::deserialize_in_place(&mut deserializer, &mut theme);
85            result.map(|_| theme)
86        })?;
87
88        // Reset name to be the file path, so that we can use it to access the stored themes
89        theme.meta.name = name.into();
90        theme.meta.id = self.next_theme_id.fetch_add(1, SeqCst);
91        let theme: Arc<Theme> = theme.into();
92        self.themes.lock().insert(name.to_string(), theme.clone());
93        Ok(theme)
94    }
95}