From d6750de0c37025330fdac3e7b03d8d789da9e0ff Mon Sep 17 00:00:00 2001 From: Jonathan Kingston Date: Fri, 15 Feb 2019 21:40:35 +0100 Subject: [PATCH] style: Add support for CSS prefers-color-scheme media feature. Bug: 1494034 Reviewed-by: emilio --- components/style/cbindgen.toml | 1 + components/style/gecko/media_features.rs | 28 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/components/style/cbindgen.toml b/components/style/cbindgen.toml index 1a736eb1be9..50696b32e6d 100644 --- a/components/style/cbindgen.toml +++ b/components/style/cbindgen.toml @@ -57,6 +57,7 @@ include = [ "CursorKind", "Display", "DisplayMode", + "PrefersColorScheme", "ExtremumLength", "FillRule", "FontDisplay", diff --git a/components/style/gecko/media_features.rs b/components/style/gecko/media_features.rs index b6596c75de8..24f2009a3a8 100644 --- a/components/style/gecko/media_features.rs +++ b/components/style/gecko/media_features.rs @@ -280,6 +280,16 @@ enum PrefersReducedMotion { Reduce, } +/// Values for the prefers-color-scheme media feature. +#[derive(Clone, Copy, Debug, FromPrimitive, Parse, PartialEq, ToCss)] +#[repr(u8)] +#[allow(missing_docs)] +pub enum PrefersColorScheme { + Light, + Dark, + NoPreference, +} + /// https://drafts.csswg.org/mediaqueries-5/#prefers-reduced-motion fn eval_prefers_reduced_motion(device: &Device, query_value: Option) -> bool { let prefers_reduced = @@ -348,6 +358,16 @@ fn eval_overflow_inline(device: &Device, query_value: Option) -> } } +/// https://drafts.csswg.org/mediaqueries-5/#prefers-color-scheme +fn eval_prefers_color_scheme(device: &Device, query_value: Option) -> bool { + let prefers_color_scheme = + unsafe { bindings::Gecko_MediaFeatures_PrefersColorScheme(device.document()) }; + match query_value { + Some(v) => prefers_color_scheme == v, + None => prefers_color_scheme != PrefersColorScheme::NoPreference, + } +} + /// https://drafts.csswg.org/mediaqueries-4/#mf-interaction bitflags! { struct PointerCapabilities: u8 { @@ -526,7 +546,7 @@ lazy_static! { /// 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 ref MEDIA_FEATURES: [MediaFeatureDescription; 52] = [ + pub static ref MEDIA_FEATURES: [MediaFeatureDescription; 53] = [ feature!( atom!("width"), AllowsRanges::Yes, @@ -657,6 +677,12 @@ lazy_static! { keyword_evaluator!(eval_overflow_inline, OverflowInline), ParsingRequirements::empty(), ), + feature!( + atom!("prefers-color-scheme"), + AllowsRanges::No, + keyword_evaluator!(eval_prefers_color_scheme, PrefersColorScheme), + ParsingRequirements::empty(), + ), feature!( atom!("pointer"), AllowsRanges::No,