Skip to main content

script/dom/webgl/
webglcontextevent.rs

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 https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
12use crate::dom::bindings::codegen::Bindings::WebGLContextEventBinding::{
13    WebGLContextEventInit, WebGLContextEventMethods,
14};
15use crate::dom::bindings::error::Fallible;
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::DOMString;
19use crate::dom::event::{Event, EventBubbles, EventCancelable};
20use crate::dom::window::Window;
21
22#[dom_struct]
23pub(crate) struct WebGLContextEvent {
24    event: Event,
25    status_message: DOMString,
26}
27
28impl WebGLContextEventMethods<crate::DomTypeHolder> for WebGLContextEvent {
29    /// <https://registry.khronos.org/webgl/specs/latest/1.0/#5.15>
30    fn Constructor(
31        cx: &mut JSContext,
32        window: &Window,
33        proto: Option<HandleObject>,
34        type_: DOMString,
35        init: &WebGLContextEventInit,
36    ) -> Fallible<DomRoot<WebGLContextEvent>> {
37        let status_message = match init.statusMessage.as_ref() {
38            Some(message) => message.clone(),
39            None => DOMString::new(),
40        };
41
42        let bubbles = EventBubbles::from(init.parent.bubbles);
43
44        let cancelable = EventCancelable::from(init.parent.cancelable);
45
46        Ok(WebGLContextEvent::new_with_proto(
47            cx,
48            window,
49            proto,
50            Atom::from(type_),
51            bubbles,
52            cancelable,
53            status_message,
54        ))
55    }
56
57    /// <https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15>
58    fn StatusMessage(&self) -> DOMString {
59        self.status_message.clone()
60    }
61
62    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
63    fn IsTrusted(&self) -> bool {
64        self.event.IsTrusted()
65    }
66}
67
68impl WebGLContextEvent {
69    fn new_inherited(status_message: DOMString) -> WebGLContextEvent {
70        WebGLContextEvent {
71            event: Event::new_inherited(),
72            status_message,
73        }
74    }
75
76    pub(crate) fn new(
77        cx: &mut JSContext,
78        window: &Window,
79        type_: Atom,
80        bubbles: EventBubbles,
81        cancelable: EventCancelable,
82        status_message: DOMString,
83    ) -> DomRoot<WebGLContextEvent> {
84        Self::new_with_proto(cx, window, None, type_, bubbles, cancelable, status_message)
85    }
86
87    fn new_with_proto(
88        cx: &mut JSContext,
89        window: &Window,
90        proto: Option<HandleObject>,
91        type_: Atom,
92        bubbles: EventBubbles,
93        cancelable: EventCancelable,
94        status_message: DOMString,
95    ) -> DomRoot<WebGLContextEvent> {
96        let event = reflect_dom_object_with_proto_and_cx(
97            Box::new(WebGLContextEvent::new_inherited(status_message)),
98            window,
99            proto,
100            cx,
101        );
102
103        {
104            let parent = event.upcast::<Event>();
105            parent.init_event(type_, bool::from(bubbles), bool::from(cancelable));
106        }
107
108        event
109    }
110}