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

add extension chrome.windows.onFocusChanged #180

Merged
merged 3 commits into from
Apr 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 71 additions & 20 deletions atom/common/api/resources/windows_bindings.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,104 @@
var binding = require('binding').Binding.create('windows')

var ipc = require('ipc_utils')

var id = 1;

var binding = {
getCurrent: function () {
var cb, getInfo;
if (arguments.length == 1) {
binding.registerCustomHook(function (bindingsAPI, extensionId) {
var apiFunctions = bindingsAPI.apiFunctions
var windows = bindingsAPI.compiledApi

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

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

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

apiFunctions.setHandleRequest('create', function (createData, cb) {
console.warn('chrome.windows.create is not supported yet')
})

apiFunctions.setHandleRequest('getCurrent', function () {
var cb, getInfo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setHandleRequest is supposed to normalize these so you always get two arguments even if the user only supplies one. I think this code should still work ok, but the argument length condition is no longer necessary

if (arguments.length === 1) {
cb = arguments[0]
} else {
getInfo = arguments[0]
cb = arguments[1]
}

var responseId = ++id
ipc.once('chrome-windows-get-current-response-' + responseId, function(evt, win) {
ipc.once('chrome-windows-get-current-response-' + responseId, function (evt, win) {
cb(win)
})
ipc.send('chrome-windows-get-current', responseId, getInfo)
},
})

getAll: function (getInfo, cb) {
if (arguments.length == 1) {
apiFunctions.setHandleRequest('getAll', function (getInfo, cb) {
if (arguments.length === 1) {
cb = arguments[0]
} else {
getInfo = arguments[0]
cb = arguments[1]
}

var responseId = ++id
ipc.once('chrome-windows-get-all-response-' + responseId, function(evt, win) {
ipc.once('chrome-windows-get-all-response-' + responseId, function (evt, win) {
cb(win)
})
ipc.send('chrome-windows-get-all', responseId, getInfo)
},
})

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

update: function (windowId, updateInfo, cb) {
apiFunctions.setHandleRequest('update', function (windowId, updateInfo, cb) {
var responseId = ++id
cb && ipc.once('chrome-windows-update-response-' + responseId, function (evt, win) {
cb(win)
})
ipc.send('chrome-windows-update', responseId, windowId, updateInfo)
},
})

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

Filters: function () {
console.warn('chrome.windows.onCreated filters are not supported yet')
}
}

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

Filters: function () {
console.warn('chrome.windows.onRemoved filters are not supported yet')
}
}

windows.onFocusChanged = {
addListener: function (cb) {
ipc.send('register-chrome-window-focus', extensionId)
ipc.on('chrome-window-focus', function (evt, windowId) {
cb(windowId)
})
},

Filters: function () {
console.warn('chrome.windows.onFocusChanged filters are not supported yet')
}
}

windows.WINDOW_ID_NONE = -1
windows.WINDOW_ID_CURRENT = -2
})

WINDOW_ID_NONE: -1,
WINDOW_ID_CURRENT: -2
};
exports.$set('binding', binding.generate())

exports.binding = binding;
38 changes: 34 additions & 4 deletions lib/browser/api/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ var addBackgroundPageEvent = function (extensionId, event) {
}

var sendToBackgroundPages = function (extensionId, session, event) {
if (!backgroundPageEvents[event] || !session)
return
if (!backgroundPageEvents[event] || !session) { return }

var pages = []
if (extensionId === 'all') {
Expand All @@ -79,8 +78,10 @@ var sendToBackgroundPages = function (extensionId, session, event) {
var args = [].slice.call(arguments, 2)
pages.forEach(function (backgroundPage) {
try {
// only send to background pages in the same browser context
if (backgroundPage.session.equal(session)) {
var sendToAll = (typeof (session) === 'string' && session === 'all')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change makes sense from a code perspective, but sending to all is a very special case and I'm a little concerned that this will get used where it shouldn't be. Maybe we should at least add a comment here explaining?

// otherwise, only send to background pages in the same browser context

if (sendToAll || backgroundPage.session.equal(session)) {
backgroundPage.send.apply(backgroundPage, args)
}
} catch (e) {
Expand Down Expand Up @@ -480,6 +481,35 @@ tabEvents.forEach((event_name) => {
})
})

ipcMain.on('register-chrome-window-focus', function (evt, extensionId) {
addBackgroundPageEvent(extensionId, 'chrome-window-focus')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to merge, but please update indenting for 2 spaces

})

app.on('browser-window-focus', function (event, browserWindow) {
var forthcomingWindow = browserWindow
var forthcomingId = (forthcomingWindow == null) ? -1 : forthcomingWindow.id

// Only background pages have access to chrome.window, so this is enough
// We do need to send this to all sessions though
sendToBackgroundPages('all', 'all', 'chrome-window-focus', 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 = (forthcomingWindow == null) ? -1 : forthcomingWindow.id
var previousId = browserWindow.id

var allWindowsBlurred = (forthcomingId === -1 && previousId !== -1)
if (allWindowsBlurred) {
var blurredResponse = -1
sendToBackgroundPages('all', 'all', 'chrome-window-focus', blurredResponse)
}
})
})

// chrome.windows
var windowInfo = function (win, populateTabs) {
var bounds = win.getBounds()
Expand Down