BlueGit

ict rusty-teams

Files

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

appearance.rs

appearance.rs 12.5 KiB Download Raw
1use store::Store;
2
3use crate::theme::{self, ColorScheme, Shade};
4
5const META_KEY: &str = "appearance.settings";
6const COLOR_SCHEME_KEY: &str = "color_scheme";
7const APP_ICON_KEY: &str = "app_icon";
8const THEME_KEY: &str = "theme";
9const ZOOM_KEY: &str = "zoom";
10pub const ZOOM_STEPS: [f32; 14] = [
11 0.5, 0.67, 0.75, 0.8, 0.9, 1., 1.1, 1.25, 1.5, 1.75, 2., 2.5, 3., 4.,
12];
13pub const DEFAULT_ZOOM: f32 = 1.;
14/// Look of a fresh install: the KVT scheme in its light shade (house style: logo on white).
15/// `ThemeChoice::default()` stays dark: it is what settings saved without a theme key meant.
16const DEFAULT_THEME: ThemeChoice = ThemeChoice::Light;
17/// Scheme of settings saved without a `color_scheme` key (before the KVT scheme existed).
18const LEGACY_COLOR_SCHEME: &str = "rusty-teams";
19const KVT_TEAMS_ICON_PNG: &[u8] = include_bytes!("../../app/assets/icon/kvt-teams-256.png");
20const RUSTY_TEAMS_ICON_PNG: &[u8] = include_bytes!("../../app/assets/icon/teams-fast-256.png");
21const MICROSOFT_TEAMS_ICON_PNG: &[u8] =
22 include_bytes!("../../app/assets/icon/microsoft-teams-256.png");
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
25pub enum AppIcon {
26 #[default]
27 KoninklijkeVanTwist,
28 RustyTeams,
29 MicrosoftTeams,
30}
31
32impl AppIcon {
33 pub const ALL: [AppIcon; 3] = [
34 AppIcon::KoninklijkeVanTwist,
35 AppIcon::RustyTeams,
36 AppIcon::MicrosoftTeams,
37 ];
38
39 pub fn key(self) -> &'static str {
40 match self {
41 AppIcon::KoninklijkeVanTwist => "koninklijke-van-twist",
42 AppIcon::RustyTeams => "rusty-teams",
43 AppIcon::MicrosoftTeams => "microsoft-teams",
44 }
45 }
46
47 pub fn from_key(key: &str) -> Option<AppIcon> {
48 AppIcon::ALL.into_iter().find(|icon| icon.key() == key)
49 }
50
51 pub fn label(self) -> &'static str {
52 match self {
53 AppIcon::KoninklijkeVanTwist => "Koninklijke van Twist",
54 AppIcon::RustyTeams => "Rusty Teams",
55 AppIcon::MicrosoftTeams => "Microsoft Teams",
56 }
57 }
58
59 pub fn png(self) -> &'static [u8] {
60 match self {
61 AppIcon::KoninklijkeVanTwist => KVT_TEAMS_ICON_PNG,
62 AppIcon::RustyTeams => RUSTY_TEAMS_ICON_PNG,
63 AppIcon::MicrosoftTeams => MICROSOFT_TEAMS_ICON_PNG,
64 }
65 }
66}
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
69pub enum ThemeChoice {
70 FollowOperatingSystem,
71 Light,
72 #[default]
73 Dark,
74 HighContrast,
75}
76
77impl ThemeChoice {
78 pub const ALL: [ThemeChoice; 4] = [
79 ThemeChoice::FollowOperatingSystem,
80 ThemeChoice::Light,
81 ThemeChoice::Dark,
82 ThemeChoice::HighContrast,
83 ];
84
85 pub fn key(self) -> &'static str {
86 match self {
87 ThemeChoice::FollowOperatingSystem => "followOsTheme",
88 ThemeChoice::Light => "light",
89 ThemeChoice::Dark => "dark",
90 ThemeChoice::HighContrast => "contrast",
91 }
92 }
93
94 pub fn from_key(key: &str) -> Option<ThemeChoice> {
95 ThemeChoice::ALL
96 .into_iter()
97 .find(|choice| choice.key() == key)
98 }
99
100 pub fn label(self) -> &'static str {
101 match self {
102 ThemeChoice::FollowOperatingSystem => "Follow operating system theme",
103 ThemeChoice::Light => "Light",
104 ThemeChoice::Dark => "Dark",
105 ThemeChoice::HighContrast => "High contrast",
106 }
107 }
108
109 pub fn shade(self, system: SystemTheme) -> Shade {
110 match self {
111 ThemeChoice::Light => Shade::Light,
112 ThemeChoice::Dark => Shade::Dark,
113 ThemeChoice::HighContrast => Shade::HighContrast,
114 ThemeChoice::FollowOperatingSystem if system.high_contrast => Shade::HighContrast,
115 ThemeChoice::FollowOperatingSystem if system.dark => Shade::Dark,
116 ThemeChoice::FollowOperatingSystem => Shade::Light,
117 }
118 }
119}
120
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
122pub struct SystemTheme {
123 pub dark: bool,
124 pub high_contrast: bool,
125}
126
127#[derive(Debug, Clone, Copy, PartialEq)]
128pub struct AppearanceSettings {
129 pub color_scheme: &'static ColorScheme,
130 pub app_icon: AppIcon,
131 pub theme: ThemeChoice,
132 pub zoom: f32,
133}
134
135impl Default for AppearanceSettings {
136 fn default() -> Self {
137 AppearanceSettings {
138 color_scheme: theme::default_scheme(),
139 app_icon: AppIcon::default(),
140 theme: DEFAULT_THEME,
141 zoom: DEFAULT_ZOOM,
142 }
143 }
144}
145
146pub fn nearest_zoom_step(zoom: f32) -> f32 {
147 ZOOM_STEPS
148 .into_iter()
149 .min_by(|first, second| (first - zoom).abs().total_cmp(&(second - zoom).abs()))
150 .unwrap_or(DEFAULT_ZOOM)
151}
152
153pub fn zoom_in(zoom: f32) -> f32 {
154 ZOOM_STEPS
155 .into_iter()
156 .find(|step| *step > zoom + f32::EPSILON)
157 .unwrap_or(ZOOM_STEPS[ZOOM_STEPS.len() - 1])
158}
159
160pub fn zoom_out(zoom: f32) -> f32 {
161 ZOOM_STEPS
162 .into_iter()
163 .rev()
164 .find(|step| *step < zoom - f32::EPSILON)
165 .unwrap_or(ZOOM_STEPS[0])
166}
167
168pub fn zoom_percent(zoom: f32) -> u32 {
169 (zoom * 100. - 0.001).ceil().max(0.) as u32
170}
171
172#[cfg(windows)]
173pub fn system_high_contrast() -> bool {
174 use windows::Win32::UI::Accessibility::{HCF_HIGHCONTRASTON, HIGHCONTRASTW};
175 use windows::Win32::UI::WindowsAndMessaging::{
176 SPI_GETHIGHCONTRAST, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SystemParametersInfoW,
177 };
178 let mut info = HIGHCONTRASTW {
179 cbSize: std::mem::size_of::<HIGHCONTRASTW>() as u32,
180 ..Default::default()
181 };
182 let read = unsafe {
183 SystemParametersInfoW(
184 SPI_GETHIGHCONTRAST,
185 info.cbSize,
186 Some(std::ptr::from_mut(&mut info).cast()),
187 SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS(0),
188 )
189 };
190 read.is_ok() && info.dwFlags.0 & HCF_HIGHCONTRASTON.0 != 0
191}
192
193#[cfg(not(windows))]
194pub fn system_high_contrast() -> bool {
195 false
196}
197
198pub fn load(store: &Store) -> AppearanceSettings {
199 store
200 .meta(META_KEY)
201 .ok()
202 .flatten()
203 .map(|text| parse(&text))
204 .unwrap_or_default()
205}
206
207pub fn save(store: &Store, settings: &AppearanceSettings) {
208 let _ = store.set_meta(META_KEY, &serialize(settings));
209}
210
211fn serialize(settings: &AppearanceSettings) -> String {
212 format!(
213 "{COLOR_SCHEME_KEY}={}\n{APP_ICON_KEY}={}\n{THEME_KEY}={}\n{ZOOM_KEY}={}",
214 settings.color_scheme.id,
215 settings.app_icon.key(),
216 settings.theme.key(),
217 settings.zoom
218 )
219}
220
221fn parse(text: &str) -> AppearanceSettings {
222 let mut settings = AppearanceSettings::default();
223 let has_key = |key: &str| {
224 text.lines()
225 .any(|line| line.split_once('=').is_some_and(|(name, _)| name == key))
226 };
227 // Settings saved before the KVT default existed may lack a key. That user never picked
228 // one and keeps the look the app had then, instead of switching to the new default.
229 if !has_key(COLOR_SCHEME_KEY) && !text.trim().is_empty() {
230 if let Some(scheme) = theme::scheme_by_id(LEGACY_COLOR_SCHEME) {
231 settings.color_scheme = scheme;
232 }
233 }
234 if !has_key(THEME_KEY) && !text.trim().is_empty() {
235 settings.theme = ThemeChoice::default();
236 }
237 if !has_key(APP_ICON_KEY) && !text.trim().is_empty() {
238 settings.app_icon = AppIcon::RustyTeams;
239 }
240 for line in text.lines() {
241 match line.split_once('=') {
242 Some((COLOR_SCHEME_KEY, value)) => {
243 if let Some(scheme) = theme::scheme_by_id(value) {
244 settings.color_scheme = scheme;
245 }
246 }
247 Some((APP_ICON_KEY, value)) => {
248 if let Some(icon) = AppIcon::from_key(value) {
249 settings.app_icon = icon;
250 }
251 }
252 Some((THEME_KEY, value)) => {
253 if let Some(choice) = ThemeChoice::from_key(value) {
254 settings.theme = choice;
255 }
256 }
257 Some((ZOOM_KEY, value)) => {
258 if let Ok(zoom) = value.parse::<f32>() {
259 settings.zoom = nearest_zoom_step(zoom);
260 }
261 }
262 _ => {}
263 }
264 }
265 settings
266}
267
268#[cfg(test)]
269mod tests {
270 use super::*;
271
272 #[test]
273 fn defaults_are_the_koninklijke_van_twist_look() {
274 let settings = AppearanceSettings::default();
275 assert_eq!(settings.color_scheme.id, "koninklijke-van-twist");
276 assert_eq!(settings.app_icon, AppIcon::KoninklijkeVanTwist);
277 assert_eq!(settings.theme, ThemeChoice::Light);
278 assert_eq!(settings.zoom, 1.);
279 }
280
281 #[test]
282 fn round_trips_every_field() {
283 let settings = AppearanceSettings {
284 color_scheme: theme::scheme_by_id("teams").unwrap(),
285 app_icon: AppIcon::MicrosoftTeams,
286 theme: ThemeChoice::FollowOperatingSystem,
287 zoom: 1.25,
288 };
289 assert_eq!(parse(&serialize(&settings)), settings);
290 }
291
292 #[test]
293 fn unknown_values_fall_back_to_defaults() {
294 assert_eq!(parse(""), AppearanceSettings::default());
295 assert_eq!(
296 parse("color_scheme=solarized\napp_icon=clippy\ntheme=sepia\nzoom=big\njunk"),
297 AppearanceSettings::default()
298 );
299 }
300
301 #[test]
302 fn persists_in_store() {
303 let store = Store::open_in_memory().unwrap();
304 assert_eq!(load(&store), AppearanceSettings::default());
305 let changed = AppearanceSettings {
306 color_scheme: theme::scheme_by_id("teams").unwrap(),
307 app_icon: AppIcon::MicrosoftTeams,
308 theme: ThemeChoice::Light,
309 zoom: 0.9,
310 };
311 save(&store, &changed);
312 assert_eq!(load(&store), changed);
313 }
314
315 #[test]
316 fn older_settings_without_theme_and_zoom_keep_the_dark_default() {
317 let settings = parse("color_scheme=teams\napp_icon=rusty-teams");
318 assert_eq!(settings.theme, ThemeChoice::Dark);
319 assert_eq!(settings.zoom, DEFAULT_ZOOM);
320 assert_eq!(parse("zoom=1.3").zoom, 1.25);
321 }
322
323 #[test]
324 fn an_explicit_choice_survives_the_new_default() {
325 for scheme in theme::SCHEMES {
326 for choice in ThemeChoice::ALL {
327 let settings = AppearanceSettings {
328 color_scheme: scheme,
329 theme: choice,
330 ..AppearanceSettings::default()
331 };
332 assert_eq!(parse(&serialize(&settings)), settings);
333 }
334 }
335 // The look the app had before the KVT scheme, saved explicitly, stays as it is.
336 let before = parse("color_scheme=rusty-teams\napp_icon=rusty-teams\ntheme=dark\nzoom=1");
337 assert_eq!(before.color_scheme.id, "rusty-teams");
338 assert_eq!(before.theme, ThemeChoice::Dark);
339 }
340
341 #[test]
342 fn settings_saved_without_a_scheme_or_theme_keep_the_old_look() {
343 let settings = parse("zoom=1.25");
344 assert_eq!(settings.color_scheme.id, "rusty-teams");
345 assert_eq!(settings.theme, ThemeChoice::Dark);
346 assert_eq!(settings.app_icon, AppIcon::RustyTeams);
347 assert_eq!(settings.zoom, 1.25);
348 assert_eq!(parse(" \n"), AppearanceSettings::default());
349 }
350
351 #[test]
352 fn follow_os_takes_dark_light_and_forced_contrast_from_the_system() {
353 let follow = ThemeChoice::FollowOperatingSystem;
354 let dark = SystemTheme {
355 dark: true,
356 high_contrast: false,
357 };
358 assert_eq!(follow.shade(dark), Shade::Dark);
359 assert_eq!(follow.shade(SystemTheme::default()), Shade::Light);
360 let forced = SystemTheme {
361 dark: false,
362 high_contrast: true,
363 };
364 assert_eq!(follow.shade(forced), Shade::HighContrast);
365 assert_eq!(ThemeChoice::Light.shade(dark), Shade::Light);
366 assert_eq!(ThemeChoice::HighContrast.shade(dark), Shade::HighContrast);
367 }
368
369 #[test]
370 fn theme_keys_are_the_teams_option_keys() {
371 let keys: Vec<_> = ThemeChoice::ALL.iter().map(|choice| choice.key()).collect();
372 assert_eq!(keys, ["followOsTheme", "light", "dark", "contrast"]);
373 }
374
375 #[test]
376 fn zoom_walks_the_steps_and_stops_at_both_ends() {
377 assert_eq!(zoom_in(1.), 1.1);
378 assert_eq!(zoom_out(1.), 0.9);
379 assert_eq!(zoom_in(4.), 4.);
380 assert_eq!(zoom_out(0.5), 0.5);
381 assert_eq!(zoom_out(zoom_in(1.25)), 1.25);
382 assert_eq!(zoom_percent(0.67), 67);
383 assert_eq!(zoom_percent(1.1), 110);
384 assert_eq!(zoom_percent(1.), 100);
385 }
386
387 #[test]
388 fn both_icons_are_png() {
389 for icon in AppIcon::ALL {
390 assert!(icon.png().starts_with(b"\x89PNG"), "{}", icon.key());
391 }
392 }
393}