From 6ee9405ab6b179d9482dd48999f5aed82e360630 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Mon, 18 Nov 2024 13:07:54 +1300 Subject: [PATCH] Enable prefers-color-scheme for Servo Signed-off-by: Nico Burns --- atoms/static_atoms.txt | 1 + style/servo/media_queries.rs | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/atoms/static_atoms.txt b/atoms/static_atoms.txt index 82065c877..d658fcf1c 100644 --- a/atoms/static_atoms.txt +++ b/atoms/static_atoms.txt @@ -105,6 +105,7 @@ play playing popstate postershown +prefers-color-scheme print progress radio diff --git a/style/servo/media_queries.rs b/style/servo/media_queries.rs index f3dda0812..6ba1f6cbb 100644 --- a/style/servo/media_queries.rs +++ b/style/servo/media_queries.rs @@ -86,6 +86,9 @@ pub struct Device { /// Whether any styles computed in the document relied on the viewport size. #[ignore_malloc_size_of = "Pure stack type"] used_viewport_units: AtomicBool, + /// Whether the user prefers light mode or dark mode + #[ignore_malloc_size_of = "Pure stack type"] + prefers_color_scheme: PrefersColorScheme, /// The CssEnvironment object responsible of getting CSS environment /// variables. environment: CssEnvironment, @@ -106,6 +109,7 @@ impl Device { device_pixel_ratio: Scale, font_metrics_provider: Box, default_computed_values: Arc, + prefers_color_scheme: PrefersColorScheme, ) -> Device { Device { media_type, @@ -118,6 +122,7 @@ impl Device { used_root_line_height: AtomicBool::new(false), used_font_metrics: AtomicBool::new(false), used_viewport_units: AtomicBool::new(false), + prefers_color_scheme, environment: CssEnvironment, font_metrics_provider, default_computed_values, @@ -357,8 +362,24 @@ fn eval_device_pixel_ratio(context: &Context) -> f32 { eval_resolution(context).dppx() } +/// 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, +} + +fn eval_prefers_color_scheme(context: &Context, query_value: Option) -> bool { + match query_value { + Some(v) => context.device().prefers_color_scheme == v, + None => true, + } +} + /// A list with all the media features that Servo supports. -pub static MEDIA_FEATURES: [QueryFeatureDescription; 5] = [ +pub static MEDIA_FEATURES: [QueryFeatureDescription; 6] = [ feature!( atom!("width"), AllowsRanges::Yes, @@ -389,4 +410,10 @@ pub static MEDIA_FEATURES: [QueryFeatureDescription; 5] = [ Evaluator::Float(eval_device_pixel_ratio), FeatureFlags::empty(), ), + feature!( + atom!("prefers-color-scheme"), + AllowsRanges::No, + keyword_evaluator!(eval_prefers_color_scheme, PrefersColorScheme), + FeatureFlags::empty(), + ), ];