On Web, implement WindowEvent::Occluded (#2940)
This commit is contained in:
parent
bd890e69aa
commit
5e0e1e96bc
9 changed files with 165 additions and 9 deletions
|
|
@ -1,6 +1,7 @@
|
|||
mod canvas;
|
||||
pub mod event;
|
||||
mod event_handle;
|
||||
mod intersection_handle;
|
||||
mod media_query_handle;
|
||||
mod pointer;
|
||||
mod resize_scaling;
|
||||
|
|
@ -16,7 +17,9 @@ use crate::dpi::LogicalSize;
|
|||
use crate::platform::web::WindowExtWebSys;
|
||||
use crate::window::Window;
|
||||
use wasm_bindgen::closure::Closure;
|
||||
use web_sys::{CssStyleDeclaration, Element, HtmlCanvasElement, PageTransitionEvent};
|
||||
use web_sys::{
|
||||
CssStyleDeclaration, Element, HtmlCanvasElement, PageTransitionEvent, VisibilityState,
|
||||
};
|
||||
|
||||
pub fn throw(msg: &str) {
|
||||
wasm_bindgen::throw_str(msg);
|
||||
|
|
@ -136,4 +139,25 @@ pub fn is_dark_mode(window: &web_sys::Window) -> Option<bool> {
|
|||
.map(|media| media.matches())
|
||||
}
|
||||
|
||||
pub fn is_visible(window: &web_sys::Window) -> bool {
|
||||
let document = window.document().expect("Failed to obtain document");
|
||||
document.visibility_state() == VisibilityState::Visible
|
||||
}
|
||||
|
||||
pub fn is_intersecting(window: &web_sys::Window, canvas: &HtmlCanvasElement) -> bool {
|
||||
let rect = canvas.get_bounding_client_rect();
|
||||
// This should never panic.
|
||||
let window_width = window.inner_width().unwrap().as_f64().unwrap() as i32;
|
||||
let window_height = window.inner_height().unwrap().as_f64().unwrap() as i32;
|
||||
let left = rect.left() as i32;
|
||||
let width = rect.width() as i32;
|
||||
let top = rect.top() as i32;
|
||||
let height = rect.height() as i32;
|
||||
|
||||
let horizontal = left <= window_width && left + width >= 0;
|
||||
let vertical = top <= window_height && top + height >= 0;
|
||||
|
||||
horizontal && vertical
|
||||
}
|
||||
|
||||
pub type RawCanvasType = HtmlCanvasElement;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue