1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use stdweb::Value;
use stdweb::{
js,
web::{window, Window},
};
use yew::callback::Callback;
#[derive(Default)]
pub struct ResizeService {}
#[must_use]
pub struct ResizeTask(Option<Value>);
pub struct WindowDimensions {
pub width: i32,
pub height: i32,
}
impl WindowDimensions {
pub fn get_dimensions(window: &Window) -> Self {
WindowDimensions {
width: window.inner_width(),
height: window.inner_height(),
}
}
}
impl ResizeService {
pub fn new() -> ResizeService {
ResizeService {}
}
pub fn register(&mut self, callback: Callback<WindowDimensions>) -> ResizeTask {
let callback = move || {
let window = window();
let dimensions = WindowDimensions::get_dimensions(&window);
callback.emit(dimensions);
};
let handle = js! {
var callback = @{callback};
var action = function() {
callback();
};
return window.addEventListener("resize", action);
};
ResizeTask(Some(handle))
}
}
impl Drop for ResizeTask {
fn drop(&mut self) {
let handle = self.0.take().expect("Resize task already empty.");
js! {
@(no_return)
var handle = @{handle};
handle.callback.drop();
}
}
}