dioxus_html/events/
selection.rs

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