Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use brave theme type as a system dark mode on Windows (uplift to 0.65.x) #2410

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions browser/themes/brave_theme_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -203,7 +210,10 @@ void BraveThemeService::OnPreferenceChanged(const std::string& pref_name) {
SetSystemTheme(static_cast<BraveThemeType>(
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
Expand Down Expand Up @@ -234,6 +244,21 @@ void BraveThemeService::OverrideDefaultThemeIfNeeded(Profile* profile) {
}
}

#if defined(OS_WIN)
void BraveThemeService::OverrideSystemDarkModeIfNeeded(Profile* profile) {
if (SystemThemeModeEnabled()) {
BraveThemeType type = static_cast<BraveThemeType>(
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);
Expand Down
3 changes: 3 additions & 0 deletions browser/themes/brave_theme_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 1 addition & 3 deletions browser/themes/brave_theme_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
29 changes: 27 additions & 2 deletions chromium_src/ui/native_theme/native_theme_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

21 changes: 21 additions & 0 deletions chromium_src/ui/native_theme/native_theme_win.h
Original file line number Diff line number Diff line change
@@ -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
12 changes: 10 additions & 2 deletions patches/ui-native_theme-native_theme_win.cc.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
diff --git a/ui/native_theme/native_theme_win.cc b/ui/native_theme/native_theme_win.cc
index 040374661374b565372fd62822ffccd7c963fd19..2d3cef44d7423223adccb32ac306309fdc5a19d6 100644
index 040374661374b565372fd62822ffccd7c963fd19..7acd014697cd7f05bd11b2e8b5365076a177df57 100644
--- a/ui/native_theme/native_theme_win.cc
+++ b/ui/native_theme/native_theme_win.cc
@@ -1933,7 +1933,11 @@ void NativeThemeWin::RegisterThemeRegkeyObserver() {
@@ -591,6 +591,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;
@@ -1933,7 +1934,11 @@ void NativeThemeWin::RegisterThemeRegkeyObserver() {
DCHECK(hkcu_themes_regkey_.Valid());
hkcu_themes_regkey_.StartWatching(base::BindOnce(
[](NativeThemeWin* native_theme) {
Expand Down