From 68db833a3a3f8218267fd40082d7e62347f289a6 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 11 Feb 2019 20:27:32 +0100 Subject: [PATCH] Add some helpful wrapper around web_sys --- Cargo.lock | 2 -- emigui_wasm/Cargo.toml | 2 ++ emigui_wasm/src/lib.rs | 32 ++++++++++++++++++++++++++++++++ example/Cargo.toml | 17 ----------------- example/src/lib.rs | 17 ++++++----------- 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1672e66c..40076bfb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -113,11 +113,9 @@ version = "0.1.0" dependencies = [ "emigui 0.1.0", "emigui_wasm 0.1.0", - "js-sys 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)", "wasm-bindgen 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", - "web-sys 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/emigui_wasm/Cargo.toml b/emigui_wasm/Cargo.toml index 8450eb60..20c3cf12 100644 --- a/emigui_wasm/Cargo.toml +++ b/emigui_wasm/Cargo.toml @@ -18,10 +18,12 @@ emigui = { path = "../emigui" } [dependencies.web-sys] version = "0.3" features = [ + 'console', 'Document', 'Element', 'HtmlCanvasElement', 'Performance', + 'Storage', 'WebGlBuffer', 'WebGlProgram', 'WebGlRenderingContext', diff --git a/emigui_wasm/src/lib.rs b/emigui_wasm/src/lib.rs index 9e548df8..cf97555f 100644 --- a/emigui_wasm/src/lib.rs +++ b/emigui_wasm/src/lib.rs @@ -5,3 +5,35 @@ extern crate wasm_bindgen; extern crate emigui; pub mod webgl; + +// ---------------------------------------------------------------------------- +// Helpers to hide some of the verbosity of web_sys + +pub fn console_log(s: String) { + web_sys::console::log_1(&s.into()); +} + +pub fn now_sec() -> f64 { + web_sys::window() + .expect("should have a Window") + .performance() + .expect("should have a Performance") + .now() + / 1000.0 +} + +pub fn local_storage() -> Option { + web_sys::window()?.local_storage().ok()? +} + +pub fn local_storage_get(key: &str) -> Option { + local_storage().map(|storage| storage.get_item(key).ok())?? +} + +pub fn local_storage_set(key: &str, value: &str) { + local_storage().map(|storage| storage.set_item(key, value)); +} + +pub fn local_storage_remove(key: &str) { + local_storage().map(|storage| storage.remove_item(key)); +} diff --git a/example/Cargo.toml b/example/Cargo.toml index 637df910..0f23363e 100644 --- a/example/Cargo.toml +++ b/example/Cargo.toml @@ -8,26 +8,9 @@ edition = "2018" crate-type = ["cdylib", "rlib"] [dependencies] -js-sys = "0.3" serde = "1" serde_json = "1" wasm-bindgen = "0.2" emigui = { path = "../emigui" } emigui_wasm = { path = "../emigui_wasm" } - -[dependencies.web-sys] -version = "0.3" -features = [ - 'Document', - 'Element', - 'HtmlCanvasElement', - 'Performance', - 'WebGlBuffer', - 'WebGlProgram', - 'WebGlRenderingContext', - 'WebGlShader', - 'WebGlTexture', - 'WebGlUniformLocation', - 'Window', -] diff --git a/example/src/lib.rs b/example/src/lib.rs index a3304f34..09216991 100644 --- a/example/src/lib.rs +++ b/example/src/lib.rs @@ -6,20 +6,15 @@ extern crate wasm_bindgen; extern crate emigui; extern crate emigui_wasm; -use emigui::{label, widgets::Label, Align, Emigui, RawInput}; +use { + emigui::{label, widgets::Label, Align, Emigui, RawInput}, + emigui_wasm::now_sec, +}; use wasm_bindgen::prelude::*; mod app; -fn now_ms() -> f64 { - web_sys::window() - .expect("should have a Window") - .performance() - .expect("should have a Performance") - .now() -} - #[wasm_bindgen] pub struct State { app: app::App, @@ -39,7 +34,7 @@ impl State { } fn run(&mut self, raw_input: RawInput) -> Result<(), JsValue> { - let everything_start = now_ms(); + let everything_start = now_sec(); self.emigui.new_frame(raw_input); @@ -60,7 +55,7 @@ impl State { self.webgl_painter .paint(&frame, self.emigui.texture(), raw_input.pixels_per_point); - self.everything_ms = now_ms() - everything_start; + self.everything_ms = 1000.0 * (now_sec() - everything_start); result }