Better event handler conversion.

This commit is contained in:
Bodil Stokke 2018-11-16 00:32:43 +00:00
parent a9dc58da9c
commit 2b95530637
3 changed files with 35 additions and 5 deletions

View File

@ -193,7 +193,7 @@ impl Element {
let key = TokenTree::Ident(key.clone()); let key = TokenTree::Ident(key.clone());
let value = process_value(value); let value = process_value(value);
body.extend(quote!( body.extend(quote!(
element.events.$key = Some(Box::new($value)); element.events.$key = Some(typed_html::events::IntoEventHandler::into_event_handler($value));
)); ));
} }

View File

@ -112,6 +112,10 @@ pub trait EventHandler<EventType> {
fn render(&self) -> Option<String>; fn render(&self) -> Option<String>;
} }
pub trait IntoEventHandler<E> {
fn into_event_handler(self) -> Box<dyn EventHandler<E>>;
}
pub struct EFn<F, E>(Option<F>, PhantomData<E>); pub struct EFn<F, E>(Option<F>, PhantomData<E>);
impl<F, E> EFn<F, E> impl<F, E> EFn<F, E>
@ -124,6 +128,26 @@ where
} }
} }
impl<F, E> IntoEventHandler<E> for EFn<F, E>
where
F: FnMut(E) + 'static,
E: ConcreteEvent + 'static,
{
fn into_event_handler(self) -> Box<dyn EventHandler<E>> {
Box::new(self)
}
}
impl<F, E> IntoEventHandler<E> for F
where
F: FnMut(E) + 'static,
E: ConcreteEvent + 'static,
{
fn into_event_handler(self) -> Box<dyn EventHandler<E>> {
Box::new(EFn::new(self))
}
}
impl<F, E> EventHandler<E> for EFn<F, E> impl<F, E> EventHandler<E> for EFn<F, E>
where where
F: FnMut(E) + 'static, F: FnMut(E) + 'static,
@ -139,7 +163,7 @@ where
} }
} }
impl<'a, EventType> EventHandler<EventType> for &'a str { impl<E> EventHandler<E> for &'static str {
fn attach(&mut self, _target: &Element) -> EventListenerHandle { fn attach(&mut self, _target: &Element) -> EventListenerHandle {
panic!("Silly wabbit, strings as event handlers are only for printing."); panic!("Silly wabbit, strings as event handlers are only for printing.");
} }
@ -148,3 +172,9 @@ impl<'a, EventType> EventHandler<EventType> for &'a str {
Some(self.to_string()) Some(self.to_string())
} }
} }
impl<E> IntoEventHandler<E> for &'static str {
fn into_event_handler(self) -> Box<dyn EventHandler<E>> {
Box::new(self)
}
}

View File

@ -6,9 +6,9 @@ extern crate typed_html;
extern crate typed_html_macros; extern crate typed_html_macros;
use stdweb::web::{self, Element, IElement, INode}; use stdweb::web::{self, Element, IElement, INode};
use typed_html::for_events;
use typed_html::dom::{Node, VNode}; use typed_html::dom::{Node, VNode};
use typed_html::events::{EFn, Events}; use typed_html::events::Events;
use typed_html::for_events;
use typed_html_macros::html; use typed_html_macros::html;
fn install_handlers(target: &Element, handlers: &mut Events) { fn install_handlers(target: &Element, handlers: &mut Events) {
@ -47,7 +47,7 @@ fn main() {
". She is a "<em>"human girl"</em>"." ". She is a "<em>"human girl"</em>"."
</p> </p>
<p> <p>
<button onclick=EFn::new(|_event| web::alert("Hello Joe!"))> <button onclick=(|_event| web::alert("Hello Joe!"))>
"Call Joe" "Call Joe"
</button> </button>
</p> </p>