Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
add extension chrome.windows.onFocusChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlawler committed Apr 10, 2017
1 parent 8ff5b78 commit 632c6de
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
52 changes: 52 additions & 0 deletions atom/common/api/resources/windows_bindings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var ipc = require('ipc_utils')

var id = 1;
var focusId = 1;

var binding = {
getCurrent: function () {
Expand Down Expand Up @@ -46,6 +47,57 @@ var binding = {
ipc.send('chrome-windows-update', responseId, windowId, updateInfo)
},

get: function (windowId, getInfo, cb) {
console.warn('chrome.windows.get is not supported yet')
},

getLastFocused: function (getInfo, cb) {
console.warn('chrome.windows.getLastFocused is not supported yet')
},

remove: function (windowId, cb) {
console.warn('chrome.windows.remove is not supported yet')
},

onCreated: {

addListener: function (callback) {
console.warn('chrome.windows.onCreated is not supported yet')
},

Filters: ['app', 'normal', 'panel', 'popup']
},

onRemoved: {

addListener: function (callback) {
console.warn('chrome.windows.onRemoved is not supported yet')
},

Filters: ['app', 'normal', 'panel', 'popup']
},

onFocusChanged: {

addListener: function (cb) {

if(cb.length != 1) {
console.warn('Callback has wrong number of arguments.')
}

var responseId = ++focusId

ipc.on('chrome-windows-focused-window-update-response-' + responseId, function (evt, responseId) {
cb(responseId);
})

ipc.send('chrome-windows-focused-window-update', responseId)

},

Filters: ['app', 'normal', 'panel', 'popup']
},

WINDOW_ID_NONE: -1,
WINDOW_ID_CURRENT: -2
};
Expand Down
42 changes: 42 additions & 0 deletions lib/browser/api/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,48 @@ ipcMain.on('chrome-windows-get-all', function (evt, responseId, getInfo) {
response)
})



ipcMain.on('chrome-windows-focused-window-update', function (evt, responseId) {

//There are two cases here:
//1. A new window gets focus (focus event)
//2. All the windows blur (blur event, no focus event)
//NB. Created or closed windows won't trigger here - presumably they use onCreated/onRemoved

app.on('browser-window-focus', function (event, browserWindow) {

var forthcomingWindow = browserWindow
var forthcomingId = (null == forthcomingWindow) ? -1 : forthcomingWindow.id

evt.sender.send('chrome-windows-focused-window-update-response-' + responseId, forthcomingId)
})


app.on('browser-window-blur', function (event, browserWindow) {

//see https://github.com/electron/electron/issues/4942
setImmediate(function() {

var forthcomingWindow = BrowserWindow.getFocusedWindow()

var forthcomingId = (null == forthcomingWindow) ? -1 : forthcomingWindow.id
var previousId = browserWindow.id

var allWindowsBlurred = (-1 == forthcomingId && -1 != previousId)
if (allWindowsBlurred) {
var blurredResponse = -1
evt.sender.send('chrome-windows-focused-window-update-response-' + responseId, blurredResponse)
}

});
})

})




ipcMain.on('chrome-windows-update', function (evt, responseId, windowId, updateInfo) {
var response = updateWindow(windowId, updateInfo)
evt.sender.send('chrome-windows-update-response-' + responseId, response)
Expand Down

0 comments on commit 632c6de

Please sign in to comment.