From d49060d659cdbacf7f5908e4a3025e494ee4158d Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 22 May 2014 09:51:32 +0800 Subject: [PATCH] Add "id" property for BrowserWindow, fixes #218. --- atom/browser/api/lib/browser-window.coffee | 13 +++++++++---- spec/api-browser-window-spec.coffee | 4 ++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/atom/browser/api/lib/browser-window.coffee b/atom/browser/api/lib/browser-window.coffee index 475541bec4f4..bf90f4776473 100644 --- a/atom/browser/api/lib/browser-window.coffee +++ b/atom/browser/api/lib/browser-window.coffee @@ -18,13 +18,15 @@ BrowserWindow::_init = -> @webContents = @getWebContents() @webContents.once 'destroyed', => @webContents = null - # Remember the window. - id = BrowserWindow.windows.add this + # Remember the window ID. + Object.defineProperty this, 'id', + value: BrowserWindow.windows.add(this) + enumerable: true # Remove the window from weak map immediately when it's destroyed, since we # could be iterating windows before GC happened. - @once 'closed', -> - BrowserWindow.windows.remove id if BrowserWindow.windows.has id + @once 'closed', => + BrowserWindow.windows.remove @id if BrowserWindow.windows.has @id BrowserWindow::openDevTools = -> @_openDevTools() @@ -67,6 +69,9 @@ BrowserWindow.fromDevToolsWebContents = (webContents) -> windows = BrowserWindow.getAllWindows() return window for window in windows when webContents.equal window.devToolsWebContents +BrowserWindow.fromId = (id) -> + BrowserWindow.windows.get id + # Helpers. BrowserWindow::loadUrl = -> @webContents.loadUrl.apply @webContents, arguments BrowserWindow::send = -> @webContents.send.apply @webContents, arguments diff --git a/spec/api-browser-window-spec.coffee b/spec/api-browser-window-spec.coffee index b515a2a0bd3e..f9eb37d4d927 100644 --- a/spec/api-browser-window-spec.coffee +++ b/spec/api-browser-window-spec.coffee @@ -90,6 +90,10 @@ describe 'browser-window module', -> assert.equal after[0], size[0] assert.equal after[1], size[1] + describe 'BrowserWindow.fromId(id)', -> + it 'returns the window with id', -> + assert.equal w.id, BrowserWindow.fromId(w.id).id + describe '"use-content-size" option', -> it 'make window created with content size when used', -> w.destroy()