From cb470cb94ba6034e4fbac6a70d0cf4345af31c90 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 2 Apr 2016 20:35:57 +0900 Subject: [PATCH] Use BrowserWindow's backgroundColor as renderer view's background color --- atom/browser/api/atom_api_window.cc | 4 +++ atom/browser/native_window.cc | 23 ---------------- atom/browser/native_window.h | 3 --- atom/browser/native_window_mac.mm | 3 ++- atom/browser/native_window_views.cc | 3 ++- atom/browser/web_contents_preferences.cc | 5 ++++ atom/common/color_util.cc | 34 ++++++++++++++++++++++++ atom/common/color_util.h | 19 +++++++++++++ atom/common/options_switches.cc | 13 ++++----- atom/common/options_switches.h | 1 + atom/renderer/atom_renderer_client.cc | 14 ++++++++-- filenames.gypi | 2 ++ 12 files changed, 88 insertions(+), 36 deletions(-) create mode 100644 atom/common/color_util.cc create mode 100644 atom/common/color_util.h diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index a10f46fe75e7..fd9dd94458c3 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -141,6 +141,10 @@ Window::Window(v8::Isolate* isolate, const mate::Dictionary& options) { if (options.Get(options::kZoomFactor, &value)) web_preferences.Set(options::kZoomFactor, value); + // Copy the backgroundColor to webContents. + if (options.Get(options::kBackgroundColor, &value)) + web_preferences.Set(options::kBackgroundColor, value); + // Creates the WebContents used by BrowserWindow. auto web_contents = WebContents::Create(isolate, web_preferences); web_contents_.Reset(isolate, web_contents.ToV8()); diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 2627c704d21f..575ac8c579f0 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -594,27 +594,4 @@ void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback, callback.Run(bitmap); } -SkColor NativeWindow::ParseHexColor(const std::string& name) { - auto color = name.substr(1); - unsigned length = color.size(); - SkColor result = (length != 8 ? 0xFF000000 : 0x00000000); - unsigned value = 0; - if (length != 3 && length != 6 && length != 8) - return result; - for (unsigned i = 0; i < length; ++i) { - if (!base::IsHexDigit(color[i])) - return result; - value <<= 4; - value |= (color[i] < 'A' ? color[i] - '0' : (color[i] - 'A' + 10) & 0xF); - } - if (length == 6 || length == 8) { - result |= value; - return result; - } - result |= (value & 0xF00) << 12 | (value & 0xF00) << 8 - | (value & 0xF0) << 8 | (value & 0xF0) << 4 - | (value & 0xF) << 4 | (value & 0xF); - return result; -} - } // namespace atom diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 49e1e71d5df6..f91d041c057e 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -282,9 +282,6 @@ class NativeWindow : public base::SupportsUserData, void BeforeUnloadDialogCancelled() override; bool OnMessageReceived(const IPC::Message& message) override; - // Parse hex color like "#FFF" or "#EFEFEF" - SkColor ParseHexColor(const std::string& name); - private: // Schedule a notification unresponsive event. void ScheduleUnresponsiveEvent(int ms); diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 2044ee7d7189..456d175f2361 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -6,6 +6,7 @@ #include +#include "atom/common/color_util.h" #include "atom/common/draggable_region.h" #include "atom/common/options_switches.h" #include "base/mac/mac_util.h" @@ -804,7 +805,7 @@ bool NativeWindowMac::IsKiosk() { } void NativeWindowMac::SetBackgroundColor(const std::string& color_name) { - SkColor background_color = NativeWindow::ParseHexColor(color_name); + SkColor background_color = ParseHexColor(color_name); NSColor *color = [NSColor colorWithCalibratedRed:SkColorGetR(background_color) green:SkColorGetG(background_color) blue:SkColorGetB(background_color) diff --git a/atom/browser/native_window_views.cc b/atom/browser/native_window_views.cc index 15f2046364bb..59d3556d05e3 100644 --- a/atom/browser/native_window_views.cc +++ b/atom/browser/native_window_views.cc @@ -9,6 +9,7 @@ #include "atom/browser/ui/views/menu_bar.h" #include "atom/browser/ui/views/menu_layout.h" +#include "atom/common/color_util.h" #include "atom/common/draggable_region.h" #include "atom/common/options_switches.h" #include "base/strings/utf_string_conversions.h" @@ -614,7 +615,7 @@ bool NativeWindowViews::IsKiosk() { void NativeWindowViews::SetBackgroundColor(const std::string& color_name) { // web views' background color. - SkColor background_color = NativeWindow::ParseHexColor(color_name); + SkColor background_color = ParseHexColor(color_name); set_background(views::Background::CreateSolidBackground(background_color)); #if defined(OS_WIN) diff --git a/atom/browser/web_contents_preferences.cc b/atom/browser/web_contents_preferences.cc index bcdbd736676a..f36e7c076dca 100644 --- a/atom/browser/web_contents_preferences.cc +++ b/atom/browser/web_contents_preferences.cc @@ -116,6 +116,11 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches( LOG(ERROR) << "preload url must be file:// protocol."; } + // --background-color. + std::string color; + if (web_preferences.GetString(options::kBackgroundColor, &color)) + command_line->AppendSwitchASCII(switches::kBackgroundColor, color); + // The zoom factor. double zoom_factor = 1.0; if (web_preferences.GetDouble(options::kZoomFactor, &zoom_factor) && diff --git a/atom/common/color_util.cc b/atom/common/color_util.cc new file mode 100644 index 000000000000..fed050cd717b --- /dev/null +++ b/atom/common/color_util.cc @@ -0,0 +1,34 @@ +// Copyright (c) 2016 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/common/color_util.h" + +#include "base/strings/string_util.h" + +namespace atom { + +SkColor ParseHexColor(const std::string& name) { + auto color = name.substr(1); + unsigned length = color.size(); + SkColor result = (length != 8 ? 0xFF000000 : 0x00000000); + unsigned value = 0; + if (length != 3 && length != 6 && length != 8) + return result; + for (unsigned i = 0; i < length; ++i) { + if (!base::IsHexDigit(color[i])) + return result; + value <<= 4; + value |= (color[i] < 'A' ? color[i] - '0' : (color[i] - 'A' + 10) & 0xF); + } + if (length == 6 || length == 8) { + result |= value; + return result; + } + result |= (value & 0xF00) << 12 | (value & 0xF00) << 8 + | (value & 0xF0) << 8 | (value & 0xF0) << 4 + | (value & 0xF) << 4 | (value & 0xF); + return result; +} + +} // namespace atom diff --git a/atom/common/color_util.h b/atom/common/color_util.h new file mode 100644 index 000000000000..0f2bb9633a73 --- /dev/null +++ b/atom/common/color_util.h @@ -0,0 +1,19 @@ +// Copyright (c) 2016 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_COMMON_COLOR_UTIL_H_ +#define ATOM_COMMON_COLOR_UTIL_H_ + +#include + +#include "third_party/skia/include/core/SkColor.h" + +namespace atom { + +// Parse hex color like "#FFF" or "#EFEFEF" +SkColor ParseHexColor(const std::string& name); + +} // namespace atom + +#endif // ATOM_COMMON_COLOR_UTIL_H_ diff --git a/atom/common/options_switches.cc b/atom/common/options_switches.cc index ac63ea1b7276..ce28cc98facf 100644 --- a/atom/common/options_switches.cc +++ b/atom/common/options_switches.cc @@ -142,12 +142,13 @@ const char kCipherSuiteBlacklist[] = "cipher-suite-blacklist"; const char kAppUserModelId[] = "app-user-model-id"; // The command line switch versions of the options. -const char kZoomFactor[] = "zoom-factor"; -const char kPreloadScript[] = "preload"; -const char kPreloadURL[] = "preload-url"; -const char kNodeIntegration[] = "node-integration"; -const char kGuestInstanceID[] = "guest-instance-id"; -const char kOpenerID[] = "opener-id"; +const char kBackgroundColor[] = "background-color"; +const char kZoomFactor[] = "zoom-factor"; +const char kPreloadScript[] = "preload"; +const char kPreloadURL[] = "preload-url"; +const char kNodeIntegration[] = "node-integration"; +const char kGuestInstanceID[] = "guest-instance-id"; +const char kOpenerID[] = "opener-id"; // Widevine options // Path to Widevine CDM binaries. diff --git a/atom/common/options_switches.h b/atom/common/options_switches.h index 3c198555a5c3..52d64c00d51a 100644 --- a/atom/common/options_switches.h +++ b/atom/common/options_switches.h @@ -76,6 +76,7 @@ extern const char kSSLVersionFallbackMin[]; extern const char kCipherSuiteBlacklist[]; extern const char kAppUserModelId[]; +extern const char kBackgroundColor[]; extern const char kZoomFactor[]; extern const char kPreloadScript[]; extern const char kPreloadURL[]; diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index 7746ce123e4f..4bb3cd2168c8 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -10,6 +10,7 @@ #include "atom/common/api/api_messages.h" #include "atom/common/api/atom_bindings.h" #include "atom/common/api/event_emitter_caller.h" +#include "atom/common/color_util.h" #include "atom/common/node_bindings.h" #include "atom/common/node_includes.h" #include "atom/common/options_switches.h" @@ -120,8 +121,17 @@ void AtomRendererClient::RenderFrameCreated( } void AtomRendererClient::RenderViewCreated(content::RenderView* render_view) { - // Set default UA-dependent background as transparent. - render_view->GetWebView()->setBaseBackgroundColor(SK_ColorTRANSPARENT); + base::CommandLine* cmd = base::CommandLine::ForCurrentProcess(); + if (cmd->HasSwitch(switches::kGuestInstanceID)) { // webview. + // Set transparent background. + render_view->GetWebView()->setBaseBackgroundColor(SK_ColorTRANSPARENT); + } else { // normal window. + // If backgroundColor is specified then use it. + std::string name = cmd->GetSwitchValueASCII(switches::kBackgroundColor); + // Otherwise use white background. + SkColor color = name.empty() ? SK_ColorWHITE : ParseHexColor(name); + render_view->GetWebView()->setBaseBackgroundColor(color); + } new printing::PrintWebViewHelper(render_view); new AtomRenderViewObserver(render_view, this); diff --git a/filenames.gypi b/filenames.gypi index 2c39a8dccef1..688886b5b9cc 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -307,6 +307,8 @@ 'atom/common/atom_command_line.h', 'atom/common/atom_constants.cc', 'atom/common/atom_constants.h', + 'atom/common/color_util.cc', + 'atom/common/color_util.h', 'atom/common/common_message_generator.cc', 'atom/common/common_message_generator.h', 'atom/common/crash_reporter/crash_reporter.cc',