use super::{backend, device, proxy::Proxy, runner, window}; use crate::event::{DeviceId, ElementState, Event, TouchPhase, WindowEvent}; use crate::event_loop::ControlFlow; use crate::window::WindowId; use std::clone::Clone; pub struct WindowTarget { pub(crate) runner: runner::Shared, } impl Clone for WindowTarget { fn clone(&self) -> Self { WindowTarget { runner: self.runner.clone(), } } } impl WindowTarget { pub fn new() -> Self { WindowTarget { runner: runner::Shared::new(), } } pub fn proxy(&self) -> Proxy { Proxy::new(self.runner.clone()) } pub fn run( &self, event_handler: Box, &mut ControlFlow)>, ) -> &runner::Shared { self.runner.set_listener(event_handler); &self.runner } pub fn register(&self, canvas: &mut backend::Canvas) { let runner = self.runner.clone(); canvas.on_mouse_out(move |pointer_id| { runner.send_event(Event::WindowEvent { window_id: WindowId(window::Id), event: WindowEvent::CursorLeft { device_id: DeviceId(device::Id(pointer_id)), }, }); }); let runner = self.runner.clone(); canvas.on_mouse_over(move |pointer_id| { runner.send_event(Event::WindowEvent { window_id: WindowId(window::Id), event: WindowEvent::CursorEntered { device_id: DeviceId(device::Id(pointer_id)), }, }); }); let runner = self.runner.clone(); canvas.on_mouse_move(move |pointer_id, position, modifiers| { runner.send_event(Event::WindowEvent { window_id: WindowId(window::Id), event: WindowEvent::CursorMoved { device_id: DeviceId(device::Id(pointer_id)), position, modifiers, }, }); }); let runner = self.runner.clone(); canvas.on_mouse_up(move |pointer_id, button, modifiers| { runner.send_event(Event::WindowEvent { window_id: WindowId(window::Id), event: WindowEvent::MouseInput { device_id: DeviceId(device::Id(pointer_id)), state: ElementState::Released, button, modifiers, }, }); }); let runner = self.runner.clone(); canvas.on_mouse_down(move |pointer_id, button, modifiers| { runner.send_event(Event::WindowEvent { window_id: WindowId(window::Id), event: WindowEvent::MouseInput { device_id: DeviceId(device::Id(pointer_id)), state: ElementState::Pressed, button, modifiers, }, }); }); let runner = self.runner.clone(); canvas.on_mouse_scroll(move |pointer_id, delta, modifiers| { runner.send_event(Event::WindowEvent { window_id: WindowId(window::Id), event: WindowEvent::MouseWheel { device_id: DeviceId(device::Id(pointer_id)), delta, phase: TouchPhase::Moved, modifiers, }, }); }); } }