1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 use dom::bindings::codegen::Bindings::EventBinding::EventBinding::EventMethods; 6 use dom::bindings::codegen::Bindings::VRDisplayEventBinding; 7 use dom::bindings::codegen::Bindings::VRDisplayEventBinding::VRDisplayEventMethods; 8 use dom::bindings::codegen::Bindings::VRDisplayEventBinding::VRDisplayEventReason; 9 use dom::bindings::error::Fallible; 10 use dom::bindings::inheritance::Castable; 11 use dom::bindings::reflector::{DomObject, reflect_dom_object}; 12 use dom::bindings::root::{Dom, DomRoot}; 13 use dom::bindings::str::DOMString; 14 use dom::event::Event; 15 use dom::globalscope::GlobalScope; 16 use dom::vrdisplay::VRDisplay; 17 use dom::window::Window; 18 use dom_struct::dom_struct; 19 use servo_atoms::Atom; 20 use webvr_traits::{WebVRDisplayEvent, WebVRDisplayEventReason}; 21 22 #[dom_struct] 23 pub struct VRDisplayEvent { 24 event: Event, 25 display: Dom<VRDisplay>, 26 reason: Option<VRDisplayEventReason> 27 } 28 29 impl VRDisplayEvent { new_inherited(display: &VRDisplay, reason: Option<VRDisplayEventReason>) -> VRDisplayEvent30 fn new_inherited(display: &VRDisplay, 31 reason: Option<VRDisplayEventReason>) 32 -> VRDisplayEvent { 33 VRDisplayEvent { 34 event: Event::new_inherited(), 35 display: Dom::from_ref(display), 36 reason: reason.clone() 37 } 38 } 39 new(global: &GlobalScope, type_: Atom, bubbles: bool, cancelable: bool, display: &VRDisplay, reason: Option<VRDisplayEventReason>) -> DomRoot<VRDisplayEvent>40 pub fn new(global: &GlobalScope, 41 type_: Atom, 42 bubbles: bool, 43 cancelable: bool, 44 display: &VRDisplay, 45 reason: Option<VRDisplayEventReason>) 46 -> DomRoot<VRDisplayEvent> { 47 let ev = reflect_dom_object( 48 Box::new(VRDisplayEvent::new_inherited(&display, reason)), 49 global, 50 VRDisplayEventBinding::Wrap 51 ); 52 { 53 let event = ev.upcast::<Event>(); 54 event.init_event(type_, bubbles, cancelable); 55 } 56 ev 57 } 58 new_from_webvr(global: &GlobalScope, display: &VRDisplay, event: &WebVRDisplayEvent) -> DomRoot<VRDisplayEvent>59 pub fn new_from_webvr(global: &GlobalScope, 60 display: &VRDisplay, 61 event: &WebVRDisplayEvent) 62 -> DomRoot<VRDisplayEvent> { 63 let (name, reason) = match *event { 64 WebVRDisplayEvent::Connect(_) => ("vrdisplayconnect", None), 65 WebVRDisplayEvent::Disconnect(_) => ("vrdisplaydisconnect", None), 66 WebVRDisplayEvent::Activate(_, reason) => ("vrdisplayactivate", Some(reason)), 67 WebVRDisplayEvent::Deactivate(_, reason) => ("vrdisplaydeactivate", Some(reason)), 68 WebVRDisplayEvent::Blur(_) => ("vrdisplayblur", None), 69 WebVRDisplayEvent::Focus(_) => ("vrdisplayfocus", None), 70 WebVRDisplayEvent::PresentChange(_, _) => ("vrdisplaypresentchange", None), 71 WebVRDisplayEvent::Change(_) | 72 WebVRDisplayEvent::Pause(_) | 73 WebVRDisplayEvent::Resume(_) | 74 WebVRDisplayEvent::Exit(_) => { 75 panic!("{:?} event not available in WebVR", event) 76 } 77 }; 78 79 // map to JS enum values 80 let reason = reason.map(|r| { 81 match r { 82 WebVRDisplayEventReason::Navigation => VRDisplayEventReason::Navigation, 83 WebVRDisplayEventReason::Mounted => VRDisplayEventReason::Mounted, 84 WebVRDisplayEventReason::Unmounted => VRDisplayEventReason::Unmounted, 85 } 86 }); 87 88 VRDisplayEvent::new(&global, 89 Atom::from(DOMString::from(name)), 90 false, 91 false, 92 &display, 93 reason) 94 } 95 Constructor(window: &Window, type_: DOMString, init: &VRDisplayEventBinding::VRDisplayEventInit) -> Fallible<DomRoot<VRDisplayEvent>>96 pub fn Constructor(window: &Window, 97 type_: DOMString, 98 init: &VRDisplayEventBinding::VRDisplayEventInit) 99 -> Fallible<DomRoot<VRDisplayEvent>> { 100 Ok(VRDisplayEvent::new(&window.global(), 101 Atom::from(type_), 102 init.parent.bubbles, 103 init.parent.cancelable, 104 &init.display, 105 init.reason)) 106 } 107 } 108 109 impl VRDisplayEventMethods for VRDisplayEvent { 110 // https://w3c.github.io/webvr/#dom-vrdisplayevent-display Display(&self) -> DomRoot<VRDisplay>111 fn Display(&self) -> DomRoot<VRDisplay> { 112 DomRoot::from_ref(&*self.display) 113 } 114 115 // https://w3c.github.io/webvr/#enumdef-vrdisplayeventreason GetReason(&self) -> Option<VRDisplayEventReason>116 fn GetReason(&self) -> Option<VRDisplayEventReason> { 117 self.reason 118 } 119 120 // https://dom.spec.whatwg.org/#dom-event-istrusted IsTrusted(&self) -> bool121 fn IsTrusted(&self) -> bool { 122 self.event.IsTrusted() 123 } 124 } 125