dioxus_html/events/
image.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
use dioxus_core::Event;

pub type ImageEvent = Event<ImageData>;
pub struct ImageData {
    inner: Box<dyn HasImageData>,
}

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

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

impl PartialEq for ImageData {
    fn eq(&self, other: &Self) -> bool {
        self.load_error() == other.load_error()
    }
}

impl ImageData {
    /// Create a new ImageData
    pub fn new(e: impl HasImageData) -> Self {
        Self { inner: Box::new(e) }
    }

    /// If the renderer encountered an error while loading the image
    pub fn load_error(&self) -> bool {
        self.inner.load_error()
    }

    pub fn downcast<T: 'static>(&self) -> Option<&T> {
        self.inner.as_any().downcast_ref::<T>()
    }
}

#[cfg(feature = "serialize")]
/// A serialized version of ImageData
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
pub struct SerializedImageData {
    #[cfg_attr(feature = "serialize", serde(default))]
    load_error: bool,
}

#[cfg(feature = "serialize")]
impl From<&ImageData> for SerializedImageData {
    fn from(data: &ImageData) -> Self {
        Self {
            load_error: data.load_error(),
        }
    }
}

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

    fn load_error(&self) -> bool {
        self.load_error
    }
}

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

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

/// A trait for any object that has the data for an image event
pub trait HasImageData: std::any::Any {
    /// If the renderer encountered an error while loading the image
    fn load_error(&self) -> bool;

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

impl_event! [
    ImageData;

    /// onerror
    onerror

    /// onload
    onload
];