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