From 91e2518065298c1b0605c168c6f0c0af7954772b Mon Sep 17 00:00:00 2001 From: Ramiro Saenz Date: Mon, 25 Jul 2016 23:43:49 -0300 Subject: [PATCH] Added Preferences - Autolaunch - Always on Top - Hide Menu Bar - Skip Taskbar - Keep in taskbar on close Fixes #39 Fixes #130 Fixes #43 --- app/package.json | 1 + electron/global_settings.js | 37 +++++++++ electron/main.js | 21 +++-- electron/menu.js | 155 +++++++++++++++++++++++++++++++++++- electron/tray.js | 4 +- package.json | 1 + 6 files changed, 209 insertions(+), 10 deletions(-) create mode 100644 electron/global_settings.js diff --git a/app/package.json b/app/package.json index 58c03c6a..035ae0eb 100644 --- a/app/package.json +++ b/app/package.json @@ -25,6 +25,7 @@ "author": "Ramiro Saenz ", "license": "MIT", "dependencies": { + "auto-launch": "^2.1.0", "electron-window-state": "^3.0.3", "firebase": "^3.0.5", "firebase-token-generator": "^2.0.0" diff --git a/electron/global_settings.js b/electron/global_settings.js new file mode 100644 index 00000000..94c04c18 --- /dev/null +++ b/electron/global_settings.js @@ -0,0 +1,37 @@ +const fs = require('fs'); +const settingsPathname = 'rambox_cfg.json'; + +var globalSettings = { + settings: { + always_on_top: 0, + hide_menu_bar: 0, + skip_taskbar: 0, + auto_launch: 1, + keep_in_taskbar_on_close: 1, + start_minimized: 0 + }, + init: function(settings) { + this.settings = settings; + }, + set: function(name, value) { + this.settings[name] = value; + }, + get: function(name) { + return this.settings[name]; + }, + save: function() { + try { + fs.writeFileSync(settingsPathname, JSON.stringify(this.settings)); + } catch (err) {} + } +}; + +try { + //test to see if settings exist + fs.openSync(settingsPathname, 'r+'); //throws error if file doesn't exist + globalSettings.init(JSON.parse(fs.readFileSync(settingsPathname))); +} catch (err) { + globalSettings.save(); +} + +module.exports = globalSettings; diff --git a/electron/main.js b/electron/main.js index 3bf97735..f36036df 100644 --- a/electron/main.js +++ b/electron/main.js @@ -13,9 +13,8 @@ const appMenu = require('./menu'); const tray = require('./tray'); // Window State Plugin const windowStateKeeper = require('electron-window-state'); - - -const MenuItem = electron.MenuItem; +// Global Settings +var globalSettings = require('./global_settings.js'); // this should be placed at top of main.js to handle setup events quickly if (handleSquirrelEvent()) { @@ -92,20 +91,26 @@ let isQuitting = false; function createWindow () { // Load the previous state with fallback to defaults + let mainWindowState = windowStateKeeper({ defaultWidth: 1000 ,defaultHeight: 800 - ,maximize: true + ,maximize: false }); + // Create the browser window using the state information mainWindow = new BrowserWindow({ title: 'Rambox' - ,skipTaskbar: false ,icon: __dirname + '/../resources/Icon.png' ,x: mainWindowState.x ,y: mainWindowState.y ,width: mainWindowState.width ,height: mainWindowState.height + ,backgroundColor: '#2E658E' + ,alwaysOnTop: parseInt(globalSettings.get('always_on_top')) ? true : false + ,autoHideMenuBar: parseInt(globalSettings.get('hide_menu_bar')) ? true : false + ,skipTaskbar: parseInt(globalSettings.get('skip_taskbar')) ? true : false + ,show: parseInt(globalSettings.get('start_minimized')) ? false : true ,webPreferences: { webSecurity: false ,nodeIntegration: true @@ -114,6 +119,8 @@ function createWindow () { } }); + if ( !parseInt(globalSettings.get('start_minimized')) && mainWindowState.isMaximized ) mainWindow.maximize(); + // Let us register listeners on the window, so we can update the state // automatically (the listeners will be removed when the window is closed) // and restore the maximized or full screen state @@ -126,7 +133,7 @@ function createWindow () { electron.Menu.setApplicationMenu(appMenu); - tray.create(mainWindow); + tray.create(mainWindow, mainWindowState); mainWindow.on('page-title-updated', (e, title) => updateBadge(title)); @@ -152,7 +159,7 @@ function createWindow () { if (process.platform === 'darwin') { app.hide(); } else { - mainWindow.hide(); + parseInt(globalSettings.get('keep_in_taskbar_on_close')) ? mainWindow.minimize() : mainWindow.hide(); } } }); diff --git a/electron/menu.js b/electron/menu.js index 0ef0863a..22d85b68 100644 --- a/electron/menu.js +++ b/electron/menu.js @@ -5,6 +5,21 @@ const app = electron.app; const BrowserWindow = electron.BrowserWindow; const shell = electron.shell; const appName = app.getName(); +// AutoLaunch +var AutoLaunch = require('auto-launch'); +// Global Settings +var globalSettings = require('./global_settings.js'); + +// Configure AutoLaunch +const appLauncher = new AutoLaunch({ + name: 'Rambox' +}); +appLauncher.isEnabled().then(function(enabled){ + if(enabled) return; + return appLauncher.enable(); +}).then(function(err){ + +}); function sendAction(action) { const win = BrowserWindow.getAllWindows()[0]; @@ -158,6 +173,24 @@ let tpl = [ label: 'Close', accelerator: 'CmdOrCtrl+W', role: 'close' + }, + { + type: 'separator' + }, + { + label: 'Always on top', + type: 'checkbox', + checked: parseInt(globalSettings.get('always_on_top')) ? true : false, + click: function(item, mainWindow) { + if ( item.checked ) { + globalSettings.set('always_on_top', 1); + if (mainWindow) mainWindow.setAlwaysOnTop(true); + } else { + globalSettings.set('always_on_top', 0); + mainWindow.setAlwaysOnTop(false); + } + globalSettings.save(); + } } ] }, @@ -167,6 +200,111 @@ let tpl = [ } ]; +let preferences = [ + { + label: 'Auto-hide Menu bar', + visible: process.platform === 'win32', + type: 'checkbox', + checked: parseInt(globalSettings.get('hide_menu_bar')) ? true : false, + click: function(item, mainWindow) { + if ( item.checked ) { + electron.dialog.showMessageBox(mainWindow, { + title: 'Don\'t need to see the menu bar all the time?' + ,message: 'To temporarily show the menu bar, just press the Alt key.' + ,buttons: ['OK'] + ,type: 'info' + }, function() { + mainWindow.focus(); + }); + globalSettings.set('hide_menu_bar', 1); + if (mainWindow) mainWindow.setAutoHideMenuBar(true); + } else { + globalSettings.set('hide_menu_bar', 0); + mainWindow.setAutoHideMenuBar(false); + } + globalSettings.save(); + } + }, + { + label: 'Show in Taskbar', + type: 'checkbox', + checked: parseInt(globalSettings.get('skip_taskbar')) ? false : true, + click: function(item, mainWindow) { + if ( item.checked ) { + globalSettings.set('skip_taskbar', 0); + globalSettings.set('keep_in_taskbar_on_close', 1); + menu.items[0].submenu.items[process.platform === 'darwin' ? 2 : 0].submenu.items[2].enabled = true; + menu.items[0].submenu.items[process.platform === 'darwin' ? 2 : 0].submenu.items[2].checked = true; + if (mainWindow) mainWindow.setSkipTaskbar(false); + } else { + globalSettings.set('skip_taskbar', 1); + globalSettings.set('keep_in_taskbar_on_close', 0); + menu.items[0].submenu.items[process.platform === 'darwin' ? 2 : 0].submenu.items[2].enabled = false; + menu.items[0].submenu.items[process.platform === 'darwin' ? 2 : 0].submenu.items[2].checked = false; + mainWindow.setSkipTaskbar(true); + } + globalSettings.save(); + } + }, + { + label: 'Keep Rambox in the taskbar when close it', + type: 'checkbox', + enabled: parseInt(globalSettings.get('skip_taskbar')) ? false : true, + checked: parseInt(globalSettings.get('keep_in_taskbar_on_close')) ? true : false, + click: function(item) { + if ( item.checked ) { + globalSettings.set('keep_in_taskbar_on_close', 1); + } else { + globalSettings.set('keep_in_taskbar_on_close', 0); + } + globalSettings.save(); + } + }, + { + label: 'Always on top', + type: 'checkbox', + checked: parseInt(globalSettings.get('always_on_top')) ? true : false, + click: function(item, mainWindow) { + if ( item.checked ) { + globalSettings.set('always_on_top', 1); + if (mainWindow) mainWindow.setAlwaysOnTop(true); + } else { + globalSettings.set('always_on_top', 0); + mainWindow.setAlwaysOnTop(false); + } + globalSettings.save(); + } + }, + { + label: 'Start minimized', + type: 'checkbox', + checked: parseInt(globalSettings.get('start_minimized')) ? true : false, + click: function(item) { + if ( item.checked ) { + globalSettings.set('start_minimized', 1); + } else { + globalSettings.set('start_minimized', 0); + } + globalSettings.save(); + } + }, + { + label: 'Start automatically on system startup', + type: 'checkbox', + checked: parseInt(globalSettings.get('auto_launch')) ? true : false, + click: function(item) { + if ( item.checked ) { + appLauncher.enable(); + globalSettings.set('auto_launch', 1); + } else { + appLauncher.disable(); + globalSettings.set('auto_launch', 0); + } + globalSettings.save(); + } + } +]; + if (process.platform === 'darwin') { tpl.unshift({ label: appName, @@ -180,6 +318,13 @@ if (process.platform === 'darwin') { { type: 'separator' }, + { + label: 'Preferences', + submenu: preferences + }, + { + type: 'separator' + }, { label: 'Services', role: 'services', @@ -218,6 +363,13 @@ if (process.platform === 'darwin') { tpl.unshift({ label: 'File', submenu: [ + { + label: 'Preferences', + submenu: preferences + }, + { + type: 'separator' + }, { label: `Quit ${appName}`, accelerator: 'Cmd+Q', @@ -240,4 +392,5 @@ if (process.platform === 'darwin') { tpl[tpl.length - 1].submenu = helpSubmenu; -module.exports = electron.Menu.buildFromTemplate(tpl); +var menu = electron.Menu.buildFromTemplate(tpl); +module.exports = menu; diff --git a/electron/tray.js b/electron/tray.js index 5ff849e2..682e22af 100644 --- a/electron/tray.js +++ b/electron/tray.js @@ -7,7 +7,7 @@ const Tray = electron.Tray; const MenuItem = electron.MenuItem; var appIcon = null; -exports.create = win => { +exports.create = function(win, mainWindowState) { if (process.platform === 'darwin' || appIcon) { return; } @@ -45,7 +45,7 @@ exports.create = win => { appIcon.setToolTip('Rambox'); appIcon.setContextMenu(contextMenu); appIcon.on('double-click', () => { - win.show(); + mainWindowState.isMaximized ? win.maximize() : win.show(); }); }; diff --git a/package.json b/package.json index b6ec98a5..354f722e 100644 --- a/package.json +++ b/package.json @@ -106,6 +106,7 @@ } }, "dependencies": { + "auto-launch": "^2.1.0", "electron-window-state": "^3.0.3", "firebase": "^3.0.5", "firebase-token-generator": "^2.0.0"