theme.rs

  1mod default_colors;
  2mod default_theme;
  3mod one_themes;
  4pub mod prelude;
  5mod registry;
  6mod scale;
  7mod settings;
  8mod styles;
  9#[cfg(not(feature = "importing-themes"))]
 10mod themes;
 11mod user_theme;
 12
 13use std::sync::Arc;
 14
 15use ::settings::Settings;
 16pub use default_colors::*;
 17pub use default_theme::*;
 18pub use registry::*;
 19pub use scale::*;
 20pub use settings::*;
 21pub use styles::*;
 22#[cfg(not(feature = "importing-themes"))]
 23pub use themes::*;
 24pub use user_theme::*;
 25
 26use gpui::{AppContext, Hsla, SharedString};
 27use serde::Deserialize;
 28
 29#[derive(Debug, PartialEq, Clone, Copy, Deserialize)]
 30pub enum Appearance {
 31    Light,
 32    Dark,
 33}
 34
 35impl Appearance {
 36    pub fn is_light(&self) -> bool {
 37        match self {
 38            Self::Light => true,
 39            Self::Dark => false,
 40        }
 41    }
 42}
 43
 44#[derive(Debug, PartialEq, Eq, Clone, Copy)]
 45pub enum LoadThemes {
 46    /// Only load the base theme.
 47    ///
 48    /// No user themes will be loaded.
 49    JustBase,
 50
 51    /// Load all of the built-in themes.
 52    All,
 53}
 54
 55pub fn init(themes_to_load: LoadThemes, cx: &mut AppContext) {
 56    cx.set_global(ThemeRegistry::default());
 57
 58    match themes_to_load {
 59        LoadThemes::JustBase => (),
 60        LoadThemes::All => cx.global_mut::<ThemeRegistry>().load_user_themes(),
 61    }
 62
 63    ThemeSettings::register(cx);
 64}
 65
 66pub trait ActiveTheme {
 67    fn theme(&self) -> &Arc<Theme>;
 68}
 69
 70impl ActiveTheme for AppContext {
 71    fn theme(&self) -> &Arc<Theme> {
 72        &ThemeSettings::get_global(self).active_theme
 73    }
 74}
 75
 76pub struct ThemeFamily {
 77    pub id: String,
 78    pub name: SharedString,
 79    pub author: SharedString,
 80    pub themes: Vec<Theme>,
 81    pub scales: ColorScales,
 82}
 83
 84impl ThemeFamily {}
 85
 86pub struct Theme {
 87    pub id: String,
 88    pub name: SharedString,
 89    pub appearance: Appearance,
 90    pub styles: ThemeStyles,
 91}
 92
 93impl Theme {
 94    /// Returns the [`SystemColors`] for the theme.
 95    #[inline(always)]
 96    pub fn system(&self) -> &SystemColors {
 97        &self.styles.system
 98    }
 99
100    /// Returns the [`PlayerColors`] for the theme.
101    #[inline(always)]
102    pub fn players(&self) -> &PlayerColors {
103        &self.styles.player
104    }
105
106    /// Returns the [`ThemeColors`] for the theme.
107    #[inline(always)]
108    pub fn colors(&self) -> &ThemeColors {
109        &self.styles.colors
110    }
111
112    /// Returns the [`SyntaxTheme`] for the theme.
113    #[inline(always)]
114    pub fn syntax(&self) -> &Arc<SyntaxTheme> {
115        &self.styles.syntax
116    }
117
118    /// Returns the [`StatusColors`] for the theme.
119    #[inline(always)]
120    pub fn status(&self) -> &StatusColors {
121        &self.styles.status
122    }
123
124    /// Returns the color for the syntax node with the given name.
125    #[inline(always)]
126    pub fn syntax_color(&self, name: &str) -> Hsla {
127        self.syntax().color(name)
128    }
129
130    /// Returns the [`Appearance`] for the theme.
131    #[inline(always)]
132    pub fn appearance(&self) -> Appearance {
133        self.appearance
134    }
135}
136
137pub fn color_alpha(color: Hsla, alpha: f32) -> Hsla {
138    let mut color = color;
139    color.a = alpha;
140    color
141}