Set WebContents background color in BrowserWindow

This commit is contained in:
Cheng Zhao 2018-03-06 13:21:47 +09:00
parent 7b8890a4c9
commit 1681ee35db
7 changed files with 14 additions and 16 deletions

View file

@ -13,6 +13,7 @@
#include "atom/browser/web_contents_preferences.h" #include "atom/browser/web_contents_preferences.h"
#include "atom/browser/window_list.h" #include "atom/browser/window_list.h"
#include "atom/common/api/api_messages.h" #include "atom/common/api/api_messages.h"
#include "atom/common/color_util.h"
#include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/file_path_converter.h" #include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/gfx_converter.h"
@ -770,7 +771,11 @@ bool BrowserWindow::IsKiosk() {
} }
void BrowserWindow::SetBackgroundColor(const std::string& color_name) { void BrowserWindow::SetBackgroundColor(const std::string& color_name) {
window_->SetBackgroundColor(color_name); SkColor color = ParseHexColor(color_name);
window_->SetBackgroundColor(color);
auto* view = web_contents()->GetRenderWidgetHostView();
if (view)
view->SetBackgroundColor(color);
} }
void BrowserWindow::SetHasShadow(bool has_shadow) { void BrowserWindow::SetHasShadow(bool has_shadow) {

View file

@ -12,6 +12,7 @@
#include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
#include "atom/browser/window_list.h" #include "atom/browser/window_list.h"
#include "atom/common/color_util.h"
#include "atom/common/draggable_region.h" #include "atom/common/draggable_region.h"
#include "atom/common/native_mate_converters/file_path_converter.h" #include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/options_switches.h" #include "atom/common/options_switches.h"
@ -172,10 +173,10 @@ void NativeWindow::InitFromOptions(const mate::Dictionary& options) {
} }
std::string color; std::string color;
if (options.Get(options::kBackgroundColor, &color)) { if (options.Get(options::kBackgroundColor, &color)) {
SetBackgroundColor(color); SetBackgroundColor(ParseHexColor(color));
} else if (!transparent()) { } else if (!transparent()) {
// For normal window, use white as default background. // For normal window, use white as default background.
SetBackgroundColor("#FFFF"); SetBackgroundColor(SK_ColorWHITE);
} }
std::string title(Browser::Get()->GetName()); std::string title(Browser::Get()->GetName());
options.Get(options::kTitle, &title); options.Get(options::kTitle, &title);

View file

@ -138,7 +138,7 @@ class NativeWindow : public base::SupportsUserData,
virtual bool IsSimpleFullScreen() = 0; virtual bool IsSimpleFullScreen() = 0;
virtual void SetKiosk(bool kiosk) = 0; virtual void SetKiosk(bool kiosk) = 0;
virtual bool IsKiosk() = 0; virtual bool IsKiosk() = 0;
virtual void SetBackgroundColor(const std::string& color_name) = 0; virtual void SetBackgroundColor(SkColor color) = 0;
virtual void SetHasShadow(bool has_shadow) = 0; virtual void SetHasShadow(bool has_shadow) = 0;
virtual bool HasShadow() = 0; virtual bool HasShadow() = 0;
virtual void SetOpacity(const double opacity) = 0; virtual void SetOpacity(const double opacity) = 0;

View file

@ -79,7 +79,7 @@ class NativeWindowMac : public NativeWindow {
bool IsSimpleFullScreen() override; bool IsSimpleFullScreen() override;
void SetKiosk(bool kiosk) override; void SetKiosk(bool kiosk) override;
bool IsKiosk() override; bool IsKiosk() override;
void SetBackgroundColor(const std::string& color_name) override; void SetBackgroundColor(SkColor color) override;
void SetHasShadow(bool has_shadow) override; void SetHasShadow(bool has_shadow) override;
bool HasShadow() override; bool HasShadow() override;
void SetOpacity(const double opacity) override; void SetOpacity(const double opacity) override;

View file

@ -12,7 +12,6 @@
#include "atom/browser/native_browser_view_mac.h" #include "atom/browser/native_browser_view_mac.h"
#include "atom/browser/ui/cocoa/atom_touch_bar.h" #include "atom/browser/ui/cocoa/atom_touch_bar.h"
#include "atom/browser/window_list.h" #include "atom/browser/window_list.h"
#include "atom/common/color_util.h"
#include "atom/common/draggable_region.h" #include "atom/common/draggable_region.h"
#include "atom/common/options_switches.h" #include "atom/common/options_switches.h"
#include "base/mac/mac_util.h" #include "base/mac/mac_util.h"
@ -1475,15 +1474,10 @@ bool NativeWindowMac::IsKiosk() {
return is_kiosk_; return is_kiosk_;
} }
void NativeWindowMac::SetBackgroundColor(const std::string& color_name) { void NativeWindowMac::SetBackgroundColor(SkColor color) {
SkColor color = ParseHexColor(color_name);
base::ScopedCFTypeRef<CGColorRef> cgcolor( base::ScopedCFTypeRef<CGColorRef> cgcolor(
skia::CGColorCreateFromSkColor(color)); skia::CGColorCreateFromSkColor(color));
[[[window_ contentView] layer] setBackgroundColor:cgcolor]; [[[window_ contentView] layer] setBackgroundColor:cgcolor];
const auto view = web_contents()->GetRenderWidgetHostView();
if (view)
view->SetBackgroundColor(color);
} }
void NativeWindowMac::SetHasShadow(bool has_shadow) { void NativeWindowMac::SetHasShadow(bool has_shadow) {

View file

@ -16,7 +16,6 @@
#include "atom/browser/web_contents_preferences.h" #include "atom/browser/web_contents_preferences.h"
#include "atom/browser/web_view_manager.h" #include "atom/browser/web_view_manager.h"
#include "atom/browser/window_list.h" #include "atom/browser/window_list.h"
#include "atom/common/color_util.h"
#include "atom/common/draggable_region.h" #include "atom/common/draggable_region.h"
#include "atom/common/native_mate_converters/image_converter.h" #include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/options_switches.h" #include "atom/common/options_switches.h"
@ -792,9 +791,8 @@ bool NativeWindowViews::IsKiosk() {
return IsFullscreen(); return IsFullscreen();
} }
void NativeWindowViews::SetBackgroundColor(const std::string& color_name) { void NativeWindowViews::SetBackgroundColor(SkColor background_color) {
// web views' background color. // web views' background color.
SkColor background_color = ParseHexColor(color_name);
SetBackground(views::CreateSolidBackground(background_color)); SetBackground(views::CreateSolidBackground(background_color));
#if defined(OS_WIN) #if defined(OS_WIN)

View file

@ -101,7 +101,7 @@ class NativeWindowViews : public NativeWindow,
bool IsSimpleFullScreen() override; bool IsSimpleFullScreen() override;
void SetKiosk(bool kiosk) override; void SetKiosk(bool kiosk) override;
bool IsKiosk() override; bool IsKiosk() override;
void SetBackgroundColor(const std::string& color_name) override; void SetBackgroundColor(SkColor color) override;
void SetHasShadow(bool has_shadow) override; void SetHasShadow(bool has_shadow) override;
bool HasShadow() override; bool HasShadow() override;
void SetOpacity(const double opacity) override; void SetOpacity(const double opacity) override;