events.rs 5.5 KB

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