From c06f023313fa1ba3a21e940746ab074f9a00477a Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 12 Nov 2018 15:31:14 -0500 Subject: [PATCH] feat: allow partial setting of window bounds (#15677) Extend the existing win.setBounds functionality by allowing developers to partially update bounds without being forced to pass in all four bounds values. No existing functionality is altere --- docs/api/browser-window.md | 16 +++++++++++++++- lib/browser/api/browser-window.js | 9 +++++++++ spec/api-browser-window-spec.js | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 075c99c2b423..e6e9c236b68e 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. Any properties that are not supplied will default to their current values. + +```javascript +const { BrowserWindow } = require('electron') +const 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 7fd90bb6cde7..a957eb390d3d 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, ...opts) => { + bounds = { + ...this.getBounds(), + ...bounds + } + nativeSetBounds.call(this, bounds, ...opts) + } + // 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 52cfcaa0145e..3bf7be4d36f0 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -524,6 +524,26 @@ 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) + + const boundsUpdate = { width: 100 } + w.setBounds(boundsUpdate) + + const expectedBounds = Object.assign(fullBounds, boundsUpdate) + assertBoundsEqual(w.getBounds(), expectedBounds) + }) + }) + describe('BrowserWindow.setSize(width, height)', () => { it('sets the window size', async () => { const size = [300, 400]