2023-08-29 09:28:30 +02:00
|
|
|
use std::cell::Cell;
|
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
|
|
use js_sys::Promise;
|
2023-10-08 02:00:51 +02:00
|
|
|
use once_cell::unsync::OnceCell;
|
2023-08-29 09:28:30 +02:00
|
|
|
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-08-29 09:28:30 +02:00
|
|
|
use super::EventListenerHandle;
|
|
|
|
|
|
2023-07-11 00:34:02 +02:00
|
|
|
thread_local! {
|
|
|
|
|
static FULLSCREEN_API_SUPPORT: OnceCell<bool> = OnceCell::new();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
pub struct FullscreenHandler {
|
|
|
|
|
document: Document,
|
|
|
|
|
canvas: HtmlCanvasElement,
|
|
|
|
|
fullscreen_requested: Rc<Cell<bool>>,
|
|
|
|
|
_fullscreen_change: EventListenerHandle<dyn FnMut()>,
|
2023-07-11 00:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
impl FullscreenHandler {
|
|
|
|
|
pub fn new(document: Document, canvas: HtmlCanvasElement) -> Self {
|
|
|
|
|
let fullscreen_requested = Rc::new(Cell::new(false));
|
|
|
|
|
let fullscreen_change = EventListenerHandle::new(
|
|
|
|
|
canvas.clone(),
|
|
|
|
|
if has_fullscreen_api_support(&canvas) {
|
|
|
|
|
"fullscreenchange"
|
|
|
|
|
} else {
|
|
|
|
|
"webkitfullscreenchange"
|
|
|
|
|
},
|
|
|
|
|
Closure::new({
|
|
|
|
|
let fullscreen_requested = fullscreen_requested.clone();
|
|
|
|
|
move || {
|
|
|
|
|
// It doesn't matter if the canvas entered or exitted fullscreen mode,
|
|
|
|
|
// we don't want to request it again later.
|
|
|
|
|
fullscreen_requested.set(false);
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
document,
|
|
|
|
|
canvas,
|
|
|
|
|
fullscreen_requested,
|
|
|
|
|
_fullscreen_change: fullscreen_change,
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
fn internal_request_fullscreen(&self) {
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
extern "C" {
|
|
|
|
|
type RequestFullscreen;
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
#[wasm_bindgen(method, js_name = requestFullscreen)]
|
|
|
|
|
fn request_fullscreen(this: &RequestFullscreen) -> Promise;
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
#[wasm_bindgen(method, js_name = webkitRequestFullscreen)]
|
|
|
|
|
fn webkit_request_fullscreen(this: &RequestFullscreen);
|
|
|
|
|
}
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
let canvas: &RequestFullscreen = self.canvas.unchecked_ref();
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
if has_fullscreen_api_support(&self.canvas) {
|
|
|
|
|
thread_local! {
|
2023-10-08 02:00:51 +02:00
|
|
|
static REJECT_HANDLER: Closure<dyn FnMut(JsValue)> = Closure::new(|_| ());
|
2023-08-29 09:28:30 +02: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-08-29 09:28:30 +02:00
|
|
|
pub fn request_fullscreen(&self) {
|
|
|
|
|
if !self.is_fullscreen() {
|
|
|
|
|
self.internal_request_fullscreen();
|
|
|
|
|
self.fullscreen_requested.set(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
pub fn transient_activation(&self) {
|
|
|
|
|
if self.fullscreen_requested.get() {
|
|
|
|
|
self.internal_request_fullscreen()
|
|
|
|
|
}
|
2023-07-11 00:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
pub fn is_fullscreen(&self) -> bool {
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
extern "C" {
|
|
|
|
|
type FullscreenElement;
|
|
|
|
|
|
|
|
|
|
#[wasm_bindgen(method, getter, js_name = webkitFullscreenElement)]
|
|
|
|
|
fn webkit_fullscreen_element(this: &FullscreenElement) -> Option<Element>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let element = if has_fullscreen_api_support(&self.canvas) {
|
|
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
|
self.document.fullscreen_element()
|
|
|
|
|
} else {
|
|
|
|
|
let document: &FullscreenElement = self.document.unchecked_ref();
|
|
|
|
|
document.webkit_fullscreen_element()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match element {
|
|
|
|
|
Some(element) => {
|
|
|
|
|
let canvas: &Element = &self.canvas;
|
|
|
|
|
canvas == &element
|
|
|
|
|
}
|
|
|
|
|
None => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn exit_fullscreen(&self) {
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
extern "C" {
|
|
|
|
|
type ExitFullscreen;
|
|
|
|
|
|
|
|
|
|
#[wasm_bindgen(method, js_name = webkitExitFullscreen)]
|
|
|
|
|
fn webkit_exit_fullscreen(this: &ExitFullscreen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if has_fullscreen_api_support(&self.canvas) {
|
|
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
|
self.document.exit_fullscreen()
|
|
|
|
|
} else {
|
|
|
|
|
let document: &ExitFullscreen = self.document.unchecked_ref();
|
|
|
|
|
document.webkit_exit_fullscreen()
|
|
|
|
|
}
|
2023-07-11 00:34:02 +02:00
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
self.fullscreen_requested.set(false);
|
2023-07-11 00:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
pub fn cancel(&self) {
|
|
|
|
|
self.fullscreen_requested.set(false);
|
2023-07-11 00:34:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 09:28:30 +02:00
|
|
|
fn has_fullscreen_api_support(canvas: &HtmlCanvasElement) -> bool {
|
|
|
|
|
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
|
|
|
}
|