2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2016-05-14 15:41:34 +00:00
|
|
|
const {ipcMain} = require('electron')
|
|
|
|
const {EventEmitter} = require('events')
|
2016-04-28 16:42:06 +00:00
|
|
|
const {BrowserWindow} = process.atomBinding('window')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-29 00:51:11 +00:00
|
|
|
Object.setPrototypeOf(BrowserWindow.prototype, EventEmitter.prototype)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype._init = function () {
|
2016-05-14 15:41:34 +00:00
|
|
|
// Avoid recursive require.
|
|
|
|
const {app} = require('electron')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
// Simulate the application menu on platforms other than macOS.
|
2016-01-12 02:40:23 +00:00
|
|
|
if (process.platform !== 'darwin') {
|
2016-05-14 15:41:34 +00:00
|
|
|
const menu = app.getApplicationMenu()
|
|
|
|
if (menu) this.setMenu(menu)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Make new windows requested by links behave like "window.open"
|
2016-03-25 03:17:33 +00:00
|
|
|
this.webContents.on('-new-window', (event, url, frameName, disposition) => {
|
2016-05-14 15:41:34 +00:00
|
|
|
const options = {
|
2016-01-12 02:40:23 +00:00
|
|
|
show: true,
|
|
|
|
width: 800,
|
|
|
|
height: 600
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-05-19 22:28:08 +00:00
|
|
|
ipcMain.emit('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', event, url, frameName, disposition, options)
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:44:21 +00:00
|
|
|
// window.resizeTo(...)
|
|
|
|
// window.moveTo(...)
|
2016-03-08 19:11:17 +00:00
|
|
|
this.webContents.on('move', (event, size) => {
|
2016-03-24 20:15:04 +00:00
|
|
|
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.
|
2016-03-08 17:36:41 +00:00
|
|
|
this.webContents.on('activate', () => {
|
|
|
|
if (process.platform !== 'darwin' && this.isMenuBarAutoHide() && this.isMenuBarVisible()) {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.setMenuBarVisibility(false)
|
2016-03-08 17:36:41 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Change window title to page title.
|
2016-01-30 04:20:28 +00:00
|
|
|
this.webContents.on('page-title-updated', (event, title) => {
|
|
|
|
// The page-title-updated event is not emitted immediately (see #3645), so
|
|
|
|
// when the callback is called the BrowserWindow might have been closed.
|
2016-03-29 01:00:30 +00:00
|
|
|
if (this.isDestroyed()) return
|
|
|
|
|
2016-01-30 04:20:28 +00:00
|
|
|
// Route the event to BrowserWindow.
|
2016-03-24 20:15:04 +00:00
|
|
|
this.emit('page-title-updated', event, title)
|
2016-03-29 01:00:30 +00:00
|
|
|
if (!event.defaultPrevented) this.setTitle(title)
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
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
|
|
|
|
this.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', isVisible ? 'visible' : 'hidden')
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 16:26:53 +00:00
|
|
|
|
2016-05-14 15:41:34 +00:00
|
|
|
const visibilityEvents = ['show', 'hide', 'minimize', 'maximize', 'restore']
|
|
|
|
for (let event of visibilityEvents) {
|
|
|
|
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.
|
2016-03-24 20:15:04 +00:00
|
|
|
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,
|
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
|
|
|
|
2016-05-14 15:41:34 +00:00
|
|
|
BrowserWindow.getFocusedWindow = () => {
|
|
|
|
for (let window of BrowserWindow.getAllWindows()) {
|
|
|
|
if (window.isFocused()) 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) => {
|
|
|
|
for (let window of BrowserWindow.getAllWindows()) {
|
|
|
|
if (window.webContents.equal(webContents)) return window
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
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.fromDevToolsWebContents = (webContents) => {
|
|
|
|
for (let window of BrowserWindow.getAllWindows()) {
|
|
|
|
if (window.devToolsWebContents.equal(webContents)) return window
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +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) {
|
|
|
|
return this.webContents.loadURL.apply(this.webContents, 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()
|
|
|
|
},
|
2016-05-14 16:26:53 +00:00
|
|
|
reload (...args) {
|
|
|
|
return this.webContents.reload.apply(this.webContents, args)
|
2016-05-14 15:41:34 +00:00
|
|
|
},
|
2016-05-14 16:26:53 +00:00
|
|
|
send (...args) {
|
|
|
|
return this.webContents.send.apply(this.webContents, args)
|
2016-05-14 15:41:34 +00:00
|
|
|
},
|
2016-05-14 16:26:53 +00:00
|
|
|
openDevTools (...args) {
|
|
|
|
return this.webContents.openDevTools.apply(this.webContents, 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) {
|
|
|
|
return this.webContents.inspectElement.apply(this.webContents, args)
|
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) {
|
|
|
|
return this.webContents.capturePage.apply(this.webContents, args)
|
2016-05-14 15:41:34 +00:00
|
|
|
}
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
module.exports = BrowserWindow
|