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 docs/api/browser-window.md
Expand Up @@ -1455,11 +1455,13 @@ On Windows and Linux always returns

* `opacity` Number - between 0.0 (fully transparent) and 1.0 (fully opaque)

Sets the opacity of the window. On Linux does nothing.
Sets the opacity of the window. On Linux, does nothing. Out of bound number
values are clamped to the [0, 1] range.

#### `win.getOpacity()` _Windows_ _macOS_

Returns `Number` - between 0.0 (fully transparent) and 1.0 (fully opaque)
Returns `Number` - between 0.0 (fully transparent) and 1.0 (fully opaque). On
Linux, always returns 1.
erickzhao marked this conversation as resolved.
Show resolved Hide resolved

#### `win.setShape(rects)` _Windows_ _Linux_ _Experimental_

Expand Down
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
19 changes: 13 additions & 6 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -7,13 +7,11 @@ import * as qs from 'querystring'
import * as http from 'http'
import { AddressInfo } from 'net'
import { app, BrowserWindow, BrowserView, ipcMain, OnBeforeSendHeadersListenerDetails, protocol, screen, webContents, session, WebContents } from 'electron'
import { emittedOnce } from './events-helpers';
import { closeWindow } from './window-helpers';
import { emittedOnce } from './events-helpers'
import { ifit, ifdescribe } from './spec-helpers'
import { closeWindow } from './window-helpers'
const { expect } = chai

const ifit = (condition: boolean) => (condition ? it : it.skip)
const ifdescribe = (condition: boolean) => (condition ? describe : describe.skip)

chai.use(chaiAsPromised)

const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures')
Expand Down Expand Up @@ -1240,7 +1238,8 @@ describe('BrowserWindow module', () => {
})
})

describe('BrowserWindow.setOpacity(opacity)', () => {

ifdescribe(process.platform !== 'linux')('BrowserWindow.setOpacity(opacity)', () => {
codebytere marked this conversation as resolved.
Show resolved Hide resolved
afterEach(closeAllWindows)
it('make window with initial opacity', () => {
const w = new BrowserWindow({ show: false, opacity: 0.5 })
Expand All @@ -1257,6 +1256,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