diff --git a/main.js b/main.js index ad1c5ebabee39..3714f8a9c85e1 100644 --- a/main.js +++ b/main.js @@ -1,4 +1,9 @@ -const {app, BrowserWindow, shell} = require('electron'); +const { + app, + BrowserWindow, + shell, + ipcMain +} = require('electron'); const serve = require('electron-serve'); const contextMenu = require('electron-context-menu'); const {autoUpdater} = require('electron-updater'); @@ -57,6 +62,12 @@ const mainWindow = (() => { return shell.openExternal(url); }); + ipcMain.on('request-visibility', (event) => { + // This is how synchronous messages work in Electron + // eslint-disable-next-line no-param-reassign + event.returnValue = browserWindow.isFocused(); + }); + return browserWindow; }) diff --git a/src/lib/Visibility/index.js b/src/lib/Visibility/index.js new file mode 100644 index 0000000000000..7929d824a5f27 --- /dev/null +++ b/src/lib/Visibility/index.js @@ -0,0 +1,22 @@ +// We conditionally import the ipcRenderer here so that we can +// communicate with the main Electron process in main.js +const ipcRenderer = window.require ? window.require('electron').ipcRenderer : null; + +/** + * Detects whether the app is visible or not. Electron supports + * document.visibilityState, but switching to another app while + * Electron is partially occluded will not trigger a state of hidden + * so we ask the main process synchronously whether the + * BrowserWindow.isFocused() + * + * @returns {Boolean} + */ +function isVisible() { + return ipcRenderer + ? ipcRenderer.sendSync('request-visibility') + : document.visibilityState === 'visible'; +} + +export default { + isVisible, +}; diff --git a/src/lib/Visibility/index.native.js b/src/lib/Visibility/index.native.js new file mode 100644 index 0000000000000..ba878d4ef7d07 --- /dev/null +++ b/src/lib/Visibility/index.native.js @@ -0,0 +1,11 @@ +// Mobile apps do not require this check for visibility as +// they do not use the Notification lib. + +/** + * @return {Boolean} + */ +const isVisible = () => true; + +export default { + isVisible, +}; diff --git a/src/lib/actions/Report.js b/src/lib/actions/Report.js index a86f0bc72c80e..90c311a88dbdc 100644 --- a/src/lib/actions/Report.js +++ b/src/lib/actions/Report.js @@ -11,6 +11,7 @@ import ExpensiMark from '../ExpensiMark'; import Notification from '../Notification'; import * as PersonalDetails from './PersonalDetails'; import {redirect} from './App'; +import Visibility from '../Visibility'; let currentUserEmail; let currentUserAccountID; @@ -200,7 +201,7 @@ function updateReportWithNewAction(reportID, reportAction) { const currentReportID = Number(lodashGet(currentURL.split('/'), [1], 0)); // If we are currently viewing this report do not show a notification. - if (reportID === currentReportID) { + if (reportID === currentReportID && Visibility.isVisible()) { console.debug('[NOTIFICATION] No notification because it was a comment for the current report'); return; }