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: normalize behavior of win.setOpacity() for invalid number values across operating systems #19535

Merged
merged 14 commits into from Aug 7, 2019
9 changes: 8 additions & 1 deletion shell/browser/native_window_mac.mm
Expand Up @@ -1032,7 +1032,14 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
}

void NativeWindowMac::SetOpacity(const double opacity) {
[window_ setAlphaValue:opacity];
double boundedOpacity = opacity;
if (opacity < 0) {
boundedOpacity = 0;
} else if (opacity > 1) {
boundedOpacity = 1;
}
erickzhao marked this conversation as resolved.
Show resolved Hide resolved

[window_ setAlphaValue:boundedOpacity];
}

double NativeWindowMac::GetOpacity() {
Expand Down
13 changes: 11 additions & 2 deletions shell/browser/native_window_views.cc
Expand Up @@ -880,16 +880,25 @@ bool NativeWindowViews::HasShadow() {

void NativeWindowViews::SetOpacity(const double opacity) {
#if defined(OS_WIN)
double boundedOpacity = opacity;
if (opacity > 1) {
boundedOpacity = 1;
} else if (opacity < 0) {
boundedOpacity = 0;
}

erickzhao marked this conversation as resolved.
Show resolved Hide resolved
HWND hwnd = GetAcceleratedWidget();
if (!layered_) {
LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
ex_style |= WS_EX_LAYERED;
::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
layered_ = true;
}
::SetLayeredWindowAttributes(hwnd, 0, opacity * 255, LWA_ALPHA);
::SetLayeredWindowAttributes(hwnd, 0, boundedOpacity * 255, LWA_ALPHA);
opacity_ = boundedOpacity;
#else
opacity_ = 1; // setOpacity unsupported on Linux
erickzhao marked this conversation as resolved.
Show resolved Hide resolved
#endif
opacity_ = opacity;
}

double NativeWindowViews::GetOpacity() {
Expand Down
14 changes: 14 additions & 0 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -1241,6 +1241,12 @@ describe('BrowserWindow module', () => {
})

describe('BrowserWindow.setOpacity(opacity)', () => {
before(function () {
erickzhao marked this conversation as resolved.
Show resolved Hide resolved
// setOpacity() unsupported on Linux
if (process.platform === 'linux') {
this.skip()
}
})
afterEach(closeAllWindows)
it('make window with initial opacity', () => {
const w = new BrowserWindow({ show: false, opacity: 0.5 })
Expand All @@ -1257,6 +1263,14 @@ describe('BrowserWindow module', () => {
expect(w.getOpacity()).to.equal(1.0)
}).to.not.throw()
})

it('does not change opacity if given number out of bounds', () => {
erickzhao marked this conversation as resolved.
Show resolved Hide resolved
const w = new BrowserWindow({ show: false, opacity: 0.5 })
w.setOpacity(100)
expect(w.getOpacity()).to.equal(1.0)
w.setOpacity(-100)
expect(w.getOpacity()).to.equal(0.0)
})
})

describe('BrowserWindow.setShape(rects)', () => {
Expand Down