split the point interaction trait into smaller traits for touch compatibility

This commit is contained in:
Evan Almloff 2023-09-12 10:01:53 -05:00
parent 3a02520ffa
commit a9bb04722f
9 changed files with 101 additions and 50 deletions

View file

@ -1,6 +1,6 @@
use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint}; use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
use crate::input_data::{MouseButton, MouseButtonSet}; use crate::input_data::{MouseButton, MouseButtonSet};
use crate::prelude::PointInteraction; use crate::prelude::*;
use dioxus_core::Event; use dioxus_core::Event;
use keyboard_types::Modifiers; use keyboard_types::Modifiers;
@ -17,7 +17,7 @@ pub struct DragData {
inner: Box<dyn HasDragData>, inner: Box<dyn HasDragData>,
} }
impl<E: HasDragData> From<E> for DragData { impl<E: HasDragData + 'static> From<E> for DragData {
fn from(e: E) -> Self { fn from(e: E) -> Self {
Self { inner: Box::new(e) } Self { inner: Box::new(e) }
} }
@ -57,7 +57,7 @@ impl DragData {
} }
} }
impl PointInteraction for DragData { impl InteractionLocation for DragData {
fn client_coordinates(&self) -> ClientPoint { fn client_coordinates(&self) -> ClientPoint {
self.inner.client_coordinates() self.inner.client_coordinates()
} }
@ -77,11 +77,15 @@ impl PointInteraction for DragData {
fn coordinates(&self) -> Coordinates { fn coordinates(&self) -> Coordinates {
self.inner.coordinates() self.inner.coordinates()
} }
}
impl ModifiersInteraction for DragData {
fn modifiers(&self) -> Modifiers { fn modifiers(&self) -> Modifiers {
self.inner.modifiers() self.inner.modifiers()
} }
}
impl PointerInteraction for DragData {
fn held_buttons(&self) -> MouseButtonSet { fn held_buttons(&self) -> MouseButtonSet {
self.inner.held_buttons() self.inner.held_buttons()
} }
@ -119,7 +123,7 @@ impl HasMouseData for SerializedDragData {
} }
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
impl crate::prelude::PointInteraction for SerializedDragData { impl InteractionLocation for SerializedDragData {
fn client_coordinates(&self) -> ClientPoint { fn client_coordinates(&self) -> ClientPoint {
self.mouse.client_coordinates() self.mouse.client_coordinates()
} }
@ -139,11 +143,17 @@ impl crate::prelude::PointInteraction for SerializedDragData {
fn coordinates(&self) -> Coordinates { fn coordinates(&self) -> Coordinates {
self.mouse.coordinates() self.mouse.coordinates()
} }
}
#[cfg(feature = "serialize")]
impl ModifiersInteraction for SerializedDragData {
fn modifiers(&self) -> Modifiers { fn modifiers(&self) -> Modifiers {
self.mouse.modifiers() self.mouse.modifiers()
} }
}
#[cfg(feature = "serialize")]
impl PointerInteraction for SerializedDragData {
fn held_buttons(&self) -> MouseButtonSet { fn held_buttons(&self) -> MouseButtonSet {
self.mouse.held_buttons() self.mouse.held_buttons()
} }

View file

@ -1,6 +1,6 @@
use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint}; use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
use crate::input_data::{MouseButton, MouseButtonSet}; use crate::input_data::{MouseButton, MouseButtonSet};
use crate::point_interaction::PointInteraction; use crate::prelude::*;
use dioxus_core::Event; use dioxus_core::Event;
use keyboard_types::Modifiers; use keyboard_types::Modifiers;
@ -12,7 +12,7 @@ pub struct MouseData {
inner: Box<dyn HasMouseData>, inner: Box<dyn HasMouseData>,
} }
impl<E: HasMouseData> From<E> for MouseData { impl<E: HasMouseData + 'static> From<E> for MouseData {
fn from(e: E) -> Self { fn from(e: E) -> Self {
Self { inner: Box::new(e) } Self { inner: Box::new(e) }
} }
@ -39,7 +39,7 @@ impl<E: HasMouseData> PartialEq<E> for MouseData {
} }
/// A trait for any object that has the data for a mouse event /// A trait for any object that has the data for a mouse event
pub trait HasMouseData: PointInteraction { pub trait HasMouseData: PointerInteraction {
/// return self as Any /// return self as Any
fn as_any(&self) -> &dyn std::any::Any; fn as_any(&self) -> &dyn std::any::Any;
} }
@ -119,7 +119,7 @@ impl MouseData {
} }
} }
impl PointInteraction for MouseData { impl InteractionLocation for MouseData {
/// The event's coordinates relative to the application's viewport (as opposed to the coordinate within the page). /// The event's coordinates relative to the application's viewport (as opposed to the coordinate within the page).
/// ///
/// For example, clicking in the top left corner of the viewport will always result in a mouse event with client coordinates (0., 0.), regardless of whether the page is scrolled horizontally. /// For example, clicking in the top left corner of the viewport will always result in a mouse event with client coordinates (0., 0.), regardless of whether the page is scrolled horizontally.
@ -149,12 +149,16 @@ impl PointInteraction for MouseData {
fn coordinates(&self) -> Coordinates { fn coordinates(&self) -> Coordinates {
self.inner.coordinates() self.inner.coordinates()
} }
}
impl ModifiersInteraction for MouseData {
/// The set of modifier keys which were pressed when the event occurred /// The set of modifier keys which were pressed when the event occurred
fn modifiers(&self) -> Modifiers { fn modifiers(&self) -> Modifiers {
self.inner.modifiers() self.inner.modifiers()
} }
}
impl PointerInteraction for MouseData {
/// The set of mouse buttons which were held when the event occurred. /// The set of mouse buttons which were held when the event occurred.
fn held_buttons(&self) -> MouseButtonSet { fn held_buttons(&self) -> MouseButtonSet {
self.inner.held_buttons() self.inner.held_buttons()
@ -224,7 +228,7 @@ impl HasMouseData for SerializedMouseData {
} }
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
impl PointInteraction for SerializedMouseData { impl InteractionLocation for SerializedMouseData {
fn client_coordinates(&self) -> ClientPoint { fn client_coordinates(&self) -> ClientPoint {
self.point_data.client_coordinates() self.point_data.client_coordinates()
} }
@ -240,11 +244,17 @@ impl PointInteraction for SerializedMouseData {
fn screen_coordinates(&self) -> ScreenPoint { fn screen_coordinates(&self) -> ScreenPoint {
self.point_data.screen_coordinates() self.point_data.screen_coordinates()
} }
}
#[cfg(feature = "serialize")]
impl ModifiersInteraction for SerializedMouseData {
fn modifiers(&self) -> Modifiers { fn modifiers(&self) -> Modifiers {
self.point_data.modifiers() self.point_data.modifiers()
} }
}
#[cfg(feature = "serialize")]
impl PointerInteraction for SerializedMouseData {
fn held_buttons(&self) -> MouseButtonSet { fn held_buttons(&self) -> MouseButtonSet {
self.point_data.held_buttons() self.point_data.held_buttons()
} }

View file

@ -1,7 +1,7 @@
use dioxus_core::Event; use dioxus_core::Event;
use keyboard_types::Modifiers; use keyboard_types::Modifiers;
use crate::{geometry::*, input_data::*, point_interaction::PointInteraction}; use crate::{geometry::*, input_data::*, prelude::*};
/// A synthetic event that wraps a web-style [`PointerEvent`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent) /// A synthetic event that wraps a web-style [`PointerEvent`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent)
pub type PointerEvent = Event<PointerData>; pub type PointerEvent = Event<PointerData>;
@ -22,7 +22,7 @@ impl PointerData {
} }
} }
impl<E: HasPointerData> From<E> for PointerData { impl<E: HasPointerData + 'static> From<E> for PointerData {
fn from(e: E) -> Self { fn from(e: E) -> Self {
Self { inner: Box::new(e) } Self { inner: Box::new(e) }
} }
@ -69,7 +69,7 @@ impl PartialEq for PointerData {
} }
/// A trait for any object that has the data for a pointer event /// A trait for any object that has the data for a pointer event
pub trait HasPointerData: PointInteraction { pub trait HasPointerData: PointerInteraction {
/// Gets the unique identifier of the pointer causing the event. /// Gets the unique identifier of the pointer causing the event.
fn pointer_id(&self) -> i32; fn pointer_id(&self) -> i32;
@ -189,7 +189,7 @@ impl PointerData {
} }
} }
impl PointInteraction for PointerData { impl InteractionLocation for PointerData {
fn client_coordinates(&self) -> ClientPoint { fn client_coordinates(&self) -> ClientPoint {
self.inner.client_coordinates() self.inner.client_coordinates()
} }
@ -205,11 +205,15 @@ impl PointInteraction for PointerData {
fn page_coordinates(&self) -> PagePoint { fn page_coordinates(&self) -> PagePoint {
self.inner.page_coordinates() self.inner.page_coordinates()
} }
}
impl ModifiersInteraction for PointerData {
fn modifiers(&self) -> Modifiers { fn modifiers(&self) -> Modifiers {
self.inner.modifiers() self.inner.modifiers()
} }
}
impl PointerInteraction for PointerData {
fn held_buttons(&self) -> MouseButtonSet { fn held_buttons(&self) -> MouseButtonSet {
self.inner.held_buttons() self.inner.held_buttons()
} }
@ -306,7 +310,7 @@ impl HasPointerData for SerializedPointerData {
} }
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
impl PointInteraction for SerializedPointerData { impl InteractionLocation for SerializedPointerData {
fn client_coordinates(&self) -> ClientPoint { fn client_coordinates(&self) -> ClientPoint {
self.point_data.client_coordinates() self.point_data.client_coordinates()
} }
@ -322,11 +326,17 @@ impl PointInteraction for SerializedPointerData {
fn page_coordinates(&self) -> PagePoint { fn page_coordinates(&self) -> PagePoint {
self.point_data.page_coordinates() self.point_data.page_coordinates()
} }
}
#[cfg(feature = "serialize")]
impl ModifiersInteraction for SerializedPointerData {
fn modifiers(&self) -> Modifiers { fn modifiers(&self) -> Modifiers {
self.point_data.modifiers() self.point_data.modifiers()
} }
}
#[cfg(feature = "serialize")]
impl PointerInteraction for SerializedPointerData {
fn held_buttons(&self) -> MouseButtonSet { fn held_buttons(&self) -> MouseButtonSet {
self.point_data.held_buttons() self.point_data.held_buttons()
} }

View file

@ -45,6 +45,6 @@ pub mod prelude {
#[cfg(feature = "eval")] #[cfg(feature = "eval")]
pub use crate::eval::*; pub use crate::eval::*;
pub use crate::events::*; pub use crate::events::*;
pub use crate::point_interaction::PointInteraction; pub use crate::point_interaction::*;
pub use keyboard_types::{self, Code, Key, Location, Modifiers}; pub use keyboard_types::{self, Code, Key, Location, Modifiers};
} }

View file

@ -5,8 +5,9 @@ use crate::{
input_data::{MouseButton, MouseButtonSet}, input_data::{MouseButton, MouseButtonSet},
}; };
pub trait PointInteraction: std::any::Any { /// A interaction that contains data about the location of the event.
/// Gets the coordinates of the pointer event. pub trait InteractionLocation {
/// Gets the coordinates of the event.
fn coordinates(&self) -> Coordinates { fn coordinates(&self) -> Coordinates {
Coordinates::new( Coordinates::new(
self.screen_coordinates(), self.screen_coordinates(),
@ -16,26 +17,32 @@ pub trait PointInteraction: std::any::Any {
) )
} }
/// Gets the coordinates of the pointer event relative to the browser viewport. /// Gets the coordinates of the event relative to the browser viewport.
fn client_coordinates(&self) -> ClientPoint; fn client_coordinates(&self) -> ClientPoint;
/// Gets the coordinates of the pointer event relative to the screen. /// Gets the coordinates of the event relative to the screen.
fn screen_coordinates(&self) -> ScreenPoint; fn screen_coordinates(&self) -> ScreenPoint;
/// Gets the coordinates of the pointer event relative to the target element. /// Gets the coordinates of the event relative to the target element.
fn element_coordinates(&self) -> ElementPoint; fn element_coordinates(&self) -> ElementPoint;
/// Gets the coordinates of the pointer event relative to the page. /// Gets the coordinates of the event relative to the page.
fn page_coordinates(&self) -> PagePoint; fn page_coordinates(&self) -> PagePoint;
}
/// Gets the modifiers of the pointer event. /// A interaction that contains data about the pointer button(s) that triggered the event.
fn modifiers(&self) -> Modifiers; pub trait PointerInteraction: InteractionLocation + ModifiersInteraction {
/// Gets the button that triggered the event.
fn trigger_button(&self) -> Option<MouseButton>;
/// Gets the buttons that are currently held down. /// Gets the buttons that are currently held down.
fn held_buttons(&self) -> MouseButtonSet; fn held_buttons(&self) -> MouseButtonSet;
}
/// Gets the button that triggered the event. /// A interaction that contains data about the current state of the keyboard modifiers.
fn trigger_button(&self) -> Option<MouseButton>; pub trait ModifiersInteraction {
/// Gets the modifiers of the pointer event.
fn modifiers(&self) -> Modifiers;
} }
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
@ -138,7 +145,7 @@ impl SerializedPointInteraction {
} }
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
impl<E: PointInteraction> From<&E> for SerializedPointInteraction { impl<E: PointerInteraction> From<&E> for SerializedPointInteraction {
fn from(data: &E) -> Self { fn from(data: &E) -> Self {
let trigger_button = data.trigger_button(); let trigger_button = data.trigger_button();
let held_buttons = data.held_buttons(); let held_buttons = data.held_buttons();
@ -149,23 +156,18 @@ impl<E: PointInteraction> From<&E> for SerializedPointInteraction {
} }
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
impl PointInteraction for SerializedPointInteraction { impl PointerInteraction for SerializedPointInteraction {
fn client_coordinates(&self) -> ClientPoint { fn held_buttons(&self) -> MouseButtonSet {
ClientPoint::new(self.client_x.into(), self.client_y.into()) crate::input_data::decode_mouse_button_set(self.buttons)
} }
fn screen_coordinates(&self) -> ScreenPoint { fn trigger_button(&self) -> Option<MouseButton> {
ScreenPoint::new(self.screen_x.into(), self.screen_y.into()) Some(MouseButton::from_web_code(self.button))
}
fn element_coordinates(&self) -> ElementPoint {
ElementPoint::new(self.offset_x.into(), self.offset_y.into())
}
fn page_coordinates(&self) -> PagePoint {
PagePoint::new(self.page_x.into(), self.page_y.into())
} }
}
#[cfg(feature = "serialize")]
impl ModifiersInteraction for SerializedPointInteraction {
fn modifiers(&self) -> Modifiers { fn modifiers(&self) -> Modifiers {
let mut modifiers = Modifiers::empty(); let mut modifiers = Modifiers::empty();
@ -184,12 +186,23 @@ impl PointInteraction for SerializedPointInteraction {
modifiers modifiers
} }
}
fn held_buttons(&self) -> MouseButtonSet { #[cfg(feature = "serialize")]
crate::input_data::decode_mouse_button_set(self.buttons) impl InteractionLocation for SerializedPointInteraction {
fn client_coordinates(&self) -> ClientPoint {
ClientPoint::new(self.client_x.into(), self.client_y.into())
} }
fn trigger_button(&self) -> Option<MouseButton> { fn screen_coordinates(&self) -> ScreenPoint {
Some(MouseButton::from_web_code(self.button)) ScreenPoint::new(self.screen_x.into(), self.screen_y.into())
}
fn element_coordinates(&self) -> ElementPoint {
ElementPoint::new(self.offset_x.into(), self.offset_y.into())
}
fn page_coordinates(&self) -> PagePoint {
PagePoint::new(self.page_x.into(), self.page_y.into())
} }
} }

View file

@ -103,7 +103,7 @@ impl HasKeyboardData for KeyboardEvent {
impl HasDragData for MouseEvent {} impl HasDragData for MouseEvent {}
impl PointInteraction for MouseEvent { impl InteractionLocation for MouseEvent {
fn client_coordinates(&self) -> ClientPoint { fn client_coordinates(&self) -> ClientPoint {
ClientPoint::new(self.client_x().into(), self.client_y().into()) ClientPoint::new(self.client_x().into(), self.client_y().into())
} }
@ -119,7 +119,9 @@ impl PointInteraction for MouseEvent {
fn screen_coordinates(&self) -> ScreenPoint { fn screen_coordinates(&self) -> ScreenPoint {
ScreenPoint::new(self.screen_x().into(), self.screen_y().into()) ScreenPoint::new(self.screen_x().into(), self.screen_y().into())
} }
}
impl ModifiersInteraction for MouseEvent {
fn modifiers(&self) -> Modifiers { fn modifiers(&self) -> Modifiers {
let mut modifiers = Modifiers::empty(); let mut modifiers = Modifiers::empty();
@ -138,7 +140,9 @@ impl PointInteraction for MouseEvent {
modifiers modifiers
} }
}
impl PointerInteraction for MouseEvent {
fn held_buttons(&self) -> crate::input_data::MouseButtonSet { fn held_buttons(&self) -> crate::input_data::MouseButtonSet {
decode_mouse_button_set(self.buttons()) decode_mouse_button_set(self.buttons())
} }
@ -222,7 +226,7 @@ impl HasPointerData for PointerEvent {
} }
} }
impl PointInteraction for PointerEvent { impl InteractionLocation for PointerEvent {
fn client_coordinates(&self) -> ClientPoint { fn client_coordinates(&self) -> ClientPoint {
ClientPoint::new(self.client_x().into(), self.client_y().into()) ClientPoint::new(self.client_x().into(), self.client_y().into())
} }
@ -238,7 +242,9 @@ impl PointInteraction for PointerEvent {
fn page_coordinates(&self) -> PagePoint { fn page_coordinates(&self) -> PagePoint {
PagePoint::new(self.page_x().into(), self.page_y().into()) PagePoint::new(self.page_x().into(), self.page_y().into())
} }
}
impl ModifiersInteraction for PointerEvent {
fn modifiers(&self) -> Modifiers { fn modifiers(&self) -> Modifiers {
let mut modifiers = Modifiers::empty(); let mut modifiers = Modifiers::empty();
@ -257,7 +263,9 @@ impl PointInteraction for PointerEvent {
modifiers modifiers
} }
}
impl PointerInteraction for PointerEvent {
fn held_buttons(&self) -> crate::input_data::MouseButtonSet { fn held_buttons(&self) -> crate::input_data::MouseButtonSet {
decode_mouse_button_set(self.buttons()) decode_mouse_button_set(self.buttons())
} }

View file

@ -18,7 +18,7 @@ use dioxus_html::input_data::keyboard_types::{Code, Key, Location, Modifiers};
use dioxus_html::input_data::{ use dioxus_html::input_data::{
MouseButton as DioxusMouseButton, MouseButtonSet as DioxusMouseButtons, MouseButton as DioxusMouseButton, MouseButtonSet as DioxusMouseButtons,
}; };
use dioxus_html::{event_bubbles, prelude::PointInteraction}; use dioxus_html::{event_bubbles, prelude::*};
use std::any::Any; use std::any::Any;
use std::collections::HashMap; use std::collections::HashMap;
use std::{ use std::{

View file

@ -1,8 +1,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use dioxus_html::{ use dioxus_html::{
input_data::keyboard_types::Key, prelude::PointInteraction, HasKeyboardData, input_data::keyboard_types::Key, prelude::*, HasKeyboardData, SerializedKeyboardData,
SerializedKeyboardData, SerializedMouseData, SerializedMouseData,
}; };
use dioxus_native_core::{ use dioxus_native_core::{
custom_element::CustomElement, custom_element::CustomElement,

View file

@ -2,8 +2,8 @@ use std::{collections::HashMap, io::stdout};
use crossterm::{cursor::MoveTo, execute}; use crossterm::{cursor::MoveTo, execute};
use dioxus_html::{ use dioxus_html::{
input_data::keyboard_types::Key, prelude::PointInteraction, HasKeyboardData, input_data::keyboard_types::Key, prelude::*, HasKeyboardData, SerializedKeyboardData,
SerializedKeyboardData, SerializedMouseData, SerializedMouseData,
}; };
use dioxus_native_core::{ use dioxus_native_core::{
custom_element::CustomElement, custom_element::CustomElement,