fix: BrowserWindow backgroundColor (#30778)

* fix: BrowserWindow backgroundColor

* refactor: propagate transparency via backgroundColor
This commit is contained in:
Samuel Maddock 2021-09-06 03:59:09 -04:00 committed by GitHub
parent 26f981fa3e
commit 7379e5eb36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 22 deletions

View file

@ -39,13 +39,15 @@ BrowserWindow::BrowserWindow(gin::Arguments* args,
// Copy the backgroundColor to webContents. // Copy the backgroundColor to webContents.
v8::Local<v8::Value> value; v8::Local<v8::Value> value;
if (options.Get(options::kBackgroundColor, &value)) bool transparent = false;
web_preferences.Set(options::kBackgroundColor, value); if (options.Get(options::kBackgroundColor, &value)) {
web_preferences.SetHidden(options::kBackgroundColor, value);
// Copy the transparent setting to webContents } else if (options.Get(options::kTransparent, &transparent) && transparent) {
v8::Local<v8::Value> transparent; // If the BrowserWindow is transparent, also propagate transparency to the
if (options.Get("transparent", &transparent)) // WebContents unless a separate backgroundColor has been set.
web_preferences.Set("transparent", transparent); web_preferences.SetHidden(options::kBackgroundColor,
ToRGBAHex(SK_ColorTRANSPARENT));
}
// Copy the show setting to webContents, but only if we don't want to paint // Copy the show setting to webContents, but only if we don't want to paint
// when initially hidden // when initially hidden
@ -360,9 +362,7 @@ void BrowserWindow::Blur() {
void BrowserWindow::SetBackgroundColor(const std::string& color_name) { void BrowserWindow::SetBackgroundColor(const std::string& color_name) {
BaseWindow::SetBackgroundColor(color_name); BaseWindow::SetBackgroundColor(color_name);
auto* view = web_contents()->GetRenderWidgetHostView(); web_contents()->SetPageBaseBackgroundColor(ParseHexColor(color_name));
if (view)
view->SetBackgroundColor(ParseHexColor(color_name));
// Also update the web preferences object otherwise the view will be reset on // Also update the web preferences object otherwise the view will be reset on
// the next load URL call // the next load URL call
if (api_web_contents_) { if (api_web_contents_) {

View file

@ -748,7 +748,7 @@ WebContents::WebContents(v8::Isolate* isolate,
} }
} else if (IsOffScreen()) { } else if (IsOffScreen()) {
bool transparent = false; bool transparent = false;
options.Get("transparent", &transparent); options.Get(options::kTransparent, &transparent);
content::WebContents::CreateParams params(session->browser_context()); content::WebContents::CreateParams params(session->browser_context());
auto* view = new OffScreenWebContentsView( auto* view = new OffScreenWebContentsView(
@ -1373,8 +1373,8 @@ void WebContents::HandleNewRenderFrame(
// Set the background color of RenderWidgetHostView. // Set the background color of RenderWidgetHostView.
auto* web_preferences = WebContentsPreferences::From(web_contents()); auto* web_preferences = WebContentsPreferences::From(web_contents());
if (web_preferences) { if (web_preferences) {
std::string color_name; web_contents()->SetPageBaseBackgroundColor(
rwhv->SetBackgroundColor(web_preferences->GetBackgroundColor()); web_preferences->GetBackgroundColor());
} }
if (!background_throttling_) if (!background_throttling_)

View file

@ -22,6 +22,7 @@
#include "shell/browser/api/electron_api_web_contents.h" #include "shell/browser/api/electron_api_web_contents.h"
#include "shell/browser/native_window.h" #include "shell/browser/native_window.h"
#include "shell/browser/session_preferences.h" #include "shell/browser/session_preferences.h"
#include "shell/common/color_util.h"
#include "shell/common/gin_converters/value_converter.h" #include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/dictionary.h" #include "shell/common/gin_helper/dictionary.h"
#include "shell/common/options_switches.h" #include "shell/common/options_switches.h"
@ -158,7 +159,7 @@ void WebContentsPreferences::Clear() {
safe_dialogs_ = false; safe_dialogs_ = false;
safe_dialogs_message_ = absl::nullopt; safe_dialogs_message_ = absl::nullopt;
ignore_menu_shortcuts_ = false; ignore_menu_shortcuts_ = false;
background_color_ = SK_ColorTRANSPARENT; background_color_ = absl::nullopt;
image_animation_policy_ = image_animation_policy_ =
blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAllowed; blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAllowed;
preload_path_ = absl::nullopt; preload_path_ = absl::nullopt;
@ -224,7 +225,9 @@ void WebContentsPreferences::SetFromDictionary(
web_preferences.Get("disablePopups", &disable_popups_); web_preferences.Get("disablePopups", &disable_popups_);
web_preferences.Get("disableDialogs", &disable_dialogs_); web_preferences.Get("disableDialogs", &disable_dialogs_);
web_preferences.Get("safeDialogs", &safe_dialogs_); web_preferences.Get("safeDialogs", &safe_dialogs_);
web_preferences.Get(options::kBackgroundColor, &background_color_); std::string background_color;
if (web_preferences.GetHidden(options::kBackgroundColor, &background_color))
background_color_ = ParseHexColor(background_color);
std::string safe_dialogs_message; std::string safe_dialogs_message;
if (web_preferences.Get("safeDialogsMessage", &safe_dialogs_message)) if (web_preferences.Get("safeDialogsMessage", &safe_dialogs_message))
safe_dialogs_message_ = safe_dialogs_message; safe_dialogs_message_ = safe_dialogs_message;

View file

@ -48,8 +48,12 @@ class WebContentsPreferences
base::Value* last_preference() { return &last_web_preferences_; } base::Value* last_preference() { return &last_web_preferences_; }
bool IsOffscreen() const { return offscreen_; } bool IsOffscreen() const { return offscreen_; }
SkColor GetBackgroundColor() const { return background_color_; } absl::optional<SkColor> GetBackgroundColor() const {
void SetBackgroundColor(SkColor color) { background_color_ = color; } return background_color_;
}
void SetBackgroundColor(absl::optional<SkColor> color) {
background_color_ = color;
}
bool ShouldUsePreferredSizeMode() const { bool ShouldUsePreferredSizeMode() const {
return enable_preferred_size_mode_; return enable_preferred_size_mode_;
} }
@ -119,7 +123,7 @@ class WebContentsPreferences
bool safe_dialogs_; bool safe_dialogs_;
absl::optional<std::string> safe_dialogs_message_; absl::optional<std::string> safe_dialogs_message_;
bool ignore_menu_shortcuts_; bool ignore_menu_shortcuts_;
SkColor background_color_; absl::optional<SkColor> background_color_;
blink::mojom::ImageAnimationPolicy image_animation_policy_; blink::mojom::ImageAnimationPolicy image_animation_policy_;
absl::optional<base::FilePath> preload_path_; absl::optional<base::FilePath> preload_path_;
blink::mojom::V8CacheOptions v8_cache_options_; blink::mojom::V8CacheOptions v8_cache_options_;

View file

@ -88,8 +88,7 @@
"sandbox": true, "sandbox": true,
"webviewTag": false, "webviewTag": false,
"nodeIntegrationInSubFrames": false, "nodeIntegrationInSubFrames": false,
"openerId": null, "openerId": null
"backgroundColor": "gray"
}, },
"x": 100, "x": 100,
"y": 100, "y": 100,

View file

@ -88,8 +88,7 @@
"nodeIntegration": false, "nodeIntegration": false,
"webviewTag": false, "webviewTag": false,
"nodeIntegrationInSubFrames": false, "nodeIntegrationInSubFrames": false,
"openerId": "placeholder-opener-id", "openerId": "placeholder-opener-id"
"backgroundColor": "gray"
}, },
"x": 100, "x": 100,
"y": 100, "y": 100,