SymphonyElectron/js/notify/settings/configure-notification-position.js
Vishwas Shashidhar 7e8814f862 ELECTRON-907: add more logging
- add logging in all the critical places
- add logging for relevant events
- add support for setting chrome flags from command line
- add support for deleting old log files
2018-11-28 14:48:56 -08:00

211 lines
6.4 KiB
JavaScript

'use strict';
const path = require('path');
const fs = require('fs');
const electron = require('electron');
const BrowserWindow = electron.BrowserWindow;
const app = electron.app;
const ipc = electron.ipcMain;
const log = require('../../log.js');
const logLevels = require('../../enums/logLevels.js');
const notify = require('./../electron-notify');
const eventEmitter = require('./../../eventEmitter');
const { updateConfigField } = require('../../config');
const i18n = require('../../translation/i18n');
const { isMac } = require('../../utils/misc');
let configurationWindow;
let screens;
let position;
let display;
let sandboxed = false;
let windowConfig = {
width: 460,
height: 360,
show: false,
modal: true,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'configure-notification-position-preload.js'),
sandbox: sandboxed,
nodeIntegration: false,
devTools: false
}
};
app.on('ready', () => {
screens = electron.screen.getAllDisplays();
// if display added/removed update the list of screens
electron.screen.on('display-added', updateScreens);
electron.screen.on('display-removed', updateScreens);
});
/**
* Update all the screens
*/
function updateScreens() {
log.send(logLevels.INFO, `Updating screens as there is change in display connections`);
screens = electron.screen.getAllDisplays();
// Notifying renderer when a display is added/removed
if (configurationWindow) {
// Event that updates the DOM elements
// notification position checkbox and monitor selection drop-down
configurationWindow.webContents.send('notificationSettings', {position: position, display: display});
if (screens && screens.length >= 0) {
configurationWindow.webContents.send('screens', screens);
}
}
}
/**
* Gets the template path
* @returns {string}
*/
function getTemplatePath() {
let templatePath = path.join(__dirname, 'configure-notification-position.html');
try {
fs.statSync(templatePath).isFile();
} catch (err) {
log.send(logLevels.ERROR, 'configure-notification-position: Could not find template ("' + templatePath + '").');
}
return 'file://' + templatePath;
}
/**
* Opens the configuration window for a specific window
* @param windowName
*/
function openConfigurationWindow(windowName) {
// This prevents creating multiple instances of the
// notification configuration window
if (configurationWindow && !configurationWindow.isDestroyed()) {
if (configurationWindow.isMinimized()) {
configurationWindow.restore();
}
configurationWindow.focus();
return;
}
const allWindows = BrowserWindow.getAllWindows();
const selectedParentWindow = allWindows.find((window) => { return window.winName === windowName });
// if we couldn't find any window matching the window name
// it will render as a new window
if (selectedParentWindow) {
windowConfig.parent = selectedParentWindow;
/**
* This is a temporary work around until there
* is a fix for the modal window in windows from the electron
* issue - https://github.com/electron/electron/issues/10721
*/
const { x, y, width, height } = selectedParentWindow.getBounds();
const windowWidth = Math.round(width * 0.5);
const windowHeight = Math.round(height * 0.5);
// Calculating the center of the parent window
// to place the configuration window
const centerX = x + width / 2.0;
const centerY = y + height / 2.0;
windowConfig.x = Math.round(centerX - (windowWidth / 2.0));
windowConfig.y = Math.round(centerY - (windowHeight / 2.0));
}
configurationWindow = new BrowserWindow(windowConfig);
configurationWindow.setVisibleOnAllWorkspaces(true);
configurationWindow.loadURL(getTemplatePath());
configurationWindow.once('ready-to-show', () => {
configurationWindow.show();
});
configurationWindow.webContents.on('did-finish-load', () => {
const notificationSettingsContent = i18n.getMessageFor('NotificationSettings');
configurationWindow.webContents.send('i18n-notification-settings', notificationSettingsContent);
if (screens && screens.length >= 0) {
configurationWindow.webContents.send('screens', screens);
}
configurationWindow.webContents.send('notificationSettings', {position: position, display: display});
if (!isMac) {
// prevents from displaying menu items when "alt" key is pressed
configurationWindow.setMenu(null);
}
});
configurationWindow.on('close', () => {
destroyWindow();
});
configurationWindow.on('closed', () => {
destroyWindow();
});
}
/**
* Destroys a window
*/
function destroyWindow() {
log.send(logLevels.INFO, `Closing notification configure window`);
configurationWindow = null;
}
/**
* Method to save 'position' & 'display' to the config file
*/
function updateConfig() {
let settings = {
position: position,
display: display
};
updateConfigField('notificationSettings', settings);
updateNotification(position, display);
log.send(logLevels.INFO, `Updating notification position on screen`);
}
/**
* Method to update the Notification class with the new 'position' & 'screen'
* @param mPosition - position to display the notifications
* ('upper-right, upper-left, lower-right, lower-left')
* @param mDisplay - id of the selected display
*/
function updateNotification(mPosition, mDisplay) {
notify.updateConfig({position: mPosition, display: mDisplay});
eventEmitter.emit('notificationSettings', {position: mPosition, display: mDisplay});
notify.reset();
}
ipc.on('close-alert', function () {
configurationWindow.close();
});
ipc.on('update-config', (event, config) => {
if (config) {
if (config.position) {
position = config.position;
}
if (config.display) {
display = config.display;
}
}
updateConfig();
});
/**
* Event to read 'Position' & 'Display' from config and
* updated the configuration view
*/
eventEmitter.on('notificationSettings', (notificationSettings) => {
position = notificationSettings.position;
display = notificationSettings.display;
});
module.exports = {
openConfigurationWindow: openConfigurationWindow
};