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