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