dioxus_html/events/
resize.rs

1use std::fmt::{Display, Formatter};
2
3pub struct ResizeData {
4    inner: Box<dyn HasResizeData>,
5}
6
7impl<E: HasResizeData> From<E> for ResizeData {
8    fn from(e: E) -> Self {
9        Self { inner: Box::new(e) }
10    }
11}
12
13impl ResizeData {
14    /// Create a new ResizeData
15    pub fn new(inner: impl HasResizeData + 'static) -> Self {
16        Self {
17            inner: Box::new(inner),
18        }
19    }
20
21    /// Get the border box size of the observed element
22    pub fn get_border_box_size(&self) -> ResizeResult<PixelsSize> {
23        self.inner.get_border_box_size()
24    }
25
26    /// Get the content box size of the observed element
27    pub fn get_content_box_size(&self) -> ResizeResult<PixelsSize> {
28        self.inner.get_content_box_size()
29    }
30
31    /// Downcast this event to a concrete event type
32    pub fn downcast<T: 'static>(&self) -> Option<&T> {
33        self.inner.as_any().downcast_ref::<T>()
34    }
35}
36
37impl std::fmt::Debug for ResizeData {
38    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39        f.debug_struct("ResizeData")
40            .field("border_box_size", &self.inner.get_border_box_size())
41            .field("content_box_size", &self.inner.get_content_box_size())
42            .finish()
43    }
44}
45
46impl PartialEq for ResizeData {
47    fn eq(&self, _: &Self) -> bool {
48        true
49    }
50}
51
52#[cfg(feature = "serialize")]
53/// A serialized version of ResizeData
54#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
55pub struct SerializedResizeData {
56    pub border_box_size: PixelsSize,
57    pub content_box_size: PixelsSize,
58}
59
60#[cfg(feature = "serialize")]
61impl SerializedResizeData {
62    /// Create a new SerializedResizeData
63    pub fn new(border_box_size: PixelsSize, content_box_size: PixelsSize) -> Self {
64        Self {
65            border_box_size,
66            content_box_size,
67        }
68    }
69}
70
71#[cfg(feature = "serialize")]
72impl From<&ResizeData> for SerializedResizeData {
73    fn from(data: &ResizeData) -> Self {
74        Self::new(
75            data.get_border_box_size().unwrap(),
76            data.get_content_box_size().unwrap(),
77        )
78    }
79}
80
81#[cfg(feature = "serialize")]
82impl HasResizeData for SerializedResizeData {
83    /// Get the border box size of the observed element
84    fn get_border_box_size(&self) -> ResizeResult<PixelsSize> {
85        Ok(self.border_box_size)
86    }
87
88    /// Get the content box size of the observed element
89    fn get_content_box_size(&self) -> ResizeResult<PixelsSize> {
90        Ok(self.content_box_size)
91    }
92
93    fn as_any(&self) -> &dyn std::any::Any {
94        self
95    }
96}
97
98#[cfg(feature = "serialize")]
99impl serde::Serialize for ResizeData {
100    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
101        SerializedResizeData::from(self).serialize(serializer)
102    }
103}
104
105#[cfg(feature = "serialize")]
106impl<'de> serde::Deserialize<'de> for ResizeData {
107    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
108        let data = SerializedResizeData::deserialize(deserializer)?;
109        Ok(Self {
110            inner: Box::new(data),
111        })
112    }
113}
114
115pub trait HasResizeData: std::any::Any {
116    /// Get the border box size of the observed element
117    fn get_border_box_size(&self) -> ResizeResult<PixelsSize> {
118        Err(ResizeError::NotSupported)
119    }
120    /// Get the content box size of the observed element
121    fn get_content_box_size(&self) -> ResizeResult<PixelsSize> {
122        Err(ResizeError::NotSupported)
123    }
124
125    /// return self as Any
126    fn as_any(&self) -> &dyn std::any::Any;
127}
128
129use dioxus_core::Event;
130
131use crate::geometry::PixelsSize;
132
133pub type ResizeEvent = Event<ResizeData>;
134
135impl_event! {
136    ResizeData;
137
138    /// onresize
139    onresize
140}
141
142/// The ResizeResult type for the ResizeData
143pub type ResizeResult<T> = Result<T, ResizeError>;
144
145#[derive(Debug)]
146/// The error type for the MountedData
147#[non_exhaustive]
148pub enum ResizeError {
149    /// The renderer does not support the requested operation
150    NotSupported,
151    /// The element was not found
152    OperationFailed(Box<dyn std::error::Error>),
153}
154
155impl Display for ResizeError {
156    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
157        match self {
158            ResizeError::NotSupported => {
159                write!(f, "The renderer does not support the requested operation")
160            }
161            ResizeError::OperationFailed(e) => {
162                write!(f, "The operation failed: {}", e)
163            }
164        }
165    }
166}
167
168impl std::error::Error for ResizeError {}