@@ -14,7 +14,9 @@ use crate::media_queries::MediaType;
1414use crate :: properties:: style_structs:: Font ;
1515use crate :: properties:: ComputedValues ;
1616use crate :: queries:: feature:: { AllowsRanges , Evaluator , FeatureFlags , QueryFeatureDescription } ;
17- use crate :: queries:: values:: PrefersColorScheme ;
17+ use crate :: queries:: values:: {
18+ PrefersColorScheme , PrefersContrast , PrefersReducedMotion , PrefersReducedTransparency ,
19+ } ;
1820use crate :: values:: computed:: font:: GenericFontFamily ;
1921use crate :: values:: computed:: {
2022 CSSPixelLength , Context , Length , LineHeight , NonNegativeLength , Resolution ,
@@ -117,6 +119,15 @@ pub struct Device {
117119 /// Whether the user prefers light mode or dark mode
118120 #[ ignore_malloc_size_of = "Pure stack type" ]
119121 prefers_color_scheme : PrefersColorScheme ,
122+ /// The users contrast preference
123+ #[ ignore_malloc_size_of = "Pure stack type" ]
124+ prefers_contrast : PrefersContrast ,
125+ /// The users motion preference
126+ #[ ignore_malloc_size_of = "Pure stack type" ]
127+ prefers_reduced_motion : PrefersReducedMotion ,
128+ /// The users transparency preference
129+ #[ ignore_malloc_size_of = "Pure stack type" ]
130+ prefers_reduced_transparency : PrefersReducedTransparency ,
120131 /// The CssEnvironment object responsible of getting CSS environment
121132 /// variables.
122133 environment : CssEnvironment ,
@@ -138,6 +149,9 @@ impl Device {
138149 font_metrics_provider : Box < dyn FontMetricsProvider > ,
139150 default_computed_values : Arc < ComputedValues > ,
140151 prefers_color_scheme : PrefersColorScheme ,
152+ prefers_contrast : PrefersContrast ,
153+ prefers_reduced_motion : PrefersReducedMotion ,
154+ prefers_reduced_transparency : PrefersReducedTransparency ,
141155 ) -> Device {
142156 let default_values =
143157 ComputedValues :: initial_values_with_font_override ( Font :: initial_values ( ) ) ;
@@ -160,6 +174,9 @@ impl Device {
160174 used_font_metrics : AtomicBool :: new ( false ) ,
161175 used_viewport_units : AtomicBool :: new ( false ) ,
162176 prefers_color_scheme,
177+ prefers_contrast,
178+ prefers_reduced_motion,
179+ prefers_reduced_transparency,
163180 environment : CssEnvironment ,
164181 font_metrics_provider,
165182 default_computed_values,
@@ -505,6 +522,51 @@ impl Device {
505522 false
506523 }
507524
525+ /// Set the [`PrefersContrast`] value on this [`Device`].
526+ ///
527+ /// Note that this does not update any associated `Stylist`. For this you must call
528+ /// `Stylist::media_features_change_changed_style` and
529+ /// `Stylist::force_stylesheet_origins_dirty`.
530+ pub fn set_prefers_contrast ( & mut self , new_prefers_contrast : PrefersContrast ) {
531+ self . prefers_contrast = new_prefers_contrast;
532+ }
533+
534+ /// Returns the preferred contrast of this [`Device`].
535+ pub fn prefers_contrast ( & self ) -> PrefersContrast {
536+ self . prefers_contrast
537+ }
538+
539+ /// Set the [`PrefersReducedMotion`] value on this [`Device`].
540+ ///
541+ /// Note that this does not update any associated `Stylist`. For this you must call
542+ /// `Stylist::media_features_change_changed_style` and
543+ /// `Stylist::force_stylesheet_origins_dirty`.
544+ pub fn set_prefers_reduced_motion ( & mut self , new_prefers_reduced_motion : PrefersReducedMotion ) {
545+ self . prefers_reduced_motion = new_prefers_reduced_motion;
546+ }
547+
548+ /// Returns the preferred contrast of this [`Device`].
549+ pub fn prefers_reduced_motion ( & self ) -> PrefersReducedMotion {
550+ self . prefers_reduced_motion
551+ }
552+
553+ /// Set the [`PrefersReducedTransparency`] value on this [`Device`].
554+ ///
555+ /// Note that this does not update any associated `Stylist`. For this you must call
556+ /// `Stylist::media_features_change_changed_style` and
557+ /// `Stylist::force_stylesheet_origins_dirty`.
558+ pub fn set_prefers_reduced_transparency (
559+ & mut self ,
560+ new_prefers_reduced_transparency : PrefersReducedTransparency ,
561+ ) {
562+ self . prefers_reduced_transparency = new_prefers_reduced_transparency;
563+ }
564+
565+ /// Returns the preferred contrast of this [`Device`].
566+ pub fn prefers_reduced_transparency ( & self ) -> PrefersReducedTransparency {
567+ self . prefers_reduced_transparency
568+ }
569+
508570 /// Returns safe area insets
509571 pub fn safe_area_insets ( & self ) -> SideOffsets2D < f32 , CSSPixel > {
510572 SideOffsets2D :: zero ( )
@@ -570,8 +632,38 @@ fn eval_prefers_color_scheme(context: &Context, query_value: Option<PrefersColor
570632 }
571633}
572634
635+ fn eval_prefers_contrast ( context : & Context , query_value : Option < PrefersContrast > ) -> bool {
636+ let prefers_contrast = context. device ( ) . prefers_contrast ;
637+ match query_value {
638+ Some ( v) => prefers_contrast == v,
639+ None => prefers_contrast != PrefersContrast :: NoPreference ,
640+ }
641+ }
642+
643+ fn eval_prefers_reduced_motion (
644+ context : & Context ,
645+ query_value : Option < PrefersReducedMotion > ,
646+ ) -> bool {
647+ let prefers_reduced_motion = context. device ( ) . prefers_reduced_motion ;
648+ match query_value {
649+ Some ( v) => prefers_reduced_motion == v,
650+ None => prefers_reduced_motion != PrefersReducedMotion :: NoPreference ,
651+ }
652+ }
653+
654+ fn eval_prefers_reduced_transparency (
655+ context : & Context ,
656+ query_value : Option < PrefersReducedTransparency > ,
657+ ) -> bool {
658+ let prefers_reduced_transparency = context. device ( ) . prefers_reduced_transparency ;
659+ match query_value {
660+ Some ( v) => prefers_reduced_transparency == v,
661+ None => prefers_reduced_transparency != PrefersReducedTransparency :: NoPreference ,
662+ }
663+ }
664+
573665/// A list with all the media features that Servo supports.
574- pub static MEDIA_FEATURES : [ QueryFeatureDescription ; 6 ] = [
666+ pub static MEDIA_FEATURES : [ QueryFeatureDescription ; 9 ] = [
575667 feature ! (
576668 atom!( "width" ) ,
577669 AllowsRanges :: Yes ,
@@ -608,4 +700,25 @@ pub static MEDIA_FEATURES: [QueryFeatureDescription; 6] = [
608700 keyword_evaluator!( eval_prefers_color_scheme, PrefersColorScheme ) ,
609701 FeatureFlags :: empty( ) ,
610702 ) ,
703+ feature ! (
704+ atom!( "prefers-contrast" ) ,
705+ AllowsRanges :: No ,
706+ keyword_evaluator!( eval_prefers_contrast, PrefersContrast ) ,
707+ FeatureFlags :: empty( ) ,
708+ ) ,
709+ feature ! (
710+ atom!( "prefers-reduced-motion" ) ,
711+ AllowsRanges :: No ,
712+ keyword_evaluator!( eval_prefers_reduced_motion, PrefersReducedMotion ) ,
713+ FeatureFlags :: empty( ) ,
714+ ) ,
715+ feature ! (
716+ atom!( "prefers-reduced-transparency" ) ,
717+ AllowsRanges :: No ,
718+ keyword_evaluator!(
719+ eval_prefers_reduced_transparency,
720+ PrefersReducedTransparency
721+ ) ,
722+ FeatureFlags :: empty( ) ,
723+ ) ,
611724] ;
0 commit comments