2023-05-28 20:02:59 +02:00
|
|
|
use std::cell::{Cell, RefCell};
|
2022-07-21 22:22:36 +03:00
|
|
|
use std::clone::Clone;
|
|
|
|
|
use std::collections::{vec_deque::IntoIter as VecDequeIter, VecDeque};
|
2023-05-28 20:02:59 +02:00
|
|
|
use std::iter;
|
2022-07-21 22:22:36 +03:00
|
|
|
use std::rc::Rc;
|
2023-06-14 10:26:26 +02:00
|
|
|
use std::sync::atomic::Ordering;
|
2022-07-21 22:22:36 +03:00
|
|
|
|
|
|
|
|
use raw_window_handle::{RawDisplayHandle, WebDisplayHandle};
|
|
|
|
|
|
2023-06-14 09:43:53 +02:00
|
|
|
use super::runner::EventWrapper;
|
2022-03-18 14:09:39 +01:00
|
|
|
use super::{
|
2023-05-28 20:02:59 +02:00
|
|
|
super::{monitor::MonitorHandle, KeyEventExtra},
|
|
|
|
|
backend,
|
|
|
|
|
device::DeviceId,
|
|
|
|
|
proxy::EventLoopProxy,
|
|
|
|
|
runner,
|
2022-03-18 14:09:39 +01:00
|
|
|
window::WindowId,
|
|
|
|
|
};
|
2021-02-16 17:50:46 -05:00
|
|
|
use crate::event::{
|
2023-08-28 19:18:10 +02:00
|
|
|
DeviceId as RootDeviceId, ElementState, Event, KeyEvent, Touch, TouchPhase, WindowEvent,
|
2021-02-16 17:50:46 -05:00
|
|
|
};
|
2023-06-14 10:26:26 +02:00
|
|
|
use crate::event_loop::DeviceEvents;
|
2023-05-28 20:02:59 +02:00
|
|
|
use crate::keyboard::ModifiersState;
|
2022-03-18 14:09:39 +01:00
|
|
|
use crate::window::{Theme, WindowId as RootWindowId};
|
2019-06-25 03:15:34 +02:00
|
|
|
|
2023-05-28 20:02:59 +02:00
|
|
|
#[derive(Default)]
|
|
|
|
|
struct ModifiersShared(Rc<Cell<ModifiersState>>);
|
|
|
|
|
|
|
|
|
|
impl ModifiersShared {
|
|
|
|
|
fn set(&self, new: ModifiersState) {
|
|
|
|
|
self.0.set(new)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get(&self) -> ModifiersState {
|
|
|
|
|
self.0.get()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Clone for ModifiersShared {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self(Rc::clone(&self.0))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
pub struct EventLoopWindowTarget<T: 'static> {
|
2019-06-25 03:15:34 +02:00
|
|
|
pub(crate) runner: runner::Shared<T>,
|
2023-05-28 20:02:59 +02:00
|
|
|
modifiers: ModifiersShared,
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
impl<T> Clone for EventLoopWindowTarget<T> {
|
2019-06-25 03:15:34 +02:00
|
|
|
fn clone(&self) -> Self {
|
2022-03-18 14:09:39 +01:00
|
|
|
Self {
|
2019-06-25 03:15:34 +02:00
|
|
|
runner: self.runner.clone(),
|
2023-05-28 20:02:59 +02:00
|
|
|
modifiers: self.modifiers.clone(),
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
impl<T> EventLoopWindowTarget<T> {
|
2019-06-25 03:15:34 +02:00
|
|
|
pub fn new() -> Self {
|
2022-03-18 14:09:39 +01:00
|
|
|
Self {
|
2019-06-25 03:15:34 +02:00
|
|
|
runner: runner::Shared::new(),
|
2023-05-28 20:02:59 +02:00
|
|
|
modifiers: ModifiersShared::default(),
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
pub fn proxy(&self) -> EventLoopProxy<T> {
|
|
|
|
|
EventLoopProxy::new(self.runner.clone())
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-25 12:03:22 +02:00
|
|
|
pub fn run(&self, event_handler: Box<runner::EventHandler<T>>, event_loop_recreation: bool) {
|
|
|
|
|
self.runner.event_loop_recreation(event_loop_recreation);
|
2019-06-25 03:15:34 +02:00
|
|
|
self.runner.set_listener(event_handler);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
pub fn generate_id(&self) -> WindowId {
|
|
|
|
|
WindowId(self.runner.generate_id())
|
2019-09-19 18:40:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-14 13:22:31 -02:30
|
|
|
pub fn register(
|
|
|
|
|
&self,
|
|
|
|
|
canvas: &Rc<RefCell<backend::Canvas>>,
|
|
|
|
|
id: WindowId,
|
|
|
|
|
prevent_default: bool,
|
|
|
|
|
) {
|
2022-03-18 14:09:39 +01:00
|
|
|
self.runner.add_canvas(RootWindowId(id), canvas);
|
2023-06-04 14:29:59 +02:00
|
|
|
let canvas_clone = canvas.clone();
|
2020-09-21 06:42:07 +08:00
|
|
|
let mut canvas = canvas.borrow_mut();
|
2019-09-27 17:06:14 -04:00
|
|
|
canvas.set_attribute("data-raw-handle", &id.0.to_string());
|
|
|
|
|
|
2022-09-04 13:45:30 +10:00
|
|
|
canvas.on_touch_start(prevent_default);
|
|
|
|
|
|
2020-09-21 06:42:07 +08:00
|
|
|
let runner = self.runner.clone();
|
2023-06-14 10:26:26 +02:00
|
|
|
let has_focus = canvas.has_focus.clone();
|
2023-05-28 20:02:59 +02:00
|
|
|
let modifiers = self.modifiers.clone();
|
2019-06-25 21:01:13 +02:00
|
|
|
canvas.on_blur(move || {
|
2023-06-14 10:26:26 +02:00
|
|
|
has_focus.store(false, Ordering::Relaxed);
|
2023-05-28 20:02:59 +02:00
|
|
|
|
|
|
|
|
let clear_modifiers = (!modifiers.get().is_empty()).then(|| {
|
|
|
|
|
modifiers.set(ModifiersState::empty());
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(ModifiersState::empty().into()),
|
|
|
|
|
}
|
2019-06-25 21:01:13 +02:00
|
|
|
});
|
2023-05-28 20:02:59 +02:00
|
|
|
|
|
|
|
|
runner.send_events(
|
|
|
|
|
clear_modifiers
|
|
|
|
|
.into_iter()
|
|
|
|
|
.chain(iter::once(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::Focused(false),
|
|
|
|
|
})),
|
|
|
|
|
);
|
2019-06-25 21:01:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let runner = self.runner.clone();
|
2023-06-14 10:26:26 +02:00
|
|
|
let has_focus = canvas.has_focus.clone();
|
2019-06-25 21:01:13 +02:00
|
|
|
canvas.on_focus(move || {
|
2023-06-14 10:26:26 +02:00
|
|
|
if !has_focus.swap(true, Ordering::Relaxed) {
|
2023-05-28 20:02:59 +02:00
|
|
|
runner.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::Focused(true),
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-06-25 21:01:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let runner = self.runner.clone();
|
2023-05-28 20:02:59 +02:00
|
|
|
let modifiers = self.modifiers.clone();
|
2022-07-14 13:22:31 -02:30
|
|
|
canvas.on_keyboard_press(
|
2023-05-28 20:02:59 +02:00
|
|
|
move |physical_key, logical_key, text, location, repeat, active_modifiers| {
|
2023-06-13 23:16:46 +02:00
|
|
|
let modifiers_changed = (modifiers.get() != active_modifiers).then(|| {
|
2023-05-28 20:02:59 +02:00
|
|
|
modifiers.set(active_modifiers);
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
|
|
|
|
}
|
2022-07-14 13:22:31 -02:30
|
|
|
});
|
2023-05-28 20:02:59 +02:00
|
|
|
|
2023-06-14 10:26:26 +02:00
|
|
|
let device_id = RootDeviceId(unsafe { DeviceId::dummy() });
|
|
|
|
|
|
2023-06-13 23:16:46 +02:00
|
|
|
runner.send_events(
|
2023-08-28 19:18:10 +02:00
|
|
|
iter::once(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::KeyboardInput {
|
|
|
|
|
device_id,
|
|
|
|
|
event: KeyEvent {
|
|
|
|
|
physical_key,
|
|
|
|
|
logical_key,
|
|
|
|
|
text,
|
|
|
|
|
location,
|
|
|
|
|
state: ElementState::Pressed,
|
|
|
|
|
repeat,
|
|
|
|
|
platform_specific: KeyEventExtra,
|
2023-06-13 23:16:46 +02:00
|
|
|
},
|
2023-08-28 19:18:10 +02:00
|
|
|
is_synthetic: false,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.chain(modifiers_changed),
|
2023-06-13 23:16:46 +02:00
|
|
|
);
|
2022-07-14 13:22:31 -02:30
|
|
|
},
|
|
|
|
|
prevent_default,
|
|
|
|
|
);
|
2019-06-25 21:01:13 +02:00
|
|
|
|
|
|
|
|
let runner = self.runner.clone();
|
2023-05-28 20:02:59 +02:00
|
|
|
let modifiers = self.modifiers.clone();
|
2022-07-14 13:22:31 -02:30
|
|
|
canvas.on_keyboard_release(
|
2023-05-28 20:02:59 +02:00
|
|
|
move |physical_key, logical_key, text, location, repeat, active_modifiers| {
|
2023-06-13 23:16:46 +02:00
|
|
|
let modifiers_changed = (modifiers.get() != active_modifiers).then(|| {
|
2023-05-28 20:02:59 +02:00
|
|
|
modifiers.set(active_modifiers);
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
|
|
|
|
}
|
2022-07-14 13:22:31 -02:30
|
|
|
});
|
2023-05-28 20:02:59 +02:00
|
|
|
|
2023-06-14 10:26:26 +02:00
|
|
|
let device_id = RootDeviceId(unsafe { DeviceId::dummy() });
|
|
|
|
|
|
2023-06-13 23:16:46 +02:00
|
|
|
runner.send_events(
|
2023-08-28 19:18:10 +02:00
|
|
|
iter::once(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::KeyboardInput {
|
|
|
|
|
device_id,
|
|
|
|
|
event: KeyEvent {
|
|
|
|
|
physical_key,
|
|
|
|
|
logical_key,
|
|
|
|
|
text,
|
|
|
|
|
location,
|
|
|
|
|
state: ElementState::Released,
|
|
|
|
|
repeat,
|
|
|
|
|
platform_specific: KeyEventExtra,
|
2023-06-13 23:16:46 +02:00
|
|
|
},
|
2023-08-28 19:18:10 +02:00
|
|
|
is_synthetic: false,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.chain(modifiers_changed),
|
2023-06-13 23:16:46 +02:00
|
|
|
)
|
2022-07-14 13:22:31 -02:30
|
|
|
},
|
|
|
|
|
prevent_default,
|
|
|
|
|
);
|
2019-06-25 21:01:13 +02:00
|
|
|
|
2023-06-14 10:26:26 +02:00
|
|
|
let has_focus = canvas.has_focus.clone();
|
2023-06-13 15:49:27 +02:00
|
|
|
canvas.on_cursor_leave({
|
|
|
|
|
let runner = self.runner.clone();
|
|
|
|
|
let has_focus = has_focus.clone();
|
|
|
|
|
let modifiers = self.modifiers.clone();
|
2019-06-25 21:18:11 +02:00
|
|
|
|
2023-06-13 15:49:27 +02:00
|
|
|
move |active_modifiers, pointer_id| {
|
|
|
|
|
let focus = (has_focus.load(Ordering::Relaxed)
|
|
|
|
|
&& modifiers.get() != active_modifiers)
|
|
|
|
|
.then(|| {
|
2023-06-04 02:16:55 +02:00
|
|
|
modifiers.set(active_modifiers);
|
2023-06-13 15:49:27 +02:00
|
|
|
Event::WindowEvent {
|
2023-06-04 02:16:55 +02:00
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
2023-06-13 15:49:27 +02:00
|
|
|
}
|
2023-06-04 02:16:55 +02:00
|
|
|
});
|
2023-06-13 15:49:27 +02:00
|
|
|
|
|
|
|
|
let pointer = pointer_id.map(|pointer_id| Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::CursorLeft {
|
|
|
|
|
device_id: RootDeviceId(DeviceId(pointer_id)),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if focus.is_some() || pointer.is_some() {
|
|
|
|
|
runner.send_events(focus.into_iter().chain(pointer))
|
2023-06-04 02:16:55 +02:00
|
|
|
}
|
2023-06-13 15:49:27 +02:00
|
|
|
}
|
|
|
|
|
});
|
2023-06-04 02:16:55 +02:00
|
|
|
|
2023-06-13 15:49:27 +02:00
|
|
|
canvas.on_cursor_enter({
|
|
|
|
|
let runner = self.runner.clone();
|
|
|
|
|
let has_focus = has_focus.clone();
|
|
|
|
|
let modifiers = self.modifiers.clone();
|
2023-06-04 02:16:55 +02:00
|
|
|
|
2023-06-13 15:49:27 +02:00
|
|
|
move |active_modifiers, pointer_id| {
|
|
|
|
|
let focus = (has_focus.load(Ordering::Relaxed)
|
|
|
|
|
&& modifiers.get() != active_modifiers)
|
|
|
|
|
.then(|| {
|
2023-06-04 02:16:55 +02:00
|
|
|
modifiers.set(active_modifiers);
|
2023-06-13 15:49:27 +02:00
|
|
|
Event::WindowEvent {
|
2023-06-04 02:16:55 +02:00
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
2023-06-13 15:49:27 +02:00
|
|
|
}
|
2023-06-04 02:16:55 +02:00
|
|
|
});
|
2023-06-13 15:49:27 +02:00
|
|
|
|
|
|
|
|
let pointer = pointer_id.map(|pointer_id| Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::CursorEntered {
|
|
|
|
|
device_id: RootDeviceId(DeviceId(pointer_id)),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if focus.is_some() || pointer.is_some() {
|
|
|
|
|
runner.send_events(focus.into_iter().chain(pointer))
|
2023-06-04 02:16:55 +02:00
|
|
|
}
|
2023-06-13 15:49:27 +02:00
|
|
|
}
|
|
|
|
|
});
|
2019-06-25 03:15:34 +02:00
|
|
|
|
2022-09-04 13:45:30 +10:00
|
|
|
canvas.on_cursor_move(
|
2023-06-04 01:08:03 +02:00
|
|
|
{
|
|
|
|
|
let runner = self.runner.clone();
|
|
|
|
|
let has_focus = has_focus.clone();
|
|
|
|
|
let modifiers = self.modifiers.clone();
|
|
|
|
|
|
|
|
|
|
move |active_modifiers| {
|
2023-06-05 02:44:54 +02:00
|
|
|
if has_focus.load(Ordering::Relaxed) && modifiers.get() != active_modifiers {
|
2023-05-28 20:02:59 +02:00
|
|
|
modifiers.set(active_modifiers);
|
2023-06-04 01:08:03 +02:00
|
|
|
runner.send_event(Event::WindowEvent {
|
2023-05-28 20:02:59 +02:00
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
2023-06-04 01:08:03 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
let runner = self.runner.clone();
|
2023-06-13 15:49:27 +02:00
|
|
|
let has_focus = has_focus.clone();
|
|
|
|
|
let modifiers = self.modifiers.clone();
|
2023-06-04 01:08:03 +02:00
|
|
|
|
2023-06-13 15:49:27 +02:00
|
|
|
move |active_modifiers, pointer_id, events| {
|
|
|
|
|
let modifiers = (has_focus.load(Ordering::Relaxed)
|
|
|
|
|
&& modifiers.get() != active_modifiers)
|
|
|
|
|
.then(|| {
|
|
|
|
|
modifiers.set(active_modifiers);
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-28 19:18:10 +02:00
|
|
|
runner.send_events(modifiers.into_iter().chain(events.flat_map(|position| {
|
|
|
|
|
let device_id = RootDeviceId(DeviceId(pointer_id));
|
|
|
|
|
|
|
|
|
|
iter::once(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::CursorMoved {
|
|
|
|
|
device_id,
|
|
|
|
|
position,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})));
|
2023-06-04 01:08:03 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
let runner = self.runner.clone();
|
2023-06-13 15:49:27 +02:00
|
|
|
let has_focus = has_focus.clone();
|
|
|
|
|
let modifiers = self.modifiers.clone();
|
2023-06-04 01:08:03 +02:00
|
|
|
|
2023-06-13 15:49:27 +02:00
|
|
|
move |active_modifiers, device_id, events| {
|
|
|
|
|
let modifiers = (has_focus.load(Ordering::Relaxed)
|
|
|
|
|
&& modifiers.get() != active_modifiers)
|
|
|
|
|
.then(|| {
|
|
|
|
|
modifiers.set(active_modifiers);
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
runner.send_events(modifiers.into_iter().chain(events.map(
|
|
|
|
|
|(location, force)| Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::Touch(Touch {
|
|
|
|
|
id: device_id as u64,
|
|
|
|
|
device_id: RootDeviceId(DeviceId(device_id)),
|
|
|
|
|
phase: TouchPhase::Moved,
|
|
|
|
|
force: Some(force),
|
|
|
|
|
location,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
)));
|
2023-06-04 01:08:03 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
let runner = self.runner.clone();
|
2023-06-13 15:49:27 +02:00
|
|
|
let has_focus = has_focus.clone();
|
|
|
|
|
let modifiers = self.modifiers.clone();
|
|
|
|
|
|
|
|
|
|
move |active_modifiers,
|
|
|
|
|
pointer_id,
|
|
|
|
|
position: crate::dpi::PhysicalPosition<f64>,
|
|
|
|
|
buttons,
|
|
|
|
|
button| {
|
|
|
|
|
let modifiers = (has_focus.load(Ordering::Relaxed)
|
|
|
|
|
&& modifiers.get() != active_modifiers)
|
|
|
|
|
.then(|| {
|
|
|
|
|
modifiers.set(active_modifiers);
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-05-28 20:02:59 +02:00
|
|
|
|
2023-06-14 10:26:26 +02:00
|
|
|
let device_id = RootDeviceId(DeviceId(pointer_id));
|
|
|
|
|
|
|
|
|
|
let state = if buttons.contains(button.into()) {
|
|
|
|
|
ElementState::Pressed
|
2023-06-02 11:37:23 +02:00
|
|
|
} else {
|
2023-06-14 10:26:26 +02:00
|
|
|
ElementState::Released
|
2023-06-04 01:08:03 +02:00
|
|
|
};
|
2023-06-02 11:37:23 +02:00
|
|
|
|
2023-06-04 01:08:03 +02:00
|
|
|
// A chorded button 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.
|
2023-08-28 19:18:10 +02:00
|
|
|
runner.send_events(modifiers.into_iter().chain([
|
2023-06-04 12:18:38 +02:00
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::CursorMoved {
|
2023-06-14 10:26:26 +02:00
|
|
|
device_id,
|
2023-06-04 12:18:38 +02:00
|
|
|
position,
|
2023-06-02 11:37:23 +02:00
|
|
|
},
|
2023-06-04 12:18:38 +02:00
|
|
|
},
|
2023-06-14 10:26:26 +02:00
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::MouseInput {
|
|
|
|
|
device_id,
|
|
|
|
|
state,
|
|
|
|
|
button,
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-06-13 15:49:27 +02:00
|
|
|
]));
|
2023-06-04 01:08:03 +02:00
|
|
|
}
|
2022-12-23 14:55:22 +09:00
|
|
|
},
|
2022-09-04 13:45:30 +10:00
|
|
|
prevent_default,
|
|
|
|
|
);
|
2019-06-25 03:15:34 +02:00
|
|
|
|
2022-12-23 14:55:22 +09:00
|
|
|
canvas.on_mouse_press(
|
2023-06-04 01:09:30 +02:00
|
|
|
{
|
|
|
|
|
let runner = self.runner.clone();
|
|
|
|
|
let modifiers = self.modifiers.clone();
|
2023-05-28 20:02:59 +02:00
|
|
|
|
2023-06-04 02:16:55 +02:00
|
|
|
move |active_modifiers| {
|
|
|
|
|
if modifiers.get() != active_modifiers {
|
2023-06-04 01:09:30 +02:00
|
|
|
modifiers.set(active_modifiers);
|
2023-06-04 02:16:55 +02:00
|
|
|
runner.send_event(Event::WindowEvent {
|
2023-06-04 01:09:30 +02:00
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
2023-06-04 02:16:55 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
let runner = self.runner.clone();
|
2023-06-13 15:49:27 +02:00
|
|
|
let modifiers = self.modifiers.clone();
|
|
|
|
|
|
|
|
|
|
move |active_modifiers, pointer_id, position, button| {
|
|
|
|
|
let modifiers = (modifiers.get() != active_modifiers).then(|| {
|
|
|
|
|
modifiers.set(active_modifiers);
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-06-04 01:09:30 +02:00
|
|
|
|
2023-06-14 10:26:26 +02:00
|
|
|
let device_id: RootDeviceId = RootDeviceId(DeviceId(pointer_id));
|
|
|
|
|
|
2023-06-04 01:09:30 +02:00
|
|
|
// 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.
|
2023-08-28 19:18:10 +02:00
|
|
|
runner.send_events(modifiers.into_iter().chain([
|
2023-06-04 12:18:38 +02:00
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::CursorMoved {
|
2023-06-14 10:26:26 +02:00
|
|
|
device_id,
|
2023-06-04 12:18:38 +02:00
|
|
|
position,
|
2023-06-04 01:09:30 +02:00
|
|
|
},
|
2023-06-04 12:18:38 +02:00
|
|
|
},
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::MouseInput {
|
2023-06-14 10:26:26 +02:00
|
|
|
device_id,
|
2023-06-04 12:18:38 +02:00
|
|
|
state: ElementState::Pressed,
|
|
|
|
|
button,
|
2023-06-04 01:09:30 +02:00
|
|
|
},
|
2023-06-04 12:18:38 +02:00
|
|
|
},
|
2023-06-13 15:49:27 +02:00
|
|
|
]));
|
2023-06-04 01:09:30 +02:00
|
|
|
}
|
2022-12-23 14:55:22 +09:00
|
|
|
},
|
2023-06-04 01:09:30 +02:00
|
|
|
{
|
|
|
|
|
let runner = self.runner.clone();
|
2023-06-13 15:49:27 +02:00
|
|
|
let modifiers = self.modifiers.clone();
|
2023-06-04 01:09:30 +02:00
|
|
|
|
2023-06-13 15:49:27 +02:00
|
|
|
move |active_modifiers, device_id, location, force| {
|
|
|
|
|
let modifiers = (modifiers.get() != active_modifiers).then(|| {
|
|
|
|
|
modifiers.set(active_modifiers);
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
runner.send_events(modifiers.into_iter().chain(iter::once(
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::Touch(Touch {
|
|
|
|
|
id: device_id as u64,
|
|
|
|
|
device_id: RootDeviceId(DeviceId(device_id)),
|
|
|
|
|
phase: TouchPhase::Started,
|
|
|
|
|
force: Some(force),
|
|
|
|
|
location,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
)))
|
2023-06-04 01:09:30 +02:00
|
|
|
}
|
2022-12-23 14:55:22 +09:00
|
|
|
},
|
2023-06-04 01:50:30 +02:00
|
|
|
prevent_default,
|
2022-12-23 14:55:22 +09:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
canvas.on_mouse_release(
|
2023-06-04 01:08:47 +02:00
|
|
|
{
|
|
|
|
|
let runner = self.runner.clone();
|
|
|
|
|
let has_focus = has_focus.clone();
|
2023-06-04 02:16:55 +02:00
|
|
|
let modifiers = self.modifiers.clone();
|
2023-06-04 01:08:47 +02:00
|
|
|
|
2023-06-04 02:16:55 +02:00
|
|
|
move |active_modifiers| {
|
2023-06-05 02:44:54 +02:00
|
|
|
if has_focus.load(Ordering::Relaxed) && modifiers.get() != active_modifiers {
|
2023-06-04 02:16:55 +02:00
|
|
|
modifiers.set(active_modifiers);
|
|
|
|
|
runner.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
2023-06-04 01:08:47 +02:00
|
|
|
});
|
2023-06-04 02:16:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
let runner = self.runner.clone();
|
2023-06-13 15:49:27 +02:00
|
|
|
let has_focus = has_focus.clone();
|
|
|
|
|
let modifiers = self.modifiers.clone();
|
|
|
|
|
|
|
|
|
|
move |active_modifiers, pointer_id, position, button| {
|
|
|
|
|
let modifiers = (has_focus.load(Ordering::Relaxed)
|
|
|
|
|
&& modifiers.get() != active_modifiers)
|
|
|
|
|
.then(|| {
|
|
|
|
|
modifiers.set(active_modifiers);
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-06-04 01:08:47 +02:00
|
|
|
|
2023-06-14 10:26:26 +02:00
|
|
|
let device_id: RootDeviceId = RootDeviceId(DeviceId(pointer_id));
|
|
|
|
|
|
2023-06-04 01:08:47 +02:00
|
|
|
// A mouse up 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.
|
2023-08-28 19:18:10 +02:00
|
|
|
runner.send_events(modifiers.into_iter().chain([
|
2023-06-04 12:18:38 +02:00
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::CursorMoved {
|
2023-06-14 10:26:26 +02:00
|
|
|
device_id,
|
2023-06-04 12:18:38 +02:00
|
|
|
position,
|
2023-06-04 01:08:47 +02:00
|
|
|
},
|
2023-06-04 12:18:38 +02:00
|
|
|
},
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::MouseInput {
|
2023-06-14 10:26:26 +02:00
|
|
|
device_id,
|
2023-06-04 12:18:38 +02:00
|
|
|
state: ElementState::Released,
|
|
|
|
|
button,
|
2023-06-04 01:08:47 +02:00
|
|
|
},
|
2023-06-04 12:18:38 +02:00
|
|
|
},
|
2023-06-13 15:49:27 +02:00
|
|
|
]));
|
2023-06-04 01:08:47 +02:00
|
|
|
}
|
2022-12-23 14:55:22 +09:00
|
|
|
},
|
2023-06-04 01:08:47 +02:00
|
|
|
{
|
|
|
|
|
let runner_touch = self.runner.clone();
|
2023-06-13 15:49:27 +02:00
|
|
|
let has_focus = has_focus.clone();
|
|
|
|
|
let modifiers = self.modifiers.clone();
|
2023-06-04 01:08:47 +02:00
|
|
|
|
2023-06-13 15:49:27 +02:00
|
|
|
move |active_modifiers, device_id, location, force| {
|
|
|
|
|
let modifiers = (has_focus.load(Ordering::Relaxed)
|
|
|
|
|
&& modifiers.get() != active_modifiers)
|
|
|
|
|
.then(|| {
|
|
|
|
|
modifiers.set(active_modifiers);
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
runner_touch.send_events(modifiers.into_iter().chain(iter::once(
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::Touch(Touch {
|
|
|
|
|
id: device_id as u64,
|
|
|
|
|
device_id: RootDeviceId(DeviceId(device_id)),
|
|
|
|
|
phase: TouchPhase::Ended,
|
|
|
|
|
force: Some(force),
|
|
|
|
|
location,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
)));
|
2023-06-04 01:08:47 +02:00
|
|
|
}
|
2022-12-23 14:55:22 +09:00
|
|
|
},
|
|
|
|
|
);
|
2019-06-25 03:15:34 +02:00
|
|
|
|
2019-06-25 18:07:47 +02:00
|
|
|
let runner = self.runner.clone();
|
2023-05-28 20:02:59 +02:00
|
|
|
let modifiers = self.modifiers.clone();
|
2022-07-14 13:22:31 -02:30
|
|
|
canvas.on_mouse_wheel(
|
2023-05-28 20:02:59 +02:00
|
|
|
move |pointer_id, delta, active_modifiers| {
|
2023-06-05 02:44:54 +02:00
|
|
|
let modifiers_changed = (has_focus.load(Ordering::Relaxed)
|
|
|
|
|
&& modifiers.get() != active_modifiers)
|
2023-05-28 20:02:59 +02:00
|
|
|
.then(|| {
|
|
|
|
|
modifiers.set(active_modifiers);
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-28 19:18:10 +02:00
|
|
|
runner.send_events(modifiers_changed.into_iter().chain(iter::once(
|
|
|
|
|
Event::WindowEvent {
|
2023-05-28 20:02:59 +02:00
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::MouseWheel {
|
|
|
|
|
device_id: RootDeviceId(DeviceId(pointer_id)),
|
|
|
|
|
delta,
|
|
|
|
|
phase: TouchPhase::Moved,
|
|
|
|
|
},
|
2023-08-28 19:18:10 +02:00
|
|
|
},
|
|
|
|
|
)));
|
2022-07-14 13:22:31 -02:30
|
|
|
},
|
|
|
|
|
prevent_default,
|
|
|
|
|
);
|
2019-10-11 11:45:07 -04:00
|
|
|
|
2022-12-23 14:55:22 +09:00
|
|
|
let runner = self.runner.clone();
|
|
|
|
|
canvas.on_touch_cancel(move |device_id, location, force| {
|
|
|
|
|
runner.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::Touch(Touch {
|
|
|
|
|
id: device_id as u64,
|
|
|
|
|
device_id: RootDeviceId(DeviceId(device_id)),
|
|
|
|
|
phase: TouchPhase::Cancelled,
|
|
|
|
|
force: Some(force),
|
|
|
|
|
location,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2020-02-17 20:25:27 +01:00
|
|
|
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 {
|
2022-03-18 14:09:39 +01:00
|
|
|
window_id: RootWindowId(id),
|
2020-02-17 20:25:27 +01:00
|
|
|
event: WindowEvent::ThemeChanged(theme),
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-06-14 09:43:53 +02:00
|
|
|
|
|
|
|
|
canvas.on_resize_scale(
|
|
|
|
|
{
|
|
|
|
|
let runner = self.runner.clone();
|
|
|
|
|
let canvas = canvas_clone.clone();
|
|
|
|
|
|
|
|
|
|
move |size, scale| {
|
|
|
|
|
runner.send_event(EventWrapper::ScaleChange {
|
|
|
|
|
canvas: Rc::downgrade(&canvas),
|
|
|
|
|
size,
|
|
|
|
|
scale,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
let runner = self.runner.clone();
|
2023-07-10 23:55:43 +02:00
|
|
|
let canvas = canvas_clone.clone();
|
2023-06-14 09:43:53 +02:00
|
|
|
|
|
|
|
|
move |new_size| {
|
2023-07-10 23:55:43 +02:00
|
|
|
let canvas = canvas.borrow();
|
2023-06-14 09:43:53 +02:00
|
|
|
canvas.set_current_size(new_size);
|
|
|
|
|
if canvas.old_size() != new_size {
|
|
|
|
|
canvas.set_old_size(new_size);
|
|
|
|
|
runner.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::Resized(new_size),
|
|
|
|
|
});
|
|
|
|
|
runner.request_redraw(RootWindowId(id));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-07-10 02:02:38 +02:00
|
|
|
|
|
|
|
|
let runner = self.runner.clone();
|
|
|
|
|
canvas.on_intersection(move |is_intersecting| {
|
2023-07-10 23:55:43 +02:00
|
|
|
// only fire if visible while skipping the first event if it's intersecting
|
2023-07-11 00:11:06 +02:00
|
|
|
if backend::is_visible(runner.document())
|
2023-07-10 23:55:43 +02:00
|
|
|
&& !(is_intersecting && canvas_clone.borrow().is_intersecting.is_none())
|
|
|
|
|
{
|
2023-07-10 02:02:38 +02:00
|
|
|
runner.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(id),
|
|
|
|
|
event: WindowEvent::Occluded(!is_intersecting),
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-07-10 23:55:43 +02:00
|
|
|
|
|
|
|
|
canvas_clone.borrow_mut().is_intersecting = Some(is_intersecting);
|
2023-06-23 16:21:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let runner = self.runner.clone();
|
|
|
|
|
canvas.on_animation_frame(move || runner.request_redraw(RootWindowId(id)));
|
2023-08-29 09:28:30 +02:00
|
|
|
|
|
|
|
|
canvas.on_touch_end();
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
2020-07-04 15:46:41 -04:00
|
|
|
|
2022-03-18 14:09:39 +01:00
|
|
|
pub fn available_monitors(&self) -> VecDequeIter<MonitorHandle> {
|
2020-07-04 15:46:41 -04:00
|
|
|
VecDeque::new().into_iter()
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 10:04:28 +02:00
|
|
|
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
|
2023-08-26 18:56:44 +02:00
|
|
|
None
|
2020-07-04 15:46:41 -04:00
|
|
|
}
|
2022-07-21 22:22:36 +03:00
|
|
|
|
|
|
|
|
pub fn raw_display_handle(&self) -> RawDisplayHandle {
|
|
|
|
|
RawDisplayHandle::Web(WebDisplayHandle::empty())
|
|
|
|
|
}
|
2023-06-14 10:26:26 +02:00
|
|
|
|
|
|
|
|
pub fn listen_device_events(&self, allowed: DeviceEvents) {
|
|
|
|
|
self.runner.listen_device_events(allowed)
|
|
|
|
|
}
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|