BlueGit

ict rusty-teams

Files

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

theme.rs

23a3899b History
theme.rs 11.1 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
7const FONT_FAMILY: &str = "Google Sans Flex";
8const MONO_FAMILIES: [&str; 2] = ["Cascadia Code", "Consolas"];
9const BASE_FONT_SIZE: f32 = 14.;
10const FONT_FILES: [&[u8]; 4] = [
11 include_bytes!("../../app/assets/fonts/GoogleSansFlex-Regular.ttf"),
12 include_bytes!("../../app/assets/fonts/GoogleSansFlex-SemiBold.ttf"),
13 include_bytes!("../../app/assets/fonts/GoogleSansFlex-Bold.ttf"),
14 include_bytes!("../../app/assets/fonts/GoogleSansFlex-Italic.ttf"),
15];
16
17pub const BACKGROUND: u32 = 0x0f0f10;
18const SURFACE: u32 = 0x161617;
19const SURFACE_RAISED: u32 = 0x232325;
20const ROW_HOVER: u32 = 0x1c1c1e;
21const POPUP: u32 = 0x2c2c2f;
22const POPUP_HOVER: u32 = 0x38383c;
23const POPUP_NESTED: u32 = 0x3a3a3e;
24const POPUP_NESTED_HOVER: u32 = 0x47474c;
25const REPLY_SURFACE: u32 = 0x19191b;
26const BORDER: u32 = 0x262628;
27const BORDER_STRONG: u32 = 0x3f3f42;
28const FOREGROUND: u32 = 0xf4f4f5;
29const TEXT_STRONG: u32 = 0xe4e4e7;
30const TEXT_SOFT: u32 = 0xd4d4d8;
31const TEXT_MUTED: u32 = 0xa1a1a6;
32const TEXT_FAINT: u32 = 0x8e8e93;
33const ACCENT: u32 = 0xce6a3b;
34const ACCENT_TEXT: u32 = 0xe08a5c;
35const ACCENT_SOFT: u32 = 0xf5c4a5;
36const ACCENT_STRONG: u32 = 0xa84f27;
37const BUBBLE_OWN: u32 = 0x4f2a1b;
38const BUBBLE_OTHER: u32 = 0x232325;
39const REACTION_ON_OWN: u32 = 0x6b3a26;
40const WHITE: u32 = 0xffffff;
41const MENTION_BACKGROUND: u32 = 0x3a2a23;
42const MENTION_BACKGROUND_OWN: u32 = 0x3b1d11;
43const OWN_META: u32 = 0xc9a28c;
44const OWN_READ: u32 = 0xf5c4a5;
45const DROP_BACKGROUND: u32 = 0xce6a3b14;
46const SELECTION: u32 = 0xce6a3b66;
47const CODE_SURFACE: u32 = 0x0000003d;
48const CODE_HEADER_BORDER: u32 = 0xffffff0f;
49const INLINE_CODE_FILL: u32 = 0xffffff14;
50const INLINE_CODE_FILL_OWN: u32 = 0x0000003d;
51const QUOTE_FILL: u32 = 0x00000052;
52const INLINE_CODE_BORDER: u32 = 0xffffff1a;
53const TABLE_HEADER: u32 = 0xffffff0d;
54const RULE_OWN: u32 = 0x6a3a25;
55const BADGE_MUTED: u32 = 0x3f3f42;
56const TOAST_MENTION: u32 = 0x2e1d15;
57const GREEN: u32 = 0x22c55e;
58const RED: u32 = 0xef4444;
59const CLOSE_BUTTON_HOVER: u32 = 0xc42b1c;
60const CLOSE_BUTTON_PRESSED: u32 = 0xb5271a;
61const CLOSE_BUTTON_ICON: u32 = 0xffffff;
62const RED_SOFT: u32 = 0xf87171;
63const RED_TINT: u32 = 0xfca5a5;
64const AMBER: u32 = 0xfbbf24;
65const OUT_OF_OFFICE: u32 = 0xc239b3;
66const MEETING_AVATAR: u32 = 0x4f52b2;
67const BOT_AVATAR: u32 = 0x3a96dd;
68const AVATAR_PALETTE: [u32; 6] = [0x9a4a22, 0x44606e, 0x8a3434, 0x4f6a3c, 0x6a4f7a, 0x2f6b63];
69
70pub fn color(hex: u32) -> Hsla {
71 rgb(hex).into()
72}
73
74fn color_with_alpha(hex: u32) -> Hsla {
75 rgba(hex).into()
76}
77
78pub fn load_fonts(cx: &mut App) {
79 let fonts = FONT_FILES
80 .iter()
81 .map(|bytes| Cow::Borrowed(*bytes))
82 .collect();
83 let _ = cx.text_system().add_fonts(fonts);
84}
85
86pub fn apply(cx: &mut App) {
87 let installed = cx.text_system().all_font_names();
88 let mono = MONO_FAMILIES
89 .iter()
90 .find(|family| installed.iter().any(|name| name == *family))
91 .map(|family| SharedString::from(*family));
92 Theme::change(ThemeMode::Dark, None, cx);
93 Theme::update(cx, |theme| {
94 theme.font_family = FONT_FAMILY.into();
95 theme.font_size = px(BASE_FONT_SIZE);
96 if let Some(mono) = mono {
97 theme.mono_font_family = mono;
98 }
99 theme.radius_lg = px(10.);
100 let colors = &mut theme.colors;
101 colors.background = color(BACKGROUND);
102 colors.foreground = color(FOREGROUND);
103 colors.border = color(BORDER);
104 colors.muted = color(BORDER);
105 colors.muted_foreground = color(TEXT_MUTED);
106 colors.sidebar = color(SURFACE);
107 colors.sidebar_foreground = color(FOREGROUND);
108 colors.sidebar_border = color(BORDER);
109 colors.sidebar_accent = color(POPUP_HOVER);
110 colors.sidebar_accent_foreground = color(FOREGROUND);
111 colors.sidebar_primary = color(ACCENT);
112 colors.popover = color(POPUP);
113 colors.popover_foreground = color(FOREGROUND);
114 colors.accent = color(SURFACE_RAISED);
115 colors.accent_foreground = color(FOREGROUND);
116 colors.secondary = color(SURFACE_RAISED);
117 colors.secondary_hover = color(BORDER_STRONG);
118 colors.secondary_active = color(BORDER_STRONG);
119 colors.secondary_foreground = color(FOREGROUND);
120 colors.primary = color(ACCENT);
121 colors.primary_hover = color(ACCENT_TEXT);
122 colors.primary_active = color(ACCENT_STRONG);
123 colors.primary_foreground = color(BACKGROUND);
124 colors.link = color(ACCENT_TEXT);
125 colors.link_hover = color(ACCENT_SOFT);
126 colors.link_active = color(ACCENT_STRONG);
127 colors.ring = color(ACCENT);
128 colors.caret = color(ACCENT);
129 colors.input = color(BORDER_STRONG);
130 colors.selection = color_with_alpha(SELECTION);
131 colors.title_bar = color(BACKGROUND);
132 colors.title_bar_border = color(BORDER);
133 colors.status_bar = color(BACKGROUND);
134 colors.status_bar_border = color(BORDER);
135 colors.success = color(GREEN);
136 colors.warning = color(AMBER);
137 colors.danger = color(CLOSE_BUTTON_HOVER);
138 colors.danger_active = color(CLOSE_BUTTON_PRESSED);
139 colors.danger_foreground = color(CLOSE_BUTTON_ICON);
140 colors.drop_target = color_with_alpha(DROP_BACKGROUND);
141 colors.window_border = color(BORDER);
142 colors.scrollbar_thumb = color(BORDER_STRONG);
143 colors.scrollbar_thumb_hover = color(TEXT_FAINT);
144 colors.list = color(BACKGROUND);
145 colors.list_hover = color(ROW_HOVER);
146 colors.list_active = color(SURFACE_RAISED);
147 });
148 cx.set_global(elevation_tokens());
149}
150
151fn elevation_tokens() -> ElevationTokens {
152 let flyout = Elevation {
153 fill: color(POPUP),
154 hover: color(POPUP_HOVER),
155 shadow: elevation::shadow16(),
156 };
157 let nested_flyout = Elevation {
158 fill: color(POPUP_NESTED),
159 hover: color(POPUP_NESTED_HOVER),
160 shadow: elevation::shadow16(),
161 };
162 ElevationTokens {
163 flyouts: vec![flyout.clone(), nested_flyout],
164 dialog: Elevation {
165 shadow: elevation::shadow64(),
166 ..flyout.clone()
167 },
168 toast: Elevation {
169 shadow: elevation::shadow8(),
170 ..flyout
171 },
172 }
173}
174
175pub fn flyout_shadow() -> Vec<BoxShadow> {
176 elevation::shadow16()
177}
178
179pub fn dialog_shadow() -> Vec<BoxShadow> {
180 elevation::shadow64()
181}
182
183pub fn toast_shadow() -> Vec<BoxShadow> {
184 elevation::shadow8()
185}
186
187pub fn popup_nested() -> Hsla {
188 color(POPUP_NESTED)
189}
190
191pub trait ElevatedSurface: Styled + Sized {
192 fn flyout_surface(self) -> Self {
193 self.bg(popup()).shadow(flyout_shadow())
194 }
195
196 fn nested_flyout_surface(self) -> Self {
197 self.bg(popup_nested()).shadow(flyout_shadow())
198 }
199
200 fn dialog_surface(self) -> Self {
201 self.bg(popup()).shadow(dialog_shadow())
202 }
203
204 fn toast_surface(self) -> Self {
205 self.bg(popup()).shadow(toast_shadow())
206 }
207}
208
209impl<T: Styled + Sized> ElevatedSurface for T {}
210
211pub fn font_family() -> SharedString {
212 FONT_FAMILY.into()
213}
214
215pub fn background() -> Hsla {
216 color(BACKGROUND)
217}
218
219pub fn toast_mention_fill() -> Hsla {
220 color(TOAST_MENTION)
221}
222
223pub fn surface() -> Hsla {
224 color(SURFACE)
225}
226
227pub fn surface_raised() -> Hsla {
228 color(SURFACE_RAISED)
229}
230
231pub fn row_hover() -> Hsla {
232 color(ROW_HOVER)
233}
234
235pub fn popup() -> Hsla {
236 color(POPUP)
237}
238
239pub fn popup_hover() -> Hsla {
240 color(POPUP_HOVER)
241}
242
243pub fn reply_surface() -> Hsla {
244 color(REPLY_SURFACE)
245}
246
247pub fn border() -> Hsla {
248 color(BORDER)
249}
250
251pub fn border_strong() -> Hsla {
252 color(BORDER_STRONG)
253}
254
255pub fn text() -> Hsla {
256 color(FOREGROUND)
257}
258
259pub fn text_strong() -> Hsla {
260 color(TEXT_STRONG)
261}
262
263pub fn text_soft() -> Hsla {
264 color(TEXT_SOFT)
265}
266
267pub fn text_muted() -> Hsla {
268 color(TEXT_MUTED)
269}
270
271pub fn text_faint() -> Hsla {
272 color(TEXT_FAINT)
273}
274
275pub fn accent() -> Hsla {
276 color(ACCENT)
277}
278
279pub fn accent_text() -> Hsla {
280 color(ACCENT_TEXT)
281}
282
283pub fn accent_soft() -> Hsla {
284 color(ACCENT_SOFT)
285}
286
287pub fn on_accent() -> Hsla {
288 color(BACKGROUND)
289}
290
291pub fn bubble_own() -> Hsla {
292 color(BUBBLE_OWN)
293}
294
295pub fn bubble_other() -> Hsla {
296 color(BUBBLE_OTHER)
297}
298
299pub fn reaction_on_own() -> Hsla {
300 color(REACTION_ON_OWN)
301}
302
303pub fn white() -> Hsla {
304 color(WHITE)
305}
306
307pub fn mention_background(own: bool) -> Hsla {
308 color(if own {
309 MENTION_BACKGROUND_OWN
310 } else {
311 MENTION_BACKGROUND
312 })
313}
314
315pub fn own_meta() -> Hsla {
316 color(OWN_META)
317}
318
319pub fn own_read() -> Hsla {
320 color(OWN_READ)
321}
322
323pub fn mention_text() -> Hsla {
324 color(ACCENT_SOFT)
325}
326
327pub fn drop_background() -> Hsla {
328 color_with_alpha(DROP_BACKGROUND)
329}
330
331pub fn code_surface() -> Hsla {
332 color_with_alpha(CODE_SURFACE)
333}
334
335pub fn code_header_border() -> Hsla {
336 color_with_alpha(CODE_HEADER_BORDER)
337}
338
339pub fn inline_code_fill(own: bool) -> Hsla {
340 color_with_alpha(if own {
341 INLINE_CODE_FILL_OWN
342 } else {
343 INLINE_CODE_FILL
344 })
345}
346
347pub fn quote_fill() -> Hsla {
348 color_with_alpha(QUOTE_FILL)
349}
350
351pub fn inline_code_border() -> Hsla {
352 color_with_alpha(INLINE_CODE_BORDER)
353}
354
355pub fn table_header() -> Hsla {
356 color_with_alpha(TABLE_HEADER)
357}
358
359pub fn rule(own: bool) -> Hsla {
360 if own {
361 color(RULE_OWN)
362 } else {
363 border_strong()
364 }
365}
366
367pub fn badge_muted() -> Hsla {
368 color(BADGE_MUTED)
369}
370
371pub fn green() -> Hsla {
372 color(GREEN)
373}
374
375pub fn red() -> Hsla {
376 color(RED)
377}
378
379pub fn red_soft() -> Hsla {
380 color(RED_SOFT)
381}
382
383pub fn red_tint() -> Hsla {
384 color(RED_TINT)
385}
386
387pub fn amber() -> Hsla {
388 color(AMBER)
389}
390
391pub fn out_of_office() -> Hsla {
392 color(OUT_OF_OFFICE)
393}
394
395pub fn meeting_avatar() -> Hsla {
396 color(MEETING_AVATAR)
397}
398
399pub fn bot_avatar() -> Hsla {
400 color(BOT_AVATAR)
401}
402
403pub fn avatar_color(index: usize) -> Hsla {
404 color(AVATAR_PALETTE[index % AVATAR_PALETTE.len()])
405}
406
407pub fn avatar_palette_len() -> usize {
408 AVATAR_PALETTE.len()
409}
410
411#[cfg(test)]
412mod tests {
413 use super::{POPUP, POPUP_HOVER, POPUP_NESTED, avatar_color, avatar_palette_len, color, elevation_tokens};
414
415 #[test]
416 fn avatar_colors_wrap_around_the_palette() {
417 assert_eq!(avatar_color(0), avatar_color(avatar_palette_len()));
418 assert_ne!(avatar_color(0), avatar_color(1));
419 }
420
421 #[test]
422 fn flyout_over_a_dialog_takes_a_lighter_fill_than_the_dialog() {
423 let tokens = elevation_tokens();
424 assert_eq!(tokens.dialog.fill, color(POPUP));
425 assert_eq!(tokens.flyout_at(0).fill, color(POPUP));
426 let nested = tokens.flyout_at(1);
427 assert_eq!(nested.fill, color(POPUP_NESTED));
428 assert!(nested.fill.l > tokens.dialog.fill.l);
429 assert!(nested.hover.l > nested.fill.l);
430 assert_ne!(nested.fill, color(POPUP_HOVER));
431 }
432
433 #[test]
434 fn fluent_shadow_blur_is_halved_for_gpui() {
435 let shadow = super::flyout_shadow();
436 assert_eq!(shadow[0].blur_radius, gpui_kit::px(1.));
437 assert_eq!(shadow[1].offset.y, gpui_kit::px(8.));
438 assert_eq!(shadow[1].blur_radius, gpui_kit::px(8.));
439 assert_eq!(super::dialog_shadow()[1].blur_radius, gpui_kit::px(32.));
440 }
441}