Skip to content

Commit

Permalink
fix: BrowserWindow backgroundColor (#30778)
Browse files Browse the repository at this point in the history
* fix: BrowserWindow backgroundColor

* refactor: propagate transparency via backgroundColor
  • Loading branch information
samuelmaddock committed Sep 6, 2021
1 parent 26f981f commit 7379e5e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
20 changes: 10 additions & 10 deletions shell/browser/api/electron_api_browser_window.cc
Expand Up @@ -39,13 +39,15 @@ BrowserWindow::BrowserWindow(gin::Arguments* args,

// Copy the backgroundColor to webContents.
v8::Local<v8::Value> value;
if (options.Get(options::kBackgroundColor, &value))
web_preferences.Set(options::kBackgroundColor, value);

// Copy the transparent setting to webContents
v8::Local<v8::Value> transparent;
if (options.Get("transparent", &transparent))
web_preferences.Set("transparent", transparent);
bool transparent = false;
if (options.Get(options::kBackgroundColor, &value)) {
web_preferences.SetHidden(options::kBackgroundColor, value);
} else if (options.Get(options::kTransparent, &transparent) && transparent) {
// If the BrowserWindow is transparent, also propagate transparency to the
// WebContents unless a separate backgroundColor has been set.
web_preferences.SetHidden(options::kBackgroundColor,
ToRGBAHex(SK_ColorTRANSPARENT));
}

// Copy the show setting to webContents, but only if we don't want to paint
// when initially hidden
Expand Down Expand Up @@ -360,9 +362,7 @@ void BrowserWindow::Blur() {

void BrowserWindow::SetBackgroundColor(const std::string& color_name) {
BaseWindow::SetBackgroundColor(color_name);
auto* view = web_contents()->GetRenderWidgetHostView();
if (view)
view->SetBackgroundColor(ParseHexColor(color_name));
web_contents()->SetPageBaseBackgroundColor(ParseHexColor(color_name));
// Also update the web preferences object otherwise the view will be reset on
// the next load URL call
if (api_web_contents_) {
Expand Down
6 changes: 3 additions & 3 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -748,7 +748,7 @@ WebContents::WebContents(v8::Isolate* isolate,
}
} else if (IsOffScreen()) {
bool transparent = false;
options.Get("transparent", &transparent);
options.Get(options::kTransparent, &transparent);

content::WebContents::CreateParams params(session->browser_context());
auto* view = new OffScreenWebContentsView(
Expand Down Expand Up @@ -1373,8 +1373,8 @@ void WebContents::HandleNewRenderFrame(
// Set the background color of RenderWidgetHostView.
auto* web_preferences = WebContentsPreferences::From(web_contents());
if (web_preferences) {
std::string color_name;
rwhv->SetBackgroundColor(web_preferences->GetBackgroundColor());
web_contents()->SetPageBaseBackgroundColor(
web_preferences->GetBackgroundColor());
}

if (!background_throttling_)
Expand Down
7 changes: 5 additions & 2 deletions shell/browser/web_contents_preferences.cc
Expand Up @@ -22,6 +22,7 @@
#include "shell/browser/api/electron_api_web_contents.h"
#include "shell/browser/native_window.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_helper/dictionary.h"
#include "shell/common/options_switches.h"
Expand Down Expand Up @@ -158,7 +159,7 @@ void WebContentsPreferences::Clear() {
safe_dialogs_ = false;
safe_dialogs_message_ = absl::nullopt;
ignore_menu_shortcuts_ = false;
background_color_ = SK_ColorTRANSPARENT;
background_color_ = absl::nullopt;
image_animation_policy_ =
blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAllowed;
preload_path_ = absl::nullopt;
Expand Down Expand Up @@ -224,7 +225,9 @@ void WebContentsPreferences::SetFromDictionary(
web_preferences.Get("disablePopups", &disable_popups_);
web_preferences.Get("disableDialogs", &disable_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;
if (web_preferences.Get("safeDialogsMessage", &safe_dialogs_message))
safe_dialogs_message_ = safe_dialogs_message;
Expand Down
10 changes: 7 additions & 3 deletions shell/browser/web_contents_preferences.h
Expand Up @@ -48,8 +48,12 @@ class WebContentsPreferences
base::Value* last_preference() { return &last_web_preferences_; }

bool IsOffscreen() const { return offscreen_; }
SkColor GetBackgroundColor() const { return background_color_; }
void SetBackgroundColor(SkColor color) { background_color_ = color; }
absl::optional<SkColor> GetBackgroundColor() const {
return background_color_;
}
void SetBackgroundColor(absl::optional<SkColor> color) {
background_color_ = color;
}
bool ShouldUsePreferredSizeMode() const {
return enable_preferred_size_mode_;
}
Expand Down Expand Up @@ -119,7 +123,7 @@ class WebContentsPreferences
bool safe_dialogs_;
absl::optional<std::string> safe_dialogs_message_;
bool ignore_menu_shortcuts_;
SkColor background_color_;
absl::optional<SkColor> background_color_;
blink::mojom::ImageAnimationPolicy image_animation_policy_;
absl::optional<base::FilePath> preload_path_;
blink::mojom::V8CacheOptions v8_cache_options_;
Expand Down
3 changes: 1 addition & 2 deletions spec-main/fixtures/snapshots/native-window-open.snapshot.txt
Expand Up @@ -88,8 +88,7 @@
"sandbox": true,
"webviewTag": false,
"nodeIntegrationInSubFrames": false,
"openerId": null,
"backgroundColor": "gray"
"openerId": null
},
"x": 100,
"y": 100,
Expand Down
3 changes: 1 addition & 2 deletions spec-main/fixtures/snapshots/proxy-window-open.snapshot.txt
Expand Up @@ -88,8 +88,7 @@
"nodeIntegration": false,
"webviewTag": false,
"nodeIntegrationInSubFrames": false,
"openerId": "placeholder-opener-id",
"backgroundColor": "gray"
"openerId": "placeholder-opener-id"
},
"x": 100,
"y": 100,
Expand Down

0 comments on commit 7379e5e

Please sign in to comment.