mirror of
https://github.com/adoyle0/thaw.git
synced 2025-02-08 19:03:09 -05:00
✨ feat: rewrite window_event_listener funtion
This commit is contained in:
parent
284a3be1f7
commit
8efe55780d
2 changed files with 27 additions and 15 deletions
|
@ -47,13 +47,13 @@ pub fn Slider(
|
||||||
let on_mouse_down = move |_| {
|
let on_mouse_down = move |_| {
|
||||||
set_mouse_move.set(true);
|
set_mouse_move.set(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
let on_mouse_up = window_event_listener("mouseup", move |_| {
|
let on_mouse_up = window_event_listener(ev::mouseup, move |_| {
|
||||||
set_mouse_move.set(false);
|
set_mouse_move.set(false);
|
||||||
});
|
});
|
||||||
on_cleanup(cx, on_mouse_up);
|
on_cleanup(cx, on_mouse_up);
|
||||||
|
|
||||||
let on_mouse_move = window_event_listener("mousemove", move |ev| {
|
let on_mouse_move = window_event_listener(ev::mousemove, move |ev| {
|
||||||
if is_mouse_move.get_untracked() {
|
if is_mouse_move.get_untracked() {
|
||||||
if let Some(rail) = rail_ref.get_untracked() {
|
if let Some(rail) = rail_ref.get_untracked() {
|
||||||
let ev = ev.unchecked_into::<web_sys::MouseEvent>();
|
let ev = ev.unchecked_into::<web_sys::MouseEvent>();
|
||||||
|
|
|
@ -1,16 +1,28 @@
|
||||||
use leptos::window;
|
use leptos::window;
|
||||||
|
use leptos_dom::ev;
|
||||||
|
use std::borrow::Cow;
|
||||||
use wasm_bindgen::{prelude::Closure, JsCast};
|
use wasm_bindgen::{prelude::Closure, JsCast};
|
||||||
|
|
||||||
pub fn window_event_listener<'a>(
|
pub fn window_event_listener<E: ev::EventDescriptor + 'static>(
|
||||||
event_name: &'a str,
|
event: E,
|
||||||
cb: impl Fn(web_sys::Event) + 'static,
|
cb: impl Fn(E::EventType) + 'static,
|
||||||
) -> impl FnOnce() -> () + 'a {
|
) -> impl FnOnce() -> ()
|
||||||
let handler = Box::new(cb) as Box<dyn FnMut(web_sys::Event)>;
|
where
|
||||||
|
E::EventType: JsCast,
|
||||||
let cb = Closure::wrap(handler).into_js_value();
|
{
|
||||||
_ = window().add_event_listener_with_callback(event_name, cb.unchecked_ref());
|
fn wel(
|
||||||
|
cb: Box<dyn FnMut(web_sys::Event)>,
|
||||||
move || {
|
event_name: Cow<'static, str>,
|
||||||
_ = window().remove_event_listener_with_callback(event_name, cb.unchecked_ref());
|
) -> impl FnOnce() -> () + 'static {
|
||||||
|
let cb = Closure::wrap(cb).into_js_value();
|
||||||
|
_ = window().add_event_listener_with_callback(&event_name, cb.unchecked_ref());
|
||||||
|
move || {
|
||||||
|
_ = window().remove_event_listener_with_callback(&event_name, cb.unchecked_ref());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
wel(
|
||||||
|
Box::new(move |e| cb(e.unchecked_into::<E::EventType>())),
|
||||||
|
event.name(),
|
||||||
|
)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue