8000 feat: allow partial setting of window bounds by codebytere · Pull Request #15699 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: allow partial setting of window bounds #15699

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

Merged
merged 1 commit into from
Nov 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/api/browser-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,18 @@ 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: 200 })
// { x: 440, y: 225, width: 200, height: 600 }
console.log(win.getBounds())
```

#### `win.getBounds()`

Expand Down
9 changes: 9 additions & 0 deletions lib/browser/api/browser-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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
19 changes: 19 additions & 0 deletions spec/api-browser-window-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,25 @@ 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: 200 }
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