move creation functions into the structs they are creating and unify user and system events

This commit is contained in:
Evan Almloff committed 2024-01-05 13:18:48 -06:00
1 parent a4a9aa5224
commit d90c71c508
8 files changed
+110 -319

No files matched your search

+13 -2
View File
@@ -1,4 +1,4 @@
use crate::{runtime::with_runtime, ScopeId};
use crate::{runtime::with_runtime, scope_context::current_scope_id, ScopeId};
use std::{
cell::{Cell, RefCell},
rc::Rc,
@@ -149,7 +149,18 @@ impl<T> Default for EventHandler<T> {
type ExternalListenerCallback<T> = Box<dyn FnMut(T)>;
impl<T> EventHandler< T> {
impl<T> EventHandler<T> {
/// Create a new [`EventHandler`] from an [`FnMut`]
pub fn new(mut f: impl FnMut(T) + 'static) -> EventHandler<T> {
let callback = RefCell::new(Some(Box::new(move |event: T| {
f(event);
}) as Box<dyn FnMut(T)>));
EventHandler {
callback,
origin: current_scope_id().expect("to be in a dioxus runtime"),
}
}
/// Call this event handler with the appropriate event type
///
/// This borrows the event using a RefCell. Recursively calling a listener will cause a panic.