datatransfer object, drag_and_drop example, fix drag-and-drop (#4789)
* wip: implement datatransfer * Editable kanban contents * desktop sorta works without crashing * lint
This commit is contained in:
1 parent
7856679641
commit
0aa7351473
13 files changed
+562
-122
No files matched your search
@@ -0,0 +1,180 @@
|
||||
pub struct DataTransfer {
|
||||
inner: Box<dyn NativeDataTransfer>,
|
||||
}
|
||||
|
||||
impl DataTransfer {
|
||||
pub fn new(inner: impl NativeDataTransfer + 'static) -> Self {
|
||||
Self {
|
||||
inner: Box::new(inner),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
pub fn store(&self, item: impl Serialize) -> Result<(), String> {
|
||||
let serialized = serde_json::to_string(&item).map_err(|e| e.to_string())?;
|
||||
self.set_data("application/json", &serialized)
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
pub fn retrieve<T: for<'de> serde::Deserialize<'de>>(&self) -> Result<Option<T>, String> {
|
||||
if let Some(data) = self.get_data("application/json") {
|
||||
let deserialized = serde_json::from_str(&data).map_err(|e| e.to_string())?;
|
||||
Ok(Some(deserialized))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_data(&self, format: &str) -> Option<String> {
|
||||
self.inner.get_data(format)
|
||||
}
|
||||
|
||||
pub fn get_as_text(&self) -> Option<String> {
|
||||
self.get_data("text/plain")
|
||||
}
|
||||
|
||||
pub fn set_data(&self, format: &str, data: &str) -> Result<(), String> {
|
||||
self.inner.set_data(format, data)
|
||||
}
|
||||
|
||||
pub fn clear_data(&self, format: Option<&str>) -> Result<(), String> {
|
||||
self.inner.clear_data(format)
|
||||
}
|
||||
|
||||
pub fn effect_allowed(&self) -> String {
|
||||
self.inner.effect_allowed()
|
||||
}
|
||||
|
||||
pub fn set_effect_allowed(&self, effect: &str) {
|
||||
self.inner.set_effect_allowed(effect)
|
||||
}
|
||||
|
||||
pub fn drop_effect(&self) -> String {
|
||||
self.inner.drop_effect()
|
||||
}
|
||||
|
||||
pub fn set_drop_effect(&self, effect: &str) {
|
||||
self.inner.set_drop_effect(effect)
|
||||
}
|
||||
|
||||
pub fn files(&self) -> Vec<crate::file_data::FileData> {
|
||||
self.inner.files()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait NativeDataTransfer: Send + Sync {
|
||||
fn get_data(&self, format: &str) -> Option<String>;
|
||||
fn set_data(&self, format: &str, data: &str) -> Result<(), String>;
|
||||
fn clear_data(&self, format: Option<&str>) -> Result<(), String>;
|
||||
fn effect_allowed(&self) -> String;
|
||||
fn set_effect_allowed(&self, effect: &str);
|
||||
fn drop_effect(&self) -> String;
|
||||
fn set_drop_effect(&self, effect: &str);
|
||||
fn files(&self) -> Vec<crate::file_data::FileData>;
|
||||
}
|
||||
|
||||
pub trait HasDataTransferData {
|
||||
fn data_transfer(&self) -> DataTransfer;
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
pub use ser::*;
|
||||
#[cfg(feature = "serialize")]
|
||||
use serde::Serialize;
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
mod ser {
|
||||
use crate::DragData;
|
||||
|
||||
use super::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A serialized version of DataTransfer
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
|
||||
pub struct SerializedDataTransfer {
|
||||
pub items: Vec<SerializedDataTransferItem>,
|
||||
pub files: Vec<crate::file_data::SerializedFileData>,
|
||||
pub effect_allowed: String,
|
||||
pub drop_effect: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
|
||||
pub struct SerializedDataTransferItem {
|
||||
pub kind: String,
|
||||
pub type_: String,
|
||||
pub data: String,
|
||||
}
|
||||
|
||||
impl NativeDataTransfer for SerializedDataTransfer {
|
||||
fn get_data(&self, format: &str) -> Option<String> {
|
||||
self.items
|
||||
.iter()
|
||||
.find(|item| item.type_ == format)
|
||||
.map(|item| item.data.clone())
|
||||
}
|
||||
|
||||
fn set_data(&self, _format: &str, _data: &str) -> Result<(), String> {
|
||||
// todo!()
|
||||
// Err("Cannot set data on serialized DataTransfer".into())
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn clear_data(&self, _format: Option<&str>) -> Result<(), String> {
|
||||
// todo!()
|
||||
// Err("Cannot clear data on serialized DataTransfer".into())
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn effect_allowed(&self) -> String {
|
||||
self.effect_allowed.clone()
|
||||
}
|
||||
|
||||
fn set_effect_allowed(&self, _effect: &str) {
|
||||
// No-op
|
||||
}
|
||||
|
||||
fn drop_effect(&self) -> String {
|
||||
self.drop_effect.clone()
|
||||
}
|
||||
|
||||
fn set_drop_effect(&self, _effect: &str) {
|
||||
// No-op
|
||||
}
|
||||
|
||||
fn files(&self) -> Vec<crate::file_data::FileData> {
|
||||
self.files
|
||||
.iter()
|
||||
.map(|f| crate::file_data::FileData::new(f.clone()))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&DragData> for SerializedDataTransfer {
|
||||
fn from(_drag: &DragData) -> Self {
|
||||
todo!()
|
||||
// let items = vec![]; // drag.data_transfer().items().iter().map(|item| SerializedDataTransferItem {
|
||||
// // kind: item.kind().to_string(),
|
||||
// // type_: item.type_().to_string(),
|
||||
// // data: item.data().unwrap_or_default(),
|
||||
// // }).collect();
|
||||
|
||||
// let files = drag
|
||||
// .files()
|
||||
// .iter()
|
||||
// .map(|f| crate::file_data::SerializedFileData {
|
||||
// name: f.name().to_string(),
|
||||
// size: f.size(),
|
||||
// type_: f.type_().to_string(),
|
||||
// })
|
||||
// .collect();
|
||||
|
||||
// Self {
|
||||
// items,
|
||||
// files,
|
||||
// effect_allowed: drag.effect_allowed().to_string(),
|
||||
// drop_effect: drag.drop_effect().to_string(),
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
|
||||
use crate::input_data::{MouseButton, MouseButtonSet};
|
||||
use crate::*;
|
||||
use crate::{
|
||||
data_transfer::DataTransfer,
|
||||
geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint},
|
||||
};
|
||||
|
||||
use dioxus_core::Event;
|
||||
use keyboard_types::Modifiers;
|
||||
@@ -51,6 +54,11 @@ impl DragData {
|
||||
}
|
||||
}
|
||||
|
||||
/// The DataTransfer object is used to hold the data that is being dragged during a drag and drop operation.
|
||||
pub fn data_transfer(&self) -> DataTransfer {
|
||||
self.inner.data_transfer()
|
||||
}
|
||||
|
||||
/// Downcast this event data to a specific type
|
||||
#[inline(always)]
|
||||
pub fn downcast<T: 'static>(&self) -> Option<&T> {
|
||||
@@ -106,111 +114,115 @@ impl PointerInteraction for DragData {
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
/// A serialized version of DragData
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
|
||||
pub struct SerializedDragData {
|
||||
pub mouse: crate::point_interaction::SerializedPointInteraction,
|
||||
|
||||
pub files: Vec<SerializedFileData>,
|
||||
}
|
||||
pub use ser::*;
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl SerializedDragData {
|
||||
fn new(drag: &DragData) -> Self {
|
||||
Self {
|
||||
mouse: crate::point_interaction::SerializedPointInteraction::from(drag),
|
||||
files: vec![],
|
||||
mod ser {
|
||||
use super::*;
|
||||
|
||||
/// A serialized version of DragData
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
|
||||
pub struct SerializedDragData {
|
||||
pub mouse: crate::point_interaction::SerializedPointInteraction,
|
||||
|
||||
pub data_transfer: crate::data_transfer::SerializedDataTransfer,
|
||||
}
|
||||
|
||||
impl SerializedDragData {
|
||||
fn new(drag: &DragData) -> Self {
|
||||
Self {
|
||||
mouse: crate::point_interaction::SerializedPointInteraction::from(drag),
|
||||
data_transfer: crate::data_transfer::SerializedDataTransfer::from(drag),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HasDataTransferData for SerializedDragData {
|
||||
fn data_transfer(&self) -> crate::data_transfer::DataTransfer {
|
||||
crate::data_transfer::DataTransfer::new(self.data_transfer.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl HasDragData for SerializedDragData {
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::file_data::HasFileData for SerializedDragData {
|
||||
fn files(&self) -> Vec<FileData> {
|
||||
self.data_transfer
|
||||
.files
|
||||
.iter()
|
||||
.map(|f| FileData::new(f.clone()))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl HasMouseData for SerializedDragData {
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
impl InteractionElementOffset for SerializedDragData {
|
||||
fn element_coordinates(&self) -> ElementPoint {
|
||||
self.mouse.element_coordinates()
|
||||
}
|
||||
|
||||
fn coordinates(&self) -> Coordinates {
|
||||
self.mouse.coordinates()
|
||||
}
|
||||
}
|
||||
|
||||
impl ModifiersInteraction for SerializedDragData {
|
||||
fn modifiers(&self) -> Modifiers {
|
||||
self.mouse.modifiers()
|
||||
}
|
||||
}
|
||||
|
||||
impl PointerInteraction for SerializedDragData {
|
||||
fn held_buttons(&self) -> MouseButtonSet {
|
||||
self.mouse.held_buttons()
|
||||
}
|
||||
|
||||
fn trigger_button(&self) -> Option<MouseButton> {
|
||||
self.mouse.trigger_button()
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::Serialize for DragData {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
SerializedDragData::new(self).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl HasDragData for SerializedDragData {
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl crate::file_data::HasFileData for SerializedDragData {
|
||||
fn files(&self) -> Vec<FileData> {
|
||||
self.files
|
||||
.iter()
|
||||
.map(|f| FileData::new(f.clone()))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[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::new(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 + crate::HasFileData {
|
||||
pub trait HasDragData: HasMouseData + crate::HasFileData + crate::HasDataTransferData {
|
||||
/// return self as Any
|
||||
fn as_any(&self) -> &dyn std::any::Any;
|
||||
}
|
||||
|
||||
@@ -25,10 +25,12 @@ pub mod events;
|
||||
pub(crate) mod file_data;
|
||||
pub use file_data::*;
|
||||
mod attribute_groups;
|
||||
mod data_transfer;
|
||||
pub mod geometry;
|
||||
pub mod input_data;
|
||||
pub mod point_interaction;
|
||||
mod render_template;
|
||||
pub use data_transfer::*;
|
||||
|
||||
pub use bytes;
|
||||
|
||||
|
||||
Reference in new issue
Block a user