dioxus_html/events/
media.rs1use dioxus_core::Event;
2
3pub type MediaEvent = Event<MediaData>;
4pub struct MediaData {
5 inner: Box<dyn HasMediaData>,
6}
7
8impl<E: HasMediaData> From<E> for MediaData {
9 fn from(e: E) -> Self {
10 Self { inner: Box::new(e) }
11 }
12}
13
14impl std::fmt::Debug for MediaData {
15 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16 f.debug_struct("MediaData").finish()
17 }
18}
19
20impl PartialEq for MediaData {
21 fn eq(&self, _: &Self) -> bool {
22 true
23 }
24}
25
26impl MediaData {
27 pub fn new(inner: impl HasMediaData + 'static) -> Self {
29 Self {
30 inner: Box::new(inner),
31 }
32 }
33
34 #[inline(always)]
36 pub fn downcast<T: 'static>(&self) -> Option<&T> {
37 self.inner.as_any().downcast_ref::<T>()
38 }
39}
40
41#[cfg(feature = "serialize")]
42#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
44pub struct SerializedMediaData {}
45
46#[cfg(feature = "serialize")]
47impl From<&MediaData> for SerializedMediaData {
48 fn from(_: &MediaData) -> Self {
49 Self {}
50 }
51}
52
53#[cfg(feature = "serialize")]
54impl HasMediaData for SerializedMediaData {
55 fn as_any(&self) -> &dyn std::any::Any {
56 self
57 }
58}
59
60#[cfg(feature = "serialize")]
61impl serde::Serialize for MediaData {
62 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
63 SerializedMediaData::from(self).serialize(serializer)
64 }
65}
66
67#[cfg(feature = "serialize")]
68impl<'de> serde::Deserialize<'de> for MediaData {
69 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
70 let data = SerializedMediaData::deserialize(deserializer)?;
71 Ok(Self {
72 inner: Box::new(data),
73 })
74 }
75}
76
77pub trait HasMediaData: std::any::Any {
78 fn as_any(&self) -> &dyn std::any::Any;
80}
81
82impl_event! [
83 MediaData;
84
85 onabort
87
88 oncanplay
90
91 oncanplaythrough
93
94 ondurationchange
96
97 onemptied
99
100 onencrypted
102
103 onended
105
106 onloadeddata
113
114 onloadedmetadata
116
117 onloadstart
119
120 onpause
122
123 onplay
125
126 onplaying
128
129 onprogress
131
132 onratechange
134
135 onseeked
137
138 onseeking
140
141 onstalled
143
144 onsuspend
146
147 ontimeupdate
149
150 onvolumechange
152
153 onwaiting
155];