From c1c70a354c98952ad2d5290c51aab7c6b0f46b1d Mon Sep 17 00:00:00 2001 From: Jonathan Watt Date: Mon, 5 Jun 2017 12:19:51 +0100 Subject: [PATCH] Bug 1344910, part 1 - Add a '-moz-win-accentcolor' color keyword to expose the Win10 accent color. r=jimm MozReview-Commit-ID: DtRcuizKRH2 --- layout/style/nsCSSKeywordList.h | 1 + layout/style/nsCSSProps.cpp | 1 + widget/LookAndFeel.h | 2 ++ widget/nsXPLookAndFeel.cpp | 3 ++ widget/windows/nsLookAndFeel.cpp | 49 ++++++++++++++++++++++++++++++++ widget/windows/nsLookAndFeel.h | 10 +++++++ widget/windows/nsWindow.cpp | 17 +++++++---- 7 files changed, 78 insertions(+), 5 deletions(-) diff --git a/layout/style/nsCSSKeywordList.h b/layout/style/nsCSSKeywordList.h index 017091be0a70..cfa2324b89e6 100644 --- a/layout/style/nsCSSKeywordList.h +++ b/layout/style/nsCSSKeywordList.h @@ -747,6 +747,7 @@ CSS_KEY(button-focus, button_focus) CSS_KEY(-moz-win-media-toolbox, _moz_win_media_toolbox) CSS_KEY(-moz-win-communications-toolbox, _moz_win_communications_toolbox) CSS_KEY(-moz-win-browsertabbar-toolbox, _moz_win_browsertabbar_toolbox) +CSS_KEY(-moz-win-accentcolor, _moz_win_accentcolor) CSS_KEY(-moz-win-mediatext, _moz_win_mediatext) CSS_KEY(-moz-win-communicationstext, _moz_win_communicationstext) CSS_KEY(-moz-win-glass, _moz_win_glass) diff --git a/layout/style/nsCSSProps.cpp b/layout/style/nsCSSProps.cpp index 3f3388144d53..0051c283fdb4 100644 --- a/layout/style/nsCSSProps.cpp +++ b/layout/style/nsCSSProps.cpp @@ -1137,6 +1137,7 @@ const KTableEntry nsCSSProps::kColorKTable[] = { { eCSSKeyword__moz_oddtreerow, LookAndFeel::eColorID__moz_oddtreerow }, { eCSSKeyword__moz_visitedhyperlinktext, NS_COLOR_MOZ_VISITEDHYPERLINKTEXT }, { eCSSKeyword_currentcolor, NS_COLOR_CURRENTCOLOR }, + { eCSSKeyword__moz_win_accentcolor, LookAndFeel::eColorID__moz_win_accentcolor }, { eCSSKeyword__moz_win_mediatext, LookAndFeel::eColorID__moz_win_mediatext }, { eCSSKeyword__moz_win_communicationstext, LookAndFeel::eColorID__moz_win_communicationstext }, { eCSSKeyword__moz_nativehyperlinktext, LookAndFeel::eColorID__moz_nativehyperlinktext }, diff --git a/widget/LookAndFeel.h b/widget/LookAndFeel.h index b71aed2e23d9..6ba292583772 100644 --- a/widget/LookAndFeel.h +++ b/widget/LookAndFeel.h @@ -158,6 +158,8 @@ public: // vista rebars + // accent color for title bar + eColorID__moz_win_accentcolor, // media rebar text eColorID__moz_win_mediatext, // communications rebar text diff --git a/widget/nsXPLookAndFeel.cpp b/widget/nsXPLookAndFeel.cpp index 83367a507f57..01c081952ad7 100644 --- a/widget/nsXPLookAndFeel.cpp +++ b/widget/nsXPLookAndFeel.cpp @@ -658,6 +658,9 @@ nsXPLookAndFeel::GetStandinForNativeColor(ColorID aID) result = NS_RGB(0x3F, 0x3F, 0x3F); break; case eColorID__moz_mac_secondaryhighlight: result = NS_RGB(0xD4, 0xD4, 0xD4); break; + case eColorID__moz_win_accentcolor: + // Seems to be the default color (hardcoded because of bug 1065998) + result = NS_RGB(0x9E, 0x9E, 0x9E); break; case eColorID__moz_win_mediatext: result = NS_RGB(0xFF, 0xFF, 0xFF); break; case eColorID__moz_win_communicationstext: diff --git a/widget/windows/nsLookAndFeel.cpp b/widget/windows/nsLookAndFeel.cpp index 2e8b6dd7a5dd..da43736707fc 100644 --- a/widget/windows/nsLookAndFeel.cpp +++ b/widget/windows/nsLookAndFeel.cpp @@ -262,6 +262,16 @@ nsLookAndFeel::NativeGetColor(ColorID aID, nscolor &aColor) case eColorID__moz_cellhighlight: idx = COLOR_3DFACE; break; + case eColorID__moz_win_accentcolor: + res = GetAccentColor(aColor); + if (NS_SUCCEEDED(res)) { + return res; + } + NS_WARNING("Using fallback for accent color - UI code failed to use the " + "-moz-windows-accent-color-applies media query properly"); + // Seems to be the default color (hardcoded because of bug 1065998) + aColor = NS_RGB(158, 158, 158); + return NS_OK; case eColorID__moz_win_mediatext: if (IsAppThemed()) { res = ::GetColorFromTheme(eUXMediaToolbar, @@ -747,3 +757,42 @@ nsLookAndFeel::SetIntCacheImpl(const nsTArray& aLookAndFeelIntCa } } +/* static */ nsresult +nsLookAndFeel::GetAccentColor(nscolor& aColor) +{ + nsresult rv; + + if (!mDwmKey) { + mDwmKey = do_CreateInstance("@mozilla.org/windows-registry-key;1", &rv); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + } + + rv = mDwmKey->Open(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER, + NS_LITERAL_STRING("SOFTWARE\\Microsoft\\Windows\\DWM"), + nsIWindowsRegKey::ACCESS_QUERY_VALUE); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + // The ColorPrevalence value is set to 1 when the "Show color on title bar" + // setting in the Color section of Window's Personalization settings is + // turned on. + uint32_t accentColor, colorPrevalence; + if (NS_SUCCEEDED(mDwmKey->ReadIntValue(NS_LITERAL_STRING("AccentColor"), &accentColor)) && + NS_SUCCEEDED(mDwmKey->ReadIntValue(NS_LITERAL_STRING("ColorPrevalence"), &colorPrevalence)) && + colorPrevalence == 1) { + // The order of the color components in the DWORD stored in the registry + // happens to be the same order as we store the components in nscolor + // so we can just assign directly here. + aColor = accentColor; + rv = NS_OK; + } else { + rv = NS_ERROR_NOT_AVAILABLE; + } + + mDwmKey->Close(); + + return rv; +} diff --git a/widget/windows/nsLookAndFeel.h b/widget/windows/nsLookAndFeel.h index 774da7c8ab78..97948ab2d8c5 100644 --- a/widget/windows/nsLookAndFeel.h +++ b/widget/windows/nsLookAndFeel.h @@ -9,6 +9,7 @@ #include "nsXPLookAndFeel.h" #include "gfxFont.h" #include "mozilla/RangedArray.h" +#include "nsIWindowsRegKey.h" /* * Gesture System Metrics @@ -51,6 +52,13 @@ public: void SetIntCacheImpl(const nsTArray& aLookAndFeelIntCache) override; private: + /** + * Fetches the Windows accent color from the Windows settings if + * the accent color is set to apply to the title bar, otherwise + * returns an error code. + */ + nsresult GetAccentColor(nscolor& aColor); + // Content process cached values that get shipped over from the browser // process. int32_t mUseAccessibilityTheme; @@ -71,6 +79,8 @@ private: mozilla::RangedArray mSystemFontCache; + + nsCOMPtr mDwmKey; }; #endif diff --git a/widget/windows/nsWindow.cpp b/widget/windows/nsWindow.cpp index b9c3d82a516d..f40381507b40 100644 --- a/widget/windows/nsWindow.cpp +++ b/widget/windows/nsWindow.cpp @@ -5197,12 +5197,19 @@ nsWindow::ProcessMessage(UINT msg, WPARAM& wParam, LPARAM& lParam, case WM_SETTINGCHANGE: { - if (IsWin10OrLater() && mWindowType == eWindowType_invisible && lParam) { + if (lParam) { auto lParamString = reinterpret_cast(lParam); - if (!wcscmp(lParamString, L"UserInteractionMode")) { - nsCOMPtr uiUtils(do_GetService("@mozilla.org/windows-ui-utils;1")); - if (uiUtils) { - uiUtils->UpdateTabletModeState(); + if (!wcscmp(lParamString, L"ImmersiveColorSet")) { + // WM_SYSCOLORCHANGE is not dispatched for accent color changes + OnSysColorChanged(); + break; + } + if (IsWin10OrLater() && mWindowType == eWindowType_invisible) { + if (!wcscmp(lParamString, L"UserInteractionMode")) { + nsCOMPtr uiUtils(do_GetService("@mozilla.org/windows-ui-utils;1")); + if (uiUtils) { + uiUtils->UpdateTabletModeState(); + } } } }