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
Conversation
@@ -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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
electron/shell/browser/api/electron_api_browser_window.cc
Lines 40 to 50 in c5b517d
// 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 😞
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()
? 🤔
electron/shell/browser/api/electron_api_browser_view.cc
Lines 149 to 151 in b491a4c
void BrowserView::SetBackgroundColor(const std::string& color_name) { | |
view_->SetBackgroundColor(ParseHexColor(color_name)); | |
} |
compared to BrowserWindow's code
electron/shell/browser/api/electron_api_browser_window.cc
Lines 363 to 375 in b491a4c
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)); | |
} | |
} | |
} |
There was a problem hiding this comment.
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! 🙂
There was a problem hiding this comment.
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) 🙂
Closing this in favor of #31003 |
Description of Change
Fixes #30975
In a recent PR that fixed background color loading, we reset the default background color to
absl::nullopt
. However, passingabsl::nullopt
into SetPageBaseBackgroundColor results in the colorSK_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)
Transparent window using https://gist.github.com/b52f026c763057f97f745fade82758fd from #30136
Background color using https://gist.github.com/4256f388d41ef0d974645a11ab72b1fe from #30759
Checklist
npm test
passesRelease Notes
Notes: Fixed child views or windows not inheriting the correct background transparency or background color.