From d86de0567593db387585692c8597acccc32a1ccb Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Sun, 11 Nov 2018 10:10:14 -0800 Subject: [PATCH 1/3] feat: allow partial setting of window bounds --- docs/api/browser-window.md | 16 +++++++++++++++- lib/browser/api/browser-window.js | 9 +++++++++ spec/api-browser-window-spec.js | 24 ++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 075c99c2b4235..252528ec740c4 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -896,7 +896,21 @@ Closes the currently open [Quick Look][quick-look] panel. * `bounds` [Rectangle](structures/rectangle.md) * `animate` Boolean (optional) _macOS_ -Resizes and moves the window to the supplied bounds +Resizes and moves the window to the supplied bounds. Also allows setting any of the `x`, `y`, `width`, or `height` properties, instead of setting them all at once. Properties that are not set will fall back to their current values. + +```javascript +const { BrowserWindow } = require('electron') +let win = new BrowserWindow() + +// set all bounds properties +win.setBounds({ x: 440, y: 225, width: 800, height: 600 }) + +// set a single bounds property +win.setBounds({ width: 100 }) + +// { x: 440, y: 225, width: 100, height: 600 } +console.log(win.getBounds()) +``` #### `win.getBounds()` diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index 7fd90bb6cde70..5292ea39dc542 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -17,6 +17,15 @@ BrowserWindow.prototype._init = function () { // Create WebContentsView. this.setContentView(new WebContentsView(this.webContents)) + const nativeSetBounds = this.setBounds + this.setBounds = (bounds, animate) => { + bounds = { + ...this.getBounds(), + ...bounds + } + nativeSetBounds.call(this, bounds, animate) + } + // Make new windows requested by links behave like "window.open" this.webContents.on('-new-window', (event, url, frameName, disposition, additionalFeatures, postData, diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 52cfcaa0145e9..611de1f913d0f 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -524,6 +524,30 @@ describe('BrowserWindow module', () => { }) }) + describe('BrowserWindow.setBounds(bounds[, animate])', () => { + it('sets the window bounds with full bounds', () => { + const fullBounds = { x: 440, y: 225, width: 500, height: 400 } + w.setBounds(fullBounds) + + assertBoundsEqual(w.getBounds(), fullBounds) + }) + + it('sets the window bounds with partial bounds', () => { + const fullBounds = { x: 440, y: 225, width: 500, height: 400 } + w.setBounds(fullBounds) + + assertBoundsEqual(w.getBounds(), fullBounds) + + w.setBounds({ width: 100 }) + assertBoundsEqual(w.getBounds(), { + x: 440, + y: 225, + width: 100, // updated bound + height: 400 + }) + }) + }) + describe('BrowserWindow.setSize(width, height)', () => { it('sets the window size', async () => { const size = [300, 400] From 35359d3d14f3be61d9e1fae2d71464a4ec51f907 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 12 Nov 2018 09:00:35 -0800 Subject: [PATCH 2/3] address feedback from review --- docs/api/browser-window.md | 2 +- lib/browser/api/browser-window.js | 4 ++-- spec/api-browser-window-spec.js | 12 ++++-------- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 252528ec740c4..15991eee9b29e 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -900,7 +900,7 @@ Resizes and moves the window to the supplied bounds. Also allows setting any of ```javascript const { BrowserWindow } = require('electron') -let win = new BrowserWindow() +const win = new BrowserWindow() // set all bounds properties win.setBounds({ x: 440, y: 225, width: 800, height: 600 }) diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index 5292ea39dc542..a957eb390d3d0 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -18,12 +18,12 @@ BrowserWindow.prototype._init = function () { this.setContentView(new WebContentsView(this.webContents)) const nativeSetBounds = this.setBounds - this.setBounds = (bounds, animate) => { + this.setBounds = (bounds, ...opts) => { bounds = { ...this.getBounds(), ...bounds } - nativeSetBounds.call(this, bounds, animate) + nativeSetBounds.call(this, bounds, ...opts) } // Make new windows requested by links behave like "window.open" diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 611de1f913d0f..3bf7be4d36f0b 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -536,15 +536,11 @@ describe('BrowserWindow module', () => { const fullBounds = { x: 440, y: 225, width: 500, height: 400 } w.setBounds(fullBounds) - assertBoundsEqual(w.getBounds(), fullBounds) + const boundsUpdate = { width: 100 } + w.setBounds(boundsUpdate) - w.setBounds({ width: 100 }) - assertBoundsEqual(w.getBounds(), { - x: 440, - y: 225, - width: 100, // updated bound - height: 400 - }) + const expectedBounds = Object.assign(fullBounds, boundsUpdate) + assertBoundsEqual(w.getBounds(), expectedBounds) }) }) From 0bab770d97dc95e3e237ff315179785eb05e4b4e Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Mon, 12 Nov 2018 12:56:36 -0500 Subject: [PATCH 3/3] Improve optional bound wording in docs Co-Authored-By: codebytere --- docs/api/browser-window.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 15991eee9b29e..e6e9c236b68e9 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -896,7 +896,7 @@ Closes the currently open [Quick Look][quick-look] panel. * `bounds` [Rectangle](structures/rectangle.md) * `animate` Boolean (optional) _macOS_ -Resizes and moves the window to the supplied bounds. Also allows setting any of the `x`, `y`, `width`, or `height` properties, instead of setting them all at once. Properties that are not set will fall back to their current values. +Resizes and moves the window to the supplied bounds. Any properties that are not supplied will default to their current values. ```javascript const { BrowserWindow } = require('electron')