script/dom/
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::rust::HandleObject;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
12use crate::dom::bindings::codegen::Bindings::MediaQueryListEventBinding::{
13    MediaQueryListEventInit, MediaQueryListEventMethods,
14};
15use crate::dom::bindings::error::Fallible;
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
18use crate::dom::bindings::root::DomRoot;
19use crate::dom::bindings::str::DOMString;
20use crate::dom::event::Event;
21use crate::dom::globalscope::GlobalScope;
22use crate::dom::window::Window;
23use crate::script_runtime::CanGc;
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        global: &GlobalScope,
36        proto: Option<HandleObject>,
37        media: DOMString,
38        matches: bool,
39        can_gc: CanGc,
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(ev, global, proto, can_gc)
47    }
48
49    pub(crate) fn new(
50        global: &GlobalScope,
51        type_: Atom,
52        bubbles: bool,
53        cancelable: bool,
54        media: DOMString,
55        matches: bool,
56        can_gc: CanGc,
57    ) -> DomRoot<MediaQueryListEvent> {
58        Self::new_with_proto(
59            global, None, type_, bubbles, cancelable, media, matches, can_gc,
60        )
61    }
62
63    #[allow(clippy::too_many_arguments)]
64    fn new_with_proto(
65        global: &GlobalScope,
66        proto: Option<HandleObject>,
67        type_: Atom,
68        bubbles: bool,
69        cancelable: bool,
70        media: DOMString,
71        matches: bool,
72        can_gc: CanGc,
73    ) -> DomRoot<MediaQueryListEvent> {
74        let ev = MediaQueryListEvent::new_initialized(global, proto, media, matches, can_gc);
75        {
76            let event = ev.upcast::<Event>();
77            event.init_event(type_, bubbles, cancelable);
78        }
79        ev
80    }
81}
82
83impl MediaQueryListEventMethods<crate::DomTypeHolder> for MediaQueryListEvent {
84    // https://drafts.csswg.org/cssom-view/#dom-mediaquerylistevent-mediaquerylistevent
85    fn Constructor(
86        window: &Window,
87        proto: Option<HandleObject>,
88        can_gc: CanGc,
89        type_: DOMString,
90        init: &MediaQueryListEventInit,
91    ) -> Fallible<DomRoot<MediaQueryListEvent>> {
92        Ok(MediaQueryListEvent::new_with_proto(
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            can_gc,
101        ))
102    }
103
104    // https://drafts.csswg.org/cssom-view/#dom-mediaquerylistevent-media
105    fn Media(&self) -> DOMString {
106        self.media.clone()
107    }
108
109    // https://drafts.csswg.org/cssom-view/#dom-mediaquerylistevent-matches
110    fn Matches(&self) -> bool {
111        self.matches.get()
112    }
113
114    // https://dom.spec.whatwg.org/#dom-event-istrusted
115    fn IsTrusted(&self) -> bool {
116        self.upcast::<Event>().IsTrusted()
117    }
118}