2023-08-29 09:28:30 +02:00
|
|
|
use std::cell::Cell;
|
2023-12-22 22:33:50 +01:00
|
|
|
use std::ops::Deref;
|
2023-12-17 13:31:48 +01:00
|
|
|
use std::rc::Rc;
|
2023-07-31 00:39:01 +04:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-10-11 11:45:07 -04:00
|
|
|
|
2023-05-28 20:02:59 +02:00
|
|
|
use smol_str::SmolStr;
|
2023-07-11 00:34:02 +02:00
|
|
|
use wasm_bindgen::{closure::Closure, JsCast};
|
2023-07-11 00:11:06 +02:00
|
|
|
use web_sys::{
|
2023-12-22 00:11:36 +01:00
|
|
|
CssStyleDeclaration, Document, Event, FocusEvent, HtmlCanvasElement, KeyboardEvent,
|
|
|
|
|
PointerEvent, WheelEvent,
|
2023-07-11 00:11:06 +02:00
|
|
|
};
|
2019-06-25 03:15:34 +02:00
|
|
|
|
2023-07-31 00:39:01 +04:00
|
|
|
use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
|
|
|
|
|
use crate::error::OsError as RootOE;
|
|
|
|
|
use crate::event::{Force, InnerSizeWriter, MouseButton, MouseScrollDelta};
|
2023-10-19 15:27:49 +01:00
|
|
|
use crate::keyboard::{Key, KeyLocation, ModifiersState, PhysicalKey};
|
2023-07-31 00:39:01 +04:00
|
|
|
use crate::platform_impl::{OsError, PlatformSpecificWindowBuilderAttributes};
|
|
|
|
|
use crate::window::{WindowAttributes, WindowId as RootWindowId};
|
|
|
|
|
|
|
|
|
|
use super::super::WindowId;
|
2023-06-23 16:21:41 +02:00
|
|
|
use super::animation_frame::AnimationFrameHandler;
|
2023-07-31 00:39:01 +04:00
|
|
|
use super::event_handle::EventListenerHandle;
|
|
|
|
|
use super::intersection_handle::IntersectionObserverHandle;
|
|
|
|
|
use super::media_query_handle::MediaQueryListHandle;
|
|
|
|
|
use super::pointer::PointerHandler;
|
2023-12-17 13:31:48 +01:00
|
|
|
use super::{event, fullscreen, ButtonsState, ResizeScaleHandle};
|
2023-07-31 00:39:01 +04:00
|
|
|
|
2021-03-30 21:27:32 +02:00
|
|
|
#[allow(dead_code)]
|
2019-06-25 03:15:34 +02:00
|
|
|
pub struct Canvas {
|
2020-08-29 21:34:33 +08:00
|
|
|
common: Common,
|
2023-06-14 09:43:53 +02:00
|
|
|
id: WindowId,
|
2023-10-16 15:50:22 +02:00
|
|
|
pub has_focus: Rc<Cell<bool>>,
|
2023-07-10 23:55:43 +02:00
|
|
|
pub is_intersecting: Option<bool>,
|
2022-09-04 13:45:30 +10:00
|
|
|
on_touch_start: Option<EventListenerHandle<dyn FnMut(Event)>>,
|
2020-09-21 06:42:07 +08:00
|
|
|
on_focus: Option<EventListenerHandle<dyn FnMut(FocusEvent)>>,
|
|
|
|
|
on_blur: Option<EventListenerHandle<dyn FnMut(FocusEvent)>>,
|
|
|
|
|
on_keyboard_release: Option<EventListenerHandle<dyn FnMut(KeyboardEvent)>>,
|
|
|
|
|
on_keyboard_press: Option<EventListenerHandle<dyn FnMut(KeyboardEvent)>>,
|
|
|
|
|
on_mouse_wheel: Option<EventListenerHandle<dyn FnMut(WheelEvent)>>,
|
|
|
|
|
on_dark_mode: Option<MediaQueryListHandle>,
|
2023-06-04 01:44:53 +02:00
|
|
|
pointer_handler: PointerHandler,
|
2023-06-14 09:43:53 +02:00
|
|
|
on_resize_scale: Option<ResizeScaleHandle>,
|
2023-07-10 02:02:38 +02:00
|
|
|
on_intersect: Option<IntersectionObserverHandle>,
|
2023-06-23 16:21:41 +02:00
|
|
|
animation_frame_handler: AnimationFrameHandler,
|
2023-08-29 09:28:30 +02:00
|
|
|
on_touch_end: Option<EventListenerHandle<dyn FnMut(Event)>>,
|
2023-12-22 00:11:36 +01:00
|
|
|
on_context_menu: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>,
|
2020-08-29 21:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-04 01:44:53 +02:00
|
|
|
pub struct Common {
|
2023-06-05 02:44:54 +02:00
|
|
|
pub window: web_sys::Window,
|
2023-07-11 00:11:06 +02:00
|
|
|
pub document: Document,
|
2020-08-29 21:34:33 +08:00
|
|
|
/// Note: resizing the HTMLCanvasElement should go through `backend::set_canvas_size` to ensure the DPI factor is maintained.
|
2023-12-22 22:33:50 +01:00
|
|
|
/// Note: this is read-only because we use a pointer to this for [`WindowHandle`](rwh_06::WindowHandle).
|
|
|
|
|
raw: Rc<HtmlCanvasElement>,
|
2023-11-10 22:46:51 +01:00
|
|
|
style: Style,
|
2023-06-14 09:43:53 +02:00
|
|
|
old_size: Rc<Cell<PhysicalSize<u32>>>,
|
|
|
|
|
current_size: Rc<Cell<PhysicalSize<u32>>>,
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-16 22:02:17 +02:00
|
|
|
#[derive(Clone, Debug)]
|
2023-11-10 22:46:51 +01:00
|
|
|
pub struct Style {
|
|
|
|
|
read: CssStyleDeclaration,
|
|
|
|
|
write: CssStyleDeclaration,
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 03:15:34 +02:00
|
|
|
impl Canvas {
|
2023-06-05 02:44:54 +02:00
|
|
|
pub fn create(
|
2023-06-14 09:43:53 +02:00
|
|
|
id: WindowId,
|
2023-06-05 02:44:54 +02:00
|
|
|
window: web_sys::Window,
|
2023-07-11 00:11:06 +02:00
|
|
|
document: Document,
|
2023-06-04 14:29:59 +02:00
|
|
|
attr: &WindowAttributes,
|
|
|
|
|
platform_attr: PlatformSpecificWindowBuilderAttributes,
|
2023-06-05 02:44:54 +02:00
|
|
|
) -> Result<Self, RootOE> {
|
2023-10-17 04:54:12 +04:00
|
|
|
let canvas = match platform_attr.canvas.0 {
|
2020-01-15 21:20:14 -05:00
|
|
|
Some(canvas) => canvas,
|
2023-07-11 00:11:06 +02:00
|
|
|
None => document
|
|
|
|
|
.create_element("canvas")
|
|
|
|
|
.map_err(|_| os_error!(OsError("Failed to create canvas element".to_owned())))?
|
|
|
|
|
.unchecked_into(),
|
2020-01-15 21:20:14 -05:00
|
|
|
};
|
2019-06-25 03:15:34 +02:00
|
|
|
|
2023-07-12 17:11:52 +02:00
|
|
|
if platform_attr.append && !document.contains(Some(&canvas)) {
|
|
|
|
|
document
|
|
|
|
|
.body()
|
|
|
|
|
.expect("Failed to get body from document")
|
|
|
|
|
.append_child(&canvas)
|
|
|
|
|
.expect("Failed to append canvas to body");
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-08 23:51:41 +02:00
|
|
|
// A tabindex is needed in order to capture local keyboard events.
|
|
|
|
|
// A "0" value means that the element should be focusable in
|
|
|
|
|
// sequential keyboard navigation, but its order is defined by the
|
|
|
|
|
// document's source order.
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex
|
2023-06-04 14:29:59 +02:00
|
|
|
if platform_attr.focusable {
|
2022-07-14 13:22:31 -02:30
|
|
|
canvas
|
|
|
|
|
.set_attribute("tabindex", "0")
|
|
|
|
|
.map_err(|_| os_error!(OsError("Failed to set a tabindex".to_owned())))?;
|
|
|
|
|
}
|
2019-06-25 21:01:13 +02:00
|
|
|
|
2023-11-10 22:46:51 +01:00
|
|
|
let style = Style::new(&window, &canvas);
|
2023-07-11 00:11:06 +02:00
|
|
|
|
2023-07-11 13:14:40 +02:00
|
|
|
let common = Common {
|
2023-06-23 16:21:41 +02:00
|
|
|
window: window.clone(),
|
2023-08-29 09:28:30 +02:00
|
|
|
document: document.clone(),
|
2023-12-22 22:33:50 +01:00
|
|
|
raw: Rc::new(canvas.clone()),
|
2023-07-11 13:14:40 +02:00
|
|
|
style,
|
|
|
|
|
old_size: Rc::default(),
|
|
|
|
|
current_size: Rc::default(),
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-14 09:43:53 +02:00
|
|
|
if let Some(size) = attr.inner_size {
|
2023-07-11 13:14:40 +02:00
|
|
|
let size = size.to_logical(super::scale_factor(&common.window));
|
|
|
|
|
super::set_canvas_size(&common.document, &common.raw, &common.style, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(size) = attr.min_inner_size {
|
|
|
|
|
let size = size.to_logical(super::scale_factor(&common.window));
|
|
|
|
|
super::set_canvas_min_size(&common.document, &common.raw, &common.style, Some(size));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(size) = attr.max_inner_size {
|
|
|
|
|
let size = size.to_logical(super::scale_factor(&common.window));
|
|
|
|
|
super::set_canvas_max_size(&common.document, &common.raw, &common.style, Some(size));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(position) = attr.position {
|
|
|
|
|
let position = position.to_logical(super::scale_factor(&common.window));
|
|
|
|
|
super::set_canvas_position(&common.document, &common.raw, &common.style, position);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 04:54:12 +04:00
|
|
|
if attr.fullscreen.0.is_some() {
|
2023-12-17 13:31:48 +01:00
|
|
|
fullscreen::request_fullscreen(&document, &canvas);
|
2023-07-11 13:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if attr.active {
|
|
|
|
|
let _ = common.raw.focus();
|
2023-06-14 09:43:53 +02:00
|
|
|
}
|
2023-06-04 14:29:59 +02:00
|
|
|
|
2023-06-14 09:43:53 +02:00
|
|
|
Ok(Canvas {
|
2023-07-11 13:14:40 +02:00
|
|
|
common,
|
2023-06-14 09:43:53 +02:00
|
|
|
id,
|
2023-10-16 15:50:22 +02:00
|
|
|
has_focus: Rc::new(Cell::new(false)),
|
2023-07-10 23:55:43 +02:00
|
|
|
is_intersecting: None,
|
2022-09-04 13:45:30 +10:00
|
|
|
on_touch_start: None,
|
2019-06-25 21:01:13 +02:00
|
|
|
on_blur: None,
|
|
|
|
|
on_focus: None,
|
2019-06-25 21:36:24 +02:00
|
|
|
on_keyboard_release: None,
|
|
|
|
|
on_keyboard_press: None,
|
|
|
|
|
on_mouse_wheel: None,
|
2020-02-17 20:25:27 +01:00
|
|
|
on_dark_mode: None,
|
2023-06-04 01:44:53 +02:00
|
|
|
pointer_handler: PointerHandler::new(),
|
2023-06-14 09:43:53 +02:00
|
|
|
on_resize_scale: None,
|
2023-07-10 02:02:38 +02:00
|
|
|
on_intersect: None,
|
2023-06-23 16:21:41 +02:00
|
|
|
animation_frame_handler: AnimationFrameHandler::new(window),
|
2023-08-29 09:28:30 +02:00
|
|
|
on_touch_end: None,
|
2023-12-22 00:11:36 +01:00
|
|
|
on_context_menu: None,
|
2023-06-14 09:43:53 +02:00
|
|
|
})
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-13 09:43:14 +03:00
|
|
|
pub fn set_cursor_lock(&self, lock: bool) -> Result<(), RootOE> {
|
|
|
|
|
if lock {
|
2022-01-05 11:13:46 +01:00
|
|
|
self.raw().request_pointer_lock();
|
|
|
|
|
} else {
|
2023-07-11 00:11:06 +02:00
|
|
|
self.common.document.exit_pointer_lock();
|
2022-01-05 11:13:46 +01:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 03:15:34 +02:00
|
|
|
pub fn set_attribute(&self, attribute: &str, value: &str) {
|
2020-08-29 21:34:33 +08:00
|
|
|
self.common
|
|
|
|
|
.raw
|
2019-06-25 03:15:34 +02:00
|
|
|
.set_attribute(attribute, value)
|
2023-01-27 07:18:58 +03:00
|
|
|
.unwrap_or_else(|err| panic!("error: {err:?}\nSet attribute: {attribute}"))
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-31 14:39:33 -08:00
|
|
|
pub fn position(&self) -> LogicalPosition<f64> {
|
2020-08-29 21:34:33 +08:00
|
|
|
let bounds = self.common.raw.get_bounding_client_rect();
|
2023-07-10 23:55:43 +02:00
|
|
|
let mut position = LogicalPosition {
|
2019-12-31 14:39:33 -08:00
|
|
|
x: bounds.x(),
|
|
|
|
|
y: bounds.y(),
|
2023-07-10 23:55:43 +02:00
|
|
|
};
|
|
|
|
|
|
2023-11-10 22:46:51 +01:00
|
|
|
if self.document().contains(Some(self.raw())) && self.style().get("display") != "none" {
|
2023-07-11 00:11:06 +02:00
|
|
|
position.x += super::style_size_property(self.style(), "border-left-width")
|
|
|
|
|
+ super::style_size_property(self.style(), "padding-left");
|
|
|
|
|
position.y += super::style_size_property(self.style(), "border-top-width")
|
|
|
|
|
+ super::style_size_property(self.style(), "padding-top");
|
2019-12-31 14:39:33 -08:00
|
|
|
}
|
2023-07-10 23:55:43 +02:00
|
|
|
|
|
|
|
|
position
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-11 00:11:06 +02:00
|
|
|
#[inline]
|
2023-06-14 09:43:53 +02:00
|
|
|
pub fn old_size(&self) -> PhysicalSize<u32> {
|
|
|
|
|
self.common.old_size.get()
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 00:11:06 +02:00
|
|
|
#[inline]
|
2023-06-14 09:43:53 +02:00
|
|
|
pub fn inner_size(&self) -> PhysicalSize<u32> {
|
|
|
|
|
self.common.current_size.get()
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 00:11:06 +02:00
|
|
|
#[inline]
|
2023-06-14 09:43:53 +02:00
|
|
|
pub fn set_old_size(&self, size: PhysicalSize<u32>) {
|
|
|
|
|
self.common.old_size.set(size)
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 00:11:06 +02:00
|
|
|
#[inline]
|
2023-06-14 09:43:53 +02:00
|
|
|
pub fn set_current_size(&self, size: PhysicalSize<u32>) {
|
|
|
|
|
self.common.current_size.set(size)
|
2023-06-04 14:29:59 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-11 00:11:06 +02:00
|
|
|
#[inline]
|
2023-06-14 09:43:53 +02:00
|
|
|
pub fn window(&self) -> &web_sys::Window {
|
|
|
|
|
&self.common.window
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-11 00:11:06 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn document(&self) -> &Document {
|
|
|
|
|
&self.common.document
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-25 18:07:47 +02:00
|
|
|
pub fn raw(&self) -> &HtmlCanvasElement {
|
2020-08-29 21:34:33 +08:00
|
|
|
&self.common.raw
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-11 00:11:06 +02:00
|
|
|
#[inline]
|
2023-11-10 22:46:51 +01:00
|
|
|
pub fn style(&self) -> &Style {
|
2023-07-11 00:11:06 +02:00
|
|
|
&self.common.style
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 13:45:30 +10:00
|
|
|
pub fn on_touch_start(&mut self, prevent_default: bool) {
|
|
|
|
|
self.on_touch_start = Some(self.common.add_event("touchstart", move |event: Event| {
|
|
|
|
|
if prevent_default {
|
|
|
|
|
event.prevent_default();
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 21:01:13 +02:00
|
|
|
pub fn on_blur<F>(&mut self, mut handler: F)
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(),
|
|
|
|
|
{
|
2020-08-29 21:34:33 +08:00
|
|
|
self.on_blur = Some(self.common.add_event("blur", move |_: FocusEvent| {
|
2019-06-25 21:01:13 +02:00
|
|
|
handler();
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn on_focus<F>(&mut self, mut handler: F)
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(),
|
|
|
|
|
{
|
2020-08-29 21:34:33 +08:00
|
|
|
self.on_focus = Some(self.common.add_event("focus", move |_: FocusEvent| {
|
2019-06-25 21:01:13 +02:00
|
|
|
handler();
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 13:22:31 -02:30
|
|
|
pub fn on_keyboard_release<F>(&mut self, mut handler: F, prevent_default: bool)
|
2019-06-25 21:01:13 +02:00
|
|
|
where
|
2023-10-19 15:27:49 +01:00
|
|
|
F: 'static + FnMut(PhysicalKey, Key, Option<SmolStr>, KeyLocation, bool, ModifiersState),
|
2019-06-25 21:01:13 +02:00
|
|
|
{
|
2023-08-29 09:28:30 +02:00
|
|
|
self.on_keyboard_release =
|
|
|
|
|
Some(self.common.add_event("keyup", move |event: KeyboardEvent| {
|
2022-07-14 13:22:31 -02:30
|
|
|
if prevent_default {
|
|
|
|
|
event.prevent_default();
|
|
|
|
|
}
|
2023-05-28 20:02:59 +02:00
|
|
|
let key = event::key(&event);
|
|
|
|
|
let modifiers = event::keyboard_modifiers(&event);
|
2019-10-11 11:45:07 -04:00
|
|
|
handler(
|
2023-05-28 20:02:59 +02:00
|
|
|
event::key_code(&event),
|
|
|
|
|
key,
|
|
|
|
|
event::key_text(&event),
|
|
|
|
|
event::key_location(&event),
|
|
|
|
|
event.repeat(),
|
|
|
|
|
modifiers,
|
2019-10-11 11:45:07 -04:00
|
|
|
);
|
2023-08-29 09:28:30 +02:00
|
|
|
}));
|
2019-06-25 21:01:13 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-14 13:22:31 -02:30
|
|
|
pub fn on_keyboard_press<F>(&mut self, mut handler: F, prevent_default: bool)
|
2019-06-25 21:01:13 +02:00
|
|
|
where
|
2023-10-19 15:27:49 +01:00
|
|
|
F: 'static + FnMut(PhysicalKey, Key, Option<SmolStr>, KeyLocation, bool, ModifiersState),
|
2019-06-25 21:01:13 +02:00
|
|
|
{
|
2023-12-17 13:31:48 +01:00
|
|
|
self.on_keyboard_press = Some(self.common.add_event(
|
2020-08-29 21:34:33 +08:00
|
|
|
"keydown",
|
|
|
|
|
move |event: KeyboardEvent| {
|
2022-07-14 13:22:31 -02:30
|
|
|
if prevent_default {
|
2023-05-28 20:02:59 +02:00
|
|
|
event.prevent_default();
|
2020-10-29 22:13:21 +01:00
|
|
|
}
|
2023-05-28 20:02:59 +02:00
|
|
|
let key = event::key(&event);
|
|
|
|
|
let modifiers = event::keyboard_modifiers(&event);
|
2019-10-11 11:45:07 -04:00
|
|
|
handler(
|
2023-05-28 20:02:59 +02:00
|
|
|
event::key_code(&event),
|
|
|
|
|
key,
|
|
|
|
|
event::key_text(&event),
|
|
|
|
|
event::key_location(&event),
|
|
|
|
|
event.repeat(),
|
|
|
|
|
modifiers,
|
2019-10-11 11:45:07 -04:00
|
|
|
);
|
2020-08-29 21:34:33 +08:00
|
|
|
},
|
|
|
|
|
));
|
2019-06-25 18:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-13 15:49:27 +02:00
|
|
|
pub fn on_cursor_leave<F>(&mut self, handler: F)
|
2019-06-25 18:07:47 +02:00
|
|
|
where
|
2023-06-13 15:49:27 +02:00
|
|
|
F: 'static + FnMut(ModifiersState, Option<i32>),
|
2019-06-25 18:07:47 +02:00
|
|
|
{
|
2023-06-13 15:49:27 +02:00
|
|
|
self.pointer_handler.on_cursor_leave(&self.common, handler)
|
2019-06-25 18:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-13 15:49:27 +02:00
|
|
|
pub fn on_cursor_enter<F>(&mut self, handler: F)
|
2019-06-25 18:07:47 +02:00
|
|
|
where
|
2023-06-13 15:49:27 +02:00
|
|
|
F: 'static + FnMut(ModifiersState, Option<i32>),
|
2019-06-25 18:07:47 +02:00
|
|
|
{
|
2023-06-13 15:49:27 +02:00
|
|
|
self.pointer_handler.on_cursor_enter(&self.common, handler)
|
2019-06-25 18:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-04 02:16:55 +02:00
|
|
|
pub fn on_mouse_release<MOD, M, T>(
|
|
|
|
|
&mut self,
|
|
|
|
|
modifier_handler: MOD,
|
|
|
|
|
mouse_handler: M,
|
|
|
|
|
touch_handler: T,
|
|
|
|
|
) where
|
|
|
|
|
MOD: 'static + FnMut(ModifiersState),
|
2023-06-13 15:49:27 +02:00
|
|
|
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
|
|
|
|
|
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
|
2019-06-25 18:07:47 +02:00
|
|
|
{
|
2023-06-04 02:16:55 +02:00
|
|
|
self.pointer_handler.on_mouse_release(
|
|
|
|
|
&self.common,
|
|
|
|
|
modifier_handler,
|
|
|
|
|
mouse_handler,
|
|
|
|
|
touch_handler,
|
|
|
|
|
)
|
2019-06-25 18:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-04 02:16:55 +02:00
|
|
|
pub fn on_mouse_press<MOD, M, T>(
|
2023-06-04 01:50:30 +02:00
|
|
|
&mut self,
|
2023-06-04 02:16:55 +02:00
|
|
|
modifier_handler: MOD,
|
2023-06-04 01:50:30 +02:00
|
|
|
mouse_handler: M,
|
|
|
|
|
touch_handler: T,
|
|
|
|
|
prevent_default: bool,
|
|
|
|
|
) where
|
2023-06-04 02:16:55 +02:00
|
|
|
MOD: 'static + FnMut(ModifiersState),
|
2023-06-13 15:49:27 +02:00
|
|
|
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
|
|
|
|
|
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
|
2019-06-25 18:07:47 +02:00
|
|
|
{
|
2023-06-04 01:50:30 +02:00
|
|
|
self.pointer_handler.on_mouse_press(
|
|
|
|
|
&self.common,
|
2023-06-04 02:16:55 +02:00
|
|
|
modifier_handler,
|
2023-06-04 01:50:30 +02:00
|
|
|
mouse_handler,
|
|
|
|
|
touch_handler,
|
|
|
|
|
prevent_default,
|
|
|
|
|
)
|
2019-06-25 18:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-04 01:08:03 +02:00
|
|
|
pub fn on_cursor_move<MOD, M, T, B>(
|
2022-12-23 14:55:22 +09:00
|
|
|
&mut self,
|
2023-06-04 01:08:03 +02:00
|
|
|
modifier_handler: MOD,
|
2022-12-23 14:55:22 +09:00
|
|
|
mouse_handler: M,
|
|
|
|
|
touch_handler: T,
|
2023-06-04 01:08:03 +02:00
|
|
|
button_handler: B,
|
2022-12-23 14:55:22 +09:00
|
|
|
prevent_default: bool,
|
|
|
|
|
) where
|
2023-06-04 01:08:03 +02:00
|
|
|
MOD: 'static + FnMut(ModifiersState),
|
2023-08-28 19:18:10 +02:00
|
|
|
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
|
2023-06-13 15:49:27 +02:00
|
|
|
T: 'static
|
|
|
|
|
+ FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>),
|
|
|
|
|
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
|
2019-06-25 18:07:47 +02:00
|
|
|
{
|
2023-06-04 01:44:53 +02:00
|
|
|
self.pointer_handler.on_cursor_move(
|
|
|
|
|
&self.common,
|
|
|
|
|
modifier_handler,
|
|
|
|
|
mouse_handler,
|
|
|
|
|
touch_handler,
|
|
|
|
|
button_handler,
|
|
|
|
|
prevent_default,
|
|
|
|
|
)
|
2022-12-23 14:55:22 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn on_touch_cancel<F>(&mut self, handler: F)
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
|
|
|
|
|
{
|
2023-06-04 01:44:53 +02:00
|
|
|
self.pointer_handler.on_touch_cancel(&self.common, handler)
|
2019-06-25 18:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-14 13:22:31 -02:30
|
|
|
pub fn on_mouse_wheel<F>(&mut self, mut handler: F, prevent_default: bool)
|
2019-06-25 18:07:47 +02:00
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(i32, MouseScrollDelta, ModifiersState),
|
|
|
|
|
{
|
2023-06-05 02:44:54 +02:00
|
|
|
let window = self.common.window.clone();
|
2020-08-29 21:34:33 +08:00
|
|
|
self.on_mouse_wheel = Some(self.common.add_event("wheel", move |event: WheelEvent| {
|
2022-07-14 13:22:31 -02:30
|
|
|
if prevent_default {
|
|
|
|
|
event.prevent_default();
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-05 02:44:54 +02:00
|
|
|
if let Some(delta) = event::mouse_scroll_delta(&window, &event) {
|
2023-05-28 20:02:59 +02:00
|
|
|
let modifiers = event::mouse_modifiers(&event);
|
|
|
|
|
handler(0, delta, modifiers);
|
2019-06-25 18:07:47 +02:00
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 20:25:27 +01:00
|
|
|
pub fn on_dark_mode<F>(&mut self, mut handler: F)
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(bool),
|
|
|
|
|
{
|
2023-06-05 01:23:48 +02:00
|
|
|
self.on_dark_mode = Some(MediaQueryListHandle::new(
|
|
|
|
|
&self.common.window,
|
|
|
|
|
"(prefers-color-scheme: dark)",
|
|
|
|
|
move |mql| handler(mql.matches()),
|
|
|
|
|
));
|
2020-02-17 20:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-14 09:43:53 +02:00
|
|
|
pub(crate) fn on_resize_scale<S, R>(&mut self, scale_handler: S, size_handler: R)
|
|
|
|
|
where
|
|
|
|
|
S: 'static + FnMut(PhysicalSize<u32>, f64),
|
|
|
|
|
R: 'static + FnMut(PhysicalSize<u32>),
|
|
|
|
|
{
|
|
|
|
|
self.on_resize_scale = Some(ResizeScaleHandle::new(
|
|
|
|
|
self.window().clone(),
|
2023-07-11 00:11:06 +02:00
|
|
|
self.document().clone(),
|
2023-06-14 09:43:53 +02:00
|
|
|
self.raw().clone(),
|
2023-07-11 00:11:06 +02:00
|
|
|
self.style().clone(),
|
2023-06-14 09:43:53 +02:00
|
|
|
scale_handler,
|
|
|
|
|
size_handler,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 02:02:38 +02:00
|
|
|
pub(crate) fn on_intersection<F>(&mut self, handler: F)
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(bool),
|
|
|
|
|
{
|
|
|
|
|
self.on_intersect = Some(IntersectionObserverHandle::new(self.raw(), handler));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 16:21:41 +02:00
|
|
|
pub(crate) fn on_animation_frame<F>(&mut self, f: F)
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(),
|
|
|
|
|
{
|
|
|
|
|
self.animation_frame_handler.on_animation_frame(f)
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-22 00:11:36 +01:00
|
|
|
pub(crate) fn on_context_menu(&mut self, prevent_default: bool) {
|
|
|
|
|
self.on_context_menu = Some(self.common.add_event(
|
|
|
|
|
"contextmenu",
|
|
|
|
|
move |event: PointerEvent| {
|
|
|
|
|
if prevent_default {
|
|
|
|
|
event.prevent_default();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 21:34:33 +08:00
|
|
|
pub fn request_fullscreen(&self) {
|
2023-12-17 13:31:48 +01:00
|
|
|
fullscreen::request_fullscreen(self.document(), self.raw());
|
2023-08-29 09:28:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn exit_fullscreen(&self) {
|
2023-12-17 13:31:48 +01:00
|
|
|
fullscreen::exit_fullscreen(self.document(), self.raw());
|
2020-08-29 21:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_fullscreen(&self) -> bool {
|
2023-12-17 13:31:48 +01:00
|
|
|
fullscreen::is_fullscreen(self.document(), self.raw())
|
2020-08-29 21:34:33 +08:00
|
|
|
}
|
2020-09-21 06:42:07 +08:00
|
|
|
|
2023-06-23 16:21:41 +02:00
|
|
|
pub fn request_animation_frame(&self) {
|
|
|
|
|
self.animation_frame_handler.request();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-03 02:26:53 +02:00
|
|
|
pub(crate) fn handle_scale_change(
|
2023-06-14 09:43:53 +02:00
|
|
|
&self,
|
2023-09-03 02:26:53 +02:00
|
|
|
runner: &super::super::event_loop::runner::Shared,
|
|
|
|
|
event_handler: impl FnOnce(crate::event::Event<()>),
|
2023-06-14 09:43:53 +02:00
|
|
|
current_size: PhysicalSize<u32>,
|
|
|
|
|
scale: f64,
|
|
|
|
|
) {
|
|
|
|
|
// First, we send the `ScaleFactorChanged` event:
|
|
|
|
|
self.set_current_size(current_size);
|
2023-07-31 00:39:01 +04:00
|
|
|
let new_size = {
|
|
|
|
|
let new_size = Arc::new(Mutex::new(current_size));
|
|
|
|
|
event_handler(crate::event::Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(self.id),
|
|
|
|
|
event: crate::event::WindowEvent::ScaleFactorChanged {
|
|
|
|
|
scale_factor: scale,
|
|
|
|
|
inner_size_writer: InnerSizeWriter::new(Arc::downgrade(&new_size)),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let new_size = *new_size.lock().unwrap();
|
|
|
|
|
new_size
|
|
|
|
|
};
|
2023-06-14 09:43:53 +02:00
|
|
|
|
|
|
|
|
if current_size != new_size {
|
|
|
|
|
// Then we resize the canvas to the new size, a new
|
|
|
|
|
// `Resized` event will be sent by the `ResizeObserver`:
|
|
|
|
|
let new_size = new_size.to_logical(scale);
|
2023-07-11 00:11:06 +02:00
|
|
|
super::set_canvas_size(self.document(), self.raw(), self.style(), new_size);
|
2023-06-14 09:43:53 +02:00
|
|
|
|
|
|
|
|
// Set the size might not trigger the event because the calculation is inaccurate.
|
|
|
|
|
self.on_resize_scale
|
|
|
|
|
.as_ref()
|
|
|
|
|
.expect("expected Window to still be active")
|
|
|
|
|
.notify_resize();
|
|
|
|
|
} else if self.old_size() != new_size {
|
|
|
|
|
// Then we at least send a resized event.
|
|
|
|
|
self.set_old_size(new_size);
|
|
|
|
|
runner.send_event(crate::event::Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(self.id),
|
|
|
|
|
event: crate::event::WindowEvent::Resized(new_size),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 06:42:07 +08:00
|
|
|
pub fn remove_listeners(&mut self) {
|
2023-08-29 09:28:30 +02:00
|
|
|
self.on_touch_start = None;
|
2020-09-21 06:42:07 +08:00
|
|
|
self.on_focus = None;
|
|
|
|
|
self.on_blur = None;
|
|
|
|
|
self.on_keyboard_release = None;
|
|
|
|
|
self.on_keyboard_press = None;
|
|
|
|
|
self.on_mouse_wheel = None;
|
|
|
|
|
self.on_dark_mode = None;
|
2023-06-14 09:43:53 +02:00
|
|
|
self.pointer_handler.remove_listeners();
|
|
|
|
|
self.on_resize_scale = None;
|
2023-07-10 02:02:38 +02:00
|
|
|
self.on_intersect = None;
|
2023-08-29 09:28:30 +02:00
|
|
|
self.animation_frame_handler.cancel();
|
|
|
|
|
self.on_touch_end = None;
|
2023-12-22 00:11:36 +01:00
|
|
|
self.on_context_menu = None;
|
2020-09-21 06:42:07 +08:00
|
|
|
}
|
2020-08-29 21:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Common {
|
2023-06-04 01:44:53 +02:00
|
|
|
pub fn add_event<E, F>(
|
2020-09-21 06:42:07 +08:00
|
|
|
&self,
|
|
|
|
|
event_name: &'static str,
|
2023-08-28 19:18:10 +02:00
|
|
|
handler: F,
|
2020-09-21 06:42:07 +08:00
|
|
|
) -> EventListenerHandle<dyn FnMut(E)>
|
2019-06-25 18:07:47 +02:00
|
|
|
where
|
|
|
|
|
E: 'static + AsRef<web_sys::Event> + wasm_bindgen::convert::FromWasmAbi,
|
|
|
|
|
F: 'static + FnMut(E),
|
|
|
|
|
{
|
2023-12-22 22:33:50 +01:00
|
|
|
EventListenerHandle::new(self.raw.deref().clone(), event_name, Closure::new(handler))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn raw(&self) -> &HtmlCanvasElement {
|
|
|
|
|
&self.raw
|
2019-06-25 18:07:47 +02:00
|
|
|
}
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
2023-11-10 22:46:51 +01:00
|
|
|
|
|
|
|
|
impl Style {
|
|
|
|
|
fn new(window: &web_sys::Window, canvas: &HtmlCanvasElement) -> Self {
|
|
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
|
let read = window
|
|
|
|
|
.get_computed_style(canvas)
|
|
|
|
|
.expect("Failed to obtain computed style")
|
|
|
|
|
// this can't fail: we aren't using a pseudo-element
|
|
|
|
|
.expect("Invalid pseudo-element");
|
|
|
|
|
|
|
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
|
let write = canvas.style();
|
|
|
|
|
|
|
|
|
|
Self { read, write }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn get(&self, property: &str) -> String {
|
|
|
|
|
self.read
|
|
|
|
|
.get_property_value(property)
|
|
|
|
|
.expect("Invalid property")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn remove(&self, property: &str) {
|
|
|
|
|
self.write
|
|
|
|
|
.remove_property(property)
|
|
|
|
|
.expect("Property is read only");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn set(&self, property: &str, value: &str) {
|
|
|
|
|
self.write
|
|
|
|
|
.set_property(property, value)
|
|
|
|
|
.expect("Property is read only");
|
|
|
|
|
}
|
|
|
|
|
}
|