mirror of
https://github.com/storytold/winit.git
synced 2026-11-23 12:50:47 +01:00
* Rename hidpi_factor to scale_factor * Deprecate WINIT_HIDPI_FACTOR environment variable in favor of WINIT_X11_SCALE_FACTOR * Rename HiDpiFactorChanged to DpiChanged and update docs I'm renaming it to DpiChanged instead of ScaleFactorChanged, since I'd like Winit to expose the raw DPI value at some point in the near future, and DpiChanged is a more apt name for that purpose. * Format * Fix macos and ios again * Fix bad macos rebase
94 lines
2.6 KiB
Rust
94 lines
2.6 KiB
Rust
mod canvas;
|
|
mod event;
|
|
mod timeout;
|
|
|
|
pub use self::canvas::Canvas;
|
|
pub use self::timeout::Timeout;
|
|
|
|
use crate::dpi::{LogicalSize, Size};
|
|
use crate::platform::web::WindowExtWebSys;
|
|
use crate::window::Window;
|
|
use wasm_bindgen::{closure::Closure, JsCast};
|
|
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");
|
|
|
|
let closure = Closure::wrap(
|
|
Box::new(move |_: BeforeUnloadEvent| handler()) as Box<dyn FnMut(BeforeUnloadEvent)>
|
|
);
|
|
|
|
window
|
|
.add_event_listener_with_callback("beforeunload", &closure.as_ref().unchecked_ref())
|
|
.expect("Failed to add close listener");
|
|
}
|
|
|
|
impl WindowExtWebSys for Window {
|
|
fn canvas(&self) -> HtmlCanvasElement {
|
|
self.window.canvas().raw().clone()
|
|
}
|
|
}
|
|
|
|
pub fn window_size() -> LogicalSize<f64> {
|
|
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 scale_factor() -> f64 {
|
|
let window = web_sys::window().expect("Failed to obtain window");
|
|
window.device_pixel_ratio()
|
|
}
|
|
|
|
pub fn set_canvas_size(raw: &HtmlCanvasElement, size: Size) {
|
|
let scale_factor = scale_factor();
|
|
|
|
let physical_size = size.to_physical::<u32>(scale_factor);
|
|
let logical_size = size.to_logical::<f64>(scale_factor);
|
|
|
|
raw.set_width(physical_size.width);
|
|
raw.set_height(physical_size.height);
|
|
|
|
let style = raw.style();
|
|
style
|
|
.set_property("width", &format!("{}px", logical_size.width))
|
|
.expect("Failed to set canvas width");
|
|
style
|
|
.set_property("height", &format!("{}px", logical_size.height))
|
|
.expect("Failed to set canvas 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,
|
|
}
|
|
}
|