Skip to content

Commit f24828a

Browse files
committed
Add a top-level web_sys::window function
Returns `Option<Window>` and can be used as a convenience to get a handle to the global `window` object.
1 parent 99e1b35 commit f24828a

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

crates/web-sys/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,15 @@ extern crate wasm_bindgen;
1717
extern crate js_sys;
1818
use js_sys::Object;
1919

20+
#[cfg(feature = "Window")]
21+
pub fn window() -> Option<Window> {
22+
use wasm_bindgen::{JsValue, JsCast};
23+
24+
js_sys::Function::new_no_args("return this")
25+
.call0(&JsValue::undefined())
26+
.ok()?
27+
.dyn_into::<Window>()
28+
.ok()
29+
}
30+
2031
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

examples/canvas/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use wasm_bindgen::JsCast;
88

99
#[wasm_bindgen]
1010
pub fn draw() {
11-
let document = web_sys::Window::document().unwrap();
11+
let document = web_sys::window().unwrap().document().unwrap();
1212
let canvas = document.get_element_by_id("canvas").unwrap();
1313
let canvas: web_sys::HtmlCanvasElement = canvas
1414
.dyn_into::<web_sys::HtmlCanvasElement>()

examples/fetch/build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ cargo +nightly build --target wasm32-unknown-unknown
88
cargo +nightly run --manifest-path ../../crates/cli/Cargo.toml \
99
--bin wasm-bindgen -- \
1010
../../target/wasm32-unknown-unknown/debug/fetch.wasm --out-dir .
11+
12+
npm install
13+
npm run serve

examples/fetch/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use wasm_bindgen::prelude::*;
1212
use wasm_bindgen::JsCast;
1313
use wasm_bindgen_futures::future_to_promise;
1414
use wasm_bindgen_futures::JsFuture;
15-
use web_sys::{Request, RequestInit, RequestMode, Response, Window};
15+
use web_sys::{Request, RequestInit, RequestMode, Response};
1616

1717
/// A struct to hold some data from the github Branch API.
1818
///
@@ -57,7 +57,8 @@ pub fn run() -> Promise {
5757
.set("Accept", "application/vnd.github.v3+json")
5858
.unwrap();
5959

60-
let request_promise = Window::fetch_with_request(&request);
60+
let window = web_sys::window().unwrap();
61+
let request_promise = window.fetch_with_request(&request);
6162

6263
let future = JsFuture::from(request_promise)
6364
.and_then(|resp_value| {

examples/paint/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use wasm_bindgen::JsCast;
99

1010
#[wasm_bindgen]
1111
pub fn main() {
12-
let document = web_sys::Window::document().unwrap();
12+
let document = web_sys::window().unwrap().document().unwrap();
1313
let canvas = document
1414
.create_element("canvas")
1515
.unwrap()

0 commit comments

Comments
 (0)