dioxus_html/events/
media.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use dioxus_core::Event;

pub type MediaEvent = Event<MediaData>;
pub struct MediaData {
    inner: Box<dyn HasMediaData>,
}

impl<E: HasMediaData> From<E> for MediaData {
    fn from(e: E) -> Self {
        Self { inner: Box::new(e) }
    }
}

impl std::fmt::Debug for MediaData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MediaData").finish()
    }
}

impl PartialEq for MediaData {
    fn eq(&self, _: &Self) -> bool {
        true
    }
}

impl MediaData {
    /// Create a new MediaData
    pub fn new(inner: impl HasMediaData + 'static) -> Self {
        Self {
            inner: Box::new(inner),
        }
    }

    /// Downcast this event to a concrete event type
    #[inline(always)]
    pub fn downcast<T: 'static>(&self) -> Option<&T> {
        self.inner.as_any().downcast_ref::<T>()
    }
}

#[cfg(feature = "serialize")]
/// A serialized version of MediaData
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
pub struct SerializedMediaData {}

#[cfg(feature = "serialize")]
impl From<&MediaData> for SerializedMediaData {
    fn from(_: &MediaData) -> Self {
        Self {}
    }
}

#[cfg(feature = "serialize")]
impl HasMediaData for SerializedMediaData {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

#[cfg(feature = "serialize")]
impl serde::Serialize for MediaData {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        SerializedMediaData::from(self).serialize(serializer)
    }
}

#[cfg(feature = "serialize")]
impl<'de> serde::Deserialize<'de> for MediaData {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let data = SerializedMediaData::deserialize(deserializer)?;
        Ok(Self {
            inner: Box::new(data),
        })
    }
}

pub trait HasMediaData: std::any::Any {
    /// return self as Any
    fn as_any(&self) -> &dyn std::any::Any;
}

impl_event! [
    MediaData;

    ///abort
    onabort

    ///canplay
    oncanplay

    ///canplaythrough
    oncanplaythrough

    ///durationchange
    ondurationchange

    ///emptied
    onemptied

    ///encrypted
    onencrypted

    ///ended
    onended

    // todo: this conflicts with Media events
    // neither have data, so it's okay
    // ///error
    // onerror

    ///loadeddata
    onloadeddata

    ///loadedmetadata
    onloadedmetadata

    ///loadstart
    onloadstart

    ///pause
    onpause

    ///play
    onplay

    ///playing
    onplaying

    ///progress
    onprogress

    ///ratechange
    onratechange

    ///seeked
    onseeked

    ///seeking
    onseeking

    ///stalled
    onstalled

    ///suspend
    onsuspend

    ///timeupdate
    ontimeupdate

    ///volumechange
    onvolumechange

    ///waiting
    onwaiting
];