BlueGit

ict rusty-teams

Files

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

appearance.rs

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