From cbd7f2de830bd79b19a8ef86b3d994900634a0b6 Mon Sep 17 00:00:00 2001 From: Pale Moon Date: Sun, 9 Jul 2017 04:23:16 +0200 Subject: [PATCH] Add a '-moz-win-accentcolor' color keyword to expose the Win10 accent color. --- layout/style/nsCSSKeywordList.h | 1 + layout/style/nsCSSProps.cpp | 1 + widget/LookAndFeel.h | 2 ++ widget/nsXPLookAndFeel.cpp | 1 + widget/windows/nsLookAndFeel.cpp | 50 ++++++++++++++++++++++++++++++++ widget/windows/nsLookAndFeel.h | 11 +++++++ widget/windows/nsWindow.cpp | 13 +++++++++ 7 files changed, 79 insertions(+) diff --git a/layout/style/nsCSSKeywordList.h b/layout/style/nsCSSKeywordList.h index 09947b7642..0d689b1d1c 100644 --- a/layout/style/nsCSSKeywordList.h +++ b/layout/style/nsCSSKeywordList.h @@ -685,6 +685,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 4445e6fb65..f6ec5912d9 100644 --- a/layout/style/nsCSSProps.cpp +++ b/layout/style/nsCSSProps.cpp @@ -971,6 +971,7 @@ const KTableValue 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 f9eda0e3c9..35959a3e5f 100644 --- a/widget/LookAndFeel.h +++ b/widget/LookAndFeel.h @@ -151,6 +151,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 b804143b11..e524a74059 100644 --- a/widget/nsXPLookAndFeel.cpp +++ b/widget/nsXPLookAndFeel.cpp @@ -226,6 +226,7 @@ const char nsXPLookAndFeel::sColorPrefs[][38] = "ui.-moz-mac-menutextselect", "ui.-moz_mac_disabledtoolbartext", "ui.-moz-mac-secondaryhighlight", + "ui.-moz-win-accentcolor", "ui.-moz-win-mediatext", "ui.-moz-win-communicationstext", "ui.-moz-nativehyperlinktext", diff --git a/widget/windows/nsLookAndFeel.cpp b/widget/windows/nsLookAndFeel.cpp index cb3bcc80ea..10152e4581 100644 --- a/widget/windows/nsLookAndFeel.cpp +++ b/widget/windows/nsLookAndFeel.cpp @@ -277,6 +277,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 (IsVistaOrLater() && IsAppThemed()) { res = ::GetColorFromTheme(eUXMediaToolbar, @@ -677,3 +687,43 @@ nsLookAndFeel::GetPasswordCharacterImpl() #define UNICODE_BLACK_CIRCLE_CHAR 0x25cf return UNICODE_BLACK_CIRCLE_CHAR; } + +/* 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; +} \ No newline at end of file diff --git a/widget/windows/nsLookAndFeel.h b/widget/windows/nsLookAndFeel.h index f83dcb8281..afcae46177 100644 --- a/widget/windows/nsLookAndFeel.h +++ b/widget/windows/nsLookAndFeel.h @@ -5,7 +5,9 @@ #ifndef __nsLookAndFeel #define __nsLookAndFeel + #include "nsXPLookAndFeel.h" +#include "nsIWindowsRegKey.h" /* * Gesture System Metrics @@ -34,6 +36,15 @@ public: gfxFontStyle& aFontStyle, float aDevPixPerCSSPixel); virtual char16_t GetPasswordCharacterImpl(); +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); + + nsCOMPtr mDwmKey; }; #endif diff --git a/widget/windows/nsWindow.cpp b/widget/windows/nsWindow.cpp index 53838e36a3..f215e27c7c 100644 --- a/widget/windows/nsWindow.cpp +++ b/widget/windows/nsWindow.cpp @@ -4630,6 +4630,19 @@ nsWindow::ProcessMessage(UINT msg, WPARAM& wParam, LPARAM& lParam, } break; + case WM_SETTINGCHANGE: + { + if (lParam) { + auto lParamString = reinterpret_cast(lParam); + if (!wcscmp(lParamString, L"ImmersiveColorSet")) { + // WM_SYSCOLORCHANGE is not dispatched for accent color changes + OnSysColorChanged(); + break; + } + } + } + break; + case WM_NCCALCSIZE: { if (mCustomNonClient) {