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

Commit

Permalink
Merge pull request #3630 from brave/window-blur
Browse files Browse the repository at this point in the history
add a null page view when all windows lose focus
  • Loading branch information
bbondy authored Sep 4, 2016
2 parents a00b0df + 3011f57 commit a988d59
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ let generateBraveManifest = () => {
name: 'brave',
manifest_version: 2,
version: '1.0',
background: {
scripts: [ 'content/scripts/idleHandler.js' ]
},
content_scripts: [
{
run_at: 'document_start',
Expand Down Expand Up @@ -90,7 +93,7 @@ let generateBraveManifest = () => {
}
],
permissions: [
'externally_connectable.all_urls', 'tabs', '<all_urls>', 'contentSettings'
'externally_connectable.all_urls', 'tabs', '<all_urls>', 'contentSettings', 'idle'
],
externally_connectable: {
matches: [
Expand Down
7 changes: 7 additions & 0 deletions app/extensions/brave/content/scripts/idleHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
chrome.idle.setDetectionInterval(15*60)
chrome.idle.onStateChanged.addListener((idleState) => {
chrome.ipc.send('dispatch-action', JSON.stringify({
actionType: 'app-idle-state-changed',
idleState
}))
})
10 changes: 10 additions & 0 deletions docs/appActions.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,16 @@ Remove credit card data



### windowBlurred(appWindowId)

Dispatches a message when appWindowId loses focus

**Parameters**

**appWindowId**: `Number`, the unique id of the window




* * *

Expand Down
12 changes: 12 additions & 0 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,18 @@ const appActions = {
actionType: AppConstants.APP_REMOVE_AUTOFILL_CREDIT_CARD,
detail
})
},

/**
* Dispatches a message when appWindowId loses focus
*
* @param {Number} appWindowId - the unique id of the window
*/
windowBlurred: function (appWindowId) {
AppDispatcher.dispatch({
actionType: AppConstants.APP_WINDOW_BLURRED,
appWindowId: appWindowId
})
}
}

Expand Down
4 changes: 3 additions & 1 deletion js/constants/appConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ const AppConstants = {
APP_ADD_AUTOFILL_CREDIT_CARD: _,
APP_REMOVE_AUTOFILL_CREDIT_CARD: _,
APP_SET_LOGIN_REQUIRED_DETAIL: _,
APP_SET_LOGIN_RESPONSE_DETAIL: _
APP_SET_LOGIN_RESPONSE_DETAIL: _,
APP_WINDOW_BLURRED: _,
APP_IDLE_STATE_CHANGED: _
}

module.exports = mapValuesByKeys(AppConstants)
4 changes: 4 additions & 0 deletions js/stores/appStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ const createWindow = (browserOpts, defaults, frameOpts, windowState) => {
mainWindow.setFullScreen(true)
}

mainWindow.on('blur', function () {
appActions.windowBlurred(mainWindow.id)
})

mainWindow.on('focus', function () {
mainWindow.webContents.send(messages.REQUEST_MENU_DATA_FOR_WINDOW)
})
Expand Down
17 changes: 17 additions & 0 deletions js/stores/eventStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ const addPageView = (url) => {
lastActivePageUrl = url
}

const windowBlurred = (windowId) => {
let windowCount = BrowserWindow.getAllWindows().filter((win) => win.isFocused()).length
if (windowCount === 0) {
addPageView(null)
}
}

const windowClosed = (windowId) => {
let windowCount = BrowserWindow.getAllWindows().length
let win = BrowserWindow.getFocusedWindow()
Expand Down Expand Up @@ -101,6 +108,16 @@ const doAction = (action) => {
addPageView(action.frameProps.get('location'))
lastActiveTabId = action.frameProps.get('tabId')
break
case AppConstants.APP_WINDOW_BLURRED:
windowBlurred(action.appWindowId)
break
case AppConstants.APP_IDLE_STATE_CHANGED:
if (action.idleState !== 'active') {
addPageView(null)
} else {
addPageView(lastActivePageUrl)
}
break
case AppConstants.APP_CLOSE_WINDOW:
AppDispatcher.waitFor([AppStore.dispatchToken], () => {
windowClosed(action.appWindowId)
Expand Down

0 comments on commit a988d59

Please sign in to comment.