Skip to content

Commit

Permalink
Use brave theme type as a system dark mode on Windows
Browse files Browse the repository at this point in the history
Aligning base UI theme(ex, context menu) with brave theme was not
implemented on Windows because Windows doesn't support per-application
theme. Whereas, MacOS support it. So, when user set dark or light, it is
set as a application theme.
To solve this on Windows, NativeThemeWin::SystemDarkModeEnabled() checks
whether dark mode enabling is overridden or not.
Overridden value is set by client(BraveThemeService).
  • Loading branch information
simonhong committed May 9, 2019
1 parent f13e6cc commit 2c1bc58
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 7 deletions.
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 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) {
Expand Down

0 comments on commit 2c1bc58

Please sign in to comment.