use gpui::{App, Subscription, Window, WindowAppearance};
use gpui_material_theme::{Appearance, AppearanceMode, MaterialTheme};
use crate::root::{set_material_theme, set_window_material_theme};
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ReduceMotionPreference {
#[default]
System,
Reduce,
Allow,
}
impl ReduceMotionPreference {
pub(crate) const fn to_base(self) -> gpui_base::ReduceMotionPreference {
match self {
Self::System => gpui_base::ReduceMotionPreference::System,
Self::Reduce => gpui_base::ReduceMotionPreference::Reduce,
Self::Allow => gpui_base::ReduceMotionPreference::Allow,
}
}
}
pub fn set_reduce_motion_preference(preference: ReduceMotionPreference, cx: &mut App) {
gpui_base::set_reduce_motion_preference(preference.to_base(), cx);
}
pub const fn appearance_from_window(appearance: WindowAppearance) -> Appearance {
match appearance {
WindowAppearance::Light | WindowAppearance::VibrantLight => Appearance::Light,
WindowAppearance::Dark | WindowAppearance::VibrantDark => Appearance::Dark,
}
}
pub fn system_appearance(cx: &App) -> Appearance {
appearance_from_window(cx.window_appearance())
}
pub fn sync_system_reduce_motion(cx: &mut App) {
gpui_base::apply_system_reduce_motion(cx);
}
struct AppearanceTracker<F> {
mode: AppearanceMode,
last_applied: Option<Appearance>,
build_theme: F,
}
impl<F> AppearanceTracker<F>
where
F: FnMut(Appearance) -> MaterialTheme,
{
fn new(mode: AppearanceMode, build_theme: F) -> Self {
Self {
mode,
last_applied: None,
build_theme,
}
}
fn next_theme(&mut self, window_appearance: WindowAppearance) -> Option<MaterialTheme> {
let resolved = self.mode.resolve(appearance_from_window(window_appearance));
if self.last_applied == Some(resolved) {
return None;
}
self.last_applied = Some(resolved);
Some((self.build_theme)(resolved))
}
}
pub fn observe_system_appearance(
window: &mut Window,
cx: &mut App,
mode: AppearanceMode,
build_theme: impl FnMut(Appearance) -> MaterialTheme + 'static,
) -> Subscription {
let mut tracker = AppearanceTracker::new(mode, build_theme);
if let Some(theme) = tracker.next_theme(window.appearance()) {
set_material_theme(theme, cx);
}
if mode != AppearanceMode::System {
return Subscription::new(|| {});
}
window.observe_window_appearance(move |window, cx| {
if let Some(theme) = tracker.next_theme(window.appearance()) {
set_material_theme(theme, cx);
}
})
}
pub fn observe_window_system_appearance(
window: &mut Window,
cx: &mut App,
mode: AppearanceMode,
build_theme: impl FnMut(Appearance) -> MaterialTheme + 'static,
) -> Subscription {
let mut tracker = AppearanceTracker::new(mode, build_theme);
if let Some(theme) = tracker.next_theme(window.appearance()) {
set_window_material_theme(Some(theme), window, cx);
}
if mode != AppearanceMode::System {
return Subscription::new(|| {});
}
window.observe_window_appearance(move |window, cx| {
if let Some(theme) = tracker.next_theme(window.appearance()) {
set_window_material_theme(Some(theme), window, cx);
}
})
}
#[cfg(test)]
mod tests {
use std::cell::Cell;
use super::*;
use gpui_material_theme::{Density, MotionScheme};
#[test]
fn reduce_motion_preference_is_material_owned_and_maps_all_variants() {
assert_ne!(
std::any::TypeId::of::<ReduceMotionPreference>(),
std::any::TypeId::of::<gpui_base::ReduceMotionPreference>()
);
assert_eq!(
ReduceMotionPreference::default(),
ReduceMotionPreference::System
);
assert_eq!(
ReduceMotionPreference::System.to_base(),
gpui_base::ReduceMotionPreference::System
);
assert_eq!(
ReduceMotionPreference::Reduce.to_base(),
gpui_base::ReduceMotionPreference::Reduce
);
assert_eq!(
ReduceMotionPreference::Allow.to_base(),
gpui_base::ReduceMotionPreference::Allow
);
}
#[test]
fn maps_all_window_appearances_to_material_appearance() {
const LIGHT: Appearance = appearance_from_window(WindowAppearance::Light);
const VIBRANT_LIGHT: Appearance = appearance_from_window(WindowAppearance::VibrantLight);
const DARK: Appearance = appearance_from_window(WindowAppearance::Dark);
const VIBRANT_DARK: Appearance = appearance_from_window(WindowAppearance::VibrantDark);
assert_eq!(LIGHT, Appearance::Light);
assert_eq!(VIBRANT_LIGHT, Appearance::Light);
assert_eq!(DARK, Appearance::Dark);
assert_eq!(VIBRANT_DARK, Appearance::Dark);
}
#[test]
fn tracker_skips_redundant_updates_and_preserves_custom_builder_settings() {
let calls = Cell::new(0usize);
let mut tracker = AppearanceTracker::new(AppearanceMode::System, |appearance| {
calls.set(calls.get() + 1);
MaterialTheme::baseline(appearance)
.motion(MotionScheme::Expressive)
.density(Density::Compact)
});
let first = tracker.next_theme(WindowAppearance::Light).unwrap();
assert_eq!(first.colors.appearance(), Appearance::Light);
assert_eq!(first.motion, MotionScheme::Expressive);
assert_eq!(first.layout.density, Density::Compact);
assert_eq!(calls.get(), 1);
assert!(tracker.next_theme(WindowAppearance::Light).is_none());
assert!(tracker.next_theme(WindowAppearance::VibrantLight).is_none());
assert_eq!(calls.get(), 1);
let dark = tracker.next_theme(WindowAppearance::Dark).unwrap();
assert_eq!(dark.colors.appearance(), Appearance::Dark);
assert_eq!(dark.motion, MotionScheme::Expressive);
assert_eq!(dark.layout.density, Density::Compact);
assert_eq!(calls.get(), 2);
assert!(tracker.next_theme(WindowAppearance::VibrantDark).is_none());
assert_eq!(calls.get(), 2);
let light_again = tracker.next_theme(WindowAppearance::VibrantLight).unwrap();
assert_eq!(light_again.colors.appearance(), Appearance::Light);
assert_eq!(calls.get(), 3);
}
#[test]
fn fixed_mode_tracker_builds_once_regardless_of_system_changes() {
for (mode, expected) in [
(AppearanceMode::Light, Appearance::Light),
(AppearanceMode::Dark, Appearance::Dark),
] {
let calls = Cell::new(0usize);
let mut tracker = AppearanceTracker::new(mode, |appearance| {
calls.set(calls.get() + 1);
MaterialTheme::baseline(appearance)
});
let initial = tracker.next_theme(WindowAppearance::Light).unwrap();
assert_eq!(initial.colors.appearance(), expected);
assert_eq!(calls.get(), 1);
assert!(tracker.next_theme(WindowAppearance::Dark).is_none());
assert!(tracker.next_theme(WindowAppearance::VibrantDark).is_none());
assert!(tracker.next_theme(WindowAppearance::VibrantLight).is_none());
assert_eq!(calls.get(), 1);
}
}
}