winit/src/platform_impl/web/event_loop/window_target.rs

283 lines
9.6 KiB
Rust
Raw Normal View History

use super::{
super::monitor::MonitorHandle, backend, device::DeviceId, proxy::EventLoopProxy, runner,
window::WindowId,
};
use crate::dpi::{PhysicalSize, Size};
use crate::event::{
DeviceEvent, DeviceId as RootDeviceId, ElementState, Event, KeyboardInput, TouchPhase,
WindowEvent,
};
2019-06-25 03:15:34 +02:00
use crate::event_loop::ControlFlow;
use crate::monitor::MonitorHandle as RootMH;
use crate::window::{Theme, WindowId as RootWindowId};
use std::cell::RefCell;
2019-06-25 03:15:34 +02:00
use std::clone::Clone;
use std::collections::{vec_deque::IntoIter as VecDequeIter, VecDeque};
use std::rc::Rc;
2019-06-25 03:15:34 +02:00
pub struct EventLoopWindowTarget<T: 'static> {
2019-06-25 03:15:34 +02:00
pub(crate) runner: runner::Shared<T>,
}
impl<T> Clone for EventLoopWindowTarget<T> {
2019-06-25 03:15:34 +02:00
fn clone(&self) -> Self {
Self {
2019-06-25 03:15:34 +02:00
runner: self.runner.clone(),
}
}
}
impl<T> EventLoopWindowTarget<T> {
2019-06-25 03:15:34 +02:00
pub fn new() -> Self {
Self {
2019-06-25 03:15:34 +02:00
runner: runner::Shared::new(),
}
}
pub fn proxy(&self) -> EventLoopProxy<T> {
EventLoopProxy::new(self.runner.clone())
2019-06-25 03:15:34 +02:00
}
pub fn run(&self, event_handler: Box<dyn FnMut(Event<'_, T>, &mut ControlFlow)>) {
2019-06-25 03:15:34 +02:00
self.runner.set_listener(event_handler);
let runner = self.runner.clone();
self.runner.set_on_scale_change(move |arg| {
runner.handle_scale_changed(arg.old_scale, arg.new_scale)
});
2019-06-25 03:15:34 +02:00
}
pub fn generate_id(&self) -> WindowId {
WindowId(self.runner.generate_id())
}
pub fn register(
&self,
canvas: &Rc<RefCell<backend::Canvas>>,
id: WindowId,
prevent_default: bool,
) {
self.runner.add_canvas(RootWindowId(id), canvas);
let mut canvas = canvas.borrow_mut();
2019-09-27 17:06:14 -04:00
canvas.set_attribute("data-raw-handle", &id.0.to_string());
let runner = self.runner.clone();
canvas.on_blur(move || {
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Focused(false),
});
});
let runner = self.runner.clone();
canvas.on_focus(move || {
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Focused(true),
});
});
let runner = self.runner.clone();
canvas.on_keyboard_press(
move |scancode, virtual_keycode, modifiers| {
#[allow(deprecated)]
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::KeyboardInput {
device_id: RootDeviceId(unsafe { DeviceId::dummy() }),
input: KeyboardInput {
scancode,
state: ElementState::Pressed,
virtual_keycode,
modifiers,
},
is_synthetic: false,
},
});
},
prevent_default,
);
let runner = self.runner.clone();
canvas.on_keyboard_release(
move |scancode, virtual_keycode, modifiers| {
#[allow(deprecated)]
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::KeyboardInput {
device_id: RootDeviceId(unsafe { DeviceId::dummy() }),
input: KeyboardInput {
scancode,
state: ElementState::Released,
virtual_keycode,
modifiers,
},
is_synthetic: false,
},
});
},
prevent_default,
);
2019-06-25 21:18:11 +02:00
let runner = self.runner.clone();
canvas.on_received_character(
move |char_code| {
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::ReceivedCharacter(char_code),
});
},
prevent_default,
);
2019-06-25 21:18:11 +02:00
let runner = self.runner.clone();
canvas.on_cursor_leave(move |pointer_id| {
2019-06-25 03:15:34 +02:00
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
2019-06-25 03:15:34 +02:00
event: WindowEvent::CursorLeft {
device_id: RootDeviceId(DeviceId(pointer_id)),
2019-06-25 03:15:34 +02:00
},
});
});
let runner = self.runner.clone();
canvas.on_cursor_enter(move |pointer_id| {
2019-06-25 03:15:34 +02:00
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
2019-06-25 03:15:34 +02:00
event: WindowEvent::CursorEntered {
device_id: RootDeviceId(DeviceId(pointer_id)),
2019-06-25 03:15:34 +02:00
},
});
});
let runner = self.runner.clone();
canvas.on_cursor_move(move |pointer_id, position, delta, modifiers| {
2019-06-25 03:15:34 +02:00
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
2019-06-25 03:15:34 +02:00
event: WindowEvent::CursorMoved {
device_id: RootDeviceId(DeviceId(pointer_id)),
2019-06-25 03:15:34 +02:00
position,
modifiers,
},
});
runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(pointer_id)),
event: DeviceEvent::MouseMotion {
delta: (delta.x, delta.y),
},
});
2019-06-25 03:15:34 +02:00
});
let runner = self.runner.clone();
canvas.on_mouse_press(move |pointer_id, position, button, modifiers| {
// A mouse down event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the
// user code has the correct cursor position.
runner.send_events(
std::iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Focused(true),
})
.chain(std::iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
device_id: RootDeviceId(DeviceId(pointer_id)),
position,
modifiers,
},
}))
.chain(std::iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::MouseInput {
device_id: RootDeviceId(DeviceId(pointer_id)),
state: ElementState::Pressed,
button,
modifiers,
},
})),
);
2019-06-25 03:15:34 +02:00
});
let runner = self.runner.clone();
canvas.on_mouse_release(move |pointer_id, button, modifiers| {
2019-06-25 03:15:34 +02:00
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
2019-06-25 03:15:34 +02:00
event: WindowEvent::MouseInput {
device_id: RootDeviceId(DeviceId(pointer_id)),
2019-06-29 17:48:22 +02:00
state: ElementState::Released,
2019-06-25 03:15:34 +02:00
button,
modifiers,
},
});
});
let runner = self.runner.clone();
canvas.on_mouse_wheel(
move |pointer_id, delta, modifiers| {
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::MouseWheel {
device_id: RootDeviceId(DeviceId(pointer_id)),
delta,
phase: TouchPhase::Moved,
modifiers,
},
});
},
prevent_default,
);
let runner = self.runner.clone();
let raw = canvas.raw().clone();
// The size to restore to after exiting fullscreen.
let mut intended_size = PhysicalSize {
width: raw.width() as u32,
height: raw.height() as u32,
};
canvas.on_fullscreen_change(move || {
// If the canvas is marked as fullscreen, it is moving *into* fullscreen
// If it is not, it is moving *out of* fullscreen
let new_size = if backend::is_fullscreen(&raw) {
intended_size = PhysicalSize {
width: raw.width() as u32,
height: raw.height() as u32,
};
backend::window_size().to_physical(backend::scale_factor())
} else {
intended_size
};
backend::set_canvas_size(&raw, Size::Physical(new_size));
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Resized(new_size),
});
runner.request_redraw(RootWindowId(id));
});
let runner = self.runner.clone();
canvas.on_dark_mode(move |is_dark_mode| {
let theme = if is_dark_mode {
Theme::Dark
} else {
Theme::Light
};
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::ThemeChanged(theme),
});
});
2019-06-25 03:15:34 +02:00
}
pub fn available_monitors(&self) -> VecDequeIter<MonitorHandle> {
VecDeque::new().into_iter()
}
pub fn primary_monitor(&self) -> Option<RootMH> {
Some(RootMH {
inner: MonitorHandle,
})
}
2019-06-25 03:15:34 +02:00
}