pointer.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. use dioxus_html::{
  2. geometry::{ClientPoint, ElementPoint, PagePoint, ScreenPoint},
  3. input_data::{decode_mouse_button_set, MouseButton},
  4. prelude::{
  5. InteractionElementOffset, InteractionLocation, Modifiers, ModifiersInteraction,
  6. PointerInteraction,
  7. },
  8. HasPointerData,
  9. };
  10. use web_sys::PointerEvent;
  11. use super::{Synthetic, WebEventExt};
  12. impl HasPointerData for Synthetic<PointerEvent> {
  13. fn pointer_id(&self) -> i32 {
  14. self.event.pointer_id()
  15. }
  16. fn width(&self) -> i32 {
  17. self.event.width()
  18. }
  19. fn height(&self) -> i32 {
  20. self.event.height()
  21. }
  22. fn pressure(&self) -> f32 {
  23. self.event.pressure()
  24. }
  25. fn tangential_pressure(&self) -> f32 {
  26. self.event.tangential_pressure()
  27. }
  28. fn tilt_x(&self) -> i32 {
  29. self.event.tilt_x()
  30. }
  31. fn tilt_y(&self) -> i32 {
  32. self.event.tilt_y()
  33. }
  34. fn twist(&self) -> i32 {
  35. self.event.twist()
  36. }
  37. fn pointer_type(&self) -> String {
  38. self.event.pointer_type()
  39. }
  40. fn is_primary(&self) -> bool {
  41. self.event.is_primary()
  42. }
  43. fn as_any(&self) -> &dyn std::any::Any {
  44. self
  45. }
  46. }
  47. impl InteractionLocation for Synthetic<PointerEvent> {
  48. fn client_coordinates(&self) -> ClientPoint {
  49. ClientPoint::new(self.event.client_x().into(), self.event.client_y().into())
  50. }
  51. fn screen_coordinates(&self) -> ScreenPoint {
  52. ScreenPoint::new(self.event.screen_x().into(), self.event.screen_y().into())
  53. }
  54. fn page_coordinates(&self) -> PagePoint {
  55. PagePoint::new(self.event.page_x().into(), self.event.page_y().into())
  56. }
  57. }
  58. impl InteractionElementOffset for Synthetic<PointerEvent> {
  59. fn element_coordinates(&self) -> ElementPoint {
  60. ElementPoint::new(self.event.offset_x().into(), self.event.offset_y().into())
  61. }
  62. }
  63. impl ModifiersInteraction for Synthetic<PointerEvent> {
  64. fn modifiers(&self) -> Modifiers {
  65. let mut modifiers = Modifiers::empty();
  66. if self.event.alt_key() {
  67. modifiers.insert(Modifiers::ALT);
  68. }
  69. if self.event.ctrl_key() {
  70. modifiers.insert(Modifiers::CONTROL);
  71. }
  72. if self.event.meta_key() {
  73. modifiers.insert(Modifiers::META);
  74. }
  75. if self.event.shift_key() {
  76. modifiers.insert(Modifiers::SHIFT);
  77. }
  78. modifiers
  79. }
  80. }
  81. impl PointerInteraction for Synthetic<PointerEvent> {
  82. fn held_buttons(&self) -> dioxus_html::input_data::MouseButtonSet {
  83. decode_mouse_button_set(self.event.buttons())
  84. }
  85. fn trigger_button(&self) -> Option<MouseButton> {
  86. Some(MouseButton::from_web_code(self.event.button()))
  87. }
  88. }
  89. impl WebEventExt for dioxus_html::PointerData {
  90. type WebEvent = web_sys::PointerEvent;
  91. #[inline(always)]
  92. fn try_as_web_event(&self) -> Option<web_sys::PointerEvent> {
  93. self.downcast::<Synthetic<web_sys::PointerEvent>>()
  94. .map(|e| e.event.clone())
  95. }
  96. }