From d0d1e1c00414a2d640465a861c9f4fc90d423e27 Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Mon, 27 Aug 2018 07:49:28 +0900 Subject: [PATCH] Add brave theme option to settings page --- app/brave_generated_resources.grd | 4 + browser/ui/BUILD.gn | 2 + browser/ui/webui/brave_md_settings_ui.cc | 4 +- .../settings/brave_appearance_handler.cc | 84 +++++++++++++++++++ .../webui/settings/brave_appearance_handler.h | 31 +++++++ .../md_settings_localized_strings_provider.cc | 4 +- ...nce_page-appearance_browser_proxy.js.patch | 41 +++++++++ ...appearance_page-appearance_page.html.patch | 25 ++++++ ...s-appearance_page-appearance_page.js.patch | 59 +++++++++++++ 9 files changed, 251 insertions(+), 3 deletions(-) create mode 100644 browser/ui/webui/settings/brave_appearance_handler.cc create mode 100644 browser/ui/webui/settings/brave_appearance_handler.h create mode 100644 patches/chrome-browser-resources-settings-appearance_page-appearance_browser_proxy.js.patch create mode 100644 patches/chrome-browser-resources-settings-appearance_page-appearance_page.html.patch create mode 100644 patches/chrome-browser-resources-settings-appearance_page-appearance_page.js.patch diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index c3ece127859c..34830a186bf9 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -138,6 +138,10 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U Continue blocking autoplay + + + Brave Themes + diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index 4da47e14795a..6e0d04310a11 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -49,6 +49,8 @@ source_set("ui") { "webui/brave_webui_source.h", "webui/brave_welcome_ui.cc", "webui/brave_welcome_ui.h", + "webui/settings/brave_appearance_handler.cc", + "webui/settings/brave_appearance_handler.h", ] if (is_linux) { diff --git a/browser/ui/webui/brave_md_settings_ui.cc b/browser/ui/webui/brave_md_settings_ui.cc index 55ec6ae9d368..7828053b9808 100644 --- a/browser/ui/webui/brave_md_settings_ui.cc +++ b/browser/ui/webui/brave_md_settings_ui.cc @@ -4,14 +4,14 @@ #include "brave/browser/ui/webui/brave_md_settings_ui.h" +#include "brave/browser/ui/webui/settings/brave_appearance_handler.h" #include "chrome/browser/ui/webui/settings/metrics_reporting_handler.h" BraveMdSettingsUI::BraveMdSettingsUI(content::WebUI* web_ui, const std::string& host) : MdSettingsUI(web_ui) { -#if defined(BRAVE_CHROMIUM_BUILD) web_ui->AddMessageHandler(std::make_unique()); -#endif + web_ui->AddMessageHandler(std::make_unique()); } BraveMdSettingsUI::~BraveMdSettingsUI() { diff --git a/browser/ui/webui/settings/brave_appearance_handler.cc b/browser/ui/webui/settings/brave_appearance_handler.cc new file mode 100644 index 000000000000..b0e81d8d7238 --- /dev/null +++ b/browser/ui/webui/settings/brave_appearance_handler.cc @@ -0,0 +1,84 @@ +/* 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/ui/webui/settings/brave_appearance_handler.h" + +#include + +#include "base/bind.h" +#include "base/values.h" +#include "brave/browser/themes/brave_theme_service.h" +#include "brave/common/pref_names.h" +#include "chrome/browser/profiles/profile.h" +#include "components/prefs/pref_service.h" +#include "content/public/browser/web_ui.h" + +namespace { +void SetBraveThemeTypePref(Profile* profile, + BraveThemeService::BraveThemeType type) { + profile->GetPrefs()->SetInteger(kBraveThemeType, type); +} + +BraveThemeService::BraveThemeType GetBraveThemeTypeFromString( + base::StringPiece theme) { + if (theme == "Default") + return BraveThemeService::BRAVE_THEME_TYPE_DEFAULT; + + if (theme == "Light") + return BraveThemeService::BRAVE_THEME_TYPE_LIGHT; + + if (theme == "Dark") + return BraveThemeService::BRAVE_THEME_TYPE_DARK; + + NOTREACHED(); + return BraveThemeService::BRAVE_THEME_TYPE_DEFAULT; +} + +std::string GetStringFromBraveThemeType( + BraveThemeService::BraveThemeType theme) { + switch (theme) { + case BraveThemeService::BRAVE_THEME_TYPE_DEFAULT: + return "Default"; + case BraveThemeService::BRAVE_THEME_TYPE_LIGHT: + return "Light"; + case BraveThemeService::BRAVE_THEME_TYPE_DARK: + return "Dark"; + default: + NOTREACHED(); + } +} +} // namespace + +void BraveAppearanceHandler::RegisterMessages() { + profile_ = Profile::FromWebUI(web_ui()); + + web_ui()->RegisterMessageCallback( + "getBraveThemeType", + base::BindRepeating(&BraveAppearanceHandler::GetBraveThemeType, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "setBraveThemeType", + base::BindRepeating(&BraveAppearanceHandler::SetBraveThemeType, + base::Unretained(this))); +} + +void BraveAppearanceHandler::SetBraveThemeType(const base::ListValue* args) { + CHECK_EQ(args->GetSize(), 1U); + CHECK(profile_); + + std::string theme; + args->GetString(0, &theme); + SetBraveThemeTypePref(profile_, GetBraveThemeTypeFromString(theme)); +} + +void BraveAppearanceHandler::GetBraveThemeType(const base::ListValue* args) { + CHECK_EQ(args->GetSize(), 1U); + CHECK(profile_); + + AllowJavascript(); + ResolveJavascriptCallback( + args->GetList()[0].Clone(), + base::Value(GetStringFromBraveThemeType( + BraveThemeService::GetBraveThemeType(profile_)))); +} diff --git a/browser/ui/webui/settings/brave_appearance_handler.h b/browser/ui/webui/settings/brave_appearance_handler.h new file mode 100644 index 000000000000..56f3c1f94af3 --- /dev/null +++ b/browser/ui/webui/settings/brave_appearance_handler.h @@ -0,0 +1,31 @@ +/* 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_UI_WEBUI_SETTINGS_BRAVE_APPEARANCE_HANDLER_H_ +#define BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_APPEARANCE_HANDLER_H_ + +#include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h" + +class Profile; + +class BraveAppearanceHandler : public settings::SettingsPageUIHandler { + public: + BraveAppearanceHandler() = default; + ~BraveAppearanceHandler() override = default; + + private: + // SettingsPageUIHandler overrides: + void RegisterMessages() override; + void OnJavascriptAllowed() override {} + void OnJavascriptDisallowed() override {} + + void SetBraveThemeType(const base::ListValue* args); + void GetBraveThemeType(const base::ListValue* args); + + Profile* profile_ = nullptr; + + DISALLOW_COPY_AND_ASSIGN(BraveAppearanceHandler); +}; + +#endif // BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_APPEARANCE_HANDLER_H_ diff --git a/chromium_src/chrome/browser/ui/webui/settings/md_settings_localized_strings_provider.cc b/chromium_src/chrome/browser/ui/webui/settings/md_settings_localized_strings_provider.cc index 18a597e8893e..263924e234d0 100644 --- a/chromium_src/chrome/browser/ui/webui/settings/md_settings_localized_strings_provider.cc +++ b/chromium_src/chrome/browser/ui/webui/settings/md_settings_localized_strings_provider.cc @@ -26,7 +26,9 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source, Profile* profi {"siteSettingsAutoplayAsk", IDS_SETTINGS_SITE_SETTINGS_AUTOPLAY_ASK}, {"siteSettingsAutoplayAskRecommended", - IDS_SETTINGS_SITE_SETTINGS_AUTOPLAY_ASK_RECOMMENDED} + IDS_SETTINGS_SITE_SETTINGS_AUTOPLAY_ASK_RECOMMENDED}, + {"appearanceSettingsBraveTheme", + IDS_SETTINGS_APPEARANCE_SETTINGS_BRAVE_THEMES}, }; AddLocalizedStringsBulk(html_source, localized_strings, arraysize(localized_strings)); diff --git a/patches/chrome-browser-resources-settings-appearance_page-appearance_browser_proxy.js.patch b/patches/chrome-browser-resources-settings-appearance_page-appearance_browser_proxy.js.patch new file mode 100644 index 000000000000..a461ac257274 --- /dev/null +++ b/patches/chrome-browser-resources-settings-appearance_page-appearance_browser_proxy.js.patch @@ -0,0 +1,41 @@ +diff --git a/chrome/browser/resources/settings/appearance_page/appearance_browser_proxy.js b/chrome/browser/resources/settings/appearance_page/appearance_browser_proxy.js +index c75464dc66ecedd8dbfac47b0dbdb4b6c68fd969..fd62faf174909c5c385277c08bb782bd4127d442 100644 +--- a/chrome/browser/resources/settings/appearance_page/appearance_browser_proxy.js ++++ b/chrome/browser/resources/settings/appearance_page/appearance_browser_proxy.js +@@ -45,6 +45,17 @@ cr.define('settings', function() { + * @return {!Promise} + */ + validateStartupPage(url) {} ++ ++ // ++ /** ++ * @return {!Promise} ++ */ ++ getBraveThemeType() {} ++ /** ++ * @param {string} theme name. ++ */ ++ setBraveThemeType(theme) {} ++ // + } + + /** +@@ -105,6 +116,18 @@ cr.define('settings', function() { + validateStartupPage(url) { + return cr.sendWithPromise('validateStartupPage', url); + } ++ ++ // ++ /** @override */ ++ getBraveThemeType() { ++ return cr.sendWithPromise('getBraveThemeType'); ++ } ++ ++ /** @override */ ++ setBraveThemeType(theme) { ++ chrome.send('setBraveThemeType', [theme]); ++ } ++ // + } + + cr.addSingletonGetter(AppearanceBrowserProxyImpl); diff --git a/patches/chrome-browser-resources-settings-appearance_page-appearance_page.html.patch b/patches/chrome-browser-resources-settings-appearance_page-appearance_page.html.patch new file mode 100644 index 000000000000..998c9080d458 --- /dev/null +++ b/patches/chrome-browser-resources-settings-appearance_page-appearance_page.html.patch @@ -0,0 +1,25 @@ +diff --git a/chrome/browser/resources/settings/appearance_page/appearance_page.html b/chrome/browser/resources/settings/appearance_page/appearance_page.html +index 720d25d126a118315a08cef90d77d1fc78775254..5072dfc50d1108b2eea75c6079ae144395d5c04b 100644 +--- a/chrome/browser/resources/settings/appearance_page/appearance_page.html ++++ b/chrome/browser/resources/settings/appearance_page/appearance_page.html +@@ -175,6 +175,20 @@ + + + ++ ++
++
$i18n{appearanceSettingsBraveTheme}
++ ++
++
+ + diff --git a/patches/chrome-browser-resources-settings-appearance_page-appearance_page.js.patch b/patches/chrome-browser-resources-settings-appearance_page-appearance_page.js.patch new file mode 100644 index 000000000000..5c5c1633e47c --- /dev/null +++ b/patches/chrome-browser-resources-settings-appearance_page-appearance_page.js.patch @@ -0,0 +1,59 @@ +diff --git a/chrome/browser/resources/settings/appearance_page/appearance_page.js b/chrome/browser/resources/settings/appearance_page/appearance_page.js +index 1c7e3474a595250da00bc9249a047e4e1eae1786..e585821e63703e66c9f06f928ec35713d15f570f 100644 +--- a/chrome/browser/resources/settings/appearance_page/appearance_page.js ++++ b/chrome/browser/resources/settings/appearance_page/appearance_page.js +@@ -87,6 +87,20 @@ Polymer({ + ], + }, + ++ // ++ braveThemeTypes_: { ++ readOnly: true, ++ type: Array, ++ value: [ ++ 'Default', ++ 'Light', ++ 'Dark', ++ ], ++ }, ++ ++ braveThemeType_: String, ++ // ++ + /** @private */ + themeSublabel_: String, + +@@ -150,6 +164,11 @@ Polymer({ + this.isWallpaperPolicyControlled_ = isPolicyControlled; + }); + // ++ // ++ this.browserProxy_.getBraveThemeType().then(theme => { ++ this.braveThemeType_ = theme; ++ }); ++ // + }, + + /** +@@ -323,5 +342,21 @@ Polymer({ + zoomValuesEqual_: function(zoom1, zoom2) { + return Math.abs(zoom1 - zoom2) <= 0.001; + }, ++ ++ // ++ /** ++ * @param {string} theme1 ++ * @param {string} theme2 ++ * @return {boolean} ++ * @private ++ */ ++ braveThemeTypeEqual_: function(theme1, theme2) { ++ return theme1 === theme2; ++ }, ++ ++ onBraveThemeTypeChange_: function() { ++ this.browserProxy_.setBraveThemeType(this.$.braveThemeType.value); ++ }, ++ // + }); + })();