#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn system_reduced_motion() -> Option<bool> {
platform::read()
}
#[cfg(windows)]
mod platform {
use windows::{
Win32::UI::WindowsAndMessaging::{
SPI_GETCLIENTAREAANIMATION, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SystemParametersInfoW,
},
core::BOOL,
};
pub(super) fn read() -> Option<bool> {
let mut animations = BOOL::from(true);
unsafe {
SystemParametersInfoW(
SPI_GETCLIENTAREAANIMATION,
0,
Some((&raw mut animations).cast()),
SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS(0),
)
}
.ok()?;
Some(!animations.as_bool())
}
}
#[cfg(target_os = "macos")]
mod platform {
use objc2::{class, msg_send, runtime::AnyObject};
#[link(name = "AppKit", kind = "framework")]
unsafe extern "C" {}
pub(super) fn read() -> Option<bool> {
unsafe {
let workspace: *mut AnyObject = msg_send![class!(NSWorkspace), sharedWorkspace];
if workspace.is_null() {
return None;
}
let reduce: bool = msg_send![workspace, accessibilityDisplayShouldReduceMotion];
Some(reduce)
}
}
}
#[cfg(target_os = "ios")]
mod platform {
#[link(name = "UIKit", kind = "framework")]
unsafe extern "C" {
fn UIAccessibilityIsReduceMotionEnabled() -> bool;
}
pub(super) fn read() -> Option<bool> {
Some(unsafe { UIAccessibilityIsReduceMotionEnabled() })
}
}
#[cfg(not(any(windows, target_os = "macos", target_os = "ios")))]
mod platform {
pub(super) fn read() -> Option<bool> {
None
}
}
#[cfg(test)]
mod tests {
use crate::app::theme::Theme;
#[cfg(any(windows, target_os = "macos", target_os = "ios"))]
#[test]
fn the_platform_has_the_setting() {
assert!(super::system_reduced_motion().is_some());
}
#[test]
fn the_app_decides_over_the_system() {
let theme = Theme::default();
assert!(!theme.reduced_motion(), "a theme no shell wrote to reduces nothing");
theme.set_system_reduced_motion(true);
assert!(theme.reduced_motion(), "the system's setting is followed");
theme.set_reduced_motion(Some(false));
assert!(!theme.reduced_motion());
theme.set_system_reduced_motion(false);
theme.set_reduced_motion(Some(true));
assert!(theme.reduced_motion());
theme.set_reduced_motion(None);
assert!(
!theme.reduced_motion(),
"and followed again once the app stops deciding"
);
}
#[test]
fn a_setting_that_does_not_change_does_not_change_the_theme() {
let theme = Theme::default();
theme.set_system_reduced_motion(true);
let version = theme.version();
theme.set_system_reduced_motion(true);
theme.set_reduced_motion(None);
assert_eq!(theme.version(), version);
}
}