theme.rs

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