trait based event system
This commit is contained in:
1 parent
a72718134b
commit
299b123446
28 files changed
+2407
-734
No files matched your search
@@ -1,10 +1,79 @@
|
||||
use dioxus_core::Event;
|
||||
|
||||
pub type ClipboardEvent = Event<ClipboardData>;
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
||||
pub struct ClipboardData {
|
||||
// DOMDataTransfer clipboardData
|
||||
inner: Box<dyn HasClipboardData>,
|
||||
}
|
||||
|
||||
impl<E: HasClipboardData> From<E> for ClipboardData {
|
||||
fn from(e: E) -> Self {
|
||||
Self { inner: Box::new(e) }
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for ClipboardData {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("ClipboardData").finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for ClipboardData {
|
||||
fn eq(&self, _other: &Self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl ClipboardData {
|
||||
pub fn new(inner: impl HasClipboardData) -> Self {
|
||||
Self {
|
||||
inner: Box::new(inner),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn downcast<T: 'static>(&self) -> Option<&T> {
|
||||
self.inner.as_ref().as_any().downcast_ref::<T>()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
struct SerializedClipboardData {}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl From<&ClipboardData> for SerializedClipboardData {
|
||||
fn from(_: &ClipboardData) -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl HasClipboardData for SerializedClipboardData {
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl serde::Serialize for ClipboardData {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
SerializedClipboardData::from(self).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl<'de> serde::Deserialize<'de> for ClipboardData {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
let data = SerializedClipboardData::deserialize(deserializer)?;
|
||||
Ok(Self {
|
||||
inner: Box::new(data),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HasClipboardData: std::any::Any {
|
||||
/// return self as Any
|
||||
fn as_any(&self) -> &dyn std::any::Any;
|
||||
}
|
||||
|
||||
impl_event![
|
||||
|
||||
Reference in new issue
Block a user