diff --git a/Cargo.toml b/Cargo.toml index e6b07eb8cd..60bdfb3026 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,7 +58,7 @@ dioxus-core = { path = "packages/core", version = "0.4.2" } dioxus-core-macro = { path = "packages/core-macro", version = "0.4.0" } dioxus-router = { path = "packages/router", version = "0.4.1" } dioxus-router-macro = { path = "packages/router-macro", version = "0.4.1" } -dioxus-html = { path = "packages/html", version = "0.4.0" } +dioxus-html = { path = "packages/html", default-features = false, version = "0.4.0" } dioxus-hooks = { path = "packages/hooks", version = "0.4.0" } dioxus-web = { path = "packages/web", version = "0.4.0" } dioxus-ssr = { path = "packages/ssr", version = "0.4.0" } diff --git a/examples/compose.rs b/examples/compose.rs index 3ee6d36ae3..5f5b9ebc16 100644 --- a/examples/compose.rs +++ b/examples/compose.rs @@ -73,7 +73,7 @@ fn compose(cx: Scope) -> Element { input { oninput: move |e| { - user_input.set(e.value.clone()); + user_input.set(e.value()); }, value: "{user_input}" } diff --git a/examples/counter.rs b/examples/counter.rs index f51083a957..1de77dc38e 100644 --- a/examples/counter.rs +++ b/examples/counter.rs @@ -22,7 +22,7 @@ fn app(cx: Scope) -> Element { input { value: "{counter}", oninput: move |e| { - if let Ok(value) = e.value.parse::() { + if let Ok(value) = e.value().parse::() { counters.make_mut()[i] = value; } } diff --git a/examples/crm.rs b/examples/crm.rs index d2cd981181..214bea3318 100644 --- a/examples/crm.rs +++ b/examples/crm.rs @@ -119,7 +119,7 @@ fn ClientAdd(cx: Scope) -> Element { placeholder: "First Name…", required: "", value: "{first_name}", - oninput: move |e| first_name.set(e.value.clone()) + oninput: move |e| first_name.set(e.value()) } } @@ -135,7 +135,7 @@ fn ClientAdd(cx: Scope) -> Element { placeholder: "Last Name…", required: "", value: "{last_name}", - oninput: move |e| last_name.set(e.value.clone()) + oninput: move |e| last_name.set(e.value()) } } @@ -149,7 +149,7 @@ fn ClientAdd(cx: Scope) -> Element { id: "description", placeholder: "Description…", value: "{description}", - oninput: move |e| description.set(e.value.clone()) + oninput: move |e| description.set(e.value()) } } diff --git a/examples/file_upload.rs b/examples/file_upload.rs index c7358c685d..882285ddf0 100644 --- a/examples/file_upload.rs +++ b/examples/file_upload.rs @@ -16,7 +16,7 @@ fn App(cx: Scope) -> Element { r#type: "checkbox", checked: "{enable_directory_upload}", oninput: move |evt| { - enable_directory_upload.set(evt.value.parse().unwrap()); + enable_directory_upload.set(evt.value().parse().unwrap()); }, }, "Enable directory upload" @@ -30,7 +30,7 @@ fn App(cx: Scope) -> Element { onchange: |evt| { to_owned![files_uploaded]; async move { - if let Some(file_engine) = &evt.files { + if let Some(file_engine) = &evt.files() { let files = file_engine.files(); for file_name in files { sleep(std::time::Duration::from_secs(1)).await; diff --git a/examples/form.rs b/examples/form.rs index a7de142f32..897ffc7108 100644 --- a/examples/form.rs +++ b/examples/form.rs @@ -14,8 +14,8 @@ fn app(cx: Scope) -> Element { div { h1 { "Form" } form { - onsubmit: move |ev| println!("Submitted {:?}", ev.values), - oninput: move |ev| println!("Input {:?}", ev.values), + onsubmit: move |ev| println!("Submitted {:?}", ev.values()), + oninput: move |ev| println!("Input {:?}", ev.values()), input { r#type: "text", name: "username" } input { r#type: "text", name: "full-name" } input { r#type: "password", name: "password" } diff --git a/examples/login_form.rs b/examples/login_form.rs index 5ee47e4b5c..d7371dc500 100644 --- a/examples/login_form.rs +++ b/examples/login_form.rs @@ -13,8 +13,8 @@ fn app(cx: Scope) -> Element { let resp = reqwest::Client::new() .post("http://localhost:8080/login") .form(&[ - ("username", &evt.values["username"]), - ("password", &evt.values["password"]), + ("username", &evt.values()["username"]), + ("password", &evt.values()["password"]), ]) .send() .await; diff --git a/examples/shared_state.rs b/examples/shared_state.rs index 37271f160d..487aa3fe3e 100644 --- a/examples/shared_state.rs +++ b/examples/shared_state.rs @@ -64,7 +64,7 @@ fn DataEditor(cx: Scope, id: usize) -> Element { fn DataView(cx: Scope, id: usize) -> Element { let cool_data = use_shared_state::(cx).unwrap(); - let oninput = |e: FormEvent| cool_data.write().set(*id, e.value.clone()); + let oninput = |e: FormEvent| cool_data.write().set(*id, e.value()); let cool_data = cool_data.read(); let my_data = &cool_data.view(id).unwrap(); diff --git a/examples/textarea.rs b/examples/textarea.rs index df684aa85a..62afc45bbc 100644 --- a/examples/textarea.rs +++ b/examples/textarea.rs @@ -17,7 +17,7 @@ fn app(cx: Scope) -> Element { rows: "10", cols: "80", value: "{model}", - oninput: move |e| model.set(e.value.clone()), + oninput: move |e| model.set(e.value().clone()), } }) } diff --git a/examples/todomvc.rs b/examples/todomvc.rs index 4b25507a58..d5dff22e8f 100644 --- a/examples/todomvc.rs +++ b/examples/todomvc.rs @@ -66,7 +66,7 @@ pub fn app(cx: Scope<()>) -> Element { value: "{draft}", autofocus: "true", oninput: move |evt| { - draft.set(evt.value.clone()); + draft.set(evt.value()); }, onkeydown: move |evt| { if evt.key() == Key::Enter && !draft.is_empty() { @@ -176,7 +176,7 @@ pub fn TodoEntry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element { id: "cbg-{todo.id}", checked: "{todo.checked}", oninput: move |evt| { - cx.props.todos.make_mut()[&cx.props.id].checked = evt.value.parse().unwrap(); + cx.props.todos.make_mut()[&cx.props.id].checked = evt.value().parse().unwrap(); } } label { @@ -195,7 +195,7 @@ pub fn TodoEntry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element { input { class: "edit", value: "{todo.contents}", - oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].contents = evt.value.clone(), + oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].contents = evt.value(), autofocus: "true", onfocusout: move |_| is_editing.set(false), onkeydown: move |evt| { diff --git a/examples/window_zoom.rs b/examples/window_zoom.rs index e45d71be24..3c4edbb907 100644 --- a/examples/window_zoom.rs +++ b/examples/window_zoom.rs @@ -14,7 +14,7 @@ fn app(cx: Scope) -> Element { r#type: "number", value: "{level}", oninput: |e| { - if let Ok(new_zoom) = e.value.parse::() { + if let Ok(new_zoom) = e.value().parse::() { level.set(new_zoom); window.webview.zoom(new_zoom); } diff --git a/examples/xss_safety.rs b/examples/xss_safety.rs index 5272d661b2..73da2ff14f 100644 --- a/examples/xss_safety.rs +++ b/examples/xss_safety.rs @@ -20,7 +20,7 @@ fn app(cx: Scope) -> Element { input { value: "{contents}", r#type: "text", - oninput: move |e| contents.set(e.value.clone()), + oninput: move |e| contents.set(e.value()), } } }) diff --git a/packages/autofmt/src/component.rs b/packages/autofmt/src/component.rs index 8cf36811db..8719fcf346 100644 --- a/packages/autofmt/src/component.rs +++ b/packages/autofmt/src/component.rs @@ -182,11 +182,32 @@ impl Writer<'_> { s.source.as_ref().unwrap().to_token_stream() )?; } - ContentField::OnHandlerRaw(exp) => { - let out = prettyplease::unparse_expr(exp); + ContentField::OnHandlerRaw { handler, metadata } => { + let out = prettyplease::unparse_expr(handler); let mut lines = out.split('\n').peekable(); + write!(self.out, "{name}")?; + if let Some(metadata) = metadata { + write!(self.out, "[")?; + for (index, expr) in metadata.iter().enumerate() { + let is_last = index == metadata.len() - 1; + + let out = prettyplease::unparse_expr(expr); + let mut lines = out.split('\n').peekable(); + let first = lines.next().unwrap(); + write!(self.out, "{first}")?; + for line in lines { + self.out.new_line()?; + self.out.indented_tab()?; + write!(self.out, "{line}")?; + } + if !is_last { + write!(self.out, ", ")?; + } + } + write!(self.out, "]")?; + } let first = lines.next().unwrap(); - write!(self.out, "{name}: {first}")?; + write!(self.out, ": {first}")?; for line in lines { self.out.new_line()?; self.out.indented_tab()?; @@ -223,7 +244,31 @@ impl Writer<'_> { .iter() .map(|field| match &field.content { ContentField::Formatted(s) => ifmt_to_string(s).len() , - ContentField::OnHandlerRaw(exp) | ContentField::ManExpr(exp) => { + ContentField::OnHandlerRaw { handler, metadata} => { + let formatted = prettyplease::unparse_expr(handler); + let mut len= if formatted.contains('\n') { + 10000 + } else { + formatted.len() + }; + self.cached_formats.insert(Location::new(handler.span().start()) , formatted); + + if let Some(metadata) = metadata{ + for expr in metadata { + let formatted = prettyplease::unparse_expr(expr); + len += if formatted.contains('\n') { + 10000 + } else { + formatted.len() + }; + self.cached_formats.insert(Location::new(expr.span().start()) , formatted); + len += 2; + } + } + + len + }, + ContentField::ManExpr(exp) => { let formatted = prettyplease::unparse_expr(exp); let len = if formatted.contains('\n') { 10000 diff --git a/packages/autofmt/src/element.rs b/packages/autofmt/src/element.rs index dbc336401d..635235f2b4 100644 --- a/packages/autofmt/src/element.rs +++ b/packages/autofmt/src/element.rs @@ -252,7 +252,11 @@ impl Writer<'_> { write!(self.out, "{}: {}", name.to_token_stream(), out)?; } - ElementAttr::EventTokens { name, tokens } => { + ElementAttr::EventTokens { + name, + tokens, + metadata, + } => { let out = self.retrieve_formatted_expr(tokens).to_string(); let mut lines = out.split('\n').peekable(); @@ -260,20 +264,34 @@ impl Writer<'_> { // a one-liner for whatever reason // Does not need a new line - if lines.peek().is_none() { - write!(self.out, "{name}: {first}")?; - } else { - writeln!(self.out, "{name}: {first}")?; - - while let Some(line) = lines.next() { - self.out.indented_tab()?; - write!(self.out, "{line}")?; - if lines.peek().is_none() { - write!(self.out, "")?; - } else { - writeln!(self.out)?; + if lines.peek().is_some() { + writeln!(self.out, "")?; + } + write!(self.out, "{name}")?; + if let Some(metadata) = metadata { + write!(self.out, "[")?; + for expr in metadata { + let out = self.retrieve_formatted_expr(expr).to_string(); + let mut lines = out.split('\n').peekable(); + let first = lines.next().unwrap(); + write!(self.out, "{first}")?; + for line in lines { + self.out.indented_tab()?; + write!(self.out, "{line}")?; } } + write!(self.out, "]")?; + } + write!(self.out, ": {first}")?; + + while let Some(line) = lines.next() { + self.out.indented_tab()?; + write!(self.out, "{line}")?; + if lines.peek().is_none() { + write!(self.out, "")?; + } else { + writeln!(self.out)?; + } } } } diff --git a/packages/autofmt/src/writer.rs b/packages/autofmt/src/writer.rs index 59f1355922..a32fc05ea4 100644 --- a/packages/autofmt/src/writer.rs +++ b/packages/autofmt/src/writer.rs @@ -159,10 +159,14 @@ impl<'a> Writer<'a> { ElementAttr::CustomAttrExpression { name, value } => { name.to_token_stream().to_string().len() + value.span().line_length() + 6 } - ElementAttr::EventTokens { tokens, name } => { + ElementAttr::EventTokens { + tokens, + name, + metadata, + } => { let location = Location::new(tokens.span().start()); - let len = if let std::collections::hash_map::Entry::Vacant(e) = + let mut len = if let std::collections::hash_map::Entry::Vacant(e) = self.cached_formats.entry(location) { let formatted = prettyplease::unparse_expr(tokens); @@ -177,6 +181,28 @@ impl<'a> Writer<'a> { self.cached_formats[&location].len() }; + if let Some(metadata) = metadata { + for expr in metadata { + let location = Location::new(expr.span().start()); + + len += if let std::collections::hash_map::Entry::Vacant(e) = + self.cached_formats.entry(location) + { + let formatted = prettyplease::unparse_expr(expr); + let len = if formatted.contains('\n') { + 10000 + } else { + formatted.len() + }; + e.insert(formatted); + len + } else { + self.cached_formats[&location].len() + }; + len += 2; + } + } + len + name.span().line_length() + 6 } }; diff --git a/packages/core/Cargo.toml b/packages/core/Cargo.toml index ce7c6bccfd..6cc7b3ad3e 100644 --- a/packages/core/Cargo.toml +++ b/packages/core/Cargo.toml @@ -36,6 +36,7 @@ serde = { version = "1", features = ["derive"], optional = true } [dev-dependencies] tokio = { workspace = true, features = ["full"] } dioxus = { workspace = true } +dioxus-html = { workspace = true, features = ["serialize"] } pretty_assertions = "1.3.0" rand = "0.8.5" dioxus-ssr = { workspace = true } diff --git a/packages/core/src/arena.rs b/packages/core/src/arena.rs index b7f768755c..cdc2594459 100644 --- a/packages/core/src/arena.rs +++ b/packages/core/src/arena.rs @@ -169,7 +169,7 @@ impl VirtualDom { let listener = unsafe { &*listener }; match &listener.value { AttributeValue::Listener(l) => { - _ = l.take(); + _ = l.callback.take(); } AttributeValue::Any(a) => { _ = a.take(); diff --git a/packages/core/src/events.rs b/packages/core/src/events.rs index 3b8edb05cd..38ab541604 100644 --- a/packages/core/src/events.rs +++ b/packages/core/src/events.rs @@ -28,6 +28,27 @@ pub struct Event { } impl Event { + /// Map the event data to a new type + /// + /// # Example + /// + /// ```rust, ignore + /// rsx! { + /// button { + /// onclick: move |evt: Event| { + /// let data = evt.map(|data| data.value()); + /// assert_eq!(data.inner(), "hello world"); + /// } + /// } + /// } + /// ``` + pub fn map U>(&self, f: F) -> Event { + Event { + data: Rc::new(f(&self.data)), + propagates: self.propagates.clone(), + } + } + /// Prevent this event from continuing to bubble up the tree to parent elements. /// /// # Example @@ -135,15 +156,23 @@ impl std::fmt::Debug for Event { /// } /// /// ``` -pub struct EventHandler<'bump, T = ()> { +pub struct EventHandler<'bump, T: ?Sized = (), M = ()> { pub(crate) origin: ScopeId, + pub(crate) metadata: M, pub(super) callback: RefCell>>, } -impl Default for EventHandler<'_, T> { +impl EventHandler<'_, T, M> { + pub fn metadata(&self) -> &M { + &self.metadata + } +} + +impl Default for EventHandler<'_, T, M> { fn default() -> Self { Self { origin: ScopeId::ROOT, + metadata: Default::default(), callback: Default::default(), } } diff --git a/packages/core/src/nodes.rs b/packages/core/src/nodes.rs index c83b1059aa..9e297e4470 100644 --- a/packages/core/src/nodes.rs +++ b/packages/core/src/nodes.rs @@ -1,5 +1,6 @@ use crate::{ - any_props::AnyProps, arena::ElementId, Element, Event, LazyNodes, ScopeId, ScopeState, + any_props::AnyProps, arena::ElementId, innerlude::EventHandler, Element, Event, LazyNodes, + ScopeId, ScopeState, }; use bumpalo::boxed::Box as BumpBox; use bumpalo::Bump; @@ -476,7 +477,7 @@ pub enum AttributeValue<'a> { Bool(bool), /// A listener, like "onclick" - Listener(RefCell>>), + Listener(EventHandler<'a, Event, Box>), /// An arbitrary value that implements PartialEq and is static Any(RefCell>>), @@ -485,8 +486,6 @@ pub enum AttributeValue<'a> { None, } -pub type ListenerCb<'a> = BumpBox<'a, dyn FnMut(Event) + 'a>; - /// Any of the built-in values that the Dioxus VirtualDom supports as dynamic attributes on elements that are borrowed /// /// These varients are used to communicate what the value of an attribute is that needs to be updated diff --git a/packages/core/src/scopes.rs b/packages/core/src/scopes.rs index 996529a0d0..b4a6c53ddb 100644 --- a/packages/core/src/scopes.rs +++ b/packages/core/src/scopes.rs @@ -460,12 +460,17 @@ impl<'src> ScopeState { } /// Create a new [`EventHandler`] from an [`FnMut`] - pub fn event_handler(&'src self, f: impl FnMut(T) + 'src) -> EventHandler<'src, T> { + pub fn event_handler( + &'src self, + f: impl FnMut(T) + 'src, + metadata: M, + ) -> EventHandler<'src, T, M> { let handler: &mut dyn FnMut(T) = self.bump().alloc(f); let caller = unsafe { BumpBox::from_raw(handler as *mut dyn FnMut(T)) }; let callback = RefCell::new(Some(caller)); EventHandler { callback, + metadata, origin: self.context().id, } } @@ -473,9 +478,10 @@ impl<'src> ScopeState { /// Create a new [`AttributeValue`] with the listener variant from a callback /// /// The callback must be confined to the lifetime of the ScopeState - pub fn listener( + pub fn listener( &'src self, mut callback: impl FnMut(Event) + 'src, + metadata: M, ) -> AttributeValue<'src> { // safety: there's no other way to create a dynamicly-dispatched bump box other than alloc + from-raw // This is the suggested way to build a bumpbox @@ -492,7 +498,11 @@ impl<'src> ScopeState { })) }; - AttributeValue::Listener(RefCell::new(Some(boxed))) + AttributeValue::Listener(EventHandler { + callback: RefCell::new(Some(boxed)), + metadata: Box::new(metadata), + origin: self.context().id, + }) } /// Create a new [`AttributeValue`] with a value that implements [`AnyValue`] diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index 2d83ba63ab..627461e505 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -397,7 +397,7 @@ impl VirtualDom { let origin = el_ref.scope; self.runtime.scope_stack.borrow_mut().push(origin); self.runtime.rendering.set(false); - if let Some(cb) = listener.borrow_mut().as_deref_mut() { + if let Some(cb) = listener.callback.borrow_mut().as_deref_mut() { cb(uievent.clone()); } self.runtime.scope_stack.borrow_mut().pop(); @@ -433,7 +433,7 @@ impl VirtualDom { let origin = el_ref.scope; self.runtime.scope_stack.borrow_mut().push(origin); self.runtime.rendering.set(false); - if let Some(cb) = listener.borrow_mut().as_deref_mut() { + if let Some(cb) = listener.callback.borrow_mut().as_deref_mut() { cb(uievent.clone()); } self.runtime.scope_stack.borrow_mut().pop(); diff --git a/packages/core/tests/fuzzing.rs b/packages/core/tests/fuzzing.rs index 1523c4e224..6eeed19d2d 100644 --- a/packages/core/tests/fuzzing.rs +++ b/packages/core/tests/fuzzing.rs @@ -216,7 +216,7 @@ fn create_random_dynamic_attr(cx: &ScopeState) -> Attribute { 4 => cx.any_value(rand::random::()), 5 => AttributeValue::None, 6 => { - let value = cx.listener(|e: Event| println!("{:?}", e)); + let value = cx.listener(|e: Event| println!("{:?}", e), ()); return Attribute::new("ondata", value, None, false); } _ => unreachable!(), diff --git a/packages/core/tests/lifecycle.rs b/packages/core/tests/lifecycle.rs index 08b3426315..6db294653e 100644 --- a/packages/core/tests/lifecycle.rs +++ b/packages/core/tests/lifecycle.rs @@ -4,6 +4,7 @@ //! Tests for the lifecycle of components. use dioxus::core::{ElementId, Mutation::*}; use dioxus::prelude::*; +use dioxus_html::SerializedHtmlEventConverter; use std::rc::Rc; use std::sync::{Arc, Mutex}; @@ -39,6 +40,7 @@ fn manual_diffing() { #[test] fn events_generate() { + set_event_converter(Box::new(SerializedHtmlEventConverter)); fn app(cx: Scope) -> Element { let count = cx.use_hook(|| 0); @@ -56,7 +58,12 @@ fn events_generate() { let mut dom = VirtualDom::new(app); _ = dom.rebuild(); - dom.handle_event("click", Rc::new(MouseData::default()), ElementId(1), true); + dom.handle_event( + "click", + Rc::new(PlatformEventData::new(Box::::default())), + ElementId(1), + true, + ); dom.mark_dirty(ScopeId::ROOT); let edits = dom.render_immediate(); diff --git a/packages/core/tests/miri_full_app.rs b/packages/core/tests/miri_full_app.rs index 33cbd9620b..c285db7d55 100644 --- a/packages/core/tests/miri_full_app.rs +++ b/packages/core/tests/miri_full_app.rs @@ -1,15 +1,23 @@ +use crate::dioxus_elements::SerializedMouseData; use dioxus::prelude::*; use dioxus_core::ElementId; +use dioxus_elements::SerializedHtmlEventConverter; use std::rc::Rc; #[test] fn miri_rollover() { + set_event_converter(Box::new(SerializedHtmlEventConverter)); let mut dom = VirtualDom::new(App); _ = dom.rebuild(); for _ in 0..3 { - dom.handle_event("click", Rc::new(MouseData::default()), ElementId(2), true); + dom.handle_event( + "click", + Rc::new(PlatformEventData::new(Box::::default())), + ElementId(2), + true, + ); dom.process_events(); _ = dom.render_immediate(); } diff --git a/packages/core/tests/suspense.rs b/packages/core/tests/suspense.rs index 7b25164478..abce7f8a65 100644 --- a/packages/core/tests/suspense.rs +++ b/packages/core/tests/suspense.rs @@ -36,7 +36,7 @@ fn suspended_child(cx: Scope) -> Element { cx.spawn(async move { val += 1; }); - return cx.suspend()?; + cx.suspend()?; } render!("child") diff --git a/packages/desktop/Cargo.toml b/packages/desktop/Cargo.toml index dcb9941204..a2a544e9eb 100644 --- a/packages/desktop/Cargo.toml +++ b/packages/desktop/Cargo.toml @@ -11,7 +11,7 @@ keywords = ["dom", "ui", "gui", "react"] [dependencies] dioxus-core = { workspace = true, features = ["serialize"] } -dioxus-html = { workspace = true, features = ["serialize", "native-bind"] } +dioxus-html = { workspace = true, features = ["serialize", "native-bind", "mounted", "eval"] } dioxus-interpreter-js = { workspace = true } dioxus-hot-reload = { workspace = true, optional = true } diff --git a/packages/desktop/headless_tests/events.rs b/packages/desktop/headless_tests/events.rs index ac62cd37b8..6765a81992 100644 --- a/packages/desktop/headless_tests/events.rs +++ b/packages/desktop/headless_tests/events.rs @@ -304,7 +304,7 @@ fn app(cx: Scope) -> Element { assert!(event.data.modifiers().is_empty()); assert_eq!(event.data.key().to_string(), "a"); assert_eq!(event.data.code().to_string(), "KeyA"); - assert_eq!(event.data.location, 0); + assert_eq!(event.data.location(), Location::Standard); assert!(event.data.is_auto_repeating()); recieved_events.modify(|x| *x + 1) @@ -317,7 +317,7 @@ fn app(cx: Scope) -> Element { assert!(event.data.modifiers().is_empty()); assert_eq!(event.data.key().to_string(), "a"); assert_eq!(event.data.code().to_string(), "KeyA"); - assert_eq!(event.data.location, 0); + assert_eq!(event.data.location(), Location::Standard); assert!(!event.data.is_auto_repeating()); recieved_events.modify(|x| *x + 1) @@ -330,7 +330,7 @@ fn app(cx: Scope) -> Element { assert!(event.data.modifiers().is_empty()); assert_eq!(event.data.key().to_string(), "a"); assert_eq!(event.data.code().to_string(), "KeyA"); - assert_eq!(event.data.location, 0); + assert_eq!(event.data.location(), Location::Standard); assert!(!event.data.is_auto_repeating()); recieved_events.modify(|x| *x + 1) diff --git a/packages/desktop/src/element.rs b/packages/desktop/src/element.rs index 20ca951bf8..86741d4158 100644 --- a/packages/desktop/src/element.rs +++ b/packages/desktop/src/element.rs @@ -3,6 +3,7 @@ use dioxus_html::{geometry::euclid::Rect, MountedResult, RenderedElementBacking} use crate::{desktop_context::DesktopContext, query::QueryEngine}; +#[derive(Clone)] /// A mounted element passed to onmounted events pub struct DesktopElement { id: ElementId, @@ -17,8 +18,8 @@ impl DesktopElement { } impl RenderedElementBacking for DesktopElement { - fn get_raw_element(&self) -> dioxus_html::MountedResult<&dyn std::any::Any> { - Ok(self) + fn as_any(&self) -> &dyn std::any::Any { + self } fn get_client_rect( diff --git a/packages/desktop/src/events.rs b/packages/desktop/src/events.rs index 7059a7c858..a4fbb4e1e2 100644 --- a/packages/desktop/src/events.rs +++ b/packages/desktop/src/events.rs @@ -1,7 +1,10 @@ //! Convert a serialized event to an event trigger +use dioxus_html::*; use serde::{Deserialize, Serialize}; +use crate::element::DesktopElement; + #[derive(Deserialize, Serialize, Debug, Clone)] pub struct IpcMessage { method: String, @@ -17,3 +20,147 @@ impl IpcMessage { self.params } } + +pub(crate) struct SerializedHtmlEventConverter; + +impl HtmlEventConverter for SerializedHtmlEventConverter { + fn convert_animation_data(&self, event: &PlatformEventData) -> AnimationData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_clipboard_data(&self, event: &PlatformEventData) -> ClipboardData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_composition_data(&self, event: &PlatformEventData) -> CompositionData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_drag_data(&self, event: &PlatformEventData) -> DragData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_focus_data(&self, event: &PlatformEventData) -> FocusData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_form_data(&self, event: &PlatformEventData) -> FormData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_image_data(&self, event: &PlatformEventData) -> ImageData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_keyboard_data(&self, event: &PlatformEventData) -> KeyboardData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_media_data(&self, event: &PlatformEventData) -> MediaData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_mounted_data(&self, event: &PlatformEventData) -> MountedData { + event.downcast::().cloned().unwrap().into() + } + + fn convert_mouse_data(&self, event: &PlatformEventData) -> MouseData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_pointer_data(&self, event: &PlatformEventData) -> PointerData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_scroll_data(&self, event: &PlatformEventData) -> ScrollData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_selection_data(&self, event: &PlatformEventData) -> SelectionData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_toggle_data(&self, event: &PlatformEventData) -> ToggleData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_touch_data(&self, event: &PlatformEventData) -> TouchData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_transition_data(&self, event: &PlatformEventData) -> TransitionData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_wheel_data(&self, event: &PlatformEventData) -> WheelData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } +} diff --git a/packages/desktop/src/lib.rs b/packages/desktop/src/lib.rs index badbe32760..6bf55fd8ff 100644 --- a/packages/desktop/src/lib.rs +++ b/packages/desktop/src/lib.rs @@ -27,10 +27,11 @@ pub use desktop_context::{ }; use desktop_context::{EventData, UserWindowEvent, WebviewQueue, WindowEventHandlers}; use dioxus_core::*; -use dioxus_html::MountedData; -use dioxus_html::{native_bind::NativeFileEngine, FormData, HtmlEvent}; +use dioxus_html::{native_bind::NativeFileEngine, HtmlEvent}; +use dioxus_html::{FileEngine, HasFormData, MountedData, PlatformEventData}; use element::DesktopElement; use eval::init_eval; +use events::SerializedHtmlEventConverter; use futures_util::{pin_mut, FutureExt}; use shortcut::ShortcutRegistry; pub use shortcut::{use_global_shortcut, ShortcutHandle, ShortcutId, ShortcutRegistryError}; @@ -136,6 +137,9 @@ pub fn launch_with_props(root: Component

, props: P, cfg: Config) } }); + // Set the event converter + dioxus_html::set_event_converter(Box::new(SerializedHtmlEventConverter)); + // We start the tokio runtime *on this thread* // Any future we poll later will use this runtime to spawn tasks and for IO let rt = tokio::runtime::Builder::new_multi_thread() @@ -292,7 +296,7 @@ pub fn launch_with_props(root: Component

