From a1dfea70bb4faa6ce6ff74e26c05175a4f64483e Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Tue, 17 Oct 2017 11:39:19 +0530 Subject: [PATCH] Electron-154 - Modified the code to open the main window after reading the config fields --- js/main.js | 17 +++++++++- js/menus/menuTemplate.js | 69 +++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/js/main.js b/js/main.js index 80963a16..752ac5c4 100644 --- a/js/main.js +++ b/js/main.js @@ -11,6 +11,7 @@ const urlParser = require('url'); // Local Dependencies const {getConfigField, updateUserConfigWin, updateUserConfigMac} = require('./config.js'); +const {setCheckboxValues} = require('./menus/menuTemplate.js'); const { isMac, isDevEnv } = require('./utils/misc.js'); const protocolHandler = require('./protocolHandler'); const getCmdLineArg = require('./utils/getCmdLineArg.js'); @@ -92,7 +93,7 @@ if (isMac) { * initialization and is ready to create browser windows. * Some APIs can only be used after this event occurs. */ -app.on('ready', setupThenOpenMainWindow); +app.on('ready', readConfigThenOpenMainWindow); /** * Is triggered when all the windows are closed @@ -127,6 +128,20 @@ app.on('open-url', function(event, url) { handleProtocolAction(url); }); +/** + * Reads the config fields that are required for the menu items + * then opens the main window + * + * This is a workaround for the issue where the menu template was returned + * even before the config data was populated + * https://perzoinc.atlassian.net/browse/ELECTRON-154 + */ +function readConfigThenOpenMainWindow() { + setCheckboxValues() + .then(setupThenOpenMainWindow) + .catch(setupThenOpenMainWindow) +} + /** * Sets up the app (to handle various things like config changes, protocol handling etc.) * and opens the main window diff --git a/js/menus/menuTemplate.js b/js/menus/menuTemplate.js index fdb961af..1010426a 100644 --- a/js/menus/menuTemplate.js +++ b/js/menus/menuTemplate.js @@ -13,8 +13,6 @@ let minimizeOnClose = false; let launchOnStartup = false; let isAlwaysOnTop = false; -setCheckboxValues(); - let symphonyAutoLauncher; if (isMac) { @@ -266,38 +264,42 @@ function getTemplate(app) { * based on configuration */ function setCheckboxValues() { - /** - * Method that reads multiple config fields - */ - getMultipleConfigField(['minimizeOnClose', 'launchOnStartup', 'alwaysOnTop', 'notificationSettings']) - .then(function (configData) { - for (let key in configData) { - if (configData.hasOwnProperty(key)) { // eslint-disable-line no-prototype-builtins - switch (key) { - case 'minimizeOnClose': - minimizeOnClose = configData[key]; - break; - case 'launchOnStartup': - launchOnStartup = configData[key]; - break; - case 'alwaysOnTop': - isAlwaysOnTop = configData[key]; - eventEmitter.emit('isAlwaysOnTop', configData[key]); - break; - case 'notificationSettings': - eventEmitter.emit('notificationSettings', configData[key]); - break; - default: - break; + return new Promise((resolve) => { + /** + * Method that reads multiple config fields + */ + getMultipleConfigField(['minimizeOnClose', 'launchOnStartup', 'alwaysOnTop', 'notificationSettings']) + .then(function (configData) { + for (let key in configData) { + if (configData.hasOwnProperty(key)) { // eslint-disable-line no-prototype-builtins + switch (key) { + case 'minimizeOnClose': + minimizeOnClose = configData[key]; + break; + case 'launchOnStartup': + launchOnStartup = configData[key]; + break; + case 'alwaysOnTop': + isAlwaysOnTop = configData[key]; + eventEmitter.emit('isAlwaysOnTop', configData[key]); + break; + case 'notificationSettings': + eventEmitter.emit('notificationSettings', configData[key]); + break; + default: + break; + } } } - } - }) - .catch((err) => { - let title = 'Error loading configuration'; - log.send(logLevels.ERROR, 'MenuTemplate: error reading configuration fields, error: ' + err); - electron.dialog.showErrorBox(title, title + ': ' + err); - }); + return resolve(); + }) + .catch((err) => { + let title = 'Error loading configuration'; + log.send(logLevels.ERROR, 'MenuTemplate: error reading configuration fields, error: ' + err); + electron.dialog.showErrorBox(title, title + ': ' + err); + return resolve(); + }); + }); } function getMinimizeOnClose() { @@ -306,5 +308,6 @@ function getMinimizeOnClose() { module.exports = { getTemplate: getTemplate, - getMinimizeOnClose: getMinimizeOnClose + getMinimizeOnClose: getMinimizeOnClose, + setCheckboxValues: setCheckboxValues }; \ No newline at end of file