2016-03-24 13:15:04 -07:00
|
|
|
'use strict'
|
2016-03-10 11:54:17 -08:00
|
|
|
|
2017-03-01 11:12:22 -08:00
|
|
|
const electron = require('electron')
|
2018-10-06 13:48:00 +02:00
|
|
|
const { WebContentsView, TopLevelWindow } = electron
|
2018-09-14 02:10:51 +10:00
|
|
|
const { BrowserWindow } = process.atomBinding('window')
|
2018-10-06 13:48:00 +02:00
|
|
|
const ipcMain = require('@electron/internal/browser/ipc-main-internal')
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2018-04-17 16:46:32 +09:00
|
|
|
Object.setPrototypeOf(BrowserWindow.prototype, TopLevelWindow.prototype)
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-03-24 13:15:04 -07:00
|
|
|
BrowserWindow.prototype._init = function () {
|
2018-04-17 16:46:32 +09:00
|
|
|
// Call parent class's _init.
|
|
|
|
TopLevelWindow.prototype._init.call(this)
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2018-04-17 16:46:32 +09:00
|
|
|
// Avoid recursive require.
|
2018-09-14 02:10:51 +10:00
|
|
|
const { app } = electron
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2018-05-08 14:47:26 +09:00
|
|
|
// Create WebContentsView.
|
|
|
|
this.setContentView(new WebContentsView(this.webContents))
|
|
|
|
|
2018-11-12 15:31:14 -05:00
|
|
|
const nativeSetBounds = this.setBounds
|
|
|
|
this.setBounds = (bounds, ...opts) => {
|
|
|
|
bounds = {
|
|
|
|
...this.getBounds(),
|
|
|
|
...bounds
|
|
|
|
}
|
|
|
|
nativeSetBounds.call(this, bounds, ...opts)
|
|
|
|
}
|
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Make new windows requested by links behave like "window.open"
|
2018-04-05 16:13:24 -07:00
|
|
|
this.webContents.on('-new-window', (event, url, frameName, disposition,
|
2018-09-14 02:10:51 +10:00
|
|
|
additionalFeatures, postData,
|
|
|
|
referrer) => {
|
2016-05-14 11:41:34 -04:00
|
|
|
const options = {
|
2016-01-11 18:40:23 -08:00
|
|
|
show: true,
|
|
|
|
width: 800,
|
|
|
|
height: 600
|
2016-03-24 13:15:04 -07:00
|
|
|
}
|
2017-01-12 12:50:14 -08:00
|
|
|
ipcMain.emit('ELECTRON_GUEST_WINDOW_MANAGER_INTERNAL_WINDOW_OPEN',
|
2018-09-14 02:10:51 +10:00
|
|
|
event, url, referrer, frameName, disposition,
|
|
|
|
options, additionalFeatures, postData)
|
2016-03-24 13:15:04 -07:00
|
|
|
})
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-08-15 21:13:18 -03:00
|
|
|
// Create a new browser window for the native implementation of
|
2017-05-23 13:26:19 -07:00
|
|
|
// "window.open", used in sandbox and nativeWindowOpen mode
|
2016-08-15 21:13:18 -03:00
|
|
|
this.webContents.on('-add-new-contents', (event, webContents, disposition,
|
2018-10-23 03:02:25 +09:00
|
|
|
userGesture, left, top, width, height, url, frameName) => {
|
2017-08-14 14:18:14 -03:00
|
|
|
if ((disposition !== 'foreground-tab' && disposition !== 'new-window' &&
|
2018-10-23 03:02:25 +09:00
|
|
|
disposition !== 'background-tab')) {
|
2017-08-14 14:18:14 -03:00
|
|
|
event.preventDefault()
|
2016-08-15 21:13:18 -03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
show: true,
|
|
|
|
x: left,
|
|
|
|
y: top,
|
|
|
|
width: width || 800,
|
|
|
|
height: height || 600,
|
|
|
|
webContents: webContents
|
|
|
|
}
|
2018-04-05 16:13:24 -07:00
|
|
|
const referrer = { url: '', policy: 'default' }
|
2017-01-12 12:50:14 -08:00
|
|
|
ipcMain.emit('ELECTRON_GUEST_WINDOW_MANAGER_INTERNAL_WINDOW_OPEN',
|
2018-09-14 02:10:51 +10:00
|
|
|
event, url, referrer, frameName, disposition, options)
|
2016-08-15 21:13:18 -03:00
|
|
|
})
|
|
|
|
|
2016-01-14 10:44:21 -08:00
|
|
|
// window.resizeTo(...)
|
|
|
|
// window.moveTo(...)
|
2016-03-08 11:11:17 -08:00
|
|
|
this.webContents.on('move', (event, size) => {
|
2016-03-24 13:15:04 -07:00
|
|
|
this.setBounds(size)
|
|
|
|
})
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Hide the auto-hide menu when webContents is focused.
|
2016-03-08 09:36:41 -08:00
|
|
|
this.webContents.on('activate', () => {
|
|
|
|
if (process.platform !== 'darwin' && this.isMenuBarAutoHide() && this.isMenuBarVisible()) {
|
2016-03-24 13:15:04 -07:00
|
|
|
this.setMenuBarVisibility(false)
|
2016-03-08 09:36:41 -08:00
|
|
|
}
|
2016-03-24 13:15:04 -07:00
|
|
|
})
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Change window title to page title.
|
2016-01-30 12:20:28 +08:00
|
|
|
this.webContents.on('page-title-updated', (event, title) => {
|
|
|
|
// Route the event to BrowserWindow.
|
2016-03-24 13:15:04 -07:00
|
|
|
this.emit('page-title-updated', event, title)
|
2017-04-05 16:57:33 +05:30
|
|
|
if (!this.isDestroyed() && !event.defaultPrevented) this.setTitle(title)
|
2016-03-24 13:15:04 -07:00
|
|
|
})
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-05-14 12:26:53 -04: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 11:41:34 -04:00
|
|
|
// have effect, if we do it later we might move focus in the page.
|
|
|
|
//
|
2016-06-18 15:26:26 +02:00
|
|
|
// Though this hack is only needed on macOS when the app is launched from
|
2016-05-14 12:26:53 -04:00
|
|
|
// Finder, we still do it on all platforms in case of other bugs we don't
|
2016-05-14 11:41:34 -04:00
|
|
|
// know.
|
2016-03-24 13:15:04 -07:00
|
|
|
this.webContents.once('load-url', function () {
|
|
|
|
this.focus()
|
|
|
|
})
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Redirect focus/blur event to app instance too.
|
2016-03-08 09:36:41 -08:00
|
|
|
this.on('blur', (event) => {
|
2016-03-24 13:15:04 -07:00
|
|
|
app.emit('browser-window-blur', event, this)
|
|
|
|
})
|
2016-03-08 09:36:41 -08:00
|
|
|
this.on('focus', (event) => {
|
2016-03-24 13:15:04 -07:00
|
|
|
app.emit('browser-window-focus', event, this)
|
|
|
|
})
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-04-13 21:36:43 +09:00
|
|
|
// Subscribe to visibilityState changes and pass to renderer process.
|
2016-04-13 22:56:11 +09:00
|
|
|
let isVisible = this.isVisible() && !this.isMinimized()
|
2016-05-14 11:41:34 -04:00
|
|
|
const visibilityChanged = () => {
|
|
|
|
const newState = this.isVisible() && !this.isMinimized()
|
2016-04-13 23:10:31 +09:00
|
|
|
if (isVisible !== newState) {
|
2016-04-13 21:36:43 +09:00
|
|
|
isVisible = newState
|
2017-02-24 10:18:09 -08:00
|
|
|
const visibilityState = isVisible ? 'visible' : 'hidden'
|
|
|
|
this.webContents.emit('-window-visibility-change', visibilityState)
|
2016-04-13 21:36:43 +09:00
|
|
|
}
|
|
|
|
}
|
2016-05-14 12:26:53 -04:00
|
|
|
|
2016-05-14 11:41:34 -04:00
|
|
|
const visibilityEvents = ['show', 'hide', 'minimize', 'maximize', 'restore']
|
2018-10-02 03:56:31 +02:00
|
|
|
for (const event of visibilityEvents) {
|
2016-05-14 11:41:34 -04:00
|
|
|
this.on(event, visibilityChanged)
|
|
|
|
}
|
2016-04-13 21:36:43 +09:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Notify the creation of the window.
|
2016-03-24 13:15:04 -07:00
|
|
|
app.emit('browser-window-created', {}, this)
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-03-11 11:05:48 -08:00
|
|
|
Object.defineProperty(this, 'devToolsWebContents', {
|
2016-01-11 18:40:23 -08:00
|
|
|
enumerable: true,
|
|
|
|
configurable: false,
|
2016-05-14 11:41:34 -04:00
|
|
|
get () {
|
2016-03-24 13:15:04 -07:00
|
|
|
return this.webContents.devToolsWebContents
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2016-03-24 13:15:04 -07:00
|
|
|
})
|
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2018-04-17 16:53:55 +09: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 11:41:34 -04:00
|
|
|
BrowserWindow.getFocusedWindow = () => {
|
2018-10-02 03:56:31 +02:00
|
|
|
for (const window of BrowserWindow.getAllWindows()) {
|
2018-04-06 06:21:32 -07:00
|
|
|
if (window.isFocused() || window.isDevToolsFocused()) return window
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2016-05-14 12:26:53 -04:00
|
|
|
return null
|
2016-03-24 13:15:04 -07:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-05-14 11:41:34 -04:00
|
|
|
BrowserWindow.fromWebContents = (webContents) => {
|
2016-12-05 16:18:53 -08:00
|
|
|
for (const window of BrowserWindow.getAllWindows()) {
|
2016-05-14 11:41:34 -04:00
|
|
|
if (window.webContents.equal(webContents)) return window
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2016-03-24 13:15:04 -07:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2017-11-22 14:36:54 -08:00
|
|
|
BrowserWindow.fromBrowserView = (browserView) => {
|
|
|
|
for (const window of BrowserWindow.getAllWindows()) {
|
|
|
|
if (window.getBrowserView() === browserView) return window
|
|
|
|
}
|
2017-11-22 15:48:11 -08:00
|
|
|
|
|
|
|
return null
|
2017-11-22 14:36:54 -08:00
|
|
|
}
|
|
|
|
|
2016-05-14 11:41:34 -04:00
|
|
|
BrowserWindow.fromDevToolsWebContents = (webContents) => {
|
2016-12-05 16:18:53 -08:00
|
|
|
for (const window of BrowserWindow.getAllWindows()) {
|
2018-09-14 02:10:51 +10:00
|
|
|
const { devToolsWebContents } = window
|
2016-12-05 16:18:53 -08:00
|
|
|
if (devToolsWebContents != null && devToolsWebContents.equal(webContents)) {
|
|
|
|
return window
|
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2016-03-24 13:15:04 -07:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Helpers.
|
2016-05-14 11:41:34 -04:00
|
|
|
Object.assign(BrowserWindow.prototype, {
|
2016-05-14 12:26:53 -04:00
|
|
|
loadURL (...args) {
|
2016-12-01 14:37:03 -08:00
|
|
|
return this.webContents.loadURL(...args)
|
2016-05-14 11:41:34 -04:00
|
|
|
},
|
2016-05-14 12:26:53 -04:00
|
|
|
getURL (...args) {
|
2016-05-14 11:41:34 -04:00
|
|
|
return this.webContents.getURL()
|
|
|
|
},
|
2018-09-11 09:56:49 +02:00
|
|
|
loadFile (...args) {
|
|
|
|
return this.webContents.loadFile(...args)
|
2018-01-04 11:38:56 +13:00
|
|
|
},
|
2016-05-14 12:26:53 -04:00
|
|
|
reload (...args) {
|
2016-12-01 14:37:03 -08:00
|
|
|
return this.webContents.reload(...args)
|
2016-05-14 11:41:34 -04:00
|
|
|
},
|
2016-05-14 12:26:53 -04:00
|
|
|
send (...args) {
|
2016-12-01 14:37:03 -08:00
|
|
|
return this.webContents.send(...args)
|
2016-05-14 11:41:34 -04:00
|
|
|
},
|
2016-05-14 12:26:53 -04:00
|
|
|
openDevTools (...args) {
|
2016-12-01 14:37:03 -08:00
|
|
|
return this.webContents.openDevTools(...args)
|
2016-05-14 11:41:34 -04:00
|
|
|
},
|
|
|
|
closeDevTools () {
|
|
|
|
return this.webContents.closeDevTools()
|
|
|
|
},
|
|
|
|
isDevToolsOpened () {
|
|
|
|
return this.webContents.isDevToolsOpened()
|
|
|
|
},
|
|
|
|
isDevToolsFocused () {
|
|
|
|
return this.webContents.isDevToolsFocused()
|
|
|
|
},
|
|
|
|
toggleDevTools () {
|
|
|
|
return this.webContents.toggleDevTools()
|
|
|
|
},
|
2016-05-14 12:26:53 -04:00
|
|
|
inspectElement (...args) {
|
2016-12-01 14:37:03 -08:00
|
|
|
return this.webContents.inspectElement(...args)
|
2016-05-14 11:41:34 -04:00
|
|
|
},
|
|
|
|
inspectServiceWorker () {
|
|
|
|
return this.webContents.inspectServiceWorker()
|
2016-06-08 10:19:28 -07:00
|
|
|
},
|
|
|
|
showDefinitionForSelection () {
|
|
|
|
return this.webContents.showDefinitionForSelection()
|
2016-07-05 15:43:57 -07:00
|
|
|
},
|
|
|
|
capturePage (...args) {
|
2016-12-01 14:37:03 -08:00
|
|
|
return this.webContents.capturePage(...args)
|
2017-03-01 10:55:28 -08:00
|
|
|
},
|
|
|
|
setTouchBar (touchBar) {
|
2017-03-01 11:12:22 -08:00
|
|
|
electron.TouchBar._setOnWindow(touchBar, this)
|
2018-10-18 17:32:22 -07:00
|
|
|
},
|
|
|
|
setBackgroundThrottling (allowed) {
|
|
|
|
this.webContents.setBackgroundThrottling(allowed)
|
2017-03-01 10:55:28 -08:00
|
|
|
}
|
|
|
|
})
|
2016-12-16 17:24:51 +11:00
|
|
|
|
2016-03-24 13:15:04 -07:00
|
|
|
module.exports = BrowserWindow
|