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    ThemeSettings::register(cx);
 66
 67    let mut prev_buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size;
 68    cx.observe_global::<SettingsStore>(move |cx| {
 69        let buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size;
 70        if buffer_font_size != prev_buffer_font_size {
 71            prev_buffer_font_size = buffer_font_size;
 72            reset_font_size(cx);
 73        }
 74    })
 75    .detach();
 76
 77    match themes_to_load {
 78        LoadThemes::JustBase => (),
 79        LoadThemes::All => cx.global_mut::<ThemeRegistry>().load_user_themes(),
 80    }
 81}
 82
 83pub trait ActiveTheme {
 84    fn theme(&self) -> &Arc<Theme>;
 85}
 86
 87impl ActiveTheme for AppContext {
 88    fn theme(&self) -> &Arc<Theme> {
 89        &ThemeSettings::get_global(self).active_theme
 90    }
 91}
 92
 93pub struct ThemeFamily {
 94    pub id: String,
 95    pub name: SharedString,
 96    pub author: SharedString,
 97    pub themes: Vec<Theme>,
 98    pub scales: ColorScales,
 99}
100
101impl ThemeFamily {}
102
103pub struct Theme {
104    pub id: String,
105    pub name: SharedString,
106    pub appearance: Appearance,
107    pub styles: ThemeStyles,
108}
109
110impl Theme {
111    /// Returns the [`SystemColors`] for the theme.
112    #[inline(always)]
113    pub fn system(&self) -> &SystemColors {
114        &self.styles.system
115    }
116
117    /// Returns the [`PlayerColors`] for the theme.
118    #[inline(always)]
119    pub fn players(&self) -> &PlayerColors {
120        &self.styles.player
121    }
122
123    /// Returns the [`ThemeColors`] for the theme.
124    #[inline(always)]
125    pub fn colors(&self) -> &ThemeColors {
126        &self.styles.colors
127    }
128
129    /// Returns the [`SyntaxTheme`] for the theme.
130    #[inline(always)]
131    pub fn syntax(&self) -> &Arc<SyntaxTheme> {
132        &self.styles.syntax
133    }
134
135    /// Returns the [`StatusColors`] for the theme.
136    #[inline(always)]
137    pub fn status(&self) -> &StatusColors {
138        &self.styles.status
139    }
140
141    /// Returns the color for the syntax node with the given name.
142    #[inline(always)]
143    pub fn syntax_color(&self, name: &str) -> Hsla {
144        self.syntax().color(name)
145    }
146
147    /// Returns the [`Appearance`] for the theme.
148    #[inline(always)]
149    pub fn appearance(&self) -> Appearance {
150        self.appearance
151    }
152}
153
154pub fn color_alpha(color: Hsla, alpha: f32) -> Hsla {
155    let mut color = color;
156    color.a = alpha;
157    color
158}