Electron-56 (Always on top) (#127)

* ELECTRON-56 - Implemented always on top feature

* ELECTRON-56 - Made some code refactoring

* ELECTRON-56 - Also fetching the alwaysOnTop prop from the event emitter

* ELECTRON-56 - Refactored code

* ELECTRON-56 - Exported only the required methods in event emitter
This commit is contained in:
Kiran Niranjan
2017-06-15 22:41:29 +05:30
committed by Lynn
parent d152a735a2
commit b72e95fd37
4 changed files with 74 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
{
"url": "https://foundation-dev.symphony.com",
"minimizeOnClose" : false,
"launchOnStartup" : true
"launchOnStartup" : true,
"alwaysOnTop" : false
}

10
js/eventEmitter.js Normal file
View File

@@ -0,0 +1,10 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
const eventEmitter = new EventEmitter();
// These method should only be used in main process
module.exports = {
emit: eventEmitter.emit,
on: eventEmitter.on
};

View File

@@ -7,9 +7,11 @@ const isMac = require('../utils/misc.js').isMac;
const childProcess = require('child_process');
const log = require('../log.js');
const logLevels = require('../enums/logLevels.js');
const eventEmitter = require('../eventEmitter');
var minimizeOnClose = false;
var launchOnStartup = false;
var isAlwaysOnTop = false;
setCheckboxValues();
@@ -183,8 +185,8 @@ function getTemplate(app) {
// Window menu -> launchOnStartup.
template[index].submenu.push(
{
label: 'Auto Launch On Startup',
type: 'checkbox',
label: 'Auto Launch On Startup',
type: 'checkbox',
checked: launchOnStartup,
click: function (item) {
if (item.checked){
@@ -232,12 +234,26 @@ function getTemplate(app) {
}
)
// Window menu -> minimizeOnClose.
// Window menu -> alwaysOnTop.
template[index].submenu.push(
{
label: 'Always on top',
type: 'checkbox',
checked: isAlwaysOnTop,
click: (item) => {
isAlwaysOnTop = item.checked;
eventEmitter.emit('isAlwaysOnTop', isAlwaysOnTop);
updateConfigField('alwaysOnTop', isAlwaysOnTop);
}
}
)
// Window menu -> minimizeOnClose.
// ToDo: Add behavior on Close.
template[index].submenu.push(
{
label: 'Minimize on Close',
type: 'checkbox',
label: 'Minimize on Close',
type: 'checkbox',
checked: minimizeOnClose,
click: function (item) {
minimizeOnClose = item.checked;
@@ -249,7 +265,7 @@ function getTemplate(app) {
if (!isMac){
template[index].submenu.push(
{
label: 'Quit Symphony',
label: 'Quit Symphony',
click: function () {
app.quit();
}
@@ -268,7 +284,7 @@ function setCheckboxValues(){
log.send(logLevels.ERROR, 'MenuTemplate: error getting config field minimizeOnClose, error: ' + err);
electron.dialog.showErrorBox(title, title + ': ' + err);
});
getConfigField('launchOnStartup').then(function(lStartup) {
launchOnStartup = lStartup;
}).catch(function (err){
@@ -276,6 +292,15 @@ function setCheckboxValues(){
log.send(logLevels.ERROR, 'MenuTemplate: error getting config field launchOnStartup, error: ' + err);
electron.dialog.showErrorBox(title, title + ': ' + err);
});
getConfigField('alwaysOnTop').then(function(mAlwaysOnTop) {
isAlwaysOnTop = mAlwaysOnTop;
eventEmitter.emit('isAlwaysOnTop', isAlwaysOnTop);
}).catch(function (err){
let title = 'Error loading configuration';
log.send(logLevels.ERROR, 'MenuTemplate: error getting config field alwaysOnTop, error: ' + err);
electron.dialog.showErrorBox(title, title + ': ' + err);
});
}
function getMinimizeOnClose(){

View File

@@ -14,6 +14,7 @@ const getGuid = require('./utils/getGuid.js');
const log = require('./log.js');
const logLevels = require('./enums/logLevels.js');
const notify = require('./notify/electron-notify.js');
const eventEmitter = require('./eventEmitter');
const throttle = require('./utils/throttle.js');
const { getConfigField, updateConfigField } = require('./config.js');
@@ -31,6 +32,7 @@ let windows = {};
let willQuitApp = false;
let isOnline = true;
let boundsChangeWindow;
let alwaysOnTop = false;
// note: this file is built using browserify in prebuild step.
const preloadMainScript = path.join(__dirname, 'preload/_preloadMain.js');
@@ -73,6 +75,7 @@ function doCreateMainWindow(initialUrl, initialBounds) {
show: true,
minWidth: MIN_WIDTH,
minHeight: MIN_HEIGHT,
alwaysOnTop: false,
webPreferences: {
sandbox: true,
nodeIntegration: false,
@@ -103,6 +106,11 @@ function doCreateMainWindow(initialUrl, initialBounds) {
newWinOpts.y = bounds.y;
}
// will set the main window on top as per the user prefs
if (alwaysOnTop){
newWinOpts.alwaysOnTop = alwaysOnTop;
}
// note: augmenting with some custom values
newWinOpts.winKey = key;
@@ -186,6 +194,7 @@ function doCreateMainWindow(initialUrl, initialBounds) {
// open external links in default browser - a tag with href='_blank' or window.open
mainWindow.webContents.on('new-window', function (event, newWinUrl,
frameName, disposition, newWinOptions) {
let newWinParsedUrl = getParsedUrl(newWinUrl);
let mainWinParsedUrl = getParsedUrl(url);
@@ -241,6 +250,7 @@ function doCreateMainWindow(initialUrl, initialBounds) {
newWinOptions.height = Math.max(height, MIN_HEIGHT);
newWinOptions.minWidth = MIN_WIDTH;
newWinOptions.minHeight = MIN_HEIGHT;
newWinOptions.alwaysOnTop = alwaysOnTop;
let newWinKey = getGuid();
@@ -256,6 +266,7 @@ function doCreateMainWindow(initialUrl, initialBounds) {
log.send(logLevels.INFO, 'loaded pop-out window url: ' + newWinParsedUrl);
browserWin.winName = frameName;
browserWin.setAlwaysOnTop(alwaysOnTop);
browserWin.once('closed', function () {
removeWindowKey(newWinKey);
@@ -386,6 +397,25 @@ function openUrlInDefaultBrower(urlToOpen) {
}
}
/**
* Called when an event is received from menu
* @param boolean weather to enable or disable alwaysOnTop.
*/
function isAlwaysOnTop(boolean) {
alwaysOnTop = boolean;
let browserWins = BrowserWindow.getAllWindows();
if (browserWins.length > 0) {
browserWins.forEach(function (browser) {
browser.setAlwaysOnTop(boolean);
});
}
}
// node event emitter to update always on top
eventEmitter.on('isAlwaysOnTop', (boolean) => {
isAlwaysOnTop(boolean);
});
module.exports = {
createMainWindow: createMainWindow,
getMainWindow: getMainWindow,