BlueGit

ict rusty-teams

Files

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

appearance.rs

appearance.rs 4.1 KiB Download Raw
1use store::Store;
2
3use crate::theme::{self, ColorScheme};
4
5const META_KEY: &str = "appearance.settings";
6const COLOR_SCHEME_KEY: &str = "color_scheme";
7const APP_ICON_KEY: &str = "app_icon";
8const RUSTY_TEAMS_ICON_PNG: &[u8] = include_bytes!("../../app/assets/icon/teams-fast-256.png");
9const MICROSOFT_TEAMS_ICON_PNG: &[u8] =
10 include_bytes!("../../app/assets/icon/microsoft-teams-256.png");
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
13pub enum AppIcon {
14 #[default]
15 RustyTeams,
16 MicrosoftTeams,
17}
18
19impl AppIcon {
20 pub const ALL: [AppIcon; 2] = [AppIcon::RustyTeams, AppIcon::MicrosoftTeams];
21
22 pub fn key(self) -> &'static str {
23 match self {
24 AppIcon::RustyTeams => "rusty-teams",
25 AppIcon::MicrosoftTeams => "microsoft-teams",
26 }
27 }
28
29 pub fn from_key(key: &str) -> Option<AppIcon> {
30 AppIcon::ALL.into_iter().find(|icon| icon.key() == key)
31 }
32
33 pub fn label(self) -> &'static str {
34 match self {
35 AppIcon::RustyTeams => "Rusty Teams",
36 AppIcon::MicrosoftTeams => "Microsoft Teams",
37 }
38 }
39
40 pub fn png(self) -> &'static [u8] {
41 match self {
42 AppIcon::RustyTeams => RUSTY_TEAMS_ICON_PNG,
43 AppIcon::MicrosoftTeams => MICROSOFT_TEAMS_ICON_PNG,
44 }
45 }
46}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub struct AppearanceSettings {
50 pub color_scheme: &'static ColorScheme,
51 pub app_icon: AppIcon,
52}
53
54impl Default for AppearanceSettings {
55 fn default() -> Self {
56 AppearanceSettings {
57 color_scheme: theme::default_scheme(),
58 app_icon: AppIcon::default(),
59 }
60 }
61}
62
63pub fn load(store: &Store) -> AppearanceSettings {
64 store
65 .meta(META_KEY)
66 .ok()
67 .flatten()
68 .map(|text| parse(&text))
69 .unwrap_or_default()
70}
71
72pub fn save(store: &Store, settings: &AppearanceSettings) {
73 let _ = store.set_meta(META_KEY, &serialize(settings));
74}
75
76fn serialize(settings: &AppearanceSettings) -> String {
77 format!(
78 "{COLOR_SCHEME_KEY}={}\n{APP_ICON_KEY}={}",
79 settings.color_scheme.id,
80 settings.app_icon.key()
81 )
82}
83
84fn parse(text: &str) -> AppearanceSettings {
85 let mut settings = AppearanceSettings::default();
86 for line in text.lines() {
87 match line.split_once('=') {
88 Some((COLOR_SCHEME_KEY, value)) => {
89 if let Some(scheme) = theme::scheme_by_id(value) {
90 settings.color_scheme = scheme;
91 }
92 }
93 Some((APP_ICON_KEY, value)) => {
94 if let Some(icon) = AppIcon::from_key(value) {
95 settings.app_icon = icon;
96 }
97 }
98 _ => {}
99 }
100 }
101 settings
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn defaults_are_the_rusty_teams_look() {
110 let settings = AppearanceSettings::default();
111 assert_eq!(settings.color_scheme.id, "rusty-teams");
112 assert_eq!(settings.app_icon, AppIcon::RustyTeams);
113 }
114
115 #[test]
116 fn round_trips_every_field() {
117 let settings = AppearanceSettings {
118 color_scheme: theme::SCHEMES[1],
119 app_icon: AppIcon::MicrosoftTeams,
120 };
121 assert_eq!(parse(&serialize(&settings)), settings);
122 }
123
124 #[test]
125 fn unknown_values_fall_back_to_defaults() {
126 assert_eq!(parse(""), AppearanceSettings::default());
127 assert_eq!(
128 parse("color_scheme=solarized\napp_icon=clippy\njunk"),
129 AppearanceSettings::default()
130 );
131 }
132
133 #[test]
134 fn persists_in_store() {
135 let store = Store::open_in_memory().unwrap();
136 assert_eq!(load(&store), AppearanceSettings::default());
137 let changed = AppearanceSettings {
138 color_scheme: theme::scheme_by_id("teams").unwrap(),
139 app_icon: AppIcon::MicrosoftTeams,
140 };
141 save(&store, &changed);
142 assert_eq!(load(&store), changed);
143 }
144
145 #[test]
146 fn both_icons_are_png() {
147 for icon in AppIcon::ALL {
148 assert!(icon.png().starts_with(b"\x89PNG"), "{}", icon.key());
149 }
150 }
151}