1use gpui::{App, Global};
36
37#[derive(Default)]
39struct SystemReduceMotion {
40 applied: Option<bool>,
41 system: Option<bool>,
42 overridden: bool,
43 following: bool,
44}
45
46impl Global for SystemReduceMotion {}
47
48pub(crate) fn init(cx: &mut App) {
50 apply_system_reduce_motion(cx);
51}
52
53pub fn apply_system_reduce_motion(cx: &mut App) {
69 if is_test_scheduler(cx) {
70 return;
71 }
72 apply_preference(platform::read(), cx);
73 let state = cx.default_global::<SystemReduceMotion>();
74 if !state.following {
75 state.following = true;
76 platform::follow(cx);
77 }
78}
79
80#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
85pub enum ReduceMotionPreference {
86 #[default]
88 System,
89 Reduce,
91 Allow,
93}
94
95pub fn set_reduce_motion_preference(preference: ReduceMotionPreference, cx: &mut App) {
112 match preference {
113 ReduceMotionPreference::Reduce | ReduceMotionPreference::Allow => {
114 cx.default_global::<SystemReduceMotion>().overridden = true;
115 cx.set_reduce_motion(preference == ReduceMotionPreference::Reduce);
116 }
117 ReduceMotionPreference::System => {
118 let current = cx.reduce_motion();
119 let state = cx.default_global::<SystemReduceMotion>();
120 state.overridden = false;
121 state.applied = Some(current);
123 let system = state.system;
124 apply_preference(system, cx);
125 apply_system_reduce_motion(cx);
126 }
127 }
128}
129
130fn is_test_scheduler(cx: &App) -> bool {
133 cx.background_executor()
134 .scheduler_executor()
135 .scheduler()
136 .as_test()
137 .is_some()
138}
139
140fn apply_preference(preference: Option<bool>, cx: &mut App) {
143 let Some(reduce) = preference else {
144 return;
145 };
146 let state = cx.default_global::<SystemReduceMotion>();
147 state.system = Some(reduce);
148 let applied = state.applied;
149 if state.overridden || cx.reduce_motion() != applied.unwrap_or(false) {
150 return;
151 }
152 cx.set_reduce_motion(reduce);
153 cx.default_global::<SystemReduceMotion>().applied = Some(reduce);
154}
155
156#[cfg(target_os = "macos")]
157mod platform {
158 use gpui::App;
159 use objc2_app_kit::NSWorkspace;
160
161 pub(super) fn read() -> Option<bool> {
162 Some(NSWorkspace::sharedWorkspace().accessibilityDisplayShouldReduceMotion())
163 }
164
165 pub(super) fn follow(_cx: &mut App) {}
166}
167
168#[cfg(target_os = "windows")]
169mod platform {
170 use gpui::App;
171 use windows::Win32::{
172 Foundation::BOOL,
173 UI::WindowsAndMessaging::{
174 SPI_GETCLIENTAREAANIMATION, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SystemParametersInfoW,
175 },
176 };
177
178 pub(super) fn read() -> Option<bool> {
179 let mut animations_enabled = BOOL(0);
180 unsafe {
183 SystemParametersInfoW(
184 SPI_GETCLIENTAREAANIMATION,
185 0,
186 Some((&mut animations_enabled as *mut BOOL).cast()),
187 SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS(0),
188 )
189 }
190 .ok()?;
191 Some(!animations_enabled.as_bool())
192 }
193
194 pub(super) fn follow(_cx: &mut App) {}
195}
196
197#[cfg(target_os = "linux")]
198mod platform {
199 use ashpd::desktop::settings::{ReducedMotion, Settings};
200 use futures::StreamExt as _;
201 use gpui::App;
202
203 pub(super) fn read() -> Option<bool> {
205 None
206 }
207
208 pub(super) fn follow(cx: &mut App) {
209 cx.spawn(async move |cx| {
210 let settings = Settings::new().await.ok()?;
211 let current = settings.reduced_motion().await.ok()?;
212 cx.update(|cx| super::apply_preference(Some(reduces(current)), cx));
213 let mut changes = settings.receive_reduced_motion_changed().await.ok()?;
214 while let Some(preference) = changes.next().await {
215 cx.update(|cx| super::apply_preference(Some(reduces(preference)), cx));
216 }
217 Some(())
218 })
219 .detach();
220 }
221
222 fn reduces(preference: ReducedMotion) -> bool {
223 matches!(preference, ReducedMotion::ReducedMotion)
224 }
225}
226
227#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
228mod platform {
229 use gpui::App;
230
231 pub(super) fn read() -> Option<bool> {
232 None
233 }
234
235 pub(super) fn follow(_cx: &mut App) {}
236}
237
238#[cfg(test)]
239mod tests {
240 use gpui::TestAppContext;
241
242 use super::{
243 ReduceMotionPreference, apply_preference, apply_system_reduce_motion,
244 set_reduce_motion_preference,
245 };
246
247 #[gpui::test]
248 fn an_explicit_same_value_override_survives_system_changes(cx: &mut TestAppContext) {
249 cx.update(|cx| {
250 for (preference, value) in [
251 (ReduceMotionPreference::Allow, false),
252 (ReduceMotionPreference::Reduce, true),
253 ] {
254 set_reduce_motion_preference(ReduceMotionPreference::System, cx);
255 apply_preference(Some(value), cx);
256 set_reduce_motion_preference(preference, cx);
257 apply_preference(Some(!value), cx);
258 assert_eq!(cx.reduce_motion(), value);
259 apply_preference(Some(value), cx);
260 apply_preference(Some(!value), cx);
261 assert_eq!(cx.reduce_motion(), value);
262 }
263 });
264 }
265
266 #[gpui::test]
267 fn clearing_an_override_restores_the_latest_system_reading(cx: &mut TestAppContext) {
268 cx.update(|cx| {
269 apply_preference(Some(false), cx);
270 set_reduce_motion_preference(ReduceMotionPreference::Reduce, cx);
271 apply_preference(Some(true), cx);
272 apply_preference(Some(false), cx);
273 assert!(cx.reduce_motion());
274 set_reduce_motion_preference(ReduceMotionPreference::System, cx);
275 assert!(!cx.reduce_motion());
276 apply_preference(Some(true), cx);
277 assert!(cx.reduce_motion());
278
279 set_reduce_motion_preference(ReduceMotionPreference::Allow, cx);
280 apply_preference(Some(false), cx);
281 apply_preference(Some(true), cx);
282 assert!(!cx.reduce_motion());
283 set_reduce_motion_preference(ReduceMotionPreference::System, cx);
284 assert!(cx.reduce_motion());
285 apply_preference(Some(false), cx);
286 assert!(!cx.reduce_motion());
287 });
288 }
289
290 #[gpui::test]
291 fn an_override_before_the_first_reading_is_explicit_even_when_false(cx: &mut TestAppContext) {
292 cx.update(|cx| {
293 set_reduce_motion_preference(ReduceMotionPreference::Allow, cx);
294 apply_preference(Some(true), cx);
295 assert!(!cx.reduce_motion());
296 set_reduce_motion_preference(ReduceMotionPreference::System, cx);
297 assert!(cx.reduce_motion());
298 });
299 }
300
301 #[gpui::test]
302 fn clearing_without_a_system_reading_keeps_the_flag_until_one_arrives(cx: &mut TestAppContext) {
303 cx.update(|cx| {
304 set_reduce_motion_preference(ReduceMotionPreference::Reduce, cx);
305 apply_preference(None, cx);
306 set_reduce_motion_preference(ReduceMotionPreference::System, cx);
307 assert!(cx.reduce_motion());
308 apply_preference(Some(false), cx);
309 assert!(!cx.reduce_motion());
310 });
311 }
312
313 #[gpui::test]
314 fn clearing_can_resume_after_a_legacy_direct_override(cx: &mut TestAppContext) {
315 cx.update(|cx| {
316 apply_preference(Some(false), cx);
317 cx.set_reduce_motion(true);
318 apply_preference(Some(false), cx);
319 assert!(cx.reduce_motion());
320 set_reduce_motion_preference(ReduceMotionPreference::System, cx);
321 assert!(!cx.reduce_motion());
322 cx.set_reduce_motion(true);
324 apply_preference(Some(false), cx);
325 assert!(cx.reduce_motion());
326 });
327 }
328
329 #[gpui::test]
330 fn unknown_readings_preserve_the_last_known_system_preference(cx: &mut TestAppContext) {
331 cx.update(|cx| {
332 apply_preference(Some(true), cx);
333 set_reduce_motion_preference(ReduceMotionPreference::Allow, cx);
334 apply_preference(None, cx);
335 set_reduce_motion_preference(ReduceMotionPreference::System, cx);
336 assert!(cx.reduce_motion());
337 });
338 }
339
340 #[gpui::test]
341 fn the_application_can_change_its_explicit_override_without_yielding_ownership(
342 cx: &mut TestAppContext,
343 ) {
344 cx.update(|cx| {
345 apply_preference(Some(false), cx);
346 set_reduce_motion_preference(ReduceMotionPreference::Reduce, cx);
347 cx.set_reduce_motion(false);
348 apply_preference(Some(true), cx);
349 assert!(!cx.reduce_motion());
350 set_reduce_motion_preference(ReduceMotionPreference::System, cx);
351 assert!(cx.reduce_motion());
352 });
353 }
354
355 #[gpui::test]
356 fn repeated_clears_and_later_readings_respect_the_current_owner(cx: &mut TestAppContext) {
357 cx.update(|cx| {
358 apply_preference(Some(false), cx);
359 set_reduce_motion_preference(ReduceMotionPreference::Reduce, cx);
360 set_reduce_motion_preference(ReduceMotionPreference::System, cx);
361 set_reduce_motion_preference(ReduceMotionPreference::System, cx);
362 assert!(!cx.reduce_motion());
363 set_reduce_motion_preference(ReduceMotionPreference::Allow, cx);
364 apply_preference(Some(true), cx);
366 assert!(!cx.reduce_motion());
367 set_reduce_motion_preference(ReduceMotionPreference::System, cx);
368 assert!(cx.reduce_motion());
369 });
370 }
371
372 #[gpui::test]
373 fn a_system_preference_for_reduced_motion_sets_the_flag(cx: &mut TestAppContext) {
374 cx.update(|cx| {
375 apply_preference(Some(true), cx);
376 assert!(cx.reduce_motion());
377 });
378 }
379
380 #[gpui::test]
381 fn an_unknown_system_preference_leaves_the_flag_alone(cx: &mut TestAppContext) {
382 cx.update(|cx| {
383 apply_preference(None, cx);
384 assert!(!cx.reduce_motion());
385
386 cx.set_reduce_motion(true);
387 apply_preference(None, cx);
388 assert!(cx.reduce_motion());
389 });
390 }
391
392 #[gpui::test]
393 fn the_system_drives_the_flag_until_the_application_sets_it(cx: &mut TestAppContext) {
394 cx.update(|cx| {
395 apply_preference(Some(true), cx);
396 apply_preference(Some(false), cx);
397 assert!(!cx.reduce_motion());
398 apply_preference(Some(true), cx);
399 assert!(cx.reduce_motion());
400
401 cx.set_reduce_motion(false);
402 apply_preference(Some(true), cx);
403 assert!(!cx.reduce_motion());
404 });
405 }
406
407 #[gpui::test]
408 fn the_test_scheduler_is_never_asked_for_the_platform_preference(cx: &mut TestAppContext) {
409 cx.update(|cx| {
410 apply_system_reduce_motion(cx);
411 assert!(!cx.reduce_motion());
412 });
413 cx.run_until_parked();
414 cx.update(|cx| assert!(!cx.reduce_motion()));
415 }
416
417 #[gpui::test]
418 fn a_flag_the_application_set_before_the_first_reading_is_kept(cx: &mut TestAppContext) {
419 cx.update(|cx| {
420 cx.set_reduce_motion(true);
421 apply_preference(Some(false), cx);
422 assert!(cx.reduce_motion());
423 });
424 }
425}