dioxus_html/events/
focus.rs

1use dioxus_core::Event;
2
3pub type FocusEvent = Event<FocusData>;
4
5pub struct FocusData {
6    inner: Box<dyn HasFocusData>,
7}
8
9impl<E: HasFocusData> From<E> for FocusData {
10    fn from(e: E) -> Self {
11        Self { inner: Box::new(e) }
12    }
13}
14
15impl std::fmt::Debug for FocusData {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        f.debug_struct("FocusData").finish()
18    }
19}
20
21impl PartialEq for FocusData {
22    fn eq(&self, _other: &Self) -> bool {
23        true
24    }
25}
26
27impl FocusData {
28    /// Create a new FocusData
29    pub fn new(inner: impl HasFocusData + 'static) -> Self {
30        Self {
31            inner: Box::new(inner),
32        }
33    }
34
35    /// Downcast this event data to a specific type
36    #[inline(always)]
37    pub fn downcast<T: 'static>(&self) -> Option<&T> {
38        self.inner.as_any().downcast_ref::<T>()
39    }
40}
41
42#[cfg(feature = "serialize")]
43/// A serialized version of FocusData
44#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone, Default)]
45pub struct SerializedFocusData {}
46
47#[cfg(feature = "serialize")]
48impl From<&FocusData> for SerializedFocusData {
49    fn from(_: &FocusData) -> Self {
50        Self {}
51    }
52}
53
54#[cfg(feature = "serialize")]
55impl HasFocusData for SerializedFocusData {
56    fn as_any(&self) -> &dyn std::any::Any {
57        self
58    }
59}
60
61#[cfg(feature = "serialize")]
62impl serde::Serialize for FocusData {
63    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
64        SerializedFocusData::from(self).serialize(serializer)
65    }
66}
67
68#[cfg(feature = "serialize")]
69impl<'de> serde::Deserialize<'de> for FocusData {
70    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
71        let data = SerializedFocusData::deserialize(deserializer)?;
72        Ok(Self {
73            inner: Box::new(data),
74        })
75    }
76}
77
78pub trait HasFocusData: std::any::Any {
79    /// return self as Any
80    fn as_any(&self) -> &dyn std::any::Any;
81}
82
83impl_event! [
84    FocusData;
85
86    /// onfocus
87    onfocus
88
89    // onfocusout
90    onfocusout
91
92    // onfocusin
93    onfocusin
94
95    /// onblur
96    onblur
97];