miracle_plugin/
animation.rs

1use crate::bindings;
2use crate::core::{Rect, mat4_from_f32_array, mat4_to_f32_array};
3use glam::Mat4;
4
5#[doc(hidden)]
6#[repr(C)]
7pub struct RawCustomAnimationData {
8    pub animation_id: u32,
9    pub dt: f32,
10    pub elapsed_seconds: f32,
11}
12
13/// Data passed to animation hook methods describing the current frame.
14pub struct AnimationFrameData {
15    /// Elapsed time since the animation started, in seconds.
16    pub runtime_seconds: f32,
17    /// Total requested duration of the animation, in seconds.
18    pub duration_seconds: f32,
19    /// The starting rectangle of the window (position and size at animation start).
20    pub origin: Rect,
21    /// The target rectangle of the window (position and size at animation end).
22    pub destination: Rect,
23    /// The starting opacity of the window.
24    pub opacity_start: f32,
25    /// The target opacity of the window.
26    pub opacity_end: f32,
27}
28
29impl From<bindings::miracle_plugin_animation_frame_data_t> for AnimationFrameData {
30    fn from(value: bindings::miracle_plugin_animation_frame_data_t) -> Self {
31        Self {
32            runtime_seconds: value.runtime_seconds,
33            duration_seconds: value.duration_seconds,
34            origin: Rect::from_array(value.origin),
35            destination: Rect::from_array(value.destination),
36            opacity_start: value.opacity_start,
37            opacity_end: value.opacity_end,
38        }
39    }
40}
41
42/// Returned from animation hooks to describe the frame's visual state.
43pub struct AnimationFrameResult {
44    /// Set to `true` to signal that the animation is finished.
45    pub completed: bool,
46    /// Override the window's rectangle for this frame. `None` leaves it unchanged.
47    pub area: Option<Rect>,
48    /// Override the window's transform matrix for this frame. `None` leaves it unchanged.
49    pub transform: Option<Mat4>,
50    /// Override the window's opacity for this frame. `None` leaves it unchanged.
51    pub opacity: Option<f32>,
52}
53
54impl From<AnimationFrameResult> for bindings::miracle_plugin_animation_frame_result_t {
55    fn from(value: AnimationFrameResult) -> Self {
56        Self {
57            completed: value.completed as i32,
58            has_area: value.area.is_some() as i32,
59            area: value.area.map(|r| r.to_array()).unwrap_or_default(),
60            has_transform: value.transform.is_some() as i32,
61            transform: value.transform.map(mat4_to_f32_array).unwrap_or_default(),
62            has_opacity: value.opacity.is_some() as i32,
63            opacity: value.opacity.unwrap_or_default(),
64        }
65    }
66}
67
68impl From<bindings::miracle_plugin_animation_frame_result_t> for AnimationFrameResult {
69    fn from(value: bindings::miracle_plugin_animation_frame_result_t) -> Self {
70        Self {
71            completed: value.completed != 0,
72            area: if value.has_area != 0 {
73                Some(Rect::from_array(value.area))
74            } else {
75                None
76            },
77            transform: if value.has_transform != 0 {
78                Some(mat4_from_f32_array(value.transform))
79            } else {
80                None
81            },
82            opacity: if value.has_opacity != 0 {
83                Some(value.opacity)
84            } else {
85                None
86            },
87        }
88    }
89}