2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
const ipcMain = require('electron').ipcMain
|
|
|
|
const deprecate = require('electron').deprecate
|
|
|
|
const EventEmitter = require('events').EventEmitter
|
|
|
|
const {BrowserWindow, _setDeprecatedOptionsCheck} = process.atomBinding('window')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.__proto__ = EventEmitter.prototype
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype._init = function () {
|
2016-01-14 18:35:29 +00:00
|
|
|
// avoid recursive require.
|
2016-03-24 20:15:04 +00:00
|
|
|
var app, menu
|
|
|
|
app = require('electron').app
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Simulate the application menu on platforms other than OS X.
|
2016-01-12 02:40:23 +00:00
|
|
|
if (process.platform !== 'darwin') {
|
2016-03-24 20:15:04 +00:00
|
|
|
menu = app.getApplicationMenu()
|
2016-01-12 02:40:23 +00:00
|
|
|
if (menu != null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
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-08 17:36:41 +00:00
|
|
|
this.webContents.on('-new-window', (event, url, frameName) => {
|
2016-03-24 20:15:04 +00:00
|
|
|
var options
|
2016-01-12 02:40:23 +00:00
|
|
|
options = {
|
|
|
|
show: true,
|
|
|
|
width: 800,
|
|
|
|
height: 600
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
|
|
|
return ipcMain.emit('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', event, url, frameName, options)
|
|
|
|
})
|
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
|
|
|
// Forward the crashed event.
|
2016-03-08 17:36:41 +00:00
|
|
|
this.webContents.on('crashed', () => {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.emit('crashed')
|
|
|
|
})
|
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.
|
|
|
|
if (this.isDestroyed())
|
2016-03-24 20:15:04 +00:00
|
|
|
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-01-30 04:20:28 +00:00
|
|
|
if (!event.defaultPrevented)
|
2016-03-24 20:15:04 +00:00
|
|
|
this.setTitle(title)
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:44:21 +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.
|
|
|
|
// Though this hack is only needed on OS X when the app is launched from
|
|
|
|
// Finder, we still do it on all platforms in case of other bugs we don't 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-03-07 09:26:36 +00:00
|
|
|
// Evented visibilityState changes
|
2016-03-08 17:36:41 +00:00
|
|
|
this.on('show', () => {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized())
|
|
|
|
})
|
2016-03-08 17:36:41 +00:00
|
|
|
this.on('hide', () => {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized())
|
|
|
|
})
|
2016-03-08 17:36:41 +00:00
|
|
|
this.on('minimize', () => {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized())
|
|
|
|
})
|
2016-03-08 17:36:41 +00:00
|
|
|
this.on('restore', () => {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), this.isMinimized())
|
|
|
|
})
|
2016-03-07 09:26:36 +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-01-14 18:35:29 +00:00
|
|
|
// Be compatible with old APIs.
|
2016-03-08 17:36:41 +00:00
|
|
|
this.webContents.on('devtools-focused', () => {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.emit('devtools-focused')
|
|
|
|
})
|
2016-03-08 17:36:41 +00:00
|
|
|
this.webContents.on('devtools-opened', () => {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.emit('devtools-opened')
|
|
|
|
})
|
2016-03-08 17:36:41 +00:00
|
|
|
this.webContents.on('devtools-closed', () => {
|
2016-03-24 20:15:04 +00:00
|
|
|
this.emit('devtools-closed')
|
|
|
|
})
|
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-03-24 20:15:04 +00:00
|
|
|
get: function () {
|
|
|
|
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-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.getFocusedWindow = function () {
|
|
|
|
var i, len, window, windows
|
|
|
|
windows = BrowserWindow.getAllWindows()
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = 0, len = windows.length; i < len; i++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
window = windows[i]
|
2016-01-12 02:40:23 +00:00
|
|
|
if (window.isFocused()) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return window
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
return null
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.fromWebContents = function (webContents) {
|
|
|
|
var i, len, ref1, window, windows
|
|
|
|
windows = BrowserWindow.getAllWindows()
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = 0, len = windows.length; i < len; i++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
window = windows[i]
|
2016-01-12 02:40:23 +00:00
|
|
|
if ((ref1 = window.webContents) != null ? ref1.equal(webContents) : void 0) {
|
2016-03-24 20:15:04 +00:00
|
|
|
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-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.fromDevToolsWebContents = function (webContents) {
|
|
|
|
var i, len, ref1, window, windows
|
|
|
|
windows = BrowserWindow.getAllWindows()
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = 0, len = windows.length; i < len; i++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
window = windows[i]
|
2016-01-12 02:40:23 +00:00
|
|
|
if ((ref1 = window.devToolsWebContents) != null ? ref1.equal(webContents) : void 0) {
|
2016-03-24 20:15:04 +00:00
|
|
|
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-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.loadURL = function () {
|
|
|
|
return this.webContents.loadURL.apply(this.webContents, arguments)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.getURL = function () {
|
|
|
|
return this.webContents.getURL()
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.reload = function () {
|
|
|
|
return this.webContents.reload.apply(this.webContents, arguments)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.send = function () {
|
|
|
|
return this.webContents.send.apply(this.webContents, arguments)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.openDevTools = function () {
|
|
|
|
return this.webContents.openDevTools.apply(this.webContents, arguments)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.closeDevTools = function () {
|
|
|
|
return this.webContents.closeDevTools()
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.isDevToolsOpened = function () {
|
|
|
|
return this.webContents.isDevToolsOpened()
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.isDevToolsFocused = function () {
|
|
|
|
return this.webContents.isDevToolsFocused()
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.toggleDevTools = function () {
|
|
|
|
return this.webContents.toggleDevTools()
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.inspectElement = function () {
|
|
|
|
return this.webContents.inspectElement.apply(this.webContents, arguments)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.inspectServiceWorker = function () {
|
|
|
|
return this.webContents.inspectServiceWorker()
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Deprecated.
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'undo', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'redo', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'cut', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'copy', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'paste', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'selectAll', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'reloadIgnoringCache', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'isLoading', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'isWaitingForResponse', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'stop', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'isCrashed', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'print', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.member(BrowserWindow, 'printToPDF', 'webContents')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.rename(BrowserWindow, 'restart', 'reload')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.rename(BrowserWindow, 'loadUrl', 'loadURL')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.rename(BrowserWindow, 'getUrl', 'getURL')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.executeJavaScriptInDevTools = deprecate('executeJavaScriptInDevTools', 'devToolsWebContents.executeJavaScript', function (code) {
|
|
|
|
var ref1
|
|
|
|
return (ref1 = this.devToolsWebContents) != null ? ref1.executeJavaScript(code) : void 0
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
BrowserWindow.prototype.getPageTitle = deprecate('getPageTitle', 'webContents.getTitle', function () {
|
|
|
|
var ref1
|
|
|
|
return (ref1 = this.webContents) != null ? ref1.getTitle() : void 0
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
const isDeprecatedKey = function (key) {
|
|
|
|
return key.indexOf('-') >= 0
|
|
|
|
}
|
2016-03-16 16:38:03 +00:00
|
|
|
|
|
|
|
// Map deprecated key with hyphens to camel case key
|
2016-03-24 20:15:04 +00:00
|
|
|
const getNonDeprecatedKey = function (deprecatedKey) {
|
|
|
|
return deprecatedKey.replace(/-./g, function (match) {
|
|
|
|
return match[1].toUpperCase()
|
|
|
|
})
|
|
|
|
}
|
2016-03-16 16:38:03 +00:00
|
|
|
|
|
|
|
// TODO Remove for 1.0
|
2016-03-24 20:15:04 +00:00
|
|
|
const checkForDeprecatedOptions = function (options) {
|
|
|
|
if (!options) return ''
|
2016-03-16 16:38:03 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
let keysToCheck = Object.keys(options)
|
2016-03-16 16:38:03 +00:00
|
|
|
if (options.webPreferences) {
|
2016-03-24 20:15:04 +00:00
|
|
|
keysToCheck = keysToCheck.concat(Object.keys(options.webPreferences))
|
2016-03-16 16:38:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 22:03:53 +00:00
|
|
|
// Check options for keys with hyphens in them
|
2016-03-24 20:15:04 +00:00
|
|
|
let deprecatedKey = keysToCheck.filter(isDeprecatedKey)[0]
|
2016-03-16 16:38:03 +00:00
|
|
|
if (deprecatedKey) {
|
|
|
|
try {
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.warn(deprecatedKey, getNonDeprecatedKey(deprecatedKey))
|
2016-03-16 16:38:03 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Return error message so it can be rethrown via C++
|
2016-03-24 20:15:04 +00:00
|
|
|
return error.message
|
2016-03-16 16:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
let webPreferenceOption
|
2016-03-16 16:38:03 +00:00
|
|
|
if (options.hasOwnProperty('nodeIntegration')) {
|
2016-03-24 20:15:04 +00:00
|
|
|
webPreferenceOption = 'nodeIntegration'
|
2016-03-16 16:38:03 +00:00
|
|
|
} else if (options.hasOwnProperty('preload')) {
|
2016-03-24 20:15:04 +00:00
|
|
|
webPreferenceOption = 'preload'
|
2016-03-16 16:38:03 +00:00
|
|
|
} else if (options.hasOwnProperty('zoomFactor')) {
|
2016-03-24 20:15:04 +00:00
|
|
|
webPreferenceOption = 'zoomFactor'
|
2016-03-16 16:38:03 +00:00
|
|
|
}
|
|
|
|
if (webPreferenceOption) {
|
|
|
|
try {
|
2016-03-24 20:15:04 +00:00
|
|
|
deprecate.warn(`options.${webPreferenceOption}`, `options.webPreferences.${webPreferenceOption}`)
|
2016-03-16 16:38:03 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Return error message so it can be rethrown via C++
|
2016-03-24 20:15:04 +00:00
|
|
|
return error.message
|
2016-03-16 16:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
_setDeprecatedOptionsCheck(checkForDeprecatedOptions)
|
2016-03-16 16:38:03 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
module.exports = BrowserWindow
|