settings.rs

 1use crate::theme::{self, DEFAULT_THEME_NAME};
 2use anyhow::Result;
 3use gpui::font_cache::{FamilyId, FontCache};
 4use postage::watch;
 5use std::sync::Arc;
 6
 7pub use theme::{HighlightId, HighlightMap, Theme, ThemeRegistry};
 8
 9#[derive(Clone)]
10pub struct Settings {
11    pub buffer_font_family: FamilyId,
12    pub buffer_font_size: f32,
13    pub tab_size: usize,
14    pub theme: Arc<Theme>,
15}
16
17impl Settings {
18    #[cfg(any(test, feature = "test-support"))]
19    pub fn test(cx: &gpui::AppContext) -> Self {
20        use crate::assets::Assets;
21        use gpui::AssetSource;
22
23        lazy_static::lazy_static! {
24            static ref DEFAULT_THEME: parking_lot::Mutex<Option<Arc<Theme>>> = Default::default();
25            static ref FONTS: Vec<Arc<Vec<u8>>> = Assets
26                .list("fonts")
27                .into_iter()
28                .map(|f| Arc::new(Assets.load(&f).unwrap().to_vec()))
29                .collect();
30        }
31
32        cx.platform().fonts().add_fonts(&FONTS).unwrap();
33
34        let mut theme_guard = DEFAULT_THEME.lock();
35        let theme = if let Some(theme) = theme_guard.as_ref() {
36            theme.clone()
37        } else {
38            let theme = ThemeRegistry::new(Assets, cx.font_cache().clone())
39                .get(DEFAULT_THEME_NAME)
40                .expect("failed to load default theme in tests");
41            *theme_guard = Some(theme.clone());
42            theme
43        };
44
45        Self::new(cx.font_cache(), theme).unwrap()
46    }
47
48    pub fn new(font_cache: &FontCache, theme: Arc<Theme>) -> Result<Self> {
49        Ok(Self {
50            buffer_font_family: font_cache.load_family(&["Inconsolata"])?,
51            buffer_font_size: 16.,
52            tab_size: 4,
53            theme,
54        })
55    }
56
57    pub fn with_tab_size(mut self, tab_size: usize) -> Self {
58        self.tab_size = tab_size;
59        self
60    }
61}
62
63#[cfg(any(test, feature = "test-support"))]
64pub fn test(cx: &gpui::AppContext) -> (watch::Sender<Settings>, watch::Receiver<Settings>) {
65    watch::channel_with(Settings::test(cx))
66}
67
68pub fn channel(
69    font_cache: &FontCache,
70    themes: &ThemeRegistry,
71) -> Result<(watch::Sender<Settings>, watch::Receiver<Settings>)> {
72    let theme = match themes.get(DEFAULT_THEME_NAME) {
73        Ok(theme) => theme,
74        Err(err) => {
75            panic!("failed to deserialize default theme: {:?}", err)
76        }
77    };
78    Ok(watch::channel_with(Settings::new(font_cache, theme)?))
79}