Merge branch 'master' into pr/atty303/1349
This commit is contained in:
commit
e5df83ceec
278 files changed
+11299
-3674
No files matched your search
+193
-5
@@ -1,3 +1,6 @@
|
||||
use std::any::Any;
|
||||
use std::sync::RwLock;
|
||||
|
||||
macro_rules! impl_event {
|
||||
(
|
||||
$data:ty;
|
||||
@@ -12,8 +15,8 @@ macro_rules! impl_event {
|
||||
pub fn $name<'a, E: crate::EventReturn<T>, T>(_cx: &'a ::dioxus_core::ScopeState, mut _f: impl FnMut(::dioxus_core::Event<$data>) -> E + 'a) -> ::dioxus_core::MountedAttribute<'a> {
|
||||
::dioxus_core::Attribute::new(
|
||||
stringify!($name),
|
||||
_cx.listener(move |e: ::dioxus_core::Event<$data>| {
|
||||
_f(e).spawn(_cx);
|
||||
_cx.listener(move |e: ::dioxus_core::Event<crate::PlatformEventData>| {
|
||||
_f(e.map(|e|e.into())).spawn(_cx);
|
||||
}),
|
||||
None,
|
||||
false,
|
||||
@@ -23,6 +26,193 @@ macro_rules! impl_event {
|
||||
};
|
||||
}
|
||||
|
||||
static EVENT_CONVERTER: RwLock<Option<Box<dyn HtmlEventConverter>>> = RwLock::new(None);
|
||||
|
||||
#[inline]
|
||||
pub fn set_event_converter(converter: Box<dyn HtmlEventConverter>) {
|
||||
*EVENT_CONVERTER.write().unwrap() = Some(converter);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn with_event_converter<F, R>(f: F) -> R
|
||||
where
|
||||
F: FnOnce(&dyn HtmlEventConverter) -> R,
|
||||
{
|
||||
let converter = EVENT_CONVERTER.read().unwrap();
|
||||
f(converter.as_ref().unwrap().as_ref())
|
||||
}
|
||||
|
||||
/// A platform specific event.
|
||||
pub struct PlatformEventData {
|
||||
event: Box<dyn Any>,
|
||||
}
|
||||
|
||||
impl PlatformEventData {
|
||||
pub fn new(event: Box<dyn Any>) -> Self {
|
||||
Self { event }
|
||||
}
|
||||
|
||||
pub fn downcast<T: 'static>(&self) -> Option<&T> {
|
||||
self.event.downcast_ref::<T>()
|
||||
}
|
||||
|
||||
pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
|
||||
self.event.downcast_mut::<T>()
|
||||
}
|
||||
|
||||
pub fn into_inner<T: 'static>(self) -> Option<T> {
|
||||
self.event.downcast::<T>().ok().map(|e| *e)
|
||||
}
|
||||
}
|
||||
|
||||
/// A converter between a platform specific event and a general event. All code in a renderer that has a large binary size should be placed in this trait. Each of these functions should be snipped in high levels of optimization.
|
||||
pub trait HtmlEventConverter: Send + Sync {
|
||||
/// Convert a general event to an animation data event
|
||||
fn convert_animation_data(&self, event: &PlatformEventData) -> AnimationData;
|
||||
/// Convert a general event to a clipboard data event
|
||||
fn convert_clipboard_data(&self, event: &PlatformEventData) -> ClipboardData;
|
||||
/// Convert a general event to a composition data event
|
||||
fn convert_composition_data(&self, event: &PlatformEventData) -> CompositionData;
|
||||
/// Convert a general event to a drag data event
|
||||
fn convert_drag_data(&self, event: &PlatformEventData) -> DragData;
|
||||
/// Convert a general event to a focus data event
|
||||
fn convert_focus_data(&self, event: &PlatformEventData) -> FocusData;
|
||||
/// Convert a general event to a form data event
|
||||
fn convert_form_data(&self, event: &PlatformEventData) -> FormData;
|
||||
/// Convert a general event to an image data event
|
||||
fn convert_image_data(&self, event: &PlatformEventData) -> ImageData;
|
||||
/// Convert a general event to a keyboard data event
|
||||
fn convert_keyboard_data(&self, event: &PlatformEventData) -> KeyboardData;
|
||||
/// Convert a general event to a media data event
|
||||
fn convert_media_data(&self, event: &PlatformEventData) -> MediaData;
|
||||
/// Convert a general event to a mounted data event
|
||||
fn convert_mounted_data(&self, event: &PlatformEventData) -> MountedData;
|
||||
/// Convert a general event to a mouse data event
|
||||
fn convert_mouse_data(&self, event: &PlatformEventData) -> MouseData;
|
||||
/// Convert a general event to a pointer data event
|
||||
fn convert_pointer_data(&self, event: &PlatformEventData) -> PointerData;
|
||||
/// Convert a general event to a scroll data event
|
||||
fn convert_scroll_data(&self, event: &PlatformEventData) -> ScrollData;
|
||||
/// Convert a general event to a selection data event
|
||||
fn convert_selection_data(&self, event: &PlatformEventData) -> SelectionData;
|
||||
/// Convert a general event to a toggle data event
|
||||
fn convert_toggle_data(&self, event: &PlatformEventData) -> ToggleData;
|
||||
/// Convert a general event to a touch data event
|
||||
fn convert_touch_data(&self, event: &PlatformEventData) -> TouchData;
|
||||
/// Convert a general event to a transition data event
|
||||
fn convert_transition_data(&self, event: &PlatformEventData) -> TransitionData;
|
||||
/// Convert a general event to a wheel data event
|
||||
fn convert_wheel_data(&self, event: &PlatformEventData) -> WheelData;
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for AnimationData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_animation_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for ClipboardData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_clipboard_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for CompositionData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_composition_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for DragData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_drag_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for FocusData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_focus_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for FormData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_form_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for ImageData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_image_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for KeyboardData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_keyboard_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for MediaData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_media_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for MountedData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_mounted_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for MouseData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_mouse_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for PointerData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_pointer_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for ScrollData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_scroll_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for SelectionData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_selection_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for ToggleData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_toggle_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for TouchData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_touch_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for TransitionData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_transition_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlatformEventData> for WheelData {
|
||||
fn from(val: &PlatformEventData) -> Self {
|
||||
with_event_converter(|c| c.convert_wheel_data(val))
|
||||
}
|
||||
}
|
||||
|
||||
mod animation;
|
||||
mod clipboard;
|
||||
mod composition;
|
||||
@@ -151,8 +341,6 @@ pub fn event_bubbles(evt: &str) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
use std::future::Future;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub trait EventReturn<P>: Sized {
|
||||
fn spawn(self, _cx: &dioxus_core::ScopeState) {}
|
||||
@@ -164,7 +352,7 @@ pub struct AsyncMarker;
|
||||
|
||||
impl<T> EventReturn<AsyncMarker> for T
|
||||
where
|
||||
T: Future<Output = ()> + 'static,
|
||||
T: std::future::Future<Output = ()> + 'static,
|
||||
{
|
||||
#[inline]
|
||||
fn spawn(self, cx: &dioxus_core::ScopeState) {
|
||||
|
||||
Reference in new issue
Block a user