diff --git a/widget/cocoa/nsLookAndFeel.h b/widget/cocoa/nsLookAndFeel.h index 87edc6db0829..fc2cba9816c8 100644 --- a/widget/cocoa/nsLookAndFeel.h +++ b/widget/cocoa/nsLookAndFeel.h @@ -14,7 +14,7 @@ public: virtual ~nsLookAndFeel(); virtual nsresult NativeGetColor(ColorID aID, nscolor &aResult); - virtual void NativeInit() final {}; + virtual void NativeInit() final; virtual nsresult GetIntImpl(IntID aID, int32_t &aResult); virtual nsresult GetFloatImpl(FloatID aID, float &aResult); virtual bool GetFontImpl(FontID aID, nsString& aFontName, diff --git a/widget/windows/nsLookAndFeel.cpp b/widget/windows/nsLookAndFeel.cpp index 5491b46747c9..cd1d88af92d5 100644 --- a/widget/windows/nsLookAndFeel.cpp +++ b/widget/windows/nsLookAndFeel.cpp @@ -14,6 +14,7 @@ #include "mozilla/WindowsVersion.h" #include "gfxFontConstants.h" + using namespace mozilla; using namespace mozilla::widget; @@ -66,6 +67,12 @@ nsLookAndFeel::nsLookAndFeel() , mUseDefaultTheme(0) , mNativeThemeId(eWindowsTheme_Generic) , mCaretBlinkTime(-1) + , mHasColorAccent(false) + , mHasColorAccentText(false) + , mHasColorMenuHoverText(false) + , mHasColorMediaText(false) + , mHasColorCommunicationsText(false) + , mInitialized(false) { mozilla::Telemetry::Accumulate(mozilla::Telemetry::TOUCH_ENABLED_DEVICE, WinUtils::IsTouchDeviceSupportPresent()); @@ -75,9 +82,31 @@ nsLookAndFeel::~nsLookAndFeel() { } +void +nsLookAndFeel::NativeInit() +{ + EnsureInit(); +} + +/* virtual */ void +nsLookAndFeel::RefreshImpl() +{ + nsXPLookAndFeel::RefreshImpl(); + + for (auto e = mSystemFontCache.begin(), end = mSystemFontCache.end(); + e != end; ++e) { + e->mCacheValid = false; + } + mCaretBlinkTime = -1; + + mInitialized = false; +} + nsresult nsLookAndFeel::NativeGetColor(ColorID aID, nscolor &aColor) { + EnsureInit(); + nsresult res = NS_OK; int idx; @@ -189,13 +218,11 @@ nsLookAndFeel::NativeGetColor(ColorID aID, nscolor &aColor) } // Fall through case eColorID__moz_menuhovertext: - if (IsAppThemed()) { - res = ::GetColorFromTheme(eUXMenu, - MENU_POPUPITEM, MPI_HOT, TMT_TEXTCOLOR, aColor); - if (NS_SUCCEEDED(res)) - return res; - // fall through to highlight case + if (mHasColorMenuHoverText) { + aColor = mColorMenuHoverText; + return NS_OK; } + // Fall through case eColorID_highlighttext: case eColorID__moz_html_cellhighlighttext: idx = COLOR_HIGHLIGHTTEXT; @@ -264,36 +291,32 @@ nsLookAndFeel::NativeGetColor(ColorID aID, nscolor &aColor) idx = COLOR_3DFACE; break; case eColorID__moz_win_accentcolor: - res = GetAccentColor(aColor); - if (NS_SUCCEEDED(res)) { - return res; + if (mHasColorAccent) { + aColor = mColorAccent; + } else { + // Seems to be the default color (hardcoded because of bug 1065998) + aColor = NS_RGB(158, 158, 158); } - // Seems to be the default color (hardcoded because of bug 1065998) - aColor = NS_RGB(158, 158, 158); return NS_OK; case eColorID__moz_win_accentcolortext: - res = GetAccentColorText(aColor); - if (NS_SUCCEEDED(res)) { - return res; + if (mHasColorAccentText) { + aColor = mColorAccentText; + } else { + aColor = NS_RGB(0, 0, 0); } - aColor = NS_RGB(0, 0, 0); return NS_OK; case eColorID__moz_win_mediatext: - if (IsAppThemed()) { - res = ::GetColorFromTheme(eUXMediaToolbar, - TP_BUTTON, TS_NORMAL, TMT_TEXTCOLOR, aColor); - if (NS_SUCCEEDED(res)) - return res; + if (mHasColorMediaText) { + aColor = mColorMediaText; + return NS_OK; } // if we've gotten here just return -moz-dialogtext instead idx = COLOR_WINDOWTEXT; break; case eColorID__moz_win_communicationstext: - if (IsAppThemed()) { - res = ::GetColorFromTheme(eUXCommunicationsToolbar, - TP_BUTTON, TS_NORMAL, TMT_TEXTCOLOR, aColor); - if (NS_SUCCEEDED(res)) - return res; + if (mHasColorCommunicationsText) { + aColor = mColorCommunicationsText; + return NS_OK; } // if we've gotten here just return -moz-dialogtext instead idx = COLOR_WINDOWTEXT; @@ -318,8 +341,7 @@ nsLookAndFeel::NativeGetColor(ColorID aID, nscolor &aColor) break; } - DWORD color = ::GetSysColor(idx); - aColor = COLOREF_2_NSRGB(color); + aColor = GetColorForSysColorIndex(idx); return res; } @@ -733,18 +755,6 @@ nsLookAndFeel::GetFontImpl(FontID anID, nsString &aFontName, return status; } -/* virtual */ void -nsLookAndFeel::RefreshImpl() -{ - nsXPLookAndFeel::RefreshImpl(); - - for (auto e = mSystemFontCache.begin(), end = mSystemFontCache.end(); - e != end; ++e) { - e->mCacheValid = false; - } - mCaretBlinkTime = -1; -} - /* virtual */ char16_t nsLookAndFeel::GetPasswordCharacterImpl() @@ -854,3 +864,47 @@ nsLookAndFeel::GetAccentColorText(nscolor& aColor) return NS_OK; } + +nscolor +nsLookAndFeel::GetColorForSysColorIndex(int index) +{ + MOZ_ASSERT(index >= SYS_COLOR_MIN && index <= SYS_COLOR_MAX); + return mSysColorTable[index - SYS_COLOR_MIN]; +} + +void +nsLookAndFeel::EnsureInit() +{ + if (mInitialized) { + return; + } + mInitialized = true; + + nsresult res; + + res = GetAccentColor(mColorAccent); + mHasColorAccent = NS_SUCCEEDED(res); + + res = GetAccentColorText(mColorAccentText); + mHasColorAccentText = NS_SUCCEEDED(res); + + if (IsAppThemed()) { + res = ::GetColorFromTheme(eUXMenu, MENU_POPUPITEM, MPI_HOT, TMT_TEXTCOLOR, + mColorMenuHoverText); + mHasColorMenuHoverText = NS_SUCCEEDED(res); + + res = ::GetColorFromTheme(eUXMediaToolbar, TP_BUTTON, TS_NORMAL, + TMT_TEXTCOLOR, mColorMediaText); + mHasColorMediaText = NS_SUCCEEDED(res); + + res = ::GetColorFromTheme(eUXCommunicationsToolbar, TP_BUTTON, TS_NORMAL, + TMT_TEXTCOLOR, mColorCommunicationsText); + mHasColorCommunicationsText = NS_SUCCEEDED(res); + } + + // Fill out the sys color table. + for (int i = SYS_COLOR_MIN; i <= SYS_COLOR_MAX; ++i) { + DWORD color = ::GetSysColor(i); + mSysColorTable[i - SYS_COLOR_MIN] = COLOREF_2_NSRGB(color); + } +} diff --git a/widget/windows/nsLookAndFeel.h b/widget/windows/nsLookAndFeel.h index 5f32f4510db8..dc1aec40d602 100644 --- a/widget/windows/nsLookAndFeel.h +++ b/widget/windows/nsLookAndFeel.h @@ -35,6 +35,13 @@ #define SM_SYSTEMDOCKED 0x00002004 #endif +/* + * Color constant inclusive bounds for GetSysColor + */ +#define SYS_COLOR_MIN 0 +#define SYS_COLOR_MAX 30 +#define SYS_COLOR_COUNT (SYS_COLOR_MAX - SYS_COLOR_MIN + 1) + class nsLookAndFeel final : public nsXPLookAndFeel { static OperatingSystemVersion GetOperatingSystemVersion(); @@ -42,14 +49,14 @@ public: nsLookAndFeel(); virtual ~nsLookAndFeel(); + void NativeInit() final; + void RefreshImpl() override; nsresult NativeGetColor(ColorID aID, nscolor &aResult) override; - void NativeInit() final {}; nsresult GetIntImpl(IntID aID, int32_t &aResult) override; nsresult GetFloatImpl(FloatID aID, float &aResult) override; bool GetFontImpl(FontID aID, nsString& aFontName, gfxFontStyle& aFontStyle, float aDevPixPerCSSPixel) override; - void RefreshImpl() override; char16_t GetPasswordCharacterImpl() override; nsTArray GetIntCacheImpl() override; @@ -72,6 +79,8 @@ private: */ nsresult GetAccentColorText(nscolor& aColor); + nscolor GetColorForSysColorIndex(int index); + // Content process cached values that get shipped over from the browser // process. int32_t mUseAccessibilityTheme; @@ -79,6 +88,24 @@ private: int32_t mNativeThemeId; // see LookAndFeel enum 'WindowsTheme' int32_t mCaretBlinkTime; + // Cached colors and flags indicating success in their retrieval. + nscolor mColorMenuHoverText; + bool mHasColorMenuHoverText; + nscolor mColorAccent; + bool mHasColorAccent; + nscolor mColorAccentText; + bool mHasColorAccentText; + nscolor mColorMediaText; + bool mHasColorMediaText; + nscolor mColorCommunicationsText; + bool mHasColorCommunicationsText; + + nscolor mSysColorTable[SYS_COLOR_COUNT]; + + bool mInitialized; + + void EnsureInit(); + struct CachedSystemFont { CachedSystemFont() : mCacheValid(false)