From f9b667bdc0bb91add46c6df8bd32b3eaad1b5bff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 17 May 2025 15:05:19 +0000 Subject: [PATCH] Bug 1966890 - Factor out the light-dark() function. r=dshin Remove the light-dark() pref while at it. Differential Revision: https://phabricator.services.mozilla.com/D249787 --- modules/libpref/init/StaticPrefList.yaml | 7 --- .../components/style/values/generics/color.rs | 45 ++++++++++++++++- .../style/values/specified/color.rs | 48 ++----------------- 3 files changed, 47 insertions(+), 53 deletions(-) diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml index c5ea8eba2380..f320897f86c6 100644 --- a/modules/libpref/init/StaticPrefList.yaml +++ b/modules/libpref/init/StaticPrefList.yaml @@ -9751,13 +9751,6 @@ mirror: always rust: true -# Is support for light-dark() on content enabled? -- name: layout.css.light-dark.enabled - type: RelaxedAtomicBool - value: true - mirror: always - rust: true - # Is support for fit-content() enabled? - name: layout.css.fit-content-function.enabled type: RelaxedAtomicBool diff --git a/servo/components/style/values/generics/color.rs b/servo/components/style/values/generics/color.rs index eaf29f8f2b37..efc926faeca7 100644 --- a/servo/components/style/values/generics/color.rs +++ b/servo/components/style/values/generics/color.rs @@ -5,7 +5,8 @@ //! Generic types for color properties. use crate::color::{mix::ColorInterpolationMethod, AbsoluteColor, ColorFunction}; -use crate::values::specified::percentage::ToPercentage; +use crate::parser::ParserContext; +use crate::values::{specified::percentage::ToPercentage, computed::ToComputedValue, Parser, ParseError}; use std::fmt::{self, Write}; use style_traits::{CssWriter, ToCss}; @@ -208,3 +209,45 @@ impl GenericCaretColor { } pub use self::GenericCaretColor as CaretColor; + +/// A light-dark(, ) function. +#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToShmem, ToCss)] +#[css(function, comma)] +#[repr(C)] +pub struct GenericLightDark { + /// The value returned when using a light theme. + pub light: T, + /// The value returned when using a dark theme. + pub dark: T, +} + +impl GenericLightDark { + /// Parse the light-dark() function. + pub fn parse_with<'i>( + context: &ParserContext, + input: &mut Parser<'i, '_>, + mut parse_one: impl FnMut(&ParserContext, &mut Parser<'i, '_>) -> Result>, + ) -> Result> { + input.expect_function_matching("light-dark")?; + input.parse_nested_block(|input| { + let light = parse_one(context, input)?; + input.expect_comma()?; + let dark = parse_one(context, input)?; + Ok(Self { light, dark }) + }) + } +} + +impl GenericLightDark { + /// Choose the light or dark version of this value for computation purposes, and compute it. + pub fn compute(&self, cx: &crate::values::computed::Context) -> T::ComputedValue { + let dark = cx.device().is_dark_color_scheme(cx.builder.color_scheme); + if cx.for_non_inherited_property { + cx.rule_cache_conditions + .borrow_mut() + .set_color_scheme_dependency(cx.builder.color_scheme); + } + let chosen = if dark { &self.dark } else { &self.light }; + chosen.to_computed_value(cx) + } +} diff --git a/servo/components/style/values/specified/color.rs b/servo/components/style/values/specified/color.rs index bd5e0f9e80c3..e89cdf193df5 100644 --- a/servo/components/style/values/specified/color.rs +++ b/servo/components/style/values/specified/color.rs @@ -11,7 +11,7 @@ use crate::media_queries::Device; use crate::parser::{Parse, ParserContext}; use crate::values::computed::{Color as ComputedColor, Context, ToComputedValue}; use crate::values::generics::color::{ - ColorMixFlags, GenericCaretColor, GenericColorMix, GenericColorOrAuto, + ColorMixFlags, GenericCaretColor, GenericColorMix, GenericColorOrAuto, GenericLightDark }; use crate::values::specified::Percentage; use crate::values::{normalize, CustomIdent}; @@ -124,54 +124,12 @@ pub enum Color { /// A color mix. ColorMix(Box), /// A light-dark() color. - LightDark(Box), + LightDark(Box>), /// Quirksmode-only rule for inheriting color from the body #[cfg(feature = "gecko")] InheritFromBodyQuirk, } -/// A light-dark(, ) function. -#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToShmem, ToCss)] -#[css(function, comma)] -pub struct LightDark { - /// The that is returned when using a light theme. - pub light: Color, - /// The that is returned when using a dark theme. - pub dark: Color, -} - -impl LightDark { - fn compute(&self, cx: &Context) -> ComputedColor { - let dark = cx.device().is_dark_color_scheme(cx.builder.color_scheme); - if cx.for_non_inherited_property { - cx.rule_cache_conditions - .borrow_mut() - .set_color_scheme_dependency(cx.builder.color_scheme); - } - let used = if dark { &self.dark } else { &self.light }; - used.to_computed_value(cx) - } - - fn parse<'i, 't>( - context: &ParserContext, - input: &mut Parser<'i, 't>, - preserve_authored: PreserveAuthored, - ) -> Result> { - let enabled = - context.chrome_rules_enabled() || static_prefs::pref!("layout.css.light-dark.enabled"); - if !enabled { - return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)); - } - input.expect_function_matching("light-dark")?; - input.parse_nested_block(|input| { - let light = Color::parse_internal(context, input, preserve_authored)?; - input.expect_comma()?; - let dark = Color::parse_internal(context, input, preserve_authored)?; - Ok(LightDark { light, dark }) - }) - } -} - impl From for Color { #[inline] fn from(value: AbsoluteColor) -> Self { @@ -483,7 +441,7 @@ impl Color { return Ok(Color::ColorMix(Box::new(mix))); } - if let Ok(ld) = input.try_parse(|i| LightDark::parse(context, i, preserve_authored)) + if let Ok(ld) = input.try_parse(|i| GenericLightDark::parse_with(context, i, |context, i| Self::parse_internal(context, i, preserve_authored))) { return Ok(Color::LightDark(Box::new(ld))); }