dioxus_html/events/
resize.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
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::fmt::{Display, Formatter};

pub struct ResizeData {
    inner: Box<dyn HasResizeData>,
}

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

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

    /// Get the border box size of the observed element
    pub fn get_border_box_size(&self) -> ResizeResult<PixelsSize> {
        self.inner.get_border_box_size()
    }

    /// Get the content box size of the observed element
    pub fn get_content_box_size(&self) -> ResizeResult<PixelsSize> {
        self.inner.get_content_box_size()
    }

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

impl std::fmt::Debug for ResizeData {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ResizeData")
            .field("border_box_size", &self.inner.get_border_box_size())
            .field("content_box_size", &self.inner.get_content_box_size())
            .finish()
    }
}

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

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

#[cfg(feature = "serialize")]
impl SerializedResizeData {
    /// Create a new SerializedResizeData
    pub fn new(border_box_size: PixelsSize, content_box_size: PixelsSize) -> Self {
        Self {
            border_box_size,
            content_box_size,
        }
    }
}

#[cfg(feature = "serialize")]
impl From<&ResizeData> for SerializedResizeData {
    fn from(data: &ResizeData) -> Self {
        Self::new(
            data.get_border_box_size().unwrap(),
            data.get_content_box_size().unwrap(),
        )
    }
}

#[cfg(feature = "serialize")]
impl HasResizeData for SerializedResizeData {
    /// Get the border box size of the observed element
    fn get_border_box_size(&self) -> ResizeResult<PixelsSize> {
        Ok(self.border_box_size)
    }

    /// Get the content box size of the observed element
    fn get_content_box_size(&self) -> ResizeResult<PixelsSize> {
        Ok(self.content_box_size)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

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

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

pub trait HasResizeData: std::any::Any {
    /// Get the border box size of the observed element
    fn get_border_box_size(&self) -> ResizeResult<PixelsSize> {
        Err(ResizeError::NotSupported)
    }
    /// Get the content box size of the observed element
    fn get_content_box_size(&self) -> ResizeResult<PixelsSize> {
        Err(ResizeError::NotSupported)
    }

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

use dioxus_core::Event;

use crate::geometry::PixelsSize;

pub type ResizeEvent = Event<ResizeData>;

impl_event! {
    ResizeData;

    /// onresize
    onresize
}

/// The ResizeResult type for the ResizeData
pub type ResizeResult<T> = Result<T, ResizeError>;

#[derive(Debug)]
/// The error type for the MountedData
#[non_exhaustive]
pub enum ResizeError {
    /// The renderer does not support the requested operation
    NotSupported,
    /// The element was not found
    OperationFailed(Box<dyn std::error::Error>),
}

impl Display for ResizeError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ResizeError::NotSupported => {
                write!(f, "The renderer does not support the requested operation")
            }
            ResizeError::OperationFailed(e) => {
                write!(f, "The operation failed: {}", e)
            }
        }
    }
}

impl std::error::Error for ResizeError {}