2023-12-22 23:49:25 +01:00
|
|
|
use std::cell::OnceCell;
|
|
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
use js_sys::Promise;
|
|
|
|
|
use wasm_bindgen::closure::Closure;
|
2023-07-11 00:34:02 +02:00
|
|
|
use wasm_bindgen::prelude::wasm_bindgen;
|
|
|
|
|
use wasm_bindgen::{JsCast, JsValue};
|
|
|
|
|
use web_sys::{Document, Element, HtmlCanvasElement};
|
|
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
pub fn request_fullscreen(document: &Document, canvas: &HtmlCanvasElement) {
|
|
|
|
|
if is_fullscreen(document, canvas) {
|
|
|
|
|
return;
|
2023-08-29 09:28:30 +02:00
|
|
|
}
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
#[wasm_bindgen]
|
|
|
|
|
extern "C" {
|
|
|
|
|
#[wasm_bindgen(extends = HtmlCanvasElement)]
|
|
|
|
|
type RequestFullscreen;
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
#[wasm_bindgen(method, js_name = requestFullscreen)]
|
|
|
|
|
fn request_fullscreen(this: &RequestFullscreen) -> Promise;
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
#[wasm_bindgen(method, js_name = webkitRequestFullscreen)]
|
|
|
|
|
fn webkit_request_fullscreen(this: &RequestFullscreen);
|
2023-07-11 00:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
let canvas: &RequestFullscreen = canvas.unchecked_ref();
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
if has_fullscreen_api_support(canvas) {
|
|
|
|
|
thread_local! {
|
|
|
|
|
static REJECT_HANDLER: Closure<dyn FnMut(JsValue)> = Closure::new(|_| ());
|
2023-08-29 09:28:30 +02:00
|
|
|
}
|
2023-12-17 13:31:48 +01:00
|
|
|
REJECT_HANDLER.with(|handler| {
|
|
|
|
|
let _ = canvas.request_fullscreen().catch(handler);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
canvas.webkit_request_fullscreen();
|
2023-07-11 00:34:02 +02:00
|
|
|
}
|
2023-12-17 13:31:48 +01:00
|
|
|
}
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
pub fn is_fullscreen(document: &Document, canvas: &HtmlCanvasElement) -> bool {
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
extern "C" {
|
|
|
|
|
type FullscreenElement;
|
2023-08-29 09:28:30 +02:00
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
#[wasm_bindgen(method, getter, js_name = webkitFullscreenElement)]
|
|
|
|
|
fn webkit_fullscreen_element(this: &FullscreenElement) -> Option<Element>;
|
2023-08-29 09:28:30 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
let element = if has_fullscreen_api_support(canvas) {
|
|
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
|
document.fullscreen_element()
|
|
|
|
|
} else {
|
|
|
|
|
let document: &FullscreenElement = document.unchecked_ref();
|
|
|
|
|
document.webkit_fullscreen_element()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match element {
|
|
|
|
|
Some(element) => {
|
|
|
|
|
let canvas: &Element = canvas;
|
|
|
|
|
canvas == &element
|
2023-08-29 09:28:30 +02:00
|
|
|
}
|
2023-12-17 13:31:48 +01:00
|
|
|
None => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-29 09:28:30 +02:00
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
pub fn exit_fullscreen(document: &Document, canvas: &HtmlCanvasElement) {
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
extern "C" {
|
|
|
|
|
type ExitFullscreen;
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
#[wasm_bindgen(method, js_name = webkitExitFullscreen)]
|
|
|
|
|
fn webkit_exit_fullscreen(this: &ExitFullscreen);
|
2023-07-11 00:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-17 13:31:48 +01:00
|
|
|
if has_fullscreen_api_support(canvas) {
|
|
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
|
document.exit_fullscreen()
|
|
|
|
|
} else {
|
|
|
|
|
let document: &ExitFullscreen = document.unchecked_ref();
|
|
|
|
|
document.webkit_exit_fullscreen()
|
2023-07-11 00:34:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
fn has_fullscreen_api_support(canvas: &HtmlCanvasElement) -> bool {
|
2023-12-17 13:31:48 +01:00
|
|
|
thread_local! {
|
|
|
|
|
static FULLSCREEN_API_SUPPORT: OnceCell<bool> = OnceCell::new();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
FULLSCREEN_API_SUPPORT.with(|support| {
|
|
|
|
|
*support.get_or_init(|| {
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
extern "C" {
|
|
|
|
|
type CanvasFullScreenApiSupport;
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
#[wasm_bindgen(method, getter, js_name = requestFullscreen)]
|
|
|
|
|
fn has_request_fullscreen(this: &CanvasFullScreenApiSupport) -> JsValue;
|
|
|
|
|
}
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
let support: &CanvasFullScreenApiSupport = canvas.unchecked_ref();
|
|
|
|
|
!support.has_request_fullscreen().is_undefined()
|
|
|
|
|
})
|
|
|
|
|
})
|
2023-07-11 00:34:02 +02:00
|
|
|
}
|