yew_stdweb/services/
resize.rsuse crate::services::Task;
use cfg_if::cfg_if;
use cfg_match::cfg_match;
use std::fmt;
use yew::callback::Callback;
cfg_if! {
if #[cfg(feature = "std_web")] {
use stdweb::js;
use stdweb::web::{event::ResizeEvent, window, Window};
use stdweb::Value;
} else if #[cfg(feature = "web_sys")] {
use gloo::events::EventListener;
use web_sys::{Event, Window};
}
}
#[derive(Default, Debug)]
pub struct ResizeService {}
#[must_use = "the listener is only active until the task is dropped"]
pub struct ResizeTask(
#[cfg(feature = "std_web")] Value,
#[cfg(feature = "web_sys")] EventListener,
);
impl fmt::Debug for ResizeTask {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("ResizeTask")
}
}
#[derive(Copy, Clone, Debug)]
pub struct WindowDimensions {
pub width: i32,
pub height: i32,
}
impl WindowDimensions {
pub fn get_dimensions(window: &Window) -> Self {
let width = window.inner_width();
let height = window.inner_height();
#[cfg(feature = "web_sys")]
let (width, height) = {
(
width.unwrap().as_f64().unwrap() as _,
height.unwrap().as_f64().unwrap() as _,
)
};
WindowDimensions { width, height }
}
}
impl ResizeService {
pub fn register(callback: Callback<WindowDimensions>) -> ResizeTask {
let callback =
move |#[cfg(feature = "web_sys")] _event: &Event,
#[cfg(feature = "std_web")] _event: ResizeEvent| {
let window = cfg_match! {
feature = "std_web" => window(),
feature = "web_sys" => web_sys::window().unwrap(),
};
let dimensions = WindowDimensions::get_dimensions(&window);
callback.emit(dimensions);
};
let handle = cfg_match! {
feature = "std_web" => js! {
var handle = @{callback};
window.addEventListener("resize", handle);
return handle;
},
feature = "web_sys" => EventListener::new(&web_sys::window().unwrap(), "resize", callback),
};
ResizeTask(handle)
}
}
impl Task for ResizeTask {
fn is_active(&self) -> bool {
true
}
}
impl Drop for ResizeTask {
fn drop(&mut self) {
#[cfg(feature = "std_web")]
{
let handle = &self.0;
js! {
@(no_return)
var handle = @{handle};
window.removeEventListener("resize", handle);
}
}
}
}