use tairitsu_vdom::Platform;
use super::super::context::AnimationContext;
use super::super::state::AnimationDataStore as StructAnimationState;
pub type AnimationCallback<P> = dyn Fn(&AnimationContext<P>) -> String + 'static;
pub type StatefulCallback<P> =
dyn Fn(&AnimationContext<P>, &mut StructAnimationState) -> String + 'static;
pub type VoidCallback<P> = dyn Fn(&AnimationContext<P>) + 'static;
pub enum DynamicValue<P: Platform> {
Static(String),
Dynamic(Box<AnimationCallback<P>>),
StatefulDynamic(Box<StatefulCallback<P>>),
}
impl<P: Platform> DynamicValue<P> {
pub fn static_value(value: impl Into<String>) -> Self {
Self::Static(value.into())
}
pub fn dynamic<F>(f: F) -> Self
where
F: Fn(&AnimationContext<P>) -> String + 'static,
{
Self::Dynamic(Box::new(f))
}
pub fn stateful_dynamic<F>(f: F) -> Self
where
F: Fn(&AnimationContext<P>, &mut StructAnimationState) -> String + 'static,
{
Self::StatefulDynamic(Box::new(f))
}
pub fn evaluate(&self, ctx: &AnimationContext<P>, state: &mut StructAnimationState) -> String {
match self {
DynamicValue::Static(s) => s.clone(),
DynamicValue::Dynamic(f) => f(ctx),
DynamicValue::StatefulDynamic(f) => f(ctx, state),
}
}
}
impl<P: Platform> From<String> for DynamicValue<P> {
fn from(s: String) -> Self {
Self::Static(s)
}
}
impl<P: Platform> From<&str> for DynamicValue<P> {
fn from(s: &str) -> Self {
Self::Static(s.to_string())
}
}