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 fallback_themes;
13mod font_family_cache;
14mod registry;
15mod scale;
16mod schema;
17mod settings;
18mod styles;
19
20use std::sync::Arc;
21
22use ::settings::{Settings, SettingsStore};
23pub use default_colors::*;
24pub use font_family_cache::*;
25pub use registry::*;
26pub use scale::*;
27pub use schema::*;
28pub use settings::*;
29pub use styles::*;
30
31use gpui::{
32 px, AppContext, AssetSource, HighlightStyle, Hsla, Pixels, Refineable, SharedString,
33 WindowAppearance, WindowBackgroundAppearance,
34};
35use serde::Deserialize;
36use uuid::Uuid;
37
38/// Defines window border radius for platforms that use client side decorations.
39pub const CLIENT_SIDE_DECORATION_ROUNDING: Pixels = px(10.0);
40/// Defines window shadow size for platforms that use client side decorations.
41pub const CLIENT_SIDE_DECORATION_SHADOW: Pixels = px(10.0);
42
43/// The appearance of the theme.
44#[derive(Debug, PartialEq, Clone, Copy, Deserialize)]
45pub enum Appearance {
46 /// A light appearance.
47 Light,
48 /// A dark appearance.
49 Dark,
50}
51
52impl Appearance {
53 /// Returns whether the appearance is light.
54 pub fn is_light(&self) -> bool {
55 match self {
56 Self::Light => true,
57 Self::Dark => false,
58 }
59 }
60}
61
62impl From<WindowAppearance> for Appearance {
63 fn from(value: WindowAppearance) -> Self {
64 match value {
65 WindowAppearance::Dark | WindowAppearance::VibrantDark => Self::Dark,
66 WindowAppearance::Light | WindowAppearance::VibrantLight => Self::Light,
67 }
68 }
69}
70
71/// Which themes should be loaded. This is used primarlily for testing.
72pub enum LoadThemes {
73 /// Only load the base theme.
74 ///
75 /// No user themes will be loaded.
76 JustBase,
77
78 /// Load all of the built-in themes.
79 All(Box<dyn AssetSource>),
80}
81
82/// Initialize the theme system.
83pub fn init(themes_to_load: LoadThemes, cx: &mut AppContext) {
84 let (assets, load_user_themes) = match themes_to_load {
85 LoadThemes::JustBase => (Box::new(()) as Box<dyn AssetSource>, false),
86 LoadThemes::All(assets) => (assets, true),
87 };
88 ThemeRegistry::set_global(assets, cx);
89
90 if load_user_themes {
91 ThemeRegistry::global(cx).load_bundled_themes();
92 }
93
94 ThemeSettings::register(cx);
95 FontFamilyCache::init_global(cx);
96
97 let mut prev_buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size;
98 cx.observe_global::<SettingsStore>(move |cx| {
99 let buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size;
100 if buffer_font_size != prev_buffer_font_size {
101 prev_buffer_font_size = buffer_font_size;
102 reset_buffer_font_size(cx);
103 }
104 })
105 .detach();
106}
107
108/// Implementing this trait allows accessing the active theme.
109pub trait ActiveTheme {
110 /// Returns the active theme.
111 fn theme(&self) -> &Arc<Theme>;
112}
113
114impl ActiveTheme for AppContext {
115 fn theme(&self) -> &Arc<Theme> {
116 &ThemeSettings::get_global(self).active_theme
117 }
118}
119
120/// A theme family is a grouping of themes under a single name.
121///
122/// For example, the "One" theme family contains the "One Light" and "One Dark" themes.
123///
124/// It can also be used to package themes with many variants.
125///
126/// For example, the "Atelier" theme family contains "Cave", "Dune", "Estuary", "Forest", "Heath", etc.
127pub struct ThemeFamily {
128 /// The unique identifier for the theme family.
129 pub id: String,
130 /// The name of the theme family. This will be displayed in the UI, such as when adding or removing a theme family.
131 pub name: SharedString,
132 /// The author of the theme family.
133 pub author: SharedString,
134 /// The [Theme]s in the family.
135 pub themes: Vec<Theme>,
136 /// The color scales used by the themes in the family.
137 /// Note: This will be removed in the future.
138 pub scales: ColorScales,
139}
140
141impl ThemeFamily {
142 // This is on ThemeFamily because we will have variables here we will need
143 // in the future to resolve @references.
144 /// Refines ThemeContent into a theme, merging it's contents with the base theme.
145 pub fn refine_theme(&self, theme: &ThemeContent) -> Theme {
146 let appearance = match theme.appearance {
147 AppearanceContent::Light => Appearance::Light,
148 AppearanceContent::Dark => Appearance::Dark,
149 };
150
151 let mut refined_theme_colors = match theme.appearance {
152 AppearanceContent::Light => ThemeColors::light(),
153 AppearanceContent::Dark => ThemeColors::dark(),
154 };
155 refined_theme_colors.refine(&theme.style.theme_colors_refinement());
156
157 let mut refined_status_colors = match theme.appearance {
158 AppearanceContent::Light => StatusColors::light(),
159 AppearanceContent::Dark => StatusColors::dark(),
160 };
161 refined_status_colors.refine(&theme.style.status_colors_refinement());
162
163 let mut refined_player_colors = match theme.appearance {
164 AppearanceContent::Light => PlayerColors::light(),
165 AppearanceContent::Dark => PlayerColors::dark(),
166 };
167 refined_player_colors.merge(&theme.style.players);
168
169 let mut refined_accent_colors = match theme.appearance {
170 AppearanceContent::Light => AccentColors::light(),
171 AppearanceContent::Dark => AccentColors::dark(),
172 };
173 refined_accent_colors.merge(&theme.style.accents);
174
175 let syntax_highlights = theme
176 .style
177 .syntax
178 .iter()
179 .map(|(syntax_token, highlight)| {
180 (
181 syntax_token.clone(),
182 HighlightStyle {
183 color: highlight
184 .color
185 .as_ref()
186 .and_then(|color| try_parse_color(color).ok()),
187 background_color: highlight
188 .background_color
189 .as_ref()
190 .and_then(|color| try_parse_color(color).ok()),
191 font_style: highlight.font_style.map(Into::into),
192 font_weight: highlight.font_weight.map(Into::into),
193 ..Default::default()
194 },
195 )
196 })
197 .collect::<Vec<_>>();
198 let syntax_theme = SyntaxTheme::merge(Arc::new(SyntaxTheme::default()), syntax_highlights);
199
200 let window_background_appearance = theme
201 .style
202 .window_background_appearance
203 .map(Into::into)
204 .unwrap_or_default();
205
206 Theme {
207 id: uuid::Uuid::new_v4().to_string(),
208 name: theme.name.clone().into(),
209 appearance,
210 styles: ThemeStyles {
211 system: SystemColors::default(),
212 window_background_appearance,
213 accents: refined_accent_colors,
214 colors: refined_theme_colors,
215 status: refined_status_colors,
216 player: refined_player_colors,
217 syntax: syntax_theme,
218 },
219 }
220 }
221}
222
223/// Refines a [ThemeFamilyContent] and it's [ThemeContent]s into a [ThemeFamily].
224pub fn refine_theme_family(theme_family_content: ThemeFamilyContent) -> ThemeFamily {
225 let id = Uuid::new_v4().to_string();
226 let name = theme_family_content.name.clone();
227 let author = theme_family_content.author.clone();
228
229 let mut theme_family = ThemeFamily {
230 id: id.clone(),
231 name: name.clone().into(),
232 author: author.clone().into(),
233 themes: vec![],
234 scales: default_color_scales(),
235 };
236
237 let refined_themes = theme_family_content
238 .themes
239 .iter()
240 .map(|theme_content| theme_family.refine_theme(theme_content))
241 .collect();
242
243 theme_family.themes = refined_themes;
244
245 theme_family
246}
247
248/// A theme is the primary mechanism for defining the appearance of the UI.
249#[derive(Clone, PartialEq)]
250pub struct Theme {
251 /// The unique identifier for the theme.
252 pub id: String,
253 /// The name of the theme.
254 pub name: SharedString,
255 /// The appearance of the theme (light or dark).
256 pub appearance: Appearance,
257 /// The colors and other styles for the theme.
258 pub styles: ThemeStyles,
259}
260
261impl Theme {
262 /// Returns the [`SystemColors`] for the theme.
263 #[inline(always)]
264 pub fn system(&self) -> &SystemColors {
265 &self.styles.system
266 }
267
268 /// Returns the [`AccentColors`] for the theme.
269 #[inline(always)]
270 pub fn accents(&self) -> &AccentColors {
271 &self.styles.accents
272 }
273
274 /// Returns the [`PlayerColors`] for the theme.
275 #[inline(always)]
276 pub fn players(&self) -> &PlayerColors {
277 &self.styles.player
278 }
279
280 /// Returns the [`ThemeColors`] for the theme.
281 #[inline(always)]
282 pub fn colors(&self) -> &ThemeColors {
283 &self.styles.colors
284 }
285
286 /// Returns the [`SyntaxTheme`] for the theme.
287 #[inline(always)]
288 pub fn syntax(&self) -> &Arc<SyntaxTheme> {
289 &self.styles.syntax
290 }
291
292 /// Returns the [`StatusColors`] for the theme.
293 #[inline(always)]
294 pub fn status(&self) -> &StatusColors {
295 &self.styles.status
296 }
297
298 /// Returns the color for the syntax node with the given name.
299 #[inline(always)]
300 pub fn syntax_color(&self, name: &str) -> Hsla {
301 self.syntax().color(name)
302 }
303
304 /// Returns the [`Appearance`] for the theme.
305 #[inline(always)]
306 pub fn appearance(&self) -> Appearance {
307 self.appearance
308 }
309
310 /// Returns the [`WindowBackgroundAppearance`] for the theme.
311 #[inline(always)]
312 pub fn window_background_appearance(&self) -> WindowBackgroundAppearance {
313 self.styles.window_background_appearance
314 }
315}
316
317/// Compounds a color with an alpha value.
318/// TODO: Replace this with a method on Hsla.
319pub fn color_alpha(color: Hsla, alpha: f32) -> Hsla {
320 let mut color = color;
321 color.a = alpha;
322 color
323}