BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-base / src / theme.rs

theme.rs

theme.rs 11.3 KiB Download Raw
1use std::borrow::Cow;
2
3use gpui_kit::component::elevation::{self, Elevation, ElevationTokens};
4use gpui_kit::component::{Theme, ThemeMode};
5use gpui_kit::*;
6
7mod schemes;
8
9pub use schemes::{
10 ColorScheme, HIGH_CONTRAST, Palette, SCHEMES, Shade, active_palette as palette,
11 active_scheme as scheme, active_shade as shade, default_scheme, palettes, scheme_by_id,
12 set_active_scheme, set_active_shade, set_thread_scheme, set_thread_shade,
13};
14
15const FONT_FAMILY: &str = "Google Sans Flex";
16const MONO_FAMILIES: [&str; 2] = ["Cascadia Code", "Consolas"];
17const BASE_FONT_SIZE: f32 = 14.;
18const FONT_FILES: [&[u8]; 4] = [
19 include_bytes!("../../app/assets/fonts/GoogleSansFlex-Regular.ttf"),
20 include_bytes!("../../app/assets/fonts/GoogleSansFlex-SemiBold.ttf"),
21 include_bytes!("../../app/assets/fonts/GoogleSansFlex-Bold.ttf"),
22 include_bytes!("../../app/assets/fonts/GoogleSansFlex-Italic.ttf"),
23];
24
25const WHITE: u32 = 0xffffff;
26const CLOSE_BUTTON_ICON: u32 = 0xffffff;
27
28pub fn color(hex: u32) -> Hsla {
29 rgb(hex).into()
30}
31
32fn color_with_alpha(hex: u32) -> Hsla {
33 rgba(hex).into()
34}
35
36pub fn load_fonts(cx: &mut App) {
37 let fonts = FONT_FILES
38 .iter()
39 .map(|bytes| Cow::Borrowed(*bytes))
40 .collect();
41 let _ = cx.text_system().add_fonts(fonts);
42}
43
44pub fn apply(cx: &mut App) {
45 let installed = cx.text_system().all_font_names();
46 let mono = MONO_FAMILIES
47 .iter()
48 .find(|family| installed.iter().any(|name| name == *family))
49 .map(|family| SharedString::from(*family));
50 let mode = if palette().shade.is_dark() {
51 ThemeMode::Dark
52 } else {
53 ThemeMode::Light
54 };
55 Theme::change(mode, None, cx);
56 Theme::update(cx, |theme| {
57 theme.font_family = FONT_FAMILY.into();
58 theme.font_size = px(BASE_FONT_SIZE);
59 if let Some(mono) = mono {
60 theme.mono_font_family = mono;
61 }
62 theme.radius_lg = px(10.);
63 let colors = &mut theme.colors;
64 colors.background = color(palette().background);
65 colors.foreground = color(palette().foreground);
66 colors.border = color(palette().border);
67 colors.muted = color(palette().border);
68 colors.muted_foreground = color(palette().text_muted);
69 colors.sidebar = color(palette().surface);
70 colors.sidebar_foreground = color(palette().foreground);
71 colors.sidebar_border = color(palette().border);
72 colors.sidebar_accent = color(palette().popup_hover);
73 colors.sidebar_accent_foreground = color(palette().foreground);
74 colors.sidebar_primary = color(palette().accent);
75 colors.popover = color(palette().popup);
76 colors.popover_foreground = color(palette().foreground);
77 colors.accent = color(palette().surface_raised);
78 colors.accent_foreground = color(palette().foreground);
79 colors.secondary = color(palette().surface_raised);
80 colors.secondary_hover = color(palette().border_strong);
81 colors.secondary_active = color(palette().border_strong);
82 colors.secondary_foreground = color(palette().foreground);
83 colors.primary = color(palette().accent);
84 colors.primary_hover = color(palette().accent_text);
85 colors.primary_active = color(palette().accent_strong);
86 colors.primary_foreground = color(palette().on_accent);
87 colors.link = color(palette().accent_text);
88 colors.link_hover = color(palette().accent_soft);
89 colors.link_active = color(palette().accent_strong);
90 colors.ring = color(palette().accent);
91 colors.caret = color(palette().accent);
92 colors.input = color(palette().border_strong);
93 colors.selection = color_with_alpha(palette().selection);
94 colors.title_bar = color(palette().background);
95 colors.title_bar_border = color(palette().border);
96 colors.status_bar = color(palette().background);
97 colors.status_bar_border = color(palette().border);
98 colors.success = color(palette().green);
99 colors.warning = color(palette().amber);
100 colors.danger = color(palette().close_button_hover);
101 colors.danger_active = color(palette().close_button_pressed);
102 colors.danger_foreground = color(CLOSE_BUTTON_ICON);
103 colors.drop_target = color_with_alpha(palette().drop_background);
104 colors.window_border = color(palette().border);
105 colors.scrollbar_thumb = color(palette().border_strong);
106 colors.scrollbar_thumb_hover = color(palette().text_faint);
107 colors.list = color(palette().background);
108 colors.list_hover = color(palette().row_hover);
109 colors.list_active = color(palette().surface_raised);
110 });
111 cx.set_global(elevation_tokens());
112}
113
114fn elevation_tokens() -> ElevationTokens {
115 let flyout = Elevation {
116 fill: color(palette().popup),
117 hover: color(palette().popup_hover),
118 shadow: elevation::shadow16(),
119 };
120 let nested_flyout = Elevation {
121 fill: color(palette().popup_nested),
122 hover: color(palette().popup_nested_hover),
123 shadow: elevation::shadow16(),
124 };
125 ElevationTokens {
126 flyouts: vec![flyout.clone(), nested_flyout],
127 dialog: Elevation {
128 shadow: elevation::shadow64(),
129 ..flyout.clone()
130 },
131 toast: Elevation {
132 shadow: elevation::shadow8(),
133 ..flyout
134 },
135 }
136}
137
138pub fn flyout_shadow() -> Vec<BoxShadow> {
139 elevation::shadow16()
140}
141
142pub fn dialog_shadow() -> Vec<BoxShadow> {
143 elevation::shadow64()
144}
145
146pub fn toast_shadow() -> Vec<BoxShadow> {
147 elevation::shadow8()
148}
149
150pub fn popup_nested() -> Hsla {
151 color(palette().popup_nested)
152}
153
154pub trait ElevatedSurface: Styled + Sized {
155 fn flyout_surface(self) -> Self {
156 self.bg(popup()).shadow(flyout_shadow())
157 }
158
159 fn nested_flyout_surface(self) -> Self {
160 self.bg(popup_nested()).shadow(flyout_shadow())
161 }
162
163 fn dialog_surface(self) -> Self {
164 self.bg(popup()).shadow(dialog_shadow())
165 }
166
167 fn toast_surface(self) -> Self {
168 self.bg(popup()).shadow(toast_shadow())
169 }
170}
171
172impl<T: Styled + Sized> ElevatedSurface for T {}
173
174pub fn font_family() -> SharedString {
175 FONT_FAMILY.into()
176}
177
178pub fn background() -> Hsla {
179 color(palette().background)
180}
181
182pub fn background_hex() -> u32 {
183 palette().background
184}
185
186pub fn toast_mention_fill() -> Hsla {
187 color(palette().toast_mention)
188}
189
190pub fn surface() -> Hsla {
191 color(palette().surface)
192}
193
194pub fn surface_raised() -> Hsla {
195 color(palette().surface_raised)
196}
197
198pub fn row_hover() -> Hsla {
199 color(palette().row_hover)
200}
201
202pub fn popup() -> Hsla {
203 color(palette().popup)
204}
205
206pub fn popup_hover() -> Hsla {
207 color(palette().popup_hover)
208}
209
210pub fn reply_surface() -> Hsla {
211 color(palette().reply_surface)
212}
213
214pub fn border() -> Hsla {
215 color(palette().border)
216}
217
218pub fn selection() -> Hsla {
219 color_with_alpha(palette().selection)
220}
221
222pub fn border_strong() -> Hsla {
223 color(palette().border_strong)
224}
225
226pub fn text() -> Hsla {
227 color(palette().foreground)
228}
229
230pub fn text_strong() -> Hsla {
231 color(palette().text_strong)
232}
233
234pub fn text_soft() -> Hsla {
235 color(palette().text_soft)
236}
237
238pub fn text_muted() -> Hsla {
239 color(palette().text_muted)
240}
241
242pub fn text_faint() -> Hsla {
243 color(palette().text_faint)
244}
245
246pub fn accent() -> Hsla {
247 color(palette().accent)
248}
249
250pub fn accent_text() -> Hsla {
251 color(palette().accent_text)
252}
253
254pub fn accent_soft() -> Hsla {
255 color(palette().accent_soft)
256}
257
258pub fn on_accent() -> Hsla {
259 color(palette().on_accent)
260}
261
262pub fn bubble_own() -> Hsla {
263 color(palette().bubble_own)
264}
265
266pub fn bubble_other() -> Hsla {
267 color(palette().bubble_other)
268}
269
270pub fn reaction_on_own() -> Hsla {
271 color(palette().reaction_on_own)
272}
273
274pub fn white() -> Hsla {
275 color(WHITE)
276}
277
278pub fn mention_background(own: bool) -> Hsla {
279 color(if own {
280 palette().mention_background_own
281 } else {
282 palette().mention_background
283 })
284}
285
286pub fn own_meta() -> Hsla {
287 color(palette().own_meta)
288}
289
290pub fn own_read() -> Hsla {
291 color(palette().own_read)
292}
293
294pub fn mention_text() -> Hsla {
295 color(palette().accent_soft)
296}
297
298pub fn drop_background() -> Hsla {
299 color_with_alpha(palette().drop_background)
300}
301
302pub fn code_surface() -> Hsla {
303 color_with_alpha(palette().code_surface)
304}
305
306pub fn code_header_border() -> Hsla {
307 color_with_alpha(palette().code_header_border)
308}
309
310pub fn inline_code_fill(own: bool) -> Hsla {
311 color_with_alpha(if own {
312 palette().inline_code_fill_own
313 } else {
314 palette().inline_code_fill
315 })
316}
317
318pub fn quote_fill() -> Hsla {
319 color_with_alpha(palette().quote_fill)
320}
321
322pub fn inline_code_border() -> Hsla {
323 color_with_alpha(palette().inline_code_border)
324}
325
326pub fn table_header() -> Hsla {
327 color_with_alpha(palette().table_header)
328}
329
330pub fn rule(own: bool) -> Hsla {
331 if own {
332 color(palette().rule_own)
333 } else {
334 border_strong()
335 }
336}
337
338pub fn badge_muted() -> Hsla {
339 color(palette().badge_muted)
340}
341
342pub fn green() -> Hsla {
343 color(palette().green)
344}
345
346pub fn red() -> Hsla {
347 color(palette().red)
348}
349
350pub fn red_soft() -> Hsla {
351 color(palette().red_soft)
352}
353
354pub fn red_tint() -> Hsla {
355 color(palette().red_tint)
356}
357
358pub fn amber() -> Hsla {
359 color(palette().amber)
360}
361
362pub fn out_of_office() -> Hsla {
363 color(palette().out_of_office)
364}
365
366pub fn meeting_avatar() -> Hsla {
367 color(palette().meeting_avatar)
368}
369
370pub fn bot_avatar() -> Hsla {
371 color(palette().bot_avatar)
372}
373
374pub fn avatar_color(index: usize) -> Hsla {
375 let palette = palette().avatar_palette;
376 color(palette[index % palette.len()])
377}
378
379pub fn avatar_palette_len() -> usize {
380 palette().avatar_palette.len()
381}
382
383#[cfg(test)]
384mod tests {
385 use super::{
386 SCHEMES, Shade, avatar_color, avatar_palette_len, color, elevation_tokens,
387 set_thread_scheme, set_thread_shade,
388 };
389
390 #[test]
391 fn avatar_colors_wrap_around_the_palette() {
392 assert_eq!(avatar_color(0), avatar_color(avatar_palette_len()));
393 assert_ne!(avatar_color(0), avatar_color(1));
394 }
395
396 #[test]
397 fn flyout_over_a_dialog_takes_a_lighter_fill_than_the_dialog() {
398 for scheme in SCHEMES {
399 for shade in Shade::ALL {
400 set_thread_scheme(Some(scheme.id));
401 set_thread_shade(Some(shade));
402 let palette = scheme.palette(shade);
403 let tokens = elevation_tokens();
404 assert_eq!(tokens.dialog.fill, color(palette.popup));
405 assert_eq!(tokens.flyout_at(0).fill, color(palette.popup));
406 let nested = tokens.flyout_at(1);
407 assert_eq!(nested.fill, color(palette.popup_nested));
408 assert_ne!(nested.hover, nested.fill);
409 if shade == Shade::Dark {
410 assert!(nested.fill.l > tokens.dialog.fill.l);
411 assert!(nested.hover.l > nested.fill.l);
412 assert_ne!(nested.fill, color(palette.popup_hover));
413 }
414 }
415 }
416 set_thread_scheme(None);
417 set_thread_shade(None);
418 }
419
420 #[test]
421 fn fluent_shadow_blur_is_halved_for_gpui() {
422 let shadow = super::flyout_shadow();
423 assert_eq!(shadow[0].blur_radius, gpui_kit::px(1.));
424 assert_eq!(shadow[1].offset.y, gpui_kit::px(8.));
425 assert_eq!(shadow[1].blur_radius, gpui_kit::px(8.));
426 assert_eq!(super::dialog_shadow()[1].blur_radius, gpui_kit::px(32.));
427 }
428}