Browse Source

Added Preferences

- Autolaunch
- Always on Top
- Hide Menu Bar
- Skip Taskbar
- Keep in taskbar on close

Fixes #39
Fixes #130
Fixes #43
pull/116/merge
Ramiro Saenz 9 years ago
parent
commit
91e2518065
  1. 1
      app/package.json
  2. 37
      electron/global_settings.js
  3. 21
      electron/main.js
  4. 155
      electron/menu.js
  5. 4
      electron/tray.js
  6. 1
      package.json

1
app/package.json

@ -25,6 +25,7 @@
"author": "Ramiro Saenz <saenzramiro@gmail.com>", "author": "Ramiro Saenz <saenzramiro@gmail.com>",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"auto-launch": "^2.1.0",
"electron-window-state": "^3.0.3", "electron-window-state": "^3.0.3",
"firebase": "^3.0.5", "firebase": "^3.0.5",
"firebase-token-generator": "^2.0.0" "firebase-token-generator": "^2.0.0"

37
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;

21
electron/main.js

@ -13,9 +13,8 @@ const appMenu = require('./menu');
const tray = require('./tray'); const tray = require('./tray');
// Window State Plugin // Window State Plugin
const windowStateKeeper = require('electron-window-state'); const windowStateKeeper = require('electron-window-state');
// Global Settings
var globalSettings = require('./global_settings.js');
const MenuItem = electron.MenuItem;
// this should be placed at top of main.js to handle setup events quickly // this should be placed at top of main.js to handle setup events quickly
if (handleSquirrelEvent()) { if (handleSquirrelEvent()) {
@ -92,20 +91,26 @@ let isQuitting = false;
function createWindow () { function createWindow () {
// Load the previous state with fallback to defaults // Load the previous state with fallback to defaults
let mainWindowState = windowStateKeeper({ let mainWindowState = windowStateKeeper({
defaultWidth: 1000 defaultWidth: 1000
,defaultHeight: 800 ,defaultHeight: 800
,maximize: true ,maximize: false
}); });
// Create the browser window using the state information // Create the browser window using the state information
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
title: 'Rambox' title: 'Rambox'
,skipTaskbar: false
,icon: __dirname + '/../resources/Icon.png' ,icon: __dirname + '/../resources/Icon.png'
,x: mainWindowState.x ,x: mainWindowState.x
,y: mainWindowState.y ,y: mainWindowState.y
,width: mainWindowState.width ,width: mainWindowState.width
,height: mainWindowState.height ,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: { ,webPreferences: {
webSecurity: false webSecurity: false
,nodeIntegration: true ,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 // Let us register listeners on the window, so we can update the state
// automatically (the listeners will be removed when the window is closed) // automatically (the listeners will be removed when the window is closed)
// and restore the maximized or full screen state // and restore the maximized or full screen state
@ -126,7 +133,7 @@ function createWindow () {
electron.Menu.setApplicationMenu(appMenu); electron.Menu.setApplicationMenu(appMenu);
tray.create(mainWindow); tray.create(mainWindow, mainWindowState);
mainWindow.on('page-title-updated', (e, title) => updateBadge(title)); mainWindow.on('page-title-updated', (e, title) => updateBadge(title));
@ -152,7 +159,7 @@ function createWindow () {
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
app.hide(); app.hide();
} else { } else {
mainWindow.hide(); parseInt(globalSettings.get('keep_in_taskbar_on_close')) ? mainWindow.minimize() : mainWindow.hide();
} }
} }
}); });

155
electron/menu.js

@ -5,6 +5,21 @@ const app = electron.app;
const BrowserWindow = electron.BrowserWindow; const BrowserWindow = electron.BrowserWindow;
const shell = electron.shell; const shell = electron.shell;
const appName = app.getName(); 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) { function sendAction(action) {
const win = BrowserWindow.getAllWindows()[0]; const win = BrowserWindow.getAllWindows()[0];
@ -158,6 +173,24 @@ let tpl = [
label: 'Close', label: 'Close',
accelerator: 'CmdOrCtrl+W', accelerator: 'CmdOrCtrl+W',
role: 'close' 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') { if (process.platform === 'darwin') {
tpl.unshift({ tpl.unshift({
label: appName, label: appName,
@ -180,6 +318,13 @@ if (process.platform === 'darwin') {
{ {
type: 'separator' type: 'separator'
}, },
{
label: 'Preferences',
submenu: preferences
},
{
type: 'separator'
},
{ {
label: 'Services', label: 'Services',
role: 'services', role: 'services',
@ -218,6 +363,13 @@ if (process.platform === 'darwin') {
tpl.unshift({ tpl.unshift({
label: 'File', label: 'File',
submenu: [ submenu: [
{
label: 'Preferences',
submenu: preferences
},
{
type: 'separator'
},
{ {
label: `Quit ${appName}`, label: `Quit ${appName}`,
accelerator: 'Cmd+Q', accelerator: 'Cmd+Q',
@ -240,4 +392,5 @@ if (process.platform === 'darwin') {
tpl[tpl.length - 1].submenu = helpSubmenu; tpl[tpl.length - 1].submenu = helpSubmenu;
module.exports = electron.Menu.buildFromTemplate(tpl); var menu = electron.Menu.buildFromTemplate(tpl);
module.exports = menu;

4
electron/tray.js

@ -7,7 +7,7 @@ const Tray = electron.Tray;
const MenuItem = electron.MenuItem; const MenuItem = electron.MenuItem;
var appIcon = null; var appIcon = null;
exports.create = win => { exports.create = function(win, mainWindowState) {
if (process.platform === 'darwin' || appIcon) { if (process.platform === 'darwin' || appIcon) {
return; return;
} }
@ -45,7 +45,7 @@ exports.create = win => {
appIcon.setToolTip('Rambox'); appIcon.setToolTip('Rambox');
appIcon.setContextMenu(contextMenu); appIcon.setContextMenu(contextMenu);
appIcon.on('double-click', () => { appIcon.on('double-click', () => {
win.show(); mainWindowState.isMaximized ? win.maximize() : win.show();
}); });
}; };

1
package.json

@ -106,6 +106,7 @@
} }
}, },
"dependencies": { "dependencies": {
"auto-launch": "^2.1.0",
"electron-window-state": "^3.0.3", "electron-window-state": "^3.0.3",
"firebase": "^3.0.5", "firebase": "^3.0.5",
"firebase-token-generator": "^2.0.0" "firebase-token-generator": "^2.0.0"

Loading…
Cancel
Save