wip: fix some event stuff for web and core

This commit is contained in:
Jonathan Kelley committed 2021-09-02 00:37:57 -04:00
1 parent dfe9f86bff
commit 725b4a1d7f
8 files changed
+101 -57

No files matched your search

+1 -1
View File
@@ -19,7 +19,7 @@ dioxus-mobile = { path = "./packages/mobile", optional = true }
[features] [features]
# core # core
default = ["core", "ssr", "web"] default = ["core", "ssr"]
core = ["macro", "hooks", "html"] core = ["macro", "hooks", "html"]
macro = ["dioxus-core-macro"] macro = ["dioxus-core-macro"]
hooks = ["dioxus-hooks"] hooks = ["dioxus-hooks"]
+24 -26
View File
@@ -5,8 +5,17 @@
//! be heavy or need to interact through FFI, so the events themselves are designed to be lazy. //! be heavy or need to interact through FFI, so the events themselves are designed to be lazy.
use crate::innerlude::{ElementId, ScopeId}; use crate::innerlude::{ElementId, ScopeId};
use bumpalo::boxed::Box as BumpBox;
use std::{any::Any, cell::RefCell, fmt::Debug, ops::Deref, rc::Rc};
use crate::{
innerlude::NodeFactory,
innerlude::{Attribute, Listener, VNode},
};
use std::cell::Cell;
#[derive(Debug)] #[derive(Debug)]
pub struct UiEvent { pub struct UserEvent {
/// The originator of the event trigger /// The originator of the event trigger
pub scope: ScopeId, pub scope: ScopeId,
@@ -23,22 +32,23 @@ pub struct UiEvent {
} }
pub enum SyntheticEvent { pub enum SyntheticEvent {
KeyboardEvent(on::KeyboardEvent), AnimationEvent(on::AnimationEvent),
TouchEvent(on::TouchEvent),
MouseEvent(on::MouseEvent),
ClipboardEvent(on::ClipboardEvent), ClipboardEvent(on::ClipboardEvent),
CompositionEvent(on::CompositionEvent), CompositionEvent(on::CompositionEvent),
FocusEvent(on::FocusEvent), FocusEvent(on::FocusEvent),
FormEvent(on::FormEvent), FormEvent(on::FormEvent),
SelectionEvent(on::SelectionEvent), KeyboardEvent(on::KeyboardEvent),
WheelEvent(on::WheelEvent), GenericEvent(on::GenericEvent),
MediaEvent(on::MediaEvent), TouchEvent(on::TouchEvent),
AnimationEvent(on::AnimationEvent),
TransitionEvent(on::TransitionEvent),
ToggleEvent(on::ToggleEvent), ToggleEvent(on::ToggleEvent),
MediaEvent(on::MediaEvent),
MouseEvent(on::MouseEvent),
WheelEvent(on::WheelEvent),
SelectionEvent(on::SelectionEvent),
TransitionEvent(on::TransitionEvent),
PointerEvent(on::PointerEvent), PointerEvent(on::PointerEvent),
// ImageEvent(event_data::ImageEvent),
} }
// ImageEvent(event_data::ImageEvent),
impl std::fmt::Debug for SyntheticEvent { impl std::fmt::Debug for SyntheticEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -57,6 +67,7 @@ impl std::fmt::Debug for SyntheticEvent {
SyntheticEvent::ToggleEvent(_) => "ToggleEvent", SyntheticEvent::ToggleEvent(_) => "ToggleEvent",
SyntheticEvent::MouseEvent(_) => "MouseEvent", SyntheticEvent::MouseEvent(_) => "MouseEvent",
SyntheticEvent::PointerEvent(_) => "PointerEvent", SyntheticEvent::PointerEvent(_) => "PointerEvent",
SyntheticEvent::GenericEvent(_) => "GenericEvent",
}; };
f.debug_struct("VirtualEvent").field("type", &name).finish() f.debug_struct("VirtualEvent").field("type", &name).finish()
@@ -71,18 +82,7 @@ pub mod on {
//! Arc allocation through "get_mut" //! Arc allocation through "get_mut"
//! //!
//! React recently dropped support for re-using event allocation and just passes the real event along. //! React recently dropped support for re-using event allocation and just passes the real event along.
use super::*;
#![allow(unused)]
use bumpalo::boxed::Box as BumpBox;
use std::{any::Any, cell::RefCell, fmt::Debug, ops::Deref, rc::Rc};
use crate::{
innerlude::NodeFactory,
innerlude::{Attribute, ElementId, Listener, VNode},
};
use std::cell::Cell;
use super::SyntheticEvent;
macro_rules! event_directory { macro_rules! event_directory {
( $( ( $(
@@ -409,12 +409,10 @@ pub mod on {
/// ///
ontoggle ontoggle
]; ];
GenericEventInner(GenericEvent): [
];
} }
pub struct GenericEvent(pub Rc<dyn GenericEventInner>);
pub trait GenericEventInner { pub trait GenericEventInner {
/// Return a reference to the raw event. User will need to downcast the event to the right platform-specific type. /// Return a reference to the raw event. User will need to downcast the event to the right platform-specific type.
fn raw_event(&self) -> &dyn Any; fn raw_event(&self) -> &dyn Any;
+1 -1
View File
@@ -58,7 +58,7 @@ pub(crate) mod innerlude {
pub use crate::innerlude::{ pub use crate::innerlude::{
format_args_f, html, rsx, Context, DiffInstruction, DioxusElement, DomEdit, DomTree, ElementId, format_args_f, html, rsx, Context, DiffInstruction, DioxusElement, DomEdit, DomTree, ElementId,
EventPriority, LazyNodes, MountType, Mutations, NodeFactory, Properties, ScopeId, EventPriority, LazyNodes, MountType, Mutations, NodeFactory, Properties, ScopeId,
SuspendedContext, SyntheticEvent, TestDom, UiEvent, VNode, VirtualDom, FC, SuspendedContext, SyntheticEvent, TestDom, UserEvent, VNode, VirtualDom, FC,
}; };
pub mod prelude { pub mod prelude {
+2 -17
View File
@@ -1,5 +1,6 @@
//! Instructions returned by the VirtualDOM on how to modify the Real DOM. //! Instructions returned by the VirtualDOM on how to modify the Real DOM.
//! //!
//! This module contains an internal API to generate these instructions.
use crate::innerlude::*; use crate::innerlude::*;
use std::any::Any; use std::any::Any;
@@ -102,30 +103,14 @@ impl<'a> Mutations<'a> {
let Attribute { let Attribute {
name, name,
value, value,
is_static,
is_volatile,
namespace, namespace,
} = attribute;
self.edits.push(SetAttribute {
field: name,
value,
ns: *namespace,
});
}
pub(crate) fn set_attribute_ns(&mut self, attribute: &'a Attribute, namespace: &'a str) {
let Attribute {
name,
value,
is_static,
is_volatile,
.. ..
} = attribute; } = attribute;
self.edits.push(SetAttribute { self.edits.push(SetAttribute {
field: name, field: name,
value, value,
ns: Some(namespace), ns: *namespace,
}); });
} }
+4 -3
View File
@@ -97,7 +97,7 @@ pub struct EventChannel {
pub enum SchedulerMsg { pub enum SchedulerMsg {
Immediate(ScopeId), Immediate(ScopeId),
UiEvent(UiEvent), UiEvent(UserEvent),
SubmitTask(FiberTask, u64), SubmitTask(FiberTask, u64),
ToggleTask(u64), ToggleTask(u64),
PauseTask(u64), PauseTask(u64),
@@ -140,11 +140,11 @@ pub(crate) struct Scheduler {
// scheduler stuff // scheduler stuff
pub current_priority: EventPriority, pub current_priority: EventPriority,
pub ui_events: VecDeque<UiEvent>, pub ui_events: VecDeque<UserEvent>,
pub pending_immediates: VecDeque<ScopeId>, pub pending_immediates: VecDeque<ScopeId>,
pub pending_tasks: VecDeque<UiEvent>, pub pending_tasks: VecDeque<UserEvent>,
pub garbage_scopes: HashSet<ScopeId>, pub garbage_scopes: HashSet<ScopeId>,
@@ -266,6 +266,7 @@ impl Scheduler {
SyntheticEvent::ToggleEvent(_) => {} SyntheticEvent::ToggleEvent(_) => {}
SyntheticEvent::MouseEvent(_) => {} SyntheticEvent::MouseEvent(_) => {}
SyntheticEvent::PointerEvent(_) => {} SyntheticEvent::PointerEvent(_) => {}
SyntheticEvent::GenericEvent(_) => {}
}; };
scope.call_listener(trigger.event, element); scope.call_listener(trigger.event, element);
+3 -3
View File
@@ -8,7 +8,7 @@ use dioxus_core::{
on::{MouseEvent, MouseEventInner}, on::{MouseEvent, MouseEventInner},
SyntheticEvent, SyntheticEvent,
}, },
ElementId, EventPriority, ScopeId, UiEvent, ElementId, EventPriority, ScopeId, UserEvent,
}; };
#[derive(serde::Serialize, serde::Deserialize)] #[derive(serde::Serialize, serde::Deserialize)]
@@ -17,14 +17,14 @@ struct ImEvent {
mounted_dom_id: u64, mounted_dom_id: u64,
scope: u64, scope: u64,
} }
pub fn trigger_from_serialized(val: serde_json::Value) -> UiEvent { pub fn trigger_from_serialized(val: serde_json::Value) -> UserEvent {
let mut data: Vec<ImEvent> = serde_json::from_value(val).unwrap(); let mut data: Vec<ImEvent> = serde_json::from_value(val).unwrap();
let data = data.drain(..).next().unwrap(); let data = data.drain(..).next().unwrap();
let event = SyntheticEvent::MouseEvent(MouseEvent(Rc::new(WebviewMouseEvent))); let event = SyntheticEvent::MouseEvent(MouseEvent(Rc::new(WebviewMouseEvent)));
let scope = ScopeId(data.scope as usize); let scope = ScopeId(data.scope as usize);
let mounted_dom_id = Some(ElementId(data.mounted_dom_id as usize)); let mounted_dom_id = Some(ElementId(data.mounted_dom_id as usize));
UiEvent { UserEvent {
name: todo!(), name: todo!(),
event, event,
scope, scope,
+6 -6
View File
@@ -1,7 +1,7 @@
use std::{collections::HashMap, fmt::Debug, rc::Rc, sync::Arc}; use std::{collections::HashMap, fmt::Debug, rc::Rc, sync::Arc};
use dioxus_core::{ use dioxus_core::{
events::{on::GenericEventInner, SyntheticEvent, UiEvent}, events::{on::GenericEventInner, SyntheticEvent, UserEvent},
mutations::NodeRefMutation, mutations::NodeRefMutation,
scheduler::SchedulerMsg, scheduler::SchedulerMsg,
DomEdit, ElementId, ScopeId, DomEdit, ElementId, ScopeId,
@@ -458,7 +458,7 @@ fn virtual_event_from_websys_event(event: web_sys::Event) -> SyntheticEvent {
} }
"change" => { "change" => {
let evt = event.dyn_into().unwrap(); let evt = event.dyn_into().unwrap();
SyntheticEvent::UIEvent(UIEvent(Rc::new(WebsysGenericUiEvent(evt)))) SyntheticEvent::GenericEvent(GenericEvent(Rc::new(WebsysGenericUiEvent(evt))))
} }
"input" | "invalid" | "reset" | "submit" => { "input" | "invalid" | "reset" | "submit" => {
let evt: web_sys::InputEvent = event.clone().dyn_into().unwrap(); let evt: web_sys::InputEvent = event.clone().dyn_into().unwrap();
@@ -485,7 +485,7 @@ fn virtual_event_from_websys_event(event: web_sys::Event) -> SyntheticEvent {
} }
"scroll" => { "scroll" => {
let evt: web_sys::UiEvent = event.clone().dyn_into().unwrap(); let evt: web_sys::UiEvent = event.clone().dyn_into().unwrap();
SyntheticEvent::UIEvent(UIEvent(Rc::new(WebsysGenericUiEvent(evt)))) SyntheticEvent::GenericEvent(GenericEvent(Rc::new(WebsysGenericUiEvent(evt))))
} }
"wheel" => { "wheel" => {
let evt: web_sys::WheelEvent = event.clone().dyn_into().unwrap(); let evt: web_sys::WheelEvent = event.clone().dyn_into().unwrap();
@@ -512,14 +512,14 @@ fn virtual_event_from_websys_event(event: web_sys::Event) -> SyntheticEvent {
} }
_ => { _ => {
let evt: web_sys::UiEvent = event.clone().dyn_into().unwrap(); let evt: web_sys::UiEvent = event.clone().dyn_into().unwrap();
SyntheticEvent::UIEvent(UIEvent(Rc::new(WebsysGenericUiEvent(evt)))) SyntheticEvent::GenericEvent(GenericEvent(Rc::new(WebsysGenericUiEvent(evt))))
} }
} }
} }
/// This function decodes a websys event and produces an EventTrigger /// This function decodes a websys event and produces an EventTrigger
/// With the websys implementation, we attach a unique key to the nodes /// With the websys implementation, we attach a unique key to the nodes
fn decode_trigger(event: &web_sys::Event) -> anyhow::Result<UiEvent> { fn decode_trigger(event: &web_sys::Event) -> anyhow::Result<UserEvent> {
log::debug!("Handling event!"); log::debug!("Handling event!");
let target = event let target = event
@@ -561,7 +561,7 @@ fn decode_trigger(event: &web_sys::Event) -> anyhow::Result<UiEvent> {
let triggered_scope = gi_id; let triggered_scope = gi_id;
// let triggered_scope: ScopeId = KeyData::from_ffi(gi_id).into(); // let triggered_scope: ScopeId = KeyData::from_ffi(gi_id).into();
log::debug!("Triggered scope is {:#?}", triggered_scope); log::debug!("Triggered scope is {:#?}", triggered_scope);
Ok(UiEvent { Ok(UserEvent {
name: event_name_from_typ(&typ), name: event_name_from_typ(&typ),
event: virtual_event_from_websys_event(event.clone()), event: virtual_event_from_websys_event(event.clone()),
mounted_dom_id: Some(ElementId(real_id as usize)), mounted_dom_id: Some(ElementId(real_id as usize)),
+60
View File
@@ -171,11 +171,71 @@ impl KeyboardEventInner for WebsysKeyboardEvent {
} }
pub struct WebsysGenericUiEvent(pub UiEvent); pub struct WebsysGenericUiEvent(pub UiEvent);
impl GenericEventInner for WebsysGenericUiEvent {
fn raw_event(&self) -> &dyn std::any::Any {
// self.0.raw_event()
todo!()
}
fn bubbles(&self) -> bool {
self.0.bubbles()
}
fn cancel_bubble(&self) {
self.0.cancel_bubble();
}
fn cancelable(&self) -> bool {
self.0.cancelable()
}
fn composed(&self) -> bool {
self.0.composed()
}
fn current_target(&self) {
// self.0.current_target()
}
fn default_prevented(&self) -> bool {
self.0.default_prevented()
}
fn event_phase(&self) -> u16 {
self.0.event_phase()
}
fn is_trusted(&self) -> bool {
self.0.is_trusted()
}
fn prevent_default(&self) {
self.0.prevent_default()
}
fn stop_immediate_propagation(&self) {
self.0.stop_immediate_propagation()
}
fn stop_propagation(&self) {
self.0.stop_propagation()
}
fn target(&self) {
// self.0.target()
}
fn time_stamp(&self) -> f64 {
self.0.time_stamp()
}
}
impl UIEventInner for WebsysGenericUiEvent { impl UIEventInner for WebsysGenericUiEvent {
fn detail(&self) -> i32 { fn detail(&self) -> i32 {
todo!() todo!()
} }
} }
impl SelectionEventInner for WebsysGenericUiEvent {} impl SelectionEventInner for WebsysGenericUiEvent {}
pub struct WebsysFocusEvent(pub web_sys::FocusEvent); pub struct WebsysFocusEvent(pub web_sys::FocusEvent);