From d3f4f81c531fe8b29f83491a4dfbf84aa7d35cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 2 Apr 2021 22:17:55 +0000 Subject: [PATCH] Bug 1702765 - Plumb ColorScheme through nsXPLookAndFeel. r=mstange After this patch, there are two remaining pieces to fix bug 1700294: * macOS nsLookAndFeel needs to return the right colors for light / dark appearance. * We need to return ColorScheme::Dark for the right documents in ColorSchemeForDocument. Both of those should be straight-forward. Differential Revision: https://phabricator.services.mozilla.com/D110680 --- widget/LookAndFeelTypes.ipdlh | 6 ++++-- widget/RemoteLookAndFeel.cpp | 17 +++++++++++++---- widget/RemoteLookAndFeel.h | 6 +++--- widget/android/nsLookAndFeel.cpp | 3 ++- widget/android/nsLookAndFeel.h | 6 +++--- widget/cocoa/nsLookAndFeel.h | 6 +++--- widget/cocoa/nsLookAndFeel.mm | 2 +- widget/gtk/nsLookAndFeel.cpp | 3 ++- widget/gtk/nsLookAndFeel.h | 2 +- widget/headless/HeadlessLookAndFeel.h | 3 ++- widget/headless/HeadlessLookAndFeelGTK.cpp | 3 ++- widget/nsXPLookAndFeel.cpp | 18 +++++++++++------- widget/nsXPLookAndFeel.h | 2 +- widget/uikit/nsLookAndFeel.h | 2 +- widget/uikit/nsLookAndFeel.mm | 2 +- widget/windows/nsLookAndFeel.cpp | 3 ++- widget/windows/nsLookAndFeel.h | 6 +++--- 17 files changed, 55 insertions(+), 35 deletions(-) diff --git a/widget/LookAndFeelTypes.ipdlh b/widget/LookAndFeelTypes.ipdlh index c936c978fd83..dbeb1a1bcb42 100644 --- a/widget/LookAndFeelTypes.ipdlh +++ b/widget/LookAndFeelTypes.ipdlh @@ -34,13 +34,15 @@ namespace widget { struct LookAndFeelTables { int32_t[] ints; float[] floats; - nscolor[] colors; LookAndFeelFont[] fonts; + nscolor[] lightColors; + nscolor[] darkColors; uint8_t[] intMap; uint8_t[] floatMap; - uint8_t[] colorMap; uint8_t[] fontMap; + uint8_t[] lightColorMap; + uint8_t[] darkColorMap; uint16_t passwordChar; bool passwordEcho; diff --git a/widget/RemoteLookAndFeel.cpp b/widget/RemoteLookAndFeel.cpp index 9a0a03a9c281..822829902dc8 100644 --- a/widget/RemoteLookAndFeel.cpp +++ b/widget/RemoteLookAndFeel.cpp @@ -106,9 +106,14 @@ void AddToMap(nsTArray& aItems, nsTArray& aMap, Id aId, } // namespace -nsresult RemoteLookAndFeel::NativeGetColor(ColorID aID, nscolor& aResult) { +nsresult RemoteLookAndFeel::NativeGetColor(ColorID aID, ColorScheme aScheme, + nscolor& aResult) { const nscolor* result; - MOZ_TRY_VAR(result, MapLookup(mTables.colors(), mTables.colorMap(), aID)); + const bool dark = aScheme == ColorScheme::Dark; + MOZ_TRY_VAR( + result, + MapLookup(dark ? mTables.darkColors() : mTables.lightColors(), + dark ? mTables.darkColorMap() : mTables.lightColorMap(), aID)); aResult = *result; return NS_OK; } @@ -149,6 +154,7 @@ static bool AddIDsToMap(nsXPLookAndFeel* aImpl, FullLookAndFeel* aLf, using FontID = LookAndFeel::FontID; using FloatID = LookAndFeel::FloatID; using ColorID = LookAndFeel::ColorID; + using ColorScheme = LookAndFeel::ColorScheme; bool anyFromOtherTheme = false; for (auto id : MakeEnumeratedRange(IntID::End)) { @@ -168,8 +174,11 @@ static bool AddIDsToMap(nsXPLookAndFeel* aImpl, FullLookAndFeel* aLf, continue; } nscolor theColor; - nsresult rv = aImpl->NativeGetColor(id, theColor); - AddToMap(aLf->tables().colors(), aLf->tables().colorMap(), id, + nsresult rv = aImpl->NativeGetColor(id, ColorScheme::Light, theColor); + AddToMap(aLf->tables().lightColors(), aLf->tables().lightColorMap(), id, + NS_SUCCEEDED(rv) ? Some(theColor) : Nothing{}); + rv = aImpl->NativeGetColor(id, ColorScheme::Dark, theColor); + AddToMap(aLf->tables().darkColors(), aLf->tables().darkColorMap(), id, NS_SUCCEEDED(rv) ? Some(theColor) : Nothing{}); } diff --git a/widget/RemoteLookAndFeel.h b/widget/RemoteLookAndFeel.h index e893b5d8e056..918f3392ec30 100644 --- a/widget/RemoteLookAndFeel.h +++ b/widget/RemoteLookAndFeel.h @@ -25,9 +25,9 @@ class RemoteLookAndFeel final : public nsXPLookAndFeel { void NativeInit() override {} - nsresult NativeGetInt(IntID aID, int32_t& aResult) override; - nsresult NativeGetFloat(FloatID aID, float& aResult) override; - nsresult NativeGetColor(ColorID aID, nscolor& aResult) override; + nsresult NativeGetInt(IntID, int32_t& aResult) override; + nsresult NativeGetFloat(FloatID, float& aResult) override; + nsresult NativeGetColor(ColorID, ColorScheme, nscolor& aResult) override; bool NativeGetFont(FontID aID, nsString& aFontName, gfxFontStyle& aFontStyle) override; diff --git a/widget/android/nsLookAndFeel.cpp b/widget/android/nsLookAndFeel.cpp index fb83acfc14a9..5a8d9242ee1c 100644 --- a/widget/android/nsLookAndFeel.cpp +++ b/widget/android/nsLookAndFeel.cpp @@ -81,7 +81,8 @@ void nsLookAndFeel::RefreshImpl() { mSystemUsesDarkThemeCached = false; } -nsresult nsLookAndFeel::NativeGetColor(ColorID aID, nscolor& aColor) { +nsresult nsLookAndFeel::NativeGetColor(ColorID aID, ColorScheme, + nscolor& aColor) { nsresult rv = NS_OK; EnsureInitSystemColors(); diff --git a/widget/android/nsLookAndFeel.h b/widget/android/nsLookAndFeel.h index f7903647ae3f..38bde6739159 100644 --- a/widget/android/nsLookAndFeel.h +++ b/widget/android/nsLookAndFeel.h @@ -15,9 +15,9 @@ class nsLookAndFeel final : public nsXPLookAndFeel { void NativeInit() final; virtual void RefreshImpl() override; - nsresult NativeGetInt(IntID aID, int32_t& aResult) override; - nsresult NativeGetFloat(FloatID aID, float& aResult) override; - nsresult NativeGetColor(ColorID aID, nscolor& aResult) override; + nsresult NativeGetInt(IntID, int32_t& aResult) override; + nsresult NativeGetFloat(FloatID, float& aResult) override; + nsresult NativeGetColor(ColorID, ColorScheme, nscolor& aResult) override; bool NativeGetFont(FontID aID, nsString& aName, gfxFontStyle& aStyle) override; virtual bool GetEchoPasswordImpl() override; diff --git a/widget/cocoa/nsLookAndFeel.h b/widget/cocoa/nsLookAndFeel.h index a32eb9e83f1b..e5dcdf5fae6b 100644 --- a/widget/cocoa/nsLookAndFeel.h +++ b/widget/cocoa/nsLookAndFeel.h @@ -14,9 +14,9 @@ class nsLookAndFeel final : public nsXPLookAndFeel { void NativeInit() final; virtual void RefreshImpl() override; - nsresult NativeGetColor(ColorID aID, nscolor& aResult) override; - nsresult NativeGetInt(IntID aID, int32_t& aResult) override; - nsresult NativeGetFloat(FloatID aID, float& aResult) override; + nsresult NativeGetColor(ColorID, ColorScheme, nscolor& aResult) override; + nsresult NativeGetInt(IntID, int32_t& aResult) override; + nsresult NativeGetFloat(FloatID, float& aResult) override; bool NativeGetFont(FontID aID, nsString& aFontName, gfxFontStyle& aFontStyle) override; diff --git a/widget/cocoa/nsLookAndFeel.mm b/widget/cocoa/nsLookAndFeel.mm index 3267e026c84e..dda370794fc6 100644 --- a/widget/cocoa/nsLookAndFeel.mm +++ b/widget/cocoa/nsLookAndFeel.mm @@ -115,7 +115,7 @@ nscolor nsLookAndFeel::ProcessSelectionBackground(nscolor aColor) { return resultColor; } -nsresult nsLookAndFeel::NativeGetColor(ColorID aID, nscolor& aColor) { +nsresult nsLookAndFeel::NativeGetColor(ColorID aID, ColorScheme, nscolor& aColor) { EnsureInit(); nsresult res = NS_OK; diff --git a/widget/gtk/nsLookAndFeel.cpp b/widget/gtk/nsLookAndFeel.cpp index d720cafe49ef..fedd5424cffd 100644 --- a/widget/gtk/nsLookAndFeel.cpp +++ b/widget/gtk/nsLookAndFeel.cpp @@ -300,7 +300,8 @@ static bool IsSelectionColorBackground(LookAndFeel::ColorID aID) { } } -nsresult nsLookAndFeel::NativeGetColor(ColorID aID, nscolor& aColor) { +nsresult nsLookAndFeel::NativeGetColor(ColorID aID, ColorScheme, + nscolor& aColor) { EnsureInit(); nsresult res = NS_OK; diff --git a/widget/gtk/nsLookAndFeel.h b/widget/gtk/nsLookAndFeel.h index ab997290b025..cb18df89b8ac 100644 --- a/widget/gtk/nsLookAndFeel.h +++ b/widget/gtk/nsLookAndFeel.h @@ -25,7 +25,7 @@ class nsLookAndFeel final : public nsXPLookAndFeel { void RefreshImpl() override; nsresult NativeGetInt(IntID aID, int32_t& aResult) override; nsresult NativeGetFloat(FloatID aID, float& aResult) override; - nsresult NativeGetColor(ColorID aID, nscolor& aResult) override; + nsresult NativeGetColor(ColorID, ColorScheme, nscolor& aResult) override; bool NativeGetFont(FontID aID, nsString& aFontName, gfxFontStyle& aFontStyle) override; diff --git a/widget/headless/HeadlessLookAndFeel.h b/widget/headless/HeadlessLookAndFeel.h index 693098d48370..7f026edd671f 100644 --- a/widget/headless/HeadlessLookAndFeel.h +++ b/widget/headless/HeadlessLookAndFeel.h @@ -37,7 +37,8 @@ class HeadlessLookAndFeel : public nsXPLookAndFeel { void NativeInit() final{}; virtual nsresult NativeGetInt(IntID aID, int32_t& aResult) override; virtual nsresult NativeGetFloat(FloatID aID, float& aResult) override; - virtual nsresult NativeGetColor(ColorID aID, nscolor& aResult) override; + virtual nsresult NativeGetColor(ColorID, ColorScheme, + nscolor& aResult) override; virtual bool NativeGetFont(FontID aID, nsString& aFontName, gfxFontStyle& aFontStyle) override; diff --git a/widget/headless/HeadlessLookAndFeelGTK.cpp b/widget/headless/HeadlessLookAndFeelGTK.cpp index cb66a5fc293b..2eb809bd96af 100644 --- a/widget/headless/HeadlessLookAndFeelGTK.cpp +++ b/widget/headless/HeadlessLookAndFeelGTK.cpp @@ -16,7 +16,8 @@ HeadlessLookAndFeel::HeadlessLookAndFeel() {} HeadlessLookAndFeel::~HeadlessLookAndFeel() = default; -nsresult HeadlessLookAndFeel::NativeGetColor(ColorID aID, nscolor& aColor) { +nsresult HeadlessLookAndFeel::NativeGetColor(ColorID aID, ColorScheme, + nscolor& aColor) { // For headless mode, we use GetStandinForNativeColor for everything we can, // and hardcoded values for everything else. diff --git a/widget/nsXPLookAndFeel.cpp b/widget/nsXPLookAndFeel.cpp index 94be42cdbc06..fc332053aa6c 100644 --- a/widget/nsXPLookAndFeel.cpp +++ b/widget/nsXPLookAndFeel.cpp @@ -93,7 +93,8 @@ class EnumeratedCache { } }; -static EnumeratedCache, ColorID::End> sColorCache; +static EnumeratedCache, ColorID::End> sLightColorCache; +static EnumeratedCache, ColorID::End> sDarkColorCache; static EnumeratedCache, FloatID::End> sFloatCache; static EnumeratedCache, IntID::End> sIntCache; static EnumeratedCache sFontCache; @@ -680,7 +681,9 @@ nsresult nsXPLookAndFeel::GetColorValue(ColorID aID, ColorScheme aScheme, return NS_OK; } - if (const auto* cached = sColorCache.Get(aID)) { + auto& cache = + aScheme == ColorScheme::Light ? sLightColorCache : sDarkColorCache; + if (const auto* cached = cache.Get(aID)) { if (cached->isNothing()) { return NS_ERROR_FAILURE; } @@ -689,11 +692,11 @@ nsresult nsXPLookAndFeel::GetColorValue(ColorID aID, ColorScheme aScheme, } if (NS_SUCCEEDED(GetColorFromPref(aID, aResult))) { - sColorCache.Insert(aID, Some(aResult)); + cache.Insert(aID, Some(aResult)); return NS_OK; } - if (NS_SUCCEEDED(NativeGetColor(aID, aResult))) { + if (NS_SUCCEEDED(NativeGetColor(aID, aScheme, aResult))) { if (gfxPlatform::GetCMSMode() == CMSMode::All && !IsSpecialColor(aID, aResult)) { qcms_transform* transform = gfxPlatform::GetCMSInverseRGBTransform(); @@ -710,11 +713,11 @@ nsresult nsXPLookAndFeel::GetColorValue(ColorID aID, ColorScheme aScheme, // NOTE: Servo holds a lock and the main thread is paused, so writing to the // global cache here is fine. - sColorCache.Insert(aID, Some(aResult)); + cache.Insert(aID, Some(aResult)); return NS_OK; } - sColorCache.Insert(aID, Nothing()); + cache.Insert(aID, Nothing()); return NS_ERROR_FAILURE; } @@ -832,7 +835,8 @@ bool nsXPLookAndFeel::GetFontValue(FontID aID, nsString& aName, void nsXPLookAndFeel::RefreshImpl() { // Wipe out our caches. - sColorCache.Clear(); + sLightColorCache.Clear(); + sDarkColorCache.Clear(); sFontCache.Clear(); sFloatCache.Clear(); sIntCache.Clear(); diff --git a/widget/nsXPLookAndFeel.h b/widget/nsXPLookAndFeel.h index efa4a19b898d..e6955e80291a 100644 --- a/widget/nsXPLookAndFeel.h +++ b/widget/nsXPLookAndFeel.h @@ -40,7 +40,7 @@ class nsXPLookAndFeel : public mozilla::LookAndFeel { virtual nsresult NativeGetInt(IntID aID, int32_t& aResult) = 0; virtual nsresult NativeGetFloat(FloatID aID, float& aResult) = 0; - virtual nsresult NativeGetColor(ColorID aID, nscolor& aResult) = 0; + virtual nsresult NativeGetColor(ColorID, ColorScheme, nscolor& aResult) = 0; virtual bool NativeGetFont(FontID aID, nsString& aName, gfxFontStyle& aStyle) = 0; diff --git a/widget/uikit/nsLookAndFeel.h b/widget/uikit/nsLookAndFeel.h index a1afbe148ff3..7de7e0712bd7 100644 --- a/widget/uikit/nsLookAndFeel.h +++ b/widget/uikit/nsLookAndFeel.h @@ -17,7 +17,7 @@ class nsLookAndFeel final : public nsXPLookAndFeel { virtual void RefreshImpl(); nsresult NativeGetImpl(IntID aID, int32_t& aResult) override; nsresult NativeGetFloat(FloatID aID, float& aResult) override; - nsresult NativeGetColor(const ColorID aID, nscolor& aResult) override; + nsresult NativeGetColor(ColorID, ColorScheme, nscolor& aResult) override; bool NativeGetFont(FontID aID, nsString& aFontName, gfxFontStyle& aFontStyle) override; virtual char16_t GetPasswordCharacterImpl() { diff --git a/widget/uikit/nsLookAndFeel.mm b/widget/uikit/nsLookAndFeel.mm index eac26f82cab2..eb6575072224 100644 --- a/widget/uikit/nsLookAndFeel.mm +++ b/widget/uikit/nsLookAndFeel.mm @@ -40,7 +40,7 @@ void nsLookAndFeel::RefreshImpl() { mInitialized = false; } -nsresult nsLookAndFeel::NativeGetColor(const ColorID aID, nscolor& aResult) { +nsresult nsLookAndFeel::NativeGetColor(ColorID, ColorScheme, nscolor& aResult) { EnsureInit(); nsresult res = NS_OK; diff --git a/widget/windows/nsLookAndFeel.cpp b/widget/windows/nsLookAndFeel.cpp index 3c771febeb1a..6a99dd849644 100644 --- a/widget/windows/nsLookAndFeel.cpp +++ b/widget/windows/nsLookAndFeel.cpp @@ -124,7 +124,8 @@ void nsLookAndFeel::RefreshImpl() { mInitialized = false; } -nsresult nsLookAndFeel::NativeGetColor(ColorID aID, nscolor& aColor) { +nsresult nsLookAndFeel::NativeGetColor(ColorID aID, ColorScheme, + nscolor& aColor) { EnsureInit(); nsresult res = NS_OK; diff --git a/widget/windows/nsLookAndFeel.h b/widget/windows/nsLookAndFeel.h index d63470c7580f..f12051d4e5b6 100644 --- a/widget/windows/nsLookAndFeel.h +++ b/widget/windows/nsLookAndFeel.h @@ -52,9 +52,9 @@ class nsLookAndFeel final : public nsXPLookAndFeel { void NativeInit() final; void RefreshImpl() override; - nsresult NativeGetInt(IntID aID, int32_t& aResult) override; - nsresult NativeGetFloat(FloatID aID, float& aResult) override; - nsresult NativeGetColor(ColorID aID, nscolor& aResult) override; + nsresult NativeGetInt(IntID, int32_t& aResult) override; + nsresult NativeGetFloat(FloatID, float& aResult) override; + nsresult NativeGetColor(ColorID, ColorScheme, nscolor& aResult) override; bool NativeGetFont(FontID aID, nsString& aFontName, gfxFontStyle& aFontStyle) override; char16_t GetPasswordCharacterImpl() override;