mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
background color
This commit is contained in:
@@ -326,16 +326,10 @@ void BrowserWindow::Blur() {
|
||||
void BrowserWindow::SetBackgroundColor(const std::string& color_name) {
|
||||
BaseWindow::SetBackgroundColor(color_name);
|
||||
SkColor color = ParseCSSColor(color_name);
|
||||
web_contents()->SetPageBaseBackgroundColor(color);
|
||||
auto* rwhv = web_contents()->GetRenderWidgetHostView();
|
||||
if (rwhv) {
|
||||
rwhv->SetBackgroundColor(color);
|
||||
static_cast<content::RenderWidgetHostViewBase*>(rwhv)
|
||||
->SetContentBackgroundColor(color);
|
||||
}
|
||||
// Also update the web preferences object otherwise the view will be reset on
|
||||
// the next load URL call
|
||||
if (api_web_contents_) {
|
||||
api_web_contents_->SetBackgroundColor(color);
|
||||
// Also update the web preferences object otherwise the view will be reset
|
||||
// on the next load URL call
|
||||
auto* web_preferences =
|
||||
WebContentsPreferences::From(api_web_contents_->web_contents());
|
||||
if (web_preferences) {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/gin_helper/object_template_builder.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
#include "ui/views/background.h"
|
||||
#include "ui/views/layout/flex_layout.h"
|
||||
#include "ui/views/layout/layout_manager_base.h"
|
||||
|
||||
@@ -243,6 +244,10 @@ std::vector<v8::Local<v8::Value>> View::GetChildren() {
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void View::SetBackgroundColor(absl::optional<WrappedSkColor> color) {
|
||||
view()->SetBackground(color ? views::CreateSolidBackground(*color) : nullptr);
|
||||
}
|
||||
#endif
|
||||
|
||||
// static
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "electron/buildflags/buildflags.h"
|
||||
#include "gin/handle.h"
|
||||
#include "shell/common/color_util.h"
|
||||
#include "shell/common/gin_helper/wrappable.h"
|
||||
#include "ui/views/view.h"
|
||||
#include "v8/include/v8-value.h"
|
||||
@@ -34,6 +35,7 @@ class View : public gin_helper::Wrappable<View> {
|
||||
gfx::Rect GetBounds();
|
||||
void SetLayout(v8::Isolate* isolate, v8::Local<v8::Object> value);
|
||||
std::vector<v8::Local<v8::Value>> GetChildren();
|
||||
void SetBackgroundColor(absl::optional<WrappedSkColor> color);
|
||||
#endif
|
||||
|
||||
views::View* view() const { return view_; }
|
||||
|
||||
43
shell/browser/api/electron_api_web_contents.cc
Executable file → Normal file
43
shell/browser/api/electron_api_web_contents.cc
Executable file → Normal file
@@ -612,12 +612,6 @@ bool IsDevToolsFileSystemAdded(content::WebContents* web_contents,
|
||||
return file_system_paths.find(file_system_path) != file_system_paths.end();
|
||||
}
|
||||
|
||||
void SetBackgroundColor(content::RenderWidgetHostView* rwhv, SkColor color) {
|
||||
rwhv->SetBackgroundColor(color);
|
||||
static_cast<content::RenderWidgetHostViewBase*>(rwhv)
|
||||
->SetContentBackgroundColor(color);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
@@ -1478,15 +1472,8 @@ 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();
|
||||
SkColor color =
|
||||
maybe_color.value_or(guest ? SK_ColorTRANSPARENT : SK_ColorWHITE);
|
||||
SetBackgroundColor(rwhv, color);
|
||||
}
|
||||
if (web_preferences)
|
||||
SetBackgroundColor(web_preferences->GetBackgroundColor());
|
||||
|
||||
if (!background_throttling_)
|
||||
render_frame_host->GetRenderViewHost()->SetSchedulerThrottling(false);
|
||||
@@ -3433,6 +3420,22 @@ void WebContents::SetImageAnimationPolicy(const std::string& new_policy) {
|
||||
web_contents()->OnWebPreferencesChanged();
|
||||
}
|
||||
|
||||
void WebContents::SetBackgroundColor(absl::optional<SkColor> maybe_color) {
|
||||
web_contents()->SetPageBaseBackgroundColor(maybe_color);
|
||||
|
||||
content::RenderFrameHost* rfh = web_contents()->GetPrimaryMainFrame();
|
||||
if (!rfh)
|
||||
return;
|
||||
content::RenderWidgetHostView* rwhv = rfh->GetView();
|
||||
if (rwhv) {
|
||||
SkColor color =
|
||||
maybe_color.value_or(IsGuest() ? SK_ColorTRANSPARENT : SK_ColorWHITE);
|
||||
rwhv->SetBackgroundColor(color);
|
||||
static_cast<content::RenderWidgetHostViewBase*>(rwhv)
|
||||
->SetContentBackgroundColor(color);
|
||||
}
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise> WebContents::GetProcessMemoryInfo(v8::Isolate* isolate) {
|
||||
gin_helper::Promise<gin_helper::Dictionary> promise(isolate);
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
@@ -4113,14 +4116,8 @@ gin::Handle<WebContents> WebContents::CreateFromWebPreferences(
|
||||
if (gin::ConvertFromV8(isolate, web_preferences.GetHandle(),
|
||||
&web_preferences_dict)) {
|
||||
existing_preferences->SetFromDictionary(web_preferences_dict);
|
||||
absl::optional<SkColor> color =
|
||||
existing_preferences->GetBackgroundColor();
|
||||
web_contents->web_contents()->SetPageBaseBackgroundColor(color);
|
||||
// Because web preferences don't recognize transparency,
|
||||
// only set rwhv background color if a color exists
|
||||
auto* rwhv = web_contents->web_contents()->GetRenderWidgetHostView();
|
||||
if (rwhv && color.has_value())
|
||||
SetBackgroundColor(rwhv, color.value());
|
||||
web_contents->SetBackgroundColor(
|
||||
existing_preferences->GetBackgroundColor());
|
||||
}
|
||||
} else {
|
||||
// Create one if not.
|
||||
|
||||
@@ -430,6 +430,8 @@ class WebContents : public ExclusiveAccessContext,
|
||||
|
||||
void SetImageAnimationPolicy(const std::string& new_policy);
|
||||
|
||||
void SetBackgroundColor(absl::optional<SkColor> color);
|
||||
|
||||
// disable copy
|
||||
WebContents(const WebContents&) = delete;
|
||||
WebContents& operator=(const WebContents&) = delete;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "shell/browser/browser.h"
|
||||
#include "shell/browser/ui/inspectable_web_contents_view.h"
|
||||
#include "shell/browser/web_contents_preferences.h"
|
||||
#include "shell/common/gin_converters/gfx_converter.h"
|
||||
#include "shell/common/gin_converters/value_converter.h"
|
||||
#include "shell/common/gin_helper/constructor.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
@@ -56,6 +57,20 @@ gin::Handle<WebContents> WebContentsView::GetWebContents(v8::Isolate* isolate) {
|
||||
return gin::CreateHandle(isolate, api_web_contents_);
|
||||
}
|
||||
|
||||
void WebContentsView::SetBackgroundColor(absl::optional<WrappedSkColor> color) {
|
||||
View::SetBackgroundColor(color);
|
||||
if (api_web_contents_) {
|
||||
api_web_contents_->SetBackgroundColor(color);
|
||||
// Also update the web preferences object otherwise the view will be reset
|
||||
// on the next load URL call
|
||||
auto* web_preferences =
|
||||
WebContentsPreferences::From(api_web_contents_->web_contents());
|
||||
if (web_preferences) {
|
||||
web_preferences->SetBackgroundColor(color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WebContentsView::WebContentsDestroyed() {
|
||||
api_web_contents_ = nullptr;
|
||||
web_contents_.Reset();
|
||||
@@ -107,6 +122,7 @@ void WebContentsView::BuildPrototype(
|
||||
v8::Local<v8::FunctionTemplate> prototype) {
|
||||
prototype->SetClassName(gin::StringToV8(isolate, "WebContentsView"));
|
||||
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
||||
.SetMethod("setBackgroundColor", &WebContentsView::SetBackgroundColor)
|
||||
.SetProperty("webContents", &WebContentsView::GetWebContents);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ class WebContentsView : public View, public content::WebContentsObserver {
|
||||
|
||||
// Public APIs.
|
||||
gin::Handle<WebContents> GetWebContents(v8::Isolate* isolate);
|
||||
void SetBackgroundColor(absl::optional<WrappedSkColor> color);
|
||||
|
||||
protected:
|
||||
// Takes an existing WebContents.
|
||||
|
||||
@@ -9,6 +9,15 @@
|
||||
|
||||
#include "third_party/skia/include/core/SkColor.h"
|
||||
|
||||
// SkColor is a typedef for uint32_t, this wrapper is to tag an SkColor for
|
||||
// ease of use in gin converters.
|
||||
struct WrappedSkColor {
|
||||
WrappedSkColor() {}
|
||||
WrappedSkColor(SkColor c) : value(c) {}
|
||||
SkColor value;
|
||||
operator SkColor() const { return value; }
|
||||
};
|
||||
|
||||
namespace electron {
|
||||
|
||||
// Parses a CSS-style color string from hex, rgb(), rgba(),
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "shell/common/gin_converters/gfx_converter.h"
|
||||
|
||||
#include "gin/data_object_builder.h"
|
||||
#include "shell/common/color_util.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "ui/display/display.h"
|
||||
#include "ui/display/screen.h"
|
||||
@@ -217,4 +218,14 @@ v8::Local<v8::Value> Converter<gfx::ResizeEdge>::ToV8(
|
||||
}
|
||||
}
|
||||
|
||||
bool Converter<WrappedSkColor>::FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
WrappedSkColor* out) {
|
||||
std::string str;
|
||||
if (!gin::ConvertFromV8(isolate, val, &str))
|
||||
return false;
|
||||
*out = electron::ParseCSSColor(str);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace gin
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define ELECTRON_SHELL_COMMON_GIN_CONVERTERS_GFX_CONVERTER_H_
|
||||
|
||||
#include "gin/converter.h"
|
||||
#include "shell/common/color_util.h"
|
||||
|
||||
namespace display {
|
||||
class Display;
|
||||
@@ -79,6 +80,13 @@ struct Converter<gfx::ResizeEdge> {
|
||||
const gfx::ResizeEdge& val);
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<WrappedSkColor> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
WrappedSkColor* out);
|
||||
};
|
||||
|
||||
} // namespace gin
|
||||
|
||||
#endif // ELECTRON_SHELL_COMMON_GIN_CONVERTERS_GFX_CONVERTER_H_
|
||||
|
||||
Reference in New Issue
Block a user