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-04-25 08:23:40 +00:00
|
|
|
wrapWebContents = require('web-contents').wrap
|
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-04-25 03:51:05 +00:00
|
|
|
@webContents = @getWebContents()
|
2014-04-25 03:59:33 +00:00
|
|
|
@webContents.once 'destroyed', => @webContents = null
|
2014-04-25 03:51:05 +00:00
|
|
|
|
|
|
|
# Define getter for devToolsWebContents.
|
2014-04-25 03:22:51 +00:00
|
|
|
devToolsWebContents = null
|
2014-04-25 02:48:11 +00:00
|
|
|
@__defineGetter__ 'devToolsWebContents', ->
|
|
|
|
if @isDevToolsOpened()
|
2014-04-25 03:51:05 +00:00
|
|
|
# Get a new devToolsWebContents if previous one has been destroyed, it
|
2014-04-25 03:22:51 +00:00
|
|
|
# could happen when the devtools has been closed and then reopened.
|
|
|
|
devToolsWebContents = null unless devToolsWebContents?.isAlive()
|
|
|
|
devToolsWebContents ?= @getDevToolsWebContents()
|
2014-04-25 02:48:11 +00:00
|
|
|
else
|
2014-04-25 03:22:51 +00:00
|
|
|
devToolsWebContents = null
|
2014-04-24 09:00:41 +00:00
|
|
|
|
2013-12-26 10:41:21 +00:00
|
|
|
# Remember the window.
|
|
|
|
id = BrowserWindow.windows.add this
|
|
|
|
|
|
|
|
# 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-04-23 00:50:33 +00:00
|
|
|
@once 'closed', ->
|
2013-12-26 10:41:21 +00:00
|
|
|
BrowserWindow.windows.remove id if BrowserWindow.windows.has id
|
|
|
|
|
2013-12-06 06:44:25 +00:00
|
|
|
# Tell the rpc server that a render view has been deleted and we need to
|
|
|
|
# release all objects owned by it.
|
2014-04-25 03:51:05 +00:00
|
|
|
@webContents.on 'render-view-deleted', (event, processId, routingId) ->
|
2014-04-25 08:45:14 +00:00
|
|
|
process.emit 'ATOM_BROWSER_RELEASE_RENDER_VIEW', "#{processId}-#{routingId}"
|
2013-12-06 06:44:25 +00:00
|
|
|
|
2013-06-18 13:40:03 +00:00
|
|
|
BrowserWindow::toggleDevTools = ->
|
2013-11-05 02:32:45 +00:00
|
|
|
if @isDevToolsOpened() then @closeDevTools() else @openDevTools()
|
2013-05-15 11:19:35 +00:00
|
|
|
|
2014-04-25 03:51:05 +00:00
|
|
|
BrowserWindow::getWebContents = ->
|
2014-04-25 08:13:16 +00:00
|
|
|
wrapWebContents @_getWebContents()
|
2014-04-25 03:51:05 +00:00
|
|
|
|
|
|
|
BrowserWindow::getDevToolsWebContents = ->
|
2014-04-25 08:13:16 +00:00
|
|
|
wrapWebContents @_getDevToolsWebContents()
|
2014-04-25 03:51:05 +00:00
|
|
|
|
2013-10-02 13:24:21 +00:00
|
|
|
BrowserWindow::setMenu = (menu) ->
|
2014-02-19 13:06:45 +00:00
|
|
|
if process.platform is 'darwin'
|
|
|
|
throw new Error('BrowserWindow.setMenu is not available on OS X')
|
2013-10-02 13:24:21 +00:00
|
|
|
|
|
|
|
throw new TypeError('Invalid menu') unless menu?.constructor?.name is 'Menu'
|
|
|
|
|
|
|
|
@menu = menu # Keep a reference of menu in case of GC.
|
|
|
|
@menu.attachToWindow this
|
|
|
|
|
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()
|
2014-04-25 08:45:14 +00:00
|
|
|
return window for window in windows when webContents.equal window.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()
|
2014-04-25 08:45:14 +00:00
|
|
|
return window for window in windows when webContents.equal window.devToolsWebContents
|
2014-04-04 14:28:18 +00:00
|
|
|
|
2014-04-25 05:14:11 +00:00
|
|
|
# Helpers.
|
|
|
|
BrowserWindow::loadUrl = -> @webContents.loadUrl.apply @webContents, arguments
|
|
|
|
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-04-25 04:52:30 +00:00
|
|
|
BrowserWindow::reload = -> @webContents.reload()
|
|
|
|
BrowserWindow::reloadIgnoringCache = -> @webContents.reloadIgnoringCache()
|
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::getRoutingId = -> @webContents.getRoutingId()
|
|
|
|
BrowserWindow::getProcessId = -> @webContents.getProcessId()
|
|
|
|
BrowserWindow::isCrashed = -> @webContents.isCrashed()
|
2014-04-25 02:48:11 +00:00
|
|
|
BrowserWindow::executeJavaScriptInDevTools = (code) ->
|
|
|
|
@devToolsWebContents.executeJavaScript code
|
2014-04-24 09:00:41 +00:00
|
|
|
|
2013-06-18 13:40:03 +00:00
|
|
|
module.exports = BrowserWindow
|