style: Implement the color-gamut media feature

Differential Revision: https://phabricator.services.mozilla.com/D165385
This commit is contained in:
Zach Hoffman 2022-12-26 22:49:46 +00:00 committed by Martin Robinson
parent 65fbb16bb4
commit a685297bf3

View file

@ -6,6 +6,7 @@
use crate::gecko_bindings::bindings; use crate::gecko_bindings::bindings;
use crate::gecko_bindings::structs; use crate::gecko_bindings::structs;
use crate::gecko_bindings::structs::ScreenColorGamut;
use crate::media_queries::{Device, MediaType}; use crate::media_queries::{Device, MediaType};
use crate::queries::feature::{AllowsRanges, Evaluator, FeatureFlags, QueryFeatureDescription}; use crate::queries::feature::{AllowsRanges, Evaluator, FeatureFlags, QueryFeatureDescription};
use crate::queries::values::Orientation; use crate::queries::values::Orientation;
@ -141,6 +142,38 @@ fn eval_monochrome(context: &Context) -> u32 {
unsafe { bindings::Gecko_MediaFeatures_GetMonochromeBitsPerPixel(context.device().document()) } unsafe { bindings::Gecko_MediaFeatures_GetMonochromeBitsPerPixel(context.device().document()) }
} }
/// Values for the color-gamut media feature.
/// This implements PartialOrd so that lower values will correctly match
/// higher capabilities.
#[derive(Clone, Copy, Debug, FromPrimitive, Parse, PartialEq, PartialOrd, ToCss)]
#[repr(u8)]
enum ColorGamut {
/// The sRGB gamut.
Srgb,
/// The gamut specified by the Display P3 Color Space.
P3,
/// The gamut specified by the ITU-R Recommendation BT.2020 Color Space.
Rec2020,
}
/// https://drafts.csswg.org/mediaqueries-4/#color-gamut
fn eval_color_gamut(context: &Context, query_value: Option<ColorGamut>) -> bool {
let query_value = match query_value {
Some(v) => v,
None => return false,
};
let color_gamut =
unsafe { bindings::Gecko_MediaFeatures_ColorGamut(context.device().document()) };
// Match if our color gamut is at least as wide as the query value
query_value <=
match color_gamut {
// EndGuard_ is not a valid color gamut, so the default color-gamut is used.
ScreenColorGamut::Srgb | ScreenColorGamut::EndGuard_ => ColorGamut::Srgb,
ScreenColorGamut::P3 => ColorGamut::P3,
ScreenColorGamut::Rec2020 => ColorGamut::Rec2020,
}
}
/// https://drafts.csswg.org/mediaqueries-4/#resolution /// https://drafts.csswg.org/mediaqueries-4/#resolution
fn eval_resolution(context: &Context) -> Resolution { fn eval_resolution(context: &Context) -> Resolution {
let resolution_dppx = let resolution_dppx =
@ -585,7 +618,7 @@ macro_rules! bool_pref_feature {
/// to support new types in these entries and (2) ensuring that either /// to support new types in these entries and (2) ensuring that either
/// nsPresContext::MediaFeatureValuesChanged is called when the value that /// nsPresContext::MediaFeatureValuesChanged is called when the value that
/// would be returned by the evaluator function could change. /// would be returned by the evaluator function could change.
pub static MEDIA_FEATURES: [QueryFeatureDescription; 63] = [ pub static MEDIA_FEATURES: [QueryFeatureDescription; 64] = [
feature!( feature!(
atom!("width"), atom!("width"),
AllowsRanges::Yes, AllowsRanges::Yes,
@ -697,6 +730,12 @@ pub static MEDIA_FEATURES: [QueryFeatureDescription; 63] = [
Evaluator::Integer(eval_monochrome), Evaluator::Integer(eval_monochrome),
FeatureFlags::empty(), FeatureFlags::empty(),
), ),
feature!(
atom!("color-gamut"),
AllowsRanges::No,
keyword_evaluator!(eval_color_gamut, ColorGamut),
FeatureFlags::empty(),
),
feature!( feature!(
atom!("prefers-reduced-motion"), atom!("prefers-reduced-motion"),
AllowsRanges::No, AllowsRanges::No,