wip: fix some event stuff for web and core

This commit is contained in:
Jonathan Kelley 2021-09-02 00:37:57 -04:00
parent dfe9f86bff
commit 725b4a1d7f
8 changed files with 101 additions and 57 deletions

View file

@ -19,7 +19,7 @@ dioxus-mobile = { path = "./packages/mobile", optional = true }
[features]
# core
default = ["core", "ssr", "web"]
default = ["core", "ssr"]
core = ["macro", "hooks", "html"]
macro = ["dioxus-core-macro"]
hooks = ["dioxus-hooks"]

View file

@ -5,8 +5,17 @@
//! be heavy or need to interact through FFI, so the events themselves are designed to be lazy.
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)]
pub struct UiEvent {
pub struct UserEvent {
/// The originator of the event trigger
pub scope: ScopeId,
@ -23,22 +32,23 @@ pub struct UiEvent {
}
pub enum SyntheticEvent {
KeyboardEvent(on::KeyboardEvent),
TouchEvent(on::TouchEvent),
MouseEvent(on::MouseEvent),
AnimationEvent(on::AnimationEvent),
ClipboardEvent(on::ClipboardEvent),
CompositionEvent(on::CompositionEvent),
FocusEvent(on::FocusEvent),
FormEvent(on::FormEvent),
SelectionEvent(on::SelectionEvent),
WheelEvent(on::WheelEvent),
MediaEvent(on::MediaEvent),
AnimationEvent(on::AnimationEvent),
TransitionEvent(on::TransitionEvent),
KeyboardEvent(on::KeyboardEvent),
GenericEvent(on::GenericEvent),
TouchEvent(on::TouchEvent),
ToggleEvent(on::ToggleEvent),
MediaEvent(on::MediaEvent),
MouseEvent(on::MouseEvent),
WheelEvent(on::WheelEvent),
SelectionEvent(on::SelectionEvent),
TransitionEvent(on::TransitionEvent),
PointerEvent(on::PointerEvent),
// ImageEvent(event_data::ImageEvent),
}
// ImageEvent(event_data::ImageEvent),
impl std::fmt::Debug for SyntheticEvent {
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::MouseEvent(_) => "MouseEvent",
SyntheticEvent::PointerEvent(_) => "PointerEvent",
SyntheticEvent::GenericEvent(_) => "GenericEvent",
};
f.debug_struct("VirtualEvent").field("type", &name).finish()
@ -71,18 +82,7 @@ pub mod on {
//! Arc allocation through "get_mut"
//!
//! React recently dropped support for re-using event allocation and just passes the real event along.
#![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;
use super::*;
macro_rules! event_directory {
( $(
@ -409,12 +409,10 @@ pub mod on {
///
ontoggle
];
GenericEventInner(GenericEvent): [
];
}
pub struct GenericEvent(pub Rc<dyn GenericEventInner>);
pub trait GenericEventInner {
/// 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;

View file

@ -58,7 +58,7 @@ pub(crate) mod innerlude {
pub use crate::innerlude::{
format_args_f, html, rsx, Context, DiffInstruction, DioxusElement, DomEdit, DomTree, ElementId,
EventPriority, LazyNodes, MountType, Mutations, NodeFactory, Properties, ScopeId,
SuspendedContext, SyntheticEvent, TestDom, UiEvent, VNode, VirtualDom, FC,
SuspendedContext, SyntheticEvent, TestDom, UserEvent, VNode, VirtualDom, FC,
};
pub mod prelude {

View file

@ -1,5 +1,6 @@
//! 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 std::any::Any;
@ -102,30 +103,14 @@ impl<'a> Mutations<'a> {
let Attribute {
name,
value,
is_static,
is_volatile,
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;
self.edits.push(SetAttribute {
field: name,
value,
ns: Some(namespace),
ns: *namespace,
});
}

View file

@ -97,7 +97,7 @@ pub struct EventChannel {
pub enum SchedulerMsg {
Immediate(ScopeId),
UiEvent(UiEvent),
UiEvent(UserEvent),
SubmitTask(FiberTask, u64),
ToggleTask(u64),
PauseTask(u64),
@ -140,11 +140,11 @@ pub(crate) struct Scheduler {
// scheduler stuff
pub current_priority: EventPriority,
pub ui_events: VecDeque<UiEvent>,
pub ui_events: VecDeque<UserEvent>,
pub pending_immediates: VecDeque<ScopeId>,
pub pending_tasks: VecDeque<UiEvent>,
pub pending_tasks: VecDeque<UserEvent>,
pub garbage_scopes: HashSet<ScopeId>,
@ -266,6 +266,7 @@ impl Scheduler {
SyntheticEvent::ToggleEvent(_) => {}
SyntheticEvent::MouseEvent(_) => {}
SyntheticEvent::PointerEvent(_) => {}
SyntheticEvent::GenericEvent(_) => {}
};
scope.call_listener(trigger.event, element);

View file

@ -8,7 +8,7 @@ use dioxus_core::{
on::{MouseEvent, MouseEventInner},
SyntheticEvent,
},
ElementId, EventPriority, ScopeId, UiEvent,
ElementId, EventPriority, ScopeId, UserEvent,
};
#[derive(serde::Serialize, serde::Deserialize)]
@ -17,14 +17,14 @@ struct ImEvent {
mounted_dom_id: 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 data = data.drain(..).next().unwrap();
let event = SyntheticEvent::MouseEvent(MouseEvent(Rc::new(WebviewMouseEvent)));
let scope = ScopeId(data.scope as usize);
let mounted_dom_id = Some(ElementId(data.mounted_dom_id as usize));
UiEvent {
UserEvent {
name: todo!(),
event,
scope,

View file

@ -1,7 +1,7 @@
use std::{collections::HashMap, fmt::Debug, rc::Rc, sync::Arc};
use dioxus_core::{
events::{on::GenericEventInner, SyntheticEvent, UiEvent},
events::{on::GenericEventInner, SyntheticEvent, UserEvent},
mutations::NodeRefMutation,
scheduler::SchedulerMsg,
DomEdit, ElementId, ScopeId,
@ -458,7 +458,7 @@ fn virtual_event_from_websys_event(event: web_sys::Event) -> SyntheticEvent {
}
"change" => {
let evt = event.dyn_into().unwrap();
SyntheticEvent::UIEvent(UIEvent(Rc::new(WebsysGenericUiEvent(evt))))
SyntheticEvent::GenericEvent(GenericEvent(Rc::new(WebsysGenericUiEvent(evt))))
}
"input" | "invalid" | "reset" | "submit" => {
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" => {
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" => {
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();
SyntheticEvent::UIEvent(UIEvent(Rc::new(WebsysGenericUiEvent(evt))))
SyntheticEvent::GenericEvent(GenericEvent(Rc::new(WebsysGenericUiEvent(evt))))
}
}
}
/// This function decodes a websys event and produces an EventTrigger
/// 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!");
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: ScopeId = KeyData::from_ffi(gi_id).into();
log::debug!("Triggered scope is {:#?}", triggered_scope);
Ok(UiEvent {
Ok(UserEvent {
name: event_name_from_typ(&typ),
event: virtual_event_from_websys_event(event.clone()),
mounted_dom_id: Some(ElementId(real_id as usize)),

View file

@ -171,11 +171,71 @@ impl KeyboardEventInner for WebsysKeyboardEvent {
}
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 {
fn detail(&self) -> i32 {
todo!()
}
}
impl SelectionEventInner for WebsysGenericUiEvent {}
pub struct WebsysFocusEvent(pub web_sys::FocusEvent);