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: BrowserView background color in webContents #33546

Merged
merged 1 commit into from May 24, 2022
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
21 changes: 19 additions & 2 deletions shell/browser/api/electron_api_browser_view.cc
Expand Up @@ -6,10 +6,13 @@

#include <vector>

#include "content/browser/renderer_host/render_widget_host_view_base.h" // nogncheck
#include "content/public/browser/render_widget_host_view.h"
#include "shell/browser/api/electron_api_web_contents.h"
#include "shell/browser/browser.h"
#include "shell/browser/native_browser_view.h"
#include "shell/browser/ui/drag_util.h"
#include "shell/browser/web_contents_preferences.h"
#include "shell/common/color_util.h"
#include "shell/common/gin_converters/gfx_converter.h"
#include "shell/common/gin_helper/dictionary.h"
Expand Down Expand Up @@ -155,11 +158,25 @@ gfx::Rect BrowserView::GetBounds() {
}

void BrowserView::SetBackgroundColor(const std::string& color_name) {
view_->SetBackgroundColor(ParseHexColor(color_name));
SkColor color = ParseHexColor(color_name);
view_->SetBackgroundColor(color);

if (web_contents()) {
auto* wc = web_contents()->web_contents();
wc->SetPageBaseBackgroundColor(ParseHexColor(color_name));
wc->SetPageBaseBackgroundColor(color);

auto* const rwhv = wc->GetRenderWidgetHostView();
if (rwhv) {
rwhv->SetBackgroundColor(color);
static_cast<content::RenderWidgetHostViewBase*>(rwhv)
->SetContentBackgroundColor(color);
}

// Ensure new color is stored in webPreferences, otherwise
// the color will be reset on the next load via HandleNewRenderFrame.
auto* web_preferences = WebContentsPreferences::From(wc);
if (web_preferences)
web_preferences->SetBackgroundColor(color);
}
}

Expand Down
10 changes: 6 additions & 4 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -1463,11 +1463,13 @@ void WebContents::HandleNewRenderFrame(
// Set the background color of RenderWidgetHostView.
auto* web_preferences = WebContentsPreferences::From(web_contents());
if (web_preferences) {
absl::optional<SkColor> maybe_color = web_preferences->GetBackgroundColor();
web_contents()->SetPageBaseBackgroundColor(maybe_color);

bool guest = IsGuest() || type_ == Type::kBrowserView;
absl::optional<SkColor> color =
guest ? SK_ColorTRANSPARENT : web_preferences->GetBackgroundColor();
web_contents()->SetPageBaseBackgroundColor(color);
SetBackgroundColor(rwhv, color.value_or(SK_ColorWHITE));
SkColor color =
maybe_color.value_or(guest ? SK_ColorTRANSPARENT : SK_ColorWHITE);
SetBackgroundColor(rwhv, color);
}

if (!background_throttling_)
Expand Down