BlueGit

ict rusty-teams

Files

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

theme.rs

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