BlueGit

ict rusty-teams

Files

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

theme.rs

theme.rs 7.9 KiB Download Raw
1use std::borrow::Cow;
2
3use gpui_kit::component::{Theme, ThemeMode};
4use gpui_kit::*;
5
6const FONT_FAMILY: &str = "Google Sans Flex";
7const MONO_FAMILIES: [&str; 2] = ["Cascadia Code", "Consolas"];
8const BASE_FONT_SIZE: f32 = 14.;
9const FONT_FILES: [&[u8]; 4] = [
10 include_bytes!("../assets/fonts/GoogleSansFlex-Regular.ttf"),
11 include_bytes!("../assets/fonts/GoogleSansFlex-SemiBold.ttf"),
12 include_bytes!("../assets/fonts/GoogleSansFlex-Bold.ttf"),
13 include_bytes!("../assets/fonts/GoogleSansFlex-Italic.ttf"),
14];
15
16const BACKGROUND: u32 = 0x0f0f10;
17const SURFACE: u32 = 0x161617;
18const SURFACE_RAISED: u32 = 0x232325;
19const ROW_HOVER: u32 = 0x1c1c1e;
20const BORDER: u32 = 0x262628;
21const BORDER_STRONG: u32 = 0x3f3f42;
22const FOREGROUND: u32 = 0xf4f4f5;
23const TEXT_STRONG: u32 = 0xe4e4e7;
24const TEXT_SOFT: u32 = 0xd4d4d8;
25const TEXT_MUTED: u32 = 0xa1a1a6;
26const TEXT_FAINT: u32 = 0x8e8e93;
27const ACCENT: u32 = 0xce6a3b;
28const ACCENT_TEXT: u32 = 0xe08a5c;
29const ACCENT_SOFT: u32 = 0xf5c4a5;
30const ACCENT_STRONG: u32 = 0xa84f27;
31const BUBBLE_OWN: u32 = 0x4f2a1b;
32const BUBBLE_OTHER: u32 = 0x232325;
33const REACTION_ON_OWN: u32 = 0x6b3a26;
34const WHITE: u32 = 0xffffff;
35const MENTION_BACKGROUND: u32 = 0x3a2a23;
36const MENTION_BACKGROUND_OWN: u32 = 0x3b1d11;
37const OWN_META: u32 = 0xc9a28c;
38const OWN_READ: u32 = 0xf5c4a5;
39const DROP_BACKGROUND: u32 = 0xce6a3b14;
40const SELECTION: u32 = 0xce6a3b66;
41const CODE_SURFACE: u32 = 0x0000003d;
42const CODE_HEADER_BORDER: u32 = 0xffffff0f;
43const INLINE_CODE_FILL: u32 = 0xffffff14;
44const INLINE_CODE_FILL_OWN: u32 = 0x0000003d;
45const INLINE_CODE_BORDER: u32 = 0xffffff1a;
46const TABLE_HEADER: u32 = 0xffffff0d;
47const RULE_OWN: u32 = 0x6a3a25;
48const BADGE_MUTED: u32 = 0x3f3f42;
49const TOAST_MENTION: u32 = 0x2e1d15;
50const GREEN: u32 = 0x22c55e;
51const RED: u32 = 0xef4444;
52const CLOSE_BUTTON_HOVER: u32 = 0xc42b1c;
53const CLOSE_BUTTON_PRESSED: u32 = 0xb5271a;
54const CLOSE_BUTTON_ICON: u32 = 0xffffff;
55const RED_SOFT: u32 = 0xf87171;
56const RED_TINT: u32 = 0xfca5a5;
57const AMBER: u32 = 0xfbbf24;
58const AVATAR_PALETTE: [u32; 6] = [0x9a4a22, 0x44606e, 0x8a3434, 0x4f6a3c, 0x6a4f7a, 0x2f6b63];
59
60pub fn color(hex: u32) -> Hsla {
61 rgb(hex).into()
62}
63
64fn color_with_alpha(hex: u32) -> Hsla {
65 rgba(hex).into()
66}
67
68pub fn load_fonts(cx: &mut App) {
69 let fonts = FONT_FILES
70 .iter()
71 .map(|bytes| Cow::Borrowed(*bytes))
72 .collect();
73 let _ = cx.text_system().add_fonts(fonts);
74}
75
76pub fn apply(cx: &mut App) {
77 let installed = cx.text_system().all_font_names();
78 let mono = MONO_FAMILIES
79 .iter()
80 .find(|family| installed.iter().any(|name| name == *family))
81 .map(|family| SharedString::from(*family));
82 Theme::change(ThemeMode::Dark, None, cx);
83 Theme::update(cx, |theme| {
84 theme.font_family = FONT_FAMILY.into();
85 theme.font_size = px(BASE_FONT_SIZE);
86 if let Some(mono) = mono {
87 theme.mono_font_family = mono;
88 }
89 let colors = &mut theme.colors;
90 colors.background = color(BACKGROUND);
91 colors.foreground = color(FOREGROUND);
92 colors.border = color(BORDER);
93 colors.muted = color(BORDER);
94 colors.muted_foreground = color(TEXT_MUTED);
95 colors.sidebar = color(SURFACE);
96 colors.sidebar_foreground = color(FOREGROUND);
97 colors.sidebar_border = color(BORDER);
98 colors.sidebar_accent = color(SURFACE_RAISED);
99 colors.sidebar_accent_foreground = color(FOREGROUND);
100 colors.sidebar_primary = color(ACCENT);
101 colors.popover = color(SURFACE);
102 colors.popover_foreground = color(FOREGROUND);
103 colors.accent = color(SURFACE_RAISED);
104 colors.accent_foreground = color(FOREGROUND);
105 colors.secondary = color(SURFACE_RAISED);
106 colors.secondary_hover = color(BORDER_STRONG);
107 colors.secondary_active = color(BORDER_STRONG);
108 colors.secondary_foreground = color(FOREGROUND);
109 colors.primary = color(ACCENT);
110 colors.primary_hover = color(ACCENT_TEXT);
111 colors.primary_active = color(ACCENT_STRONG);
112 colors.primary_foreground = color(BACKGROUND);
113 colors.link = color(ACCENT_TEXT);
114 colors.link_hover = color(ACCENT_SOFT);
115 colors.link_active = color(ACCENT_STRONG);
116 colors.ring = color(ACCENT);
117 colors.caret = color(ACCENT);
118 colors.input = color(BORDER_STRONG);
119 colors.selection = color_with_alpha(SELECTION);
120 colors.title_bar = color(BACKGROUND);
121 colors.title_bar_border = color(BORDER);
122 colors.status_bar = color(BACKGROUND);
123 colors.status_bar_border = color(BORDER);
124 colors.success = color(GREEN);
125 colors.warning = color(AMBER);
126 colors.danger = color(CLOSE_BUTTON_HOVER);
127 colors.danger_active = color(CLOSE_BUTTON_PRESSED);
128 colors.danger_foreground = color(CLOSE_BUTTON_ICON);
129 colors.drop_target = color_with_alpha(DROP_BACKGROUND);
130 colors.window_border = color(BORDER);
131 colors.scrollbar_thumb = color(BORDER_STRONG);
132 colors.scrollbar_thumb_hover = color(TEXT_FAINT);
133 colors.list = color(BACKGROUND);
134 colors.list_hover = color(ROW_HOVER);
135 colors.list_active = color(SURFACE_RAISED);
136 });
137}
138
139pub fn font_family() -> SharedString {
140 FONT_FAMILY.into()
141}
142
143pub fn background() -> Hsla {
144 color(BACKGROUND)
145}
146
147pub fn toast_mention_fill() -> Hsla {
148 color(TOAST_MENTION)
149}
150
151pub fn surface() -> Hsla {
152 color(SURFACE)
153}
154
155pub fn surface_raised() -> Hsla {
156 color(SURFACE_RAISED)
157}
158
159pub fn row_hover() -> Hsla {
160 color(ROW_HOVER)
161}
162
163pub fn border() -> Hsla {
164 color(BORDER)
165}
166
167pub fn border_strong() -> Hsla {
168 color(BORDER_STRONG)
169}
170
171pub fn text() -> Hsla {
172 color(FOREGROUND)
173}
174
175pub fn text_strong() -> Hsla {
176 color(TEXT_STRONG)
177}
178
179pub fn text_soft() -> Hsla {
180 color(TEXT_SOFT)
181}
182
183pub fn text_muted() -> Hsla {
184 color(TEXT_MUTED)
185}
186
187pub fn text_faint() -> Hsla {
188 color(TEXT_FAINT)
189}
190
191pub fn accent() -> Hsla {
192 color(ACCENT)
193}
194
195pub fn accent_text() -> Hsla {
196 color(ACCENT_TEXT)
197}
198
199pub fn accent_soft() -> Hsla {
200 color(ACCENT_SOFT)
201}
202
203pub fn on_accent() -> Hsla {
204 color(BACKGROUND)
205}
206
207pub fn bubble_own() -> Hsla {
208 color(BUBBLE_OWN)
209}
210
211pub fn bubble_other() -> Hsla {
212 color(BUBBLE_OTHER)
213}
214
215pub fn reaction_on_own() -> Hsla {
216 color(REACTION_ON_OWN)
217}
218
219pub fn white() -> Hsla {
220 color(WHITE)
221}
222
223pub fn mention_background(own: bool) -> Hsla {
224 color(if own {
225 MENTION_BACKGROUND_OWN
226 } else {
227 MENTION_BACKGROUND
228 })
229}
230
231pub fn own_meta() -> Hsla {
232 color(OWN_META)
233}
234
235pub fn own_read() -> Hsla {
236 color(OWN_READ)
237}
238
239pub fn mention_text() -> Hsla {
240 color(ACCENT_SOFT)
241}
242
243pub fn drop_background() -> Hsla {
244 color_with_alpha(DROP_BACKGROUND)
245}
246
247pub fn code_surface() -> Hsla {
248 color_with_alpha(CODE_SURFACE)
249}
250
251pub fn code_header_border() -> Hsla {
252 color_with_alpha(CODE_HEADER_BORDER)
253}
254
255pub fn inline_code_fill(own: bool) -> Hsla {
256 color_with_alpha(if own {
257 INLINE_CODE_FILL_OWN
258 } else {
259 INLINE_CODE_FILL
260 })
261}
262
263pub fn inline_code_border() -> Hsla {
264 color_with_alpha(INLINE_CODE_BORDER)
265}
266
267pub fn table_header() -> Hsla {
268 color_with_alpha(TABLE_HEADER)
269}
270
271pub fn rule(own: bool) -> Hsla {
272 if own {
273 color(RULE_OWN)
274 } else {
275 border_strong()
276 }
277}
278
279pub fn badge_muted() -> Hsla {
280 color(BADGE_MUTED)
281}
282
283pub fn green() -> Hsla {
284 color(GREEN)
285}
286
287pub fn red() -> Hsla {
288 color(RED)
289}
290
291pub fn red_soft() -> Hsla {
292 color(RED_SOFT)
293}
294
295pub fn red_tint() -> Hsla {
296 color(RED_TINT)
297}
298
299pub fn amber() -> Hsla {
300 color(AMBER)
301}
302
303pub fn avatar_color(index: usize) -> Hsla {
304 color(AVATAR_PALETTE[index % AVATAR_PALETTE.len()])
305}
306
307pub fn avatar_palette_len() -> usize {
308 AVATAR_PALETTE.len()
309}
310
311#[cfg(test)]
312mod tests {
313 use super::{avatar_color, avatar_palette_len};
314
315 #[test]
316 fn avatar_colors_wrap_around_the_palette() {
317 assert_eq!(avatar_color(0), avatar_color(avatar_palette_len()));
318 assert_ne!(avatar_color(0), avatar_color(1));
319 }
320}