BlueGit

ict rusty-teams

Files

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

appearance.rs

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