2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2017-03-01 19:12:22 +00:00
|
|
|
const electron = require('electron')
|
2019-06-03 16:09:47 +00:00
|
|
|
const { WebContentsView, TopLevelWindow, deprecate } = electron
|
2019-03-18 19:37:06 +00:00
|
|
|
const { BrowserWindow } = process.electronBinding('window')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-04-17 07:46:32 +00:00
|
|
|
Object.setPrototypeOf(BrowserWindow.prototype, TopLevelWindow.prototype)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype._init = function () {
|
2018-04-17 07:46:32 +00:00
|
|
|
// Call parent class's _init.
|
|
|
|
TopLevelWindow.prototype._init.call(this)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-04-17 07:46:32 +00:00
|
|
|
// Avoid recursive require.
|
2018-09-13 16:10:51 +00:00
|
|
|
const { app } = electron
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-05-08 05:47:26 +00:00
|
|
|
// Create WebContentsView.
|
|
|
|
this.setContentView(new WebContentsView(this.webContents))
|
|
|
|
|
2018-11-12 20:31:14 +00:00
|
|
|
const nativeSetBounds = this.setBounds
|
|
|
|
this.setBounds = (bounds, ...opts) => {
|
|
|
|
bounds = {
|
|
|
|
...this.getBounds(),
|
|
|
|
...bounds
|
|
|
|
}
|
|
|
|
nativeSetBounds.call(this, bounds, ...opts)
|
|
|
|
}
|
|
|
|
|
2016-05-14 16:26:53 +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
|
2016-05-14 15:41:34 +00:00
|
|
|
// 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
|
2016-05-14 16:26:53 +00:00
|
|
|
// Finder, we still do it on all platforms in case of other bugs we don't
|
2016-05-14 15:41:34 +00:00
|
|
|
// know.
|
2016-03-24 20:15:04 +00:00
|
|
|
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.
|
2016-03-08 17:36:41 +00:00
|
|
|
this.on('blur', (event) => {
|
2016-03-24 20:15:04 +00:00
|
|
|
app.emit('browser-window-blur', event, this)
|
|
|
|
})
|
2016-03-08 17:36:41 +00:00
|
|
|
this.on('focus', (event) => {
|
2016-03-24 20:15:04 +00:00
|
|
|
app.emit('browser-window-focus', event, this)
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-04-13 12:36:43 +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()
|
2016-05-14 15:41:34 +00:00
|
|
|
const visibilityChanged = () => {
|
|
|
|
const newState = this.isVisible() && !this.isMinimized()
|
2016-04-13 14:10:31 +00:00
|
|
|
if (isVisible !== newState) {
|
2016-04-13 12:36:43 +00:00
|
|
|
isVisible = newState
|
2017-02-24 18:18:09 +00:00
|
|
|
const visibilityState = isVisible ? 'visible' : 'hidden'
|
|
|
|
this.webContents.emit('-window-visibility-change', visibilityState)
|
2016-04-13 12:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-14 16:26:53 +00:00
|
|
|
|
2016-05-14 15:41:34 +00:00
|
|
|
const visibilityEvents = ['show', 'hide', 'minimize', 'maximize', 'restore']
|
2018-10-02 01:56:31 +00:00
|
|
|
for (const event of visibilityEvents) {
|
2016-05-14 15:41:34 +00:00
|
|
|
this.on(event, visibilityChanged)
|
|
|
|
}
|
2016-04-13 12:36:43 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Notify the creation of the window.
|
2019-07-29 20:54:37 +00:00
|
|
|
const event = process.electronBinding('event').createEmpty()
|
|
|
|
app.emit('browser-window-created', event, 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,
|
2016-05-14 15:41:34 +00:00
|
|
|
get () {
|
2016-03-24 20:15:04 +00:00
|
|
|
return this.webContents.devToolsWebContents
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-04-17 07:53:55 +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)
|
|
|
|
}
|
|
|
|
|
2016-05-14 15:41:34 +00:00
|
|
|
BrowserWindow.getFocusedWindow = () => {
|
2018-10-02 01:56:31 +00:00
|
|
|
for (const window of BrowserWindow.getAllWindows()) {
|
2018-04-06 13:21:32 +00:00
|
|
|
if (window.isFocused() || window.isDevToolsFocused()) return window
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-05-14 16:26:53 +00:00
|
|
|
return null
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-05-14 15:41:34 +00:00
|
|
|
BrowserWindow.fromWebContents = (webContents) => {
|
2016-12-06 00:18:53 +00:00
|
|
|
for (const window of BrowserWindow.getAllWindows()) {
|
2019-12-17 19:35:28 +00:00
|
|
|
if (window.webContents && window.webContents.equal(webContents)) return window
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2019-09-27 11:07:55 +00:00
|
|
|
|
|
|
|
return null
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-11-22 22:36:54 +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
|
2017-11-22 22:36:54 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Helpers.
|
2016-05-14 15:41:34 +00:00
|
|
|
Object.assign(BrowserWindow.prototype, {
|
2016-05-14 16:26:53 +00:00
|
|
|
loadURL (...args) {
|
2016-12-01 22:37:03 +00:00
|
|
|
return this.webContents.loadURL(...args)
|
2016-05-14 15:41:34 +00:00
|
|
|
},
|
2016-05-14 16:26:53 +00:00
|
|
|
getURL (...args) {
|
2016-05-14 15:41:34 +00:00
|
|
|
return this.webContents.getURL()
|
|
|
|
},
|
2018-09-11 07:56:49 +00:00
|
|
|
loadFile (...args) {
|
|
|
|
return this.webContents.loadFile(...args)
|
2018-01-03 22:38:56 +00:00
|
|
|
},
|
2016-05-14 16:26:53 +00:00
|
|
|
reload (...args) {
|
2016-12-01 22:37:03 +00:00
|
|
|
return this.webContents.reload(...args)
|
2016-05-14 15:41:34 +00:00
|
|
|
},
|
2016-05-14 16:26:53 +00:00
|
|
|
send (...args) {
|
2016-12-01 22:37:03 +00:00
|
|
|
return this.webContents.send(...args)
|
2016-05-14 15:41:34 +00:00
|
|
|
},
|
2016-05-14 16:26:53 +00:00
|
|
|
openDevTools (...args) {
|
2016-12-01 22:37:03 +00:00
|
|
|
return this.webContents.openDevTools(...args)
|
2016-05-14 15:41:34 +00:00
|
|
|
},
|
|
|
|
closeDevTools () {
|
|
|
|
return this.webContents.closeDevTools()
|
|
|
|
},
|
|
|
|
isDevToolsOpened () {
|
|
|
|
return this.webContents.isDevToolsOpened()
|
|
|
|
},
|
|
|
|
isDevToolsFocused () {
|
|
|
|
return this.webContents.isDevToolsFocused()
|
|
|
|
},
|
|
|
|
toggleDevTools () {
|
|
|
|
return this.webContents.toggleDevTools()
|
|
|
|
},
|
2016-05-14 16:26:53 +00:00
|
|
|
inspectElement (...args) {
|
2016-12-01 22:37:03 +00:00
|
|
|
return this.webContents.inspectElement(...args)
|
2016-05-14 15:41:34 +00:00
|
|
|
},
|
2019-02-01 18:44:24 +00:00
|
|
|
inspectSharedWorker () {
|
|
|
|
return this.webContents.inspectSharedWorker()
|
|
|
|
},
|
2016-05-14 15:41:34 +00:00
|
|
|
inspectServiceWorker () {
|
|
|
|
return this.webContents.inspectServiceWorker()
|
2016-06-08 17:19:28 +00:00
|
|
|
},
|
|
|
|
showDefinitionForSelection () {
|
|
|
|
return this.webContents.showDefinitionForSelection()
|
2016-07-05 22:43:57 +00:00
|
|
|
},
|
|
|
|
capturePage (...args) {
|
2016-12-01 22:37:03 +00:00
|
|
|
return this.webContents.capturePage(...args)
|
2017-03-01 18:55:28 +00:00
|
|
|
},
|
|
|
|
setTouchBar (touchBar) {
|
2017-03-01 19:12:22 +00:00
|
|
|
electron.TouchBar._setOnWindow(touchBar, this)
|
2018-10-19 00:32:22 +00:00
|
|
|
},
|
|
|
|
setBackgroundThrottling (allowed) {
|
|
|
|
this.webContents.setBackgroundThrottling(allowed)
|
2017-03-01 18:55:28 +00:00
|
|
|
}
|
|
|
|
})
|
2016-12-16 06:24:51 +00:00
|
|
|
|
2019-06-03 16:09:47 +00:00
|
|
|
// Deprecations
|
|
|
|
deprecate.fnToProperty(BrowserWindow.prototype, 'autoHideMenuBar', '_isMenuBarAutoHide', '_setAutoHideMenuBar')
|
2019-06-05 05:44:16 +00:00
|
|
|
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')
|
2019-06-03 16:09:47 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
module.exports = BrowserWindow
|