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::PrefersColorScheme;
10use crate::values::computed::{CSSPixelLength, Context, Resolution};
11use std::fmt::Debug;
12
13/// https://drafts.csswg.org/mediaqueries-4/#width
14fn eval_width(context: &Context) -> CSSPixelLength {
15    CSSPixelLength::new(context.device().au_viewport_size().width.to_f32_px())
16}
17
18#[derive(Clone, Copy, Debug, FromPrimitive, Parse, ToCss)]
19#[repr(u8)]
20enum Scan {
21    Progressive,
22    Interlace,
23}
24
25/// https://drafts.csswg.org/mediaqueries-4/#scan
26fn eval_scan(_: &Context, _: Option<Scan>) -> bool {
27    // Since we doesn't support the 'tv' media type, the 'scan' feature never
28    // matches.
29    false
30}
31
32/// https://drafts.csswg.org/mediaqueries-4/#resolution
33fn eval_resolution(context: &Context) -> Resolution {
34    Resolution::from_dppx(context.device().device_pixel_ratio().0)
35}
36
37/// https://compat.spec.whatwg.org/#css-media-queries-webkit-device-pixel-ratio
38fn eval_device_pixel_ratio(context: &Context) -> f32 {
39    eval_resolution(context).dppx()
40}
41
42fn eval_prefers_color_scheme(context: &Context, query_value: Option<PrefersColorScheme>) -> bool {
43    match query_value {
44        Some(v) => context.device().color_scheme() == v,
45        None => true,
46    }
47}
48
49/// A list with all the media features that Servo supports.
50pub static MEDIA_FEATURES: [QueryFeatureDescription; 6] = [
51    feature!(
52        atom!("width"),
53        AllowsRanges::Yes,
54        Evaluator::Length(eval_width),
55        FeatureFlags::empty(),
56    ),
57    feature!(
58        atom!("scan"),
59        AllowsRanges::No,
60        keyword_evaluator!(eval_scan, Scan),
61        FeatureFlags::empty(),
62    ),
63    feature!(
64        atom!("resolution"),
65        AllowsRanges::Yes,
66        Evaluator::Resolution(eval_resolution),
67        FeatureFlags::empty(),
68    ),
69    feature!(
70        atom!("device-pixel-ratio"),
71        AllowsRanges::Yes,
72        Evaluator::Float(eval_device_pixel_ratio),
73        FeatureFlags::WEBKIT_PREFIX,
74    ),
75    feature!(
76        atom!("-moz-device-pixel-ratio"),
77        AllowsRanges::Yes,
78        Evaluator::Float(eval_device_pixel_ratio),
79        FeatureFlags::empty(),
80    ),
81    feature!(
82        atom!("prefers-color-scheme"),
83        AllowsRanges::No,
84        keyword_evaluator!(eval_prefers_color_scheme, PrefersColorScheme),
85        FeatureFlags::empty(),
86    ),
87];