#![deny(clippy::unwrap_used)]
#![deny(clippy::panic)]
#![deny(unused_variables)]
#![deny(unused_must_use)]
#![deny(unsafe_code)] #![deny(clippy::unwrap_in_result)] #![deny(rustdoc::broken_intra_doc_links)] #![deny(clippy::modulo_arithmetic)] #![deny(clippy::option_if_let_else)]
#[cfg(feature = "dioxus")]
use animations::core::Animatable;
#[cfg(feature = "dioxus")]
use dioxus::prelude::*;
pub use instant::Duration;
pub mod animations;
pub mod keyframes;
#[cfg(feature = "dioxus")]
pub mod manager;
pub mod motion;
#[allow(dead_code)]
pub(crate) mod pool;
pub mod sequence;
#[cfg(feature = "transitions")]
pub mod transitions;
#[cfg(feature = "transitions")]
pub use dioxus_motion_transitions_macro;
pub use animations::platform::{MotionTime, TimeProvider};
pub use keyframes::{Keyframe, KeyframeAnimation};
#[cfg(feature = "dioxus")]
pub use manager::{AnimationManager, MotionHandle};
#[cfg(test)]
pub(crate) use motion::Motion;
pub mod prelude {
pub use crate::animations::core::{AnimationConfig, AnimationMode, LoopMode};
pub use crate::animations::{
colors::Color, spring::Spring, transform::Transform, tween::Tween,
};
#[cfg(feature = "transitions")]
pub use crate::dioxus_motion_transitions_macro::MotionTransitions;
pub use crate::sequence::AnimationSequence;
#[cfg(feature = "transitions")]
pub use crate::transitions::config::TransitionVariant;
#[cfg(feature = "transitions")]
pub use crate::transitions::page_transitions::TransitionVariantResolver;
#[cfg(feature = "transitions")]
pub use crate::transitions::page_transitions::{AnimatableRoute, AnimatedOutlet};
#[cfg(feature = "dioxus")]
pub use crate::{AnimationManager, MotionHandle, use_motion};
pub use crate::{Duration, Time, TimeProvider};
}
pub type Time = MotionTime;
#[cfg(feature = "dioxus")]
fn calculate_delay(dt: f32, running_frames: u32) -> Duration {
#[cfg(feature = "web")]
{
let _ = running_frames;
match dt {
x if x < 0.008 => Duration::from_millis(8), x if x < 0.016 => Duration::from_millis(16), _ => Duration::from_millis(32), }
}
#[cfg(not(feature = "web"))]
{
if running_frames <= 200 {
Duration::from_micros(8333) } else {
match dt {
x if x < 0.005 => Duration::from_millis(8), x if x < 0.011 => Duration::from_millis(16), _ => Duration::from_millis(33), }
}
}
}
#[cfg(feature = "dioxus")]
pub fn use_motion<T: Animatable + Send + 'static>(initial: T) -> MotionHandle<T> {
let mut state = MotionHandle::new_hook(initial);
#[cfg(feature = "web")]
let idle_poll_rate = Duration::from_millis(100);
#[cfg(not(feature = "web"))]
let idle_poll_rate = Duration::from_millis(33);
use_effect(move || {
spawn(async move {
let mut last_frame = Time::now();
let mut running_frames = 0u32;
loop {
let now = Time::now();
let dt = (now.duration_since(last_frame).as_secs_f32()).min(0.1);
last_frame = now;
if state.is_running() {
running_frames += 1;
let prev_value = state.get_value();
let updated = state.update(dt);
let new_value = state.get_value();
let epsilon = state.epsilon();
if (new_value - prev_value).magnitude() <= epsilon && !updated {
let delay = calculate_delay(dt, running_frames);
Time::delay(delay).await;
continue;
}
let delay = calculate_delay(dt, running_frames);
Time::delay(delay).await;
} else {
running_frames = 0;
Time::delay(idle_poll_rate).await;
}
}
});
});
state
}