2019-06-25 03:15:34 +02:00
|
|
|
mod canvas;
|
2023-06-14 10:26:26 +02:00
|
|
|
pub mod event;
|
2020-09-21 06:42:07 +08:00
|
|
|
mod event_handle;
|
2023-07-10 02:02:38 +02:00
|
|
|
mod intersection_handle;
|
2020-09-21 06:42:07 +08:00
|
|
|
mod media_query_handle;
|
2023-06-04 01:44:53 +02:00
|
|
|
mod pointer;
|
2023-06-14 09:43:53 +02:00
|
|
|
mod resize_scaling;
|
2019-06-25 03:15:34 +02:00
|
|
|
mod timeout;
|
|
|
|
|
|
|
|
|
|
pub use self::canvas::Canvas;
|
2023-06-02 11:37:23 +02:00
|
|
|
pub use self::event::ButtonsState;
|
2023-06-14 10:26:26 +02:00
|
|
|
pub use self::event_handle::EventListenerHandle;
|
2023-06-14 09:43:53 +02:00
|
|
|
pub use self::resize_scaling::ResizeScaleHandle;
|
2023-06-13 16:39:06 +02:00
|
|
|
pub use self::timeout::{IdleCallback, Timeout};
|
2019-06-25 03:15:34 +02:00
|
|
|
|
2023-06-14 09:43:53 +02:00
|
|
|
use crate::dpi::LogicalSize;
|
2019-06-25 18:07:47 +02:00
|
|
|
use crate::platform::web::WindowExtWebSys;
|
|
|
|
|
use crate::window::Window;
|
2020-09-21 06:42:07 +08:00
|
|
|
use wasm_bindgen::closure::Closure;
|
2023-07-10 02:02:38 +02:00
|
|
|
use web_sys::{
|
2023-07-11 00:11:06 +02:00
|
|
|
CssStyleDeclaration, Document, Element, HtmlCanvasElement, PageTransitionEvent, VisibilityState,
|
2023-07-10 02:02:38 +02:00
|
|
|
};
|
2019-06-25 18:07:47 +02:00
|
|
|
|
2019-06-25 03:15:34 +02:00
|
|
|
pub fn throw(msg: &str) {
|
|
|
|
|
wasm_bindgen::throw_str(msg);
|
|
|
|
|
}
|
2019-06-25 18:07:47 +02:00
|
|
|
|
2023-07-11 00:11:06 +02:00
|
|
|
pub fn exit_fullscreen(document: &Document) {
|
2019-10-11 11:45:07 -04:00
|
|
|
document.exit_fullscreen();
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-28 12:54:21 +02:00
|
|
|
pub struct PageTransitionEventHandle {
|
|
|
|
|
_show_listener: event_handle::EventListenerHandle<dyn FnMut(PageTransitionEvent)>,
|
|
|
|
|
_hide_listener: event_handle::EventListenerHandle<dyn FnMut(PageTransitionEvent)>,
|
2020-09-21 06:42:07 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-28 12:54:21 +02:00
|
|
|
pub fn on_page_transition(
|
|
|
|
|
window: &web_sys::Window,
|
|
|
|
|
show_handler: impl FnMut(PageTransitionEvent) + 'static,
|
|
|
|
|
hide_handler: impl FnMut(PageTransitionEvent) + 'static,
|
|
|
|
|
) -> PageTransitionEventHandle {
|
|
|
|
|
let show_closure = Closure::new(show_handler);
|
|
|
|
|
let hide_closure = Closure::new(hide_handler);
|
2019-09-11 11:47:03 -04:00
|
|
|
|
2023-06-28 12:54:21 +02:00
|
|
|
let show_listener = event_handle::EventListenerHandle::new(window, "pageshow", show_closure);
|
|
|
|
|
let hide_listener = event_handle::EventListenerHandle::new(window, "pagehide", hide_closure);
|
|
|
|
|
PageTransitionEventHandle {
|
|
|
|
|
_show_listener: show_listener,
|
|
|
|
|
_hide_listener: hide_listener,
|
2020-09-21 06:42:07 +08:00
|
|
|
}
|
2019-09-11 11:47:03 -04:00
|
|
|
}
|
|
|
|
|
|
2019-06-25 18:07:47 +02:00
|
|
|
impl WindowExtWebSys for Window {
|
2023-06-05 02:44:54 +02:00
|
|
|
fn canvas(&self) -> Option<HtmlCanvasElement> {
|
|
|
|
|
self.window.canvas()
|
2019-06-25 18:07:47 +02:00
|
|
|
}
|
2020-02-17 20:25:27 +01:00
|
|
|
|
|
|
|
|
fn is_dark_mode(&self) -> bool {
|
2023-06-05 02:44:54 +02:00
|
|
|
self.window
|
|
|
|
|
.inner
|
|
|
|
|
.queue(|inner| is_dark_mode(&inner.window).unwrap_or(false))
|
2020-02-17 20:25:27 +01:00
|
|
|
}
|
2019-06-25 18:07:47 +02:00
|
|
|
}
|
2019-10-11 11:45:07 -04:00
|
|
|
|
2023-06-05 02:44:54 +02:00
|
|
|
pub fn scale_factor(window: &web_sys::Window) -> f64 {
|
2019-12-31 14:39:33 -08:00
|
|
|
window.device_pixel_ratio()
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-14 09:43:53 +02:00
|
|
|
pub fn set_canvas_size(
|
2023-07-11 00:11:06 +02:00
|
|
|
document: &Document,
|
2023-06-14 09:43:53 +02:00
|
|
|
raw: &HtmlCanvasElement,
|
2023-07-11 00:11:06 +02:00
|
|
|
style: &CssStyleDeclaration,
|
2023-06-14 09:43:53 +02:00
|
|
|
mut new_size: LogicalSize<f64>,
|
|
|
|
|
) {
|
2023-07-10 23:55:43 +02:00
|
|
|
if !document.contains(Some(raw)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if style.get_property_value("display").unwrap() == "none" {
|
2023-06-14 09:43:53 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if style.get_property_value("box-sizing").unwrap() == "border-box" {
|
2023-07-11 00:11:06 +02:00
|
|
|
new_size.width += style_size_property(style, "border-left-width")
|
|
|
|
|
+ style_size_property(style, "border-right-width")
|
|
|
|
|
+ style_size_property(style, "padding-left")
|
|
|
|
|
+ style_size_property(style, "padding-right");
|
|
|
|
|
new_size.height += style_size_property(style, "border-top-width")
|
|
|
|
|
+ style_size_property(style, "border-bottom-width")
|
|
|
|
|
+ style_size_property(style, "padding-top")
|
|
|
|
|
+ style_size_property(style, "padding-bottom");
|
2023-06-14 09:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set_canvas_style_property(raw, "width", &format!("{}px", new_size.width));
|
|
|
|
|
set_canvas_style_property(raw, "height", &format!("{}px", new_size.height));
|
|
|
|
|
}
|
2023-06-08 08:40:26 +02:00
|
|
|
|
2023-06-14 09:43:53 +02:00
|
|
|
/// This function will panic if the element is not inserted in the DOM
|
|
|
|
|
/// or is not a CSS property that represents a size in pixel.
|
|
|
|
|
pub fn style_size_property(style: &CssStyleDeclaration, property: &str) -> f64 {
|
|
|
|
|
let prop = style
|
|
|
|
|
.get_property_value(property)
|
|
|
|
|
.expect("Found invalid property");
|
|
|
|
|
prop.strip_suffix("px")
|
|
|
|
|
.expect("Element was not inserted into the DOM or is not a size in pixel")
|
|
|
|
|
.parse()
|
|
|
|
|
.expect("CSS property is not a size in pixel")
|
2020-08-17 16:48:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_canvas_style_property(raw: &HtmlCanvasElement, property: &str, value: &str) {
|
2019-12-31 14:39:33 -08:00
|
|
|
let style = raw.style();
|
|
|
|
|
style
|
2020-08-17 16:48:29 -07:00
|
|
|
.set_property(property, value)
|
2023-01-27 07:18:58 +03:00
|
|
|
.unwrap_or_else(|err| panic!("error: {err:?}\nFailed to set {property}"))
|
2019-12-31 14:39:33 -08:00
|
|
|
}
|
|
|
|
|
|
2023-07-11 00:11:06 +02:00
|
|
|
pub fn is_fullscreen(document: &Document, canvas: &HtmlCanvasElement) -> bool {
|
2019-10-11 11:45:07 -04:00
|
|
|
match document.fullscreen_element() {
|
|
|
|
|
Some(elem) => {
|
2023-07-08 17:05:05 +02:00
|
|
|
let canvas: &Element = canvas;
|
|
|
|
|
canvas == &elem
|
2019-10-11 11:45:07 -04:00
|
|
|
}
|
|
|
|
|
None => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-15 21:20:14 -05:00
|
|
|
|
2023-06-05 02:44:54 +02:00
|
|
|
pub fn is_dark_mode(window: &web_sys::Window) -> Option<bool> {
|
|
|
|
|
window
|
|
|
|
|
.match_media("(prefers-color-scheme: dark)")
|
|
|
|
|
.ok()
|
|
|
|
|
.flatten()
|
|
|
|
|
.map(|media| media.matches())
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 00:11:06 +02:00
|
|
|
pub fn is_visible(document: &Document) -> bool {
|
2023-07-10 02:02:38 +02:00
|
|
|
document.visibility_state() == VisibilityState::Visible
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 21:20:14 -05:00
|
|
|
pub type RawCanvasType = HtmlCanvasElement;
|