diff options
author | athenian200 <athenian200@outlook.com> | 2021-03-23 02:14:40 -0500 |
---|---|---|
committer | athenian200 <athenian200@outlook.com> | 2021-03-23 22:25:54 -0500 |
commit | e946ca91fb032149131087ce24fbba886c53ee18 (patch) | |
tree | aff4c2f37a80c6f4c79e32df564bb18a31172536 /layout/style | |
parent | d3543ca2fecf39a83952872c0b936e4566fe02ff (diff) | |
download | uxp-e946ca91fb032149131087ce24fbba886c53ee18.tar.gz |
Issue #1752 - Implement "prefers-color-scheme" as a user preference.
This PR passes all current tests for this feature, and implements the "prefers-color-scheme" media query as an enumerated keyword that is controlled by an integer preference.
Currently, the possible options are 0 to see a website's fallback code and essentially behave like this isn't implemented (our current behavior), 1 to express a preference for a light theme (the default for spec reasons), and 2 to express a preference for a dark theme. Over time, this list may expand to include other preferences like a preference for a sepia theme or something, and this leaves us prepared for that future.
Diffstat (limited to 'layout/style')
-rw-r--r-- | layout/style/nsCSSKeywordList.h | 2 | ||||
-rw-r--r-- | layout/style/nsMediaFeatures.cpp | 34 | ||||
-rw-r--r-- | layout/style/nsStyleConsts.h | 4 |
3 files changed, 40 insertions, 0 deletions
diff --git a/layout/style/nsCSSKeywordList.h b/layout/style/nsCSSKeywordList.h index 689ff47eb6..be1691e629 100644 --- a/layout/style/nsCSSKeywordList.h +++ b/layout/style/nsCSSKeywordList.h @@ -224,6 +224,7 @@ CSS_KEY(crosshair, crosshair) CSS_KEY(currentcolor, currentcolor) CSS_KEY(cursive, cursive) CSS_KEY(cyclic, cyclic) +CSS_KEY(dark, dark) CSS_KEY(darken, darken) CSS_KEY(dashed, dashed) CSS_KEY(dense, dense) @@ -364,6 +365,7 @@ CSS_KEY(last baseline, last_baseline) // only used for DevTools auto-completion CSS_KEY(layout, layout) CSS_KEY(left, left) CSS_KEY(legacy, legacy) +CSS_KEY(light, light) CSS_KEY(lighten, lighten) CSS_KEY(lighter, lighter) CSS_KEY(line-through, line_through) diff --git a/layout/style/nsMediaFeatures.cpp b/layout/style/nsMediaFeatures.cpp index e44003ce48..9b5331a4a1 100644 --- a/layout/style/nsMediaFeatures.cpp +++ b/layout/style/nsMediaFeatures.cpp @@ -20,6 +20,7 @@ #include "nsIDocument.h" #include "nsIWidget.h" #include "nsContentUtils.h" +#include "mozilla/Preferences.h" #include "mozilla/StyleSheet.h" #include "mozilla/StyleSheetInlines.h" @@ -45,6 +46,12 @@ static const nsCSSProps::KTableEntry kDisplayModeKeywords[] = { { eCSSKeyword_UNKNOWN, -1 } }; +static const nsCSSProps::KTableEntry kPrefersColorSchemeKeywords[] = { + { eCSSKeyword_light, NS_STYLE_PREFERS_COLOR_SCHEME_LIGHT }, + { eCSSKeyword_dark, NS_STYLE_PREFERS_COLOR_SCHEME_DARK }, + { eCSSKeyword_UNKNOWN, -1 }, +}; + #ifdef XP_WIN struct WindowsThemeName { LookAndFeel::WindowsTheme id; @@ -456,6 +463,25 @@ GetOperatingSystemVersion(nsPresContext* aPresContext, const nsMediaFeature* aFe } static nsresult +GetPrefersColorScheme(nsPresContext* aPresContext, const nsMediaFeature* aFeature, + nsCSSValue& aResult) +{ + switch(Preferences::GetInt("browser.display.prefers_color_scheme", 1)) { + case 1: + aResult.SetIntValue(NS_STYLE_PREFERS_COLOR_SCHEME_LIGHT, + eCSSUnit_Enumerated); + break; + case 2: + aResult.SetIntValue(NS_STYLE_PREFERS_COLOR_SCHEME_DARK, + eCSSUnit_Enumerated); + break; + default: + aResult.Reset(); + } + return NS_OK; +} + +static nsresult GetIsGlyph(nsPresContext* aPresContext, const nsMediaFeature* aFeature, nsCSSValue& aResult) { @@ -555,6 +581,14 @@ nsMediaFeatures::features[] = { GetMonochrome }, { + &nsGkAtoms::prefers_color_scheme, + nsMediaFeature::eMinMaxNotAllowed, + nsMediaFeature::eEnumerated, + nsMediaFeature::eNoRequirements, + { kPrefersColorSchemeKeywords }, + GetPrefersColorScheme + }, + { &nsGkAtoms::resolution, nsMediaFeature::eMinMaxAllowed, nsMediaFeature::eResolution, diff --git a/layout/style/nsStyleConsts.h b/layout/style/nsStyleConsts.h index 308a66647b..60377928b0 100644 --- a/layout/style/nsStyleConsts.h +++ b/layout/style/nsStyleConsts.h @@ -1306,6 +1306,10 @@ enum class StyleDisplay : uint8_t { #define NS_STYLE_DISPLAY_MODE_STANDALONE 2 #define NS_STYLE_DISPLAY_MODE_FULLSCREEN 3 +// prefers-color-scheme +#define NS_STYLE_PREFERS_COLOR_SCHEME_LIGHT 0 +#define NS_STYLE_PREFERS_COLOR_SCHEME_DARK 1 + } // namespace mozilla #endif /* nsStyleConsts_h___ */ |