use crate::event_handlers::{WindowEventHandlers, WinitEventHandlerId};
use dioxus_core::{Runtime, consume_context, current_scope_id, use_hook_with_cleanup};
use std::rc::Rc;
use winit::{
event::{ElementState, WindowEvent},
event_loop::ActiveEventLoop,
keyboard::{Key, NamedKey},
};
pub fn use_window_event(
mut handler: impl FnMut(&WindowEvent, &dyn ActiveEventLoop) + 'static,
) -> WinitEventHandlerId {
let runtime = Runtime::current();
let scope_id = current_scope_id();
let window_id = crate::use_window().id();
use_hook_with_cleanup(
move || {
let handlers: Rc<WindowEventHandlers> = consume_context();
handlers.add(window_id, move |event, target| {
runtime.in_scope(scope_id, || handler(event, target))
})
},
move |handler| handler.remove(),
)
}
pub fn use_back_button(mut handler: impl FnMut() + 'static) -> WinitEventHandlerId {
use_window_event(move |event, _target| {
if let WindowEvent::KeyboardInput { event, .. } = event
&& event.state == ElementState::Pressed
&& !event.repeat
&& event.logical_key == Key::Named(NamedKey::BrowserBack)
{
handler();
}
})
}