Skip to main content

style/servo/
media_features.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
5//! Servo's media feature list and evaluator.
6
7use crate::derives::*;
8use crate::queries::feature::{AllowsRanges, Evaluator, FeatureFlags, QueryFeatureDescription};
9use crate::queries::values::{
10    Orientation, PrefersColorScheme, PrefersContrast, PrefersReducedMotion,
11    PrefersReducedTransparency,
12};
13use crate::values::computed::{CSSPixelLength, Context, Ratio, Resolution};
14use std::fmt::Debug;
15
16/// https://drafts.csswg.org/mediaqueries-4/#width
17fn eval_width(context: &Context) -> CSSPixelLength {
18    CSSPixelLength::new(context.device().au_viewport_size().width.to_f32_px())
19}
20
21/// https://drafts.csswg.org/mediaqueries-4/#height
22fn eval_height(context: &Context) -> CSSPixelLength {
23    CSSPixelLength::new(context.device().au_viewport_size().height.to_f32_px())
24}
25
26/// https://drafts.csswg.org/mediaqueries-4/#device-width
27fn eval_device_width(context: &Context) -> CSSPixelLength {
28    let device = context.device();
29    let scaled = device.device_size() / device.device_pixel_ratio();
30    CSSPixelLength::new(scaled.width)
31}
32
33/// https://drafts.csswg.org/mediaqueries-4/#device-height
34fn eval_device_height(context: &Context) -> CSSPixelLength {
35    let device = context.device();
36    let scaled = device.device_size() / device.device_pixel_ratio();
37    CSSPixelLength::new(scaled.height)
38}
39
40/// <https://drafts.csswg.org/mediaqueries-4/#device-aspect-ratio>
41fn eval_device_aspect_ratio(context: &Context) -> Ratio {
42    let size = context.device().device_size();
43    Ratio::new(size.width, size.height)
44}
45
46/// https://drafts.csswg.org/mediaqueries-4/#orientation
47fn eval_orientation(context: &Context, value: Option<Orientation>) -> bool {
48    Orientation::eval(context.device().au_viewport_size(), value)
49}
50
51#[derive(Clone, Copy, Debug, FromPrimitive, Parse, ToCss)]
52#[repr(u8)]
53enum Scan {
54    Progressive,
55    Interlace,
56}
57
58/// https://drafts.csswg.org/mediaqueries-4/#scan
59fn eval_scan(_: &Context, _: Option<Scan>) -> bool {
60    // Since we doesn't support the 'tv' media type, the 'scan' feature never
61    // matches.
62    false
63}
64
65/// https://drafts.csswg.org/mediaqueries-4/#resolution
66fn eval_resolution(context: &Context) -> Resolution {
67    Resolution::from_dppx(context.device().device_pixel_ratio().0)
68}
69
70/// https://compat.spec.whatwg.org/#css-media-queries-webkit-device-pixel-ratio
71fn eval_device_pixel_ratio(context: &Context) -> f32 {
72    eval_resolution(context).dppx()
73}
74
75fn eval_prefers_color_scheme(context: &Context, query_value: Option<PrefersColorScheme>) -> bool {
76    match query_value {
77        Some(v) => context.device().color_scheme() == v,
78        None => true,
79    }
80}
81
82fn eval_prefers_contrast(context: &Context, query_value: Option<PrefersContrast>) -> bool {
83    let contrast = context.device().contrast();
84    match query_value {
85        Some(v) => contrast == v,
86        None => contrast != PrefersContrast::NoPreference,
87    }
88}
89
90fn eval_prefers_reduced_motion(
91    context: &Context,
92    query_value: Option<PrefersReducedMotion>,
93) -> bool {
94    let reduced_motion = context.device().reduced_motion();
95    match query_value {
96        Some(v) => reduced_motion == v,
97        None => reduced_motion != PrefersReducedMotion::NoPreference,
98    }
99}
100
101fn eval_prefers_reduced_transparency(
102    context: &Context,
103    query_value: Option<PrefersReducedTransparency>,
104) -> bool {
105    let reduced_transparency = context.device().reduced_transparency();
106    match query_value {
107        Some(v) => reduced_transparency == v,
108        None => reduced_transparency != PrefersReducedTransparency::NoPreference,
109    }
110}
111
112bitflags! {
113    /// https://drafts.csswg.org/mediaqueries-4/#mf-interaction
114    #[derive(Debug, Clone, Copy, PartialEq)]
115    pub struct PointerCapabilities: u8 {
116        /// The input mechanism includes a pointing device of limited accuracy, such as a finger on a touchscreen.
117        const COARSE = 0b001;
118        /// The input mechanism includes an accurate pointing device, such as a mouse.
119        const FINE = 0b010;
120        /// The input mechanism can conveniently hover over elements.
121        const HOVER = 0b100;
122    }
123}
124
125impl Default for PointerCapabilities {
126    #[cfg(any(target_os = "ios", target_os = "android", target_env = "ohos"))]
127    fn default() -> Self {
128        PointerCapabilities::COARSE
129    }
130    #[cfg(not(any(target_os = "ios", target_os = "android", target_env = "ohos")))]
131    fn default() -> Self {
132        PointerCapabilities::FINE | PointerCapabilities::HOVER
133    }
134}
135
136#[derive(Clone, Copy, Debug, FromPrimitive, Parse, ToCss)]
137#[repr(u8)]
138enum Pointer {
139    None,
140    Coarse,
141    Fine,
142}
143
144fn eval_pointer_capabilities(
145    query_value: Option<Pointer>,
146    pointer_capabilities: PointerCapabilities,
147) -> bool {
148    match query_value {
149        None => !pointer_capabilities.is_empty(),
150        Some(Pointer::None) => pointer_capabilities.is_empty(),
151        Some(Pointer::Coarse) => pointer_capabilities.intersects(PointerCapabilities::COARSE),
152        Some(Pointer::Fine) => pointer_capabilities.intersects(PointerCapabilities::FINE),
153    }
154}
155
156/// https://drafts.csswg.org/mediaqueries-4/#pointer
157fn eval_pointer(context: &Context, query_value: Option<Pointer>) -> bool {
158    eval_pointer_capabilities(query_value, context.device().primary_pointer_capabilities())
159}
160
161/// https://drafts.csswg.org/mediaqueries-4/#descdef-media-any-pointer
162fn eval_any_pointer(context: &Context, query_value: Option<Pointer>) -> bool {
163    eval_pointer_capabilities(query_value, context.device().all_pointer_capabilities())
164}
165
166#[derive(Clone, Copy, Debug, FromPrimitive, Parse, ToCss)]
167#[repr(u8)]
168enum Hover {
169    None,
170    Hover,
171}
172
173fn eval_hover_capabilities(
174    query_value: Option<Hover>,
175    pointer_capabilities: PointerCapabilities,
176) -> bool {
177    let can_hover = pointer_capabilities.intersects(PointerCapabilities::HOVER);
178    match query_value {
179        Some(Hover::None) => !can_hover,
180        Some(Hover::Hover) => can_hover,
181        None => return can_hover,
182    }
183}
184
185/// https://drafts.csswg.org/mediaqueries-4/#hover
186fn eval_hover(context: &Context, query_value: Option<Hover>) -> bool {
187    eval_hover_capabilities(query_value, context.device().primary_pointer_capabilities())
188}
189
190/// https://drafts.csswg.org/mediaqueries-4/#descdef-media-any-hover
191fn eval_any_hover(context: &Context, query_value: Option<Hover>) -> bool {
192    eval_hover_capabilities(query_value, context.device().all_pointer_capabilities())
193}
194
195/// <https://drafts.csswg.org/mediaqueries-4/#aspect-ratio>
196fn eval_aspect_ratio(context: &Context) -> Ratio {
197    let size = context.device().au_viewport_size();
198    Ratio::new(size.width.0 as f32, size.height.0 as f32)
199}
200
201/// A list with all the media features that Servo supports.
202pub static MEDIA_FEATURES: [QueryFeatureDescription; 19] = [
203    feature!(
204        atom!("width"),
205        AllowsRanges::Yes,
206        Evaluator::Length(eval_width),
207        FeatureFlags::VIEWPORT_DEPENDENT,
208    ),
209    feature!(
210        atom!("height"),
211        AllowsRanges::Yes,
212        Evaluator::Length(eval_height),
213        FeatureFlags::VIEWPORT_DEPENDENT,
214    ),
215    feature!(
216        atom!("orientation"),
217        AllowsRanges::No,
218        keyword_evaluator!(eval_orientation, Orientation),
219        FeatureFlags::VIEWPORT_DEPENDENT,
220    ),
221    feature!(
222        atom!("pointer"),
223        AllowsRanges::No,
224        keyword_evaluator!(eval_pointer, Pointer),
225        FeatureFlags::empty(),
226    ),
227    feature!(
228        atom!("any-pointer"),
229        AllowsRanges::No,
230        keyword_evaluator!(eval_any_pointer, Pointer),
231        FeatureFlags::empty(),
232    ),
233    feature!(
234        atom!("hover"),
235        AllowsRanges::No,
236        keyword_evaluator!(eval_hover, Hover),
237        FeatureFlags::empty(),
238    ),
239    feature!(
240        atom!("any-hover"),
241        AllowsRanges::No,
242        keyword_evaluator!(eval_any_hover, Hover),
243        FeatureFlags::empty(),
244    ),
245    feature!(
246        atom!("aspect-ratio"),
247        AllowsRanges::Yes,
248        Evaluator::NumberRatio(eval_aspect_ratio),
249        FeatureFlags::VIEWPORT_DEPENDENT,
250    ),
251    feature!(
252        atom!("device-width"),
253        AllowsRanges::Yes,
254        Evaluator::Length(eval_device_width),
255        FeatureFlags::empty(),
256    ),
257    feature!(
258        atom!("device-height"),
259        AllowsRanges::Yes,
260        Evaluator::Length(eval_device_height),
261        FeatureFlags::empty(),
262    ),
263    feature!(
264        atom!("device-aspect-ratio"),
265        AllowsRanges::Yes,
266        Evaluator::NumberRatio(eval_device_aspect_ratio),
267        FeatureFlags::empty(),
268    ),
269    feature!(
270        atom!("scan"),
271        AllowsRanges::No,
272        keyword_evaluator!(eval_scan, Scan),
273        FeatureFlags::empty(),
274    ),
275    feature!(
276        atom!("resolution"),
277        AllowsRanges::Yes,
278        Evaluator::Resolution(eval_resolution),
279        FeatureFlags::empty(),
280    ),
281    feature!(
282        atom!("device-pixel-ratio"),
283        AllowsRanges::Yes,
284        Evaluator::Float(eval_device_pixel_ratio),
285        FeatureFlags::WEBKIT_PREFIX,
286    ),
287    feature!(
288        atom!("-moz-device-pixel-ratio"),
289        AllowsRanges::Yes,
290        Evaluator::Float(eval_device_pixel_ratio),
291        FeatureFlags::empty(),
292    ),
293    feature!(
294        atom!("prefers-color-scheme"),
295        AllowsRanges::No,
296        keyword_evaluator!(eval_prefers_color_scheme, PrefersColorScheme),
297        FeatureFlags::empty(),
298    ),
299    feature!(
300        atom!("prefers-contrast"),
301        AllowsRanges::No,
302        keyword_evaluator!(eval_prefers_contrast, PrefersContrast),
303        FeatureFlags::empty(),
304    ),
305    feature!(
306        atom!("prefers-reduced-motion"),
307        AllowsRanges::No,
308        keyword_evaluator!(eval_prefers_reduced_motion, PrefersReducedMotion),
309        FeatureFlags::empty(),
310    ),
311    feature!(
312        atom!("prefers-reduced-transparency"),
313        AllowsRanges::No,
314        keyword_evaluator!(
315            eval_prefers_reduced_transparency,
316            PrefersReducedTransparency
317        ),
318        FeatureFlags::empty(),
319    ),
320];