BlueGit

ict rusty-teams

Files

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

schemes.rs

schemes.rs 6.8 KiB Download Raw
1use std::cell::Cell;
2use std::sync::atomic::{AtomicUsize, Ordering};
3
4#[derive(Debug, PartialEq, Eq)]
5pub struct ColorScheme {
6 pub id: &'static str,
7 pub name: &'static str,
8 pub background: u32,
9 pub surface: u32,
10 pub surface_raised: u32,
11 pub row_hover: u32,
12 pub popup: u32,
13 pub popup_hover: u32,
14 pub popup_nested: u32,
15 pub popup_nested_hover: u32,
16 pub reply_surface: u32,
17 pub border: u32,
18 pub border_strong: u32,
19 pub foreground: u32,
20 pub text_strong: u32,
21 pub text_soft: u32,
22 pub text_muted: u32,
23 pub text_faint: u32,
24 pub accent: u32,
25 pub accent_text: u32,
26 pub accent_soft: u32,
27 pub accent_strong: u32,
28 pub on_accent: u32,
29 pub bubble_own: u32,
30 pub bubble_other: u32,
31 pub reaction_on_own: u32,
32 pub mention_background: u32,
33 pub mention_background_own: u32,
34 pub own_meta: u32,
35 pub own_read: u32,
36 pub drop_background: u32,
37 pub selection: u32,
38 pub rule_own: u32,
39 pub badge_muted: u32,
40 pub toast_mention: u32,
41 pub green: u32,
42 pub red: u32,
43 pub red_soft: u32,
44 pub red_tint: u32,
45 pub amber: u32,
46 pub out_of_office: u32,
47 pub close_button_hover: u32,
48 pub close_button_pressed: u32,
49 pub meeting_avatar: u32,
50 pub bot_avatar: u32,
51 pub avatar_palette: [u32; 6],
52}
53
54pub const RUSTY_TEAMS: ColorScheme = ColorScheme {
55 id: "rusty-teams",
56 name: "Rusty Teams",
57 background: 0x0f0f10,
58 surface: 0x161617,
59 surface_raised: 0x232325,
60 row_hover: 0x1c1c1e,
61 popup: 0x2c2c2f,
62 popup_hover: 0x38383c,
63 popup_nested: 0x3a3a3e,
64 popup_nested_hover: 0x47474c,
65 reply_surface: 0x19191b,
66 border: 0x262628,
67 border_strong: 0x3f3f42,
68 foreground: 0xf4f4f5,
69 text_strong: 0xe4e4e7,
70 text_soft: 0xd4d4d8,
71 text_muted: 0xa1a1a6,
72 text_faint: 0x8e8e93,
73 accent: 0xce6a3b,
74 accent_text: 0xe08a5c,
75 accent_soft: 0xf5c4a5,
76 accent_strong: 0xa84f27,
77 on_accent: 0x0f0f10,
78 bubble_own: 0x4f2a1b,
79 bubble_other: 0x232325,
80 reaction_on_own: 0x6b3a26,
81 mention_background: 0x3a2a23,
82 mention_background_own: 0x3b1d11,
83 own_meta: 0xc9a28c,
84 own_read: 0xf5c4a5,
85 drop_background: 0xce6a3b14,
86 selection: 0xce6a3b66,
87 rule_own: 0x6a3a25,
88 badge_muted: 0x3f3f42,
89 toast_mention: 0x2e1d15,
90 green: 0x22c55e,
91 red: 0xef4444,
92 red_soft: 0xf87171,
93 red_tint: 0xfca5a5,
94 amber: 0xfbbf24,
95 out_of_office: 0xc239b3,
96 close_button_hover: 0xc42b1c,
97 close_button_pressed: 0xb5271a,
98 meeting_avatar: 0x4f52b2,
99 bot_avatar: 0x3a96dd,
100 avatar_palette: [0x9a4a22, 0x44606e, 0x8a3434, 0x4f6a3c, 0x6a4f7a, 0x2f6b63],
101};
102
103pub const TEAMS: ColorScheme = ColorScheme {
104 id: "teams",
105 name: "Teams",
106 background: 0x0a0a0a,
107 surface: 0x1f1f1f,
108 surface_raised: 0x2e2e2e,
109 row_hover: 0x292929,
110 popup: 0x333333,
111 popup_hover: 0x3d3d3d,
112 popup_nested: 0x424242,
113 popup_nested_hover: 0x4d4d4d,
114 reply_surface: 0x242424,
115 border: 0x333333,
116 border_strong: 0x3d3d3d,
117 foreground: 0xffffff,
118 text_strong: 0xe0e0e0,
119 text_soft: 0xd6d6d6,
120 text_muted: 0xadadad,
121 text_faint: 0x999999,
122 accent: 0x5b5fc7,
123 accent_text: 0x7f85f5,
124 accent_soft: 0xaab1fa,
125 accent_strong: 0x444791,
126 on_accent: 0xffffff,
127 bubble_own: 0x2f2f4a,
128 bubble_other: 0x292929,
129 reaction_on_own: 0x383966,
130 mention_background: 0x2f2f4a,
131 mention_background_own: 0x3d3e78,
132 own_meta: 0xadadad,
133 own_read: 0xaab1fa,
134 drop_background: 0x5b5fc714,
135 selection: 0x5b5fc766,
136 rule_own: 0x444791,
137 badge_muted: 0x3d3d3d,
138 toast_mention: 0x2f2f4a,
139 green: 0x92c353,
140 red: 0xc4314b,
141 red_soft: 0xe37d80,
142 red_tint: 0xf1bbbc,
143 amber: 0xffaa44,
144 out_of_office: 0xc239b3,
145 close_button_hover: 0xc4314b,
146 close_button_pressed: 0xa72037,
147 meeting_avatar: 0x4f52b2,
148 bot_avatar: 0x3a96dd,
149 avatar_palette: [0x9a4a22, 0x44606e, 0x8a3434, 0x4f6a3c, 0x6a4f7a, 0x2f6b63],
150};
151
152pub const SCHEMES: [&ColorScheme; 2] = [&RUSTY_TEAMS, &TEAMS];
153
154static ACTIVE_SCHEME: AtomicUsize = AtomicUsize::new(0);
155
156thread_local! {
157 static THREAD_SCHEME: Cell<Option<usize>> = const { Cell::new(None) };
158}
159
160pub fn default_scheme() -> &'static ColorScheme {
161 SCHEMES[0]
162}
163
164pub fn scheme_by_id(id: &str) -> Option<&'static ColorScheme> {
165 SCHEMES.into_iter().find(|scheme| scheme.id == id)
166}
167
168fn position(id: &str) -> usize {
169 SCHEMES
170 .iter()
171 .position(|scheme| scheme.id == id)
172 .unwrap_or(0)
173}
174
175pub fn active_scheme() -> &'static ColorScheme {
176 let index = THREAD_SCHEME
177 .with(Cell::get)
178 .unwrap_or_else(|| ACTIVE_SCHEME.load(Ordering::Relaxed));
179 SCHEMES[index.min(SCHEMES.len() - 1)]
180}
181
182pub fn set_active_scheme(id: &str) {
183 ACTIVE_SCHEME.store(position(id), Ordering::Relaxed);
184}
185
186pub fn set_thread_scheme(id: Option<&str>) {
187 THREAD_SCHEME.with(|cell| cell.set(id.map(position)));
188}
189
190#[cfg(test)]
191mod tests {
192 use std::collections::HashSet;
193
194 use super::*;
195
196 #[test]
197 fn scheme_ids_and_names_are_unique() {
198 let ids: HashSet<_> = SCHEMES.iter().map(|scheme| scheme.id).collect();
199 let names: HashSet<_> = SCHEMES.iter().map(|scheme| scheme.name).collect();
200 assert_eq!(ids.len(), SCHEMES.len());
201 assert_eq!(names.len(), SCHEMES.len());
202 }
203
204 #[test]
205 fn rusty_teams_is_the_default() {
206 assert_eq!(default_scheme().id, "rusty-teams");
207 assert_eq!(scheme_by_id("teams"), Some(&TEAMS));
208 assert_eq!(scheme_by_id("solarized"), None);
209 }
210
211 #[test]
212 fn teams_scheme_uses_the_teams_dark_brand_ramp() {
213 assert_eq!(TEAMS.accent_text, 0x7f85f5);
214 assert_eq!(TEAMS.bubble_own, 0x2f2f4a);
215 assert_eq!(TEAMS.surface, 0x1f1f1f);
216 }
217
218 #[test]
219 fn every_scheme_layers_its_surfaces_from_dark_to_light() {
220 for scheme in SCHEMES {
221 let layers = [
222 scheme.background,
223 scheme.surface,
224 scheme.reply_surface,
225 scheme.row_hover,
226 scheme.surface_raised,
227 scheme.popup,
228 scheme.popup_hover,
229 ];
230 assert!(
231 layers.windows(2).all(|pair| pair[0] < pair[1]),
232 "{} surfaces are not ordered dark to light",
233 scheme.id
234 );
235 assert!(scheme.popup_nested > scheme.popup, "{}", scheme.id);
236 assert!(
237 scheme.popup_nested_hover > scheme.popup_nested,
238 "{}",
239 scheme.id
240 );
241 }
242 }
243
244 #[test]
245 fn a_thread_override_wins_over_the_process_scheme() {
246 set_thread_scheme(Some("teams"));
247 assert_eq!(active_scheme().id, "teams");
248 set_thread_scheme(Some("unknown"));
249 assert_eq!(active_scheme().id, "rusty-teams");
250 set_thread_scheme(None);
251 }
252}