Merge branch 'master' into add-file-data-drag-event
This commit is contained in:
commit
56798b3d1c
407 files changed
+16602
-11298
No files matched your search
@@ -1,5 +1,12 @@
|
||||
use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
|
||||
use crate::input_data::{MouseButton, MouseButtonSet};
|
||||
use crate::prelude::*;
|
||||
use crate::FileEngine;
|
||||
use crate::MouseData;
|
||||
|
||||
use dioxus_core::Event;
|
||||
use keyboard_types::Modifiers;
|
||||
|
||||
use crate::HasMouseData;
|
||||
|
||||
use dioxus_core::Event;
|
||||
use std::fmt::Debug;
|
||||
@@ -10,38 +17,185 @@ pub type DragEvent = Event<DragData>;
|
||||
/// placing a pointer device (such as a mouse) on the touch surface and then dragging the pointer to a new location
|
||||
/// (such as another DOM element). Applications are free to interpret a drag and drop interaction in an
|
||||
/// application-specific way.
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Clone)]
|
||||
pub struct DragData {
|
||||
/// Inherit mouse data
|
||||
pub mouse: MouseData,
|
||||
|
||||
#[cfg_attr(
|
||||
feature = "serialize",
|
||||
serde(
|
||||
default,
|
||||
skip_serializing,
|
||||
deserialize_with = "crate::events::deserialize_file_engine"
|
||||
)
|
||||
)]
|
||||
pub files: Option<std::sync::Arc<dyn FileEngine>>,
|
||||
inner: Box<dyn HasDragData>,
|
||||
}
|
||||
|
||||
impl Debug for DragData {
|
||||
impl<E: HasDragData + 'static> From<E> for DragData {
|
||||
fn from(e: E) -> Self {
|
||||
Self { inner: Box::new(e) }
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for DragData {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("DragData")
|
||||
.field("mouse", &self.mouse)
|
||||
.field("coordinates", &self.coordinates())
|
||||
.field("modifiers", &self.modifiers())
|
||||
.field("held_buttons", &self.held_buttons())
|
||||
.field("trigger_button", &self.trigger_button())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for DragData {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.mouse == other.mouse
|
||||
self.coordinates() == other.coordinates()
|
||||
&& self.modifiers() == other.modifiers()
|
||||
&& self.held_buttons() == other.held_buttons()
|
||||
&& self.trigger_button() == other.trigger_button()
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for DragData {}
|
||||
impl DragData {
|
||||
/// Create a new DragData
|
||||
pub fn new(inner: impl HasDragData + 'static) -> Self {
|
||||
Self {
|
||||
inner: Box::new(inner),
|
||||
}
|
||||
}
|
||||
|
||||
/// Downcast this event data to a specific type
|
||||
pub fn downcast<T: 'static>(&self) -> Option<&T> {
|
||||
self.inner.as_any().downcast_ref::<T>()
|
||||
}
|
||||
|
||||
/// Get the files of the form event
|
||||
pub fn files(&self) -> Option<std::sync::Arc<dyn FileEngine>> {
|
||||
self.inner.files()
|
||||
}
|
||||
}
|
||||
|
||||
impl InteractionLocation for DragData {
|
||||
fn client_coordinates(&self) -> ClientPoint {
|
||||
self.inner.client_coordinates()
|
||||
}
|
||||
|
||||
fn page_coordinates(&self) -> PagePoint {
|
||||
self.inner.page_coordinates()
|
||||
}
|
||||
|
||||
fn screen_coordinates(&self) -> ScreenPoint {
|
||||
self.inner.screen_coordinates()
|
||||
}
|
||||
}
|
||||
|
||||
impl InteractionElementOffset for DragData {
|
||||
fn element_coordinates(&self) -> ElementPoint {
|
||||
self.inner.element_coordinates()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
// 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)]
|
||||
pub struct SerializedDragData {
|
||||
mouse: crate::point_interaction::SerializedPointInteraction,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl From<&DragData> for SerializedDragData {
|
||||
fn from(data: &DragData) -> Self {
|
||||
Self {
|
||||
mouse: crate::point_interaction::SerializedPointInteraction::from(data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl HasDragData for SerializedDragData {}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl HasMouseData for SerializedDragData {
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl InteractionLocation for SerializedDragData {
|
||||
fn client_coordinates(&self) -> ClientPoint {
|
||||
self.mouse.client_coordinates()
|
||||
}
|
||||
|
||||
fn page_coordinates(&self) -> PagePoint {
|
||||
self.mouse.page_coordinates()
|
||||
}
|
||||
|
||||
fn screen_coordinates(&self) -> ScreenPoint {
|
||||
self.mouse.screen_coordinates()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl InteractionElementOffset for SerializedDragData {
|
||||
fn element_coordinates(&self) -> ElementPoint {
|
||||
self.mouse.element_coordinates()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
fn trigger_button(&self) -> Option<MouseButton> {
|
||||
self.mouse.trigger_button()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl serde::Serialize for DragData {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
SerializedDragData::from(self).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl<'de> serde::Deserialize<'de> for DragData {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
let data = SerializedDragData::deserialize(deserializer)?;
|
||||
Ok(Self {
|
||||
inner: Box::new(data),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait for any object that has the data for a drag event
|
||||
pub trait HasDragData: HasMouseData {}
|
||||
|
||||
impl_event! {
|
||||
DragData;
|
||||
|
||||
Reference in new issue
Block a user