Run rustfmt

This commit is contained in:
Alex Crichton 2020-03-17 07:49:46 -07:00
parent 29fabdddb1
commit d04930c2a2

View File

@ -1,18 +1,18 @@
#[macro_use] mod utils; #[macro_use]
mod utils;
use futures::{future, Future}; use futures::{future, Future};
use js_sys::Promise; use js_sys::Promise;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use utils::set_panic_hook;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
use wasm_bindgen_futures::future_to_promise; use wasm_bindgen_futures::future_to_promise;
use wasm_bindgen_futures::JsFuture; use wasm_bindgen_futures::JsFuture;
use web_sys::*; use web_sys::*;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use utils::set_panic_hook;
// A macro to provide `println!(..)`-style syntax for `console.log` logging. // A macro to provide `println!(..)`-style syntax for `console.log` logging.
macro_rules! log { macro_rules! log {
@ -21,50 +21,46 @@ macro_rules! log {
} }
} }
fn request_animation_frame(session: &XrSession, f: &Closure<dyn FnMut(f64, XrFrame)>) -> i32 { fn request_animation_frame(session: &XrSession, f: &Closure<dyn FnMut(f64, XrFrame)>) -> i32 {
// This turns the Closure into a js_sys::Function // This turns the Closure into a js_sys::Function
// See https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen/closure/struct.Closure.html#casting-a-closure-to-a-js_sysfunction // See https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen/closure/struct.Closure.html#casting-a-closure-to-a-js_sysfunction
session session.request_animation_frame(f.as_ref().unchecked_ref())
.request_animation_frame(f.as_ref().unchecked_ref())
} }
pub fn create_webgl_context(xr_mode: bool) -> Result<WebGl2RenderingContext, JsValue> { pub fn create_webgl_context(xr_mode: bool) -> Result<WebGl2RenderingContext, JsValue> {
let canvas = web_sys::window() let canvas = web_sys::window()
.unwrap() .unwrap()
.document() .document()
.unwrap() .unwrap()
.get_element_by_id("canvas") .get_element_by_id("canvas")
.unwrap() .unwrap()
.dyn_into::<HtmlCanvasElement>() .dyn_into::<HtmlCanvasElement>()
.unwrap(); .unwrap();
let gl: WebGl2RenderingContext = if xr_mode { let gl: WebGl2RenderingContext = if xr_mode {
let mut gl_attribs = HashMap::new(); let mut gl_attribs = HashMap::new();
gl_attribs.insert(String::from("xrCompatible"), true); gl_attribs.insert(String::from("xrCompatible"), true);
let js_gl_attribs = JsValue::from_serde(&gl_attribs).unwrap(); let js_gl_attribs = JsValue::from_serde(&gl_attribs).unwrap();
canvas.get_context_with_context_options("webgl2", &js_gl_attribs)?.unwrap().dyn_into()? canvas
} .get_context_with_context_options("webgl2", &js_gl_attribs)?
else { .unwrap()
.dyn_into()?
} else {
canvas.get_context("webgl2")?.unwrap().dyn_into()? canvas.get_context("webgl2")?.unwrap().dyn_into()?
}; };
Ok(gl) Ok(gl)
} }
#[wasm_bindgen] #[wasm_bindgen]
pub struct XrApp { pub struct XrApp {
session: Rc<RefCell<Option<XrSession>>>, session: Rc<RefCell<Option<XrSession>>>,
gl: Rc<WebGl2RenderingContext> gl: Rc<WebGl2RenderingContext>,
} }
#[wasm_bindgen] #[wasm_bindgen]
impl XrApp { impl XrApp {
#[wasm_bindgen(constructor)] #[wasm_bindgen(constructor)]
pub fn new() -> XrApp { pub fn new() -> XrApp {
set_panic_hook(); set_panic_hook();
@ -93,7 +89,8 @@ impl XrApp {
let gl = self.gl.clone(); let gl = self.gl.clone();
let future_ = async move { let future_ = async move {
let supports_session = wasm_bindgen_futures::JsFuture::from(session_supported_promise).await; let supports_session =
wasm_bindgen_futures::JsFuture::from(session_supported_promise).await;
let supports_session = supports_session.unwrap(); let supports_session = supports_session.unwrap();
if supports_session == false { if supports_session == false {
log!("XR session not supported"); log!("XR session not supported");
@ -140,11 +137,14 @@ impl XrApp {
// Schedule ourself for another requestAnimationFrame callback. // Schedule ourself for another requestAnimationFrame callback.
// TODO: WebXR Samples call this at top of request_animation_frame - should this be moved? // TODO: WebXR Samples call this at top of request_animation_frame - should this be moved?
request_animation_frame(&sess, f.borrow().as_ref().unwrap()); request_animation_frame(&sess, f.borrow().as_ref().unwrap());
}) as Box<dyn FnMut(f64, XrFrame)>)); }) as Box<dyn FnMut(f64, XrFrame)>));
let session: &Option<XrSession> = &self.session.borrow(); let session: &Option<XrSession> = &self.session.borrow();
let sess: &XrSession = if let Some(sess) = session { sess } else { return () }; let sess: &XrSession = if let Some(sess) = session {
sess
} else {
return ();
};
request_animation_frame(sess, g.borrow().as_ref().unwrap()); request_animation_frame(sess, g.borrow().as_ref().unwrap());
} }