, props: P, cfg: Config) let element = DesktopElement::new(element, view.desktop_context.clone(), query); - Rc::new(MountedData::new(element)) + Rc::new(PlatformEventData::new(Box::new(MountedData::new(element)))) } else { data.into_any() }; @@ -340,15 +344,28 @@ pub fn launch_with_props(root: Component

, props: P, cfg: Config) if let Ok(file_diolog) = serde_json::from_value::(msg.params()) { + struct DesktopFileUploadForm { + files: Arc, + } + + impl HasFormData for DesktopFileUploadForm { + fn files(&self) -> Option> { + Some(self.files.clone()) + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } + } + let id = ElementId(file_diolog.target); let event_name = &file_diolog.event; let event_bubbles = file_diolog.bubbles; let files = file_upload::get_file_event(&file_diolog); - let data = Rc::new(FormData { - value: Default::default(), - values: Default::default(), - files: Some(Arc::new(NativeFileEngine::new(files))), - }); + let data = + Rc::new(PlatformEventData::new(Box::new(DesktopFileUploadForm { + files: Arc::new(NativeFileEngine::new(files)), + }))); let view = webviews.get_mut(&event.1).unwrap(); diff --git a/packages/dioxus-tui/examples/colorpicker.rs b/packages/dioxus-tui/examples/colorpicker.rs index 00f8ef7e01..694d58ed82 100644 --- a/packages/dioxus-tui/examples/colorpicker.rs +++ b/packages/dioxus-tui/examples/colorpicker.rs @@ -15,21 +15,21 @@ fn app(cx: Scope) -> Element { let mapping: DioxusElementToNodeId = cx.consume_context().unwrap(); // disable templates so that every node has an id and can be queried cx.render(rsx! { - div{ + div { width: "100%", background_color: "hsl({hue}, 70%, {brightness}%)", onmousemove: move |evt| { if let RenderReturn::Ready(node) = cx.root_node() { if let Some(id) = node.root_ids.borrow().get(0).cloned() { let node = tui_query.get(mapping.get_node_id(id).unwrap()); - let Size{width, height} = node.size().unwrap(); + let Size { width, height } = node.size().unwrap(); let pos = evt.inner().element_coordinates(); - hue.set((pos.x as f32/width as f32)*255.0); - brightness.set((pos.y as f32/height as f32)*100.0); + hue.set((pos.x as f32 / width as f32) * 255.0); + brightness.set((pos.y as f32 / height as f32) * 100.0); } } }, - "hsl({hue}, 70%, {brightness}%)", + "hsl({hue}, 70%, {brightness}%)" } }) } diff --git a/packages/dioxus-tui/examples/hover.rs b/packages/dioxus-tui/examples/hover.rs index 3af2d1616d..2c2cda17d6 100644 --- a/packages/dioxus-tui/examples/hover.rs +++ b/packages/dioxus-tui/examples/hover.rs @@ -56,7 +56,6 @@ fn app(cx: Scope) -> Element { width: "100%", height: "100%", flex_direction: "column", - div { width: "100%", height: "50%", @@ -71,7 +70,7 @@ fn app(cx: Scope) -> Element { onmouseenter: move |m| q1_color.set([get_brightness(m.inner()), 0, 0]), onmousedown: move |m| q1_color.set([get_brightness(m.inner()), 0, 0]), onmouseup: move |m| q1_color.set([get_brightness(m.inner()), 0, 0]), - onwheel: move |w| q1_color.set([q1_color[0] + (10.0*w.delta().strip_units().y) as i32, 0, 0]), + onwheel: move |w| q1_color.set([q1_color[0] + (10.0 * w.delta().strip_units().y) as i32, 0, 0]), onmouseleave: move |_| q1_color.set([200; 3]), onmousemove: update_data, "click me" @@ -85,7 +84,7 @@ fn app(cx: Scope) -> Element { onmouseenter: move |m| q2_color.set([get_brightness(m.inner()); 3]), onmousedown: move |m| q2_color.set([get_brightness(m.inner()); 3]), onmouseup: move |m| q2_color.set([get_brightness(m.inner()); 3]), - onwheel: move |w| q2_color.set([q2_color[0] + (10.0*w.delta().strip_units().y) as i32;3]), + onwheel: move |w| q2_color.set([q2_color[0] + (10.0 * w.delta().strip_units().y) as i32; 3]), onmouseleave: move |_| q2_color.set([200; 3]), onmousemove: update_data, "click me" @@ -105,7 +104,7 @@ fn app(cx: Scope) -> Element { onmouseenter: move |m| q3_color.set([0, get_brightness(m.inner()), 0]), onmousedown: move |m| q3_color.set([0, get_brightness(m.inner()), 0]), onmouseup: move |m| q3_color.set([0, get_brightness(m.inner()), 0]), - onwheel: move |w| q3_color.set([0, q3_color[1] + (10.0*w.delta().strip_units().y) as i32, 0]), + onwheel: move |w| q3_color.set([0, q3_color[1] + (10.0 * w.delta().strip_units().y) as i32, 0]), onmouseleave: move |_| q3_color.set([200; 3]), onmousemove: update_data, "click me" @@ -119,16 +118,16 @@ fn app(cx: Scope) -> Element { onmouseenter: move |m| q4_color.set([0, 0, get_brightness(m.inner())]), onmousedown: move |m| q4_color.set([0, 0, get_brightness(m.inner())]), onmouseup: move |m| q4_color.set([0, 0, get_brightness(m.inner())]), - onwheel: move |w| q4_color.set([0, 0, q4_color[2] + (10.0*w.delta().strip_units().y) as i32]), + onwheel: move |w| q4_color.set([0, 0, q4_color[2] + (10.0 * w.delta().strip_units().y) as i32]), onmouseleave: move |_| q4_color.set([200; 3]), onmousemove: update_data, "click me" } - }, - div {"Page coordinates: {page_coordinates}"}, - div {"Element coordinates: {element_coordinates}"}, - div {"Buttons: {buttons}"}, - div {"Modifiers: {modifiers}"}, + } + div { "Page coordinates: {page_coordinates}" } + div { "Element coordinates: {element_coordinates}" } + div { "Buttons: {buttons}" } + div { "Modifiers: {modifiers}" } } }) } diff --git a/packages/dioxus-tui/examples/widgets.rs b/packages/dioxus-tui/examples/widgets.rs index b5df353886..2d0a5bf726 100644 --- a/packages/dioxus-tui/examples/widgets.rs +++ b/packages/dioxus-tui/examples/widgets.rs @@ -18,7 +18,7 @@ fn app(cx: Scope) -> Element { justify_content: "center", input { - oninput: |data| if &data.value == "good"{ + oninput: |data| if &data.value()== "good"{ bg_green.set(true); } else{ bg_green.set(false); @@ -30,7 +30,7 @@ fn app(cx: Scope) -> Element { checked: "true", } input { - oninput: |data| if &data.value == "hello world"{ + oninput: |data| if &data.value()== "hello world"{ bg_green.set(true); } else{ bg_green.set(false); @@ -41,7 +41,7 @@ fn app(cx: Scope) -> Element { } input { oninput: |data| { - if (data.value.parse::().unwrap() - 40.0).abs() < 5.0 { + if (data.value().parse::().unwrap() - 40.0).abs() < 5.0 { bg_green.set(true); } else{ bg_green.set(false); @@ -55,7 +55,7 @@ fn app(cx: Scope) -> Element { } input { oninput: |data| { - if data.value == "10"{ + if data.value()== "10"{ bg_green.set(true); } else{ bg_green.set(false); @@ -68,7 +68,7 @@ fn app(cx: Scope) -> Element { } input { oninput: |data| { - if data.value == "hello world"{ + if data.value()== "hello world"{ bg_green.set(true); } else{ bg_green.set(false); diff --git a/packages/dioxus-tui/src/element.rs b/packages/dioxus-tui/src/element.rs index eeb4d5b864..8fdb60b9f3 100644 --- a/packages/dioxus-tui/src/element.rs +++ b/packages/dioxus-tui/src/element.rs @@ -1,7 +1,6 @@ use std::{ any::Any, fmt::{Display, Formatter}, - rc::Rc, }; use dioxus_core::{ElementId, Mutations, VirtualDom}; @@ -30,7 +29,7 @@ pub(crate) fn find_mount_events(mutations: &Mutations) -> Vec { // We need to queue the mounted events to give rink time to rendere and resolve the layout of elements after they are created pub(crate) fn create_mounted_events( vdom: &VirtualDom, - events: &mut Vec<(ElementId, &'static str, Rc, bool)>, + events: &mut Vec<(ElementId, &'static str, Box, bool)>, mount_events: impl Iterator, ) { let query: Query = vdom @@ -42,11 +41,12 @@ pub(crate) fn create_mounted_events( query: query.clone(), id: node_id, }; - events.push((id, "mounted", Rc::new(MountedData::new(element)), false)); + events.push((id, "mounted", Box::new(MountedData::new(element)), false)); } } -struct TuiElement { +#[derive(Clone)] +pub(crate) struct TuiElement { query: Query, id: NodeId, } @@ -82,8 +82,8 @@ impl RenderedElementBacking for TuiElement { }) } - fn get_raw_element(&self) -> dioxus_html::MountedResult<&dyn std::any::Any> { - Ok(self) + fn as_any(&self) -> &dyn std::any::Any { + self } } diff --git a/packages/dioxus-tui/src/events.rs b/packages/dioxus-tui/src/events.rs new file mode 100644 index 0000000000..3e7b6fd2a1 --- /dev/null +++ b/packages/dioxus-tui/src/events.rs @@ -0,0 +1,108 @@ +use core::panic; + +use dioxus_html::*; + +use crate::element::TuiElement; + +fn downcast(event: &PlatformEventData) -> plasmo::EventData { + event + .downcast::() + .expect("event should be of type EventData") + .clone() +} + +pub(crate) struct SerializedHtmlEventConverter; + +impl HtmlEventConverter for SerializedHtmlEventConverter { + fn convert_animation_data(&self, _: &PlatformEventData) -> AnimationData { + panic!("animation events not supported") + } + + fn convert_clipboard_data(&self, _: &PlatformEventData) -> ClipboardData { + panic!("clipboard events not supported") + } + + fn convert_composition_data(&self, _: &PlatformEventData) -> CompositionData { + panic!("composition events not supported") + } + + fn convert_drag_data(&self, _: &PlatformEventData) -> DragData { + panic!("drag events not supported") + } + + fn convert_focus_data(&self, event: &PlatformEventData) -> FocusData { + if let plasmo::EventData::Focus(event) = downcast(event) { + FocusData::new(event) + } else { + panic!("event should be of type Focus") + } + } + + fn convert_form_data(&self, event: &PlatformEventData) -> FormData { + if let plasmo::EventData::Form(event) = downcast(event) { + FormData::new(event) + } else { + panic!("event should be of type Form") + } + } + + fn convert_image_data(&self, _: &PlatformEventData) -> ImageData { + panic!("image events not supported") + } + + fn convert_keyboard_data(&self, event: &PlatformEventData) -> KeyboardData { + if let plasmo::EventData::Keyboard(event) = downcast(event) { + KeyboardData::new(event) + } else { + panic!("event should be of type Keyboard") + } + } + + fn convert_media_data(&self, _: &PlatformEventData) -> MediaData { + panic!("media events not supported") + } + + fn convert_mounted_data(&self, event: &PlatformEventData) -> MountedData { + event.downcast::().cloned().unwrap().into() + } + + fn convert_mouse_data(&self, event: &PlatformEventData) -> MouseData { + if let plasmo::EventData::Mouse(event) = downcast(event) { + MouseData::new(event) + } else { + panic!("event should be of type Mouse") + } + } + + fn convert_pointer_data(&self, _: &PlatformEventData) -> PointerData { + panic!("pointer events not supported") + } + + fn convert_scroll_data(&self, _: &PlatformEventData) -> ScrollData { + panic!("scroll events not supported") + } + + fn convert_selection_data(&self, _: &PlatformEventData) -> SelectionData { + panic!("selection events not supported") + } + + fn convert_toggle_data(&self, _: &PlatformEventData) -> ToggleData { + panic!("toggle events not supported") + } + + fn convert_touch_data(&self, _: &PlatformEventData) -> TouchData { + panic!("touch events not supported") + } + + fn convert_transition_data(&self, _: &PlatformEventData) -> TransitionData { + panic!("transition events not supported") + } + + fn convert_wheel_data(&self, event: &PlatformEventData) -> WheelData { + if let plasmo::EventData::Wheel(event) = downcast(event) { + WheelData::new(event) + } else { + panic!("event should be of type Wheel") + } + } +} diff --git a/packages/dioxus-tui/src/lib.rs b/packages/dioxus-tui/src/lib.rs index fd5b635013..6fafa47efe 100644 --- a/packages/dioxus-tui/src/lib.rs +++ b/packages/dioxus-tui/src/lib.rs @@ -1,4 +1,5 @@ mod element; +mod events; use std::{ any::Any, @@ -8,6 +9,7 @@ use std::{ }; use dioxus_core::{Component, ElementId, VirtualDom}; +use dioxus_html::PlatformEventData; use dioxus_native_core::dioxus::{DioxusState, NodeImmutableDioxusExt}; use dioxus_native_core::prelude::*; @@ -24,6 +26,8 @@ pub fn launch_cfg(app: Component<()>, cfg: Config) { } pub fn launch_cfg_with_props(app: Component, props: Props, cfg: Config) { + dioxus_html::set_event_converter(Box::new(events::SerializedHtmlEventConverter)); + render(cfg, |rdom, taffy, event_tx| { let dioxus_state = { let mut rdom = rdom.write().unwrap(); @@ -81,7 +85,7 @@ struct DioxusRenderer { vdom: VirtualDom, dioxus_state: Rc>, // Events that are queued up to be sent to the vdom next time the vdom is polled - queued_events: Vec<(ElementId, &'static str, Rc, bool)>, + queued_events: Vec<(ElementId, &'static str, Box, bool)>, #[cfg(all(feature = "hot-reload", debug_assertions))] hot_reload_rx: tokio::sync::mpsc::UnboundedReceiver, } @@ -122,15 +126,19 @@ impl Driver for DioxusRenderer { let id = { rdom.read().unwrap().get(id).unwrap().mounted_id() }; if let Some(id) = id { let inner_value = value.deref().clone(); + let boxed_event = Box::new(inner_value); + let platform_event = PlatformEventData::new(boxed_event); self.vdom - .handle_event(event, inner_value.into_any(), id, bubbles); + .handle_event(event, Rc::new(platform_event), id, bubbles); } } fn poll_async(&mut self) -> std::pin::Pin + '_>> { // Add any queued events for (id, event, value, bubbles) in self.queued_events.drain(..) { - self.vdom.handle_event(event, value, id, bubbles); + let platform_event = PlatformEventData::new(value); + self.vdom + .handle_event(event, Rc::new(platform_event), id, bubbles); } #[cfg(all(feature = "hot-reload", debug_assertions))] diff --git a/packages/dioxus-tui/tests/events.rs b/packages/dioxus-tui/tests/events.rs index 671cfc3496..4d2639371a 100644 --- a/packages/dioxus-tui/tests/events.rs +++ b/packages/dioxus-tui/tests/events.rs @@ -63,7 +63,7 @@ fn key_down() { onkeydown: move |evt| { assert_eq!(evt.data.code(), Code::KeyA); tui_ctx.quit(); - }, + } } }) } @@ -95,9 +95,11 @@ fn mouse_down() { width: "100%", height: "100%", onmousedown: move |evt| { - assert!(evt.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Primary)); + assert!( + evt.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Primary) + ); tui_ctx.quit(); - }, + } } }) } @@ -136,7 +138,7 @@ fn mouse_up() { height: "100%", onmouseup: move |_| { tui_ctx.quit(); - }, + } } }) } @@ -175,7 +177,7 @@ fn mouse_enter() { height: "50%", onmouseenter: move |_| { tui_ctx.quit(); - }, + } } }) } @@ -214,7 +216,7 @@ fn mouse_exit() { height: "50%", onmouseenter: move |_| { tui_ctx.quit(); - }, + } } }) } @@ -251,9 +253,9 @@ fn mouse_move() { div { width: "100%", height: "100%", - onmousemove: move |_|{ + onmousemove: move |_| { tui_ctx.quit(); - }, + } } }) } @@ -293,7 +295,7 @@ fn wheel() { onwheel: move |evt| { assert!(evt.data.delta().strip_units().y > 0.0); tui_ctx.quit(); - }, + } } }) } @@ -330,9 +332,9 @@ fn click() { div { width: "100%", height: "100%", - onclick: move |_|{ + onclick: move |_| { tui_ctx.quit(); - }, + } } }) } @@ -369,9 +371,9 @@ fn context_menu() { div { width: "100%", height: "100%", - oncontextmenu: move |_|{ + oncontextmenu: move |_| { tui_ctx.quit(); - }, + } } }) } diff --git a/packages/html/Cargo.toml b/packages/html/Cargo.toml index 5644b440ca..00df6040a8 100644 --- a/packages/html/Cargo.toml +++ b/packages/html/Cargo.toml @@ -16,7 +16,7 @@ serde = { version = "1", features = ["derive"], optional = true } serde_repr = { version = "0.1", optional = true } wasm-bindgen = { workspace = true, optional = true } euclid = "0.22.7" -enumset = "1.0.11" +enumset = "1.1.2" keyboard-types = "0.7" async-trait = "0.1.58" serde-value = "0.7.0" @@ -29,28 +29,29 @@ serde_json = { version = "1", optional = true } optional = true version = "0.3.56" features = [ - "TouchEvent", - "MouseEvent", - "InputEvent", - "ClipboardEvent", - "KeyboardEvent", - "TouchEvent", - "WheelEvent", - "AnimationEvent", - "TransitionEvent", - "PointerEvent", - "FocusEvent", - "CompositionEvent", - "ClipboardEvent", + "Touch", + "TouchList", + "TouchEvent", + "MouseEvent", + "InputEvent", + "ClipboardEvent", + "KeyboardEvent", + "WheelEvent", + "AnimationEvent", + "TransitionEvent", + "PointerEvent", + "FocusEvent", + "CompositionEvent", ] [dev-dependencies] serde_json = "1" [features] -default = ["serialize", "mounted"] +default = ["serialize", "mounted", "eval"] serialize = [ "serde", + "serde/rc", "serde_repr", "serde_json", "euclid/serde", @@ -65,6 +66,10 @@ mounted = [ "web-sys/ScrollBehavior", "web-sys/HtmlElement", ] +eval = [ + "serde", + "serde_json" +] wasm-bind = ["web-sys", "wasm-bindgen"] native-bind = ["tokio"] hot-reload-context = ["dioxus-rsx"] diff --git a/packages/html/src/events.rs b/packages/html/src/events.rs index c591494939..02d1a22fdb 100644 --- a/packages/html/src/events.rs +++ b/packages/html/src/events.rs @@ -1,3 +1,6 @@ +use std::any::Any; +use std::sync::RwLock; + macro_rules! impl_event { ( $data:ty; @@ -9,12 +12,15 @@ macro_rules! impl_event { $( $( #[$attr] )* #[inline] - pub fn $name<'a, E: crate::EventReturn, T>(_cx: &'a ::dioxus_core::ScopeState, mut _f: impl FnMut(::dioxus_core::Event<$data>) -> E + 'a) -> ::dioxus_core::Attribute<'a> { + pub fn $name<'a, E: crate::EventReturn, T>(_cx: &'a ::dioxus_core::ScopeState, mut _f: impl FnMut(::dioxus_core::Event<$data>) -> E + 'a, _metadata: ()) -> ::dioxus_core::Attribute<'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| { + _f(e.map(|e|e.into())).spawn(_cx); + }, + _metadata + ), None, false, ) @@ -23,6 +29,193 @@ macro_rules! impl_event { }; } +static EVENT_CONVERTER: RwLock>> = RwLock::new(None); + +#[inline] +pub fn set_event_converter(converter: Box) { + *EVENT_CONVERTER.write().unwrap() = Some(converter); +} + +#[inline] +pub(crate) fn with_event_converter(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, +} + +impl PlatformEventData { + pub fn new(event: Box) -> Self { + Self { event } + } + + pub fn downcast(&self) -> Option<&T> { + self.event.downcast_ref::() + } + + pub fn downcast_mut(&mut self) -> Option<&mut T> { + self.event.downcast_mut::() + } + + pub fn into_inner(self) -> Option { + self.event.downcast::().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 +344,6 @@ pub fn event_bubbles(evt: &str) -> bool { } } -use std::future::Future; - #[doc(hidden)] pub trait EventReturn

: Sized { fn spawn(self, _cx: &dioxus_core::ScopeState) {} @@ -164,7 +355,7 @@ pub struct AsyncMarker; impl EventReturn for T where - T: Future + 'static, + T: std::future::Future + 'static, { #[inline] fn spawn(self, cx: &dioxus_core::ScopeState) { diff --git a/packages/html/src/events/animation.rs b/packages/html/src/events/animation.rs index 4495191fc0..26db06f9db 100644 --- a/packages/html/src/events/animation.rs +++ b/packages/html/src/events/animation.rs @@ -2,12 +2,132 @@ use dioxus_core::Event; pub type AnimationEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq)] pub struct AnimationData { - pub animation_name: String, - pub pseudo_element: String, - pub elapsed_time: f32, + inner: Box, +} + +impl AnimationData { + /// Create a new AnimationData + pub fn new(inner: impl HasAnimationData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// The name of the animation + pub fn animation_name(&self) -> String { + self.inner.animation_name() + } + + /// The name of the pseudo-element the animation runs on + pub fn pseudo_element(&self) -> String { + self.inner.pseudo_element() + } + + /// The amount of time the animation has been running + pub fn elapsed_time(&self) -> f32 { + self.inner.elapsed_time() + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_ref().as_any().downcast_ref::() + } +} + +impl From for AnimationData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl std::fmt::Debug for AnimationData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("AnimationData") + .field("animation_name", &self.animation_name()) + .field("pseudo_element", &self.pseudo_element()) + .field("elapsed_time", &self.elapsed_time()) + .finish() + } +} + +impl PartialEq for AnimationData { + fn eq(&self, other: &Self) -> bool { + self.animation_name() == other.animation_name() + && self.pseudo_element() == other.pseudo_element() + && self.elapsed_time() == other.elapsed_time() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of AnimationData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedAnimationData { + animation_name: String, + pseudo_element: String, + elapsed_time: f32, +} + +#[cfg(feature = "serialize")] +impl From<&AnimationData> for SerializedAnimationData { + fn from(data: &AnimationData) -> Self { + Self { + animation_name: data.animation_name(), + pseudo_element: data.pseudo_element(), + elapsed_time: data.elapsed_time(), + } + } +} + +#[cfg(feature = "serialize")] +impl HasAnimationData for SerializedAnimationData { + fn animation_name(&self) -> String { + self.animation_name.clone() + } + + fn pseudo_element(&self) -> String { + self.pseudo_element.clone() + } + + fn elapsed_time(&self) -> f32 { + self.elapsed_time + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for AnimationData { + fn serialize(&self, serializer: S) -> Result { + SerializedAnimationData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for AnimationData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedAnimationData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +/// A trait for any object that has the data for an animation event +pub trait HasAnimationData: std::any::Any { + /// The name of the animation + fn animation_name(&self) -> String; + + /// The name of the pseudo-element the animation runs on + fn pseudo_element(&self) -> String; + + /// The amount of time the animation has been running + fn elapsed_time(&self) -> f32; + + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; } impl_event! [ diff --git a/packages/html/src/events/clipboard.rs b/packages/html/src/events/clipboard.rs index 54390c6b04..c0180c728c 100644 --- a/packages/html/src/events/clipboard.rs +++ b/packages/html/src/events/clipboard.rs @@ -1,10 +1,82 @@ use dioxus_core::Event; pub type ClipboardEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq)] + pub struct ClipboardData { - // DOMDataTransfer clipboardData + inner: Box, +} + +impl From 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 { + /// Create a new ClipboardData + pub fn new(inner: impl HasClipboardData) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_ref().as_any().downcast_ref::() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of ClipboardData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub 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(&self, serializer: S) -> Result { + SerializedClipboardData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for ClipboardData { + fn deserialize>(deserializer: D) -> Result { + 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![ diff --git a/packages/html/src/events/composition.rs b/packages/html/src/events/composition.rs index 480868f247..4d161436ed 100644 --- a/packages/html/src/events/composition.rs +++ b/packages/html/src/events/composition.rs @@ -1,10 +1,98 @@ use dioxus_core::Event; pub type CompositionEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq)] + pub struct CompositionData { - pub data: String, + inner: Box, +} + +impl std::fmt::Debug for CompositionData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CompositionData") + .field("data", &self.data()) + .finish() + } +} + +impl From for CompositionData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl PartialEq for CompositionData { + fn eq(&self, other: &Self) -> bool { + self.data() == other.data() + } +} + +impl CompositionData { + /// Create a new CompositionData + pub fn new(inner: impl HasCompositionData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// The characters generated by the input method that raised the event + pub fn data(&self) -> String { + self.inner.data() + } + + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of CompositionData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedCompositionData { + data: String, +} + +#[cfg(feature = "serialize")] +impl From<&CompositionData> for SerializedCompositionData { + fn from(data: &CompositionData) -> Self { + Self { data: data.data() } + } +} + +#[cfg(feature = "serialize")] +impl HasCompositionData for SerializedCompositionData { + fn data(&self) -> String { + self.data.clone() + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for CompositionData { + fn serialize(&self, serializer: S) -> Result { + SerializedCompositionData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for CompositionData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedCompositionData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +/// A trait for any object that has the data for a composition event +pub trait HasCompositionData: std::any::Any { + /// The characters generated by the input method that raised the event + fn data(&self) -> String; + + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; } impl_event! [ diff --git a/packages/html/src/events/drag.rs b/packages/html/src/events/drag.rs index fa3219631c..e58c20e221 100644 --- a/packages/html/src/events/drag.rs +++ b/packages/html/src/events/drag.rs @@ -1,6 +1,11 @@ +use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint}; +use crate::input_data::{MouseButton, MouseButtonSet}; +use crate::prelude::*; + use dioxus_core::Event; +use keyboard_types::Modifiers; -use crate::MouseData; +use crate::HasMouseData; pub type DragEvent = Event; @@ -8,13 +13,181 @@ pub type DragEvent = Event; /// placing a pointer device (such as a mouse) on the touch surface and then dragging the pointer to a new location /// (such as another DOM element). Applications are free to interpret a drag and drop interaction in an /// application-specific way. -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq)] pub struct DragData { - /// Inherit mouse data - pub mouse: MouseData, + inner: Box, +} + +impl From for DragData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl std::fmt::Debug for DragData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("DragData") + .field("coordinates", &self.coordinates()) + .field("modifiers", &self.modifiers()) + .field("held_buttons", &self.held_buttons()) + .field("trigger_button", &self.trigger_button()) + .finish() + } +} + +impl PartialEq for DragData { + fn eq(&self, other: &Self) -> bool { + self.coordinates() == other.coordinates() + && self.modifiers() == other.modifiers() + && self.held_buttons() == other.held_buttons() + && self.trigger_button() == other.trigger_button() + } +} + +impl DragData { + /// Create a new DragData + pub fn new(inner: impl HasDragData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// Downcast this event data to a specific type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +impl InteractionLocation for DragData { + fn client_coordinates(&self) -> ClientPoint { + self.inner.client_coordinates() + } + + fn page_coordinates(&self) -> PagePoint { + self.inner.page_coordinates() + } + + fn screen_coordinates(&self) -> ScreenPoint { + self.inner.screen_coordinates() + } +} + +impl InteractionElementOffset for DragData { + fn element_coordinates(&self) -> ElementPoint { + self.inner.element_coordinates() + } + + fn coordinates(&self) -> Coordinates { + self.inner.coordinates() + } +} + +impl ModifiersInteraction for DragData { + fn modifiers(&self) -> Modifiers { + self.inner.modifiers() + } +} + +impl PointerInteraction for DragData { + fn held_buttons(&self) -> MouseButtonSet { + self.inner.held_buttons() + } + + // todo the following is kind of bad; should we just return None when the trigger_button is unreliable (and frankly irrelevant)? i guess we would need the event_type here + fn trigger_button(&self) -> Option { + self.inner.trigger_button() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of DragData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedDragData { + mouse: crate::point_interaction::SerializedPointInteraction, +} + +#[cfg(feature = "serialize")] +impl From<&DragData> for SerializedDragData { + fn from(data: &DragData) -> Self { + Self { + mouse: crate::point_interaction::SerializedPointInteraction::from(data), + } + } +} + +#[cfg(feature = "serialize")] +impl HasDragData for SerializedDragData {} + +#[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 { + self.mouse.trigger_button() + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for DragData { + fn serialize(&self, serializer: S) -> Result { + SerializedDragData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for DragData { + fn deserialize>(deserializer: D) -> Result { + 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 {} + impl_event! { DragData; diff --git a/packages/html/src/events/focus.rs b/packages/html/src/events/focus.rs index 2092c972e2..f121feb7b9 100644 --- a/packages/html/src/events/focus.rs +++ b/packages/html/src/events/focus.rs @@ -2,9 +2,82 @@ use dioxus_core::Event; pub type FocusEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct FocusData {/* DOMEventInner: Send + SyncTarget relatedTarget */} +pub struct FocusData { + inner: Box, +} + +impl From for FocusData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl std::fmt::Debug for FocusData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FocusData").finish() + } +} + +impl PartialEq for FocusData { + fn eq(&self, _other: &Self) -> bool { + true + } +} + +impl FocusData { + /// Create a new FocusData + pub fn new(inner: impl HasFocusData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// Downcast this event data to a specific type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of FocusData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone, Default)] +pub struct SerializedFocusData {} + +#[cfg(feature = "serialize")] +impl From<&FocusData> for SerializedFocusData { + fn from(_: &FocusData) -> Self { + Self {} + } +} + +#[cfg(feature = "serialize")] +impl HasFocusData for SerializedFocusData { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for FocusData { + fn serialize(&self, serializer: S) -> Result { + SerializedFocusData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for FocusData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedFocusData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +pub trait HasFocusData: std::any::Any { + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; +} impl_event! [ FocusData; diff --git a/packages/html/src/events/form.rs b/packages/html/src/events/form.rs index fab13482b9..de8bb0dbf8 100644 --- a/packages/html/src/events/form.rs +++ b/packages/html/src/events/form.rs @@ -4,28 +4,176 @@ use dioxus_core::Event; pub type FormEvent = Event; -/* DOMEvent: Send + SyncTarget relatedTarget */ -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Clone)] pub struct FormData { - pub value: String, - - pub values: HashMap>, - - #[cfg_attr( - feature = "serialize", - serde( - default, - skip_serializing, - deserialize_with = "deserialize_file_engine" - ) - )] - pub files: Option>, + inner: Box, +} + +impl From for FormData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl PartialEq for FormData { + fn eq(&self, other: &Self) -> bool { + self.value() == other.value() && self.values() == other.values() + } +} + +impl Debug for FormData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FormEvent") + .field("value", &self.value()) + .field("values", &self.values()) + .finish() + } +} + +impl FormData { + /// Create a new form event + pub fn new(event: impl HasFormData + 'static) -> Self { + Self { + inner: Box::new(event), + } + } + + /// Get the value of the form event + pub fn value(&self) -> String { + self.inner.value() + } + + /// Get the values of the form event + pub fn values(&self) -> HashMap> { + self.inner.values() + } + + /// Get the files of the form event + pub fn files(&self) -> Option> { + self.inner.files() + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +/// An object that has all the data for a form event +pub trait HasFormData: std::any::Any { + fn value(&self) -> String { + Default::default() + } + + fn values(&self) -> HashMap> { + Default::default() + } + + fn files(&self) -> Option> { + None + } + + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; +} + +#[cfg(feature = "serialize")] +/// A serialized form data object +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedFormData { + value: String, + values: HashMap>, + files: Option>, +} + +#[cfg(feature = "serialize")] +impl SerializedFormData { + /// Create a new serialized form data object + pub fn new( + value: String, + values: HashMap>, + files: Option>, + ) -> Self { + Self { + value, + values, + files, + } + } + + /// Create a new serialized form data object from a traditional form data object + pub async fn async_from(data: &FormData) -> Self { + Self { + value: data.value(), + values: data.values(), + files: match data.files() { + Some(files) => { + let mut resolved_files = HashMap::new(); + + for file in files.files() { + let bytes = files.read_file(&file).await; + resolved_files.insert(file, bytes.unwrap_or_default()); + } + + Some(std::sync::Arc::new(SerializedFileEngine { + files: resolved_files, + })) + } + None => None, + }, + } + } + + fn from_lossy(data: &FormData) -> Self { + Self { + value: data.value(), + values: data.values(), + files: None, + } + } +} + +#[cfg(feature = "serialize")] +impl HasFormData for SerializedFormData { + fn value(&self) -> String { + self.value.clone() + } + + fn values(&self) -> HashMap> { + self.values.clone() + } + + fn files(&self) -> Option> { + self.files + .as_ref() + .map(|files| std::sync::Arc::clone(files) as std::sync::Arc) + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for FormData { + fn serialize(&self, serializer: S) -> Result { + SerializedFormData::from_lossy(self).serialize(serializer) + } } #[cfg(feature = "serialize")] -#[derive(serde::Serialize, serde::Deserialize)] -struct SerializedFileEngine { +impl<'de> serde::Deserialize<'de> for FormData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedFormData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +#[cfg(feature = "serialize")] +/// A file engine that serializes files to bytes +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedFileEngine { files: HashMap>, } @@ -53,38 +201,6 @@ impl FileEngine for SerializedFileEngine { } } -#[cfg(feature = "serialize")] -fn deserialize_file_engine<'de, D>( - deserializer: D, -) -> Result>, D::Error> -where - D: serde::Deserializer<'de>, -{ - use serde::Deserialize; - - let Ok(file_engine) = SerializedFileEngine::deserialize(deserializer) else { - return Ok(None); - }; - - let file_engine = std::sync::Arc::new(file_engine); - Ok(Some(file_engine)) -} - -impl PartialEq for FormData { - fn eq(&self, other: &Self) -> bool { - self.value == other.value && self.values == other.values - } -} - -impl Debug for FormData { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("FormEvent") - .field("value", &self.value) - .field("values", &self.values) - .finish() - } -} - #[async_trait::async_trait(?Send)] pub trait FileEngine { // get a list of file names diff --git a/packages/html/src/events/image.rs b/packages/html/src/events/image.rs index d5858a7f11..3c75edbdd1 100644 --- a/packages/html/src/events/image.rs +++ b/packages/html/src/events/image.rs @@ -1,11 +1,97 @@ use dioxus_core::Event; pub type ImageEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq)] pub struct ImageData { - #[cfg_attr(feature = "serialize", serde(default))] - pub load_error: bool, + inner: Box, +} + +impl From for ImageData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl std::fmt::Debug for ImageData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ImageData") + .field("load_error", &self.load_error()) + .finish() + } +} + +impl PartialEq for ImageData { + fn eq(&self, other: &Self) -> bool { + self.load_error() == other.load_error() + } +} + +impl ImageData { + /// Create a new ImageData + pub fn new(e: impl HasImageData) -> Self { + Self { inner: Box::new(e) } + } + + /// If the renderer encountered an error while loading the image + pub fn load_error(&self) -> bool { + self.inner.load_error() + } + + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of ImageData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedImageData { + load_error: bool, +} + +#[cfg(feature = "serialize")] +impl From<&ImageData> for SerializedImageData { + fn from(data: &ImageData) -> Self { + Self { + load_error: data.load_error(), + } + } +} + +#[cfg(feature = "serialize")] +impl HasImageData for SerializedImageData { + fn as_any(&self) -> &dyn std::any::Any { + self + } + + fn load_error(&self) -> bool { + self.load_error + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for ImageData { + fn serialize(&self, serializer: S) -> Result { + SerializedImageData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for ImageData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedImageData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +/// A trait for any object that has the data for an image event +pub trait HasImageData: std::any::Any { + /// If the renderer encountered an error while loading the image + fn load_error(&self) -> bool; + + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; } impl_event! [ diff --git a/packages/html/src/events/keyboard.rs b/packages/html/src/events/keyboard.rs index d00dcf8d2c..feb0ab313c 100644 --- a/packages/html/src/events/keyboard.rs +++ b/packages/html/src/events/keyboard.rs @@ -1,9 +1,8 @@ -use crate::input_data::{decode_key_location, encode_key_location}; use dioxus_core::Event; use keyboard_types::{Code, Key, Location, Modifiers}; -use std::convert::TryInto; -use std::fmt::{Debug, Formatter}; -use std::str::FromStr; +use std::fmt::Debug; + +use crate::prelude::ModifiersInteraction; #[cfg(feature = "serialize")] fn resilient_deserialize_code<'de, D>(deserializer: D) -> Result @@ -16,73 +15,99 @@ where } pub type KeyboardEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Clone, PartialEq, Eq)] pub struct KeyboardData { - #[deprecated( - since = "0.3.0", - note = "This may not work in all environments. Use key() instead." - )] - pub char_code: u32, - - /// Identify which "key" was entered. - #[deprecated(since = "0.3.0", note = "use key() instead")] - pub key: String, - - /// Get the key code as an enum Variant. - #[deprecated( - since = "0.3.0", - note = "This may not work in all environments. Use code() instead." - )] - pub key_code: KeyCode, - - /// the physical key on the keyboard - #[cfg_attr( - feature = "serialize", - serde(deserialize_with = "resilient_deserialize_code") - )] - code: Code, + inner: Box, +} - /// Indicate if the `alt` modifier key was pressed during this keyboard event - #[deprecated(since = "0.3.0", note = "use modifiers() instead")] - pub alt_key: bool, +impl From for KeyboardData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} - /// Indicate if the `ctrl` modifier key was pressed during this keyboard event - #[deprecated(since = "0.3.0", note = "use modifiers() instead")] - pub ctrl_key: bool, +impl std::fmt::Debug for KeyboardData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("KeyboardData") + .field("key", &self.key()) + .field("code", &self.code()) + .field("modifiers", &self.modifiers()) + .field("location", &self.location()) + .field("is_auto_repeating", &self.is_auto_repeating()) + .finish() + } +} - /// Indicate if the `meta` modifier key was pressed during this keyboard event - #[deprecated(since = "0.3.0", note = "use modifiers() instead")] - pub meta_key: bool, +impl PartialEq for KeyboardData { + fn eq(&self, other: &Self) -> bool { + self.key() == other.key() + && self.code() == other.code() + && self.modifiers() == other.modifiers() + && self.location() == other.location() + && self.is_auto_repeating() == other.is_auto_repeating() + } +} - /// Indicate if the `shift` modifier key was pressed during this keyboard event - #[deprecated(since = "0.3.0", note = "use modifiers() instead")] - pub shift_key: bool, +impl KeyboardData { + /// Create a new KeyboardData + pub fn new(inner: impl HasKeyboardData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } - #[deprecated(since = "0.3.0", note = "use location() instead")] - pub location: usize, + /// The value of the key pressed by the user, taking into consideration the state of modifier keys such as Shift as well as the keyboard locale and layout. + pub fn key(&self) -> Key { + self.inner.key() + } - #[deprecated(since = "0.3.0", note = "use is_auto_repeating() instead")] - pub repeat: bool, + /// A physical key on the keyboard (as opposed to the character generated by pressing the key). In other words, this property returns a value that isn't altered by keyboard layout or the state of the modifier keys. + pub fn code(&self) -> Code { + self.inner.code() + } - #[deprecated(since = "0.3.0", note = "use code() or key() instead")] - pub which: usize, -} + /// The location of the key on the keyboard or other input device. + pub fn location(&self) -> Location { + self.inner.location() + } -impl_event! { - KeyboardData; + /// `true` iff the key is being held down such that it is automatically repeating. + pub fn is_auto_repeating(&self) -> bool { + self.inner.is_auto_repeating() + } - /// onkeydown - onkeydown + /// Downcast this KeyboardData to a concrete type. + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} - /// onkeypress - onkeypress +impl ModifiersInteraction for KeyboardData { + fn modifiers(&self) -> Modifiers { + self.inner.modifiers() + } +} - /// onkeyup - onkeyup +#[cfg(feature = "serialize")] +/// A serialized version of KeyboardData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedKeyboardData { + char_code: u32, + key: String, + key_code: KeyCode, + #[serde(deserialize_with = "resilient_deserialize_code")] + code: Code, + alt_key: bool, + ctrl_key: bool, + meta_key: bool, + shift_key: bool, + location: usize, + repeat: bool, + which: usize, } -impl KeyboardData { +#[cfg(feature = "serialize")] +impl SerializedKeyboardData { + /// Create a new SerializedKeyboardData pub fn new( key: Key, code: Code, @@ -90,13 +115,11 @@ impl KeyboardData { is_auto_repeating: bool, modifiers: Modifiers, ) -> Self { - #[allow(deprecated)] - KeyboardData { + Self { char_code: key.legacy_charcode(), key: key.to_string(), key_code: KeyCode::from_raw_code( - key.legacy_keycode() - .try_into() + std::convert::TryInto::try_into(key.legacy_keycode()) .expect("could not convert keycode to u8"), ), code, @@ -104,72 +127,117 @@ impl KeyboardData { ctrl_key: modifiers.contains(Modifiers::CONTROL), meta_key: modifiers.contains(Modifiers::META), shift_key: modifiers.contains(Modifiers::SHIFT), - location: encode_key_location(location), + location: crate::input_data::encode_key_location(location), repeat: is_auto_repeating, - which: key - .legacy_charcode() - .try_into() + which: std::convert::TryInto::try_into(key.legacy_charcode()) .expect("could not convert charcode to usize"), } } +} - /// The value of the key pressed by the user, taking into consideration the state of modifier keys such as Shift as well as the keyboard locale and layout. - pub fn key(&self) -> Key { - #[allow(deprecated)] - FromStr::from_str(&self.key).unwrap_or(Key::Unidentified) +#[cfg(feature = "serialize")] +impl From<&KeyboardData> for SerializedKeyboardData { + fn from(data: &KeyboardData) -> Self { + Self::new( + data.key(), + data.code(), + data.location(), + data.is_auto_repeating(), + data.modifiers(), + ) } +} - /// A physical key on the keyboard (as opposed to the character generated by pressing the key). In other words, this property returns a value that isn't altered by keyboard layout or the state of the modifier keys. - pub fn code(&self) -> Code { +#[cfg(feature = "serialize")] +impl HasKeyboardData for SerializedKeyboardData { + fn key(&self) -> Key { + std::str::FromStr::from_str(&self.key).unwrap_or(Key::Unidentified) + } + + fn code(&self) -> Code { self.code } - /// The set of modifier keys which were pressed when the event occurred - pub fn modifiers(&self) -> Modifiers { + fn location(&self) -> Location { + crate::input_data::decode_key_location(self.location) + } + + fn is_auto_repeating(&self) -> bool { + self.repeat + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl ModifiersInteraction for SerializedKeyboardData { + fn modifiers(&self) -> Modifiers { let mut modifiers = Modifiers::empty(); - #[allow(deprecated)] - { - if self.alt_key { - modifiers.insert(Modifiers::ALT); - } - if self.ctrl_key { - modifiers.insert(Modifiers::CONTROL); - } - if self.meta_key { - modifiers.insert(Modifiers::META); - } - if self.shift_key { - modifiers.insert(Modifiers::SHIFT); - } + if self.alt_key { + modifiers.insert(Modifiers::ALT); + } + if self.ctrl_key { + modifiers.insert(Modifiers::CONTROL); + } + if self.meta_key { + modifiers.insert(Modifiers::META); + } + if self.shift_key { + modifiers.insert(Modifiers::SHIFT); } modifiers } +} - /// The location of the key on the keyboard or other input device. - pub fn location(&self) -> Location { - #[allow(deprecated)] - decode_key_location(self.location) +#[cfg(feature = "serialize")] +impl serde::Serialize for KeyboardData { + fn serialize(&self, serializer: S) -> Result { + SerializedKeyboardData::from(self).serialize(serializer) } +} - /// `true` iff the key is being held down such that it is automatically repeating. - pub fn is_auto_repeating(&self) -> bool { - #[allow(deprecated)] - self.repeat +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for KeyboardData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedKeyboardData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) } } -impl Debug for KeyboardData { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.debug_struct("KeyboardData") - .field("key", &self.key()) - .field("code", &self.code()) - .field("modifiers", &self.modifiers()) - .field("location", &self.location()) - .field("is_auto_repeating", &self.is_auto_repeating()) - .finish() - } +impl_event! { + KeyboardData; + + /// onkeydown + onkeydown + + /// onkeypress + onkeypress + + /// onkeyup + onkeyup +} + +pub trait HasKeyboardData: ModifiersInteraction + std::any::Any { + /// The value of the key pressed by the user, taking into consideration the state of modifier keys such as Shift as well as the keyboard locale and layout. + fn key(&self) -> Key; + + /// A physical key on the keyboard (as opposed to the character generated by pressing the key). In other words, this property returns a value that isn't altered by keyboard layout or the state of the modifier keys. + fn code(&self) -> Code; + + /// The location of the key on the keyboard or other input device. + fn location(&self) -> Location; + + /// `true` iff the key is being held down such that it is automatically repeating. + fn is_auto_repeating(&self) -> bool; + + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; } #[cfg(feature = "serialize")] @@ -178,6 +246,8 @@ impl<'de> serde::Deserialize<'de> for KeyCode { where D: serde::Deserializer<'de>, { + use std::convert::TryInto; + // We could be deserializing a unicode character, so we need to use u64 even if the output only takes u8 let value = u64::deserialize(deserializer)?; diff --git a/packages/html/src/events/media.rs b/packages/html/src/events/media.rs index d2ba71c08b..7f3180cf7b 100644 --- a/packages/html/src/events/media.rs +++ b/packages/html/src/events/media.rs @@ -1,9 +1,82 @@ use dioxus_core::Event; pub type MediaEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct MediaData {} +pub struct MediaData { + inner: Box, +} + +impl From for MediaData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl std::fmt::Debug for MediaData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("MediaData").finish() + } +} + +impl PartialEq for MediaData { + fn eq(&self, _: &Self) -> bool { + true + } +} + +impl MediaData { + /// Create a new MediaData + pub fn new(inner: impl HasMediaData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of MediaData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedMediaData {} + +#[cfg(feature = "serialize")] +impl From<&MediaData> for SerializedMediaData { + fn from(_: &MediaData) -> Self { + Self {} + } +} + +#[cfg(feature = "serialize")] +impl HasMediaData for SerializedMediaData { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for MediaData { + fn serialize(&self, serializer: S) -> Result { + SerializedMediaData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for MediaData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedMediaData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +pub trait HasMediaData: std::any::Any { + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; +} impl_event! [ MediaData; @@ -29,7 +102,7 @@ impl_event! [ ///ended onended - // todo: this conflicts with image events + // todo: this conflicts with Media events // neither have data, so it's okay // ///error // onerror diff --git a/packages/html/src/events/mounted.rs b/packages/html/src/events/mounted.rs index bc561bf1b6..e3908ca8a3 100644 --- a/packages/html/src/events/mounted.rs +++ b/packages/html/src/events/mounted.rs @@ -3,22 +3,18 @@ use euclid::Rect; use std::{ - any::Any, fmt::{Display, Formatter}, future::Future, pin::Pin, - rc::Rc, }; /// An Element that has been rendered and allows reading and modifying information about it. /// /// Different platforms will have different implementations and different levels of support for this trait. Renderers that do not support specific features will return `None` for those queries. // we can not use async_trait here because it does not create a trait that is object safe -pub trait RenderedElementBacking { - /// Get the renderer specific element for the given id - fn get_raw_element(&self) -> MountedResult<&dyn Any> { - Err(MountedError::NotSupported) - } +pub trait RenderedElementBacking: std::any::Any { + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; /// Get the bounding rectangle of the element relative to the viewport (this does not include the scroll position) #[allow(clippy::type_complexity)] @@ -40,7 +36,11 @@ pub trait RenderedElementBacking { } } -impl RenderedElementBacking for () {} +impl RenderedElementBacking for () { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} /// The way that scrolling should be performed #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] @@ -57,22 +57,23 @@ pub enum ScrollBehavior { /// /// Different platforms will have different implementations and different levels of support for this trait. Renderers that do not support specific features will return `None` for those queries. pub struct MountedData { - inner: Rc, + inner: Box, +} + +impl From for MountedData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } } impl MountedData { /// Create a new MountedData pub fn new(registry: impl RenderedElementBacking + 'static) -> Self { Self { - inner: Rc::new(registry), + inner: Box::new(registry), } } - /// Get the renderer specific element for the given id - pub fn get_raw_element(&self) -> MountedResult<&dyn Any> { - self.inner.get_raw_element() - } - /// Get the bounding rectangle of the element relative to the viewport (this does not include the scroll position) pub async fn get_client_rect(&self) -> MountedResult> { self.inner.get_client_rect().await @@ -90,6 +91,11 @@ impl MountedData { pub fn set_focus(&self, focus: bool) -> Pin>>> { self.inner.set_focus(focus) } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } } use dioxus_core::Event; diff --git a/packages/html/src/events/mouse.rs b/packages/html/src/events/mouse.rs index 55d081900b..c2bc573288 100644 --- a/packages/html/src/events/mouse.rs +++ b/packages/html/src/events/mouse.rs @@ -1,91 +1,47 @@ use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint}; -use crate::input_data::{ - decode_mouse_button_set, encode_mouse_button_set, MouseButton, MouseButtonSet, -}; +use crate::input_data::{MouseButton, MouseButtonSet}; +use crate::prelude::*; use dioxus_core::Event; use keyboard_types::Modifiers; -use std::fmt::{Debug, Formatter}; pub type MouseEvent = Event; /// A synthetic event that wraps a web-style [`MouseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent) -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Clone, Default, PartialEq, Eq)] /// Data associated with a mouse event -/// -/// Do not use the deprecated fields; they may change or become private in the future. pub struct MouseData { - /// True if the alt key was down when the mouse event was fired. - #[deprecated(since = "0.3.0", note = "use modifiers() instead")] - pub alt_key: bool, - - /// The button number that was pressed (if applicable) when the mouse event was fired. - #[deprecated(since = "0.3.0", note = "use trigger_button() instead")] - pub button: i16, - - /// Indicates which buttons are pressed on the mouse (or other input device) when a mouse event is triggered. - /// - /// Each button that can be pressed is represented by a given number (see below). If more than one button is pressed, the button values are added together to produce a new number. For example, if the secondary (2) and auxiliary (4) buttons are pressed simultaneously, the value is 6 (i.e., 2 + 4). - /// - /// - 1: Primary button (usually the left button) - /// - 2: Secondary button (usually the right button) - /// - 4: Auxiliary button (usually the mouse wheel button or middle button) - /// - 8: 4th button (typically the "Browser Back" button) - /// - 16 : 5th button (typically the "Browser Forward" button) - #[deprecated(since = "0.3.0", note = "use held_buttons() instead")] - pub buttons: u16, - - /// The horizontal coordinate within the application's viewport at which the event occurred (as opposed to the coordinate within the page). - /// - /// For example, clicking on the left edge of the viewport will always result in a mouse event with a clientX value of 0, regardless of whether the page is scrolled horizontally. - #[deprecated(since = "0.3.0", note = "use client_coordinates() instead")] - pub client_x: i32, - - /// The vertical coordinate within the application's viewport at which the event occurred (as opposed to the coordinate within the page). - /// - /// For example, clicking on the top edge of the viewport will always result in a mouse event with a clientY value of 0, regardless of whether the page is scrolled vertically. - #[deprecated(since = "0.3.0", note = "use client_coordinates() instead")] - pub client_y: i32, - - /// True if the control key was down when the mouse event was fired. - #[deprecated(since = "0.3.0", note = "use modifiers() instead")] - pub ctrl_key: bool, - - /// True if the meta key was down when the mouse event was fired. - #[deprecated(since = "0.3.0", note = "use modifiers() instead")] - pub meta_key: bool, - - /// The offset in the X coordinate of the mouse pointer between that event and the padding edge of the target node. - #[deprecated(since = "0.3.0", note = "use element_coordinates() instead")] - pub offset_x: i32, - - /// The offset in the Y coordinate of the mouse pointer between that event and the padding edge of the target node. - #[deprecated(since = "0.3.0", note = "use element_coordinates() instead")] - pub offset_y: i32, - - /// The X (horizontal) coordinate (in pixels) of the mouse, relative to the left edge of the entire document. This includes any portion of the document not currently visible. - /// - /// Being based on the edge of the document as it is, this property takes into account any horizontal scrolling of the page. For example, if the page is scrolled such that 200 pixels of the left side of the document are scrolled out of view, and the mouse is clicked 100 pixels inward from the left edge of the view, the value returned by pageX will be 300. - #[deprecated(since = "0.3.0", note = "use page_coordinates() instead")] - pub page_x: i32, + inner: Box, +} - /// The Y (vertical) coordinate in pixels of the event relative to the whole document. - /// - /// See `page_x`. - #[deprecated(since = "0.3.0", note = "use page_coordinates() instead")] - pub page_y: i32, +impl From for MouseData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} - /// The X coordinate of the mouse pointer in global (screen) coordinates. - #[deprecated(since = "0.3.0", note = "use screen_coordinates() instead")] - pub screen_x: i32, +impl std::fmt::Debug for MouseData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("MouseData") + .field("coordinates", &self.coordinates()) + .field("modifiers", &self.modifiers()) + .field("held_buttons", &self.held_buttons()) + .field("trigger_button", &self.trigger_button()) + .finish() + } +} - /// The Y coordinate of the mouse pointer in global (screen) coordinates. - #[deprecated(since = "0.3.0", note = "use screen_coordinates() instead")] - pub screen_y: i32, +impl PartialEq for MouseData { + fn eq(&self, other: &E) -> bool { + self.coordinates() == other.coordinates() + && self.modifiers() == other.modifiers() + && self.held_buttons() == other.held_buttons() + && self.trigger_button() == other.trigger_button() + } +} - /// True if the shift key was down when the mouse event was fired. - #[deprecated(since = "0.3.0", note = "use modifiers() instead")] - pub shift_key: bool, +/// A trait for any object that has the data for a mouse event +pub trait HasMouseData: PointerInteraction { + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; } impl_event! { @@ -150,131 +106,172 @@ impl_event! { } impl MouseData { - /// Construct MouseData with the specified properties + /// Create a new instance of MouseData + pub fn new(inner: impl HasMouseData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +impl InteractionLocation for MouseData { + fn client_coordinates(&self) -> ClientPoint { + self.inner.client_coordinates() + } + + fn page_coordinates(&self) -> PagePoint { + self.inner.page_coordinates() + } + + fn screen_coordinates(&self) -> ScreenPoint { + self.inner.screen_coordinates() + } +} + +impl InteractionElementOffset for MouseData { + fn element_coordinates(&self) -> ElementPoint { + self.inner.element_coordinates() + } + + fn coordinates(&self) -> Coordinates { + self.inner.coordinates() + } +} + +impl ModifiersInteraction for MouseData { + /// The set of modifier keys which were pressed when the event occurred + fn modifiers(&self) -> Modifiers { + self.inner.modifiers() + } +} + +impl PointerInteraction for MouseData { + /// The set of mouse buttons which were held when the event occurred. + fn held_buttons(&self) -> MouseButtonSet { + self.inner.held_buttons() + } + + /// The mouse button that triggered the event /// - /// Note: the current implementation truncates coordinates. In the future, when we change the internal representation, it may also support a fractional part. + // todo the following is kind of bad; should we just return None when the trigger_button is unreliable (and frankly irrelevant)? i guess we would need the event_type here + /// This is only guaranteed to indicate which button was pressed during events caused by pressing or releasing a button. As such, it is not reliable for events such as mouseenter, mouseleave, mouseover, mouseout, or mousemove. For example, a value of MouseButton::Primary may also indicate that no button was pressed. + fn trigger_button(&self) -> Option { + self.inner.trigger_button() + } +} + +impl PartialEq for MouseData { + fn eq(&self, other: &Self) -> bool { + self.coordinates() == other.coordinates() + && self.modifiers() == other.modifiers() + && self.held_buttons() == other.held_buttons() + && self.trigger_button() == other.trigger_button() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of [`MouseData`] +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone, Default)] +pub struct SerializedMouseData { + /// Common data for all pointer/mouse events + #[serde(flatten)] + point_data: crate::point_interaction::SerializedPointInteraction, +} + +#[cfg(feature = "serialize")] +impl SerializedMouseData { + /// Create a new instance of SerializedMouseData pub fn new( - coordinates: Coordinates, trigger_button: Option, held_buttons: MouseButtonSet, + coordinates: Coordinates, modifiers: Modifiers, ) -> Self { - let alt_key = modifiers.contains(Modifiers::ALT); - let ctrl_key = modifiers.contains(Modifiers::CONTROL); - let meta_key = modifiers.contains(Modifiers::META); - let shift_key = modifiers.contains(Modifiers::SHIFT); - - let [client_x, client_y]: [i32; 2] = coordinates.client().cast().into(); - let [offset_x, offset_y]: [i32; 2] = coordinates.element().cast().into(); - let [page_x, page_y]: [i32; 2] = coordinates.page().cast().into(); - let [screen_x, screen_y]: [i32; 2] = coordinates.screen().cast().into(); + Self { + point_data: crate::point_interaction::SerializedPointInteraction::new( + trigger_button, + held_buttons, + coordinates, + modifiers, + ), + } + } +} - #[allow(deprecated)] +#[cfg(feature = "serialize")] +impl From<&MouseData> for SerializedMouseData { + fn from(e: &MouseData) -> Self { Self { - alt_key, - ctrl_key, - meta_key, - shift_key, - - button: trigger_button.map_or(0, |b| b.into_web_code()), - buttons: encode_mouse_button_set(held_buttons), - - client_x, - client_y, - offset_x, - offset_y, - page_x, - page_y, - screen_x, - screen_y, + point_data: crate::point_interaction::SerializedPointInteraction::from(e), } } +} - /// The event's coordinates relative to the application's viewport (as opposed to the coordinate within the page). - /// - /// For example, clicking in the top left corner of the viewport will always result in a mouse event with client coordinates (0., 0.), regardless of whether the page is scrolled horizontally. - pub fn client_coordinates(&self) -> ClientPoint { - #[allow(deprecated)] - ClientPoint::new(self.client_x.into(), self.client_y.into()) +#[cfg(feature = "serialize")] +impl HasMouseData for SerializedMouseData { + fn as_any(&self) -> &dyn std::any::Any { + self } +} - /// The event's coordinates relative to the padding edge of the target element - /// - /// For example, clicking in the top left corner of an element will result in element coordinates (0., 0.) - pub fn element_coordinates(&self) -> ElementPoint { - #[allow(deprecated)] - ElementPoint::new(self.offset_x.into(), self.offset_y.into()) +#[cfg(feature = "serialize")] +impl InteractionLocation for SerializedMouseData { + fn client_coordinates(&self) -> ClientPoint { + self.point_data.client_coordinates() } - /// The event's coordinates relative to the entire document. This includes any portion of the document not currently visible. - /// - /// For example, if the page is scrolled 200 pixels to the right and 300 pixels down, clicking in the top left corner of the viewport would result in page coordinates (200., 300.) - pub fn page_coordinates(&self) -> PagePoint { - #[allow(deprecated)] - PagePoint::new(self.page_x.into(), self.page_y.into()) + fn page_coordinates(&self) -> PagePoint { + self.point_data.page_coordinates() } - /// The event's coordinates relative to the entire screen. This takes into account the window's offset. - pub fn screen_coordinates(&self) -> ScreenPoint { - #[allow(deprecated)] - ScreenPoint::new(self.screen_x.into(), self.screen_y.into()) + fn screen_coordinates(&self) -> ScreenPoint { + self.point_data.screen_coordinates() } +} - pub fn coordinates(&self) -> Coordinates { - Coordinates::new( - self.screen_coordinates(), - self.client_coordinates(), - self.element_coordinates(), - self.page_coordinates(), - ) +#[cfg(feature = "serialize")] +impl InteractionElementOffset for SerializedMouseData { + fn element_coordinates(&self) -> ElementPoint { + self.point_data.element_coordinates() } +} - /// The set of modifier keys which were pressed when the event occurred - pub fn modifiers(&self) -> Modifiers { - let mut modifiers = Modifiers::empty(); - - #[allow(deprecated)] - { - if self.alt_key { - modifiers.insert(Modifiers::ALT); - } - if self.ctrl_key { - modifiers.insert(Modifiers::CONTROL); - } - if self.meta_key { - modifiers.insert(Modifiers::META); - } - if self.shift_key { - modifiers.insert(Modifiers::SHIFT); - } - } +#[cfg(feature = "serialize")] +impl ModifiersInteraction for SerializedMouseData { + fn modifiers(&self) -> Modifiers { + self.point_data.modifiers() + } +} - modifiers +#[cfg(feature = "serialize")] +impl PointerInteraction for SerializedMouseData { + fn held_buttons(&self) -> MouseButtonSet { + self.point_data.held_buttons() } - /// The set of mouse buttons which were held when the event occurred. - pub fn held_buttons(&self) -> MouseButtonSet { - #[allow(deprecated)] - decode_mouse_button_set(self.buttons) + fn trigger_button(&self) -> Option { + self.point_data.trigger_button() } +} - /// The mouse button that triggered the event - /// - // todo the following is kind of bad; should we just return None when the trigger_button is unreliable (and frankly irrelevant)? i guess we would need the event_type here - /// This is only guaranteed to indicate which button was pressed during events caused by pressing or releasing a button. As such, it is not reliable for events such as mouseenter, mouseleave, mouseover, mouseout, or mousemove. For example, a value of MouseButton::Primary may also indicate that no button was pressed. - pub fn trigger_button(&self) -> Option { - #[allow(deprecated)] - Some(MouseButton::from_web_code(self.button)) +#[cfg(feature = "serialize")] +impl serde::Serialize for MouseData { + fn serialize(&self, serializer: S) -> Result { + SerializedMouseData::from(self).serialize(serializer) } } -impl Debug for MouseData { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.debug_struct("MouseData") - .field("coordinates", &self.coordinates()) - .field("modifiers", &self.modifiers()) - .field("held_buttons", &self.held_buttons()) - .field("trigger_button", &self.trigger_button()) - .finish() +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for MouseData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedMouseData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) } } diff --git a/packages/html/src/events/pointer.rs b/packages/html/src/events/pointer.rs index 4134ac8529..9556502d3f 100644 --- a/packages/html/src/events/pointer.rs +++ b/packages/html/src/events/pointer.rs @@ -1,33 +1,107 @@ use dioxus_core::Event; +use keyboard_types::Modifiers; +use crate::{geometry::*, input_data::*, prelude::*}; + +/// A synthetic event that wraps a web-style [`PointerEvent`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent) pub type PointerEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq)] + pub struct PointerData { - // Mouse only - pub alt_key: bool, - pub button: i16, - pub buttons: u16, - pub client_x: i32, - pub client_y: i32, - pub ctrl_key: bool, - pub meta_key: bool, - pub page_x: i32, - pub page_y: i32, - pub screen_x: i32, - pub screen_y: i32, - pub shift_key: bool, - pub pointer_id: i32, - pub width: i32, - pub height: i32, - pub pressure: f32, - pub tangential_pressure: f32, - pub tilt_x: i32, - pub tilt_y: i32, - pub twist: i32, - pub pointer_type: String, - pub is_primary: bool, - // pub get_modifier_state: bool, + inner: Box, +} + +impl PointerData { + /// Create a new PointerData + pub fn new(data: impl HasPointerData + 'static) -> Self { + Self::from(data) + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +impl From for PointerData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl std::fmt::Debug for PointerData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("PointerData") + .field("pointer_id", &self.pointer_id()) + .field("width", &self.width()) + .field("height", &self.height()) + .field("pressure", &self.pressure()) + .field("tangential_pressure", &self.tangential_pressure()) + .field("tilt_x", &self.tilt_x()) + .field("tilt_y", &self.tilt_y()) + .field("twist", &self.twist()) + .field("pointer_type", &self.pointer_type()) + .field("is_primary", &self.is_primary()) + .field("coordinates", &self.coordinates()) + .field("modifiers", &self.modifiers()) + .field("held_buttons", &self.held_buttons()) + .field("trigger_button", &self.trigger_button()) + .finish() + } +} + +impl PartialEq for PointerData { + fn eq(&self, other: &Self) -> bool { + self.pointer_id() == other.pointer_id() + && self.width() == other.width() + && self.height() == other.height() + && self.pressure() == other.pressure() + && self.tangential_pressure() == other.tangential_pressure() + && self.tilt_x() == other.tilt_x() + && self.tilt_y() == other.tilt_y() + && self.twist() == other.twist() + && self.pointer_type() == other.pointer_type() + && self.is_primary() == other.is_primary() + && self.coordinates() == other.coordinates() + && self.modifiers() == other.modifiers() + && self.held_buttons() == other.held_buttons() + && self.trigger_button() == other.trigger_button() + } +} + +/// A trait for any object that has the data for a pointer event +pub trait HasPointerData: PointerInteraction { + /// Gets the unique identifier of the pointer causing the event. + fn pointer_id(&self) -> i32; + + /// Gets the width (magnitude on the X axis), in CSS pixels, of the contact geometry of the pointer. + fn width(&self) -> i32; + + /// Gets the height (magnitude on the Y axis), in CSS pixels, of the contact geometry of the pointer. + fn height(&self) -> i32; + + /// Gets the normalized pressure of the pointer input in the range of 0 to 1, + fn pressure(&self) -> f32; + + /// Gets the normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1, + fn tangential_pressure(&self) -> f32; + + /// Gets the plane angle (in degrees, in the range of -90 to 90) between the Y-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the Y axis. + fn tilt_x(&self) -> i32; + + /// Gets the plane angle (in degrees, in the range of -90 to 90) between the X-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the X axis. + fn tilt_y(&self) -> i32; + + /// Gets the clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359. + fn twist(&self) -> i32; + + /// Gets the device type that caused the event (mouse, pen, touch, etc.). + fn pointer_type(&self) -> String; + + /// Gets if the pointer represents the primary pointer of this pointer type. + fn is_primary(&self) -> bool; + + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; } impl_event![ @@ -62,3 +136,253 @@ impl_event![ /// pointerout onpointerout ]; + +impl PointerData { + /// Gets the unique identifier of the pointer causing the event. + pub fn pointer_id(&self) -> i32 { + self.inner.pointer_id() + } + + /// Gets the width (magnitude on the X axis), in CSS pixels, of the contact geometry of the pointer. + pub fn width(&self) -> i32 { + self.inner.width() + } + + /// Gets the height (magnitude on the Y axis), in CSS pixels, of the contact geometry of the pointer. + pub fn height(&self) -> i32 { + self.inner.height() + } + + /// Gets the normalized pressure of the pointer input in the range of 0 to 1, + pub fn pressure(&self) -> f32 { + self.inner.pressure() + } + + /// Gets the normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1, + pub fn tangential_pressure(&self) -> f32 { + self.inner.tangential_pressure() + } + + /// Gets the plane angle (in degrees, in the range of -90 to 90) between the Y-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the Y axis. + pub fn tilt_x(&self) -> i32 { + self.inner.tilt_x() + } + + /// Gets the plane angle (in degrees, in the range of -90 to 90) between the X-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the X axis. + pub fn tilt_y(&self) -> i32 { + self.inner.tilt_y() + } + + /// Gets the clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359. + pub fn twist(&self) -> i32 { + self.inner.twist() + } + + /// Gets the device type that caused the event (mouse, pen, touch, etc.). + pub fn pointer_type(&self) -> String { + self.inner.pointer_type() + } + + /// Gets if the pointer represents the primary pointer of this pointer type. + pub fn is_primary(&self) -> bool { + self.inner.is_primary() + } +} + +impl InteractionLocation for PointerData { + fn client_coordinates(&self) -> ClientPoint { + self.inner.client_coordinates() + } + + fn screen_coordinates(&self) -> ScreenPoint { + self.inner.screen_coordinates() + } + + fn page_coordinates(&self) -> PagePoint { + self.inner.page_coordinates() + } +} + +impl InteractionElementOffset for PointerData { + fn element_coordinates(&self) -> ElementPoint { + self.inner.element_coordinates() + } +} + +impl ModifiersInteraction for PointerData { + fn modifiers(&self) -> Modifiers { + self.inner.modifiers() + } +} + +impl PointerInteraction for PointerData { + fn held_buttons(&self) -> MouseButtonSet { + self.inner.held_buttons() + } + + fn trigger_button(&self) -> Option { + self.inner.trigger_button() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of PointerData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedPointerData { + /// Common data for all pointer/mouse events + #[serde(flatten)] + point_data: crate::point_interaction::SerializedPointInteraction, + + /// The unique identifier of the pointer causing the event. + pointer_id: i32, + + /// The width (magnitude on the X axis), in CSS pixels, of the contact geometry of the pointer. + width: i32, + + /// The height (magnitude on the Y axis), in CSS pixels, of the contact geometry of the pointer. + height: i32, + + /// The normalized pressure of the pointer input in the range of 0 to 1, + pressure: f32, + + /// The normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1, + tangential_pressure: f32, + + /// The plane angle (in degrees, in the range of -90 to 90) between the Y-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the Y axis. + tilt_x: i32, + + /// The plane angle (in degrees, in the range of -90 to 90) between the X-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the X axis. + tilt_y: i32, + + /// The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359. + twist: i32, + + /// Indicates the device type that caused the event (mouse, pen, touch, etc.). + pointer_type: String, + + /// Indicates if the pointer represents the primary pointer of this pointer type. + is_primary: bool, +} + +#[cfg(feature = "serialize")] +impl HasPointerData for SerializedPointerData { + fn pointer_id(&self) -> i32 { + self.pointer_id + } + + fn width(&self) -> i32 { + self.width + } + + fn height(&self) -> i32 { + self.height + } + + fn pressure(&self) -> f32 { + self.pressure + } + + fn tangential_pressure(&self) -> f32 { + self.tangential_pressure + } + + fn tilt_x(&self) -> i32 { + self.tilt_x + } + + fn tilt_y(&self) -> i32 { + self.tilt_y + } + + fn twist(&self) -> i32 { + self.twist + } + + fn pointer_type(&self) -> String { + self.pointer_type.clone() + } + + fn is_primary(&self) -> bool { + self.is_primary + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl InteractionLocation for SerializedPointerData { + fn client_coordinates(&self) -> ClientPoint { + self.point_data.client_coordinates() + } + + fn screen_coordinates(&self) -> ScreenPoint { + self.point_data.screen_coordinates() + } + + fn page_coordinates(&self) -> PagePoint { + self.point_data.page_coordinates() + } +} + +#[cfg(feature = "serialize")] +impl InteractionElementOffset for SerializedPointerData { + fn element_coordinates(&self) -> ElementPoint { + self.point_data.element_coordinates() + } +} + +#[cfg(feature = "serialize")] +impl ModifiersInteraction for SerializedPointerData { + fn modifiers(&self) -> Modifiers { + self.point_data.modifiers() + } +} + +#[cfg(feature = "serialize")] +impl PointerInteraction for SerializedPointerData { + fn held_buttons(&self) -> MouseButtonSet { + self.point_data.held_buttons() + } + + fn trigger_button(&self) -> Option { + self.point_data.trigger_button() + } +} + +#[cfg(feature = "serialize")] +impl From<&PointerData> for SerializedPointerData { + fn from(data: &PointerData) -> Self { + Self { + point_data: data.into(), + pointer_id: data.pointer_id(), + width: data.width(), + height: data.height(), + pressure: data.pressure(), + tangential_pressure: data.tangential_pressure(), + tilt_x: data.tilt_x(), + tilt_y: data.tilt_y(), + twist: data.twist(), + pointer_type: data.pointer_type().to_string(), + is_primary: data.is_primary(), + } + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for PointerData { + fn serialize(&self, serializer: S) -> Result { + SerializedPointerData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for PointerData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedPointerData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} diff --git a/packages/html/src/events/scroll.rs b/packages/html/src/events/scroll.rs index 798218b020..3693cc1442 100644 --- a/packages/html/src/events/scroll.rs +++ b/packages/html/src/events/scroll.rs @@ -1,9 +1,83 @@ use dioxus_core::Event; pub type ScrollEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ScrollData {} + +pub struct ScrollData { + inner: Box, +} + +impl From for ScrollData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl ScrollData { + /// Create a new ScrollData + pub fn new(inner: impl HasScrollData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +impl std::fmt::Debug for ScrollData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ScrollData").finish() + } +} + +impl PartialEq for ScrollData { + fn eq(&self, _other: &Self) -> bool { + true + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of ScrollData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedScrollData {} + +#[cfg(feature = "serialize")] +impl From<&ScrollData> for SerializedScrollData { + fn from(_: &ScrollData) -> Self { + Self {} + } +} + +#[cfg(feature = "serialize")] +impl HasScrollData for SerializedScrollData { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for ScrollData { + fn serialize(&self, serializer: S) -> Result { + SerializedScrollData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for ScrollData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedScrollData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +pub trait HasScrollData: std::any::Any { + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; +} impl_event! { ScrollData; diff --git a/packages/html/src/events/selection.rs b/packages/html/src/events/selection.rs index 249353e63f..30c3d9bf01 100644 --- a/packages/html/src/events/selection.rs +++ b/packages/html/src/events/selection.rs @@ -1,9 +1,83 @@ use dioxus_core::Event; pub type SelectionEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct SelectionData {} + +pub struct SelectionData { + inner: Box, +} + +impl SelectionData { + /// Create a new SelectionData + pub fn new(inner: impl HasSelectionData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +impl From for SelectionData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl std::fmt::Debug for SelectionData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SelectionData").finish() + } +} + +impl PartialEq for SelectionData { + fn eq(&self, _other: &Self) -> bool { + true + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of SelectionData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedSelectionData {} + +#[cfg(feature = "serialize")] +impl From<&SelectionData> for SerializedSelectionData { + fn from(_: &SelectionData) -> Self { + Self {} + } +} + +#[cfg(feature = "serialize")] +impl HasSelectionData for SerializedSelectionData { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for SelectionData { + fn serialize(&self, serializer: S) -> Result { + SerializedSelectionData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for SelectionData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedSelectionData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +pub trait HasSelectionData: std::any::Any { + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; +} impl_event! [ SelectionData; diff --git a/packages/html/src/events/toggle.rs b/packages/html/src/events/toggle.rs index 4f225a8e60..e929a6a645 100644 --- a/packages/html/src/events/toggle.rs +++ b/packages/html/src/events/toggle.rs @@ -1,9 +1,83 @@ use dioxus_core::Event; pub type ToggleEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ToggleData {} + +pub struct ToggleData { + inner: Box, +} + +impl From for ToggleData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl std::fmt::Debug for ToggleData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ToggleData").finish() + } +} + +impl PartialEq for ToggleData { + fn eq(&self, _other: &Self) -> bool { + true + } +} + +impl ToggleData { + /// Create a new ToggleData + pub fn new(inner: impl HasToggleData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of ToggleData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedToggleData {} + +#[cfg(feature = "serialize")] +impl From<&ToggleData> for SerializedToggleData { + fn from(_: &ToggleData) -> Self { + Self {} + } +} + +#[cfg(feature = "serialize")] +impl HasToggleData for SerializedToggleData { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for ToggleData { + fn serialize(&self, serializer: S) -> Result { + SerializedToggleData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for ToggleData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedToggleData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +pub trait HasToggleData: std::any::Any { + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; +} impl_event! { ToggleData; diff --git a/packages/html/src/events/touch.rs b/packages/html/src/events/touch.rs index 1e795b6dac..b30ace17c6 100644 --- a/packages/html/src/events/touch.rs +++ b/packages/html/src/events/touch.rs @@ -1,17 +1,353 @@ use dioxus_core::Event; +use keyboard_types::Modifiers; + +use crate::geometry::*; +use crate::prelude::{InteractionLocation, ModifiersInteraction}; pub type TouchEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq, Eq)] pub struct TouchData { - pub alt_key: bool, - pub ctrl_key: bool, - pub meta_key: bool, - pub shift_key: bool, - // get_modifier_state: bool, - // changedTouches: DOMTouchList, - // targetTouches: DOMTouchList, - // touches: DOMTouchList, + inner: Box, +} + +impl From for TouchData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl std::fmt::Debug for TouchData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TouchData") + .field("modifiers", &self.modifiers()) + .field("touches", &self.touches()) + .field("touches_changed", &self.touches_changed()) + .field("target_touches", &self.target_touches()) + .finish() + } +} + +impl PartialEq for TouchData { + fn eq(&self, other: &Self) -> bool { + self.modifiers() == other.modifiers() + } +} + +impl TouchData { + /// Create a new TouchData + pub fn new(inner: impl HasTouchData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// Get the pointers that are currently down + pub fn touches(&self) -> Vec { + self.inner.touches() + } + + /// Get the touches that have changed since the last event + pub fn touches_changed(&self) -> Vec { + self.inner.touches_changed() + } + + /// Get the touches that started and stayed on the element that triggered this event + pub fn target_touches(&self) -> Vec { + self.inner.target_touches() + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +impl ModifiersInteraction for TouchData { + fn modifiers(&self) -> Modifiers { + self.inner.modifiers() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of TouchData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedTouchData { + alt_key: bool, + ctrl_key: bool, + meta_key: bool, + shift_key: bool, + touches: Vec, + changed_touches: Vec, + target_touches: Vec, +} + +#[cfg(feature = "serialize")] +impl From<&TouchData> for SerializedTouchData { + fn from(data: &TouchData) -> Self { + let modifiers = data.modifiers(); + Self { + alt_key: modifiers.contains(Modifiers::ALT), + ctrl_key: modifiers.contains(Modifiers::CONTROL), + meta_key: modifiers.contains(Modifiers::META), + shift_key: modifiers.contains(Modifiers::SHIFT), + touches: data.touches().iter().map(|t| t.into()).collect(), + changed_touches: data.touches_changed().iter().map(|t| t.into()).collect(), + target_touches: data.target_touches().iter().map(|t| t.into()).collect(), + } + } +} + +#[cfg(feature = "serialize")] +impl ModifiersInteraction for SerializedTouchData { + fn modifiers(&self) -> Modifiers { + let mut modifiers = Modifiers::default(); + if self.alt_key { + modifiers.insert(Modifiers::ALT); + } + if self.ctrl_key { + modifiers.insert(Modifiers::CONTROL); + } + if self.meta_key { + modifiers.insert(Modifiers::META); + } + if self.shift_key { + modifiers.insert(Modifiers::SHIFT); + } + modifiers + } +} + +#[cfg(feature = "serialize")] +impl HasTouchData for SerializedTouchData { + fn touches(&self) -> Vec { + self.touches.clone().into_iter().map(Into::into).collect() + } + + fn touches_changed(&self) -> Vec { + self.changed_touches + .clone() + .into_iter() + .map(Into::into) + .collect() + } + + fn target_touches(&self) -> Vec { + self.target_touches + .clone() + .into_iter() + .map(Into::into) + .collect() + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for TouchData { + fn serialize(&self, serializer: S) -> Result { + SerializedTouchData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for TouchData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedTouchData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +pub trait HasTouchData: ModifiersInteraction + std::any::Any { + /// Get the touches that are currently down + fn touches(&self) -> Vec; + + /// Get the touches that have changed since the last event + fn touches_changed(&self) -> Vec; + + /// Get the touches that started and stayed on the element that triggered this event + fn target_touches(&self) -> Vec; + + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; +} + +pub struct TouchPoint { + inner: Box, +} + +impl From for TouchPoint { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl TouchPoint { + /// Create a new TouchPoint + pub fn new(inner: impl HasTouchPointData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// A unique identifier for this touch point that will be the same for the duration of the touch + fn identifier(&self) -> i32 { + self.inner.identifier() + } + + /// the pressure of the touch + fn force(&self) -> f64 { + self.inner.force() + } + + /// the radius of the touch + fn radius(&self) -> ScreenPoint { + self.inner.radius() + } + + /// the rotation of the touch in degrees between 0 and 90 + fn rotation(&self) -> f64 { + self.inner.rotation() + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +impl std::fmt::Debug for TouchPoint { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TouchPoint") + .field("client_coordinates", &self.client_coordinates()) + .field("page_coordinates", &self.page_coordinates()) + .field("screen_coordinates", &self.screen_coordinates()) + .field("identifier", &self.identifier()) + .field("force", &self.force()) + .field("radius", &self.radius()) + .field("rotation", &self.rotation()) + .finish() + } +} + +impl InteractionLocation for TouchPoint { + fn client_coordinates(&self) -> ClientPoint { + self.inner.client_coordinates() + } + + fn page_coordinates(&self) -> PagePoint { + self.inner.page_coordinates() + } + + fn screen_coordinates(&self) -> ScreenPoint { + self.inner.screen_coordinates() + } +} + +/// A trait for touch point data +pub trait HasTouchPointData: InteractionLocation + std::any::Any { + /// A unique identifier for this touch point that will be the same for the duration of the touch + fn identifier(&self) -> i32; + + /// the pressure of the touch + fn force(&self) -> f64; + + /// the radius of the touch + fn radius(&self) -> ScreenPoint; + + /// the rotation of the touch in degrees between 0 and 90 + fn rotation(&self) -> f64; + + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; +} + +#[cfg(feature = "serialize")] +/// A serialized version of TouchPoint +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +struct SerializedTouchPoint { + identifier: i32, + client_x: f64, + client_y: f64, + page_x: f64, + page_y: f64, + screen_x: f64, + screen_y: f64, + force: f64, + radius_x: f64, + radius_y: f64, + rotation_angle: f64, +} + +#[cfg(feature = "serialize")] +impl From<&TouchPoint> for SerializedTouchPoint { + fn from(point: &TouchPoint) -> Self { + let client_coordinates = point.client_coordinates(); + + let page_coordinates = point.page_coordinates(); + let screen_coordinates = point.screen_coordinates(); + Self { + identifier: point.identifier(), + client_x: client_coordinates.x, + client_y: client_coordinates.y, + page_x: page_coordinates.x, + page_y: page_coordinates.y, + screen_x: screen_coordinates.x, + screen_y: screen_coordinates.y, + force: point.force(), + radius_x: point.radius().x, + radius_y: point.radius().y, + rotation_angle: point.rotation(), + } + } +} + +#[cfg(feature = "serialize")] +impl HasTouchPointData for SerializedTouchPoint { + /// A unique identifier for this touch point that will be the same for the duration of the touch + fn identifier(&self) -> i32 { + self.identifier + } + + /// the pressure of the touch + fn force(&self) -> f64 { + self.force + } + + /// the radius of the touch + fn radius(&self) -> ScreenPoint { + ScreenPoint::new(self.radius_x, self.radius_y) + } + + /// the rotation of the touch in degrees between 0 and 90 + fn rotation(&self) -> f64 { + self.rotation_angle + } + + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl InteractionLocation for SerializedTouchPoint { + /// Gets the coordinates of the event relative to the browser viewport. + fn client_coordinates(&self) -> ClientPoint { + ClientPoint::new(self.client_x, self.client_y) + } + + /// Gets the coordinates of the event relative to the screen. + fn screen_coordinates(&self) -> ScreenPoint { + ScreenPoint::new(self.screen_x, self.screen_y) + } + + /// Gets the coordinates of the event relative to the page. + fn page_coordinates(&self) -> PagePoint { + PagePoint::new(self.page_x, self.page_y) + } } impl_event! { diff --git a/packages/html/src/events/transition.rs b/packages/html/src/events/transition.rs index ecc3e16360..4e680d650f 100644 --- a/packages/html/src/events/transition.rs +++ b/packages/html/src/events/transition.rs @@ -1,12 +1,111 @@ use dioxus_core::Event; pub type TransitionEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Debug, Clone, PartialEq)] + pub struct TransitionData { - pub property_name: String, - pub pseudo_element: String, - pub elapsed_time: f32, + inner: Box, +} + +impl From for TransitionData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} + +impl std::fmt::Debug for TransitionData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TransitionData") + .field("property_name", &self.inner.property_name()) + .field("pseudo_element", &self.inner.pseudo_element()) + .field("elapsed_time", &self.inner.elapsed_time()) + .finish() + } +} + +impl PartialEq for TransitionData { + fn eq(&self, other: &Self) -> bool { + self.inner.property_name() == other.inner.property_name() + && self.inner.pseudo_element() == other.inner.pseudo_element() + && self.inner.elapsed_time() == other.inner.elapsed_time() + } +} + +impl TransitionData { + /// Create a new TransitionData + pub fn new(inner: impl HasTransitionData + 'static) -> Self { + Self { + inner: Box::new(inner), + } + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of TransitionData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedTransitionData { + property_name: String, + pseudo_element: String, + elapsed_time: f32, +} + +#[cfg(feature = "serialize")] +impl From<&TransitionData> for SerializedTransitionData { + fn from(data: &TransitionData) -> Self { + Self { + property_name: data.inner.property_name(), + pseudo_element: data.inner.pseudo_element(), + elapsed_time: data.inner.elapsed_time(), + } + } +} + +#[cfg(feature = "serialize")] +impl HasTransitionData for SerializedTransitionData { + fn property_name(&self) -> String { + self.property_name.clone() + } + + fn pseudo_element(&self) -> String { + self.pseudo_element.clone() + } + + fn elapsed_time(&self) -> f32 { + self.elapsed_time + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +#[cfg(feature = "serialize")] +impl serde::Serialize for TransitionData { + fn serialize(&self, serializer: S) -> Result { + SerializedTransitionData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for TransitionData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedTransitionData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +pub trait HasTransitionData: std::any::Any { + fn property_name(&self) -> String; + fn pseudo_element(&self) -> String; + fn elapsed_time(&self) -> f32; + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; } impl_event! { diff --git a/packages/html/src/events/wheel.rs b/packages/html/src/events/wheel.rs index 758fa79364..2661eee2b6 100644 --- a/packages/html/src/events/wheel.rs +++ b/packages/html/src/events/wheel.rs @@ -1,78 +1,129 @@ use dioxus_core::Event; -use euclid::UnknownUnit; -use std::fmt::{Debug, Formatter}; +use std::fmt::Formatter; -use crate::geometry::{LinesVector, PagesVector, PixelsVector, WheelDelta}; +use crate::geometry::WheelDelta; pub type WheelEvent = Event; -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[derive(Clone, PartialEq, Default)] + pub struct WheelData { - #[deprecated(since = "0.3.0", note = "use delta() instead")] - pub delta_mode: u32, - #[deprecated(since = "0.3.0", note = "use delta() instead")] - pub delta_x: f64, - #[deprecated(since = "0.3.0", note = "use delta() instead")] - pub delta_y: f64, - #[deprecated(since = "0.3.0", note = "use delta() instead")] - pub delta_z: f64, + inner: Box, } -impl_event![ - WheelData; - - /// Called when the mouse wheel is rotated over an element. - onwheel -]; +impl From for WheelData { + fn from(e: E) -> Self { + Self { inner: Box::new(e) } + } +} -impl WheelData { - /// Construct a new WheelData with the specified wheel movement delta - pub fn new(delta: WheelDelta) -> Self { - let (delta_mode, vector) = match delta { - WheelDelta::Pixels(v) => (0, v.cast_unit::()), - WheelDelta::Lines(v) => (1, v.cast_unit::()), - WheelDelta::Pages(v) => (2, v.cast_unit::()), - }; +impl std::fmt::Debug for WheelData { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_struct("WheelData") + .field("delta", &self.delta()) + .finish() + } +} - #[allow(deprecated)] - WheelData { - delta_mode, - delta_x: vector.x, - delta_y: vector.y, - delta_z: vector.z, - } +impl PartialEq for WheelData { + fn eq(&self, other: &Self) -> bool { + self.inner.delta() == other.inner.delta() } +} - /// Construct from the attributes of the web wheel event - pub fn from_web_attributes(delta_mode: u32, delta_x: f64, delta_y: f64, delta_z: f64) -> Self { - #[allow(deprecated)] +impl WheelData { + /// Create a new WheelData + pub fn new(inner: impl HasWheelData + 'static) -> Self { Self { - delta_mode, - delta_x, - delta_y, - delta_z, + inner: Box::new(inner), } } /// The amount of wheel movement #[allow(deprecated)] pub fn delta(&self) -> WheelDelta { - let x = self.delta_x; - let y = self.delta_y; - let z = self.delta_z; - match self.delta_mode { - 0 => WheelDelta::Pixels(PixelsVector::new(x, y, z)), - 1 => WheelDelta::Lines(LinesVector::new(x, y, z)), - 2 => WheelDelta::Pages(PagesVector::new(x, y, z)), - _ => panic!("Invalid delta mode, {:?}", self.delta_mode), + self.inner.delta() + } + + /// Downcast this event to a concrete event type + pub fn downcast(&self) -> Option<&T> { + self.inner.as_any().downcast_ref::() + } +} + +#[cfg(feature = "serialize")] +/// A serialized version of WheelData +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +pub struct SerializedWheelData { + pub delta_mode: u32, + pub delta_x: f64, + pub delta_y: f64, + pub delta_z: f64, +} + +#[cfg(feature = "serialize")] +impl SerializedWheelData { + /// Create a new SerializedWheelData + pub fn new(delta: WheelDelta) -> Self { + let delta_mode = match delta { + WheelDelta::Pixels(_) => 0, + WheelDelta::Lines(_) => 1, + WheelDelta::Pages(_) => 2, + }; + let delta_raw = delta.strip_units(); + Self { + delta_mode, + delta_x: delta_raw.x, + delta_y: delta_raw.y, + delta_z: delta_raw.z, } } } -impl Debug for WheelData { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.debug_struct("WheelData") - .field("delta", &self.delta()) - .finish() +#[cfg(feature = "serialize")] +impl From<&WheelData> for SerializedWheelData { + fn from(data: &WheelData) -> Self { + Self::new(data.delta()) + } +} + +#[cfg(feature = "serialize")] +impl HasWheelData for SerializedWheelData { + fn delta(&self) -> WheelDelta { + WheelDelta::from_web_attributes(self.delta_mode, self.delta_x, self.delta_y, self.delta_z) + } + + fn as_any(&self) -> &dyn std::any::Any { + self } } + +#[cfg(feature = "serialize")] +impl serde::Serialize for WheelData { + fn serialize(&self, serializer: S) -> Result { + SerializedWheelData::from(self).serialize(serializer) + } +} + +#[cfg(feature = "serialize")] +impl<'de> serde::Deserialize<'de> for WheelData { + fn deserialize>(deserializer: D) -> Result { + let data = SerializedWheelData::deserialize(deserializer)?; + Ok(Self { + inner: Box::new(data), + }) + } +} + +impl_event![ + WheelData; + + /// Called when the mouse wheel is rotated over an element. + onwheel +]; + +pub trait HasWheelData: std::any::Any { + /// The amount of wheel movement + fn delta(&self) -> WheelDelta; + + /// return self as Any + fn as_any(&self) -> &dyn std::any::Any; +} diff --git a/packages/html/src/geometry.rs b/packages/html/src/geometry.rs index 990ca5b5e6..4a0b16f4a1 100644 --- a/packages/html/src/geometry.rs +++ b/packages/html/src/geometry.rs @@ -47,7 +47,7 @@ pub type PagesVector = Vector3D; /// A vector representing the amount the mouse wheel was moved /// /// This may be expressed in Pixels, Lines or Pages -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] pub enum WheelDelta { /// Movement in Pixels @@ -59,6 +59,16 @@ pub enum WheelDelta { } impl WheelDelta { + /// Construct from the attributes of the web wheel event + pub fn from_web_attributes(delta_mode: u32, delta_x: f64, delta_y: f64, delta_z: f64) -> Self { + match delta_mode { + 0 => WheelDelta::Pixels(PixelsVector::new(delta_x, delta_y, delta_z)), + 1 => WheelDelta::Lines(LinesVector::new(delta_x, delta_y, delta_z)), + 2 => WheelDelta::Pages(PagesVector::new(delta_x, delta_y, delta_z)), + _ => panic!("Invalid delta mode, {:?}", delta_mode), + } + } + /// Convenience function for constructing a WheelDelta with pixel units pub fn pixels(x: f64, y: f64, z: f64) -> Self { WheelDelta::Pixels(PixelsVector::new(x, y, z)) @@ -96,7 +106,7 @@ impl WheelDelta { } /// Coordinates of a point in the app's interface -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct Coordinates { screen: ScreenPoint, client: ClientPoint, diff --git a/packages/html/src/input_data.rs b/packages/html/src/input_data.rs index 28d182fdc0..d354c78a9d 100644 --- a/packages/html/src/input_data.rs +++ b/packages/html/src/input_data.rs @@ -8,9 +8,10 @@ use keyboard_types::Location; /// A mouse button type (such as Primary/Secondary) // note: EnumSetType also derives Copy and Clone for some reason #[allow(clippy::unused_unit)] -#[derive(EnumSetType, Debug)] +#[derive(EnumSetType, Debug, Default)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] pub enum MouseButton { + #[default] /// Primary button (typically the left button) Primary, /// Secondary button (typically the right button) diff --git a/packages/html/src/lib.rs b/packages/html/src/lib.rs index 2fc1baeac1..e8ee3ffb86 100644 --- a/packages/html/src/lib.rs +++ b/packages/html/src/lib.rs @@ -22,6 +22,7 @@ mod global_attributes; pub mod input_data; #[cfg(feature = "native-bind")] pub mod native_bind; +pub mod point_interaction; mod render_template; #[cfg(feature = "wasm-bind")] mod web_sys_bind; @@ -37,9 +38,13 @@ pub use events::*; pub use global_attributes::*; pub use render_template::*; -mod eval; +#[cfg(feature = "eval")] +pub mod eval; pub mod prelude { + #[cfg(feature = "eval")] pub use crate::eval::*; pub use crate::events::*; + pub use crate::point_interaction::*; + pub use keyboard_types::{self, Code, Key, Location, Modifiers}; } diff --git a/packages/html/src/point_interaction.rs b/packages/html/src/point_interaction.rs new file mode 100644 index 0000000000..9aa4e0f31e --- /dev/null +++ b/packages/html/src/point_interaction.rs @@ -0,0 +1,214 @@ +use keyboard_types::Modifiers; + +use crate::{ + geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint}, + input_data::{MouseButton, MouseButtonSet}, +}; + +/// A interaction that contains data about the location of the event. +pub trait InteractionLocation { + /// Gets the coordinates of the event relative to the browser viewport. + fn client_coordinates(&self) -> ClientPoint; + + /// Gets the coordinates of the event relative to the screen. + fn screen_coordinates(&self) -> ScreenPoint; + + /// Gets the coordinates of the event relative to the page. + fn page_coordinates(&self) -> PagePoint; +} + +/// A interaction that contains data about the location of the event. +pub trait InteractionElementOffset: InteractionLocation { + /// Gets the coordinates of the event. + fn coordinates(&self) -> Coordinates { + Coordinates::new( + self.screen_coordinates(), + self.client_coordinates(), + self.element_coordinates(), + self.page_coordinates(), + ) + } + + /// Gets the coordinates of the event relative to the target element. + fn element_coordinates(&self) -> ElementPoint; +} + +/// A interaction that contains data about the pointer button(s) that triggered the event. +pub trait PointerInteraction: InteractionElementOffset + ModifiersInteraction { + /// Gets the button that triggered the event. + fn trigger_button(&self) -> Option; + + /// Gets the buttons that are currently held down. + fn held_buttons(&self) -> MouseButtonSet; +} + +/// A interaction that contains data about the current state of the keyboard modifiers. +pub trait ModifiersInteraction { + /// Gets the modifiers of the pointer event. + fn modifiers(&self) -> Modifiers; +} + +#[cfg(feature = "serialize")] +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone, Default)] +pub(crate) struct SerializedPointInteraction { + pub alt_key: bool, + + /// The button number that was pressed (if applicable) when the mouse event was fired. + pub button: i16, + + /// Indicates which buttons are pressed on the mouse (or other input device) when a mouse event is triggered. + /// + /// Each button that can be pressed is represented by a given number (see below). If more than one button is pressed, the button values are added together to produce a new number. For example, if the secondary (2) and auxiliary (4) buttons are pressed simultaneously, the value is 6 (i.e., 2 + 4). + /// + /// - 1: Primary button (usually the left button) + /// - 2: Secondary button (usually the right button) + /// - 4: Auxiliary button (usually the mouse wheel button or middle button) + /// - 8: 4th button (typically the "Browser Back" button) + /// - 16 : 5th button (typically the "Browser Forward" button) + pub buttons: u16, + + /// The horizontal coordinate within the application's viewport at which the event occurred (as opposed to the coordinate within the page). + /// + /// For example, clicking on the left edge of the viewport will always result in a mouse event with a clientX value of 0, regardless of whether the page is scrolled horizontally. + pub client_x: i32, + + /// The vertical coordinate within the application's viewport at which the event occurred (as opposed to the coordinate within the page). + /// + /// For example, clicking on the top edge of the viewport will always result in a mouse event with a clientY value of 0, regardless of whether the page is scrolled vertically. + pub client_y: i32, + + /// True if the control key was down when the mouse event was fired. + pub ctrl_key: bool, + + /// True if the meta key was down when the mouse event was fired. + pub meta_key: bool, + + /// The offset in the X coordinate of the mouse pointer between that event and the padding edge of the target node. + pub offset_x: i32, + + /// The offset in the Y coordinate of the mouse pointer between that event and the padding edge of the target node. + pub offset_y: i32, + + /// The X (horizontal) coordinate (in pixels) of the mouse, relative to the left edge of the entire document. This includes any portion of the document not currently visible. + /// + /// Being based on the edge of the document as it is, this property takes into account any horizontal scrolling of the page. For example, if the page is scrolled such that 200 pixels of the left side of the document are scrolled out of view, and the mouse is clicked 100 pixels inward from the left edge of the view, the value returned by pageX will be 300. + pub page_x: i32, + + /// The Y (vertical) coordinate in pixels of the event relative to the whole document. + /// + /// See `page_x`. + pub page_y: i32, + + /// The X coordinate of the mouse pointer in global (screen) coordinates. + pub screen_x: i32, + + /// The Y coordinate of the mouse pointer in global (screen) coordinates. + pub screen_y: i32, + + /// True if the shift key was down when the mouse event was fired. + pub shift_key: bool, +} + +#[cfg(feature = "serialize")] +impl SerializedPointInteraction { + pub fn new( + trigger_button: Option, + held_buttons: MouseButtonSet, + coordinates: Coordinates, + modifiers: Modifiers, + ) -> Self { + let alt_key = modifiers.contains(Modifiers::ALT); + let ctrl_key = modifiers.contains(Modifiers::CONTROL); + let meta_key = modifiers.contains(Modifiers::META); + let shift_key = modifiers.contains(Modifiers::SHIFT); + + let [client_x, client_y]: [i32; 2] = coordinates.client().cast().into(); + let [offset_x, offset_y]: [i32; 2] = coordinates.element().cast().into(); + let [page_x, page_y]: [i32; 2] = coordinates.page().cast().into(); + let [screen_x, screen_y]: [i32; 2] = coordinates.screen().cast().into(); + Self { + button: trigger_button + .map_or(MouseButton::default(), |b| b) + .into_web_code(), + buttons: crate::input_data::encode_mouse_button_set(held_buttons), + meta_key, + ctrl_key, + shift_key, + alt_key, + client_x, + client_y, + screen_x, + screen_y, + offset_x, + offset_y, + page_x, + page_y, + } + } +} + +#[cfg(feature = "serialize")] +impl From<&E> for SerializedPointInteraction { + fn from(data: &E) -> Self { + let trigger_button = data.trigger_button(); + let held_buttons = data.held_buttons(); + let coordinates = data.coordinates(); + let modifiers = data.modifiers(); + Self::new(trigger_button, held_buttons, coordinates, modifiers) + } +} + +#[cfg(feature = "serialize")] +impl PointerInteraction for SerializedPointInteraction { + fn held_buttons(&self) -> MouseButtonSet { + crate::input_data::decode_mouse_button_set(self.buttons) + } + + fn trigger_button(&self) -> Option { + Some(MouseButton::from_web_code(self.button)) + } +} + +#[cfg(feature = "serialize")] +impl ModifiersInteraction for SerializedPointInteraction { + fn modifiers(&self) -> Modifiers { + let mut modifiers = Modifiers::empty(); + + if self.alt_key { + modifiers.insert(Modifiers::ALT); + } + if self.ctrl_key { + modifiers.insert(Modifiers::CONTROL); + } + if self.meta_key { + modifiers.insert(Modifiers::META); + } + if self.shift_key { + modifiers.insert(Modifiers::SHIFT); + } + + modifiers + } +} + +#[cfg(feature = "serialize")] +impl InteractionLocation for SerializedPointInteraction { + fn client_coordinates(&self) -> ClientPoint { + ClientPoint::new(self.client_x.into(), self.client_y.into()) + } + + fn screen_coordinates(&self) -> ScreenPoint { + ScreenPoint::new(self.screen_x.into(), self.screen_y.into()) + } + + fn page_coordinates(&self) -> PagePoint { + PagePoint::new(self.page_x.into(), self.page_y.into()) + } +} + +#[cfg(feature = "serialize")] +impl InteractionElementOffset for SerializedPointInteraction { + fn element_coordinates(&self) -> ElementPoint { + ElementPoint::new(self.offset_x.into(), self.offset_y.into()) + } +} diff --git a/packages/html/src/transit.rs b/packages/html/src/transit.rs index b0fa0d16f5..17a8a5f2d7 100644 --- a/packages/html/src/transit.rs +++ b/packages/html/src/transit.rs @@ -4,7 +4,8 @@ use crate::events::*; use dioxus_core::ElementId; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Debug, Clone, PartialEq)] +#[cfg(feature = "serialize")] +#[derive(Serialize, Debug, PartialEq)] pub struct HtmlEvent { pub element: ElementId, pub name: String, @@ -12,6 +13,7 @@ pub struct HtmlEvent { pub data: EventData, } +#[cfg(feature = "serialize")] impl<'de> Deserialize<'de> for HtmlEvent { fn deserialize(deserializer: D) -> Result where @@ -41,6 +43,7 @@ impl<'de> Deserialize<'de> for HtmlEvent { } } +#[cfg(feature = "serialize")] fn fun_name( name: &str, data: serde_value::Value, @@ -130,57 +133,90 @@ fn fun_name( Ok(data) } +#[cfg(feature = "serialize")] impl HtmlEvent { pub fn bubbles(&self) -> bool { event_bubbles(&self.name) } } -#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)] +#[derive(Deserialize, Serialize, Debug, PartialEq)] #[serde(untagged)] #[non_exhaustive] pub enum EventData { - Mouse(MouseData), - Clipboard(ClipboardData), - Composition(CompositionData), - Keyboard(KeyboardData), - Focus(FocusData), - Form(FormData), - Drag(DragData), - Pointer(PointerData), - Selection(SelectionData), - Touch(TouchData), - Scroll(ScrollData), - Wheel(WheelData), - Media(MediaData), - Animation(AnimationData), - Transition(TransitionData), - Toggle(ToggleData), - Image(ImageData), + Mouse(SerializedMouseData), + Clipboard(SerializedClipboardData), + Composition(SerializedCompositionData), + Keyboard(SerializedKeyboardData), + Focus(SerializedFocusData), + Form(SerializedFormData), + Drag(SerializedDragData), + Pointer(SerializedPointerData), + Selection(SerializedSelectionData), + Touch(SerializedTouchData), + Scroll(SerializedScrollData), + Wheel(SerializedWheelData), + Media(SerializedMediaData), + Animation(SerializedAnimationData), + Transition(SerializedTransitionData), + Toggle(SerializedToggleData), + Image(SerializedImageData), Mounted, } impl EventData { pub fn into_any(self) -> Rc { match self { - EventData::Mouse(data) => Rc::new(data) as Rc, - EventData::Clipboard(data) => Rc::new(data) as Rc, - EventData::Composition(data) => Rc::new(data) as Rc, - EventData::Keyboard(data) => Rc::new(data) as Rc, - EventData::Focus(data) => Rc::new(data) as Rc, - EventData::Form(data) => Rc::new(data) as Rc, - EventData::Drag(data) => Rc::new(data) as Rc, - EventData::Pointer(data) => Rc::new(data) as Rc, - EventData::Selection(data) => Rc::new(data) as Rc, - EventData::Touch(data) => Rc::new(data) as Rc, - EventData::Scroll(data) => Rc::new(data) as Rc, - EventData::Wheel(data) => Rc::new(data) as Rc, - EventData::Media(data) => Rc::new(data) as Rc, - EventData::Animation(data) => Rc::new(data) as Rc, - EventData::Transition(data) => Rc::new(data) as Rc, - EventData::Toggle(data) => Rc::new(data) as Rc, - EventData::Image(data) => Rc::new(data) as Rc, - EventData::Mounted => Rc::new(MountedData::new(())) as Rc, + EventData::Mouse(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Clipboard(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Composition(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Keyboard(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Focus(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Form(data) => Rc::new(PlatformEventData::new(Box::new(data))) as Rc, + EventData::Drag(data) => Rc::new(PlatformEventData::new(Box::new(data))) as Rc, + EventData::Pointer(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Selection(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Touch(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Scroll(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Wheel(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Media(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Animation(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Transition(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Toggle(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Image(data) => { + Rc::new(PlatformEventData::new(Box::new(data))) as Rc + } + EventData::Mounted => { + Rc::new(PlatformEventData::new(Box::new(MountedData::new(())))) as Rc + } } } } @@ -189,7 +225,7 @@ impl EventData { fn test_back_and_forth() { let data = HtmlEvent { element: ElementId(0), - data: EventData::Mouse(MouseData::default()), + data: EventData::Mouse(SerializedMouseData::default()), name: "click".to_string(), bubbles: true, }; @@ -224,3 +260,148 @@ fn test_back_and_forth() { assert_eq!(data, p); } + +/// A trait for converting from a serialized event to a concrete event type. +pub struct SerializedHtmlEventConverter; + +impl HtmlEventConverter for SerializedHtmlEventConverter { + fn convert_animation_data(&self, event: &PlatformEventData) -> AnimationData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_clipboard_data(&self, event: &PlatformEventData) -> ClipboardData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_composition_data(&self, event: &PlatformEventData) -> CompositionData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_drag_data(&self, event: &PlatformEventData) -> DragData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_focus_data(&self, event: &PlatformEventData) -> FocusData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_form_data(&self, event: &PlatformEventData) -> FormData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_image_data(&self, event: &PlatformEventData) -> ImageData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_keyboard_data(&self, event: &PlatformEventData) -> KeyboardData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_media_data(&self, event: &PlatformEventData) -> MediaData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_mounted_data(&self, _: &PlatformEventData) -> MountedData { + MountedData::from(()) + } + + fn convert_mouse_data(&self, event: &PlatformEventData) -> MouseData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_pointer_data(&self, event: &PlatformEventData) -> PointerData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_scroll_data(&self, event: &PlatformEventData) -> ScrollData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_selection_data(&self, event: &PlatformEventData) -> SelectionData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_toggle_data(&self, event: &PlatformEventData) -> ToggleData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_touch_data(&self, event: &PlatformEventData) -> TouchData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_transition_data(&self, event: &PlatformEventData) -> TransitionData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_wheel_data(&self, event: &PlatformEventData) -> WheelData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } +} diff --git a/packages/html/src/web_sys_bind/events.rs b/packages/html/src/web_sys_bind/events.rs index e5a6c0d783..b1e6c53414 100644 --- a/packages/html/src/web_sys_bind/events.rs +++ b/packages/html/src/web_sys_bind/events.rs @@ -1,17 +1,17 @@ +use crate::events::HasKeyboardData; use crate::events::{ AnimationData, CompositionData, KeyboardData, MouseData, PointerData, TouchData, TransitionData, WheelData, }; -use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint}; +use crate::geometry::{ClientPoint, ElementPoint, PagePoint, ScreenPoint}; use crate::input_data::{decode_key_location, decode_mouse_button_set, MouseButton}; -use crate::{DragData, MountedData}; +use crate::prelude::*; use keyboard_types::{Code, Key, Modifiers}; -use std::convert::TryInto; use std::str::FromStr; use wasm_bindgen::{JsCast, JsValue}; use web_sys::{ - AnimationEvent, CompositionEvent, Event, KeyboardEvent, MouseEvent, PointerEvent, TouchEvent, - TransitionEvent, WheelEvent, + AnimationEvent, CompositionEvent, Event, KeyboardEvent, MouseEvent, PointerEvent, Touch, + TouchEvent, TransitionEvent, WheelEvent, }; macro_rules! uncheck_convert { @@ -20,7 +20,7 @@ macro_rules! uncheck_convert { #[inline] fn from(e: Event) -> Self { let e: $t = e.unchecked_into(); - Self::from(&e) + Self::from(e) } } @@ -28,7 +28,7 @@ macro_rules! uncheck_convert { #[inline] fn from(e: &Event) -> Self { let e: &$t = e.unchecked_ref(); - Self::from(e) + Self::from(e.clone()) } } }; @@ -38,159 +38,368 @@ macro_rules! uncheck_convert { } uncheck_convert![ - CompositionEvent => CompositionData, - KeyboardEvent => KeyboardData, - MouseEvent => MouseData, - MouseEvent => DragData, - TouchEvent => TouchData, - PointerEvent => PointerData, - WheelEvent => WheelData, - AnimationEvent => AnimationData, - TransitionEvent => TransitionData, + web_sys::CompositionEvent => CompositionData, + web_sys::KeyboardEvent => KeyboardData, + web_sys::MouseEvent => MouseData, + web_sys::TouchEvent => TouchData, + web_sys::PointerEvent => PointerData, + web_sys::WheelEvent => WheelData, + web_sys::AnimationEvent => AnimationData, + web_sys::TransitionEvent => TransitionData, + web_sys::MouseEvent => DragData, + web_sys::FocusEvent => FocusData, ]; -impl From<&CompositionEvent> for CompositionData { - fn from(e: &CompositionEvent) -> Self { - Self { - data: e.data().unwrap_or_default(), - } +impl HasCompositionData for CompositionEvent { + fn data(&self) -> std::string::String { + self.data().unwrap_or_default() + } + + fn as_any(&self) -> &dyn std::any::Any { + self } } -impl From<&KeyboardEvent> for KeyboardData { - fn from(e: &KeyboardEvent) -> Self { +impl HasKeyboardData for KeyboardEvent { + fn key(&self) -> Key { + Key::from_str(self.key().as_str()).unwrap_or(Key::Unidentified) + } + + fn code(&self) -> Code { + Code::from_str(self.code().as_str()).unwrap_or(Code::Unidentified) + } + + fn location(&self) -> keyboard_types::Location { + decode_key_location(self.location() as usize) + } + + fn is_auto_repeating(&self) -> bool { + self.repeat() + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +impl ModifiersInteraction for KeyboardEvent { + fn modifiers(&self) -> Modifiers { let mut modifiers = Modifiers::empty(); - if e.alt_key() { + if self.alt_key() { modifiers.insert(Modifiers::ALT); } - if e.ctrl_key() { + if self.ctrl_key() { modifiers.insert(Modifiers::CONTROL); } - if e.meta_key() { + if self.meta_key() { modifiers.insert(Modifiers::META); } - if e.shift_key() { + if self.shift_key() { modifiers.insert(Modifiers::SHIFT); } - Self::new( - Key::from_str(&e.key()).expect("could not parse key"), - Code::from_str(&e.code()).expect("could not parse code"), - decode_key_location( - e.location() - .try_into() - .expect("could not convert location to u32"), - ), - e.repeat(), - modifiers, - ) + modifiers } } -impl From<&MouseEvent> for MouseData { - fn from(e: &MouseEvent) -> Self { +impl HasDragData for MouseEvent {} + +impl InteractionLocation for MouseEvent { + fn client_coordinates(&self) -> ClientPoint { + ClientPoint::new(self.client_x().into(), self.client_y().into()) + } + + fn page_coordinates(&self) -> PagePoint { + PagePoint::new(self.page_x().into(), self.page_y().into()) + } + + fn screen_coordinates(&self) -> ScreenPoint { + ScreenPoint::new(self.screen_x().into(), self.screen_y().into()) + } +} + +impl InteractionElementOffset for MouseEvent { + fn element_coordinates(&self) -> ElementPoint { + ElementPoint::new(self.offset_x().into(), self.offset_y().into()) + } +} + +impl ModifiersInteraction for MouseEvent { + fn modifiers(&self) -> Modifiers { let mut modifiers = Modifiers::empty(); - if e.alt_key() { + if self.alt_key() { modifiers.insert(Modifiers::ALT); } - if e.ctrl_key() { + if self.ctrl_key() { modifiers.insert(Modifiers::CONTROL); } - if e.meta_key() { + if self.meta_key() { modifiers.insert(Modifiers::META); } - if e.shift_key() { + if self.shift_key() { modifiers.insert(Modifiers::SHIFT); } - MouseData::new( - Coordinates::new( - ScreenPoint::new(e.screen_x().into(), e.screen_y().into()), - ClientPoint::new(e.client_x().into(), e.client_y().into()), - ElementPoint::new(e.offset_x().into(), e.offset_y().into()), - PagePoint::new(e.page_x().into(), e.page_y().into()), - ), - Some(MouseButton::from_web_code(e.button())), - decode_mouse_button_set(e.buttons()), - modifiers, - ) + modifiers } } -impl From<&MouseEvent> for DragData { - fn from(value: &MouseEvent) -> Self { - Self { - mouse: MouseData::from(value), - } +impl PointerInteraction for MouseEvent { + fn held_buttons(&self) -> crate::input_data::MouseButtonSet { + decode_mouse_button_set(self.buttons()) + } + + fn trigger_button(&self) -> Option { + Some(MouseButton::from_web_code(self.button())) + } +} + +impl HasMouseData for MouseEvent { + fn as_any(&self) -> &dyn std::any::Any { + self } } -impl From<&TouchEvent> for TouchData { - fn from(e: &TouchEvent) -> Self { - Self { - alt_key: e.alt_key(), - ctrl_key: e.ctrl_key(), - meta_key: e.meta_key(), - shift_key: e.shift_key(), +impl ModifiersInteraction for TouchEvent { + fn modifiers(&self) -> Modifiers { + let mut modifiers = Modifiers::empty(); + + if self.alt_key() { + modifiers.insert(Modifiers::ALT); } + if self.ctrl_key() { + modifiers.insert(Modifiers::CONTROL); + } + if self.meta_key() { + modifiers.insert(Modifiers::META); + } + if self.shift_key() { + modifiers.insert(Modifiers::SHIFT); + } + + modifiers } } -impl From<&PointerEvent> for PointerData { - fn from(e: &PointerEvent) -> Self { - Self { - alt_key: e.alt_key(), - button: e.button(), - buttons: e.buttons(), - client_x: e.client_x(), - client_y: e.client_y(), - ctrl_key: e.ctrl_key(), - meta_key: e.meta_key(), - page_x: e.page_x(), - page_y: e.page_y(), - screen_x: e.screen_x(), - screen_y: e.screen_y(), - shift_key: e.shift_key(), - pointer_id: e.pointer_id(), - width: e.width(), - height: e.height(), - pressure: e.pressure(), - tangential_pressure: e.tangential_pressure(), - tilt_x: e.tilt_x(), - tilt_y: e.tilt_y(), - twist: e.twist(), - pointer_type: e.pointer_type(), - is_primary: e.is_primary(), - // get_modifier_state: evt.get_modifier_state(), +impl crate::events::HasTouchData for TouchEvent { + fn touches(&self) -> Vec { + let touches = TouchEvent::touches(self); + let mut result = Vec::with_capacity(touches.length() as usize); + for i in 0..touches.length() { + let touch = touches.get(i).unwrap(); + result.push(TouchPoint::new(touch)); + } + result + } + + fn touches_changed(&self) -> Vec { + let touches = self.changed_touches(); + let mut result = Vec::with_capacity(touches.length() as usize); + for i in 0..touches.length() { + let touch = touches.get(i).unwrap(); + result.push(TouchPoint::new(touch)); + } + result + } + + fn target_touches(&self) -> Vec { + let touches = self.target_touches(); + let mut result = Vec::with_capacity(touches.length() as usize); + for i in 0..touches.length() { + let touch = touches.get(i).unwrap(); + result.push(TouchPoint::new(touch)); } + result + } + + fn as_any(&self) -> &dyn std::any::Any { + self } } -impl From<&WheelEvent> for WheelData { - fn from(e: &WheelEvent) -> Self { - WheelData::from_web_attributes(e.delta_mode(), e.delta_x(), e.delta_y(), e.delta_z()) +impl HasTouchPointData for Touch { + fn identifier(&self) -> i32 { + self.identifier() + } + + fn radius(&self) -> ScreenPoint { + ScreenPoint::new(self.radius_x().into(), self.radius_y().into()) + } + + fn rotation(&self) -> f64 { + self.rotation_angle() as f64 + } + + fn force(&self) -> f64 { + self.force() as f64 + } + + fn as_any(&self) -> &dyn std::any::Any { + self } } -impl From<&AnimationEvent> for AnimationData { - fn from(e: &AnimationEvent) -> Self { - Self { - elapsed_time: e.elapsed_time(), - animation_name: e.animation_name(), - pseudo_element: e.pseudo_element(), - } +impl InteractionLocation for Touch { + fn client_coordinates(&self) -> ClientPoint { + ClientPoint::new(self.client_x().into(), self.client_y().into()) + } + + fn screen_coordinates(&self) -> ScreenPoint { + ScreenPoint::new(self.screen_x().into(), self.screen_y().into()) + } + + fn page_coordinates(&self) -> PagePoint { + PagePoint::new(self.page_x().into(), self.page_y().into()) } } -impl From<&TransitionEvent> for TransitionData { - fn from(e: &TransitionEvent) -> Self { - Self { - elapsed_time: e.elapsed_time(), - property_name: e.property_name(), - pseudo_element: e.pseudo_element(), +impl HasPointerData for PointerEvent { + fn pointer_id(&self) -> i32 { + self.pointer_id() + } + + fn width(&self) -> i32 { + self.width() + } + + fn height(&self) -> i32 { + self.height() + } + + fn pressure(&self) -> f32 { + self.pressure() + } + + fn tangential_pressure(&self) -> f32 { + self.tangential_pressure() + } + + fn tilt_x(&self) -> i32 { + self.tilt_x() + } + + fn tilt_y(&self) -> i32 { + self.tilt_y() + } + + fn twist(&self) -> i32 { + self.twist() + } + + fn pointer_type(&self) -> String { + self.pointer_type() + } + + fn is_primary(&self) -> bool { + self.is_primary() + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +impl InteractionLocation for PointerEvent { + fn client_coordinates(&self) -> ClientPoint { + ClientPoint::new(self.client_x().into(), self.client_y().into()) + } + + fn screen_coordinates(&self) -> ScreenPoint { + ScreenPoint::new(self.screen_x().into(), self.screen_y().into()) + } + + fn page_coordinates(&self) -> PagePoint { + PagePoint::new(self.page_x().into(), self.page_y().into()) + } +} + +impl InteractionElementOffset for PointerEvent { + fn element_coordinates(&self) -> ElementPoint { + ElementPoint::new(self.offset_x().into(), self.offset_y().into()) + } +} + +impl ModifiersInteraction for PointerEvent { + fn modifiers(&self) -> Modifiers { + let mut modifiers = Modifiers::empty(); + + if self.alt_key() { + modifiers.insert(Modifiers::ALT); + } + if self.ctrl_key() { + modifiers.insert(Modifiers::CONTROL); } + if self.meta_key() { + modifiers.insert(Modifiers::META); + } + if self.shift_key() { + modifiers.insert(Modifiers::SHIFT); + } + + modifiers + } +} + +impl PointerInteraction for PointerEvent { + fn held_buttons(&self) -> crate::input_data::MouseButtonSet { + decode_mouse_button_set(self.buttons()) + } + + fn trigger_button(&self) -> Option { + Some(MouseButton::from_web_code(self.button())) + } +} + +impl HasWheelData for WheelEvent { + fn delta(&self) -> crate::geometry::WheelDelta { + crate::geometry::WheelDelta::from_web_attributes( + self.delta_mode(), + self.delta_x(), + self.delta_y(), + self.delta_z(), + ) + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +impl HasAnimationData for AnimationEvent { + fn animation_name(&self) -> String { + self.animation_name() + } + + fn pseudo_element(&self) -> String { + self.pseudo_element() + } + + fn elapsed_time(&self) -> f32 { + self.elapsed_time() + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +impl HasTransitionData for TransitionEvent { + fn elapsed_time(&self) -> f32 { + self.elapsed_time() + } + + fn property_name(&self) -> String { + self.property_name() + } + + fn pseudo_element(&self) -> String { + self.pseudo_element() + } + + fn as_any(&self) -> &dyn std::any::Any { + self } } @@ -216,8 +425,8 @@ impl crate::RenderedElementBacking for web_sys::Element { Box::pin(async { result }) } - fn get_raw_element(&self) -> crate::MountedResult<&dyn std::any::Any> { - Ok(self) + fn as_any(&self) -> &dyn std::any::Any { + self } fn scroll_to( @@ -261,3 +470,45 @@ impl std::fmt::Display for FocusError { } impl std::error::Error for FocusError {} + +impl HasScrollData for Event { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +impl HasClipboardData for Event { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +impl From<&Event> for ClipboardData { + fn from(e: &Event) -> Self { + ClipboardData::new(e.clone()) + } +} + +impl HasFocusData for web_sys::FocusEvent { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +impl HasToggleData for web_sys::Event { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +impl HasSelectionData for web_sys::Event { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} + +impl HasMediaData for web_sys::Event { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} diff --git a/packages/liveview/Cargo.toml b/packages/liveview/Cargo.toml index cbd7b75f5d..6600cd4b51 100644 --- a/packages/liveview/Cargo.toml +++ b/packages/liveview/Cargo.toml @@ -22,7 +22,7 @@ tokio-stream = { version = "0.1.11", features = ["net"] } tokio-util = { version = "0.7.4", features = ["rt"] } serde = { version = "1.0.151", features = ["derive"] } serde_json = "1.0.91" -dioxus-html = { workspace = true, features = ["serialize"] } +dioxus-html = { workspace = true, features = ["serialize", "eval", "mounted"] } dioxus-core = { workspace = true, features = ["serialize"] } dioxus-interpreter-js = { workspace = true } dioxus-hot-reload = { workspace = true, optional = true } diff --git a/packages/liveview/src/element.rs b/packages/liveview/src/element.rs index ae0eb4efe3..eef0fa48da 100644 --- a/packages/liveview/src/element.rs +++ b/packages/liveview/src/element.rs @@ -4,6 +4,7 @@ use dioxus_html::{geometry::euclid::Rect, MountedResult, RenderedElementBacking} use crate::query::QueryEngine; /// A mounted element passed to onmounted events +#[derive(Clone)] pub struct LiveviewElement { id: ElementId, query: QueryEngine, @@ -16,8 +17,8 @@ impl LiveviewElement { } impl RenderedElementBacking for LiveviewElement { - fn get_raw_element(&self) -> dioxus_html::MountedResult<&dyn std::any::Any> { - Ok(self) + fn as_any(&self) -> &dyn std::any::Any { + self } fn get_client_rect( diff --git a/packages/liveview/src/events.rs b/packages/liveview/src/events.rs new file mode 100644 index 0000000000..b764bf7cc6 --- /dev/null +++ b/packages/liveview/src/events.rs @@ -0,0 +1,149 @@ +//! Convert a serialized event to an event trigger + +use dioxus_html::*; + +use crate::element::LiveviewElement; + +pub(crate) struct SerializedHtmlEventConverter; + +impl HtmlEventConverter for SerializedHtmlEventConverter { + fn convert_animation_data(&self, event: &PlatformEventData) -> AnimationData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_clipboard_data(&self, event: &PlatformEventData) -> ClipboardData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_composition_data(&self, event: &PlatformEventData) -> CompositionData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_drag_data(&self, event: &PlatformEventData) -> DragData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_focus_data(&self, event: &PlatformEventData) -> FocusData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_form_data(&self, event: &PlatformEventData) -> FormData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_image_data(&self, event: &PlatformEventData) -> ImageData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_keyboard_data(&self, event: &PlatformEventData) -> KeyboardData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_media_data(&self, event: &PlatformEventData) -> MediaData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_mounted_data(&self, event: &PlatformEventData) -> MountedData { + event.downcast::().cloned().unwrap().into() + } + + fn convert_mouse_data(&self, event: &PlatformEventData) -> MouseData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_pointer_data(&self, event: &PlatformEventData) -> PointerData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_scroll_data(&self, event: &PlatformEventData) -> ScrollData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_selection_data(&self, event: &PlatformEventData) -> SelectionData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_toggle_data(&self, event: &PlatformEventData) -> ToggleData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_touch_data(&self, event: &PlatformEventData) -> TouchData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_transition_data(&self, event: &PlatformEventData) -> TransitionData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } + + fn convert_wheel_data(&self, event: &PlatformEventData) -> WheelData { + event + .downcast::() + .cloned() + .unwrap() + .into() + } +} diff --git a/packages/liveview/src/lib.rs b/packages/liveview/src/lib.rs index a7370e91cc..6f9c5646d8 100644 --- a/packages/liveview/src/lib.rs +++ b/packages/liveview/src/lib.rs @@ -24,6 +24,7 @@ mod query; use futures_util::{SinkExt, StreamExt}; pub use pool::*; mod eval; +mod events; pub trait WebsocketTx: SinkExt {} impl WebsocketTx for T where T: SinkExt {} diff --git a/packages/liveview/src/pool.rs b/packages/liveview/src/pool.rs index 2fd06cec67..ed0649c002 100644 --- a/packages/liveview/src/pool.rs +++ b/packages/liveview/src/pool.rs @@ -1,11 +1,12 @@ use crate::{ element::LiveviewElement, eval::init_eval, + events::SerializedHtmlEventConverter, query::{QueryEngine, QueryResult}, LiveViewError, }; use dioxus_core::{prelude::*, Mutations}; -use dioxus_html::{EventData, HtmlEvent, MountedData}; +use dioxus_html::{EventData, HtmlEvent, MountedData, PlatformEventData}; use futures_util::{pin_mut, SinkExt, StreamExt}; use serde::Serialize; use std::{rc::Rc, time::Duration}; @@ -24,6 +25,9 @@ impl Default for LiveViewPool { impl LiveViewPool { pub fn new() -> Self { + // Set the event converter + dioxus_html::set_event_converter(Box::new(SerializedHtmlEventConverter)); + LiveViewPool { pool: LocalPoolHandle::new(16), } @@ -171,12 +175,11 @@ pub async fn run(mut vdom: VirtualDom, ws: impl LiveViewSocket) -> Result<(), Li let element = LiveviewElement::new(evt.element, query_engine.clone()); vdom.handle_event( &evt.name, - Rc::new(MountedData::new(element)), + Rc::new(PlatformEventData::new(Box::new(MountedData::new(element)))), evt.element, evt.bubbles, ); - } - else{ + } else { vdom.handle_event( &evt.name, evt.data.into_any(), diff --git a/packages/rink/Cargo.toml b/packages/rink/Cargo.toml index ba3d549f80..69d9a5c7bf 100644 --- a/packages/rink/Cargo.toml +++ b/packages/rink/Cargo.toml @@ -10,7 +10,7 @@ keywords = ["dom", "ui", "gui", "react", "terminal"] license = "MIT OR Apache-2.0" [dependencies] -dioxus-html = { workspace = true } +dioxus-html = { workspace = true, features = ["serialize", "mounted"] } dioxus-native-core = { workspace = true, features = ["layout-attributes"] } dioxus-native-core-macro = { workspace = true } diff --git a/packages/rink/examples/widgets.rs b/packages/rink/examples/widgets.rs index e1f71fe427..42dc3723db 100644 --- a/packages/rink/examples/widgets.rs +++ b/packages/rink/examples/widgets.rs @@ -1,3 +1,4 @@ +use dioxus_html::HasFormData; use dioxus_native_core::{ prelude::*, real_dom::{NodeImmutable, NodeTypeMut}, @@ -79,7 +80,7 @@ impl Driver for Counter { if event_type == "input" { // when the button is clicked, increment the counter if let EventData::Form(input_event) = &*event { - if let Ok(value) = input_event.value.parse::() { + if let Ok(value) = input_event.value().parse::() { self.count = value; } } diff --git a/packages/rink/src/hooks.rs b/packages/rink/src/hooks.rs index f8c4500d2d..1ffa361f7b 100644 --- a/packages/rink/src/hooks.rs +++ b/packages/rink/src/hooks.rs @@ -2,6 +2,10 @@ use crossterm::event::{ Event as TermEvent, KeyCode as TermKeyCode, KeyModifiers, ModifierKeyCode, MouseButton, MouseEventKind, }; +use dioxus_html::{ + HasFormData, HasKeyboardData, HasWheelData, SerializedFocusData, SerializedKeyboardData, + SerializedMouseData, SerializedWheelData, +}; use dioxus_native_core::prelude::*; use dioxus_native_core::real_dom::NodeImmutable; use rustc_hash::{FxHashMap, FxHashSet}; @@ -11,9 +15,10 @@ use dioxus_html::geometry::{ ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint, WheelDelta, }; use dioxus_html::input_data::keyboard_types::{Code, Key, Location, Modifiers}; -use dioxus_html::input_data::MouseButtonSet as DioxusMouseButtons; -use dioxus_html::input_data::{MouseButton as DioxusMouseButton, MouseButtonSet}; -use dioxus_html::{event_bubbles, FocusData, KeyboardData, MouseData, WheelData}; +use dioxus_html::input_data::{ + MouseButton as DioxusMouseButton, MouseButtonSet as DioxusMouseButtons, +}; +use dioxus_html::{event_bubbles, prelude::*}; use std::any::Any; use std::collections::HashMap; use std::{ @@ -38,10 +43,10 @@ pub struct Event { #[derive(Debug, Clone, PartialEq)] pub enum EventData { - Mouse(MouseData), - Keyboard(KeyboardData), - Focus(FocusData), - Wheel(WheelData), + Mouse(SerializedMouseData), + Keyboard(SerializedKeyboardData), + Focus(SerializedFocusData), + Wheel(SerializedWheelData), Form(FormData), } @@ -52,27 +57,31 @@ impl EventData { EventData::Keyboard(k) => Rc::new(k), EventData::Focus(f) => Rc::new(f), EventData::Wheel(w) => Rc::new(w), - EventData::Form(f) => Rc::new(f.into_html()), + EventData::Form(f) => Rc::new(f), } } } #[derive(Clone, Debug, PartialEq)] pub struct FormData { - pub value: String, + pub(crate) value: String, - pub values: HashMap>, + pub(crate) values: HashMap>, - pub files: Option, + pub(crate) files: Option, } -impl FormData { - fn into_html(self) -> dioxus_html::FormData { - dioxus_html::FormData { - value: self.value, - values: self.values, - files: None, - } +impl HasFormData for FormData { + fn value(&self) -> String { + self.value.clone() + } + + fn values(&self) -> HashMap> { + self.values.clone() + } + + fn as_any(&self) -> &dyn std::any::Any { + self } } @@ -89,9 +98,9 @@ type EventCore = (&'static str, EventData); const MAX_REPEAT_TIME: Duration = Duration::from_millis(100); pub struct InnerInputState { - mouse: Option, - wheel: Option, - last_key_pressed: Option<(KeyboardData, Instant)>, + mouse: Option, + wheel: Option, + last_key_pressed: Option<(SerializedKeyboardData, Instant)>, pub(crate) focus_state: FocusState, // subscribers: Vec>, } @@ -114,7 +123,7 @@ impl InnerInputState { EventData::Mouse(ref mut m) => { let mut held_buttons = match &self.mouse { Some(previous_data) => previous_data.held_buttons(), - None => MouseButtonSet::empty(), + None => DioxusMouseButtons::empty(), }; match evt.0 { @@ -133,10 +142,16 @@ impl InnerInputState { _ => {} } - let new_mouse_data = MouseData::new( - m.coordinates(), + let coordinates = m.coordinates(); + let new_mouse_data = SerializedMouseData::new( m.trigger_button(), held_buttons, + Coordinates::new( + m.screen_coordinates(), + m.client_coordinates(), + coordinates.element(), + m.page_coordinates(), + ), m.modifiers(), ); @@ -155,7 +170,13 @@ impl InnerInputState { .is_some(); if is_repeating { - *k = KeyboardData::new(k.key(), k.code(), k.location(), true, k.modifiers()); + *k = SerializedKeyboardData::new( + k.key(), + k.code(), + k.location(), + is_repeating, + k.modifiers(), + ); } self.last_key_pressed = Some((k.clone(), Instant::now())); @@ -199,13 +220,13 @@ impl InnerInputState { resolved_events.push(Event { name: "focus", id, - data: EventData::Focus(FocusData {}), + data: EventData::Focus(SerializedFocusData::default()), bubbles: event_bubbles("focus"), }); resolved_events.push(Event { name: "focusin", id, - data: EventData::Focus(FocusData {}), + data: EventData::Focus(SerializedFocusData::default()), bubbles: event_bubbles("focusin"), }); } @@ -213,7 +234,7 @@ impl InnerInputState { resolved_events.push(Event { name: "focusout", id, - data: EventData::Focus(FocusData {}), + data: EventData::Focus(SerializedFocusData::default()), bubbles: event_bubbles("focusout"), }); } @@ -226,7 +247,7 @@ impl InnerInputState { fn resolve_mouse_events( &mut self, - previous_mouse: Option, + previous_mouse: Option, resolved_events: &mut Vec, layout: &Taffy, dom: &mut RealDom, @@ -273,7 +294,10 @@ impl InnerInputState { } } - fn prepare_mouse_data(mouse_data: &MouseData, layout: &Layout) -> MouseData { + fn prepare_mouse_data( + mouse_data: &SerializedMouseData, + layout: &Layout, + ) -> SerializedMouseData { let Point { x, y } = layout.location; let node_origin = ClientPoint::new( layout_to_screen_space(x).into(), @@ -284,17 +308,15 @@ impl InnerInputState { .to_point() .cast_unit(); - let coordinates = Coordinates::new( - mouse_data.screen_coordinates(), - mouse_data.client_coordinates(), - new_client_coordinates, - mouse_data.page_coordinates(), - ); - - MouseData::new( - coordinates, + SerializedMouseData::new( mouse_data.trigger_button(), mouse_data.held_buttons(), + Coordinates::new( + mouse_data.screen_coordinates(), + mouse_data.client_coordinates(), + new_client_coordinates, + mouse_data.page_coordinates(), + ), mouse_data.modifiers(), ) } @@ -305,7 +327,7 @@ impl InnerInputState { // a mouse button is pressed if a button was not down and is now down let previous_buttons = previous_mouse - .map_or(MouseButtonSet::empty(), |previous_data| { + .map_or(DioxusMouseButtons::empty(), |previous_data| { previous_data.held_buttons() }); let was_pressed = !(mouse_data.held_buttons() - previous_buttons).is_empty(); @@ -689,17 +711,6 @@ fn get_event(evt: TermEvent) -> Option<(&'static str, EventData)> { // from https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent - // The `page` and `screen` coordinates are inconsistent with the MDN definition, as they are relative to the viewport (client), not the target element/page/screen, respectively. - // todo? - // But then, MDN defines them in terms of pixels, yet crossterm provides only row/column, and it might not be possible to get pixels. So we can't get 100% consistency anyway. - let coordinates = Coordinates::new( - ScreenPoint::new(x, y), - ClientPoint::new(x, y), - // offset x/y are set when the origin of the event is assigned to an element - ElementPoint::new(0., 0.), - PagePoint::new(x, y), - ); - let mut modifiers = Modifiers::empty(); if shift { modifiers.insert(Modifiers::SHIFT); @@ -715,17 +726,26 @@ fn get_event(evt: TermEvent) -> Option<(&'static str, EventData)> { } // held mouse buttons get set later by maintaining state, as crossterm does not provide them - EventData::Mouse(MouseData::new( - coordinates, + EventData::Mouse(SerializedMouseData::new( button, DioxusMouseButtons::empty(), + Coordinates::new( + // The `page` and `screen` coordinates are inconsistent with the MDN definition, as they are relative to the viewport (client), not the target element/page/screen, respectively. + // todo? + // But then, MDN defines them in terms of pixels, yet crossterm provides only row/column, and it might not be possible to get pixels. So we can't get 100% consistency anyway. + ScreenPoint::new(x, y), + ClientPoint::new(x, y), + // offset x/y are set when the origin of the event is assigned to an element + ElementPoint::new(0., 0.), + PagePoint::new(x, y), + ), modifiers, )) }; let get_wheel_data = |up| { let y = if up { -1.0 } else { 1.0 }; - EventData::Wheel(WheelData::new(WheelDelta::lines(0., y, 0.))) + EventData::Wheel(SerializedWheelData::new(WheelDelta::lines(0., y, 0.))) }; match m.kind { @@ -750,7 +770,7 @@ fn translate_key_event(event: crossterm::event::KeyEvent) -> Option { let code = guess_code_from_crossterm_key_code(event.code)?; let modifiers = modifiers_from_crossterm_modifiers(event.modifiers); - Some(EventData::Keyboard(KeyboardData::new( + Some(EventData::Keyboard(SerializedKeyboardData::new( key, code, Location::Standard, diff --git a/packages/rink/src/widgets/button.rs b/packages/rink/src/widgets/button.rs index 523b2392af..25f0e07ef8 100644 --- a/packages/rink/src/widgets/button.rs +++ b/packages/rink/src/widgets/button.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use dioxus_html::input_data::keyboard_types::Key; +use dioxus_html::{input_data::keyboard_types::Key, HasKeyboardData}; use dioxus_native_core::{ custom_element::CustomElement, node::OwnedAttributeDiscription, @@ -11,7 +11,7 @@ use dioxus_native_core::{ }; use shipyard::UniqueView; -use crate::FormData; +use crate::hooks::FormData; use super::{RinkWidget, WidgetContext}; diff --git a/packages/rink/src/widgets/checkbox.rs b/packages/rink/src/widgets/checkbox.rs index fab809ed2e..a8a8253a01 100644 --- a/packages/rink/src/widgets/checkbox.rs +++ b/packages/rink/src/widgets/checkbox.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use dioxus_html::input_data::keyboard_types::Key; +use dioxus_html::{input_data::keyboard_types::Key, HasKeyboardData}; use dioxus_native_core::{ custom_element::CustomElement, node::OwnedAttributeDiscription, @@ -11,7 +11,7 @@ use dioxus_native_core::{ }; use shipyard::UniqueView; -use crate::FormData; +use crate::hooks::FormData; use super::{RinkWidget, WidgetContext}; diff --git a/packages/rink/src/widgets/number.rs b/packages/rink/src/widgets/number.rs index 371c8b41db..c2a71ceacd 100644 --- a/packages/rink/src/widgets/number.rs +++ b/packages/rink/src/widgets/number.rs @@ -1,4 +1,4 @@ -use dioxus_html::input_data::keyboard_types::Key; +use dioxus_html::{input_data::keyboard_types::Key, HasKeyboardData}; use dioxus_native_core::{ custom_element::CustomElement, real_dom::{NodeImmutable, RealDom}, diff --git a/packages/rink/src/widgets/slider.rs b/packages/rink/src/widgets/slider.rs index 7080b6c12d..bb06396106 100644 --- a/packages/rink/src/widgets/slider.rs +++ b/packages/rink/src/widgets/slider.rs @@ -1,6 +1,9 @@ use std::collections::HashMap; -use dioxus_html::{input_data::keyboard_types::Key, KeyboardData, MouseData}; +use dioxus_html::{ + input_data::keyboard_types::Key, prelude::*, HasKeyboardData, SerializedKeyboardData, + SerializedMouseData, +}; use dioxus_native_core::{ custom_element::CustomElement, node::{OwnedAttributeDiscription, OwnedAttributeValue}, @@ -12,7 +15,8 @@ use dioxus_native_core::{ use shipyard::UniqueView; use super::{RinkWidget, WidgetContext}; -use crate::{query::get_layout, Event, EventData, FormData, Query}; +use crate::hooks::FormData; +use crate::{query::get_layout, Event, EventData, Query}; #[derive(Debug)] pub(crate) struct Slider { @@ -209,7 +213,7 @@ impl Slider { } } - fn handle_keydown(&mut self, mut root: NodeMut, data: &KeyboardData) { + fn handle_keydown(&mut self, mut root: NodeMut, data: &SerializedKeyboardData) { let key = data.key(); let step = self.step(); @@ -231,7 +235,7 @@ impl Slider { self.write_value(rdom, id); } - fn handle_mousemove(&mut self, mut root: NodeMut, data: &MouseData) { + fn handle_mousemove(&mut self, mut root: NodeMut, data: &SerializedMouseData) { if !data.held_buttons().is_empty() { let id = root.id(); let rdom = root.real_dom_mut(); diff --git a/packages/rink/src/widgets/text_like.rs b/packages/rink/src/widgets/text_like.rs index 6521073d00..770dd9d33b 100644 --- a/packages/rink/src/widgets/text_like.rs +++ b/packages/rink/src/widgets/text_like.rs @@ -1,7 +1,10 @@ use std::{collections::HashMap, io::stdout}; use crossterm::{cursor::MoveTo, execute}; -use dioxus_html::{input_data::keyboard_types::Key, KeyboardData, MouseData}; +use dioxus_html::{ + input_data::keyboard_types::Key, prelude::*, HasKeyboardData, SerializedKeyboardData, + SerializedMouseData, +}; use dioxus_native_core::{ custom_element::CustomElement, node::OwnedAttributeDiscription, @@ -14,7 +17,8 @@ use dioxus_native_core::{ use shipyard::UniqueView; use taffy::geometry::Point; -use crate::{query::get_layout, Event, EventData, FormData, Query}; +use crate::hooks::FormData; +use crate::{query::get_layout, Event, EventData, Query}; use super::{RinkWidget, WidgetContext}; @@ -184,7 +188,7 @@ impl TextLike { } } - fn handle_keydown(&mut self, mut root: NodeMut, data: &KeyboardData) { + fn handle_keydown(&mut self, mut root: NodeMut, data: &SerializedKeyboardData) { let key = data.key(); let modifiers = data.modifiers(); let code = data.code(); @@ -228,7 +232,7 @@ impl TextLike { } } - fn handle_mousemove(&mut self, mut root: NodeMut, data: &MouseData) { + fn handle_mousemove(&mut self, mut root: NodeMut, data: &SerializedMouseData) { if self.dragging { let id = root.id(); let offset = data.element_coordinates(); @@ -245,7 +249,7 @@ impl TextLike { } } - fn handle_mousedown(&mut self, mut root: NodeMut, data: &MouseData) { + fn handle_mousedown(&mut self, mut root: NodeMut, data: &SerializedMouseData) { let offset = data.element_coordinates(); let mut new = Pos::new(offset.x as usize, offset.y as usize); diff --git a/packages/router/examples/simple_routes.rs b/packages/router/examples/simple_routes.rs index 3634f7df38..26ef3a9db0 100644 --- a/packages/router/examples/simple_routes.rs +++ b/packages/router/examples/simple_routes.rs @@ -79,7 +79,7 @@ fn Route3(cx: Scope, dynamic: String) -> Element { render! { input { oninput: move |evt| { - *current_route_str.write() = evt.value.clone(); + *current_route_str.write() = evt.value(); }, value: "{current_route_str.read()}" } diff --git a/packages/rsx/src/component.rs b/packages/rsx/src/component.rs index 6487e5ab72..2321bcecce 100644 --- a/packages/rsx/src/component.rs +++ b/packages/rsx/src/component.rs @@ -16,8 +16,10 @@ use super::*; use proc_macro2::TokenStream as TokenStream2; use quote::{quote, ToTokens, TokenStreamExt}; use syn::{ + bracketed, ext::IdentExt, parse::{Parse, ParseBuffer, ParseStream}, + punctuated::Punctuated, spanned::Spanned, AngleBracketedGenericArguments, Error, Expr, Ident, LitStr, PathArguments, Result, Token, }; @@ -209,7 +211,10 @@ pub struct ComponentField { pub enum ContentField { ManExpr(Expr), Formatted(IfmtInput), - OnHandlerRaw(Expr), + OnHandlerRaw { + metadata: Option>, + handler: Expr, + }, } impl ToTokens for ContentField { @@ -219,8 +224,14 @@ impl ToTokens for ContentField { ContentField::Formatted(s) => tokens.append_all(quote! { __cx.raw_text(#s) }), - ContentField::OnHandlerRaw(e) => tokens.append_all(quote! { - __cx.event_handler(#e) + ContentField::OnHandlerRaw { metadata, handler } => tokens.append_all({ + let metadata = match metadata { + Some(m) => quote! { (#(#m),*) }, + None => quote! { () }, + }; + quote! { + __cx.event_handler(#handler, #metadata) + } }), } } @@ -229,15 +240,31 @@ impl ToTokens for ContentField { impl Parse for ComponentField { fn parse(input: ParseStream) -> Result { let name = Ident::parse_any(input)?; - input.parse::()?; let content = { if name.to_string().starts_with("on") { - ContentField::OnHandlerRaw(input.parse()?) + // parse an array index expression + let metadata = { + if input.peek(Token![:]) { + // if there's a colon, then there's no metadata + None + } else { + let between_brackets; + bracketed!(between_brackets in input); + let separated = + Punctuated::::parse_terminated(&between_brackets)?; + Some(separated.into_iter().collect()) + } + }; + input.parse::()?; + let handler = input.parse::()?; + ContentField::OnHandlerRaw { metadata, handler } } else if name == "key" { + input.parse::()?; let content = ContentField::Formatted(input.parse()?); return Ok(Self { name, content }); } else if input.peek(LitStr) { + input.parse::()?; let forked = input.fork(); let t: LitStr = forked.parse()?; // the string literal must either be the end of the input or a followed by a comma @@ -247,6 +274,7 @@ impl Parse for ComponentField { ContentField::ManExpr(input.parse()?) } } else { + input.parse::()?; ContentField::ManExpr(input.parse()?) } }; diff --git a/packages/rsx/src/element.rs b/packages/rsx/src/element.rs index 8f45d91ff0..dc31e62da7 100644 --- a/packages/rsx/src/element.rs +++ b/packages/rsx/src/element.rs @@ -5,6 +5,7 @@ use super::*; use proc_macro2::{Span, TokenStream as TokenStream2}; use quote::{quote, ToTokens, TokenStreamExt}; use syn::{ + bracketed, parse::{Parse, ParseBuffer, ParseStream}, punctuated::Punctuated, spanned::Spanned, @@ -77,21 +78,33 @@ impl Parse for Element { let name = content.parse::()?; let name_str = name.to_string(); - content.parse::()?; // The span of the content to be parsed, // for example the `hi` part of `class: "hi"`. let span = content.span(); if name_str.starts_with("on") { + let metadata = if content.peek(Token![:]) { + // if there's a colon, then there's no metadata + None + } else { + let between_brackets; + bracketed!(between_brackets in content); + let separated = + Punctuated::::parse_terminated(&between_brackets)?; + Some(separated.into_iter().collect()) + }; + content.parse::()?; attributes.push(ElementAttrNamed { el_name: el_name.clone(), attr: ElementAttr::EventTokens { name, tokens: content.parse()?, + metadata, }, }); } else { + content.parse::()?; match name_str.as_str() { "key" => { key = Some(content.parse()?); @@ -282,7 +295,11 @@ pub enum ElementAttr { // /// onclick: move |_| {} // EventClosure { name: Ident, closure: ExprClosure }, /// onclick: {} - EventTokens { name: Ident, tokens: Expr }, + EventTokens { + name: Ident, + tokens: Expr, + metadata: Option>, + }, } impl ElementAttr { @@ -379,9 +396,19 @@ impl ToTokens for ElementAttrNamed { ) } } - ElementAttr::EventTokens { name, tokens } => { + ElementAttr::EventTokens { + name, + tokens, + metadata, + } => { + let metadata = match metadata { + Some(m) => { + quote! { (#(#m),*) } + } + None => quote! { () }, + }; quote! { - dioxus_elements::events::#name(__cx, #tokens) + dioxus_elements::events::#name(__cx, #tokens, #metadata) } } }; diff --git a/packages/web/Cargo.toml b/packages/web/Cargo.toml index 35eb9c5f16..36912390bc 100644 --- a/packages/web/Cargo.toml +++ b/packages/web/Cargo.toml @@ -65,7 +65,9 @@ hot_reload = [ "web-sys/WebSocket", "web-sys/Location", ] -eval = [] +eval = [ + "dioxus-html/eval", +] [dev-dependencies] dioxus = { workspace = true } diff --git a/packages/web/src/dom.rs b/packages/web/src/dom.rs index 4fa5c9444c..50d990cb27 100644 --- a/packages/web/src/dom.rs +++ b/packages/web/src/dom.rs @@ -1,7 +1,6 @@ //! Implementation of a renderer for Dioxus on the web. //! -//! Oustanding todos: -//! - Removing event listeners (delegation) +//! Outstanding todos: //! - Passive event listeners //! - no-op event listener patch for safari //! - tests to ensure dyn_into works for various event types. @@ -10,16 +9,14 @@ use dioxus_core::{ BorrowedAttributeValue, ElementId, Mutation, Template, TemplateAttribute, TemplateNode, }; -use dioxus_html::{event_bubbles, CompositionData, FormData, MountedData}; -use dioxus_interpreter_js::{get_node, minimal_bindings, save_template, Channel}; +use dioxus_html::{event_bubbles, PlatformEventData}; +use dioxus_interpreter_js::{minimal_bindings, save_template, Channel}; use futures_channel::mpsc; -use js_sys::Array; use rustc_hash::FxHashMap; -use std::{any::Any, rc::Rc}; -use wasm_bindgen::{closure::Closure, prelude::wasm_bindgen, JsCast, JsValue}; +use wasm_bindgen::{closure::Closure, JsCast, JsValue}; use web_sys::{Document, Element, Event}; -use crate::Config; +use crate::{load_document, virtual_event_from_websys_event, Config, WebEventConverter}; pub struct WebsysDom { document: Document, @@ -28,6 +25,7 @@ pub struct WebsysDom { templates: FxHashMap, max_template_id: u32, pub(crate) interpreter: Channel, + #[cfg(feature = "mounted")] event_channel: mpsc::UnboundedSender, } @@ -35,7 +33,7 @@ pub struct UiEvent { pub name: String, pub bubbles: bool, pub element: ElementId, - pub data: Rc, + pub data: PlatformEventData, } impl WebsysDom { @@ -94,6 +92,7 @@ impl WebsysDom { root.clone().unchecked_into(), handler.as_ref().unchecked_ref(), ); + dioxus_html::set_event_converter(Box::new(WebEventConverter)); handler.forget(); Self { document, @@ -101,6 +100,7 @@ impl WebsysDom { interpreter, templates: FxHashMap::default(), max_template_id: 0, + #[cfg(feature = "mounted")] event_channel, } } @@ -171,6 +171,7 @@ impl WebsysDom { pub fn apply_edits(&mut self, mut edits: Vec) { use Mutation::*; let i = &mut self.interpreter; + #[cfg(feature = "mounted")] // we need to apply the mount events last, so we collect them here let mut to_mount = Vec::new(); for edit in &edits { @@ -226,6 +227,7 @@ impl WebsysDom { match *name { // mounted events are fired immediately after the element is mounted. "mounted" => { + #[cfg(feature = "mounted")] to_mount.push(*id); } _ => { @@ -246,11 +248,11 @@ impl WebsysDom { edits.clear(); i.flush(); + #[cfg(feature = "mounted")] for id in to_mount { - let node = get_node(id.0 as u32); + let node = dioxus_interpreter_js::get_node(id.0 as u32); if let Some(element) = node.dyn_ref::() { - let data: MountedData = element.into(); - let data = Rc::new(data); + let data = PlatformEventData::new(Box::new(element.clone())); let _ = self.event_channel.unbounded_send(UiEvent { name: "mounted".to_string(), bubbles: false, @@ -262,165 +264,6 @@ impl WebsysDom { } } -// todo: some of these events are being casted to the wrong event type. -// We need tests that simulate clicks/etc and make sure every event type works. -pub fn virtual_event_from_websys_event(event: web_sys::Event, target: Element) -> Rc { - use dioxus_html::events::*; - - match event.type_().as_str() { - "copy" | "cut" | "paste" => Rc::new(ClipboardData {}), - "compositionend" | "compositionstart" | "compositionupdate" => { - make_composition_event(&event) - } - "keydown" | "keypress" | "keyup" => Rc::new(KeyboardData::from(event)), - "focus" | "blur" | "focusout" | "focusin" => Rc::new(FocusData {}), - - "change" | "input" | "invalid" | "reset" | "submit" => read_input_to_data(target), - - "click" | "contextmenu" | "dblclick" | "doubleclick" | "mousedown" | "mouseenter" - | "mouseleave" | "mousemove" | "mouseout" | "mouseover" | "mouseup" => { - Rc::new(MouseData::from(event)) - } - "drag" | "dragend" | "dragenter" | "dragexit" | "dragleave" | "dragover" | "dragstart" - | "drop" => { - let mouse = MouseData::from(event); - Rc::new(DragData { mouse }) - } - - "pointerdown" | "pointermove" | "pointerup" | "pointercancel" | "gotpointercapture" - | "lostpointercapture" | "pointerenter" | "pointerleave" | "pointerover" | "pointerout" => { - Rc::new(PointerData::from(event)) - } - "select" => Rc::new(SelectionData {}), - "touchcancel" | "touchend" | "touchmove" | "touchstart" => Rc::new(TouchData::from(event)), - - "scroll" => Rc::new(()), - "wheel" => Rc::new(WheelData::from(event)), - "animationstart" | "animationend" | "animationiteration" => { - Rc::new(AnimationData::from(event)) - } - "transitionend" => Rc::new(TransitionData::from(event)), - "abort" | "canplay" | "canplaythrough" | "durationchange" | "emptied" | "encrypted" - | "ended" | "loadeddata" | "loadedmetadata" | "loadstart" | "pause" | "play" - | "playing" | "progress" | "ratechange" | "seeked" | "seeking" | "stalled" | "suspend" - | "timeupdate" | "volumechange" | "waiting" => Rc::new(MediaData {}), - "error" => Rc::new(ImageData { load_error: true }), - "load" => Rc::new(ImageData { load_error: false }), - "toggle" => Rc::new(ToggleData {}), - - _ => Rc::new(()), - } -} - -fn make_composition_event(event: &Event) -> Rc { - let evt: &web_sys::CompositionEvent = event.dyn_ref().unwrap(); - Rc::new(CompositionData { - data: evt.data().unwrap_or_default(), - }) -} - -pub(crate) fn load_document() -> Document { - web_sys::window() - .expect("should have access to the Window") - .document() - .expect("should have access to the Document") -} - -fn read_input_to_data(target: Element) -> Rc { - // todo: these handlers might get really slow if the input box gets large and allocation pressure is heavy - // don't have a good solution with the serialized event problem - - let value: String = target - .dyn_ref() - .map(|input: &web_sys::HtmlInputElement| { - // todo: special case more input types - match input.type_().as_str() { - "checkbox" => { - match input.checked() { - true => "true".to_string(), - false => "false".to_string(), - } - }, - _ => { - input.value() - } - } - }) - .or_else(|| { - target - .dyn_ref() - .map(|input: &web_sys::HtmlTextAreaElement| input.value()) - }) - // select elements are NOT input events - because - why woudn't they be?? - .or_else(|| { - target - .dyn_ref() - .map(|input: &web_sys::HtmlSelectElement| input.value()) - }) - .or_else(|| { - target - .dyn_ref::() - .unwrap() - .text_content() - }) - .expect("only an InputElement or TextAreaElement or an element with contenteditable=true can have an oninput event listener"); - - let mut values = std::collections::HashMap::new(); - - // try to fill in form values - if let Some(form) = target.dyn_ref::() { - let form_data = get_form_data(form); - for value in form_data.entries().into_iter().flatten() { - if let Ok(array) = value.dyn_into::() { - if let Some(name) = array.get(0).as_string() { - if let Ok(item_values) = array.get(1).dyn_into::() { - let item_values = - item_values.iter().filter_map(|v| v.as_string()).collect(); - - values.insert(name, item_values); - } - } - } - } - } - - #[cfg(not(feature = "file_engine"))] - let files = None; - #[cfg(feature = "file_engine")] - let files = target - .dyn_ref() - .and_then(|input: &web_sys::HtmlInputElement| { - input.files().and_then(|files| { - #[allow(clippy::arc_with_non_send_sync)] - crate::file_engine::WebFileEngine::new(files) - .map(|f| std::sync::Arc::new(f) as std::sync::Arc) - }) - }); - - Rc::new(FormData { - value, - values, - files, - }) -} - -// web-sys does not expose the keys api for form data, so we need to manually bind to it -#[wasm_bindgen(inline_js = r#" - export function get_form_data(form) { - let values = new Map(); - const formData = new FormData(form); - - for (let name of formData.keys()) { - values.set(name, formData.getAll(name)); - } - - return values; - } -"#)] -extern "C" { - fn get_form_data(form: &web_sys::HtmlFormElement) -> js_sys::Map; -} - fn walk_event_for_id(event: &web_sys::Event) -> Option<(ElementId, web_sys::Element)> { let target = event .target() diff --git a/packages/web/src/event.rs b/packages/web/src/event.rs new file mode 100644 index 0000000000..d1eed67a6d --- /dev/null +++ b/packages/web/src/event.rs @@ -0,0 +1,443 @@ +use std::{any::Any, collections::HashMap}; + +use dioxus_html::{ + FileEngine, FormData, HasFormData, HasImageData, HtmlEventConverter, ImageData, MountedData, + PlatformEventData, ScrollData, +}; +use js_sys::Array; +use wasm_bindgen::{prelude::wasm_bindgen, JsCast}; +use web_sys::{Document, Element, Event}; + +pub(crate) struct WebEventConverter; + +#[inline(always)] +fn downcast_event(event: &dioxus_html::PlatformEventData) -> &GenericWebSysEvent { + event + .downcast::() + .expect("event should be a GenericWebSysEvent") +} + +impl HtmlEventConverter for WebEventConverter { + #[inline(always)] + fn convert_animation_data( + &self, + event: &dioxus_html::PlatformEventData, + ) -> dioxus_html::AnimationData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_clipboard_data( + &self, + event: &dioxus_html::PlatformEventData, + ) -> dioxus_html::ClipboardData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_composition_data( + &self, + event: &dioxus_html::PlatformEventData, + ) -> dioxus_html::CompositionData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_drag_data(&self, event: &dioxus_html::PlatformEventData) -> dioxus_html::DragData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_focus_data(&self, event: &dioxus_html::PlatformEventData) -> dioxus_html::FocusData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_form_data(&self, event: &dioxus_html::PlatformEventData) -> dioxus_html::FormData { + let event = downcast_event(event); + FormData::new(WebFormData::new(event.element.clone(), event.raw.clone())) + } + + #[inline(always)] + fn convert_image_data(&self, event: &dioxus_html::PlatformEventData) -> dioxus_html::ImageData { + let event = downcast_event(event); + let error = event.raw.type_() == "error"; + ImageData::new(WebImageEvent::new(event.raw.clone(), error)) + } + + #[inline(always)] + fn convert_keyboard_data( + &self, + event: &dioxus_html::PlatformEventData, + ) -> dioxus_html::KeyboardData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_media_data(&self, event: &dioxus_html::PlatformEventData) -> dioxus_html::MediaData { + downcast_event(event).raw.clone().into() + } + + #[allow(unused_variables)] + #[inline(always)] + fn convert_mounted_data(&self, event: &dioxus_html::PlatformEventData) -> MountedData { + #[cfg(feature = "mounted")] + { + MountedData::from( + event + .downcast::() + .expect("event should be a web_sys::Element"), + ) + } + #[cfg(not(feature = "mounted"))] + { + panic!("mounted events are not supported without the mounted feature on the dioxus-web crate enabled") + } + } + + #[inline(always)] + fn convert_mouse_data(&self, event: &dioxus_html::PlatformEventData) -> dioxus_html::MouseData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_pointer_data( + &self, + event: &dioxus_html::PlatformEventData, + ) -> dioxus_html::PointerData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_scroll_data( + &self, + event: &dioxus_html::PlatformEventData, + ) -> dioxus_html::ScrollData { + ScrollData::from(downcast_event(event).raw.clone()) + } + + #[inline(always)] + fn convert_selection_data( + &self, + event: &dioxus_html::PlatformEventData, + ) -> dioxus_html::SelectionData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_toggle_data( + &self, + event: &dioxus_html::PlatformEventData, + ) -> dioxus_html::ToggleData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_touch_data(&self, event: &dioxus_html::PlatformEventData) -> dioxus_html::TouchData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_transition_data( + &self, + event: &dioxus_html::PlatformEventData, + ) -> dioxus_html::TransitionData { + downcast_event(event).raw.clone().into() + } + + #[inline(always)] + fn convert_wheel_data(&self, event: &dioxus_html::PlatformEventData) -> dioxus_html::WheelData { + downcast_event(event).raw.clone().into() + } +} + +/// A extension trait for web-sys events that provides a way to get the event as a web-sys event. +pub trait WebEventExt { + /// Get the event as a web-sys event. + fn web_event(&self) -> &E; +} + +impl WebEventExt for dioxus_html::AnimationData { + fn web_event(&self) -> &web_sys::AnimationEvent { + self.downcast::() + .expect("event should be a WebAnimationEvent") + } +} + +impl WebEventExt for dioxus_html::ClipboardData { + fn web_event(&self) -> &web_sys::Event { + self.downcast::() + .expect("event should be a web_sys::Event") + } +} + +impl WebEventExt for dioxus_html::CompositionData { + fn web_event(&self) -> &web_sys::CompositionEvent { + self.downcast::() + .expect("event should be a WebCompositionEvent") + } +} + +impl WebEventExt for dioxus_html::DragData { + fn web_event(&self) -> &web_sys::MouseEvent { + self.downcast::() + .expect("event should be a WebMouseEvent") + } +} + +impl WebEventExt for dioxus_html::FocusData { + fn web_event(&self) -> &web_sys::FocusEvent { + self.downcast::() + .expect("event should be a WebFocusEvent") + } +} + +impl WebEventExt for dioxus_html::FormData { + fn web_event(&self) -> &web_sys::Event { + self.downcast::() + .expect("event should be a WebFormData") + } +} + +impl WebEventExt for dioxus_html::ImageData { + fn web_event(&self) -> &WebImageEvent { + self.downcast::() + .expect("event should be a WebImageEvent") + } +} + +impl WebEventExt for dioxus_html::KeyboardData { + fn web_event(&self) -> &web_sys::KeyboardEvent { + self.downcast::() + .expect("event should be a WebKeyboardEvent") + } +} + +impl WebEventExt for dioxus_html::MediaData { + fn web_event(&self) -> &web_sys::Event { + self.downcast::() + .expect("event should be a WebMediaEvent") + } +} + +impl WebEventExt for MountedData { + fn web_event(&self) -> &web_sys::Element { + self.downcast::() + .expect("event should be a web_sys::Element") + } +} + +impl WebEventExt for dioxus_html::MouseData { + fn web_event(&self) -> &web_sys::MouseEvent { + self.downcast::() + .expect("event should be a WebMouseEvent") + } +} + +impl WebEventExt for dioxus_html::PointerData { + fn web_event(&self) -> &web_sys::PointerEvent { + self.downcast::() + .expect("event should be a WebPointerEvent") + } +} + +impl WebEventExt for ScrollData { + fn web_event(&self) -> &web_sys::Event { + self.downcast::() + .expect("event should be a WebScrollEvent") + } +} + +impl WebEventExt for dioxus_html::SelectionData { + fn web_event(&self) -> &web_sys::Event { + self.downcast::() + .expect("event should be a WebSelectionEvent") + } +} + +impl WebEventExt for dioxus_html::ToggleData { + fn web_event(&self) -> &web_sys::Event { + self.downcast::() + .expect("event should be a WebToggleEvent") + } +} + +impl WebEventExt for dioxus_html::TouchData { + fn web_event(&self) -> &web_sys::TouchEvent { + self.downcast::() + .expect("event should be a WebTouchEvent") + } +} + +impl WebEventExt for dioxus_html::TransitionData { + fn web_event(&self) -> &web_sys::TransitionEvent { + self.downcast::() + .expect("event should be a WebTransitionEvent") + } +} + +impl WebEventExt for dioxus_html::WheelData { + fn web_event(&self) -> &web_sys::WheelEvent { + self.downcast::() + .expect("event should be a WebWheelEvent") + } +} + +struct GenericWebSysEvent { + raw: Event, + element: Element, +} + +// todo: some of these events are being casted to the wrong event type. +// We need tests that simulate clicks/etc and make sure every event type works. +pub(crate) fn virtual_event_from_websys_event( + event: web_sys::Event, + target: Element, +) -> PlatformEventData { + PlatformEventData::new(Box::new(GenericWebSysEvent { + raw: event, + element: target, + })) +} + +pub(crate) fn load_document() -> Document { + web_sys::window() + .expect("should have access to the Window") + .document() + .expect("should have access to the Document") +} + +struct WebImageEvent { + raw: Event, + error: bool, +} + +impl WebImageEvent { + fn new(raw: Event, error: bool) -> Self { + Self { raw, error } + } +} + +impl HasImageData for WebImageEvent { + fn load_error(&self) -> bool { + self.error + } + + fn as_any(&self) -> &dyn Any { + &self.raw as &dyn Any + } +} + +struct WebFormData { + element: Element, + raw: Event, +} + +impl WebFormData { + fn new(element: Element, raw: Event) -> Self { + Self { element, raw } + } +} + +impl HasFormData for WebFormData { + fn value(&self) -> String { + let target = &self.element; + target + .dyn_ref() + .map(|input: &web_sys::HtmlInputElement| { + // todo: special case more input types + match input.type_().as_str() { + "checkbox" => { + match input.checked() { + true => "true".to_string(), + false => "false".to_string(), + } + }, + _ => { + input.value() + } + } + }) + .or_else(|| { + target + .dyn_ref() + .map(|input: &web_sys::HtmlTextAreaElement| input.value()) + }) + // select elements are NOT input events - because - why woudn't they be?? + .or_else(|| { + target + .dyn_ref() + .map(|input: &web_sys::HtmlSelectElement| input.value()) + }) + .or_else(|| { + target + .dyn_ref::() + .unwrap() + .text_content() + }) + .expect("only an InputElement or TextAreaElement or an element with contenteditable=true can have an oninput event listener") + } + + fn values(&self) -> HashMap> { + let mut values = std::collections::HashMap::new(); + + // try to fill in form values + if let Some(form) = self.element.dyn_ref::() { + let form_data = get_form_data(form); + for value in form_data.entries().into_iter().flatten() { + if let Ok(array) = value.dyn_into::() { + if let Some(name) = array.get(0).as_string() { + if let Ok(item_values) = array.get(1).dyn_into::() { + let item_values = + item_values.iter().filter_map(|v| v.as_string()).collect(); + + values.insert(name, item_values); + } + } + } + } + } + + values + } + + fn files(&self) -> Option> { + #[cfg(not(feature = "file_engine"))] + let files = None; + #[cfg(feature = "file_engine")] + let files = self + .element + .dyn_ref() + .and_then(|input: &web_sys::HtmlInputElement| { + input.files().and_then(|files| { + #[allow(clippy::arc_with_non_send_sync)] + crate::file_engine::WebFileEngine::new(files).map(|f| { + std::sync::Arc::new(f) as std::sync::Arc + }) + }) + }); + + files + } + + fn as_any(&self) -> &dyn Any { + &self.raw as &dyn Any + } +} + +// web-sys does not expose the keys api for form data, so we need to manually bind to it +#[wasm_bindgen(inline_js = r#" + export function get_form_data(form) { + let values = new Map(); + const formData = new FormData(form); + + for (let name of formData.keys()) { + values.set(name, formData.getAll(name)); + } + + return values; + } +"#)] +extern "C" { + fn get_form_data(form: &web_sys::HtmlFormElement) -> js_sys::Map; +} diff --git a/packages/web/src/lib.rs b/packages/web/src/lib.rs index 60f84de379..112d10ee2f 100644 --- a/packages/web/src/lib.rs +++ b/packages/web/src/lib.rs @@ -53,7 +53,10 @@ // - Do the VDOM work during the idlecallback // - Do DOM work in the next requestAnimationFrame callback +use std::rc::Rc; + pub use crate::cfg::Config; +#[cfg(feature = "file_engine")] pub use crate::file_engine::WebFileEngineExt; use dioxus_core::{Element, Scope, VirtualDom}; use futures_util::{ @@ -66,6 +69,8 @@ mod cfg; mod dom; #[cfg(feature = "eval")] mod eval; +mod event; +pub use event::*; #[cfg(feature = "file_engine")] mod file_engine; #[cfg(all(feature = "hot_reload", debug_assertions))] @@ -275,7 +280,12 @@ pub async fn run_with_props(root: fn(Scope) -> Element, root_prop // Dequeue all of the events from the channel in send order // todo: we should re-order these if possible while let Some(evt) = res { - dom.handle_event(evt.name.as_str(), evt.data, evt.element, evt.bubbles); + dom.handle_event( + evt.name.as_str(), + Rc::new(evt.data), + evt.element, + evt.bubbles, + ); res = rx.try_next().transpose().unwrap().ok(); }