Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resize service #577

Merged
merged 2 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod fetch;
pub mod interval;
pub mod reader;
pub mod render;
pub mod resize;
pub mod storage;
pub mod timeout;
pub mod websocket;
Expand All @@ -19,6 +20,7 @@ pub use self::fetch::FetchService;
pub use self::interval::IntervalService;
pub use self::reader::ReaderService;
pub use self::render::RenderService;
pub use self::resize::ResizeService;
pub use self::storage::StorageService;
pub use self::timeout::TimeoutService;
pub use self::websocket::WebSocketService;
Expand Down
66 changes: 66 additions & 0 deletions src/services/resize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! This module contains the implementation of a service that listens for browser window resize events.
use stdweb::Value;
jstarry marked this conversation as resolved.
Show resolved Hide resolved
use yew::callback::Callback;
use stdweb::{js, web::{window, Window}};


/// A service that fires events when the browser window resizes.
#[derive(Default)]
pub struct ResizeService {}

/// A handle to the event listener for resize events.
#[must_use]
pub struct ResizeTask(Option<Value>);

/// Dimensions of the window.
pub struct WindowDimensions {
/// The width of the viewport of the browser window.
pub width: i32,
/// The height of the viewport of the browser window.
pub height: i32,
}

impl WindowDimensions {
/// Gets the dimensions of the browser window.
pub fn get_dimensions(window: &Window) -> Self {
WindowDimensions {
width: window.inner_width(),
height: window.inner_height(),
}
}
}

impl ResizeService {
/// Creates a new ResizeService.
pub fn new() -> ResizeService {
ResizeService {}
}

/// Register a callback that will be called when the browser window resizes.
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();
jstarry marked this conversation as resolved.
Show resolved Hide resolved
};
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();
}
}
}