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

add a null page view when all windows lose focus #3630

Merged
merged 2 commits into from
Sep 4, 2016
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
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)
Copy link
Member

Choose a reason for hiding this comment

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

How does a null page view behave?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it just logs the timestamp when the browser became inactive. We use the same thing for closing all windows

} else {
addPageView(lastActivePageUrl)
}
break
case AppConstants.APP_CLOSE_WINDOW:
AppDispatcher.waitFor([AppStore.dispatchToken], () => {
windowClosed(action.appWindowId)
Expand Down