use crate::color::Rgba;
use crate::theme::{Theme, ThemeId, ThemeMode, effective_id};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ThemeState {
requested: ThemeId,
follow_system: bool,
effective: ThemeId,
reduced_motion: bool,
accent: Option<Rgba>,
custom: Option<Theme>,
}
impl ThemeState {
pub fn new(requested: ThemeId, follow_system: bool) -> Self {
Self {
requested,
follow_system,
effective: requested,
reduced_motion: false,
accent: None,
custom: None,
}
}
pub fn with_reduced_motion(requested: ThemeId, follow_system: bool, reduced_motion: bool) -> Self {
Self {
reduced_motion,
..Self::new(requested, follow_system)
}
}
pub fn from_settings(requested: &str, follow_system: bool, reduced_motion: bool) -> Self {
Self::with_reduced_motion(ThemeId::from_setting(requested), follow_system, reduced_motion)
}
pub fn requested(&self) -> ThemeId {
self.requested
}
pub fn effective(&self) -> ThemeId {
self.effective
}
pub fn follow_system(&self) -> bool {
self.follow_system
}
pub fn reduced_motion(&self) -> bool {
self.reduced_motion
}
pub fn accent(&self) -> Option<Rgba> {
self.accent
}
pub fn custom(&self) -> Option<Theme> {
self.custom
}
pub fn tokens(&self) -> Theme {
self.custom.unwrap_or_else(|| {
let base = Theme::for_id(self.effective);
self.accent.map_or(base, |accent| base.with_accent(accent))
})
}
pub fn is_light(&self) -> bool {
match self.custom {
Some(custom) => !custom.is_dark(),
None => self.effective.mode() == ThemeMode::Light,
}
}
pub fn is_dark(&self) -> bool {
!self.is_light()
}
pub fn set_accent(&mut self, accent: Option<Rgba>) -> bool {
if accent == self.accent {
return false;
}
self.accent = accent;
true
}
pub fn set_custom(&mut self, custom: Option<Theme>) -> bool {
if custom == self.custom {
return false;
}
self.custom = custom;
true
}
pub fn set_requested(&mut self, requested: ThemeId) -> bool {
if requested == self.requested {
return false;
}
self.requested = requested;
true
}
pub fn set_follow_system(&mut self, follow_system: bool) -> bool {
if follow_system == self.follow_system {
return false;
}
self.follow_system = follow_system;
true
}
pub fn set_reduced_motion(&mut self, reduced_motion: bool) -> bool {
if reduced_motion == self.reduced_motion {
return false;
}
self.reduced_motion = reduced_motion;
true
}
pub fn resolve(&mut self, system_dark: bool) -> (ThemeId, bool) {
let previous = self.effective;
if self.custom.is_none() {
self.effective = effective_id(self.requested, self.follow_system, system_dark);
}
(self.effective, self.effective != previous)
}
pub fn sync_system(&mut self, system_dark: bool) -> bool {
if !self.follow_system || self.custom.is_some() {
return false;
}
let (_, changed) = self.resolve(system_dark);
changed
}
pub fn theme_setting(&self) -> &'static str {
self.requested.as_setting()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_and_settings_round_trip() {
let state = ThemeState::default();
assert_eq!(state.requested(), ThemeId::GithubLight);
assert!(state.is_light());
let state = ThemeState::from_settings("dracula-soft", false, true);
assert_eq!(state.requested(), ThemeId::DraculaSoft);
assert!(state.is_dark());
assert!(state.reduced_motion());
assert_eq!(state.theme_setting(), "dracula-soft");
}
#[test]
fn follow_system_resolves_and_only_reports_changes() {
let mut state = ThemeState::new(ThemeId::GithubDark, true);
assert!(state.sync_system(false));
assert_eq!(state.effective(), ThemeId::GithubLight);
assert!(!state.sync_system(false));
state.set_custom(Some(Theme::dark()));
assert!(!state.sync_system(true));
assert!(state.is_dark());
}
#[test]
fn accent_and_custom_tokens_are_pure() {
let mut state = ThemeState::new(ThemeId::GithubLight, false);
assert!(state.set_accent(Some(Rgba::from_rgb(0xe0, 0x6c, 0x2b))));
assert!(!state.set_accent(Some(Rgba::from_rgb(0xe0, 0x6c, 0x2b))));
assert!(state.tokens().accessibility_issues().is_empty());
assert!(state.set_custom(Some(Theme::dark())));
assert!(state.is_dark());
assert_eq!(state.custom(), Some(Theme::dark()));
assert!(state.set_custom(None));
assert!(!state.set_custom(None));
}
}