diff --git a/atom/browser/api/lib/browser-window.coffee b/atom/browser/api/lib/browser-window.coffee index 283e99641f47..2a92cfc55f88 100644 --- a/atom/browser/api/lib/browser-window.coffee +++ b/atom/browser/api/lib/browser-window.coffee @@ -45,6 +45,9 @@ BrowserWindow::_init = -> @on 'focus', (event) => app.emit 'browser-window-focus', event, this + # Notify the creation of the window. + app.emit 'browser-window-created', {}, this + BrowserWindow.getFocusedWindow = -> windows = BrowserWindow.getAllWindows() return window for window in windows when window.isFocused() diff --git a/docs/api/app.md b/docs/api/app.md index 42a5dfc24249..799db98882fe 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -118,6 +118,15 @@ Returns: Emitted when a [browserWindow](browser-window.md) gets focused. +### Event: 'browser-window-created' + +Returns: + +* `event` Event +* `window` BrowserWindow + +Emitted when a new [browserWindow](browser-window.md) is created. + ### Event: 'select-certificate' Emitted when a client certificate is requested. diff --git a/spec/api-app-spec.coffee b/spec/api-app-spec.coffee index 5c6591cadf50..38ef22ac80bd 100644 --- a/spec/api-app-spec.coffee +++ b/spec/api-app-spec.coffee @@ -26,19 +26,30 @@ describe 'app module', -> assert.equal app.getName(), 'test-name' app.setName 'Electron Test' - describe 'focus/blur event', -> + describe 'BrowserWindow events', -> w = null - beforeEach -> - w.destroy() if w? - w = new BrowserWindow(show: false, width: 400, height: 400) afterEach -> w.destroy() if w? w = null - it 'should emit focus event', (done) -> + + it 'should emit browser-window-focus event when window is focused', (done) -> + app.once 'browser-window-focus', (e, window) -> + assert.equal w.id, window.id + done() + w = new BrowserWindow(show: false) + w.emit 'focus' + + it 'should emit browser-window-blur event when window is blured', (done) -> app.once 'browser-window-blur', (e, window) -> assert.equal w.id, window.id done() - app.once 'browser-window-focus', (e, window) -> - assert.equal w.id, window.id - w.emit 'blur' - w.emit 'focus' + w = new BrowserWindow(show: false) + w.emit 'blur' + + it 'should emit browser-window-created event when window is created', (done) -> + app.once 'browser-window-created', (e, window) -> + setImmediate -> + assert.equal w.id, window.id + done() + w = new BrowserWindow(show: false) + w.emit 'blur'