fix desktop events
This commit is contained in:
1 parent
bd387d601c
commit
8ba1c05a93
12 files changed
+507
-217
No files matched your search
@@ -1,5 +1,6 @@
|
||||
use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
|
||||
use crate::input_data::{MouseButton, MouseButtonSet};
|
||||
use crate::prelude::PointInteraction;
|
||||
|
||||
use dioxus_core::Event;
|
||||
use keyboard_types::Modifiers;
|
||||
@@ -43,60 +44,47 @@ impl PartialEq for DragData {
|
||||
}
|
||||
|
||||
impl DragData {
|
||||
/// 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.
|
||||
pub fn client_coordinates(&self) -> ClientPoint {
|
||||
self.inner.client_coordinates()
|
||||
}
|
||||
|
||||
/// The event's coordinates relative to the padding edge of the target element
|
||||
///
|
||||
/// For example, clicking in the top left corner of an element will result in element coordinates (0., 0.)
|
||||
pub fn element_coordinates(&self) -> ElementPoint {
|
||||
self.inner.element_coordinates()
|
||||
}
|
||||
|
||||
/// The event's coordinates relative to the entire document. This includes any portion of the document not currently visible.
|
||||
///
|
||||
/// For example, if the page is scrolled 200 pixels to the right and 300 pixels down, clicking in the top left corner of the viewport would result in page coordinates (200., 300.)
|
||||
pub fn page_coordinates(&self) -> PagePoint {
|
||||
self.inner.page_coordinates()
|
||||
}
|
||||
|
||||
/// The event's coordinates relative to the entire screen. This takes into account the window's offset.
|
||||
pub fn screen_coordinates(&self) -> ScreenPoint {
|
||||
self.inner.screen_coordinates()
|
||||
}
|
||||
|
||||
pub fn coordinates(&self) -> Coordinates {
|
||||
self.inner.coordinates()
|
||||
}
|
||||
|
||||
/// The set of modifier keys which were pressed when the event occurred
|
||||
pub fn modifiers(&self) -> Modifiers {
|
||||
self.inner.modifiers()
|
||||
}
|
||||
|
||||
/// The set of mouse buttons which were held when the event occurred.
|
||||
pub fn held_buttons(&self) -> MouseButtonSet {
|
||||
self.inner.held_buttons()
|
||||
}
|
||||
|
||||
/// The mouse button that triggered the event
|
||||
///
|
||||
// todo the following is kind of bad; should we just return None when the trigger_button is unreliable (and frankly irrelevant)? i guess we would need the event_type here
|
||||
/// This is only guaranteed to indicate which button was pressed during events caused by pressing or releasing a button. As such, it is not reliable for events such as mouseenter, mouseleave, mouseover, mouseout, or mousemove. For example, a value of MouseButton::Primary may also indicate that no button was pressed.
|
||||
pub fn trigger_button(&self) -> Option<MouseButton> {
|
||||
self.inner.trigger_button()
|
||||
}
|
||||
|
||||
/// Downcast this event data to a specific type
|
||||
pub fn downcast<T: 'static>(&self) -> Option<&T> {
|
||||
self.inner.as_any().downcast_ref::<T>()
|
||||
}
|
||||
}
|
||||
|
||||
impl PointInteraction for DragData {
|
||||
fn client_coordinates(&self) -> ClientPoint {
|
||||
self.inner.client_coordinates()
|
||||
}
|
||||
|
||||
fn element_coordinates(&self) -> ElementPoint {
|
||||
self.inner.element_coordinates()
|
||||
}
|
||||
|
||||
fn page_coordinates(&self) -> PagePoint {
|
||||
self.inner.page_coordinates()
|
||||
}
|
||||
|
||||
fn screen_coordinates(&self) -> ScreenPoint {
|
||||
self.inner.screen_coordinates()
|
||||
}
|
||||
|
||||
fn coordinates(&self) -> Coordinates {
|
||||
self.inner.coordinates()
|
||||
}
|
||||
|
||||
fn modifiers(&self) -> Modifiers {
|
||||
self.inner.modifiers()
|
||||
}
|
||||
|
||||
fn held_buttons(&self) -> MouseButtonSet {
|
||||
self.inner.held_buttons()
|
||||
}
|
||||
|
||||
// todo the following is kind of bad; should we just return None when the trigger_button is unreliable (and frankly irrelevant)? i guess we would need the event_type here
|
||||
fn trigger_button(&self) -> Option<MouseButton> {
|
||||
self.inner.trigger_button()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
/// A serialized version of DragData
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
|
||||
@@ -108,15 +96,7 @@ pub struct SerializedDragData {
|
||||
impl From<&DragData> for SerializedDragData {
|
||||
fn from(data: &DragData) -> Self {
|
||||
Self {
|
||||
mouse: crate::point_interaction::SerializedPointInteraction {
|
||||
client_coordinates: data.client_coordinates(),
|
||||
element_coordinates: data.element_coordinates(),
|
||||
page_coordinates: data.page_coordinates(),
|
||||
screen_coordinates: data.screen_coordinates(),
|
||||
modifiers: data.modifiers(),
|
||||
held_buttons: data.held_buttons(),
|
||||
trigger_button: data.trigger_button(),
|
||||
},
|
||||
mouse: crate::point_interaction::SerializedPointInteraction::from(data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,30 +81,46 @@ impl KeyboardData {
|
||||
/// A serialized version of KeyboardData
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
|
||||
pub struct SerializedKeyboardData {
|
||||
auto_repeating: bool,
|
||||
char_code: u32,
|
||||
key: String,
|
||||
key_code: KeyCode,
|
||||
#[serde(deserialize_with = "resilient_deserialize_code")]
|
||||
code: Code,
|
||||
key: Key,
|
||||
location: Location,
|
||||
modifiers: Modifiers,
|
||||
alt_key: bool,
|
||||
ctrl_key: bool,
|
||||
meta_key: bool,
|
||||
shift_key: bool,
|
||||
location: usize,
|
||||
repeat: bool,
|
||||
which: usize,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl SerializedKeyboardData {
|
||||
/// Create a new SerializedKeyboardData
|
||||
pub fn new(
|
||||
auto_repeating: bool,
|
||||
code: Code,
|
||||
key: Key,
|
||||
code: Code,
|
||||
location: Location,
|
||||
is_auto_repeating: bool,
|
||||
modifiers: Modifiers,
|
||||
) -> Self {
|
||||
Self {
|
||||
auto_repeating,
|
||||
char_code: key.legacy_charcode(),
|
||||
key: key.to_string(),
|
||||
key_code: KeyCode::from_raw_code(
|
||||
std::convert::TryInto::try_into(key.legacy_keycode())
|
||||
.expect("could not convert keycode to u8"),
|
||||
),
|
||||
code,
|
||||
key,
|
||||
location,
|
||||
modifiers,
|
||||
alt_key: modifiers.contains(Modifiers::ALT),
|
||||
ctrl_key: modifiers.contains(Modifiers::CONTROL),
|
||||
meta_key: modifiers.contains(Modifiers::META),
|
||||
shift_key: modifiers.contains(Modifiers::SHIFT),
|
||||
location: crate::input_data::encode_key_location(location),
|
||||
repeat: is_auto_repeating,
|
||||
which: std::convert::TryInto::try_into(key.legacy_charcode())
|
||||
.expect("could not convert charcode to usize"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,20 +128,20 @@ impl SerializedKeyboardData {
|
||||
#[cfg(feature = "serialize")]
|
||||
impl From<&KeyboardData> for SerializedKeyboardData {
|
||||
fn from(data: &KeyboardData) -> Self {
|
||||
Self {
|
||||
auto_repeating: data.is_auto_repeating(),
|
||||
code: data.code(),
|
||||
key: data.key(),
|
||||
location: data.location(),
|
||||
modifiers: data.modifiers(),
|
||||
}
|
||||
Self::new(
|
||||
data.key(),
|
||||
data.code(),
|
||||
data.location(),
|
||||
data.is_auto_repeating(),
|
||||
data.modifiers(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl HasKeyboardData for SerializedKeyboardData {
|
||||
fn key(&self) -> Key {
|
||||
self.key.clone()
|
||||
std::str::FromStr::from_str(&self.key).unwrap_or(Key::Unidentified)
|
||||
}
|
||||
|
||||
fn code(&self) -> Code {
|
||||
@@ -133,15 +149,30 @@ impl HasKeyboardData for SerializedKeyboardData {
|
||||
}
|
||||
|
||||
fn modifiers(&self) -> Modifiers {
|
||||
self.modifiers
|
||||
let mut modifiers = Modifiers::empty();
|
||||
|
||||
if self.alt_key {
|
||||
modifiers.insert(Modifiers::ALT);
|
||||
}
|
||||
if self.ctrl_key {
|
||||
modifiers.insert(Modifiers::CONTROL);
|
||||
}
|
||||
if self.meta_key {
|
||||
modifiers.insert(Modifiers::META);
|
||||
}
|
||||
if self.shift_key {
|
||||
modifiers.insert(Modifiers::SHIFT);
|
||||
}
|
||||
|
||||
modifiers
|
||||
}
|
||||
|
||||
fn location(&self) -> Location {
|
||||
self.location
|
||||
crate::input_data::decode_key_location(self.location)
|
||||
}
|
||||
|
||||
fn is_auto_repeating(&self) -> bool {
|
||||
self.auto_repeating
|
||||
self.repeat
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
|
||||
@@ -191,23 +191,17 @@ pub struct SerializedMouseData {
|
||||
impl SerializedMouseData {
|
||||
/// Create a new instance of SerializedMouseData
|
||||
pub fn new(
|
||||
client_coordinates: ClientPoint,
|
||||
element_coordinates: ElementPoint,
|
||||
page_coordinates: PagePoint,
|
||||
screen_coordinates: ScreenPoint,
|
||||
modifiers: Modifiers,
|
||||
held_buttons: MouseButtonSet,
|
||||
trigger_button: Option<MouseButton>,
|
||||
held_buttons: MouseButtonSet,
|
||||
coordinates: Coordinates,
|
||||
modifiers: Modifiers,
|
||||
) -> Self {
|
||||
Self {
|
||||
point_data: crate::point_interaction::SerializedPointInteraction::new(
|
||||
client_coordinates,
|
||||
element_coordinates,
|
||||
page_coordinates,
|
||||
screen_coordinates,
|
||||
modifiers,
|
||||
held_buttons,
|
||||
trigger_button,
|
||||
held_buttons,
|
||||
coordinates,
|
||||
modifiers,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,30 +46,42 @@ impl WheelData {
|
||||
/// A serialized version of WheelData
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
|
||||
pub struct SerializedWheelData {
|
||||
delta: WheelDelta,
|
||||
pub delta_mode: u32,
|
||||
pub delta_x: f64,
|
||||
pub delta_y: f64,
|
||||
pub delta_z: f64,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl SerializedWheelData {
|
||||
/// Create a new SerializedWheelData
|
||||
pub fn new(delta: WheelDelta) -> Self {
|
||||
Self { delta }
|
||||
let delta_mode = match delta {
|
||||
WheelDelta::Pixels(_) => 0,
|
||||
WheelDelta::Lines(_) => 1,
|
||||
WheelDelta::Pages(_) => 2,
|
||||
};
|
||||
let delta_raw = delta.strip_units();
|
||||
Self {
|
||||
delta_mode,
|
||||
delta_x: delta_raw.x,
|
||||
delta_y: delta_raw.y,
|
||||
delta_z: delta_raw.z,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl From<&WheelData> for SerializedWheelData {
|
||||
fn from(data: &WheelData) -> Self {
|
||||
Self {
|
||||
delta: data.inner.delta(),
|
||||
}
|
||||
Self::new(data.delta())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl HasWheelData for SerializedWheelData {
|
||||
fn delta(&self) -> WheelDelta {
|
||||
self.delta
|
||||
WheelDelta::from_web_attributes(self.delta_mode, self.delta_x, self.delta_y, self.delta_z)
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
|
||||
Reference in new issue
Block a user