From a685297bf3696789cef09dc406de4d8bb0d6c526 Mon Sep 17 00:00:00 2001 From: Zach Hoffman Date: Mon, 26 Dec 2022 22:49:46 +0000 Subject: [PATCH] style: Implement the color-gamut media feature Differential Revision: https://phabricator.services.mozilla.com/D165385 --- components/style/gecko/media_features.rs | 41 +++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/components/style/gecko/media_features.rs b/components/style/gecko/media_features.rs index 778a57cd95d..8f56f185d2a 100644 --- a/components/style/gecko/media_features.rs +++ b/components/style/gecko/media_features.rs @@ -6,6 +6,7 @@ use crate::gecko_bindings::bindings; use crate::gecko_bindings::structs; +use crate::gecko_bindings::structs::ScreenColorGamut; use crate::media_queries::{Device, MediaType}; use crate::queries::feature::{AllowsRanges, Evaluator, FeatureFlags, QueryFeatureDescription}; use crate::queries::values::Orientation; @@ -141,6 +142,38 @@ fn eval_monochrome(context: &Context) -> u32 { 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) -> 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 fn eval_resolution(context: &Context) -> Resolution { let resolution_dppx = @@ -585,7 +618,7 @@ macro_rules! bool_pref_feature { /// to support new types in these entries and (2) ensuring that either /// nsPresContext::MediaFeatureValuesChanged is called when the value that /// would be returned by the evaluator function could change. -pub static MEDIA_FEATURES: [QueryFeatureDescription; 63] = [ +pub static MEDIA_FEATURES: [QueryFeatureDescription; 64] = [ feature!( atom!("width"), AllowsRanges::Yes, @@ -697,6 +730,12 @@ pub static MEDIA_FEATURES: [QueryFeatureDescription; 63] = [ Evaluator::Integer(eval_monochrome), FeatureFlags::empty(), ), + feature!( + atom!("color-gamut"), + AllowsRanges::No, + keyword_evaluator!(eval_color_gamut, ColorGamut), + FeatureFlags::empty(), + ), feature!( atom!("prefers-reduced-motion"), AllowsRanges::No,