Skip to main content

script/dom/media/
mediaquerylistevent.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 std::cell::Cell;
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use js::rust::HandleObject;
10use script_bindings::domstring::TracedDOMString;
11use script_bindings::reflector::reflect_dom_object_with_proto;
12use stylo_atoms::Atom;
13
14use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
15use crate::dom::bindings::codegen::Bindings::MediaQueryListEventBinding::{
16    MediaQueryListEventInit, MediaQueryListEventMethods,
17};
18use crate::dom::bindings::error::Fallible;
19use crate::dom::bindings::inheritance::Castable;
20use crate::dom::bindings::root::DomRoot;
21use crate::dom::bindings::str::DOMString;
22use crate::dom::event::Event;
23use crate::dom::globalscope::GlobalScope;
24use crate::dom::window::Window;
25
26// https://drafts.csswg.org/cssom-view/#dom-mediaquerylistevent-mediaquerylistevent
27#[dom_struct]
28pub(crate) struct MediaQueryListEvent {
29    event: Event,
30    media: TracedDOMString,
31    matches: Cell<bool>,
32}
33
34impl MediaQueryListEvent {
35    fn new_initialized(
36        cx: &mut JSContext,
37        global: &GlobalScope,
38        proto: Option<HandleObject>,
39        media: DOMString,
40        matches: bool,
41    ) -> DomRoot<MediaQueryListEvent> {
42        let ev = Box::new(MediaQueryListEvent {
43            event: Event::new_inherited(),
44            media: media.traced(),
45            matches: Cell::new(matches),
46        });
47        reflect_dom_object_with_proto(cx, ev, global, proto)
48    }
49
50    pub(crate) fn new(
51        cx: &mut JSContext,
52        global: &GlobalScope,
53        type_: Atom,
54        bubbles: bool,
55        cancelable: bool,
56        media: DOMString,
57        matches: bool,
58    ) -> DomRoot<MediaQueryListEvent> {
59        Self::new_with_proto(cx, global, None, type_, bubbles, cancelable, media, matches)
60    }
61
62    #[expect(clippy::too_many_arguments)]
63    fn new_with_proto(
64        cx: &mut JSContext,
65        global: &GlobalScope,
66        proto: Option<HandleObject>,
67        type_: Atom,
68        bubbles: bool,
69        cancelable: bool,
70        media: DOMString,
71        matches: bool,
72    ) -> DomRoot<MediaQueryListEvent> {
73        let ev = MediaQueryListEvent::new_initialized(cx, global, proto, media, matches);
74        {
75            let event = ev.upcast::<Event>();
76            event.init_event(type_, bubbles, cancelable);
77        }
78        ev
79    }
80}
81
82impl MediaQueryListEventMethods<crate::DomTypeHolder> for MediaQueryListEvent {
83    /// <https://drafts.csswg.org/cssom-view/#dom-mediaquerylistevent-mediaquerylistevent>
84    fn Constructor(
85        cx: &mut JSContext,
86        window: &Window,
87        proto: Option<HandleObject>,
88        type_: DOMString,
89        init: &MediaQueryListEventInit,
90    ) -> Fallible<DomRoot<MediaQueryListEvent>> {
91        Ok(MediaQueryListEvent::new_with_proto(
92            cx,
93            window.as_global_scope(),
94            proto,
95            Atom::from(type_),
96            init.parent.bubbles,
97            init.parent.cancelable,
98            init.media.clone(),
99            init.matches,
100        ))
101    }
102
103    /// <https://drafts.csswg.org/cssom-view/#dom-mediaquerylistevent-media>
104    fn Media(&self) -> DOMString {
105        self.media.clone().root()
106    }
107
108    /// <https://drafts.csswg.org/cssom-view/#dom-mediaquerylistevent-matches>
109    fn Matches(&self) -> bool {
110        self.matches.get()
111    }
112
113    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
114    fn IsTrusted(&self) -> bool {
115        self.upcast::<Event>().IsTrusted()
116    }
117}