BlueGit

ict rusty-teams

Files

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

theme.rs

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