wip: pass more events

This commit is contained in:
Jonathan Kelley committed 2022-11-15 16:37:23 -08:00
1 parent 47d0f51e00
commit 0dad91bc08
20 files changed
+773 -31

No files matched your search

+34 -1
View File
@@ -1,5 +1,38 @@
use bumpalo::boxed::Box as BumpBox;
use std::{any::Any, cell::RefCell};
use std::{
any::Any,
cell::{Cell, RefCell},
fmt::Debug,
rc::Rc,
};
pub struct UiEvent<T> {
bubble_state: Cell<bool>,
data: Rc<T>,
}
impl<T> UiEvent<T> {
pub fn cancel_bubble(&self) {
self.bubble_state.set(false);
}
}
impl<T> std::ops::Deref for UiEvent<T> {
type Target = Rc<T>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T: Debug> std::fmt::Debug for UiEvent<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("UiEvent")
.field("bubble_state", &self.bubble_state)
.field("data", &self.data)
.finish()
}
}
/// Priority of Event Triggers.
///
+13 -2
View File
@@ -1,4 +1,7 @@
use std::{cell::Cell, fmt::Arguments};
use std::{
cell::{Cell, RefCell},
fmt::Arguments,
};
use bumpalo::boxed::Box as BumpBox;
use bumpalo::Bump;
@@ -7,7 +10,7 @@ use std::future::Future;
use crate::{
any_props::{AnyProps, VComponentProps},
arena::ElementId,
innerlude::DynamicNode,
innerlude::{DynamicNode, EventHandler},
Attribute, AttributeValue, Element, LazyNodes, Properties, Scope, ScopeState, VNode,
};
@@ -106,6 +109,14 @@ impl ScopeState {
scope: Cell::new(None),
}
}
/// Create a new [`EventHandler`] from an [`FnMut`]
pub fn event_handler<'a, T>(&'a self, f: impl FnMut(T) + 'a) -> EventHandler<'a, T> {
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 }
}
}
pub trait ComponentReturn<'a, A = ()> {
+2 -1
View File
@@ -92,6 +92,7 @@ pub use crate::innerlude::{
Template,
TemplateAttribute,
TemplateNode,
UiEvent,
VNode,
VirtualDom,
};
@@ -103,7 +104,7 @@ pub mod prelude {
pub use crate::innerlude::{
fc_to_builder, Element, EventHandler, EventPriority, Fragment, LazyNodes, NodeFactory,
Properties, Scope, ScopeId, ScopeState, Scoped, TaskId, Template, TemplateAttribute,
TemplateNode, VNode, VirtualDom,
TemplateNode, UiEvent, VNode, VirtualDom,
};
}