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

web-sys listener conversion #813

Merged
merged 19 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
170 changes: 0 additions & 170 deletions src/html/listener.rs

This file was deleted.

46 changes: 46 additions & 0 deletions src/html/listener/listener_stdweb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Inspired by: http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html-Events
impl_action! {
onclick(event: ClickEvent) -> ClickEvent => |_, event| { event }
ondoubleclick(event: DoubleClickEvent) -> DoubleClickEvent => |_, event| { event }
onkeypress(event: KeyPressEvent) -> KeyPressEvent => |_, event| { event }
onkeydown(event: KeyDownEvent) -> KeyDownEvent => |_, event| { event }
onkeyup(event: KeyUpEvent) -> KeyUpEvent => |_, event| { event }
onmousemove(event: MouseMoveEvent) -> MouseMoveEvent => |_, event| { event }
onmousedown(event: MouseDownEvent) -> MouseDownEvent => |_, event| { event }
onmouseup(event: MouseUpEvent) -> MouseUpEvent => |_, event| { event }
onmouseover(event: MouseOverEvent) -> MouseOverEvent => |_, event| { event }
onmouseout(event: MouseOutEvent) -> MouseOutEvent => |_, event| { event }
onmouseenter(event: MouseEnterEvent) -> MouseEnterEvent => |_, event| { event }
onmouseleave(event: MouseLeaveEvent) -> MouseLeaveEvent => |_, event| { event }
onmousewheel(event: MouseWheelEvent) -> MouseWheelEvent => |_, event| { event }
ongotpointercapture(event: GotPointerCaptureEvent) -> GotPointerCaptureEvent => |_, event| { event }
onlostpointercapture(event: LostPointerCaptureEvent) -> LostPointerCaptureEvent => |_, event| { event }
onpointercancel(event: PointerCancelEvent) -> PointerCancelEvent => |_, event| { event }
onpointerdown(event: PointerDownEvent) -> PointerDownEvent => |_, event| { event }
onpointerenter(event: PointerEnterEvent) -> PointerEnterEvent => |_, event| { event }
onpointerleave(event: PointerLeaveEvent) -> PointerLeaveEvent => |_, event| { event }
onpointermove(event: PointerMoveEvent) -> PointerMoveEvent => |_, event| { event }
onpointerout(event: PointerOutEvent) -> PointerOutEvent => |_, event| { event }
onpointerover(event: PointerOverEvent) -> PointerOverEvent => |_, event| { event }
onpointerup(event: PointerUpEvent) -> PointerUpEvent => |_, event| { event }
onscroll(event: ScrollEvent) -> ScrollEvent => |_, event| { event }
onblur(event: BlurEvent) -> BlurEvent => |_, event| { event }
onfocus(event: FocusEvent) -> FocusEvent => |_, event| { event }
onsubmit(event: SubmitEvent) -> SubmitEvent => |_, event| { event }
ondragstart(event: DragStartEvent) -> DragStartEvent => |_, event| { event }
ondrag(event: DragEvent) -> DragEvent => |_, event| { event }
ondragend(event: DragEndEvent) -> DragEndEvent => |_, event| { event }
ondragenter(event: DragEnterEvent) -> DragEnterEvent => |_, event| { event }
ondragleave(event: DragLeaveEvent) -> DragLeaveEvent => |_, event| { event }
ondragover(event: DragOverEvent) -> DragOverEvent => |_, event| { event }
ondragexit(event: DragExitEvent) -> DragExitEvent => |_, event| { event }
ondrop(event: DragDropEvent) -> DragDropEvent => |_, event| { event }
oncontextmenu(event: ContextMenuEvent) -> ContextMenuEvent => |_, event| { event }
oninput(event: InputEvent) -> InputData => |this: &Element, _| { oninput_handler(this) }
onchange(event: ChangeEvent) -> ChangeData => |this: &Element, _| { onchange_handler(this) }
touchcancel(event: TouchCancel) -> TouchCancel => |_, event| { event }
touchend(event: TouchEnd) -> TouchEnd => |_, event| { event }
touchenter(event: TouchEnter) -> TouchEnter => |_, event| { event }
touchmove(event: TouchMove) -> TouchMove => |_, event| { event }
touchstart(event: TouchStart) -> TouchStart => |_, event| { event }
}
46 changes: 46 additions & 0 deletions src/html/listener/listener_web_sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Inspired by: http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html-Events
impl_action! {
onclick(name: "click", event: MouseEvent) -> web_sys::MouseEvent => |_, event| { event }
ondoubleclick(name: "dblclick", event: MouseEvent) -> web_sys::MouseEvent => |_, event| { event }
onkeypress(name: "keypress", event: KeyboardEvent) -> web_sys::KeyboardEvent => |_, event| { event }
onkeydown(name: "keydown", event: KeyboardEvent) -> web_sys::KeyboardEvent => |_, event| { event }
onkeyup(name: "keyup", event: KeyboardEvent) -> web_sys::KeyboardEvent => |_, event| { event }
onmousemove(name: "mousemove", event: MouseEvent) -> web_sys::MouseEvent => |_, event| { event }
onmousedown(name: "mousedown", event: MouseEvent) -> web_sys::MouseEvent => |_, event| { event }
onmouseup(name: "mouseup", event: MouseEvent) -> web_sys::MouseEvent => |_, event| { event }
onmouseover(name: "mouseover", event: MouseEvent) -> web_sys::MouseEvent => |_, event| { event }
onmouseout(name: "mouseout", event: MouseEvent) -> web_sys::MouseEvent => |_, event| { event }
onmouseenter(name: "mouseenter", event: MouseEvent) -> web_sys::MouseEvent => |_, event| { event }
onmouseleave(name: "mouseleave", event: MouseEvent) -> web_sys::MouseEvent => |_, event| { event }
onmousewheel(name: "wheel", event: WheelEvent) -> web_sys::WheelEvent => |_, event| { event }
ongotpointercapture(name: "gotpointercapture", event: PointerEvent) -> web_sys::PointerEvent => |_, event| { event }
onlostpointercapture(name: "lostpointercapture", event: PointerEvent) -> web_sys::PointerEvent => |_, event| { event }
onpointercancel(name: "pointercancel", event: PointerEvent) -> web_sys::PointerEvent => |_, event| { event }
onpointerdown(name: "pointerdown", event: PointerEvent) -> web_sys::PointerEvent => |_, event| { event }
onpointerenter(name: "pointerenter", event: PointerEvent) -> web_sys::PointerEvent => |_, event| { event }
onpointerleave(name: "pointerleave", event: PointerEvent) -> web_sys::PointerEvent => |_, event| { event }
onpointermove(name: "pointermove", event: PointerEvent) -> web_sys::PointerEvent => |_, event| { event }
onpointerout(name: "pointerout", event: PointerEvent) -> web_sys::PointerEvent => |_, event| { event }
onpointerover(name: "pointerover", event: PointerEvent) -> web_sys::PointerEvent => |_, event| { event }
onpointerup(name: "pointerup", event: PointerEvent) -> web_sys::PointerEvent => |_, event| { event }
onscroll(name: "scroll", event: UiEvent) -> web_sys::UiEvent => |_, event| { event }
onblur(name: "blur", event: FocusEvent) -> web_sys::FocusEvent => |_, event| { event }
onfocus(name: "focus", event: FocusEvent) -> web_sys::FocusEvent => |_, event| { event }
onsubmit(name: "submit", event: Event) -> web_sys::Event => |_, event| { event }
ondragstart(name: "dragstart", event: DragEvent) -> web_sys::DragEvent => |_, event| { event }
ondrag(name: "drag", event: DragEvent) -> web_sys::DragEvent => |_, event| { event }
ondragend(name: "dragend", event: DragEvent) -> web_sys::DragEvent => |_, event| { event }
ondragenter(name: "dragenter", event: DragEvent) -> web_sys::DragEvent => |_, event| { event }
ondragleave(name: "dragleave", event: DragEvent) -> web_sys::DragEvent => |_, event| { event }
ondragover(name: "dragover", event: DragEvent) -> web_sys::DragEvent => |_, event| { event }
ondragexit(name: "dragexit", event: DragEvent) -> web_sys::DragEvent => |_, event| { event }
ondrop(name: "drop", event: DragEvent) -> web_sys::DragEvent => |_, event| { event }
oncontextmenu(name: "contextmenu", event: MouseEvent) -> web_sys::MouseEvent => |_, event| { event }
oninput(name: "input", event: Event) -> InputData => |this: &Element, _| { oninput_handler(this) }
onchange(name: "change", event: Event) -> ChangeData => |this: &Element, _| { onchange_handler(this) }
touchcancel(name: "touchcancel", event: TouchEvent) -> web_sys::TouchEvent => |_, event| { event }
touchend(name: "touchend", event: TouchEvent) -> web_sys::TouchEvent => |_, event| { event }
touchenter(name: "touchenter", event: TouchEvent) -> web_sys::TouchEvent => |_, event| { event }
touchmove(name: "touchmove", event: TouchEvent) -> web_sys::TouchEvent => |_, event| { event }
touchstart(name: "touchstart", event: TouchEvent) -> web_sys::TouchEvent => |_, event| { event }
}
68 changes: 68 additions & 0 deletions src/html/listener/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#[macro_use]
macro_rules! impl_action {
($($action:ident(event: $type:ident) -> $ret:ty => $convert:expr)*) => {$(
impl_action!($action(name: "", event: $type) -> $ret => $convert);
)*};
($($action:ident(name: $name:literal, event: $type:ident) -> $ret:ty => $convert:expr)*) => {$(
/// An abstract implementation of a listener.
pub mod $action {
use crate::callback::Callback;
#[allow(unused_imports)]
use crate::html::listener::*;
use crate::virtual_dom::Listener;
#[cfg(feature = "std_web")]
use stdweb::web::{
event::{$type, IEvent},
Element, IEventTarget,
};
#[cfg(feature = "web_sys")]
use ::{
gloo::events::EventListener,
wasm_bindgen::JsValue,
web_sys::{$type as WebSysType, Element, EventTarget},
};

/// A wrapper for a callback which attaches event listeners to elements.
#[derive(Clone, Debug)]
pub struct Wrapper {
callback: Callback<Event>,
}

impl Wrapper {
/// Create a wrapper for an event-typed callback
pub fn new(callback: Callback<Event>) -> Self {
Wrapper { callback }
}
}

/// And event type which keeps the returned type.
pub type Event = $ret;

impl Listener for Wrapper {
fn kind(&self) -> &'static str {
stringify!($action)
}

fn attach(&self, element: &Element) -> EventListener {
let this = element.clone();
let callback = self.callback.clone();
let listener = move |
#[cfg(feature = "std_web")] event: $type,
#[cfg(feature = "web_sys")] event: &web_sys::Event
| {
event.stop_propagation();
#[cfg(feature = "web_sys")]
let event: WebSysType = JsValue::from(event).into();
callback.emit($convert(&this, event));
};
#[cfg(feature = "std_web")]
{
EventListener(Some(element.add_event_listener(listener)))
}
#[cfg(feature = "web_sys")]
EventListener::new(&EventTarget::from(element.clone()), $name, listener)
}
}
}
)*};
}
Loading