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

Brave theme #351

Merged
merged 7 commits into from
Aug 24, 2018
Merged
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
3 changes: 3 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "brave/browser/brave_profile_prefs.h"

#include "brave/browser/alternate_private_search_engine_util.h"
#include "brave/browser/themes/brave_theme_service.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "chrome/browser/net/prediction_options.h"
Expand All @@ -24,6 +25,8 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {

RegisterAlternatePrivateSearchEngineProfilePrefs(registry);

BraveThemeService::RegisterProfilePrefs(registry);

registry->RegisterBooleanPref(kWidevineOptedIn, false);

// No sign into Brave functionality
Expand Down
11 changes: 11 additions & 0 deletions browser/themes/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
source_set("themes") {
sources = [
"brave_theme_service.cc",
"brave_theme_service.h",
"brave_theme_service_win.cc",
"brave_theme_service_win.h",
"theme_properties.cc",
"theme_properties.h",
]

deps = [
"//base",
"//brave/common",
"//chrome/common",
"//components/prefs",
"//components/pref_registry",
"//components/version_info",
"//skia",
]
}
51 changes: 51 additions & 0 deletions browser/themes/brave_theme_service.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* 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 "brave/browser/themes/brave_theme_service.h"

#include "brave/browser/themes/theme_properties.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"

// static
void BraveThemeService::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterIntegerPref(kBraveThemeType, BRAVE_THEME_TYPE_DEFAULT);
}

// static
BraveThemeService::BraveThemeType BraveThemeService::GetBraveThemeType(Profile* profile) {
return static_cast<BraveThemeService::BraveThemeType>(
profile->GetPrefs()->GetInteger(kBraveThemeType));
}

BraveThemeService::BraveThemeService() {}

BraveThemeService::~BraveThemeService() {}

void BraveThemeService::Init(Profile* profile) {
brave_theme_type_pref_.Init(
kBraveThemeType,
profile->GetPrefs(),
base::Bind(&BraveThemeService::OnPreferenceChanged,
base::Unretained(this)));
ThemeService::Init(profile);
}

SkColor BraveThemeService::GetDefaultColor(int id, bool incognito) const {
const base::Optional<SkColor> braveColor =
MaybeGetDefaultColorForBraveUi(id, incognito, profile());
if (braveColor)
return braveColor.value();

return ThemeService::GetDefaultColor(id, incognito);
}

void BraveThemeService::OnPreferenceChanged(const std::string& pref_name) {
DCHECK(pref_name == kBraveThemeType);
NotifyThemeChanged();
}
44 changes: 44 additions & 0 deletions browser/themes/brave_theme_service.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* 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/. */

#ifndef BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_H_
#define BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_H_

#include "chrome/browser/themes/theme_service.h"
#include "components/prefs/pref_member.h"

namespace user_prefs {
class PrefRegistrySyncable;
}

class BraveThemeService : public ThemeService {
public:
enum BraveThemeType {
BRAVE_THEME_TYPE_DEFAULT, // Choose theme by channel
BRAVE_THEME_TYPE_DARK, // Use dark theme regardless of channel
BRAVE_THEME_TYPE_LIGHT, // Use light theme regardless of channel
};

static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
static BraveThemeType GetBraveThemeType(Profile* profile);

BraveThemeService();
~BraveThemeService() override;

// ThemeService overrides:
void Init(Profile* profile) override;

protected:
// ThemeService overrides:
SkColor GetDefaultColor(int id, bool incognito) const override;

private:
void OnPreferenceChanged(const std::string& pref_name);

IntegerPrefMember brave_theme_type_pref_;

DISALLOW_COPY_AND_ASSIGN(BraveThemeService);
};

#endif // BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_H_
46 changes: 46 additions & 0 deletions browser/themes/brave_theme_service_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* 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 "brave/browser/themes/brave_theme_service.h"
#include "brave/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/ui/browser.h"
#include "components/prefs/pref_service.h"

using BraveThemeServiceTest = InProcessBrowserTest;
using BTS = BraveThemeService;

namespace {
void SetBraveThemeType(Profile* profile, BTS::BraveThemeType type) {
profile->GetPrefs()->SetInteger(kBraveThemeType, type);
}
} // namespace

