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

View file

@ -1,6 +1,6 @@
use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
use crate::input_data::{MouseButton, MouseButtonSet};
use crate::point_interaction::PointInteraction;
use crate::prelude::*;
use dioxus_core::Event;
use keyboard_types::Modifiers;
@ -12,7 +12,7 @@ pub struct MouseData {
inner: Box<dyn HasMouseData>,
}
impl<E: HasMouseData> From<E> for MouseData {
impl<E: HasMouseData + 'static> From<E> for MouseData {
fn from(e: E) -> Self {
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
pub trait HasMouseData: PointInteraction {
pub trait HasMouseData: PointerInteraction {
/// return self as 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).
///
/// 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 {
self.inner.coordinates()
}
}
impl ModifiersInteraction for MouseData {
/// The set of modifier keys which were pressed when the event occurred
fn modifiers(&self) -> Modifiers {
self.inner.modifiers()
}
}
impl PointerInteraction for MouseData {
/// The set of mouse buttons which were held when the event occurred.
fn held_buttons(&self) -> MouseButtonSet {
self.inner.held_buttons()
@ -224,7 +228,7 @@ impl HasMouseData for SerializedMouseData {
}
#[cfg(feature = "serialize")]
impl PointInteraction for SerializedMouseData {
impl InteractionLocation for SerializedMouseData {
fn client_coordinates(&self) -> ClientPoint {
self.point_data.client_coordinates()
}
@ -240,11 +244,17 @@ impl PointInteraction for SerializedMouseData {
fn screen_coordinates(&self) -> ScreenPoint {
self.point_data.screen_coordinates()
}
}
#[cfg(feature = "serialize")]
impl ModifiersInteraction for SerializedMouseData {
fn modifiers(&self) -> Modifiers {
self.point_data.modifiers()
}
}
#[cfg(feature = "serialize")]
impl PointerInteraction for SerializedMouseData {
fn held_buttons(&self) -> MouseButtonSet {
self.point_data.held_buttons()
}

View file

@ -1,7 +1,7 @@
use dioxus_core::Event;
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)
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 {
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
pub trait HasPointerData: PointInteraction {
pub trait HasPointerData: PointerInteraction {
/// Gets the unique identifier of the pointer causing the event.
fn pointer_id(&self) -> i32;
@ -189,7 +189,7 @@ impl PointerData {
}
}
impl PointInteraction for PointerData {
impl InteractionLocation for PointerData {
fn client_coordinates(&self) -> ClientPoint {
self.inner.client_coordinates()
}
@ -205,11 +205,15 @@ impl PointInteraction for PointerData {
fn page_coordinates(&self) -> PagePoint {
self.inner.page_coordinates()
}
}
impl ModifiersInteraction for PointerData {
fn modifiers(&self) -> Modifiers {
self.inner.modifiers()
}
}
impl PointerInteraction for PointerData {
fn held_buttons(&self) -> MouseButtonSet {
self.inner.held_buttons()
}
@ -306,7 +310,7 @@ impl HasPointerData for SerializedPointerData {
}
#[cfg(feature = "serialize")]
impl PointInteraction for SerializedPointerData {
impl InteractionLocation for SerializedPointerData {
fn client_coordinates(&self) -> ClientPoint {
self.point_data.client_coordinates()
}
@ -322,11 +326,17 @@ impl PointInteraction for SerializedPointerData {
fn page_coordinates(&self) -> PagePoint {
self.point_data.page_coordinates()
}
}
#[cfg(feature = "serialize")]
impl ModifiersInteraction for SerializedPointerData {
fn modifiers(&self) -> Modifiers {
self.point_data.modifiers()
}
}
#[cfg(feature = "serialize")]
impl PointerInteraction for SerializedPointerData {
fn held_buttons(&self) -> MouseButtonSet {
self.point_data.held_buttons()
}

View file

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

View file

@ -5,8 +5,9 @@ use crate::{
input_data::{MouseButton, MouseButtonSet},
};
pub trait PointInteraction: std::any::Any {
/// Gets the coordinates of the pointer event.
/// A interaction that contains data about the location of the event.
pub trait InteractionLocation {
/// Gets the coordinates of the event.
fn coordinates(&self) -> Coordinates {
Coordinates::new(
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;
/// 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;
/// 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;
/// 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;
}
/// Gets the modifiers of the pointer event.
fn modifiers(&self) -> Modifiers;
/// A interaction that contains data about the pointer button(s) that triggered the event.
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.
fn held_buttons(&self) -> MouseButtonSet;
}
/// Gets the button that triggered the event.
fn trigger_button(&self) -> Option<MouseButton>;
/// A interaction that contains data about the current state of the keyboard modifiers.
pub trait ModifiersInteraction {
/// Gets the modifiers of the pointer event.
fn modifiers(&self) -> Modifiers;
}
#[cfg(feature = "serialize")]
@ -138,7 +145,7 @@ impl SerializedPointInteraction {
}
#[cfg(feature = "serialize")]
impl<E: PointInteraction> From<&E> for SerializedPointInteraction {
impl<E: PointerInteraction> From<&E> for SerializedPointInteraction {
fn from(data: &E) -> Self {
let trigger_button = data.trigger_button();
let held_buttons = data.held_buttons();
@ -149,23 +156,18 @@ impl<E: PointInteraction> From<&E> for SerializedPointInteraction {
}
#[cfg(feature = "serialize")]
impl PointInteraction for SerializedPointInteraction {
fn client_coordinates(&self) -> ClientPoint {
ClientPoint::new(self.client_x.into(), self.client_y.into())
impl PointerInteraction for SerializedPointInteraction {
fn held_buttons(&self) -> MouseButtonSet {
crate::input_data::decode_mouse_button_set(self.buttons)
}
fn screen_coordinates(&self) -> ScreenPoint {
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())
fn trigger_button(&self) -> Option<MouseButton> {
Some(MouseButton::from_web_code(self.button))
}
}
#[cfg(feature = "serialize")]
impl ModifiersInteraction for SerializedPointInteraction {
fn modifiers(&self) -> Modifiers {
let mut modifiers = Modifiers::empty();
@ -184,12 +186,23 @@ impl PointInteraction for SerializedPointInteraction {
modifiers
}
fn held_buttons(&self) -> MouseButtonSet {
crate::input_data::decode_mouse_button_set(self.buttons)
}
fn trigger_button(&self) -> Option<MouseButton> {
Some(MouseButton::from_web_code(self.button))
#[cfg(feature = "serialize")]
impl InteractionLocation for SerializedPointInteraction {
fn client_coordinates(&self) -> ClientPoint {
ClientPoint::new(self.client_x.into(), self.client_y.into())
}
fn screen_coordinates(&self) -> ScreenPoint {
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 PointInteraction for MouseEvent {
impl InteractionLocation for MouseEvent {
fn client_coordinates(&self) -> ClientPoint {
ClientPoint::new(self.client_x().into(), self.client_y().into())
}
@ -119,7 +119,9 @@ impl PointInteraction for MouseEvent {
fn screen_coordinates(&self) -> ScreenPoint {
ScreenPoint::new(self.screen_x().into(), self.screen_y().into())
}
}
impl ModifiersInteraction for MouseEvent {
fn modifiers(&self) -> Modifiers {
let mut modifiers = Modifiers::empty();
@ -138,7 +140,9 @@ impl PointInteraction for MouseEvent {
modifiers
}
}
impl PointerInteraction for MouseEvent {
fn held_buttons(&self) -> crate::input_data::MouseButtonSet {
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 {
ClientPoint::new(self.client_x().into(), self.client_y().into())
}
@ -238,7 +242,9 @@ impl PointInteraction for PointerEvent {
fn page_coordinates(&self) -> PagePoint {
PagePoint::new(self.page_x().into(), self.page_y().into())
}
}
impl ModifiersInteraction for PointerEvent {
fn modifiers(&self) -> Modifiers {
let mut modifiers = Modifiers::empty();
@ -257,7 +263,9 @@ impl PointInteraction for PointerEvent {
modifiers
}
}
impl PointerInteraction for PointerEvent {
fn held_buttons(&self) -> crate::input_data::MouseButtonSet {
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::{
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::collections::HashMap;
use std::{

View file

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

View file

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