107 lines
3.2 KiB
Rust
107 lines
3.2 KiB
Rust
|
|
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<T: 'static> {
|
||
|
|
pub(crate) runner: runner::Shared<T>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<T> Clone for WindowTarget<T> {
|
||
|
|
fn clone(&self) -> Self {
|
||
|
|
WindowTarget {
|
||
|
|
runner: self.runner.clone(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<T> WindowTarget<T> {
|
||
|
|
pub fn new() -> Self {
|
||
|
|
WindowTarget {
|
||
|
|
runner: runner::Shared::new(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn proxy(&self) -> Proxy<T> {
|
||
|
|
Proxy::new(self.runner.clone())
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn run(
|
||
|
|
&self,
|
||
|
|
event_handler: Box<dyn FnMut(Event<T>, &mut ControlFlow)>,
|
||
|
|
) -> &runner::Shared<T> {
|
||
|
|
self.runner.set_listener(event_handler);
|
||
|
|
&self.runner
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn register(&self, canvas: &backend::Canvas) {
|
||
|
|
let runner = &self.runner;
|
||
|
|
|
||
|
|
canvas.on_mouse_out(|pointer_id| {
|
||
|
|
runner.send_event(Event::WindowEvent {
|
||
|
|
window_id: WindowId(window::Id),
|
||
|
|
event: WindowEvent::CursorLeft {
|
||
|
|
device_id: DeviceId(device::Id(pointer_id)),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
canvas.on_mouse_over(|pointer_id| {
|
||
|
|
runner.send_event(Event::WindowEvent {
|
||
|
|
window_id: WindowId(window::Id),
|
||
|
|
event: WindowEvent::CursorEntered {
|
||
|
|
device_id: DeviceId(device::Id(pointer_id)),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
canvas.on_mouse_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,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
canvas.on_mouse_up(|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,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
canvas.on_mouse_down(|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,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
canvas.on_mouse_scroll(|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,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|