BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / active_appearance.rs

active_appearance.rs

active_appearance.rs 8.3 KiB Download Raw
1use gpui_kit::*;
2use store::Store;
3
4use crate::appearance::{self, AppIcon, AppearanceSettings, SystemTheme, ThemeChoice};
5use crate::theme::{self, ColorScheme};
6use crate::zoom;
7
8#[derive(Default)]
9pub struct ActiveAppearance {
10 settings: AppearanceSettings,
11 system: SystemTheme,
12}
13
14impl Global for ActiveAppearance {}
15
16pub fn system_theme(appearance: WindowAppearance) -> SystemTheme {
17 SystemTheme {
18 dark: matches!(
19 appearance,
20 WindowAppearance::Dark | WindowAppearance::VibrantDark
21 ),
22 high_contrast: appearance::system_high_contrast(),
23 }
24}
25
26pub fn install(store: &Store, cx: &mut App) {
27 let settings = appearance::load(store);
28 let system = system_theme(cx.window_appearance());
29 theme::set_active_scheme(settings.color_scheme.id);
30 theme::set_active_shade(settings.theme.shade(system));
31 zoom::set_factor(settings.zoom);
32 cx.set_global(ActiveAppearance { settings, system });
33}
34
35pub fn current(cx: &App) -> AppearanceSettings {
36 cx.try_global::<ActiveAppearance>()
37 .map(|active| active.settings)
38 .unwrap_or_default()
39}
40
41fn system(cx: &App) -> SystemTheme {
42 cx.try_global::<ActiveAppearance>()
43 .map(|active| active.system)
44 .unwrap_or_default()
45}
46
47fn store_settings(settings: AppearanceSettings, store: &Store, cx: &mut App) {
48 appearance::save(store, &settings);
49 let system = system(cx);
50 cx.set_global(ActiveAppearance { settings, system });
51}
52
53fn restyle(cx: &mut App) {
54 let active = cx.global::<ActiveAppearance>();
55 let shade = active.settings.theme.shade(active.system);
56 let scheme = active.settings.color_scheme.id;
57 if theme::scheme().id != scheme || theme::shade() != shade {
58 theme::set_active_scheme(scheme);
59 theme::set_active_shade(shade);
60 theme::apply(cx);
61 }
62 cx.refresh_windows();
63}
64
65pub fn set_color_scheme(scheme: &'static ColorScheme, store: &Store, cx: &mut App) {
66 let settings = AppearanceSettings {
67 color_scheme: scheme,
68 ..current(cx)
69 };
70 store_settings(settings, store, cx);
71 restyle(cx);
72}
73
74pub fn set_theme(choice: ThemeChoice, store: &Store, cx: &mut App) {
75 let settings = AppearanceSettings {
76 theme: choice,
77 ..current(cx)
78 };
79 store_settings(settings, store, cx);
80 restyle(cx);
81}
82
83pub fn system_appearance_changed(appearance: WindowAppearance, cx: &mut App) {
84 let system = system_theme(appearance);
85 if cx.try_global::<ActiveAppearance>().is_none() || self::system(cx) == system {
86 return;
87 }
88 let settings = current(cx);
89 cx.set_global(ActiveAppearance { settings, system });
90 restyle(cx);
91}
92
93pub fn set_app_icon(icon: AppIcon, store: &Store, cx: &mut App) {
94 let settings = AppearanceSettings {
95 app_icon: icon,
96 ..current(cx)
97 };
98 store_settings(settings, store, cx);
99 cx.refresh_windows();
100}
101
102pub fn set_zoom(zoom: f32, store: &Store, cx: &mut App) -> bool {
103 let current = current(cx);
104 if (current.zoom - zoom).abs() < f32::EPSILON {
105 return false;
106 }
107 let settings = AppearanceSettings { zoom, ..current };
108 store_settings(settings, store, cx);
109 zoom::set_factor(zoom);
110 cx.refresh_windows();
111 true
112}
113
114#[cfg(test)]
115mod tests {
116 use std::sync::{Mutex, MutexGuard};
117
118 use gpui_kit::{TestAppContext, WindowAppearance};
119 use store::Store;
120
121 use super::{
122 current, install, set_app_icon, set_color_scheme, set_theme, set_zoom,
123 system_appearance_changed,
124 };
125 use crate::appearance::{self, AppIcon, AppearanceSettings, ThemeChoice};
126 use crate::theme::{self, Shade};
127
128 /// The active scheme and shade are process-wide (`theme::set_active_scheme` and
129 /// friends), and every test here writes and reads them end to end. The harness
130 /// runs tests in parallel threads, so without this lock a sibling `install()`
131 /// resets the scheme to the default between one test's write and its assertion.
132 static THEME_STATE: Mutex<()> = Mutex::new(());
133
134 fn theme_state() -> MutexGuard<'static, ()> {
135 THEME_STATE
136 .lock()
137 .unwrap_or_else(|poisoned| poisoned.into_inner())
138 }
139
140 #[gpui_kit::test]
141 fn switching_saves_and_applies_live(cx: &mut TestAppContext) {
142 let _theme_state = theme_state();
143 let store = Store::open_in_memory().expect("in-memory store");
144 cx.update(|cx| {
145 gpui_kit::init(cx);
146 install(&store, cx);
147 theme::set_thread_scheme(None);
148 theme::set_thread_shade(None);
149 assert_eq!(current(cx), AppearanceSettings::default());
150 let teams = theme::scheme_by_id("teams").expect("teams scheme");
151 set_color_scheme(teams, &store, cx);
152 set_app_icon(AppIcon::MicrosoftTeams, &store, cx);
153 assert_eq!(theme::scheme().id, "teams");
154 assert_eq!(theme::accent(), theme::color(teams.dark.accent));
155 assert_eq!(appearance::load(&store), current(cx));
156 assert_eq!(current(cx).app_icon, AppIcon::MicrosoftTeams);
157 set_theme(ThemeChoice::Light, &store, cx);
158 assert_eq!(theme::shade(), Shade::Light);
159 assert_eq!(theme::background(), theme::color(teams.light.background));
160 assert_eq!(appearance::load(&store).theme, ThemeChoice::Light);
161 set_color_scheme(theme::default_scheme(), &store, cx);
162 set_theme(ThemeChoice::Dark, &store, cx);
163 assert_eq!(theme::scheme().id, "koninklijke-van-twist");
164 assert_eq!(theme::shade(), Shade::Dark);
165 });
166 }
167
168 #[gpui_kit::test]
169 fn a_fresh_install_starts_in_koninklijke_van_twist_light(cx: &mut TestAppContext) {
170 let _theme_state = theme_state();
171 let store = Store::open_in_memory().expect("in-memory store");
172 cx.update(|cx| {
173 gpui_kit::init(cx);
174 install(&store, cx);
175 theme::set_thread_scheme(None);
176 theme::set_thread_shade(None);
177 assert_eq!(theme::scheme().id, "koninklijke-van-twist");
178 assert_eq!(theme::shade(), Shade::Light);
179 assert_eq!(
180 theme::background(),
181 theme::color(theme::scheme().light.background)
182 );
183 });
184 }
185
186 #[gpui_kit::test]
187 fn a_saved_choice_wins_over_the_default(cx: &mut TestAppContext) {
188 let _theme_state = theme_state();
189 let store = Store::open_in_memory().expect("in-memory store");
190 appearance::save(
191 &store,
192 &AppearanceSettings {
193 color_scheme: theme::scheme_by_id("rusty-teams").expect("rusty-teams scheme"),
194 theme: ThemeChoice::Dark,
195 ..AppearanceSettings::default()
196 },
197 );
198 cx.update(|cx| {
199 gpui_kit::init(cx);
200 install(&store, cx);
201 theme::set_thread_scheme(None);
202 theme::set_thread_shade(None);
203 assert_eq!(theme::scheme().id, "rusty-teams");
204 assert_eq!(theme::shade(), Shade::Dark);
205 });
206 }
207
208 #[gpui_kit::test]
209 fn follow_os_tracks_the_window_appearance_at_runtime(cx: &mut TestAppContext) {
210 let _theme_state = theme_state();
211 let store = Store::open_in_memory().expect("in-memory store");
212 cx.update(|cx| {
213 gpui_kit::init(cx);
214 install(&store, cx);
215 set_theme(ThemeChoice::FollowOperatingSystem, &store, cx);
216 system_appearance_changed(WindowAppearance::Dark, cx);
217 assert_eq!(theme::shade(), Shade::Dark);
218 system_appearance_changed(WindowAppearance::Light, cx);
219 assert_eq!(theme::shade(), Shade::Light);
220 set_theme(ThemeChoice::Dark, &store, cx);
221 system_appearance_changed(WindowAppearance::Light, cx);
222 assert_eq!(theme::shade(), Shade::Dark);
223 });
224 }
225
226 #[gpui_kit::test]
227 fn zoom_is_saved_and_unchanged_steps_report_nothing(cx: &mut TestAppContext) {
228 let _theme_state = theme_state();
229 let store = Store::open_in_memory().expect("in-memory store");
230 cx.update(|cx| {
231 gpui_kit::init(cx);
232 install(&store, cx);
233 assert!(set_zoom(1.25, &store, cx));
234 assert!(!set_zoom(1.25, &store, cx));
235 assert_eq!(appearance::load(&store).zoom, 1.25);
236 assert!(set_zoom(1., &store, cx));
237 });
238 }
239}