BlueGit

ict rusty-teams

Files

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

theme.rs

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