Bug 1570721 - Add an API to nsPresContext to override the prefers-color-scheme value. r=heycam

Differential Revision: https://phabricator.services.mozilla.com/D40248
This commit is contained in:
Emilio Cobos Álvarez 2019-08-01 18:00:35 +02:00
parent d93f6419d8
commit 0548a84746
4 changed files with 23 additions and 0 deletions

View File

@ -11,6 +11,7 @@
#define mozilla_MediaEmulationData_h #define mozilla_MediaEmulationData_h
#include "nsAtom.h" #include "nsAtom.h"
#include "mozilla/Maybe.h"
namespace mozilla { namespace mozilla {
@ -19,6 +20,7 @@ struct MediaEmulationData final {
RefPtr<nsAtom> mMedium; RefPtr<nsAtom> mMedium;
float mDPPX = 0.0; float mDPPX = 0.0;
Maybe<StylePrefersColorScheme> mPrefersColorScheme;
}; };
} // namespace mozilla } // namespace mozilla

View File

@ -987,6 +987,17 @@ void nsPresContext::SetOverrideDPPX(float aDPPX) {
MediaFeatureValuesChanged({MediaFeatureChangeReason::ResolutionChange}); MediaFeatureValuesChanged({MediaFeatureChangeReason::ResolutionChange});
} }
void nsPresContext::SetOverridePrefersColorScheme(
const Maybe<StylePrefersColorScheme>& aOverride) {
if (GetOverridePrefersColorScheme() == aOverride) {
return;
}
mMediaEmulationData.mPrefersColorScheme = aOverride;
// This is a bit of a lie, but it's the code-path that gets taken for regular
// system metrics changes via ThemeChanged().
MediaFeatureValuesChanged({MediaFeatureChangeReason::SystemMetricsChange});
}
gfxSize nsPresContext::ScreenSizeInchesForFontInflation(bool* aChanged) { gfxSize nsPresContext::ScreenSizeInchesForFontInflation(bool* aChanged) {
if (aChanged) { if (aChanged) {
*aChanged = false; *aChanged = false;

View File

@ -134,6 +134,7 @@ class nsPresContext : public nsISupports,
template <typename T> template <typename T>
using NotNull = mozilla::NotNull<T>; using NotNull = mozilla::NotNull<T>;
using MediaEmulationData = mozilla::MediaEmulationData; using MediaEmulationData = mozilla::MediaEmulationData;
using StylePrefersColorScheme = mozilla::StylePrefersColorScheme;
typedef mozilla::ScrollStyles ScrollStyles; typedef mozilla::ScrollStyles ScrollStyles;
typedef mozilla::StaticPresData StaticPresData; typedef mozilla::StaticPresData StaticPresData;
@ -491,6 +492,11 @@ class nsPresContext : public nsISupports,
float GetOverrideDPPX() const { return mMediaEmulationData.mDPPX; } float GetOverrideDPPX() const { return mMediaEmulationData.mDPPX; }
void SetOverrideDPPX(float); void SetOverrideDPPX(float);
Maybe<StylePrefersColorScheme> GetOverridePrefersColorScheme() const {
return mMediaEmulationData.mPrefersColorScheme;
}
void SetOverridePrefersColorScheme(const Maybe<StylePrefersColorScheme>&);
nscoord GetAutoQualityMinFontSize() { nscoord GetAutoQualityMinFontSize() {
return DevPixelsToAppUnits(mAutoQualityMinFontSizePixelsPref); return DevPixelsToAppUnits(mAutoQualityMinFontSizePixelsPref);
} }

View File

@ -248,6 +248,10 @@ StylePrefersColorScheme Gecko_MediaFeatures_PrefersColorScheme(
return StylePrefersColorScheme::Light; return StylePrefersColorScheme::Light;
} }
if (nsPresContext* pc = aDocument->GetPresContext()) { if (nsPresContext* pc = aDocument->GetPresContext()) {
if (auto devtoolsOverride = pc->GetOverridePrefersColorScheme()) {
return *devtoolsOverride;
}
if (pc->IsPrintingOrPrintPreview()) { if (pc->IsPrintingOrPrintPreview()) {
return StylePrefersColorScheme::Light; return StylePrefersColorScheme::Light;
} }