use super::Context;
use crate::{Id, Interpolate, Style, Theme, Tween, TweenOptions};
impl Context {
pub fn theme(&self) -> Option<&Theme> {
self.theme.as_ref()
}
pub fn set_theme(&mut self, theme: Theme) {
if self.theme.as_ref() == Some(&theme) {
return;
}
let style = theme.resolve();
self.set_style(style);
self.theme = Some(theme);
}
pub fn set_theme_animated(&mut self, theme: Theme, options: TweenOptions) {
if self.theme.as_ref() == Some(&theme) {
return;
}
assert!(
options.repeat == crate::Repeat::Once && !options.auto_reverse,
"theme transition must finish at its target"
);
let from = self.style.clone();
self.set_theme(theme);
let to = self.style.clone();
if to.motion.reduced_motion || from == to {
return;
}
self.restart_animation(
Id::new("theme-palette"),
Tween::with_options(from.interpolate(&to, 0.0), to.clone(), options),
);
self.palette_transition = Some(());
self.sample_theme_palette();
}
pub(super) fn sample_theme_palette(&mut self) {
if self.palette_transition.is_none() {
return;
}
let id = Id::new("theme-palette");
let pass = self.animation_pass(true);
if let Some(value) = self.animations.read::<Style>(id, pass) {
let completed = value.completed();
self.style = value.value;
if completed {
self.palette_transition = None;
self.style_revision = self.style_revision.wrapping_add(1);
self.animations.remove(id);
}
}
}
}