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