Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework Event System #1402

Merged
merged 65 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
cc3f60d
Adding PointInteraction trait, etc.
esimkowitz Aug 23, 2023
0e5600f
add impl for pointer
esimkowitz Aug 23, 2023
e0f42f8
Fix rink
esimkowitz Aug 23, 2023
b771386
fix dioxusmousebuttons
esimkowitz Aug 23, 2023
af9875f
revert change to cargo
esimkowitz Aug 23, 2023
5609511
Fix tests
esimkowitz Aug 23, 2023
e8b9599
fix text_like import
esimkowitz Aug 23, 2023
500051e
fix formatting issue
esimkowitz Aug 24, 2023
a8af958
Add dxfmt::skip and workspace recommendations
esimkowitz Aug 24, 2023
4e584fb
add even better toml
esimkowitz Aug 24, 2023
bad3078
address comments
esimkowitz Aug 24, 2023
f599ad6
remove unnecessary PointInteraction imports now that it's in prelude
esimkowitz Aug 24, 2023
c9da743
fix another bad format
esimkowitz Aug 24, 2023
a5b97aa
fix hover.rs formatting
esimkowitz Aug 24, 2023
f9de91e
revert settings.json, remove extensions.json
esimkowitz Aug 24, 2023
a5d687f
fix formatting
esimkowitz Aug 24, 2023
124b58a
missed one
esimkowitz Aug 24, 2023
a727181
fix failing check
esimkowitz Aug 24, 2023
299b123
trait based event system
ealmloff Aug 25, 2023
ca1a502
use an event converter for bundle splitting
ealmloff Aug 26, 2023
b54f7dd
fix no default features on the web renderer
ealmloff Aug 26, 2023
b4ebf34
forward data PointInteraction data in the PointerData debug and parti…
ealmloff Aug 28, 2023
f7dff22
expose all serialized events as public
ealmloff Aug 28, 2023
93cbfd0
derive more traits for serialized types
ealmloff Aug 28, 2023
0ef765e
move rink over to the new event system
ealmloff Aug 28, 2023
62556b2
fix desktop and liveview compilation
ealmloff Aug 28, 2023
bd387d6
fix formatting
ealmloff Aug 28, 2023
8ba1c05
fix desktop events
ealmloff Aug 28, 2023
097eb20
add extension to downcast web events
ealmloff Aug 28, 2023
177db23
add some inline hints
ealmloff Aug 28, 2023
4879f65
fix tui events
ealmloff Aug 31, 2023
f7451c0
port liveview events
ealmloff Aug 31, 2023
3eafb05
fix features on rink html dependency
ealmloff Aug 31, 2023
3c01593
document HtmlEventConverter
ealmloff Aug 31, 2023
bf90b75
add some inline hints
ealmloff Aug 31, 2023
364654e
fix clippy with events
ealmloff Aug 31, 2023
f812e52
Merge branch 'fix-new-clippy-lints' into events-2
ealmloff Aug 31, 2023
dee92b8
remove the serde feature flag from enumset
ealmloff Sep 1, 2023
df222c1
port examples to new events
ealmloff Sep 1, 2023
baf36ab
Merge branch 'master' into events-2
ealmloff Sep 2, 2023
b1b2537
Merge branch 'master' into events-2
ealmloff Sep 4, 2023
18fd40a
pin tauri bundler version
ealmloff Sep 4, 2023
9e623d5
Merge branch 'master' into events-2
ealmloff Sep 4, 2023
bd38cdd
fix dioxus core event tests
ealmloff Sep 5, 2023
0ab7a02
fix clippy
ealmloff Sep 6, 2023
d06e64e
fix dioxus-tui events
ealmloff Sep 6, 2023
3a02520
fix web onmount event
ealmloff Sep 8, 2023
f449823
Merge branch 'master' into events-2
ealmloff Sep 12, 2023
a9bb047
split the point interaction trait into smaller traits for touch compa…
ealmloff Sep 12, 2023
b470641
Merge branch 'events-2' of https://github.com/Demonthos/dioxus into e…
ealmloff Sep 12, 2023
c10700c
add touch data to touch events
ealmloff Sep 12, 2023
27f4b3f
add touches_changed and target_touches
ealmloff Sep 12, 2023
fc5c538
fix rink imports
ealmloff Sep 13, 2023
219c068
Merge branch 'master' into events-2
ealmloff Sep 18, 2023
c933bfa
fix formatting
ealmloff Sep 18, 2023
072ca35
Merge branch 'DioxusLabs:master' into events-2
ealmloff Oct 3, 2023
5bb06b9
Merge branch 'master' into events-2
ealmloff Oct 30, 2023
57dd56c
Merge branch 'miri-leak' into events-2
ealmloff Oct 30, 2023
ff7aca7
fix todomvc
ealmloff Oct 31, 2023
dccfba1
Merge branch 'master' into events-2
ealmloff Jan 5, 2024
0b40878
fix cargo check
ealmloff Jan 5, 2024
0388a5c
fix formatting
ealmloff Jan 5, 2024
fae81e2
fix event tests in core
ealmloff Jan 5, 2024
7ffb204
fix event tests in core
ealmloff Jan 5, 2024
4e6eee8
Merge branch 'events-2' of https://github.com/Demonthos/dioxus into e…
ealmloff Jan 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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" }
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/examples/hello_world_tui_no_ctrl_c.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// todo remove deprecated
#![allow(non_snake_case, deprecated)]

