BlueGit

ict rusty-teams

Files

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

theme.rs

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