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        lazy_static::lazy_static! {
21            static ref DEFAULT_THEME: parking_lot::Mutex<Option<Arc<Theme>>> = Default::default();
22        }
23
24        let mut theme_guard = DEFAULT_THEME.lock();
25        let theme = if let Some(theme) = theme_guard.as_ref() {
26            theme.clone()
27        } else {
28            let theme = ThemeRegistry::new(crate::assets::Assets, cx.font_cache().clone())
29                .get(DEFAULT_THEME_NAME)
30                .expect("failed to load default theme in tests");
31            *theme_guard = Some(theme.clone());
32            theme
33        };
34
35        Self::new(cx.font_cache(), theme).unwrap()
36    }
37
38    pub fn new(font_cache: &FontCache, theme: Arc<Theme>) -> Result<Self> {
39        Ok(Self {
40            buffer_font_family: font_cache.load_family(&["Fira Code", "Monaco"])?,
41            buffer_font_size: 14.0,
42            tab_size: 4,
43            theme,
44        })
45    }
46
47    pub fn with_tab_size(mut self, tab_size: usize) -> Self {
48        self.tab_size = tab_size;
49        self
50    }
51}
52
53#[cfg(any(test, feature = "test-support"))]
54pub fn test(cx: &gpui::AppContext) -> (watch::Sender<Settings>, watch::Receiver<Settings>) {
55    watch::channel_with(Settings::test(cx))
56}
57
58pub fn channel(
59    font_cache: &FontCache,
60    themes: &ThemeRegistry,
61) -> Result<(watch::Sender<Settings>, watch::Receiver<Settings>)> {
62    let theme = match themes.get(DEFAULT_THEME_NAME) {
63        Ok(theme) => theme,
64        Err(err) => {
65            panic!("failed to deserialize default theme: {:?}", err)
66        }
67    };
68    Ok(watch::channel_with(Settings::new(font_cache, theme)?))
69}