Add web fullscreen support (#1142)
Adds fullscreen using native web APIs to the stdweb and web-sys backends. Due to limitations of browser APIs, requests for fullscreen can only be fulfilled during a short-lived user-triggered event. This commit does automatically handle that under the hood, but it does introduce unavoidable latency in full-screening the canvas.
This commit is contained in:
parent
bedb889693
commit
3ff4834bd5
7 changed files with 242 additions and 48 deletions
|
|
@ -5,15 +5,23 @@ mod timeout;
|
|||
pub use self::canvas::Canvas;
|
||||
pub use self::timeout::Timeout;
|
||||
|
||||
use crate::dpi::LogicalSize;
|
||||
use crate::platform::web::WindowExtWebSys;
|
||||
use crate::window::Window;
|
||||
use wasm_bindgen::{closure::Closure, JsCast};
|
||||
use web_sys::{BeforeUnloadEvent, HtmlCanvasElement};
|
||||
use web_sys::{window, 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");
|
||||
let document = window.document().expect("Failed to obtain document");
|
||||
|
||||
document.exit_fullscreen();
|
||||
}
|
||||
|
||||
pub fn on_unload(mut handler: impl FnMut() + 'static) {
|
||||
let window = web_sys::window().expect("Failed to obtain window");
|
||||
|
||||
|
|
@ -31,3 +39,32 @@ impl WindowExtWebSys for Window {
|
|||
self.window.canvas().raw().clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_size() -> LogicalSize {
|
||||
let window = web_sys::window().expect("Failed to obtain window");
|
||||
let width = window
|
||||
.inner_width()
|
||||
.expect("Failed to get width")
|
||||
.as_f64()
|
||||
.expect("Failed to get width as f64");
|
||||
let height = window
|
||||
.inner_height()
|
||||
.expect("Failed to get height")
|
||||
.as_f64()
|
||||
.expect("Failed to get height as f64");
|
||||
|
||||
LogicalSize { width, height }
|
||||
}
|
||||
|
||||
pub fn is_fullscreen(canvas: &HtmlCanvasElement) -> bool {
|
||||
let window = window().expect("Failed to obtain window");
|
||||
let document = window.document().expect("Failed to obtain document");
|
||||
|
||||
match document.fullscreen_element() {
|
||||
Some(elem) => {
|
||||
let raw: Element = canvas.clone().into();
|
||||
raw == elem
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue