electron/lib/browser/api/browser-window.js

204 lines
6.2 KiB
JavaScript
Raw Normal View History

'use strict'
const electron = require('electron')
const { WebContentsView, TopLevelWindow, deprecate } = electron
const { BrowserWindow } = process.electronBinding('window')
2016-01-12 02:40:23 +00:00
Object.setPrototypeOf(BrowserWindow.prototype, TopLevelWindow.prototype)
2016-01-12 02:40:23 +00:00
BrowserWindow.prototype._init = function () {
// Call parent class's _init.
TopLevelWindow.prototype._init.call(this)
2016-01-12 02:40:23 +00:00
// Avoid recursive require.
2018-09-13 16:10:51 +00:00
const { app } = electron
2016-01-12 02:40:23 +00:00
// 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)
}
2016-01-14 18:44:21 +00:00
// window.resizeTo(...)
// window.moveTo(...)
this.webContents.on('move', (event, size) => {
this.setBounds(size)
})
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Hide the auto-hide menu when webContents is focused.
this.webContents.on('activate', () => {
if (process.platform !== 'darwin' && this.isMenuBarAutoHide() && this.isMenuBarVisible()) {
this.setMenuBarVisibility(false)
}
})
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Change window title to page title.
this.webContents.on('page-title-updated', (event, title, ...args) => {
// Route the event to BrowserWindow.
this.emit('page-title-updated', event, title, ...args)
if (!this.isDestroyed() && !event.defaultPrevented) this.setTitle(title)
})
2016-01-12 02:40:23 +00:00
// Sometimes the webContents doesn't get focus when window is shown, so we
// have to force focusing on webContents in this case. The safest way is to
// focus it when we first start to load URL, if we do it earlier it won't
// have effect, if we do it later we might move focus in the page.
//
2016-06-18 13:26:26 +00:00
// Though this hack is only needed on macOS when the app is launched from
// Finder, we still do it on all platforms in case of other bugs we don't
// know.
this.webContents.once('load-url', function () {
this.focus()
})
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Redirect focus/blur event to app instance too.
this.on('blur', (event) => {
app.emit('browser-window-blur', event, this)
})
this.on('focus', (event) => {
app.emit('browser-window-focus', event, this)
})
2016-01-12 02:40:23 +00:00
// Subscribe to visibilityState changes and pass to renderer process.
2016-04-13 13:56:11 +00:00
let isVisible = this.isVisible() && !this.isMinimized()
const visibilityChanged = () => {
const newState = this.isVisible() && !this.isMinimized()
2016-04-13 14:10:31 +00:00
if (isVisible !== newState) {
isVisible = newState
const visibilityState = isVisible ? 'visible' : 'hidden'
this.webContents.emit('-window-visibility-change', visibilityState)
}
}
const visibilityEvents = ['show', 'hide', 'minimize', 'maximize', 'restore']
for (const event of visibilityEvents) {
this.on(event, visibilityChanged)
}
2016-01-14 18:35:29 +00:00
// Notify the creation of the window.
app.emit('browser-window-created', {}, this)
2016-01-12 02:40:23 +00:00
2016-03-11 19:05:48 +00:00
Object.defineProperty(this, 'devToolsWebContents', {
2016-01-12 02:40:23 +00:00
enumerable: true,
configurable: false,
get () {
return this.webContents.devToolsWebContents
2016-01-12 02:40:23 +00:00
}
})
}
2016-01-12 02:40:23 +00:00
const isBrowserWindow = (win) => {
return win && win.constructor.name === 'BrowserWindow'
}
BrowserWindow.fromId = (id) => {
const win = TopLevelWindow.fromId(id)
return isBrowserWindow(win) ? win : null
}
BrowserWindow.getAllWindows = () => {
return TopLevelWindow.getAllWindows().filter(isBrowserWindow)
}
BrowserWindow.getFocusedWindow = () => {
for (const window of BrowserWindow.getAllWindows()) {
if (window.isFocused() || window.isDevToolsFocused()) return window
2016-01-12 02:40:23 +00:00
}
return null
}
2016-01-12 02:40:23 +00:00
BrowserWindow.fromWebContents = (webContents) => {
2016-12-06 00:18:53 +00:00
for (const window of BrowserWindow.getAllWindows()) {
if (window.webContents.equal(webContents)) return window
2016-01-12 02:40:23 +00:00
}
}
2016-01-12 02:40:23 +00:00
BrowserWindow.fromBrowserView = (browserView) => {
for (const window of BrowserWindow.getAllWindows()) {
if (window.getBrowserView() === browserView) return window
}
2017-11-22 23:48:11 +00:00
return null
}
BrowserWindow.fromDevToolsWebContents = (webContents) => {
2016-12-06 00:18:53 +00:00
for (const window of BrowserWindow.getAllWindows()) {
2018-09-13 16:10:51 +00:00
const { devToolsWebContents } = window
2016-12-06 00:18:53 +00:00
if (devToolsWebContents != null && devToolsWebContents.equal(webContents)) {
return window
}
2016-01-12 02:40:23 +00:00
}
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Helpers.
Object.assign(BrowserWindow.prototype, {
loadURL (...args) {
return this.webContents.loadURL(...args)
},
getURL (...args) {
return this.webContents.getURL()
},
loadFile (...args) {
return this.webContents.loadFile(...args)
},
reload (...args) {
return this.webContents.reload(...args)
},
send (...args) {
return this.webContents.send(...args)
},
openDevTools (...args) {
return this.webContents.openDevTools(...args)
},
closeDevTools () {
return this.webContents.closeDevTools()
},
isDevToolsOpened () {
return this.webContents.isDevToolsOpened()
},
isDevToolsFocused () {
return this.webContents.isDevToolsFocused()
},
toggleDevTools () {
return this.webContents.toggleDevTools()
},
inspectElement (...args) {
return this.webContents.inspectElement(...args)
},
inspectSharedWorker () {
return this.webContents.inspectSharedWorker()
},
inspectServiceWorker () {
return this.webContents.inspectServiceWorker()
},
showDefinitionForSelection () {
return this.webContents.showDefinitionForSelection()
2016-07-05 22:43:57 +00:00
},
capturePage (...args) {
return this.webContents.capturePage(...args)
2017-03-01 18:55:28 +00:00
},
setTouchBar (touchBar) {
electron.TouchBar._setOnWindow(touchBar, this)
},
setBackgroundThrottling (allowed) {
this.webContents.setBackgroundThrottling(allowed)
2017-03-01 18:55:28 +00:00
}
})
// Deprecations
deprecate.fnToProperty(BrowserWindow.prototype, 'autoHideMenuBar', '_isMenuBarAutoHide', '_setAutoHideMenuBar')
deprecate.fnToProperty(BrowserWindow.prototype, 'minimizable', '_isMinimizable', '_setMinimizable')
deprecate.fnToProperty(BrowserWindow.prototype, 'maximizable', '_isMaximizable', '_setMaximizable')
deprecate.fnToProperty(BrowserWindow.prototype, 'resizable', '_isResizable', '_setResizable')
deprecate.fnToProperty(BrowserWindow.prototype, 'fullScreenable', '_isFullScreenable', '_setFullScreenable')
deprecate.fnToProperty(BrowserWindow.prototype, 'closable', '_isClosable', '_setClosable')
deprecate.fnToProperty(BrowserWindow.prototype, 'movable', '_isMovable', '_setMovable')
module.exports = BrowserWindow