diff --git a/browser/themes/brave_theme_service.cc b/browser/themes/brave_theme_service.cc index 4f4cb4975d0e..1455e7fd905e 100644 --- a/browser/themes/brave_theme_service.cc +++ b/browser/themes/brave_theme_service.cc @@ -27,6 +27,10 @@ #include "ui/native_theme/native_theme.h" #include "ui/native_theme/native_theme_dark_aura.h" +#if defined(OS_WIN) +#include "ui/native_theme/native_theme_win.h" +#endif + namespace { BraveThemeType GetThemeTypeBasedOnChannel() { switch (chrome::GetChannel()) { @@ -151,6 +155,9 @@ void BraveThemeService::Init(Profile* profile) { if (profile->GetPrefs()->FindPreference(kBraveThemeType)) { RecoverPrefStates(profile); OverrideDefaultThemeIfNeeded(profile); +#if defined(OS_WIN) + OverrideSystemDarkModeIfNeeded(profile); +#endif if (SystemThemeModeEnabled()) { // Start with proper system theme to make brave theme and // base ui components theme use same theme. @@ -203,7 +210,10 @@ void BraveThemeService::OnPreferenceChanged(const std::string& pref_name) { SetSystemTheme(static_cast( profile()->GetPrefs()->GetInteger(kBraveThemeType))); } +#elif defined(OS_WIN) + OverrideSystemDarkModeIfNeeded(profile()); #endif + if (notify_theme_observer_here) { // Notify dark (cross-platform) and light (platform-specific) variants // When theme is changed from light to dark, we notify to light theme @@ -234,6 +244,21 @@ void BraveThemeService::OverrideDefaultThemeIfNeeded(Profile* profile) { } } +#if defined(OS_WIN) +void BraveThemeService::OverrideSystemDarkModeIfNeeded(Profile* profile) { + if (SystemThemeModeEnabled()) { + BraveThemeType type = static_cast( + profile->GetPrefs()->GetInteger(kBraveThemeType)); + // Overrides system dark mode only when brave theme type is not + // "Same as Windows". In this case, follow os theme. + // Otherwise, use brave theme as a native theme regardless of os theme. + ui::SetOverrideSystemDarkMode( + type != BraveThemeType::BRAVE_THEME_TYPE_DEFAULT, + type == BraveThemeType::BRAVE_THEME_TYPE_DARK); + } +} +#endif + void BraveThemeService::SetBraveThemeEventRouterForTesting( extensions::BraveThemeEventRouter* mock_router) { brave_theme_event_router_.reset(mock_router); diff --git a/browser/themes/brave_theme_service.h b/browser/themes/brave_theme_service.h index d5580b3bac54..3f25cb5e1ede 100644 --- a/browser/themes/brave_theme_service.h +++ b/browser/themes/brave_theme_service.h @@ -64,6 +64,9 @@ class BraveThemeService : public ThemeService { void RecoverPrefStates(Profile* profile); void OverrideDefaultThemeIfNeeded(Profile* profile); +#if defined(OS_WIN) + void OverrideSystemDarkModeIfNeeded(Profile* profile); +#endif static bool SystemThemeModeEnabled(); diff --git a/browser/themes/brave_theme_service_browsertest.cc b/browser/themes/brave_theme_service_browsertest.cc index 3b80fe432475..94c82c54bfc8 100644 --- a/browser/themes/brave_theme_service_browsertest.cc +++ b/browser/themes/brave_theme_service_browsertest.cc @@ -121,10 +121,8 @@ IN_PROC_BROWSER_TEST_F(BraveThemeServiceTest, NativeThemeObserverTest) { SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_LIGHT); } -#if defined(OS_MACOSX) +#if defined(OS_MACOSX) || defined(OS_WIN) IN_PROC_BROWSER_TEST_F(BraveThemeServiceTest, SystemThemeChangeTest) { - // TODO(simonhong): Delete this when we gets dark mode enabled branch on - // MacOS. if (!BraveThemeService::SystemThemeModeEnabled()) return; diff --git a/chromium_src/ui/native_theme/native_theme_win.cc b/chromium_src/ui/native_theme/native_theme_win.cc index 1e429495a97f..5609d1ea8136 100644 --- a/chromium_src/ui/native_theme/native_theme_win.cc +++ b/chromium_src/ui/native_theme/native_theme_win.cc @@ -4,10 +4,12 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "ui/native_theme/native_theme_dark_aura.h" +#include "ui/native_theme/native_theme_win.h" -void NotifyProperThemeObserver(); +namespace { -#include "../../../../ui/native_theme/native_theme_win.cc" // NOLINT +bool system_dark_mode_overridden = false; +bool dark_mode_enabled = false; // TODO(simonhong): Move this function to ui namespace to share with // native_theme_mac.mm. @@ -18,3 +20,26 @@ void NotifyProperThemeObserver() { ? ui::NativeTheme::GetInstanceForNativeUi()->NotifyObservers() : ui::NativeThemeDarkAura::instance()->NotifyObservers(); } + +bool OverrideSystemDarkMode() { + return system_dark_mode_overridden; +} + +bool GetSystemDarkModeEnabledOverrides() { + return dark_mode_enabled; +} + +} // namespace + +#include "../../../../ui/native_theme/native_theme_win.cc" // NOLINT + +namespace ui { + +void SetOverrideSystemDarkMode(bool override, + bool enable_dark_mode) { + system_dark_mode_overridden = override; + dark_mode_enabled = enable_dark_mode; +} + +} // namespace ui + diff --git a/chromium_src/ui/native_theme/native_theme_win.h b/chromium_src/ui/native_theme/native_theme_win.h new file mode 100644 index 000000000000..5589ce4cf59b --- /dev/null +++ b/chromium_src/ui/native_theme/native_theme_win.h @@ -0,0 +1,21 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "../../../../ui/native_theme/native_theme_win.h" + +// Custom header guard is used to avoid including function declaration multiple +// times. +#ifndef UI_NATIVE_THEME_NATIVE_THEME_WIN_H_BRAVE_ // NOLINT +#define UI_NATIVE_THEME_NATIVE_THEME_WIN_H_BRAVE_ // NOLINT + +namespace ui { + +// If |override| is true, system dark mode is overridden by |enable_dark_mode|. +// If |override| is false, |enable_dark_mode| is ignored. +void NATIVE_THEME_EXPORT SetOverrideSystemDarkMode(bool override, + bool enable_dark_mode); +} // namespace ui + +#endif // UI_NATIVE_THEME_NATIVE_THEME_WIN_H_BRAVE_ // NOLINT diff --git a/patches/ui-native_theme-native_theme_win.cc.patch b/patches/ui-native_theme-native_theme_win.cc.patch index 1c2a4affc7ba..89e0dda67ff4 100644 --- a/patches/ui-native_theme-native_theme_win.cc.patch +++ b/patches/ui-native_theme-native_theme_win.cc.patch @@ -1,8 +1,16 @@ diff --git a/ui/native_theme/native_theme_win.cc b/ui/native_theme/native_theme_win.cc -index 0df107bf9757a81897e9312a9eba977b8a173646..61e4f4ce671c5e0f92208b54aade903110ddc7a7 100644 +index 0df107bf9757a81897e9312a9eba977b8a173646..760ea5114e984b4536b2f894f8e7a2fb1a24b372 100644 --- a/ui/native_theme/native_theme_win.cc +++ b/ui/native_theme/native_theme_win.cc -@@ -1934,7 +1934,11 @@ void NativeThemeWin::RegisterThemeRegkeyObserver() { +@@ -592,6 +592,7 @@ bool NativeThemeWin::SystemDarkModeEnabled() const { + // ...unless --force-dark-mode was specified in which case caveat emptor. + if (UsesHighContrastColors() && !NativeTheme::SystemDarkModeEnabled()) + return false; ++ if (OverrideSystemDarkMode()) return GetSystemDarkModeEnabledOverrides(); + bool fDarkModeEnabled = false; + if (hkcu_themes_regkey_.Valid()) { + DWORD apps_use_light_theme = 1; +@@ -1934,7 +1935,11 @@ void NativeThemeWin::RegisterThemeRegkeyObserver() { DCHECK(hkcu_themes_regkey_.Valid()); hkcu_themes_regkey_.StartWatching(base::BindOnce( [](NativeThemeWin* native_theme) {