events.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
  2. use crate::input_data::{decode_mouse_button_set, MouseButton};
  3. use crate::on::{
  4. AnimationData, CompositionData, KeyboardData, MouseData, PointerData, TouchData,
  5. TransitionData, WheelData,
  6. };
  7. use crate::KeyCode;
  8. use keyboard_types::Modifiers;
  9. use wasm_bindgen::JsCast;
  10. use web_sys::{
  11. AnimationEvent, CompositionEvent, Event, KeyboardEvent, MouseEvent, PointerEvent, TouchEvent,
  12. TransitionEvent, WheelEvent,
  13. };
  14. macro_rules! uncheck_convert {
  15. ($t:ty, $d:ty) => {
  16. impl From<Event> for $d {
  17. #[inline]
  18. fn from(e: Event) -> Self {
  19. let e: $t = e.unchecked_into();
  20. Self::from(&e)
  21. }
  22. }
  23. impl From<&Event> for $d {
  24. #[inline]
  25. fn from(e: &Event) -> Self {
  26. let e: &$t = e.unchecked_ref();
  27. Self::from(e)
  28. }
  29. }
  30. };
  31. ($($t:ty => $d:ty),+ $(,)?) => {
  32. $(uncheck_convert!($t, $d);)+
  33. };
  34. }
  35. uncheck_convert![
  36. CompositionEvent => CompositionData,
  37. KeyboardEvent => KeyboardData,
  38. MouseEvent => MouseData,
  39. TouchEvent => TouchData,
  40. PointerEvent => PointerData,
  41. WheelEvent => WheelData,
  42. AnimationEvent => AnimationData,
  43. TransitionEvent => TransitionData,
  44. ];
  45. impl From<&CompositionEvent> for CompositionData {
  46. fn from(e: &CompositionEvent) -> Self {
  47. Self {
  48. data: e.data().unwrap_or_default(),
  49. }
  50. }
  51. }
  52. impl From<&KeyboardEvent> for KeyboardData {
  53. fn from(e: &KeyboardEvent) -> Self {
  54. Self {
  55. alt_key: e.alt_key(),
  56. char_code: e.char_code(),
  57. key: e.key(),
  58. key_code: KeyCode::from_raw_code(e.key_code() as u8),
  59. ctrl_key: e.ctrl_key(),
  60. locale: "not implemented".to_string(),
  61. location: e.location() as usize,
  62. meta_key: e.meta_key(),
  63. repeat: e.repeat(),
  64. shift_key: e.shift_key(),
  65. which: e.which() as usize,
  66. }
  67. }
  68. }
  69. impl From<&MouseEvent> for MouseData {
  70. fn from(e: &MouseEvent) -> Self {
  71. let mut modifiers = Modifiers::empty();
  72. if e.alt_key() {
  73. modifiers.insert(Modifiers::ALT);
  74. }
  75. if e.ctrl_key() {
  76. modifiers.insert(Modifiers::CONTROL);
  77. }
  78. if e.meta_key() {
  79. modifiers.insert(Modifiers::META);
  80. }
  81. if e.shift_key() {
  82. modifiers.insert(Modifiers::SHIFT);
  83. }
  84. MouseData::new(
  85. Coordinates::new(
  86. ScreenPoint::new(e.screen_x().into(), e.screen_y().into()),
  87. ClientPoint::new(e.client_x().into(), e.client_y().into()),
  88. ElementPoint::new(e.offset_x().into(), e.offset_y().into()),
  89. PagePoint::new(e.page_x().into(), e.page_y().into()),
  90. ),
  91. Some(MouseButton::from_web_code(e.button().into())),
  92. decode_mouse_button_set(e.buttons()),
  93. modifiers,
  94. )
  95. }
  96. }
  97. impl From<&TouchEvent> for TouchData {
  98. fn from(e: &TouchEvent) -> Self {
  99. Self {
  100. alt_key: e.alt_key(),
  101. ctrl_key: e.ctrl_key(),
  102. meta_key: e.meta_key(),
  103. shift_key: e.shift_key(),
  104. }
  105. }
  106. }
  107. impl From<&PointerEvent> for PointerData {
  108. fn from(e: &PointerEvent) -> Self {
  109. Self {
  110. alt_key: e.alt_key(),
  111. button: e.button(),
  112. buttons: e.buttons(),
  113. client_x: e.client_x(),
  114. client_y: e.client_y(),
  115. ctrl_key: e.ctrl_key(),
  116. meta_key: e.meta_key(),
  117. page_x: e.page_x(),
  118. page_y: e.page_y(),
  119. screen_x: e.screen_x(),
  120. screen_y: e.screen_y(),
  121. shift_key: e.shift_key(),
  122. pointer_id: e.pointer_id(),
  123. width: e.width(),
  124. height: e.height(),
  125. pressure: e.pressure(),
  126. tangential_pressure: e.tangential_pressure(),
  127. tilt_x: e.tilt_x(),
  128. tilt_y: e.tilt_y(),
  129. twist: e.twist(),
  130. pointer_type: e.pointer_type(),
  131. is_primary: e.is_primary(),
  132. // get_modifier_state: evt.get_modifier_state(),
  133. }
  134. }
  135. }
  136. impl From<&WheelEvent> for WheelData {
  137. fn from(e: &WheelEvent) -> Self {
  138. Self {
  139. delta_x: e.delta_x(),
  140. delta_y: e.delta_y(),
  141. delta_z: e.delta_z(),
  142. delta_mode: e.delta_mode(),
  143. }
  144. }
  145. }
  146. impl From<&AnimationEvent> for AnimationData {
  147. fn from(e: &AnimationEvent) -> Self {
  148. Self {
  149. elapsed_time: e.elapsed_time(),
  150. animation_name: e.animation_name(),
  151. pseudo_element: e.pseudo_element(),
  152. }
  153. }
  154. }
  155. impl From<&TransitionEvent> for TransitionData {
  156. fn from(e: &TransitionEvent) -> Self {
  157. Self {
  158. elapsed_time: e.elapsed_time(),
  159. property_name: e.property_name(),
  160. pseudo_element: e.pseudo_element(),
  161. }
  162. }
  163. }