2013-05-15 11:19:35 +00:00
|
|
|
EventEmitter = require('events').EventEmitter
|
2013-12-26 10:41:21 +00:00
|
|
|
IDWeakMap = require 'id-weak-map'
|
2013-10-05 05:13:04 +00:00
|
|
|
app = require 'app'
|
2014-10-27 15:46:25 +00:00
|
|
|
ipc = require 'ipc'
|
2013-05-15 11:19:35 +00:00
|
|
|
|
2013-06-18 13:40:03 +00:00
|
|
|
BrowserWindow = process.atomBinding('window').BrowserWindow
|
|
|
|
BrowserWindow::__proto__ = EventEmitter.prototype
|
2013-05-17 08:09:12 +00:00
|
|
|
|
2013-12-26 10:41:21 +00:00
|
|
|
# Store all created windows in the weak map.
|
|
|
|
BrowserWindow.windows = new IDWeakMap
|
|
|
|
|
2013-10-05 13:05:59 +00:00
|
|
|
BrowserWindow::_init = ->
|
|
|
|
# Simulate the application menu on platforms other than OS X.
|
|
|
|
if process.platform isnt 'darwin'
|
|
|
|
menu = app.getApplicationMenu()
|
|
|
|
@setMenu menu if menu?
|
|
|
|
|
2014-05-22 01:51:32 +00:00
|
|
|
# Remember the window ID.
|
|
|
|
Object.defineProperty this, 'id',
|
|
|
|
value: BrowserWindow.windows.add(this)
|
|
|
|
enumerable: true
|
2013-12-26 10:41:21 +00:00
|
|
|
|
2014-10-27 15:46:25 +00:00
|
|
|
# Make new windows requested by links behave like "window.open"
|
2014-11-04 10:12:57 +00:00
|
|
|
@on '-new-window', (event, url, frameName) =>
|
2014-10-27 15:46:25 +00:00
|
|
|
event.sender = @webContents
|
|
|
|
options = show: true, width: 800, height: 600
|
|
|
|
ipc.emit 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', event, url, frameName, options
|
2014-10-27 10:52:55 +00:00
|
|
|
|
2014-12-17 22:56:51 +00:00
|
|
|
# Redirect "will-navigate" to webContents.
|
|
|
|
@on '-will-navigate', (event, url) =>
|
|
|
|
@webContents.emit 'will-navigate', event, url
|
|
|
|
|
2015-06-18 05:59:08 +00:00
|
|
|
# Redirect focus/blur event to app instance too.
|
|
|
|
@on 'blur', (event) =>
|
|
|
|
app.emit 'browser-window-blur', event, this
|
|
|
|
@on 'focus', (event) =>
|
|
|
|
app.emit 'browser-window-focus', event, this
|
|
|
|
|
2013-12-26 10:41:21 +00:00
|
|
|
# Remove the window from weak map immediately when it's destroyed, since we
|
2014-05-07 06:34:53 +00:00
|
|
|
# could be iterating windows before GC happened.
|
2014-05-22 01:51:32 +00:00
|
|
|
@once 'closed', =>
|
|
|
|
BrowserWindow.windows.remove @id if BrowserWindow.windows.has @id
|
2013-12-26 10:41:21 +00:00
|
|
|
|
2013-10-02 13:24:21 +00:00
|
|
|
BrowserWindow::setMenu = (menu) ->
|
2015-06-04 07:14:43 +00:00
|
|
|
throw new TypeError('Invalid menu') unless menu is null or menu?.constructor?.name is 'Menu'
|
2013-10-02 13:24:21 +00:00
|
|
|
|
|
|
|
@menu = menu # Keep a reference of menu in case of GC.
|
2015-06-04 07:32:33 +00:00
|
|
|
@_setMenu menu
|
2013-10-02 13:24:21 +00:00
|
|
|
|
2013-12-26 10:41:21 +00:00
|
|
|
BrowserWindow.getAllWindows = ->
|
|
|
|
windows = BrowserWindow.windows
|
|
|
|
windows.get key for key in windows.keys()
|
|
|
|
|
2013-06-18 13:40:03 +00:00
|
|
|
BrowserWindow.getFocusedWindow = ->
|
2013-12-26 10:41:21 +00:00
|
|
|
windows = BrowserWindow.getAllWindows()
|
2013-06-18 13:40:03 +00:00
|
|
|
return window for window in windows when window.isFocused()
|
2013-05-28 08:01:44 +00:00
|
|
|
|
2014-04-25 08:45:14 +00:00
|
|
|
BrowserWindow.fromWebContents = (webContents) ->
|
2013-12-26 10:41:21 +00:00
|
|
|
windows = BrowserWindow.getAllWindows()
|
2015-06-03 10:48:10 +00:00
|
|
|
return window for window in windows when window.webContents?.equal webContents
|
2013-05-16 15:00:43 +00:00
|
|
|
|
2014-04-25 08:45:14 +00:00
|
|
|
BrowserWindow.fromDevToolsWebContents = (webContents) ->
|
2014-04-04 14:28:18 +00:00
|
|
|
windows = BrowserWindow.getAllWindows()
|
2015-06-03 10:48:10 +00:00
|
|
|
return window for window in windows when window.devToolsWebContents?.equal webContents
|
2014-04-04 14:28:18 +00:00
|
|
|
|
2014-05-22 01:51:32 +00:00
|
|
|
BrowserWindow.fromId = (id) ->
|
2015-06-18 05:59:08 +00:00
|
|
|
BrowserWindow.windows.get id if BrowserWindow.windows.has id
|
2014-05-22 01:51:32 +00:00
|
|
|
|
2014-04-25 05:14:11 +00:00
|
|
|
# Helpers.
|
2014-11-12 02:28:50 +00:00
|
|
|
BrowserWindow::loadUrl = -> @webContents.loadUrl.apply @webContents, arguments
|
2014-04-25 05:14:11 +00:00
|
|
|
BrowserWindow::send = -> @webContents.send.apply @webContents, arguments
|
|
|
|
|
2014-04-24 09:00:41 +00:00
|
|
|
# Be compatible with old API.
|
2014-04-29 07:35:12 +00:00
|
|
|
BrowserWindow::restart = -> @webContents.reload()
|
2014-04-24 09:00:41 +00:00
|
|
|
BrowserWindow::getUrl = -> @webContents.getUrl()
|
2014-11-12 02:28:50 +00:00
|
|
|
BrowserWindow::reload = -> @webContents.reload.apply @webContents, arguments
|
|
|
|
BrowserWindow::reloadIgnoringCache = -> @webContents.reloadIgnoringCache.apply @webContents, arguments
|
2014-04-24 09:00:41 +00:00
|
|
|
BrowserWindow::getPageTitle = -> @webContents.getTitle()
|
|
|
|
BrowserWindow::isLoading = -> @webContents.isLoading()
|
|
|
|
BrowserWindow::isWaitingForResponse = -> @webContents.isWaitingForResponse()
|
|
|
|
BrowserWindow::stop = -> @webContents.stop()
|
|
|
|
BrowserWindow::isCrashed = -> @webContents.isCrashed()
|
2015-06-05 09:01:17 +00:00
|
|
|
BrowserWindow::executeJavaScriptInDevTools = (code) -> @devToolsWebContents?.executeJavaScript code
|
|
|
|
BrowserWindow::openDevTools = -> @webContents.openDevTools.apply @webContents, arguments
|
|
|
|
BrowserWindow::closeDevTools = -> @webContents.closeDevTools()
|
|
|
|
BrowserWindow::isDevToolsOpened = -> @webContents.isDevToolsOpened()
|
|
|
|
BrowserWindow::toggleDevTools = -> @webContents.toggleDevTools()
|
|
|
|
BrowserWindow::inspectElement = -> @webContents.inspectElement.apply @webContents, arguments
|
|
|
|
BrowserWindow::inspectServiceWorker = -> @webContents.inspectServiceWorker()
|
2015-06-13 13:23:45 +00:00
|
|
|
BrowserWindow::print = -> @webContents.print.apply @webContents, arguments
|
2015-06-13 13:39:06 +00:00
|
|
|
BrowserWindow::printToPDF = -> @webContents.printToPDF.apply @webContents, arguments
|
2014-04-24 09:00:41 +00:00
|
|
|
|
2013-06-18 13:40:03 +00:00
|
|
|
module.exports = BrowserWindow
|