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::rust::HandleObject;
7use stylo_atoms::Atom;
8
9use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
10use crate::dom::bindings::codegen::Bindings::WebGLContextEventBinding::{
11    WebGLContextEventInit, WebGLContextEventMethods,
12};
13use crate::dom::bindings::error::Fallible;
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::event::{Event, EventBubbles, EventCancelable};
19use crate::dom::window::Window;
20use crate::script_runtime::CanGc;
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        window: &Window,
32        proto: Option<HandleObject>,
33        can_gc: CanGc,
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            window,
48            proto,
49            Atom::from(type_),
50            bubbles,
51            cancelable,
52            status_message,
53            can_gc,
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        window: &Window,
78        type_: Atom,
79        bubbles: EventBubbles,
80        cancelable: EventCancelable,
81        status_message: DOMString,
82        can_gc: CanGc,
83    ) -> DomRoot<WebGLContextEvent> {
84        Self::new_with_proto(
85            window,
86            None,
87            type_,
88            bubbles,
89            cancelable,
90            status_message,
91            can_gc,
92        )
93    }
94
95    fn new_with_proto(
96        window: &Window,
97        proto: Option<HandleObject>,
98        type_: Atom,
99        bubbles: EventBubbles,
100        cancelable: EventCancelable,
101        status_message: DOMString,
102        can_gc: CanGc,
103    ) -> DomRoot<WebGLContextEvent> {
104        let event = reflect_dom_object_with_proto(
105            Box::new(WebGLContextEvent::new_inherited(status_message)),
106            window,
107            proto,
108            can_gc,
109        );
110
111        {
112            let parent = event.upcast::<Event>();
113            parent.init_event(type_, bool::from(bubbles), bool::from(cancelable));
114        }
115
116        event
117    }
118}