dioxus_html/events/
animation.rs1use dioxus_core::Event;
2
3pub type AnimationEvent = Event<AnimationData>;
4
5pub struct AnimationData {
6 inner: Box<dyn HasAnimationData>,
7}
8
9impl AnimationData {
10 pub fn new(inner: impl HasAnimationData + 'static) -> Self {
12 Self {
13 inner: Box::new(inner),
14 }
15 }
16
17 pub fn animation_name(&self) -> String {
19 self.inner.animation_name()
20 }
21
22 pub fn pseudo_element(&self) -> String {
24 self.inner.pseudo_element()
25 }
26
27 pub fn elapsed_time(&self) -> f32 {
29 self.inner.elapsed_time()
30 }
31
32 #[inline(always)]
34 pub fn downcast<T: 'static>(&self) -> Option<&T> {
35 self.inner.as_ref().as_any().downcast_ref::<T>()
36 }
37}
38
39impl<E: HasAnimationData> From<E> for AnimationData {
40 fn from(e: E) -> Self {
41 Self { inner: Box::new(e) }
42 }
43}
44
45impl std::fmt::Debug for AnimationData {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 f.debug_struct("AnimationData")
48 .field("animation_name", &self.animation_name())
49 .field("pseudo_element", &self.pseudo_element())
50 .field("elapsed_time", &self.elapsed_time())
51 .finish()
52 }
53}
54
55impl PartialEq for AnimationData {
56 fn eq(&self, other: &Self) -> bool {
57 self.animation_name() == other.animation_name()
58 && self.pseudo_element() == other.pseudo_element()
59 && self.elapsed_time() == other.elapsed_time()
60 }
61}
62
63#[cfg(feature = "serialize")]
64#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
66pub struct SerializedAnimationData {
67 animation_name: String,
68 pseudo_element: String,
69 elapsed_time: f32,
70}
71
72#[cfg(feature = "serialize")]
73impl From<&AnimationData> for SerializedAnimationData {
74 fn from(data: &AnimationData) -> Self {
75 Self {
76 animation_name: data.animation_name(),
77 pseudo_element: data.pseudo_element(),
78 elapsed_time: data.elapsed_time(),
79 }
80 }
81}
82
83#[cfg(feature = "serialize")]
84impl HasAnimationData for SerializedAnimationData {
85 fn animation_name(&self) -> String {
86 self.animation_name.clone()
87 }
88
89 fn pseudo_element(&self) -> String {
90 self.pseudo_element.clone()
91 }
92
93 fn elapsed_time(&self) -> f32 {
94 self.elapsed_time
95 }
96
97 fn as_any(&self) -> &dyn std::any::Any {
98 self
99 }
100}
101
102#[cfg(feature = "serialize")]
103impl serde::Serialize for AnimationData {
104 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
105 SerializedAnimationData::from(self).serialize(serializer)
106 }
107}
108
109#[cfg(feature = "serialize")]
110impl<'de> serde::Deserialize<'de> for AnimationData {
111 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
112 let data = SerializedAnimationData::deserialize(deserializer)?;
113 Ok(Self {
114 inner: Box::new(data),
115 })
116 }
117}
118
119pub trait HasAnimationData: std::any::Any {
121 fn animation_name(&self) -> String;
123
124 fn pseudo_element(&self) -> String;
126
127 fn elapsed_time(&self) -> f32;
129
130 fn as_any(&self) -> &dyn std::any::Any;
132}
133
134impl_event! [
135 AnimationData;
136
137 onanimationstart
139
140 onanimationend
142
143 onanimationiteration
145];