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