2022-09-08 21:11:48 +00:00
|
|
|
use crate::{Theme, ThemeMeta};
|
2021-09-15 18:56:20 +00:00
|
|
|
use anyhow::{Context, Result};
|
2021-08-26 22:06:00 +00:00
|
|
|
use gpui::{fonts, AssetSource, FontCache};
|
2021-08-24 23:33:56 +00:00
|
|
|
use parking_lot::Mutex;
|
2023-03-17 14:39:24 +00:00
|
|
|
use serde::Deserialize;
|
2022-04-01 17:45:08 +00:00
|
|
|
use serde_json::Value;
|
2021-09-15 18:56:20 +00:00
|
|
|
use std::{collections::HashMap, sync::Arc};
|
2021-08-24 23:33:56 +00:00
|
|
|
|
|
|
|
pub struct ThemeRegistry {
|
|
|
|
assets: Box<dyn AssetSource>,
|
|
|
|
themes: Mutex<HashMap<String, Arc<Theme>>>,
|
|
|
|
theme_data: Mutex<HashMap<String, Arc<Value>>>,
|
2021-08-26 22:06:00 +00:00
|
|
|
font_cache: Arc<FontCache>,
|
2021-08-24 23:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ThemeRegistry {
|
2022-09-08 22:47:27 +00:00
|
|
|
pub fn new(source: impl AssetSource, font_cache: Arc<FontCache>) -> Arc<Self> {
|
2021-08-24 23:33:56 +00:00
|
|
|
Arc::new(Self {
|
|
|
|
assets: Box::new(source),
|
|
|
|
themes: Default::default(),
|
|
|
|
theme_data: Default::default(),
|
2021-08-26 22:06:00 +00:00
|
|
|
font_cache,
|
2021-08-24 23:33:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-23 22:31:10 +00:00
|
|
|
pub fn list(&self, staff: bool) -> impl Iterator<Item = ThemeMeta> + '_ {
|
2022-09-08 22:06:08 +00:00
|
|
|
let mut dirs = self.assets.list("themes/");
|
|
|
|
|
2023-01-23 22:31:10 +00:00
|
|
|
if !staff {
|
2022-09-08 22:47:27 +00:00
|
|
|
dirs = dirs
|
|
|
|
.into_iter()
|
2023-01-23 22:31:10 +00:00
|
|
|
.filter(|path| !path.starts_with("themes/staff"))
|
2022-09-08 22:47:27 +00:00
|
|
|
.collect()
|
|
|
|
}
|
2022-09-08 22:06:08 +00:00
|
|
|
|
|
|
|
dirs.into_iter().filter_map(|path| {
|
2021-08-24 23:33:56 +00:00
|
|
|
let filename = path.strip_prefix("themes/")?;
|
2022-04-01 17:45:08 +00:00
|
|
|
let theme_name = filename.strip_suffix(".json")?;
|
2022-09-08 21:11:48 +00:00
|
|
|
self.get(theme_name).ok().map(|theme| theme.meta.clone())
|
2021-08-24 23:33:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&self) {
|
|
|
|
self.theme_data.lock().clear();
|
|
|
|
self.themes.lock().clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, name: &str) -> Result<Arc<Theme>> {
|
|
|
|
if let Some(theme) = self.themes.lock().get(name) {
|
|
|
|
return Ok(theme.clone());
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:45:08 +00:00
|
|
|
let asset_path = format!("themes/{}.json", name);
|
|
|
|
let theme_json = self
|
|
|
|
.assets
|
|
|
|
.load(&asset_path)
|
|
|
|
.with_context(|| format!("failed to load theme file {}", asset_path))?;
|
|
|
|
|
2023-02-07 18:39:48 +00:00
|
|
|
// Allocate into the heap directly, the Theme struct is too large to fit in the stack.
|
2023-03-17 14:39:24 +00:00
|
|
|
let mut theme = fonts::with_font_cache(self.font_cache.clone(), || {
|
|
|
|
let mut theme = Box::new(Theme::default());
|
|
|
|
let mut deserializer = serde_json::Deserializer::from_slice(&theme_json);
|
|
|
|
let result = Theme::deserialize_in_place(&mut deserializer, &mut theme);
|
|
|
|
result.map(|_| theme)
|
2021-08-26 22:06:00 +00:00
|
|
|
})?;
|
|
|
|
|
2022-09-08 22:06:08 +00:00
|
|
|
// Reset name to be the file path, so that we can use it to access the stored themes
|
2023-03-17 14:39:24 +00:00
|
|
|
theme.meta.name = name.into();
|
|
|
|
let theme: Arc<Theme> = theme.into();
|
2021-08-24 23:33:56 +00:00
|
|
|
self.themes.lock().insert(name.to_string(), theme.clone());
|
|
|
|
Ok(theme)
|
|
|
|
}
|
|
|
|
}
|