IN_PROC_BROWSER_TEST_F(BraveThemeServiceTest, BraveThemeChangeTest) {
Profile* profile = browser()->profile();
#if defined(OFFICIAL_BUILD)
const SkColor light_frame_color = SkColorSetRGB(0xD8, 0xDE, 0xE1);
#endif
const SkColor dark_frame_color = SkColorSetRGB(0x58, 0x5B, 0x5E);

// Check default type is set initially.
EXPECT_EQ(BTS::BRAVE_THEME_TYPE_DEFAULT, BTS::GetBraveThemeType(profile));

const ui::ThemeProvider& tp = ThemeService::GetThemeProviderForProfile(profile);
SetBraveThemeType(browser()->profile(), BTS::BRAVE_THEME_TYPE_LIGHT);
EXPECT_EQ(BTS::BRAVE_THEME_TYPE_LIGHT, BTS::GetBraveThemeType(profile));
#if defined(OFFICIAL_BUILD)
EXPECT_EQ(light_frame_color, tp.GetColor(ThemeProperties::COLOR_FRAME));
#else
// Non-official build always uses dark theme.
EXPECT_EQ(dark_frame_color, tp.GetColor(ThemeProperties::COLOR_FRAME));
#endif

SetBraveThemeType(browser()->profile(), BTS::BRAVE_THEME_TYPE_DARK);
EXPECT_EQ(BTS::BRAVE_THEME_TYPE_DARK, BTS::GetBraveThemeType(profile));
EXPECT_EQ(dark_frame_color, tp.GetColor(ThemeProperties::COLOR_FRAME));
}
16 changes: 16 additions & 0 deletions browser/themes/brave_theme_service_win.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* 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 "brave/browser/themes/brave_theme_service_win.h"

#include "chrome/browser/themes/theme_properties.h"

SkColor BraveThemeServiceWin::GetDefaultColor(int id, bool incognito) const {
// Prevent dcheck in chrome/browser/themes/theme_properties.cc(384)
// It assumes this id handled in theme service.
if (DwmColorsAllowed() && id == ThemeProperties::COLOR_ACCENT_BORDER)
return dwm_accent_border_color_;
// Skip ThemeServiceWin::GetDefaultColor() to prevent using dwm frame color.
return BraveThemeService::GetDefaultColor(id, incognito);
}
22 changes: 22 additions & 0 deletions browser/themes/brave_theme_service_win.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* 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/. */

#ifndef BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_WIN_H_
#define BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_WIN_H_

#include "chrome/browser/themes/theme_service_win.h"

class BraveThemeServiceWin : public ThemeServiceWin {
public:
BraveThemeServiceWin() = default;
~BraveThemeServiceWin() override = default;

private:
// ThemeServiceWin overrides:
SkColor GetDefaultColor(int id, bool incognito) const override;

DISALLOW_COPY_AND_ASSIGN(BraveThemeServiceWin);
};

#endif // BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_WIN_H_
34 changes: 22 additions & 12 deletions browser/themes/theme_properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

#include "brave/browser/themes/theme_properties.h"

#include "brave/browser/themes/brave_theme_service.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/common/channel_info.h"
#include "components/prefs/pref_service.h"
#include "components/version_info/channel.h"
#include "ui/gfx/color_palette.h"

