Skip to content

Commit

Permalink
feat: allow partial setting of window bounds (#15677)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
codebytere committed Nov 12, 2018
1 parent 0c46a7a commit c06f023
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
16 changes: 15 additions & 1 deletion docs/api/browser-window.md
Expand Up @@ -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()`

Expand Down
9 changes: 9 additions & 0 deletions lib/browser/api/browser-window.js
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions spec/api-browser-window-spec.js
Expand Up @@ -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]
Expand Down

0 comments on commit c06f023

Please sign in to comment.