Skip to content

Commit 16c1b68

Browse files
authored
Enable prefers-color-scheme (#93)
* Enable prefers-color-scheme for Servo Signed-off-by: Nico Burns <[email protected]> * Dedup PrefersColorScheme type --------- Signed-off-by: Nico Burns <[email protected]>
1 parent 4eb9da3 commit 16c1b68

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

atoms/static_atoms.txt

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ play
105105
playing
106106
popstate
107107
postershown
108+
prefers-color-scheme
108109
print
109110
progress
110111
radio

style/gecko/media_features.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::gecko_bindings::bindings;
88
use crate::gecko_bindings::structs;
99
use crate::media_queries::{Device, MediaType};
1010
use crate::queries::feature::{AllowsRanges, Evaluator, FeatureFlags, QueryFeatureDescription};
11-
use crate::queries::values::Orientation;
11+
use crate::queries::values::{Orientation, PrefersColorScheme};
1212
use crate::values::computed::{CSSPixelLength, Context, Ratio, Resolution};
1313
use crate::values::specified::color::ForcedColors;
1414
use crate::values::AtomString;
@@ -191,15 +191,6 @@ enum PrefersReducedTransparency {
191191
Reduce,
192192
}
193193

194-
/// Values for the prefers-color-scheme media feature.
195-
#[derive(Clone, Copy, Debug, FromPrimitive, Parse, PartialEq, ToCss)]
196-
#[repr(u8)]
197-
#[allow(missing_docs)]
198-
pub enum PrefersColorScheme {
199-
Light,
200-
Dark,
201-
}
202-
203194
/// Values for the dynamic-range and video-dynamic-range media features.
204195
/// https://drafts.csswg.org/mediaqueries-5/#dynamic-range
205196
/// This implements PartialOrd so that lower values will correctly match

style/queries/values.rs

+9
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,12 @@ impl Orientation {
3434
}
3535
}
3636
}
37+
38+
/// Values for the prefers-color-scheme media feature.
39+
#[derive(Clone, Copy, Debug, FromPrimitive, Parse, PartialEq, ToCss, MallocSizeOf)]
40+
#[repr(u8)]
41+
#[allow(missing_docs)]
42+
pub enum PrefersColorScheme {
43+
Light,
44+
Dark,
45+
}

style/servo/media_queries.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::context::QuirksMode;
99
use crate::custom_properties::CssEnvironment;
1010
use crate::font_metrics::FontMetrics;
1111
use crate::queries::feature::{AllowsRanges, Evaluator, FeatureFlags, QueryFeatureDescription};
12+
use crate::queries::values::PrefersColorScheme;
1213
use crate::logical_geometry::WritingMode;
1314
use crate::media_queries::MediaType;
1415
use crate::properties::style_structs::Font;
@@ -86,6 +87,9 @@ pub struct Device {
8687
/// Whether any styles computed in the document relied on the viewport size.
8788
#[ignore_malloc_size_of = "Pure stack type"]
8889
used_viewport_units: AtomicBool,
90+
/// Whether the user prefers light mode or dark mode
91+
#[ignore_malloc_size_of = "Pure stack type"]
92+
prefers_color_scheme: PrefersColorScheme,
8993
/// The CssEnvironment object responsible of getting CSS environment
9094
/// variables.
9195
environment: CssEnvironment,
@@ -106,6 +110,7 @@ impl Device {
106110
device_pixel_ratio: Scale<f32, CSSPixel, DevicePixel>,
107111
font_metrics_provider: Box<dyn FontMetricsProvider>,
108112
default_computed_values: Arc<ComputedValues>,
113+
prefers_color_scheme: PrefersColorScheme,
109114
) -> Device {
110115
Device {
111116
media_type,
@@ -118,6 +123,7 @@ impl Device {
118123
used_root_line_height: AtomicBool::new(false),
119124
used_font_metrics: AtomicBool::new(false),
120125
used_viewport_units: AtomicBool::new(false),
126+
prefers_color_scheme,
121127
environment: CssEnvironment,
122128
font_metrics_provider,
123129
default_computed_values,
@@ -357,8 +363,15 @@ fn eval_device_pixel_ratio(context: &Context) -> f32 {
357363
eval_resolution(context).dppx()
358364
}
359365

366+
fn eval_prefers_color_scheme(context: &Context, query_value: Option<PrefersColorScheme>) -> bool {
367+
match query_value {
368+
Some(v) => context.device().prefers_color_scheme == v,
369+
None => true,
370+
}
371+
}
372+
360373
/// A list with all the media features that Servo supports.
361-
pub static MEDIA_FEATURES: [QueryFeatureDescription; 5] = [
374+
pub static MEDIA_FEATURES: [QueryFeatureDescription; 6] = [
362375
feature!(
363376
atom!("width"),
364377
AllowsRanges::Yes,
@@ -389,4 +402,10 @@ pub static MEDIA_FEATURES: [QueryFeatureDescription; 5] = [
389402
Evaluator::Float(eval_device_pixel_ratio),
390403
FeatureFlags::empty(),
391404
),
405+
feature!(
406+
atom!("prefers-color-scheme"),
407+
AllowsRanges::No,
408+
keyword_evaluator!(eval_prefers_color_scheme, PrefersColorScheme),
409+
FeatureFlags::empty(),
410+
),
392411
];

0 commit comments

Comments
 (0)