Use BrowserWindow's backgroundColor as renderer view's background color

This commit is contained in:
Cheng Zhao 2016-04-02 20:35:57 +09:00
parent 70ca49e36a
commit cb470cb94b
12 changed files with 88 additions and 36 deletions

View file

@ -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());

View file

@ -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

View file

@ -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);

View file

@ -6,6 +6,7 @@
#include <string>
#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)

View file

@ -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)

View file

@ -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) &&

34
atom/common/color_util.cc Normal file
View file

@ -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

19
atom/common/color_util.h Normal file
View file

@ -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 <string>
#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_

View file

@ -142,6 +142,7 @@ const char kCipherSuiteBlacklist[] = "cipher-suite-blacklist";
const char kAppUserModelId[] = "app-user-model-id";
// The command line switch versions of the options.
const char kBackgroundColor[] = "background-color";
const char kZoomFactor[] = "zoom-factor";
const char kPreloadScript[] = "preload";
const char kPreloadURL[] = "preload-url";

View file

@ -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[];

View file

@ -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.
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);

View file

@ -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',