BlueGit

ict rusty-teams

Files

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

theme.rs

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