From 92722fbd1762f96e423409363ba92bd26a68e668 Mon Sep 17 00:00:00 2001 From: bridiver Date: Wed, 31 Aug 2016 19:27:42 -0700 Subject: [PATCH] add a null page view when all windows lose focus add a null page view when idle fixes #3650 --- app/extensions.js | 5 ++++- .../brave/content/scripts/idleHandler.js | 7 +++++++ docs/appActions.md | 10 ++++++++++ js/actions/appActions.js | 12 ++++++++++++ js/constants/appConstants.js | 4 +++- js/stores/appStore.js | 4 ++++ js/stores/eventStore.js | 17 +++++++++++++++++ 7 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 app/extensions/brave/content/scripts/idleHandler.js diff --git a/app/extensions.js b/app/extensions.js index f4806e7a8f6..eb785065e49 100644 --- a/app/extensions.js +++ b/app/extensions.js @@ -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', @@ -90,7 +93,7 @@ let generateBraveManifest = () => { } ], permissions: [ - 'externally_connectable.all_urls', 'tabs', '', 'contentSettings' + 'externally_connectable.all_urls', 'tabs', '', 'contentSettings', 'idle' ], externally_connectable: { matches: [ diff --git a/app/extensions/brave/content/scripts/idleHandler.js b/app/extensions/brave/content/scripts/idleHandler.js new file mode 100644 index 00000000000..20e08949cbe --- /dev/null +++ b/app/extensions/brave/content/scripts/idleHandler.js @@ -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 + })) +}) diff --git a/docs/appActions.md b/docs/appActions.md index 31dee856baa..e81f659c744 100644 --- a/docs/appActions.md +++ b/docs/appActions.md @@ -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 + + + * * * diff --git a/js/actions/appActions.js b/js/actions/appActions.js index 6b6ce08a5d2..451745a708f 100644 --- a/js/actions/appActions.js +++ b/js/actions/appActions.js @@ -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 + }) } } diff --git a/js/constants/appConstants.js b/js/constants/appConstants.js index 54e802c3977..3f0f1169b30 100644 --- a/js/constants/appConstants.js +++ b/js/constants/appConstants.js @@ -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) diff --git a/js/stores/appStore.js b/js/stores/appStore.js index bcec5264baf..3174fcda57c 100644 --- a/js/stores/appStore.js +++ b/js/stores/appStore.js @@ -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) }) diff --git a/js/stores/eventStore.js b/js/stores/eventStore.js index 68b4fb4ddf2..b9e74837775 100644 --- a/js/stores/eventStore.js +++ b/js/stores/eventStore.js @@ -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() @@ -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)