drag.rs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
  2. use crate::input_data::{MouseButton, MouseButtonSet};
  3. use crate::prelude::*;
  4. use dioxus_core::Event;
  5. use keyboard_types::Modifiers;
  6. use crate::HasMouseData;
  7. pub type DragEvent = Event<DragData>;
  8. /// The DragEvent interface is a DOM event that represents a drag and drop interaction. The user initiates a drag by
  9. /// placing a pointer device (such as a mouse) on the touch surface and then dragging the pointer to a new location
  10. /// (such as another DOM element). Applications are free to interpret a drag and drop interaction in an
  11. /// application-specific way.
  12. pub struct DragData {
  13. inner: Box<dyn HasDragData>,
  14. }
  15. impl<E: HasDragData + 'static> From<E> for DragData {
  16. fn from(e: E) -> Self {
  17. Self { inner: Box::new(e) }
  18. }
  19. }
  20. impl std::fmt::Debug for DragData {
  21. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  22. f.debug_struct("DragData")
  23. .field("coordinates", &self.coordinates())
  24. .field("modifiers", &self.modifiers())
  25. .field("held_buttons", &self.held_buttons())
  26. .field("trigger_button", &self.trigger_button())
  27. .finish()
  28. }
  29. }
  30. impl PartialEq for DragData {
  31. fn eq(&self, other: &Self) -> bool {
  32. self.coordinates() == other.coordinates()
  33. && self.modifiers() == other.modifiers()
  34. && self.held_buttons() == other.held_buttons()
  35. && self.trigger_button() == other.trigger_button()
  36. }
  37. }
  38. impl DragData {
  39. /// Create a new DragData
  40. pub fn new(inner: impl HasDragData + 'static) -> Self {
  41. Self {
  42. inner: Box::new(inner),
  43. }
  44. }
  45. /// Downcast this event data to a specific type
  46. #[inline(always)]
  47. pub fn downcast<T: 'static>(&self) -> Option<&T> {
  48. HasDragData::as_any(&*self.inner).downcast_ref::<T>()
  49. }
  50. }
  51. impl crate::HasFileData for DragData {
  52. fn files(&self) -> Option<std::sync::Arc<dyn crate::file_data::FileEngine>> {
  53. self.inner.files()
  54. }
  55. }
  56. impl InteractionLocation for DragData {
  57. fn client_coordinates(&self) -> ClientPoint {
  58. self.inner.client_coordinates()
  59. }
  60. fn page_coordinates(&self) -> PagePoint {
  61. self.inner.page_coordinates()
  62. }
  63. fn screen_coordinates(&self) -> ScreenPoint {
  64. self.inner.screen_coordinates()
  65. }
  66. }
  67. impl InteractionElementOffset for DragData {
  68. fn element_coordinates(&self) -> ElementPoint {
  69. self.inner.element_coordinates()
  70. }
  71. fn coordinates(&self) -> Coordinates {
  72. self.inner.coordinates()
  73. }
  74. }
  75. impl ModifiersInteraction for DragData {
  76. fn modifiers(&self) -> Modifiers {
  77. self.inner.modifiers()
  78. }
  79. }
  80. impl PointerInteraction for DragData {
  81. fn held_buttons(&self) -> MouseButtonSet {
  82. self.inner.held_buttons()
  83. }
  84. // todo the following is kind of bad; should we just return None when the trigger_button is unreliable (and frankly irrelevant)? i guess we would need the event_type here
  85. fn trigger_button(&self) -> Option<MouseButton> {
  86. self.inner.trigger_button()
  87. }
  88. }
  89. #[cfg(feature = "serialize")]
  90. /// A serialized version of DragData
  91. #[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
  92. pub struct SerializedDragData {
  93. pub mouse: crate::point_interaction::SerializedPointInteraction,
  94. #[serde(default)]
  95. files: Option<crate::file_data::SerializedFileEngine>,
  96. }
  97. #[cfg(feature = "serialize")]
  98. impl SerializedDragData {
  99. fn new(drag: &DragData) -> Self {
  100. Self {
  101. mouse: crate::point_interaction::SerializedPointInteraction::from(drag),
  102. files: None,
  103. }
  104. }
  105. }
  106. #[cfg(feature = "serialize")]
  107. impl HasDragData for SerializedDragData {
  108. fn as_any(&self) -> &dyn std::any::Any {
  109. self
  110. }
  111. }
  112. #[cfg(feature = "serialize")]
  113. impl crate::file_data::HasFileData for SerializedDragData {
  114. fn files(&self) -> Option<std::sync::Arc<dyn crate::file_data::FileEngine>> {
  115. self.files
  116. .as_ref()
  117. .map(|files| std::sync::Arc::new(files.clone()) as _)
  118. }
  119. }
  120. #[cfg(feature = "serialize")]
  121. impl HasMouseData for SerializedDragData {
  122. fn as_any(&self) -> &dyn std::any::Any {
  123. self
  124. }
  125. }
  126. #[cfg(feature = "serialize")]
  127. impl InteractionLocation for SerializedDragData {
  128. fn client_coordinates(&self) -> ClientPoint {
  129. self.mouse.client_coordinates()
  130. }
  131. fn page_coordinates(&self) -> PagePoint {
  132. self.mouse.page_coordinates()
  133. }
  134. fn screen_coordinates(&self) -> ScreenPoint {
  135. self.mouse.screen_coordinates()
  136. }
  137. }
  138. #[cfg(feature = "serialize")]
  139. impl InteractionElementOffset for SerializedDragData {
  140. fn element_coordinates(&self) -> ElementPoint {
  141. self.mouse.element_coordinates()
  142. }
  143. fn coordinates(&self) -> Coordinates {
  144. self.mouse.coordinates()
  145. }
  146. }
  147. #[cfg(feature = "serialize")]
  148. impl ModifiersInteraction for SerializedDragData {
  149. fn modifiers(&self) -> Modifiers {
  150. self.mouse.modifiers()
  151. }
  152. }
  153. #[cfg(feature = "serialize")]
  154. impl PointerInteraction for SerializedDragData {
  155. fn held_buttons(&self) -> MouseButtonSet {
  156. self.mouse.held_buttons()
  157. }
  158. fn trigger_button(&self) -> Option<MouseButton> {
  159. self.mouse.trigger_button()
  160. }
  161. }
  162. #[cfg(feature = "serialize")]
  163. impl serde::Serialize for DragData {
  164. fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
  165. SerializedDragData::new(self).serialize(serializer)
  166. }
  167. }
  168. #[cfg(feature = "serialize")]
  169. impl<'de> serde::Deserialize<'de> for DragData {
  170. fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
  171. let data = SerializedDragData::deserialize(deserializer)?;
  172. Ok(Self {
  173. inner: Box::new(data),
  174. })
  175. }
  176. }
  177. /// A trait for any object that has the data for a drag event
  178. pub trait HasDragData: HasMouseData + crate::HasFileData {
  179. /// return self as Any
  180. fn as_any(&self) -> &dyn std::any::Any;
  181. }
  182. impl_event! {
  183. DragData;
  184. /// ondrag
  185. ondrag
  186. /// ondragend
  187. ondragend
  188. /// ondragenter
  189. ondragenter
  190. /// ondragexit
  191. ondragexit
  192. /// ondragleave
  193. ondragleave
  194. /// ondragover
  195. ondragover
  196. /// ondragstart
  197. ondragstart
  198. /// ondrop
  199. ondrop
  200. }