Skip to content

Commit

Permalink
Handle AddInput/RemoveInput events
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Jan 9, 2020
1 parent a90a420 commit f721113
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
1 change: 1 addition & 0 deletions components/atoms/static_atoms.txt
Expand Up @@ -44,6 +44,7 @@ iceconnectionstatechange
icegatheringstatechange
image
input
inputsourceschange
invalid
keydown
keypress
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/webidls/XRSession.webidl
Expand Up @@ -40,7 +40,7 @@ interface XRSession : EventTarget {
attribute EventHandler onend;
attribute EventHandler onselect;
attribute EventHandler onsqueeze;
// attribute EventHandler oninputsourceschange;
attribute EventHandler oninputsourceschange;
attribute EventHandler onselectstart;
attribute EventHandler onselectend;
attribute EventHandler onsqueezestart;
Expand Down
55 changes: 54 additions & 1 deletion components/script/dom/xrinputsourcearray.rs
Expand Up @@ -5,13 +5,16 @@
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::XRInputSourceArrayBinding;
use crate::dom::bindings::codegen::Bindings::XRInputSourceArrayBinding::XRInputSourceArrayMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::event::Event;
use crate::dom::globalscope::GlobalScope;
use crate::dom::xrinputsource::XRInputSource;
use crate::dom::xrinputsourceschangeevent::XRInputSourcesChangeEvent;
use crate::dom::xrsession::XRSession;
use dom_struct::dom_struct;
use webxr_api::InputId;
use webxr_api::{InputId, InputSource};

#[dom_struct]
pub struct XRInputSourceArray {
Expand Down Expand Up @@ -48,6 +51,56 @@ impl XRInputSourceArray {
});
}

pub fn add_input_source(&self, session: &XRSession, info: InputSource) {
let mut input_sources = self.input_sources.borrow_mut();
let global = self.global();
let input = XRInputSource::new(&global, &session, info);
debug_assert!(
input_sources.iter().find(|i| i.id() == info.id).is_none(),
"Should never add a duplicate input id!"
);
input_sources.push(Dom::from_ref(&input));

let added = [input];

let event = XRInputSourcesChangeEvent::new(
&global,
atom!("inputsourceschange"),
false,
true,
session,
&added,
&[],
);
// Release the refcell guard
drop(input_sources);
event.upcast::<Event>().fire(session.upcast());
}

pub fn remove_input_source(&self, session: &XRSession, id: InputId) {
let mut input_sources = self.input_sources.borrow_mut();
let global = self.global();
let removed = if let Some(i) = input_sources.iter().find(|i| i.id() == id) {
[DomRoot::from_ref(&**i)]
} else {
return;
};

let event = XRInputSourcesChangeEvent::new(
&global,
atom!("inputsourceschange"),
false,
true,
session,
&[],
&removed,
);
input_sources.retain(|i| i.id() != id);
// release the refcell guard
drop(input_sources);
event.upcast::<Event>().fire(session.upcast());
}

pub fn find(&self, id: InputId) -> Option<DomRoot<XRInputSource>> {
self.input_sources
.borrow()
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/xrinputsourceschangeevent.rs
Expand Up @@ -66,10 +66,10 @@ impl XRInputSourcesChangeEvent {
let cx = global.get_cx();
unsafe {
rooted!(in(*cx) let mut added_val = UndefinedValue());
rooted!(in(*cx) let mut removed_val = UndefinedValue());
added.to_jsval(*cx, added_val.handle_mut());
removed.to_jsval(*cx, removed_val.handle_mut());
changeevent.added.set(added_val.get());
rooted!(in(*cx) let mut removed_val = UndefinedValue());
removed.to_jsval(*cx, removed_val.handle_mut());
changeevent.added.set(removed_val.get());
}

Expand Down
14 changes: 13 additions & 1 deletion components/script/dom/xrsession.rs
Expand Up @@ -289,7 +289,12 @@ impl XRSession {
);
event.upcast::<Event>().fire(self.upcast());
},
_ => (), // XXXManishearth TBD
XREvent::AddInput(info) => {
self.input_sources.add_input_source(self, info);
},
XREvent::RemoveInput(id) => {
self.input_sources.remove_input_source(self, id);
},
}
}

Expand Down Expand Up @@ -432,6 +437,13 @@ impl XRSessionMethods for XRSession {
SetOnvisibilitychange
);

/// https://immersive-web.github.io/webxr/#eventdef-xrsession-inputsourceschange
event_handler!(
inputsourceschange,
GetOninputsourceschange,
SetOninputsourceschange
);

// https://immersive-web.github.io/webxr/#dom-xrsession-renderstate
fn RenderState(&self) -> DomRoot<XRRenderState> {
self.active_render_state.get()
Expand Down

0 comments on commit f721113

Please sign in to comment.