app: adding browser-window-focus/blur events
This commit is contained in:
parent
ad59393641
commit
2f36f5ca78
3 changed files with 40 additions and 1 deletions
|
@ -30,6 +30,12 @@ BrowserWindow::_init = ->
|
||||||
@on '-will-navigate', (event, url) =>
|
@on '-will-navigate', (event, url) =>
|
||||||
@webContents.emit 'will-navigate', event, url
|
@webContents.emit 'will-navigate', event, url
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
# Remove the window from weak map immediately when it's destroyed, since we
|
# Remove the window from weak map immediately when it's destroyed, since we
|
||||||
# could be iterating windows before GC happened.
|
# could be iterating windows before GC happened.
|
||||||
@once 'closed', =>
|
@once 'closed', =>
|
||||||
|
@ -58,7 +64,7 @@ BrowserWindow.fromDevToolsWebContents = (webContents) ->
|
||||||
return window for window in windows when window.devToolsWebContents?.equal webContents
|
return window for window in windows when window.devToolsWebContents?.equal webContents
|
||||||
|
|
||||||
BrowserWindow.fromId = (id) ->
|
BrowserWindow.fromId = (id) ->
|
||||||
BrowserWindow.windows.get id
|
BrowserWindow.windows.get id if BrowserWindow.windows.has id
|
||||||
|
|
||||||
# Helpers.
|
# Helpers.
|
||||||
BrowserWindow::loadUrl = -> @webContents.loadUrl.apply @webContents, arguments
|
BrowserWindow::loadUrl = -> @webContents.loadUrl.apply @webContents, arguments
|
||||||
|
|
|
@ -87,6 +87,20 @@ Emitted when the application is activated while there is no opened windows. It
|
||||||
usually happens when user has closed all of application's windows and then
|
usually happens when user has closed all of application's windows and then
|
||||||
click on the application's dock icon.
|
click on the application's dock icon.
|
||||||
|
|
||||||
|
## Event: browser-window-blur
|
||||||
|
|
||||||
|
* `event` Event
|
||||||
|
* `window` BrowserWindow
|
||||||
|
|
||||||
|
Emitted when a [browserWindow](browser-window.md) gets blurred.
|
||||||
|
|
||||||
|
## Event: browser-window-focus
|
||||||
|
|
||||||
|
* `event` Event
|
||||||
|
* `window` BrowserWindow
|
||||||
|
|
||||||
|
Emitted when a [browserWindow](browser-window.md) gets focused.
|
||||||
|
|
||||||
## app.quit()
|
## app.quit()
|
||||||
|
|
||||||
Try to close all windows. The `before-quit` event will first be emitted. If all
|
Try to close all windows. The `before-quit` event will first be emitted. If all
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
assert = require 'assert'
|
assert = require 'assert'
|
||||||
app = require('remote').require 'app'
|
app = require('remote').require 'app'
|
||||||
|
remote = require 'remote'
|
||||||
|
BrowserWindow = remote.require 'browser-window'
|
||||||
|
|
||||||
describe 'app module', ->
|
describe 'app module', ->
|
||||||
describe 'app.getVersion()', ->
|
describe 'app.getVersion()', ->
|
||||||
|
@ -23,3 +25,20 @@ describe 'app module', ->
|
||||||
app.setName 'test-name'
|
app.setName 'test-name'
|
||||||
assert.equal app.getName(), 'test-name'
|
assert.equal app.getName(), 'test-name'
|
||||||
app.setName 'Electron Test'
|
app.setName 'Electron Test'
|
||||||
|
|
||||||
|
describe 'focus/blur event', ->
|
||||||
|
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) ->
|
||||||
|
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.hide()
|
||||||
|
w.show()
|
||||||
|
|
Loading…
Reference in a new issue