Browse Source

fix: BrowserWindow backgroundColor (#30778)

* fix: BrowserWindow backgroundColor

* refactor: propagate transparency via backgroundColor
Samuel Maddock 3 years ago
parent
commit
7379e5eb36

+ 10 - 10
shell/browser/api/electron_api_browser_window.cc

@@ -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
@@ -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_) {

+ 3 - 3
shell/browser/api/electron_api_web_contents.cc

@@ -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(
@@ -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_)

+ 5 - 2
shell/browser/web_contents_preferences.cc

@@ -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"
@@ -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;
@@ -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;

+ 7 - 3
shell/browser/web_contents_preferences.h

@@ -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_;
   }
@@ -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_;

+ 1 - 2
spec-main/fixtures/snapshots/native-window-open.snapshot.txt

@@ -88,8 +88,7 @@
         "sandbox": true,
         "webviewTag": false,
         "nodeIntegrationInSubFrames": false,
-        "openerId": null,
-        "backgroundColor": "gray"
+        "openerId": null
       },
       "x": 100,
       "y": 100,

+ 1 - 2
spec-main/fixtures/snapshots/proxy-window-open.snapshot.txt

@@ -88,8 +88,7 @@
         "nodeIntegration": false,
         "webviewTag": false,
         "nodeIntegrationInSubFrames": false,
-        "openerId": "placeholder-opener-id",
-        "backgroundColor": "gray"
+        "openerId": "placeholder-opener-id"
       },
       "x": 100,
       "y": 100,