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
6 changes: 4 additions & 2 deletions shell/browser/native_window_views.cc
Expand Up @@ -879,14 +879,14 @@ 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
#if defined(OS_WIN)
HWND hwnd = GetAcceleratedWidget();
if (!layered_) {
LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
Expand All @@ -895,8 +895,10 @@ void NativeWindowViews::SetOpacity(const double opacity) {
layered_ = true;
}
::SetLayeredWindowAttributes(hwnd, 0, boundedOpacity * 255, LWA_ALPHA);
#endif
opacity_ = boundedOpacity;
#else
opacity_ = 1; // setOpacity unsupported on Linux
erickzhao marked this conversation as resolved.
Show resolved Hide resolved
#endif
}

double NativeWindowViews::GetOpacity() {
Expand Down
6 changes: 6 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 Down