mirror of
https://github.com/servo/servo.git
synced 2025-08-04 05:00:08 +01:00
Handle AddInput/RemoveInput events
This commit is contained in:
parent
a90a42012b
commit
f721113f8d
5 changed files with 71 additions and 5 deletions
|
@ -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 {
|
||||
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue