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.
This commit is contained in:
athenian200 2021-03-23 02:14:40 -05:00 committed by roytam1
commit da2461fa57
5 changed files with 45 additions and 0 deletions

View file

@ -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;
@ -457,6 +464,25 @@ GetOperatingSystemVersion(nsPresContext* aPresContext, const nsMediaFeature* aFe
return NS_OK;
}
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)
@ -556,6 +582,14 @@ nsMediaFeatures::features[] = {
{ nullptr },
GetMonochrome
},
{
&nsGkAtoms::prefers_color_scheme,
nsMediaFeature::eMinMaxNotAllowed,
nsMediaFeature::eEnumerated,
nsMediaFeature::eNoRequirements,
{ kPrefersColorSchemeKeywords },
GetPrefersColorScheme
},
{
&nsGkAtoms::resolution,
nsMediaFeature::eMinMaxAllowed,