1#![deny(missing_docs)]
2
3//! # Theme
4//!
5//! This crate provides the theme system for Zed.
6//!
7//! ## Overview
8//!
9//! A theme is a collection of colors used to build a consistent appearance for UI components across the application.
10
11mod default_colors;
12mod default_theme;
13mod font_family_cache;
14mod one_themes;
15/// A prelude for working with the theme system.
16///
17/// TODO: remove this. This only publishes default colors.
18pub mod prelude;
19mod registry;
20mod scale;
21mod schema;
22mod settings;
23mod styles;
24
25use std::sync::Arc;
26
27use ::settings::{Settings, SettingsStore};
28pub use default_colors::*;
29pub use default_theme::*;
30pub use font_family_cache::*;
31pub use registry::*;
32pub use scale::*;
33pub use schema::*;
34pub use settings::*;
35pub use styles::*;
36
37use gpui::{
38 px, AppContext, AssetSource, Hsla, Pixels, SharedString, WindowAppearance,
39 WindowBackgroundAppearance,
40};
41use serde::Deserialize;
42
43/// Defines window border radius for platforms that use client side decorations.
44pub const CLIENT_SIDE_DECORATION_ROUNDING: Pixels = px(10.0);
45/// Defines window shadow size for platforms that use client side decorations.
46pub const CLIENT_SIDE_DECORATION_SHADOW: Pixels = px(10.0);
47
48/// The appearance of the theme.
49#[derive(Debug, PartialEq, Clone, Copy, Deserialize)]
50pub enum Appearance {
51 /// A light appearance.
52 Light,
53 /// A dark appearance.
54 Dark,
55}
56
57impl Appearance {
58 /// Returns whether the appearance is light.
59 pub fn is_light(&self) -> bool {
60 match self {
61 Self::Light => true,
62 Self::Dark => false,
63 }
64 }
65}
66
67impl From<WindowAppearance> for Appearance {
68 fn from(value: WindowAppearance) -> Self {
69 match value {
70 WindowAppearance::Dark | WindowAppearance::VibrantDark => Self::Dark,
71 WindowAppearance::Light | WindowAppearance::VibrantLight => Self::Light,
72 }
73 }
74}
75
76/// Which themes should be loaded. This is used primarlily for testing.
77pub enum LoadThemes {
78 /// Only load the base theme.
79 ///
80 /// No user themes will be loaded.
81 JustBase,
82
83 /// Load all of the built-in themes.
84 All(Box<dyn AssetSource>),
85}
86
87/// Initialize the theme system.
88pub fn init(themes_to_load: LoadThemes, cx: &mut AppContext) {
89 let (assets, load_user_themes) = match themes_to_load {
90 LoadThemes::JustBase => (Box::new(()) as Box<dyn AssetSource>, false),
91 LoadThemes::All(assets) => (assets, true),
92 };
93 ThemeRegistry::set_global(assets, cx);
94
95 if load_user_themes {
96 ThemeRegistry::global(cx).load_bundled_themes();
97 }
98
99 ThemeSettings::register(cx);
100 FontFamilyCache::init_global(cx);
101
102 let mut prev_buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size;
103 cx.observe_global::<SettingsStore>(move |cx| {
104 let buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size;
105 if buffer_font_size != prev_buffer_font_size {
106 prev_buffer_font_size = buffer_font_size;
107 reset_buffer_font_size(cx);
108 }
109 })
110 .detach();
111}
112
113/// Implementing this trait allows accessing the active theme.
114pub trait ActiveTheme {
115 /// Returns the active theme.
116 fn theme(&self) -> &Arc<Theme>;
117}
118
119impl ActiveTheme for AppContext {
120 fn theme(&self) -> &Arc<Theme> {
121 &ThemeSettings::get_global(self).active_theme
122 }
123}
124
125/// A theme family is a grouping of themes under a single name.
126///
127/// For example, the "One" theme family contains the "One Light" and "One Dark" themes.
128///
129/// It can also be used to package themes with many variants.
130///
131/// For example, the "Atelier" theme family contains "Cave", "Dune", "Estuary", "Forest", "Heath", etc.
132pub struct ThemeFamily {
133 /// The unique identifier for the theme family.
134 pub id: String,
135 /// The name of the theme family. This will be displayed in the UI, such as when adding or removing a theme family.
136 pub name: SharedString,
137 /// The author of the theme family.
138 pub author: SharedString,
139 /// The [Theme]s in the family.
140 pub themes: Vec<Theme>,
141 /// The color scales used by the themes in the family.
142 /// Note: This will be removed in the future.
143 pub scales: ColorScales,
144}
145
146impl ThemeFamily {}
147
148/// A theme is the primary mechanism for defining the appearance of the UI.
149#[derive(Clone)]
150pub struct Theme {
151 /// The unique identifier for the theme.
152 pub id: String,
153 /// The name of the theme.
154 pub name: SharedString,
155 /// The appearance of the theme (light or dark).
156 pub appearance: Appearance,
157 /// The colors and other styles for the theme.
158 pub styles: ThemeStyles,
159}
160
161impl Theme {
162 /// Returns the [`SystemColors`] for the theme.
163 #[inline(always)]
164 pub fn system(&self) -> &SystemColors {
165 &self.styles.system
166 }
167
168 /// Returns the [`AccentColors`] for the theme.
169 #[inline(always)]
170 pub fn accents(&self) -> &AccentColors {
171 &self.styles.accents
172 }
173
174 /// Returns the [`PlayerColors`] for the theme.
175 #[inline(always)]
176 pub fn players(&self) -> &PlayerColors {
177 &self.styles.player
178 }
179
180 /// Returns the [`ThemeColors`] for the theme.
181 #[inline(always)]
182 pub fn colors(&self) -> &ThemeColors {
183 &self.styles.colors
184 }
185
186 /// Returns the [`SyntaxTheme`] for the theme.
187 #[inline(always)]
188 pub fn syntax(&self) -> &Arc<SyntaxTheme> {
189 &self.styles.syntax
190 }
191
192 /// Returns the [`StatusColors`] for the theme.
193 #[inline(always)]
194 pub fn status(&self) -> &StatusColors {
195 &self.styles.status
196 }
197
198 /// Returns the color for the syntax node with the given name.
199 #[inline(always)]
200 pub fn syntax_color(&self, name: &str) -> Hsla {
201 self.syntax().color(name)
202 }
203
204 /// Returns the [`Appearance`] for the theme.
205 #[inline(always)]
206 pub fn appearance(&self) -> Appearance {
207 self.appearance
208 }
209
210 /// Returns the [`WindowBackgroundAppearance`] for the theme.
211 #[inline(always)]
212 pub fn window_background_appearance(&self) -> WindowBackgroundAppearance {
213 self.styles.window_background_appearance
214 }
215}
216
217/// Compounds a color with an alpha value.
218/// TODO: Replace this with a method on Hsla.
219pub fn color_alpha(color: Hsla, alpha: f32) -> Hsla {
220 let mut color = color;
221 color.a = alpha;
222 color
223}