Skip to content

Commit

Permalink
Enable prefers-color-scheme (#93)
Browse files Browse the repository at this point in the history
* Enable prefers-color-scheme for Servo

Signed-off-by: Nico Burns <[email protected]>

* Dedup PrefersColorScheme type

---------

Signed-off-by: Nico Burns <[email protected]>
  • Loading branch information
nicoburns authored Nov 29, 2024
1 parent 4eb9da3 commit 16c1b68
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions atoms/static_atoms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ play
playing
popstate
postershown
prefers-color-scheme
print
progress
radio
Expand Down
11 changes: 1 addition & 10 deletions style/gecko/media_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::gecko_bindings::bindings;
use crate::gecko_bindings::structs;
use crate::media_queries::{Device, MediaType};
use crate::queries::feature::{AllowsRanges, Evaluator, FeatureFlags, QueryFeatureDescription};
use crate::queries::values::Orientation;
use crate::queries::values::{Orientation, PrefersColorScheme};
use crate::values::computed::{CSSPixelLength, Context, Ratio, Resolution};
use crate::values::specified::color::ForcedColors;
use crate::values::AtomString;
Expand Down Expand Up @@ -191,15 +191,6 @@ enum PrefersReducedTransparency {
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,
}

/// Values for the dynamic-range and video-dynamic-range media features.
/// https://drafts.csswg.org/mediaqueries-5/#dynamic-range
/// This implements PartialOrd so that lower values will correctly match
Expand Down
9 changes: 9 additions & 0 deletions style/queries/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ impl Orientation {
}
}
}

/// Values for the prefers-color-scheme media feature.
#[derive(Clone, Copy, Debug, FromPrimitive, Parse, PartialEq, ToCss, MallocSizeOf)]
#[repr(u8)]
#[allow(missing_docs)]
pub enum PrefersColorScheme {
Light,
Dark,
}
21 changes: 20 additions & 1 deletion style/servo/media_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::context::QuirksMode;
use crate::custom_properties::CssEnvironment;
use crate::font_metrics::FontMetrics;
use crate::queries::feature::{AllowsRanges, Evaluator, FeatureFlags, QueryFeatureDescription};
use crate::queries::values::PrefersColorScheme;
use crate::logical_geometry::WritingMode;
use crate::media_queries::MediaType;
use crate::properties::style_structs::Font;
Expand Down Expand Up @@ -86,6 +87,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,
Expand All @@ -106,6 +110,7 @@ impl Device {
device_pixel_ratio: Scale<f32, CSSPixel, DevicePixel>,
font_metrics_provider: Box<dyn FontMetricsProvider>,
default_computed_values: Arc<ComputedValues>,
prefers_color_scheme: PrefersColorScheme,
) -> Device {
Device {
media_type,
Expand All @@ -118,6 +123,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,
Expand Down Expand Up @@ -357,8 +363,15 @@ fn eval_device_pixel_ratio(context: &Context) -> f32 {
eval_resolution(context).dppx()
}

fn eval_prefers_color_scheme(context: &Context, query_value: Option<PrefersColorScheme>) -> 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,
Expand Down Expand Up @@ -389,4 +402,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(),
),
];

0 comments on commit 16c1b68

Please sign in to comment.