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