namespace {

Expand Down Expand Up @@ -70,22 +73,29 @@ base::Optional<SkColor> MaybeGetDefaultColorForBraveUiDevChannel(int id, bool in
} // namespace

// Returns a |nullopt| if the UI color is not handled by Brave.
base::Optional<SkColor> MaybeGetDefaultColorForBraveUi(int id, bool incognito) {
if (id == BRAVE_COLOR_FOR_TEST) {
return SkColorSetRGB(11, 13, 17);
}
base::Optional<SkColor> MaybeGetDefaultColorForBraveUi(int id, bool incognito, Profile* profile) {
#if !defined(OFFICIAL_BUILD)
return MaybeGetDefaultColorForBraveUiDevChannel(id, incognito);
#else
switch (chrome::GetChannel()) {
case version_info::Channel::STABLE:
case version_info::Channel::BETA:
switch (BraveThemeService::GetBraveThemeType(profile)) {
case BraveThemeService::BRAVE_THEME_TYPE_DEFAULT:
switch (chrome::GetChannel()) {
case version_info::Channel::STABLE:
case version_info::Channel::BETA:
return MaybeGetDefaultColorForBraveUiReleaseChannel(id, incognito);
case version_info::Channel::DEV:
case version_info::Channel::CANARY:
case version_info::Channel::UNKNOWN:
default:
return MaybeGetDefaultColorForBraveUiDevChannel(id, incognito);
}
case BraveThemeService::BRAVE_THEME_TYPE_LIGHT:
return MaybeGetDefaultColorForBraveUiReleaseChannel(id, incognito);
case version_info::Channel::DEV:
case version_info::Channel::CANARY:
case version_info::Channel::UNKNOWN:
default:
case BraveThemeService::BRAVE_THEME_TYPE_DARK:
return MaybeGetDefaultColorForBraveUiDevChannel(id, incognito);
default:
NOTREACHED();

}
#endif
return base::nullopt;
Expand Down
8 changes: 3 additions & 5 deletions browser/themes/theme_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
#include "base/optional.h"
#include "third_party/skia/include/core/SkColor.h"

#define BRAVE_COLOR_FOR_TEST 0x7FFFFFFF
class Profile;

base::Optional<SkColor> MaybeGetDefaultColorForBraveUi(int id, bool incognito);
#define BRAVE_COLOR_FOR_TEST 0x7FFFFFFF

#define MAYBE_OVERRIDE_DEFAULT_COLOR_FOR_BRAVE(id, incognito) \
const base::Optional<SkColor> braveColor = MaybeGetDefaultColorForBraveUi(id, incognito); \
if (braveColor) return braveColor.value();
base::Optional<SkColor> MaybeGetDefaultColorForBraveUi(int id, bool incognito, Profile* profile);

#endif // BRAVE_BROWSER_THEMES_THEME_PROPERTIES_H_
13 changes: 0 additions & 13 deletions browser/themes/theme_properties_unittest.cc

This file was deleted.

10 changes: 10 additions & 0 deletions chromium_src/chrome/browser/themes/theme_service_aurax11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* 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 "brave/browser/themes/brave_theme_service.h"

#undef ThemeService
#define ThemeService BraveThemeService
#include "../../../../../chrome/browser/themes/theme_service_aurax11.h"
#undef ThemeService
18 changes: 18 additions & 0 deletions chromium_src/chrome/browser/themes/theme_service_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* 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 "build/build_config.h"
#include "chrome/browser/themes/theme_service.h"

#if defined(OS_WIN)
#include "brave/browser/themes/brave_theme_service_win.h"
#undef ThemeServiceWin
#define ThemeServiceWin BraveThemeServiceWin
#elif !defined(USE_X11)
#include "brave/browser/themes/brave_theme_service.h"
#undef ThemeService
#define ThemeService BraveThemeService
#endif

#include "../../../../../chrome/browser/themes/theme_service_factory.cc"
13 changes: 13 additions & 0 deletions chromium_src/chrome/browser/themes/theme_service_win.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* 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 "brave/browser/themes/brave_theme_service.h"

#undef ThemeService
#define ThemeService BraveThemeService

#include "../../../../../chrome/browser/themes/theme_service_win.h"

#undef ThemeService
#define ThemeService ThemeService
1 change: 1 addition & 0 deletions common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ const char kAdBlockCurrentRegion[] = "brave.ad_block.current_region";
const char kWidevineOptedIn[] = "brave.widevine_opted_in";
const char kUseAlternatePrivateSearchEngine[] =
"brave.use_alternate_private_search_engine";
const char kBraveThemeType[] = "brave.theme.type";
3 changes: 2 additions & 1 deletion common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ extern const char kWeekOfInstallation[];
extern const char kAdBlockCurrentRegion[];
extern const char kWidevineOptedIn[];
extern const char kUseAlternatePrivateSearchEngine[];
extern const char kBraveThemeType[];

#endif
#endif // BRAVE_COMMON_PREF_NAMES_H_
20 changes: 0 additions & 20 deletions patches/chrome-browser-themes-theme_properties.cc.patch

This file was deleted.

12 changes: 12 additions & 0 deletions patches/chrome-browser-themes-theme_service_win.h.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/chrome/browser/themes/theme_service_win.h b/chrome/browser/themes/theme_service_win.h
index eba1cb2596d71b91e06183209503f3fc83b0e593..e6261a982859a6a66caa00d362f290d247ce7922 100644
--- a/chrome/browser/themes/theme_service_win.h
+++ b/chrome/browser/themes/theme_service_win.h
@@ -18,6 +18,7 @@ class ThemeServiceWin : public ThemeService {
~ThemeServiceWin() override;

private:
+ friend class BraveThemeServiceWin;
// ThemeService:
bool ShouldUseNativeFrame() const override;
SkColor GetDefaultColor(int id, bool incognito) const override;
Loading