Add some helpful wrapper around web_sys
This commit is contained in:
parent
eb589757a8
commit
68db833a3a
5 changed files with 40 additions and 30 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -113,11 +113,9 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"emigui 0.1.0",
|
"emigui 0.1.0",
|
||||||
"emigui_wasm 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 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)",
|
"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)",
|
"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]]
|
[[package]]
|
||||||
|
|
|
@ -18,10 +18,12 @@ emigui = { path = "../emigui" }
|
||||||
[dependencies.web-sys]
|
[dependencies.web-sys]
|
||||||
version = "0.3"
|
version = "0.3"
|
||||||
features = [
|
features = [
|
||||||
|
'console',
|
||||||
'Document',
|
'Document',
|
||||||
'Element',
|
'Element',
|
||||||
'HtmlCanvasElement',
|
'HtmlCanvasElement',
|
||||||
'Performance',
|
'Performance',
|
||||||
|
'Storage',
|
||||||
'WebGlBuffer',
|
'WebGlBuffer',
|
||||||
'WebGlProgram',
|
'WebGlProgram',
|
||||||
'WebGlRenderingContext',
|
'WebGlRenderingContext',
|
||||||
|
|
|
@ -5,3 +5,35 @@ extern crate wasm_bindgen;
|
||||||
extern crate emigui;
|
extern crate emigui;
|
||||||
|
|
||||||
pub mod webgl;
|
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::Storage> {
|
||||||
|
web_sys::window()?.local_storage().ok()?
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn local_storage_get(key: &str) -> Option<String> {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
|
@ -8,26 +8,9 @@ edition = "2018"
|
||||||
crate-type = ["cdylib", "rlib"]
|
crate-type = ["cdylib", "rlib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
js-sys = "0.3"
|
|
||||||
serde = "1"
|
serde = "1"
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
wasm-bindgen = "0.2"
|
wasm-bindgen = "0.2"
|
||||||
|
|
||||||
emigui = { path = "../emigui" }
|
emigui = { path = "../emigui" }
|
||||||
emigui_wasm = { path = "../emigui_wasm" }
|
emigui_wasm = { path = "../emigui_wasm" }
|
||||||
|
|
||||||
[dependencies.web-sys]
|
|
||||||
version = "0.3"
|
|
||||||
features = [
|
|
||||||
'Document',
|
|
||||||
'Element',
|
|
||||||
'HtmlCanvasElement',
|
|
||||||
'Performance',
|
|
||||||
'WebGlBuffer',
|
|
||||||
'WebGlProgram',
|
|
||||||
'WebGlRenderingContext',
|
|
||||||
'WebGlShader',
|
|
||||||
'WebGlTexture',
|
|
||||||
'WebGlUniformLocation',
|
|
||||||
'Window',
|
|
||||||
]
|
|
||||||
|
|
|
@ -6,20 +6,15 @@ extern crate wasm_bindgen;
|
||||||
extern crate emigui;
|
extern crate emigui;
|
||||||
extern crate emigui_wasm;
|
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::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
|
|
||||||
fn now_ms() -> f64 {
|
|
||||||
web_sys::window()
|
|
||||||
.expect("should have a Window")
|
|
||||||
.performance()
|
|
||||||
.expect("should have a Performance")
|
|
||||||
.now()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
app: app::App,
|
app: app::App,
|
||||||
|
@ -39,7 +34,7 @@ impl State {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&mut self, raw_input: RawInput) -> Result<(), JsValue> {
|
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);
|
self.emigui.new_frame(raw_input);
|
||||||
|
|
||||||
|
@ -60,7 +55,7 @@ impl State {
|
||||||
self.webgl_painter
|
self.webgl_painter
|
||||||
.paint(&frame, self.emigui.texture(), raw_input.pixels_per_point);
|
.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
|
result
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue