gpui_component/theme/
mod.rs1use crate::{highlighter::HighlightTheme, scroll::ScrollbarShow};
2use gpui::{px, App, Global, Hsla, Pixels, SharedString, Window, WindowAppearance};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use std::{
6 ops::{Deref, DerefMut},
7 rc::Rc,
8 sync::Arc,
9};
10
11mod color;
12mod registry;
13mod schema;
14mod theme_color;
15
16pub use color::*;
17pub use registry::*;
18pub use schema::*;
19pub use theme_color::*;
20
21pub fn init(cx: &mut App) {
22 registry::init(cx);
23
24 Theme::sync_system_appearance(None, cx);
25 Theme::sync_scrollbar_appearance(cx);
26}
27
28pub trait ActiveTheme {
29 fn theme(&self) -> &Theme;
30}
31
32impl ActiveTheme for App {
33 #[inline(always)]
34 fn theme(&self) -> &Theme {
35 Theme::global(self)
36 }
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
40pub struct Theme {
41 pub colors: ThemeColor,
42 pub highlight_theme: Arc<HighlightTheme>,
43 pub light_theme: Rc<ThemeConfig>,
44 pub dark_theme: Rc<ThemeConfig>,
45
46 pub mode: ThemeMode,
47 pub font_family: SharedString,
48 pub font_size: Pixels,
49 pub radius: Pixels,
51 pub radius_lg: Pixels,
53 pub shadow: bool,
54 pub transparent: Hsla,
55 pub scrollbar_show: ScrollbarShow,
57 pub tile_grid_size: Pixels,
59 pub tile_shadow: bool,
61}
62
63impl Default for Theme {
64 fn default() -> Self {
65 Self::from(ThemeColor::default())
66 }
67}
68
69impl Deref for Theme {
70 type Target = ThemeColor;
71
72 fn deref(&self) -> &Self::Target {
73 &self.colors
74 }
75}
76
77impl DerefMut for Theme {
78 fn deref_mut(&mut self) -> &mut Self::Target {
79 &mut self.colors
80 }
81}
82
83impl Global for Theme {}
84
85impl Theme {
86 #[inline(always)]
88 pub fn global(cx: &App) -> &Theme {
89 cx.global::<Theme>()
90 }
91
92 #[inline(always)]
94 pub fn global_mut(cx: &mut App) -> &mut Theme {
95 cx.global_mut::<Theme>()
96 }
97
98 #[inline(always)]
100 pub fn is_dark(&self) -> bool {
101 self.mode.is_dark()
102 }
103
104 pub fn theme_name(&self) -> &SharedString {
106 if self.is_dark() {
107 &self.dark_theme.name
108 } else {
109 &self.light_theme.name
110 }
111 }
112
113 pub fn sync_system_appearance(window: Option<&mut Window>, cx: &mut App) {
131 let appearance = window
134 .as_ref()
135 .map(|window| window.appearance())
136 .unwrap_or_else(|| cx.window_appearance());
137
138 Self::change(appearance, window, cx);
139 }
140
141 pub fn sync_scrollbar_appearance(cx: &mut App) {
143 Theme::global_mut(cx).scrollbar_show = if cx.should_auto_hide_scrollbars() {
144 ScrollbarShow::Scrolling
145 } else {
146 ScrollbarShow::Hover
147 };
148 }
149
150 pub fn change(mode: impl Into<ThemeMode>, window: Option<&mut Window>, cx: &mut App) {
151 let mode = mode.into();
152 if !cx.has_global::<Theme>() {
153 let mut theme = Theme::default();
154 theme.light_theme = ThemeRegistry::global(cx).default_light_theme().clone();
155 theme.dark_theme = ThemeRegistry::global(cx).default_dark_theme().clone();
156 cx.set_global(theme);
157 }
158
159 let theme = cx.global_mut::<Theme>();
160 theme.mode = mode;
161 if mode.is_dark() {
162 theme.apply_config(&theme.dark_theme.clone());
163 } else {
164 theme.apply_config(&theme.light_theme.clone());
165 }
166
167 if let Some(window) = window {
168 window.refresh();
169 }
170 }
171
172 #[inline]
174 pub(crate) fn editor_background(&self) -> Hsla {
175 self.highlight_theme
176 .style
177 .editor_background
178 .unwrap_or(self.background)
179 }
180}
181
182impl From<ThemeColor> for Theme {
183 fn from(colors: ThemeColor) -> Self {
184 Theme {
185 mode: ThemeMode::default(),
186 transparent: Hsla::transparent_black(),
187 font_size: px(16.),
188 font_family: if cfg!(target_os = "macos") {
189 ".SystemUIFont".into()
190 } else if cfg!(target_os = "windows") {
191 "Segoe UI".into()
192 } else {
193 "FreeMono".into()
194 },
195 radius: px(6.),
196 radius_lg: px(8.),
197 shadow: true,
198 scrollbar_show: ScrollbarShow::default(),
199 tile_grid_size: px(8.),
200 tile_shadow: true,
201 colors,
202 light_theme: Rc::new(ThemeConfig::default()),
203 dark_theme: Rc::new(ThemeConfig::default()),
204 highlight_theme: HighlightTheme::default_light(),
205 }
206 }
207}
208
209#[derive(
210 Debug, Clone, Copy, Default, PartialEq, PartialOrd, Eq, Hash, Serialize, Deserialize, JsonSchema,
211)]
212#[serde(rename_all = "snake_case")]
213pub enum ThemeMode {
214 #[default]
215 Light,
216 Dark,
217}
218
219impl ThemeMode {
220 #[inline(always)]
221 pub fn is_dark(&self) -> bool {
222 matches!(self, Self::Dark)
223 }
224
225 pub fn name(&self) -> &'static str {
227 match self {
228 ThemeMode::Light => "light",
229 ThemeMode::Dark => "dark",
230 }
231 }
232}
233
234impl From<WindowAppearance> for ThemeMode {
235 fn from(appearance: WindowAppearance) -> Self {
236 match appearance {
237 WindowAppearance::Dark | WindowAppearance::VibrantDark => Self::Dark,
238 WindowAppearance::Light | WindowAppearance::VibrantLight => Self::Light,
239 }
240 }
241}