theme2.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
 76// impl<'a> ActiveTheme for WindowContext<'a> {
 77//     fn theme(&self) -> &Arc<Theme> {
 78//         &ThemeSettings::get_global(self.app()).active_theme
 79//     }
 80// }
 81
 82pub struct ThemeFamily {
 83    pub id: String,
 84    pub name: SharedString,
 85    pub author: SharedString,
 86    pub themes: Vec<Theme>,
 87    pub scales: ColorScales,
 88}
 89
 90impl ThemeFamily {}
 91
 92pub struct Theme {
 93    pub id: String,
 94    pub name: SharedString,
 95    pub appearance: Appearance,
 96    pub styles: ThemeStyles,
 97}
 98
 99impl Theme {
100    /// Returns the [`SystemColors`] for the theme.
101    #[inline(always)]
102    pub fn system(&self) -> &SystemColors {
103        &self.styles.system
104    }
105
106    /// Returns the [`PlayerColors`] for the theme.
107    #[inline(always)]
108    pub fn players(&self) -> &PlayerColors {
109        &self.styles.player
110    }
111
112    /// Returns the [`ThemeColors`] for the theme.
113    #[inline(always)]
114    pub fn colors(&self) -> &ThemeColors {
115        &self.styles.colors
116    }
117
118    /// Returns the [`SyntaxTheme`] for the theme.
119    #[inline(always)]
120    pub fn syntax(&self) -> &Arc<SyntaxTheme> {
121        &self.styles.syntax
122    }
123
124    /// Returns the [`StatusColors`] for the theme.
125    #[inline(always)]
126    pub fn status(&self) -> &StatusColors {
127        &self.styles.status
128    }
129
130    /// Returns the color for the syntax node with the given name.
131    #[inline(always)]
132    pub fn syntax_color(&self, name: &str) -> Hsla {
133        self.syntax().color(name)
134    }
135
136    /// Returns the [`DiagnosticStyle`] for the theme.
137    #[inline(always)]
138    pub fn diagnostic_style(&self) -> DiagnosticStyle {
139        DiagnosticStyle {
140            error: self.status().error,
141            warning: self.status().warning,
142            info: self.status().info,
143            hint: self.status().info,
144            ignored: self.status().ignored,
145        }
146    }
147
148    /// Returns the [`Appearance`] for the theme.
149    #[inline(always)]
150    pub fn appearance(&self) -> Appearance {
151        self.appearance
152    }
153}
154
155#[derive(Clone, Debug, Default)]
156pub struct DiagnosticStyle {
157    pub error: Hsla,
158    pub warning: Hsla,
159    pub info: Hsla,
160    pub hint: Hsla,
161    pub ignored: Hsla,
162}
163
164pub fn color_alpha(color: Hsla, alpha: f32) -> Hsla {
165    let mut color = color;
166    color.a = alpha;
167    color
168}