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: change default background_color from absl::nullopt to transparent #30988

Closed
wants to merge 1 commit into from

Conversation

VerteDinde
Copy link
Member

Description of Change

Fixes #30975

In a recent PR that fixed background color loading, we reset the default background color to absl::nullopt. However, passing absl::nullopt into SetPageBaseBackgroundColor results in the color SK_ColorWHITE being used, which then breaks loading transparency in child windows and views. This PR resets the default to transparent, which fixes transparency for child windows.

I tested the gist in the new issue, as well as the original two gists from the background color PR, to make sure we didn't regress. Both still appear to work as expected.

Child view background color using https://gist.github.com/c-liang/e04698f37dce36c11ff0ecfa9e64b683 from #30975 (Windows)
Screenshot (33)

Transparent window using https://gist.github.com/b52f026c763057f97f745fade82758fd from #30136
Screen Shot 2021-09-15 at 10 45 49 AM

Background color using https://gist.github.com/4256f388d41ef0d974645a11ab72b1fe from #30759
Screen Shot 2021-09-15 at 10 46 41 AM

Checklist

Release Notes

Notes: Fixed child views or windows not inheriting the correct background transparency or background color.

@VerteDinde VerteDinde added semver/patch backwards-compatible bug fixes target/14-x-y labels Sep 15, 2021
@electron-cation electron-cation bot added the new-pr 🌱 PR opened in the last 24 hours label Sep 15, 2021
@@ -159,7 +159,7 @@ void WebContentsPreferences::Clear() {
safe_dialogs_ = false;
safe_dialogs_message_ = absl::nullopt;
ignore_menu_shortcuts_ = false;
background_color_ = absl::nullopt;
background_color_ = SK_ColorTRANSPARENT;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is correct 🤔 I think this should only be SK_ColorTRANSPARENT if the owning BrowserWindow is transparent 🤔 I think WebContentsPreferences used to have a concept of transparency but apparently not any more

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the intention with using the optional value.

The backgroundColor web preferences property was changed to use a hidden field. I wonder if this also needs to be the same with BrowserViews somewhere?

// Copy the backgroundColor to webContents.
v8::Local<v8::Value> value;
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));
}

Also, just a heads up that the v14 implementation differs a bit because of the WebPreferences refactor. Backport might be painful 😞

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, I have some wip tests written for backgroundColor so hopefully we'll be able to catch these issues in the future! 🤞 https://github.com/electron/electron/compare/main...samuelmaddock:test/bw-bg-color?expand=1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little more investigation: BrowserView might not be calling WebContents::SetPageBaseBackgroundColor()? 🤔

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

compared to BrowserWindow's code

void BrowserWindow::SetBackgroundColor(const std::string& color_name) {
BaseWindow::SetBackgroundColor(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_) {
auto* web_preferences =
WebContentsPreferences::From(api_web_contents_->web_contents());
if (web_preferences) {
web_preferences->SetBackgroundColor(ParseHexColor(color_name));
}
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not be calling it or there may be a painting/timing error on when it's called?

I actually found this bug yesterday while digging into this more: #30993 If I start a transparent window in Fiddle, the background renders as white, but then sometimes will switch back to transparent if I alt+tab away. Maybe there's a timing issue where background_color sometimes isn't being set before the first paint? Looking into it more today! 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samuelmaddock You were right - it looks like there's an issue with SetPageBaseBackgroundColor, where transparent windows would be white until a repaint (e.g. resize) happened. It looks like Chrome's going to merge the two APIs and address this down the line (https://chromium-review.googlesource.com/c/chromium/src/+/2958369), but we can get around it now by setting the background directly on the render widget host view. There's a new PR open to address this (linked below) 🙂

@electron-cation electron-cation bot removed the new-pr 🌱 PR opened in the last 24 hours label Sep 16, 2021
@VerteDinde
Copy link
Member Author

VerteDinde commented Sep 16, 2021

Closing this in favor of #31003

@VerteDinde VerteDinde closed this Sep 16, 2021
@VerteDinde VerteDinde deleted the child-window-transparency-fix branch September 16, 2021 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/patch backwards-compatible bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: BrowserView setBackgroundColor has no effect
3 participants