BlueGit

ict rusty-teams

Files

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

theme.rs

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