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        this.themes.lock().insert(
34            settings::EMPTY_THEME_NAME.to_string(),
35            gpui::fonts::with_font_cache(this.font_cache.clone(), || {
36                let mut theme = Theme::default();
37                theme.meta.id = this.next_theme_id.fetch_add(1, SeqCst);
38                theme.meta.name = settings::EMPTY_THEME_NAME.into();
39                Arc::new(theme)
40            }),
41        );
42
43        this
44    }
45
46    pub fn list(&self, staff: bool) -> impl Iterator<Item = ThemeMeta> + '_ {
47        let mut dirs = self.assets.list("themes/");
48
49        if !staff {
50            dirs = dirs
51                .into_iter()
52                .filter(|path| !path.starts_with("themes/staff"))
53                .collect()
54        }
55
56        dirs.into_iter().filter_map(|path| {
57            let filename = path.strip_prefix("themes/")?;
58            let theme_name = filename.strip_suffix(".json")?;
59            self.get(theme_name).ok().map(|theme| theme.meta.clone())
60        })
61    }
62
63    pub fn clear(&self) {
64        self.theme_data.lock().clear();
65        self.themes.lock().clear();
66    }
67
68    pub fn get(&self, name: &str) -> Result<Arc<Theme>> {
69        if let Some(theme) = self.themes.lock().get(name) {
70            return Ok(theme.clone());
71        }
72
73        let asset_path = format!("themes/{}.json", name);
74        let theme_json = self
75            .assets
76            .load(&asset_path)
77            .with_context(|| format!("failed to load theme file {}", asset_path))?;
78
79        // Allocate into the heap directly, the Theme struct is too large to fit in the stack.
80        let mut theme = fonts::with_font_cache(self.font_cache.clone(), || {
81            let mut theme = Box::new(Theme::default());
82            let mut deserializer = serde_json::Deserializer::from_slice(&theme_json);
83            let result = Theme::deserialize_in_place(&mut deserializer, &mut theme);
84            result.map(|_| theme)
85        })?;
86
87        // Reset name to be the file path, so that we can use it to access the stored themes
88        theme.meta.name = name.into();
89        theme.meta.id = self.next_theme_id.fetch_add(1, SeqCst);
90        let theme: Arc<Theme> = theme.into();
91        self.themes.lock().insert(name.to_string(), theme.clone());
92        Ok(theme)
93    }
94}