use super::window::Appearance;
pub fn system_appearance() -> Option<Appearance> {
read()
}
#[cfg(windows)]
fn read() -> Option<Appearance> {
use windows::{
Win32::{
System::Registry::{HKEY_CURRENT_USER, RRF_RT_REG_DWORD, RegGetValueW},
UI::{
Accessibility::{HCF_HIGHCONTRASTON, HIGHCONTRASTW},
WindowsAndMessaging::{SPI_GETHIGHCONTRAST, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SystemParametersInfoW},
},
},
core::w,
};
let (mut light, mut size) = (1u32, size_of::<u32>() as u32);
let read = unsafe {
RegGetValueW(
HKEY_CURRENT_USER,
w!("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"),
w!("AppsUseLightTheme"),
RRF_RT_REG_DWORD,
None,
Some((&raw mut light).cast()),
Some(&raw mut size),
)
};
if read.is_err() {
return None;
}
let mut contrast = HIGHCONTRASTW {
cbSize: size_of::<HIGHCONTRASTW>() as u32,
..Default::default()
};
let high = unsafe {
SystemParametersInfoW(
SPI_GETHIGHCONTRAST,
contrast.cbSize,
Some((&raw mut contrast).cast()),
SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS(0),
)
}
.is_ok()
&& contrast.dwFlags.contains(HCF_HIGHCONTRASTON);
Some(if light == 0 && !high {
Appearance::Dark
} else {
Appearance::Light
})
}
#[cfg(target_os = "macos")]
fn read() -> Option<Appearance> {
use objc2::{class, msg_send, rc::Retained, runtime::AnyObject};
use objc2_foundation::NSString;
let style: Option<Retained<NSString>> = unsafe {
let defaults: Option<Retained<AnyObject>> = msg_send![class!(NSUserDefaults), standardUserDefaults];
let defaults = defaults?;
msg_send![&*defaults, stringForKey: &*NSString::from_str("AppleInterfaceStyle")]
};
Some(match style {
Some(style) if style.to_string().eq_ignore_ascii_case("dark") => Appearance::Dark,
_ => Appearance::Light,
})
}
#[cfg(target_os = "ios")]
fn read() -> Option<Appearance> {
use objc2::{class, msg_send, rc::Retained, runtime::AnyObject};
let style: isize = unsafe {
let screen: Option<Retained<AnyObject>> = msg_send![class!(UIScreen), mainScreen];
let screen = screen?;
let traits: Option<Retained<AnyObject>> = msg_send![&*screen, traitCollection];
let traits = traits?;
msg_send![&*traits, userInterfaceStyle]
};
match style {
1 => Some(Appearance::Light),
2 => Some(Appearance::Dark),
_ => None,
}
}
#[cfg(target_arch = "wasm32")]
fn read() -> Option<Appearance> {
let dark = web_sys::window()?
.match_media("(prefers-color-scheme: dark)")
.ok()??
.matches();
Some(if dark { Appearance::Dark } else { Appearance::Light })
}
#[cfg(not(any(windows, target_os = "macos", target_os = "ios", target_arch = "wasm32")))]
fn read() -> Option<Appearance> {
None
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(windows)]
#[test]
fn windows_says_what_it_is_in_with_no_window() {
assert!(system_appearance().is_some());
}
#[cfg(not(any(windows, target_os = "macos", target_os = "ios", target_arch = "wasm32")))]
#[test]
fn a_system_that_cannot_be_asked_says_nothing() {
assert_eq!(system_appearance(), None);
}
}