selection.rs 2.0 KB

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