On Web, implement Send and Sync where appropriate (#2834)
This commit is contained in:
parent
eb2d3894ef
commit
8f7f3efc0d
21 changed files with 609 additions and 196 deletions
|
|
@ -15,14 +15,13 @@ use crate::dpi::{LogicalSize, Size};
|
|||
use crate::platform::web::WindowExtWebSys;
|
||||
use crate::window::Window;
|
||||
use wasm_bindgen::closure::Closure;
|
||||
use web_sys::{window, BeforeUnloadEvent, Element, HtmlCanvasElement};
|
||||
use web_sys::{BeforeUnloadEvent, Element, HtmlCanvasElement};
|
||||
|
||||
pub fn throw(msg: &str) {
|
||||
wasm_bindgen::throw_str(msg);
|
||||
}
|
||||
|
||||
pub fn exit_fullscreen() {
|
||||
let window = web_sys::window().expect("Failed to obtain window");
|
||||
pub fn exit_fullscreen(window: &web_sys::Window) {
|
||||
let document = window.document().expect("Failed to obtain document");
|
||||
|
||||
document.exit_fullscreen();
|
||||
|
|
@ -32,38 +31,33 @@ pub struct UnloadEventHandle {
|
|||
_listener: event_handle::EventListenerHandle<dyn FnMut(BeforeUnloadEvent)>,
|
||||
}
|
||||
|
||||
pub fn on_unload(mut handler: impl FnMut() + 'static) -> UnloadEventHandle {
|
||||
let window = web_sys::window().expect("Failed to obtain window");
|
||||
|
||||
pub fn on_unload(
|
||||
window: &web_sys::Window,
|
||||
mut handler: impl FnMut() + 'static,
|
||||
) -> UnloadEventHandle {
|
||||
let closure = Closure::wrap(
|
||||
Box::new(move |_: BeforeUnloadEvent| handler()) as Box<dyn FnMut(BeforeUnloadEvent)>
|
||||
);
|
||||
|
||||
let listener = event_handle::EventListenerHandle::new(&window, "beforeunload", closure);
|
||||
let listener = event_handle::EventListenerHandle::new(window, "beforeunload", closure);
|
||||
UnloadEventHandle {
|
||||
_listener: listener,
|
||||
}
|
||||
}
|
||||
|
||||
impl WindowExtWebSys for Window {
|
||||
fn canvas(&self) -> HtmlCanvasElement {
|
||||
self.window.canvas().raw().clone()
|
||||
fn canvas(&self) -> Option<HtmlCanvasElement> {
|
||||
self.window.canvas()
|
||||
}
|
||||
|
||||
fn is_dark_mode(&self) -> bool {
|
||||
let window = web_sys::window().expect("Failed to obtain window");
|
||||
|
||||
window
|
||||
.match_media("(prefers-color-scheme: dark)")
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|media| media.matches())
|
||||
.unwrap_or(false)
|
||||
self.window
|
||||
.inner
|
||||
.queue(|inner| is_dark_mode(&inner.window).unwrap_or(false))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_size() -> LogicalSize<f64> {
|
||||
let window = web_sys::window().expect("Failed to obtain window");
|
||||
pub fn window_size(window: &web_sys::Window) -> LogicalSize<f64> {
|
||||
let width = window
|
||||
.inner_width()
|
||||
.expect("Failed to get width")
|
||||
|
|
@ -78,13 +72,12 @@ pub fn window_size() -> LogicalSize<f64> {
|
|||
LogicalSize { width, height }
|
||||
}
|
||||
|
||||
pub fn scale_factor() -> f64 {
|
||||
let window = web_sys::window().expect("Failed to obtain window");
|
||||
pub fn scale_factor(window: &web_sys::Window) -> f64 {
|
||||
window.device_pixel_ratio()
|
||||
}
|
||||
|
||||
pub fn set_canvas_size(raw: &HtmlCanvasElement, size: Size) {
|
||||
let scale_factor = scale_factor();
|
||||
pub fn set_canvas_size(window: &web_sys::Window, raw: &HtmlCanvasElement, size: Size) {
|
||||
let scale_factor = scale_factor(window);
|
||||
let logical_size = size.to_logical::<f64>(scale_factor);
|
||||
|
||||
set_canvas_style_property(raw, "width", &format!("{}px", logical_size.width));
|
||||
|
|
@ -98,8 +91,7 @@ pub fn set_canvas_style_property(raw: &HtmlCanvasElement, property: &str, value:
|
|||
.unwrap_or_else(|err| panic!("error: {err:?}\nFailed to set {property}"))
|
||||
}
|
||||
|
||||
pub fn is_fullscreen(canvas: &HtmlCanvasElement) -> bool {
|
||||
let window = window().expect("Failed to obtain window");
|
||||
pub fn is_fullscreen(window: &web_sys::Window, canvas: &HtmlCanvasElement) -> bool {
|
||||
let document = window.document().expect("Failed to obtain document");
|
||||
|
||||
match document.fullscreen_element() {
|
||||
|
|
@ -111,4 +103,12 @@ pub fn is_fullscreen(canvas: &HtmlCanvasElement) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn is_dark_mode(window: &web_sys::Window) -> Option<bool> {
|
||||
window
|
||||
.match_media("(prefers-color-scheme: dark)")
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|media| media.matches())
|
||||
}
|
||||
|
||||
pub type RawCanvasType = HtmlCanvasElement;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue