use wasm_bindgen::JsCast;
use winit::{
dpi::PhysicalSize,
platform::web::{WindowAttributesExtWebSys, WindowExtWebSys},
window::{Window, WindowAttributes},
};
pub(super) fn window_attributes(mut attrs: WindowAttributes) -> WindowAttributes {
attrs.inner_size = None;
attrs.min_inner_size = None;
attrs.max_inner_size = None;
attrs.position = None;
match crate::app::web_canvas::page_canvas() {
Some(canvas) => attrs.with_canvas(Some(canvas)),
None => attrs.with_append(true),
}
}
pub(super) struct TurnEnd;
impl Drop for TurnEnd {
fn drop(&mut self) {
crate::app::window::set_in_loop_turn(false);
}
}
pub(super) fn scrolls_the_systems_distance() -> bool {
thread_local! {
static APPLE: bool = web_sys::window()
.and_then(|window| window.navigator().user_agent().ok())
.is_some_and(|agent| agent.contains("Mac OS"));
}
APPLE.with(|apple| *apple)
}
pub(super) struct WheelPointer {
position: std::rc::Rc<std::cell::Cell<Option<(f64, f64)>>>,
_listener: wasm_bindgen::closure::Closure<dyn FnMut(web_sys::WheelEvent)>,
}
impl WheelPointer {
pub(super) fn watch(window: &Window) -> Option<Self> {
let canvas = window.canvas()?;
let position = std::rc::Rc::new(std::cell::Cell::new(None));
let heard = position.clone();
let listener =
wasm_bindgen::closure::Closure::<dyn FnMut(web_sys::WheelEvent)>::new(move |event: web_sys::WheelEvent| {
heard.set(Some((f64::from(event.offset_x()), f64::from(event.offset_y()))))
});
canvas
.add_event_listener_with_callback_and_bool("wheel", listener.as_ref().unchecked_ref(), true)
.ok()?;
Some(Self {
position,
_listener: listener,
})
}
pub(super) fn take(&self, scale: f64) -> Option<(f64, f64)> {
self.position.take().map(|(x, y)| (x * scale, y * scale))
}
}
pub(super) struct PenPointer {
pen: std::rc::Rc<std::cell::Cell<bool>>,
_listeners: Vec<wasm_bindgen::closure::Closure<dyn FnMut(web_sys::PointerEvent)>>,
}
impl PenPointer {
pub(super) fn watch(window: &Window) -> Option<Self> {
let canvas = window.canvas()?;
let pen = std::rc::Rc::new(std::cell::Cell::new(false));
let mut listeners = Vec::new();
for kind in ["pointerdown", "pointermove", "pointerup"] {
let heard = pen.clone();
let listener =
wasm_bindgen::closure::Closure::<dyn FnMut(web_sys::PointerEvent)>::new(move |event: web_sys::PointerEvent| {
heard.set(event.pointer_type() == "pen")
});
canvas
.add_event_listener_with_callback_and_bool(kind, listener.as_ref().unchecked_ref(), true)
.ok()?;
listeners.push(listener);
}
Some(Self {
pen,
_listeners: listeners,
})
}
pub(super) fn is_pen(&self) -> bool {
self.pen.get()
}
}
pub(super) fn canvas_size(window: &Window) -> Option<PhysicalSize<u32>> {
let canvas = window.canvas()?;
let scale = window.scale_factor();
let width = (f64::from(canvas.client_width()) * scale).round();
let height = (f64::from(canvas.client_height()) * scale).round();
(width >= 1.0 && height >= 1.0).then(|| PhysicalSize::new(width as u32, height as u32))
}
const FIRST_FRAME_MARK: &str = "lurq:first-frame";
pub(super) fn mark_first_frame() {
use std::sync::atomic::{AtomicBool, Ordering};
static MARKED: AtomicBool = AtomicBool::new(false);
if MARKED.swap(true, Ordering::Relaxed) {
return;
}
if let Some(performance) = web_sys::window().and_then(|window| window.performance()) {
let _ = performance.mark(FIRST_FRAME_MARK);
}
}
pub(super) fn size_disagrees(winit: PhysicalSize<u32>, page: PhysicalSize<u32>) -> bool {
winit.width != 0
&& winit.height != 0
&& (winit.width.abs_diff(page.width) > 2 || winit.height.abs_diff(page.height) > 2)
}