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
This commit is contained in:
Emilio Cobos Álvarez
2025-05-17 15:05:19 +00:00
committed by ealvarez@mozilla.com
parent 88ed80b1ee
commit f9b667bdc0
3 changed files with 47 additions and 53 deletions

View File

@@ -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

View File

@@ -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<C> GenericCaretColor<C> {
}
pub use self::GenericCaretColor as CaretColor;
/// A light-dark(<light>, <dark>) function.
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToShmem, ToCss)]
#[css(function, comma)]
#[repr(C)]
pub struct GenericLightDark<T> {
/// The value returned when using a light theme.
pub light: T,
/// The value returned when using a dark theme.
pub dark: T,
}
impl<T> GenericLightDark<T> {
/// 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<T, ParseError<'i>>,
) -> Result<Self, ParseError<'i>> {
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<T: ToComputedValue> GenericLightDark<T> {
/// 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)
}
}

View File

@@ -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<ColorMix>),
/// A light-dark() color.
LightDark(Box<LightDark>),
LightDark(Box<GenericLightDark<Self>>),
/// Quirksmode-only rule for inheriting color from the body
#[cfg(feature = "gecko")]
InheritFromBodyQuirk,
}
/// A light-dark(<light-color>, <dark-color>) function.
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToShmem, ToCss)]
#[css(function, comma)]
pub struct LightDark {
/// The <color> that is returned when using a light theme.
pub light: Color,
/// The <color> 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<Self, ParseError<'i>> {
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<AbsoluteColor> 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)));
}