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