Skip to content

Commit

Permalink
feat: implement on_mousedown on Window
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 2, 2024
1 parent e1b9a47 commit ee71b36
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions crates/core/src/dom/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ impl DomNode {
}
}

pub(crate) fn as_element(&self) -> web_sys::Element {
/// exposed the underlying wrapped node as `web_sys::Element`
pub fn as_element(&self) -> web_sys::Element {
match &self.inner {
DomInner::Element { element, .. } => element.clone().unchecked_into(),
DomInner::Element { element, .. } => element.clone(),
DomInner::Fragment { fragment, .. } => {
let fragment: web_sys::Element = fragment.clone().unchecked_into();
assert!(fragment.is_object());
Expand Down
22 changes: 22 additions & 0 deletions crates/core/src/dom/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ impl Window {
Cmd::sub(rx, mousemove_cb)
}

///
pub fn on_mousedown<F, MSG>(mut cb: F) -> Cmd<MSG>
where
F: FnMut(web_sys::MouseEvent) -> MSG + Clone + 'static,
MSG: 'static,
{
let (mut tx, rx) = mpsc::unbounded();
let mousemove_cb: Closure<dyn FnMut(web_sys::Event)> =
Closure::new(move |event: web_sys::Event| {
let mouse_event: MouseEvent = event.dyn_into().expect("must be mouse event");
let msg = cb(mouse_event);
tx.start_send(msg).expect("send");
});
window()
.add_event_listener_with_callback(
intern("mousedown"),
mousemove_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, mousemove_cb)
}

/// do this task at every `ms` interval
pub fn every_interval<F, MSG>(interval_ms: i32, cb: F) -> Cmd<MSG>
where
Expand Down

0 comments on commit ee71b36

Please sign in to comment.