use gpui::{App, Global};
#[derive(Default)]
struct SystemReduceMotion {
applied: Option<bool>,
system: Option<bool>,
overridden: bool,
following: bool,
}
impl Global for SystemReduceMotion {}
pub(crate) fn init(cx: &mut App) {
apply_system_reduce_motion(cx);
}
pub fn apply_system_reduce_motion(cx: &mut App) {
if is_test_scheduler(cx) {
return;
}
apply_preference(platform::read(), cx);
let state = cx.default_global::<SystemReduceMotion>();
if !state.following {
state.following = true;
platform::follow(cx);
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ReduceMotionPreference {
#[default]
System,
Reduce,
Allow,
}
pub fn set_reduce_motion_preference(preference: ReduceMotionPreference, cx: &mut App) {
match preference {
ReduceMotionPreference::Reduce | ReduceMotionPreference::Allow => {
cx.default_global::<SystemReduceMotion>().overridden = true;
cx.set_reduce_motion(preference == ReduceMotionPreference::Reduce);
}
ReduceMotionPreference::System => {
let current = cx.reduce_motion();
let state = cx.default_global::<SystemReduceMotion>();
state.overridden = false;
state.applied = Some(current);
let system = state.system;
apply_preference(system, cx);
apply_system_reduce_motion(cx);
}
}
}
fn is_test_scheduler(cx: &App) -> bool {
cx.background_executor()
.scheduler_executor()
.scheduler()
.as_test()
.is_some()
}
fn apply_preference(preference: Option<bool>, cx: &mut App) {
let Some(reduce) = preference else {
return;
};
let state = cx.default_global::<SystemReduceMotion>();
state.system = Some(reduce);
let applied = state.applied;
if state.overridden || cx.reduce_motion() != applied.unwrap_or(false) {
return;
}
cx.set_reduce_motion(reduce);
cx.default_global::<SystemReduceMotion>().applied = Some(reduce);
}
#[cfg(target_os = "macos")]
mod platform {
use gpui::App;
use objc2_app_kit::NSWorkspace;
pub(super) fn read() -> Option<bool> {
Some(NSWorkspace::sharedWorkspace().accessibilityDisplayShouldReduceMotion())
}
pub(super) fn follow(_cx: &mut App) {}
}
#[cfg(target_os = "windows")]
mod platform {
use gpui::App;
use windows::Win32::{
Foundation::BOOL,
UI::WindowsAndMessaging::{
SPI_GETCLIENTAREAANIMATION, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SystemParametersInfoW,
},
};
pub(super) fn read() -> Option<bool> {
let mut animations_enabled = BOOL(0);
unsafe {
SystemParametersInfoW(
SPI_GETCLIENTAREAANIMATION,
0,
Some((&mut animations_enabled as *mut BOOL).cast()),
SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS(0),
)
}
.ok()?;
Some(!animations_enabled.as_bool())
}
pub(super) fn follow(_cx: &mut App) {}
}
#[cfg(target_os = "linux")]
mod platform {
use ashpd::desktop::settings::{ReducedMotion, Settings};
use futures::StreamExt as _;
use gpui::App;
pub(super) fn read() -> Option<bool> {
None
}
pub(super) fn follow(cx: &mut App) {
cx.spawn(async move |cx| {
let settings = Settings::new().await.ok()?;
let current = settings.reduced_motion().await.ok()?;
cx.update(|cx| super::apply_preference(Some(reduces(current)), cx));
let mut changes = settings.receive_reduced_motion_changed().await.ok()?;
while let Some(preference) = changes.next().await {
cx.update(|cx| super::apply_preference(Some(reduces(preference)), cx));
}
Some(())
})
.detach();
}
fn reduces(preference: ReducedMotion) -> bool {
matches!(preference, ReducedMotion::ReducedMotion)
}
}
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
mod platform {
use gpui::App;
pub(super) fn read() -> Option<bool> {
None
}
pub(super) fn follow(_cx: &mut App) {}
}
#[cfg(test)]
mod tests {
use gpui::TestAppContext;
use super::{
ReduceMotionPreference, apply_preference, apply_system_reduce_motion,
set_reduce_motion_preference,
};
#[gpui::test]
fn an_explicit_same_value_override_survives_system_changes(cx: &mut TestAppContext) {
cx.update(|cx| {
for (preference, value) in [
(ReduceMotionPreference::Allow, false),
(ReduceMotionPreference::Reduce, true),
] {
set_reduce_motion_preference(ReduceMotionPreference::System, cx);
apply_preference(Some(value), cx);
set_reduce_motion_preference(preference, cx);
apply_preference(Some(!value), cx);
assert_eq!(cx.reduce_motion(), value);
apply_preference(Some(value), cx);
apply_preference(Some(!value), cx);
assert_eq!(cx.reduce_motion(), value);
}
});
}
#[gpui::test]
fn clearing_an_override_restores_the_latest_system_reading(cx: &mut TestAppContext) {
cx.update(|cx| {
apply_preference(Some(false), cx);
set_reduce_motion_preference(ReduceMotionPreference::Reduce, cx);
apply_preference(Some(true), cx);
apply_preference(Some(false), cx);
assert!(cx.reduce_motion());
set_reduce_motion_preference(ReduceMotionPreference::System, cx);
assert!(!cx.reduce_motion());
apply_preference(Some(true), cx);
assert!(cx.reduce_motion());
set_reduce_motion_preference(ReduceMotionPreference::Allow, cx);
apply_preference(Some(false), cx);
apply_preference(Some(true), cx);
assert!(!cx.reduce_motion());
set_reduce_motion_preference(ReduceMotionPreference::System, cx);
assert!(cx.reduce_motion());
apply_preference(Some(false), cx);
assert!(!cx.reduce_motion());
});
}
#[gpui::test]
fn an_override_before_the_first_reading_is_explicit_even_when_false(cx: &mut TestAppContext) {
cx.update(|cx| {
set_reduce_motion_preference(ReduceMotionPreference::Allow, cx);
apply_preference(Some(true), cx);
assert!(!cx.reduce_motion());
set_reduce_motion_preference(ReduceMotionPreference::System, cx);
assert!(cx.reduce_motion());
});
}
#[gpui::test]
fn clearing_without_a_system_reading_keeps_the_flag_until_one_arrives(cx: &mut TestAppContext) {
cx.update(|cx| {
set_reduce_motion_preference(ReduceMotionPreference::Reduce, cx);
apply_preference(None, cx);
set_reduce_motion_preference(ReduceMotionPreference::System, cx);
assert!(cx.reduce_motion());
apply_preference(Some(false), cx);
assert!(!cx.reduce_motion());
});
}
#[gpui::test]
fn clearing_can_resume_after_a_legacy_direct_override(cx: &mut TestAppContext) {
cx.update(|cx| {
apply_preference(Some(false), cx);
cx.set_reduce_motion(true);
apply_preference(Some(false), cx);
assert!(cx.reduce_motion());
set_reduce_motion_preference(ReduceMotionPreference::System, cx);
assert!(!cx.reduce_motion());
cx.set_reduce_motion(true);
apply_preference(Some(false), cx);
assert!(cx.reduce_motion());
});
}
#[gpui::test]
fn unknown_readings_preserve_the_last_known_system_preference(cx: &mut TestAppContext) {
cx.update(|cx| {
apply_preference(Some(true), cx);
set_reduce_motion_preference(ReduceMotionPreference::Allow, cx);
apply_preference(None, cx);
set_reduce_motion_preference(ReduceMotionPreference::System, cx);
assert!(cx.reduce_motion());
});
}
#[gpui::test]
fn the_application_can_change_its_explicit_override_without_yielding_ownership(
cx: &mut TestAppContext,
) {
cx.update(|cx| {
apply_preference(Some(false), cx);
set_reduce_motion_preference(ReduceMotionPreference::Reduce, cx);
cx.set_reduce_motion(false);
apply_preference(Some(true), cx);
assert!(!cx.reduce_motion());
set_reduce_motion_preference(ReduceMotionPreference::System, cx);
assert!(cx.reduce_motion());
});
}
#[gpui::test]
fn repeated_clears_and_later_readings_respect_the_current_owner(cx: &mut TestAppContext) {
cx.update(|cx| {
apply_preference(Some(false), cx);
set_reduce_motion_preference(ReduceMotionPreference::Reduce, cx);
set_reduce_motion_preference(ReduceMotionPreference::System, cx);
set_reduce_motion_preference(ReduceMotionPreference::System, cx);
assert!(!cx.reduce_motion());
set_reduce_motion_preference(ReduceMotionPreference::Allow, cx);
apply_preference(Some(true), cx);
assert!(!cx.reduce_motion());
set_reduce_motion_preference(ReduceMotionPreference::System, cx);
assert!(cx.reduce_motion());
});
}
#[gpui::test]
fn a_system_preference_for_reduced_motion_sets_the_flag(cx: &mut TestAppContext) {
cx.update(|cx| {
apply_preference(Some(true), cx);
assert!(cx.reduce_motion());
});
}
#[gpui::test]
fn an_unknown_system_preference_leaves_the_flag_alone(cx: &mut TestAppContext) {
cx.update(|cx| {
apply_preference(None, cx);
assert!(!cx.reduce_motion());
cx.set_reduce_motion(true);
apply_preference(None, cx);
assert!(cx.reduce_motion());
});
}
#[gpui::test]
fn the_system_drives_the_flag_until_the_application_sets_it(cx: &mut TestAppContext) {
cx.update(|cx| {
apply_preference(Some(true), cx);
apply_preference(Some(false), cx);
assert!(!cx.reduce_motion());
apply_preference(Some(true), cx);
assert!(cx.reduce_motion());
cx.set_reduce_motion(false);
apply_preference(Some(true), cx);
assert!(!cx.reduce_motion());
});
}
#[gpui::test]
fn the_test_scheduler_is_never_asked_for_the_platform_preference(cx: &mut TestAppContext) {
cx.update(|cx| {
apply_system_reduce_motion(cx);
assert!(!cx.reduce_motion());
});
cx.run_until_parked();
cx.update(|cx| assert!(!cx.reduce_motion()));
}
#[gpui::test]
fn a_flag_the_application_set_before_the_first_reading_is_kept(cx: &mut TestAppContext) {
cx.update(|cx| {
cx.set_reduce_motion(true);
apply_preference(Some(false), cx);
assert!(cx.reduce_motion());
});
}
}