Skip to main content

script/dom/media/
mediaquerylist.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;
6use std::rc::Rc;
7
8use dom_struct::dom_struct;
9use js::context::JSContext;
10use script_bindings::callback::RootedCallback;
11use script_bindings::reflector::reflect_weak_referenceable_dom_object;
12use script_bindings::trace::RootedTraceableBox;
13use style::media_queries::MediaList;
14use style::stylesheets::CustomMediaEvaluator;
15use style_traits::ToCss;
16
17use crate::dom::bindings::codegen::Bindings::EventListenerBinding::EventListener;
18use crate::dom::bindings::codegen::Bindings::EventTargetBinding::{
19    AddEventListenerOptions, EventListenerOptions,
20};
21use crate::dom::bindings::codegen::Bindings::MediaQueryListBinding::MediaQueryListMethods;
22use crate::dom::bindings::inheritance::Castable;
23use crate::dom::bindings::root::{Dom, DomRoot};
24use crate::dom::bindings::str::DOMString;
25use crate::dom::document::Document;
26use crate::dom::eventtarget::EventTarget;
27
28pub(crate) enum MediaQueryListMatchState {
29    Same,
30    Changed,
31}
32
33#[dom_struct]
34pub(crate) struct MediaQueryList {
35    eventtarget: EventTarget,
36    document: Dom<Document>,
37    #[no_trace]
38    media_query_list: MediaList,
39    last_match_state: Cell<Option<bool>>,
40}
41
42impl MediaQueryList {
43    fn new_inherited(document: &Document, media_query_list: MediaList) -> MediaQueryList {
44        MediaQueryList {
45            eventtarget: EventTarget::new_inherited(),
46            document: Dom::from_ref(document),
47            media_query_list,
48            last_match_state: Cell::new(None),
49        }
50    }
51
52    pub(crate) fn new(
53        cx: &mut JSContext,
54        document: &Document,
55        media_query_list: MediaList,
56    ) -> DomRoot<MediaQueryList> {
57        reflect_weak_referenceable_dom_object(
58            cx,
59            Rc::new(MediaQueryList::new_inherited(document, media_query_list)),
60            document.window(),
61        )
62    }
63}
64
65impl MediaQueryList {
66    pub(crate) fn evaluate_changes(&self) -> MediaQueryListMatchState {
67        let matches = self.evaluate();
68
69        let result = if let Some(old_matches) = self.last_match_state.get() {
70            if old_matches == matches {
71                MediaQueryListMatchState::Same
72            } else {
73                MediaQueryListMatchState::Changed
74            }
75        } else {
76            MediaQueryListMatchState::Changed
77        };
78
79        self.last_match_state.set(Some(matches));
80        result
81    }
82
83    pub(crate) fn evaluate(&self) -> bool {
84        let quirks_mode = self.document.quirks_mode();
85        self.media_query_list.evaluate(
86            self.document.window().layout().device(),
87            quirks_mode,
88            &mut CustomMediaEvaluator::none(),
89        )
90    }
91}
92
93impl MediaQueryListMethods<crate::DomTypeHolder> for MediaQueryList {
94    /// <https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-media>
95    fn Media(&self) -> DOMString {
96        self.media_query_list.to_css_string().into()
97    }
98
99    /// <https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-matches>
100    fn Matches(&self) -> bool {
101        match self.last_match_state.get() {
102            None => self.evaluate(),
103            Some(state) => state,
104        }
105    }
106
107    /// <https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-addlistener>
108    fn AddListener(&self, listener: Option<RootedCallback<EventListener>>) {
109        let event_listener_options = RootedTraceableBox::new(AddEventListenerOptions {
110            parent: EventListenerOptions { capture: false },
111            once: false,
112            passive: None,
113            signal: None,
114        });
115        self.upcast::<EventTarget>().add_event_listener(
116            DOMString::from_static("change"),
117            listener,
118            &event_listener_options,
119        );
120    }
121
122    /// <https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-removelistener>
123    fn RemoveListener(&self, listener: Option<RootedCallback<EventListener>>) {
124        self.upcast::<EventTarget>().remove_event_listener(
125            DOMString::from_static("change"),
126            listener.as_deref(),
127            &EventListenerOptions { capture: false },
128        );
129    }
130
131    // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-onchange
132    event_handler!(change, GetOnchange, SetOnchange);
133}