use dioxus::events::{KeyCode, KeyboardEvent};
use dioxus::events::KeyboardEvent;
use dioxus::prelude::*;
use dioxus_tui::TuiContext;

Expand All @@ -26,7 +26,7 @@ fn App(cx: Scope) -> Element {
background_color: "red",
justify_content: "center",
align_items: "center",
onkeydown: move |k: KeyboardEvent| if let KeyCode::Q = k.key_code {
onkeydown: move |k: KeyboardEvent| if let Code::KeyQ = k.code() {
tui_ctx.quit();
},

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/examples/input_controlled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn App(cx: Scope) -> Element {
// we tell the component what to render
value: "{name}",
// and what to do when the value changes
oninput: move |evt| name.set(evt.value.clone()),
oninput: move |evt| name.set(evt.value()),
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/examples/meme_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn MemeEditor(cx: Scope) -> Element {
},
CaptionEditor {
caption: caption,
on_input: move |event: FormEvent| {caption.set(event.value.clone());},
on_input: move |event: FormEvent| {caption.set(event.value());},
},
}
})
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/examples/meme_editor_dark_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn DarkModeToggle(cx: Scope) -> Element {
input {
r#type: "checkbox",
oninput: move |event| {
let is_enabled = event.value == "true";
let is_enabled = event.value() == "true";
dark_mode.write().0 = is_enabled;
},
},
Expand Down Expand Up @@ -97,7 +97,7 @@ fn MemeEditor(cx: Scope) -> Element {
},
CaptionEditor {
caption: caption,
on_input: move |event: FormEvent| {caption.set(event.value.clone());},
on_input: move |event: FormEvent| {caption.set(event.value().clone());},
},
}
})
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/examples/rendering_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ cx.render(rsx!(
},
input {
value: "{comment_field}",
oninput: |event| comment_field.set(event.value.clone()),
oninput: |event| comment_field.set(event.value()),
}
input {
r#type: "submit",
Expand Down Expand Up @@ -71,7 +71,7 @@ cx.render(rsx!(
},
input {
value: "{comment_field}",
oninput: |event| comment_field.set(event.value.clone()),
oninput: |event| comment_field.set(event.value()),
}
input {
r#type: "submit",
Expand Down
2 changes: 1 addition & 1 deletion examples/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn compose(cx: Scope<ComposeProps>) -> Element {

input {
oninput: move |e| {
user_input.set(e.value.clone());
user_input.set(e.value());
},
value: "{user_input}"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn app(cx: Scope) -> Element {
input {
value: "{counter}",
oninput: move |e| {
if let Ok(value) = e.value.parse::<usize>() {
if let Ok(value) = e.value().parse::<usize>() {
counters.make_mut()[i] = value;
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/crm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand All @@ -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())
}
}

Expand All @@ -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())
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/file_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions examples/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 2 additions & 2 deletions examples/login_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/shared_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn DataEditor(cx: Scope, id: usize) -> Element {
fn DataView(cx: Scope, id: usize) -> Element {
let cool_data = use_shared_state::<CoolData>(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();
Expand Down
2 changes: 1 addition & 1 deletion examples/textarea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
})
}
6 changes: 3 additions & 3 deletions examples/todomvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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| {
Expand Down
2 changes: 1 addition & 1 deletion examples/window_zoom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn app(cx: Scope) -> Element {
r#type: "number",
value: "{level}",
oninput: |e| {
if let Ok(new_zoom) = e.value.parse::<f64>() {
if let Ok(new_zoom) = e.value().parse::<f64>() {
level.set(new_zoom);
window.webview.zoom(new_zoom);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/xss_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}
})
Expand Down
1 change: 1 addition & 0 deletions packages/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,7 @@ impl<'b> VirtualDom {
}

// 4. Compute the LIS of this list
let mut lis_sequence = Vec::default();
lis_sequence.reserve(new_index_to_old_index.len());
let mut lis_sequence = Vec::with_capacity(new_index_to_old_index.len());

let mut predecessors = vec![0; new_index_to_old_index.len()];
let mut starts = vec![0; new_index_to_old_index.len()];
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ pub struct Event<T: 'static + ?Sized> {
}

impl<T> Event<T> {
/// Map the event data to a new type
///
/// # Example
///
/// ```rust, ignore
/// rsx! {
/// button {
/// onclick: move |evt: Event<FormData>| {
/// let data = evt.map(|data| data.value());
/// assert_eq!(data.inner(), "hello world");
/// }
/// }
/// }
/// ```
pub fn map<U: 'static, F: FnOnce(&T) -> U>(&self, f: F) -> Event<U> {
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
Expand Down
9 changes: 8 additions & 1 deletion packages/core/tests/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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);

Expand All @@ -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::<SerializedMouseData>::default())),
ElementId(1),
true,
);

dom.mark_dirty(ScopeId::ROOT);
let edits = dom.render_immediate();
Expand Down
10 changes: 9 additions & 1 deletion packages/core/tests/miri_full_app.rs
Original file line number Diff line number Diff line change
@@ -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::<SerializedMouseData>::default())),
ElementId(2),
true,
);
dom.process_events();
_ = dom.render_immediate();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/suspense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn suspended_child(cx: Scope) -> Element {
cx.spawn(async move {
val += 1;
});
return cx.suspend()?;
cx.suspend()?;
}

render!("child")
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
6 changes: 3 additions & 3 deletions packages/desktop/headless_tests/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions packages/desktop/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
Loading