fix: BrowserWindow.setBackgroundColor
should work with transparency (#42927)
fix: BrowserWindow.setBackgroundColor should work with transparency Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
parent
24a6c66145
commit
e880530911
2 changed files with 35 additions and 12 deletions
|
@ -3729,14 +3729,17 @@ void WebContents::SetBackgroundColor(std::optional<SkColor> maybe_color) {
|
||||||
type_ == Type::kBrowserView
|
type_ == Type::kBrowserView
|
||||||
? SK_ColorTRANSPARENT
|
? SK_ColorTRANSPARENT
|
||||||
: SK_ColorWHITE);
|
: SK_ColorWHITE);
|
||||||
|
bool is_opaque = SkColorGetA(color) == SK_AlphaOPAQUE;
|
||||||
web_contents()->SetPageBaseBackgroundColor(color);
|
web_contents()->SetPageBaseBackgroundColor(color);
|
||||||
|
|
||||||
content::RenderFrameHost* rfh = web_contents()->GetPrimaryMainFrame();
|
content::RenderFrameHost* rfh = web_contents()->GetPrimaryMainFrame();
|
||||||
if (!rfh)
|
if (!rfh)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
content::RenderWidgetHostView* rwhv = rfh->GetView();
|
content::RenderWidgetHostView* rwhv = rfh->GetView();
|
||||||
if (rwhv) {
|
if (rwhv) {
|
||||||
rwhv->SetBackgroundColor(color);
|
// RenderWidgetHostView doesn't allow setting an alpha that's not 0 or 255.
|
||||||
|
rwhv->SetBackgroundColor(is_opaque ? color : SK_ColorTRANSPARENT);
|
||||||
static_cast<content::RenderWidgetHostViewBase*>(rwhv)
|
static_cast<content::RenderWidgetHostViewBase*>(rwhv)
|
||||||
->SetContentBackgroundColor(color);
|
->SetContentBackgroundColor(color);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1660,6 +1660,7 @@ describe('BrowserWindow module', () => {
|
||||||
w = new BrowserWindow({});
|
w = new BrowserWindow({});
|
||||||
expect(w.getBackgroundColor()).to.equal('#FFFFFF');
|
expect(w.getBackgroundColor()).to.equal('#FFFFFF');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns correct value if backgroundColor is set', () => {
|
it('returns correct value if backgroundColor is set', () => {
|
||||||
const backgroundColor = '#BBAAFF';
|
const backgroundColor = '#BBAAFF';
|
||||||
w.destroy();
|
w.destroy();
|
||||||
|
@ -1668,6 +1669,7 @@ describe('BrowserWindow module', () => {
|
||||||
});
|
});
|
||||||
expect(w.getBackgroundColor()).to.equal(backgroundColor);
|
expect(w.getBackgroundColor()).to.equal(backgroundColor);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns correct value from setBackgroundColor()', () => {
|
it('returns correct value from setBackgroundColor()', () => {
|
||||||
const backgroundColor = '#AABBFF';
|
const backgroundColor = '#AABBFF';
|
||||||
w.destroy();
|
w.destroy();
|
||||||
|
@ -1675,24 +1677,42 @@ describe('BrowserWindow module', () => {
|
||||||
w.setBackgroundColor(backgroundColor);
|
w.setBackgroundColor(backgroundColor);
|
||||||
expect(w.getBackgroundColor()).to.equal(backgroundColor);
|
expect(w.getBackgroundColor()).to.equal(backgroundColor);
|
||||||
});
|
});
|
||||||
it('returns correct color with multiple passed formats', () => {
|
|
||||||
|
it('returns correct color with multiple passed formats', async () => {
|
||||||
w.destroy();
|
w.destroy();
|
||||||
w = new BrowserWindow({});
|
w = new BrowserWindow({});
|
||||||
|
|
||||||
w.setBackgroundColor('#AABBFF');
|
await w.loadURL('about:blank');
|
||||||
expect(w.getBackgroundColor()).to.equal('#AABBFF');
|
|
||||||
|
|
||||||
w.setBackgroundColor('blueviolet');
|
const colors = new Map([
|
||||||
expect(w.getBackgroundColor()).to.equal('#8A2BE2');
|
['blueviolet', '#8A2BE2'],
|
||||||
|
['rgb(255, 0, 185)', '#FF00B9'],
|
||||||
|
['hsl(155, 100%, 50%)', '#00FF95'],
|
||||||
|
['#355E3B', '#355E3B']
|
||||||
|
]);
|
||||||
|
|
||||||
w.setBackgroundColor('rgb(255, 0, 185)');
|
for (const [color, hex] of colors) {
|
||||||
expect(w.getBackgroundColor()).to.equal('#FF00B9');
|
w.setBackgroundColor(color);
|
||||||
|
expect(w.getBackgroundColor()).to.equal(hex);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
w.setBackgroundColor('rgba(245, 40, 145, 0.8)');
|
it('can set the background color with transparency', async () => {
|
||||||
expect(w.getBackgroundColor()).to.equal('#F52891');
|
w.destroy();
|
||||||
|
w = new BrowserWindow({});
|
||||||
|
|
||||||
w.setBackgroundColor('hsl(155, 100%, 50%)');
|
await w.loadURL('about:blank');
|
||||||
expect(w.getBackgroundColor()).to.equal('#00FF95');
|
|
||||||
|
const colors = new Map([
|
||||||
|
['hsl(155, 100%, 50%)', '#00FF95'],
|
||||||
|
['rgba(245, 40, 145, 0.8)', '#F52891'],
|
||||||
|
['#1D1F21d9', '#1F21D9']
|
||||||
|
]);
|
||||||
|
|
||||||
|
for (const [color, hex] of colors) {
|
||||||
|
w.setBackgroundColor(color);
|
||||||
|
expect(w.getBackgroundColor()).to.equal(hex);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue