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 #19723

Merged
merged 6 commits into from Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion atom/browser/native_window_mac.mm
Expand Up @@ -22,6 +22,7 @@
#include "atom/common/options_switches.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/numerics/ranges.h"
#include "base/strings/sys_string_conversions.h"
#include "components/remote_cocoa/app_shim/bridged_native_widget_impl.h"
#include "content/public/browser/browser_accessibility_state.h"
Expand Down Expand Up @@ -1047,7 +1048,8 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
}

void NativeWindowMac::SetOpacity(const double opacity) {
[window_ setAlphaValue:opacity];
const double boundedOpacity = base::ClampToRange(opacity, 0.0, 1.0);
[window_ setAlphaValue:boundedOpacity];
}

double NativeWindowMac::GetOpacity() {
Expand Down
8 changes: 6 additions & 2 deletions atom/browser/native_window_views.cc
Expand Up @@ -25,6 +25,7 @@
#include "atom/common/draggable_region.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/options_switches.h"
#include "base/numerics/ranges.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/browser_thread.h"
#include "native_mate/dictionary.h"
Expand Down Expand Up @@ -866,16 +867,19 @@ bool NativeWindowViews::HasShadow() {

void NativeWindowViews::SetOpacity(const double opacity) {
#if defined(OS_WIN)
const double boundedOpacity = base::ClampToRange(opacity, 0.0, 1.0);
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.0; // setOpacity unsupported on Linux
#endif
opacity_ = opacity;
}

double NativeWindowViews::GetOpacity() {
Expand Down
54 changes: 39 additions & 15 deletions spec/api-browser-window-spec.js
Expand Up @@ -1214,24 +1214,48 @@ describe('BrowserWindow module', () => {
})

describe('BrowserWindow.setOpacity(opacity)', () => {
it('make window with initial opacity', () => {
w.destroy()
w = new BrowserWindow({
show: false,
width: 400,
height: 400,
opacity: 0.5
describe('Windows and Mac', function () {
if (process.platform === 'linux') {
this.skip()
}

it('make window with initial opacity', () => {
const w = new BrowserWindow({ show: false, opacity: 0.5 })
expect(w.getOpacity()).to.equal(0.5)
})

it('allows setting the opacity', () => {
const w = new BrowserWindow({ show: false })
expect(() => {
w.setOpacity(0.0)
expect(w.getOpacity()).to.equal(0.0)
w.setOpacity(0.5)
expect(w.getOpacity()).to.equal(0.5)
w.setOpacity(1.0)
expect(w.getOpacity()).to.equal(1.0)
}).to.not.throw()
})

it('clamps opacity to [0.0...1.0]', () => {
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)
})
assert.strictEqual(w.getOpacity(), 0.5)
})
it('allows setting the opacity', () => {
assert.doesNotThrow(() => {
w.setOpacity(0.0)
assert.strictEqual(w.getOpacity(), 0.0)

describe('Linux', function () {
if (process.platform !== 'linux') {
this.skip()
}

it('sets 1 regardless of parameter', () => {
const w = new BrowserWindow({ show: false })
w.setOpacity(0)
expect(w.getOpacity()).to.equal(1.0)
w.setOpacity(0.5)
assert.strictEqual(w.getOpacity(), 0.5)
w.setOpacity(1.0)
assert.strictEqual(w.getOpacity(), 1.0)
expect(w.getOpacity()).to.equal(1.0)
})
})
})
Expand Down