This repository has been archived by the owner on Jan 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
add extension chrome.windows.onFocusChanged #180
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') { | ||
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this change makes sense from a code perspective, but sending to |
||
// otherwise, only send to background pages in the same browser context | ||
|
||
if (sendToAll || backgroundPage.session.equal(session)) { | ||
backgroundPage.send.apply(backgroundPage, args) | ||
} | ||
} catch (e) { | ||
|
@@ -480,6 +481,35 @@ tabEvents.forEach((event_name) => { | |
}) | ||
}) | ||
|
||
ipcMain.on('register-chrome-window-focus', function (evt, extensionId) { | ||
addBackgroundPageEvent(extensionId, 'chrome-window-focus') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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