BlueGit

ict rusty-teams

Files

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

active_appearance.rs

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