events.rs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. use blitz_traits::{BlitzKeyEvent, BlitzMouseButtonEvent, MouseEventButton};
  2. use dioxus_html::{
  3. geometry::{ClientPoint, ElementPoint, PagePoint, ScreenPoint},
  4. input_data::{MouseButton, MouseButtonSet},
  5. point_interaction::{
  6. InteractionElementOffset, InteractionLocation, ModifiersInteraction, PointerInteraction,
  7. },
  8. AnimationData, ClipboardData, CompositionData, DragData, FocusData, FormData, FormValue,
  9. HasFileData, HasFormData, HasMouseData, HtmlEventConverter, ImageData, KeyboardData, MediaData,
  10. MountedData, MouseData, PlatformEventData, PointerData, ResizeData, ScrollData, SelectionData,
  11. ToggleData, TouchData, TransitionData, VisibleData, WheelData,
  12. };
  13. use dioxus_html::{HasFocusData, HasKeyboardData};
  14. use keyboard_types::{Code, Key, Location, Modifiers};
  15. use std::any::Any;
  16. use std::collections::HashMap;
  17. pub struct NativeConverter {}
  18. impl HtmlEventConverter for NativeConverter {
  19. fn convert_form_data(&self, event: &PlatformEventData) -> FormData {
  20. event.downcast::<NativeFormData>().unwrap().clone().into()
  21. }
  22. fn convert_mouse_data(&self, event: &PlatformEventData) -> MouseData {
  23. event.downcast::<NativeClickData>().unwrap().clone().into()
  24. }
  25. fn convert_keyboard_data(&self, event: &PlatformEventData) -> KeyboardData {
  26. event
  27. .downcast::<BlitzKeyboardData>()
  28. .unwrap()
  29. .clone()
  30. .into()
  31. }
  32. fn convert_focus_data(&self, _event: &PlatformEventData) -> FocusData {
  33. NativeFocusData {}.into()
  34. }
  35. fn convert_animation_data(&self, _event: &PlatformEventData) -> AnimationData {
  36. todo!("todo: convert_animation_data in dioxus-native. requires support in blitz")
  37. }
  38. fn convert_clipboard_data(&self, _event: &PlatformEventData) -> ClipboardData {
  39. todo!("todo: convert_clipboard_data in dioxus-native. requires support in blitz")
  40. }
  41. fn convert_composition_data(&self, _event: &PlatformEventData) -> CompositionData {
  42. todo!("todo: convert_composition_data in dioxus-native. requires support in blitz")
  43. }
  44. fn convert_drag_data(&self, _event: &PlatformEventData) -> DragData {
  45. todo!("todo: convert_drag_data in dioxus-native. requires support in blitz")
  46. }
  47. fn convert_image_data(&self, _event: &PlatformEventData) -> ImageData {
  48. todo!("todo: convert_image_data in dioxus-native. requires support in blitz")
  49. }
  50. fn convert_media_data(&self, _event: &PlatformEventData) -> MediaData {
  51. todo!("todo: convert_media_data in dioxus-native. requires support in blitz")
  52. }
  53. fn convert_mounted_data(&self, _event: &PlatformEventData) -> MountedData {
  54. todo!("todo: convert_mounted_data in dioxus-native. requires support in blitz")
  55. }
  56. fn convert_pointer_data(&self, _event: &PlatformEventData) -> PointerData {
  57. todo!("todo: convert_pointer_data in dioxus-native. requires support in blitz")
  58. }
  59. fn convert_scroll_data(&self, _event: &PlatformEventData) -> ScrollData {
  60. todo!("todo: convert_scroll_data in dioxus-native. requires support in blitz")
  61. }
  62. fn convert_selection_data(&self, _event: &PlatformEventData) -> SelectionData {
  63. todo!("todo: convert_selection_data in dioxus-native. requires support in blitz")
  64. }
  65. fn convert_toggle_data(&self, _event: &PlatformEventData) -> ToggleData {
  66. todo!("todo: convert_toggle_data in dioxus-native. requires support in blitz")
  67. }
  68. fn convert_touch_data(&self, _event: &PlatformEventData) -> TouchData {
  69. todo!("todo: convert_touch_data in dioxus-native. requires support in blitz")
  70. }
  71. fn convert_transition_data(&self, _event: &PlatformEventData) -> TransitionData {
  72. todo!("todo: convert_transition_data in dioxus-native. requires support in blitz")
  73. }
  74. fn convert_wheel_data(&self, _event: &PlatformEventData) -> WheelData {
  75. todo!("todo: convert_wheel_data in dioxus-native. requires support in blitz")
  76. }
  77. fn convert_resize_data(&self, _event: &PlatformEventData) -> ResizeData {
  78. todo!("todo: convert_resize_data in dioxus-native. requires support in blitz")
  79. }
  80. fn convert_visible_data(&self, _event: &PlatformEventData) -> VisibleData {
  81. todo!("todo: convert_visible_data in dioxus-native. requires support in blitz")
  82. }
  83. }
  84. #[derive(Clone, Debug)]
  85. pub struct NativeFormData {
  86. pub value: String,
  87. pub values: HashMap<String, FormValue>,
  88. }
  89. impl HasFormData for NativeFormData {
  90. fn as_any(&self) -> &dyn std::any::Any {
  91. self as &dyn std::any::Any
  92. }
  93. fn value(&self) -> String {
  94. self.value.clone()
  95. }
  96. fn values(&self) -> HashMap<String, FormValue> {
  97. self.values.clone()
  98. }
  99. }
  100. impl HasFileData for NativeFormData {}
  101. #[derive(Clone, Debug)]
  102. pub(crate) struct BlitzKeyboardData(pub(crate) BlitzKeyEvent);
  103. impl ModifiersInteraction for BlitzKeyboardData {
  104. fn modifiers(&self) -> Modifiers {
  105. self.0.modifiers
  106. }
  107. }
  108. impl HasKeyboardData for BlitzKeyboardData {
  109. fn key(&self) -> Key {
  110. self.0.key.clone()
  111. }
  112. fn code(&self) -> Code {
  113. self.0.code
  114. }
  115. fn location(&self) -> Location {
  116. self.0.location
  117. }
  118. fn is_auto_repeating(&self) -> bool {
  119. self.0.is_auto_repeating
  120. }
  121. fn is_composing(&self) -> bool {
  122. self.0.is_composing
  123. }
  124. fn as_any(&self) -> &dyn std::any::Any {
  125. self as &dyn Any
  126. }
  127. }
  128. #[derive(Clone)]
  129. pub struct NativeClickData(pub(crate) BlitzMouseButtonEvent);
  130. impl InteractionLocation for NativeClickData {
  131. fn client_coordinates(&self) -> ClientPoint {
  132. ClientPoint::new(self.0.x as _, self.0.y as _)
  133. }
  134. // these require blitz to pass them along, or a dom rect
  135. fn screen_coordinates(&self) -> ScreenPoint {
  136. unimplemented!()
  137. }
  138. fn page_coordinates(&self) -> PagePoint {
  139. unimplemented!()
  140. }
  141. }
  142. impl InteractionElementOffset for NativeClickData {
  143. fn element_coordinates(&self) -> ElementPoint {
  144. todo!()
  145. }
  146. }
  147. impl ModifiersInteraction for NativeClickData {
  148. fn modifiers(&self) -> Modifiers {
  149. self.0.mods
  150. }
  151. }
  152. impl PointerInteraction for NativeClickData {
  153. fn trigger_button(&self) -> Option<MouseButton> {
  154. Some(match self.0.button {
  155. MouseEventButton::Main => MouseButton::Primary,
  156. MouseEventButton::Auxiliary => MouseButton::Auxiliary,
  157. MouseEventButton::Secondary => MouseButton::Secondary,
  158. MouseEventButton::Fourth => MouseButton::Fourth,
  159. MouseEventButton::Fifth => MouseButton::Fifth,
  160. })
  161. }
  162. fn held_buttons(&self) -> MouseButtonSet {
  163. dioxus_html::input_data::decode_mouse_button_set(self.0.buttons.bits() as u16)
  164. }
  165. }
  166. impl HasMouseData for NativeClickData {
  167. fn as_any(&self) -> &dyn std::any::Any {
  168. self as &dyn std::any::Any
  169. }
  170. }
  171. #[derive(Clone)]
  172. pub struct NativeFocusData {}
  173. impl HasFocusData for NativeFocusData {
  174. fn as_any(&self) -> &dyn std::any::Any {
  175. self as &dyn std::any::Any
  176. }
  177. }