Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: BrowserWindow backgroundColor #30778

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions shell/browser/api/electron_api_browser_window.cc
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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