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