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:
Ryan G 2019-10-11 11:45:07 -04:00 committed by GitHub
parent bedb889693
commit 3ff4834bd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 242 additions and 48 deletions

View file

@ -5,19 +5,24 @@ mod timeout;
pub use self::canvas::Canvas;
pub use self::timeout::Timeout;
use crate::dpi::LogicalSize;
use crate::platform::web::WindowExtStdweb;
use crate::window::Window;
use stdweb::js;
use stdweb::web::event::BeforeUnloadEvent;
use stdweb::web::html_element::CanvasElement;
use stdweb::web::window;
use stdweb::web::IEventTarget;
use stdweb::web::{document, html_element::CanvasElement, Element};
pub fn throw(msg: &str) {
js! { throw @{msg} }
}
pub fn exit_fullscreen() {
document().exit_fullscreen();
}
pub fn on_unload(mut handler: impl FnMut() + 'static) {
window().add_event_listener(move |_: BeforeUnloadEvent| handler());
}
@ -27,3 +32,21 @@ impl WindowExtStdweb for Window {
self.window.canvas().raw().clone()
}
}
pub fn window_size() -> LogicalSize {
let window = window();
let width = window.inner_width() as f64;
let height = window.inner_height() as f64;
LogicalSize { width, height }
}
pub fn is_fullscreen(canvas: &CanvasElement) -> bool {
match document().fullscreen_element() {
Some(elem) => {
let raw: Element = canvas.clone().into();
raw == elem
}
None => false,
}
}