From 0ff4a8bb6d67521813347622fe88b9b322c3898c Mon Sep 17 00:00:00 2001 From: Vishwas Shashidhar Date: Thu, 10 May 2018 15:06:37 +0530 Subject: [PATCH] ELECTRON-481: fix the error dialogs with always on top use case (#364) --- js/downloadManager/index.js | 10 ++++++---- js/main.js | 4 ++-- js/menus/menuTemplate.js | 34 ++++++++++++++++++++++------------ js/windowMgr.js | 2 +- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/js/downloadManager/index.js b/js/downloadManager/index.js index 0187110d..f81fca4b 100644 --- a/js/downloadManager/index.js +++ b/js/downloadManager/index.js @@ -27,8 +27,9 @@ function openFile(id) { }); if (fileIndex !== -1) { let openResponse = remote.shell.openExternal(`file:///${local.downloadItems[fileIndex].savedPath}`); - if (!openResponse) { - remote.dialog.showErrorBox("File not found", 'The file you are trying to open cannot be found in the specified path.'); + let focusedWindow = remote.BrowserWindow.getFocusedWindow(); + if (!openResponse && focusedWindow && !focusedWindow.isDestroyed()) { + remote.dialog.showMessageBox(focusedWindow, {type: 'error', title: 'File not found', message: 'The file you are trying to open cannot be found in the specified path.'}); } } } @@ -43,8 +44,9 @@ function showInFinder(id) { }); if (showFileIndex !== -1) { let showResponse = remote.shell.showItemInFolder(local.downloadItems[showFileIndex].savedPath); - if (!showResponse) { - remote.dialog.showErrorBox("File not found", 'The file you are trying to access cannot be found in the specified path.'); + let focusedWindow = remote.BrowserWindow.getFocusedWindow(); + if (!showResponse && focusedWindow && !focusedWindow.isDestroyed()) { + remote.dialog.showMessageBox(focusedWindow, {type: 'error', title: 'File not found', message: 'The file you are trying to open cannot be found in the specified path.'}); } } } diff --git a/js/main.js b/js/main.js index ed650c43..4aaa3a67 100644 --- a/js/main.js +++ b/js/main.js @@ -328,8 +328,8 @@ function getUrlAndCreateMainWindow() { getConfigField('url') .then(createWin).catch(function(err) { - let title = 'Error loading configuration'; - electron.dialog.showErrorBox(title, title + ': ' + err); + log.send(logLevels.ERROR, `unable to create main window -> ${err}`); + app.quit(); }); } diff --git a/js/menus/menuTemplate.js b/js/menus/menuTemplate.js index 53727ae8..5e336bf1 100644 --- a/js/menus/menuTemplate.js +++ b/js/menus/menuTemplate.js @@ -116,7 +116,7 @@ const template = [{ submenu: [ { label: isMac ? 'Show Logs in Finder' : 'Show Logs in Explorer', - click() { + click(item, focusedWindow) { const FILE_EXTENSIONS = [ '.log' ]; const MAC_LOGS_PATH = '/Library/Logs/Symphony/'; @@ -125,8 +125,8 @@ const template = [{ let logsPath = isMac ? MAC_LOGS_PATH : WINDOWS_LOGS_PATH; let source = electron.app.getPath('home') + logsPath; - if (!fs.existsSync(source)) { - electron.dialog.showErrorBox('Failed!', 'No logs are available to share'); + if (!fs.existsSync(source) && focusedWindow && !focusedWindow.isDestroyed()) { + electron.dialog.showMessageBox(focusedWindow, {type: 'error', title: 'Failed!', message: 'No logs are available to share'}); return; } @@ -140,20 +140,22 @@ const template = [{ electron.shell.showItemInFolder(destination); }) .catch((err) => { - electron.dialog.showErrorBox('Failed!', 'Unable to generate logs due to -> ' + err); + if (focusedWindow && !focusedWindow.isDestroyed()) { + electron.dialog.showMessageBox(focusedWindow, {type: 'error', title: 'Failed!', message: `Unable to generate logs due to -> ${err}`}); + } }) } }, { label: isMac ? 'Show crash dump in Finder' : 'Show crash dump in Explorer', - click() { + click(item, focusedWindow) { const FILE_EXTENSIONS = isMac ? [ '.dmp' ] : [ '.dmp', '.txt' ]; const crashesDirectory = electron.crashReporter.getCrashesDirectory(); let source = isMac ? crashesDirectory + '/completed' : crashesDirectory; - if (!fs.existsSync(source) || fs.readdirSync(source).length === 0) { - electron.dialog.showErrorBox('Failed!', 'No crashes available to share'); + if (!fs.existsSync(source) || fs.readdirSync(source).length === 0 && focusedWindow && !focusedWindow.isDestroyed()) { + electron.dialog.showMessageBox(focusedWindow, {type: 'error', title: 'Failed!', message: 'No crashes available to share'}); return; } @@ -167,7 +169,9 @@ const template = [{ electron.shell.showItemInFolder(destination); }) .catch((err) => { - electron.dialog.showErrorBox('Failed!', 'Unable to generate crash report due to -> ' + err); + if (focusedWindow && !focusedWindow.isDestroyed()) { + electron.dialog.showMessageBox(focusedWindow, {type: 'error', title: 'Failed!', message: `Unable to generate crash reports due to -> ${err}`}); + } }); } } @@ -259,20 +263,24 @@ function getTemplate(app) { label: 'Auto Launch On Startup', type: 'checkbox', checked: launchOnStartup, - click: function(item) { + click: function(item, focusedWindow) { if (item.checked) { symphonyAutoLauncher.enable() .catch(function(err) { let title = 'Error setting AutoLaunch configuration'; log.send(logLevels.ERROR, 'MenuTemplate: ' + title + ': auto launch error ' + err); - electron.dialog.showErrorBox(title, title + ': ' + err); + if (focusedWindow && !focusedWindow.isDestroyed()) { + electron.dialog.showMessageBox(focusedWindow, {type: 'error', title, message: title + ': ' + err}); + } }); } else { symphonyAutoLauncher.disable() .catch(function(err) { let title = 'Error setting AutoLaunch configuration'; log.send(logLevels.ERROR, 'MenuTemplate: ' + title + ': auto launch error ' + err); - electron.dialog.showErrorBox(title, title + ': ' + err); + if (focusedWindow && !focusedWindow.isDestroyed()) { + electron.dialog.showMessageBox(focusedWindow, {type: 'error', title, message: title + ': ' + err}); + } }); } launchOnStartup = item.checked; @@ -401,7 +409,9 @@ function setCheckboxValues() { .catch((err) => { let title = 'Error loading configuration'; log.send(logLevels.ERROR, 'MenuTemplate: error reading configuration fields, error: ' + err); - electron.dialog.showErrorBox(title, title + ': ' + err); + if (electron.BrowserWindow.getFocusedWindow() && !electron.BrowserWindow.getFocusedWindow().isDestroyed()) { + electron.dialog.showMessageBox(electron.BrowserWindow.getFocusedWindow(), {type: 'error', title, message: title + ': ' + err}); + } return resolve(); }); }); diff --git a/js/windowMgr.js b/js/windowMgr.js index 3f2bd207..ca0c165e 100644 --- a/js/windowMgr.js +++ b/js/windowMgr.js @@ -513,7 +513,7 @@ function doCreateMainWindow(initialUrl, initialBounds, isCustomTitleBar) { if (!userPermission) { let fullMessage = `Your administrator has disabled ${message}. Please contact your admin for help.`; - electron.dialog.showErrorBox('Permission Denied!', fullMessage); + electron.dialog.showMessageBox(BrowserWindow.fromWebContents(webContents), {type: 'error', title: 'Permission Denied!', message: fullMessage}); } return cb(userPermission);