azul_core/animation.rs
1//! Core data structures for configuring and tracking CSS animations
2
3use azul_css::props::{basic::AnimationInterpolationFunction, property::CssProperty};
4
5use crate::task::{Duration as AzDuration, GetSystemTimeCallback, Instant as AzInstant};
6
7/// Specifies which image layer of an element an animation should apply to.
8#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9#[repr(C)]
10pub enum UpdateImageType {
11 /// The animation targets the element's background.
12 Background,
13 /// The animation targets the element's main content.
14 Content,
15}
16
17/// Holds the dynamic, runtime state of an active animation instance.
18/// This is created when an `Animation` begins playing.
19#[derive(Debug, Clone, PartialEq)]
20pub struct AnimationData {
21 /// The starting `CssProperty` value of the animation.
22 pub from: CssProperty,
23 /// The target `CssProperty` value at the end of the animation.
24 pub to: CssProperty,
25 /// The timestamp marking when the animation began.
26 pub start: AzInstant,
27 /// The total time the animation takes to complete one cycle.
28 pub duration: AzDuration,
29 /// The repetition behavior of the animation (e.g., loop, ping-pong).
30 pub repeat: AnimationRepeat,
31 /// The easing function used to control the animation's pacing.
32 pub interpolate: AnimationInterpolationFunction,
33 /// If `true`, a relayout is triggered after the animation finishes.
34 pub relayout_on_finish: bool,
35 /// The width of the parent's bounding rectangle at the animation's start.
36 pub parent_rect_width: f32,
37 /// The height of the parent's bounding rectangle at the animation's start.
38 pub parent_rect_height: f32,
39 /// The width of the animated element's bounding rectangle at the animation's start.
40 pub current_rect_width: f32,
41 /// The height of the animated element's bounding rectangle at the animation's start.
42 pub current_rect_height: f32,
43 /// A callback function used to get the current system time for synchronized timing.
44 pub get_system_time_fn: GetSystemTimeCallback,
45}
46
47/// Defines the static configuration for a CSS animation, parsed from a stylesheet.
48#[derive(Debug, Clone, PartialEq)]
49#[repr(C)]
50pub struct Animation {
51 /// The `CssProperty` at the beginning of the animation (0% keyframe).
52 pub from: CssProperty,
53 /// The `CssProperty` at the end of the animation (100% keyframe).
54 pub to: CssProperty,
55 /// The time it takes for the animation to complete one cycle.
56 pub duration: AzDuration,
57 /// The repetition behavior to apply when a cycle finishes.
58 pub repeat: AnimationRepeat,
59 /// How many times the animation should repeat.
60 pub repeat_times: AnimationRepeatCount,
61 /// The easing function that dictates the animation's rate of change.
62 pub easing: AnimationInterpolationFunction,
63 /// If `true`, a full relayout is performed after the animation concludes.
64 pub relayout_on_finish: bool,
65}
66
67/// Describes the behavior of an animation when it reaches the end of a cycle.
68#[derive(Debug, Copy, Clone, PartialEq)]
69#[repr(C)]
70pub enum AnimationRepeat {
71 /// The animation plays once and then stops.
72 NoRepeat,
73 /// The animation restarts from the beginning after finishing.
74 Loop,
75 /// The animation plays forwards, then backwards, alternating each cycle.
76 PingPong,
77}
78
79/// Specifies how many times an animation cycle should repeat.
80#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Hash)]
81#[repr(C, u8)]
82pub enum AnimationRepeatCount {
83 /// The animation repeats for a specific number of cycles.
84 Times(usize),
85 /// The animation repeats indefinitely.
86 Infinite,
87}