1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::cell::DomRefCell;
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, InputSource};

#[dom_struct]
pub struct XRInputSourceArray {
    reflector_: Reflector,
    input_sources: DomRefCell<Vec<Dom<XRInputSource>>>,
}

impl XRInputSourceArray {
    fn new_inherited() -> XRInputSourceArray {
        XRInputSourceArray {
            reflector_: Reflector::new(),
            input_sources: DomRefCell::new(vec![]),
        }
    }

    pub fn new(global: &GlobalScope) -> DomRoot<XRInputSourceArray> {
        reflect_dom_object(Box::new(XRInputSourceArray::new_inherited()), global)
    }

    pub fn add_input_sources(&self, session: &XRSession, inputs: &[InputSource]) {
        let mut input_sources = self.input_sources.borrow_mut();
        let global = self.global();

        let mut added = vec![];
        for info in inputs {
            // This is quadratic, but won't be a problem for the only case
            // where we add multiple input sources (the initial input sources case)
            debug_assert!(
                input_sources.iter().find(|i| i.id() == info.id).is_none(),
                "Should never add a duplicate input id!"
            );
            let input = XRInputSource::new(&global, &session, info.clone());
            input_sources.push(Dom::from_ref(&input));
            added.push(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 add_remove_input_source(&self, session: &XRSession, id: InputId, info: InputSource) {
        let mut input_sources = self.input_sources.borrow_mut();
        let global = self.global();
        let root;
        let removed = if let Some(i) = input_sources.iter().find(|i| i.id() == id) {
            root = [DomRoot::from_ref(&**i)];
            &root as &[_]
        } else {
            warn!("Could not find removed input source with id {:?}", id);
            &[]
        };
        input_sources.retain(|i| i.id() != id);
        let input = XRInputSource::new(&global, &session, info);
        input_sources.push(Dom::from_ref(&input));

        let added = [input];

        let event = XRInputSourcesChangeEvent::new(
            &global,
            atom!("inputsourceschange"),
            false,
            true,
            session,
            &added,
            removed,
        );
        // 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()
            .iter()
            .find(|x| x.id() == id)
            .map(|x| DomRoot::from_ref(&**x))
    }
}

impl XRInputSourceArrayMethods for XRInputSourceArray {
    /// https://immersive-web.github.io/webxr/#dom-xrinputsourcearray-length
    fn Length(&self) -> u32 {
        self.input_sources.borrow().len() as u32
    }

    /// https://immersive-web.github.io/webxr/#xrinputsourcearray
    fn IndexedGetter(&self, n: u32) -> Option<DomRoot<XRInputSource>> {
        self.input_sources
            .borrow()
            .get(n as usize)
            .map(|x| DomRoot::from_ref(&**x))
    }
}