keeweb/electron/app.js

143 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-10-21 22:32:02 +02:00
'use strict';
2015-10-25 08:29:31 +01:00
/* jshint node:true */
/* jshint browser:false */
2015-10-21 22:32:02 +02:00
var app = require('app'),
path = require('path'),
2015-11-22 21:18:05 +01:00
BrowserWindow = require('browser-window'),
2015-11-21 08:29:49 +01:00
Menu = require('menu'),
Tray = require('tray');
2015-10-21 22:32:02 +02:00
2015-10-24 21:06:44 +02:00
var mainWindow = null,
appIcon = null,
2015-10-25 08:29:31 +01:00
openFile = process.argv.filter(function(arg) { return /\.kdbx$/i.test(arg); })[0],
2015-10-25 20:26:33 +01:00
ready = false,
2015-11-14 12:09:36 +01:00
restartPending = false,
htmlPath = path.join(__dirname, 'index.html');
2015-10-25 20:26:33 +01:00
process.argv.forEach(function(arg) {
if (arg.lastIndexOf('--htmlpath=', 0) === 0) {
htmlPath = path.resolve(arg.replace('--htmlpath=', ''), 'index.html');
}
});
2015-10-21 22:32:02 +02:00
2015-11-14 12:09:36 +01:00
app.on('window-all-closed', function() {
2015-11-22 21:18:05 +01:00
if (restartPending) {
// unbind all handlers, load new app.js module and pass control to it
app.removeAllListeners('window-all-closed');
app.removeAllListeners('ready');
app.removeAllListeners('open-file');
var userDataAppFile = path.join(app.getPath('userData'), 'app.js');
delete require.cache[require.resolve('./app.js')];
require(userDataAppFile);
app.emit('ready');
} else {
app.quit();
}
2015-11-14 12:09:36 +01:00
});
2015-10-21 22:32:02 +02:00
app.on('ready', function() {
mainWindow = new BrowserWindow({
2015-10-24 21:06:44 +02:00
show: false,
2015-10-21 23:10:36 +02:00
width: 1000, height: 700, 'min-width': 600, 'min-height': 300,
2015-10-21 22:32:02 +02:00
icon: path.join(__dirname, 'icon.png')
});
2015-11-02 18:35:56 +01:00
setMenu();
mainWindow.loadUrl('file://' + htmlPath);
2015-10-24 21:06:44 +02:00
mainWindow.webContents.on('dom-ready', function() {
2015-10-25 20:26:33 +01:00
setTimeout(function() {
mainWindow.show();
2015-10-25 20:26:33 +01:00
ready = true;
notifyOpenFile();
}, 50);
2015-10-24 21:06:44 +02:00
});
2015-11-14 12:09:36 +01:00
mainWindow.on('closed', function() {
mainWindow = null;
});
2015-10-21 22:32:02 +02:00
});
2015-10-24 21:06:44 +02:00
app.on('open-file', function(e, path) {
e.preventDefault();
2015-10-25 08:53:05 +01:00
openFile = path;
2015-10-24 21:06:44 +02:00
notifyOpenFile();
});
2015-11-22 21:18:05 +01:00
app.restartApp = function() {
2015-11-14 12:09:36 +01:00
restartPending = true;
2015-11-22 21:18:05 +01:00
mainWindow.close();
2015-11-14 12:09:36 +01:00
setTimeout(function() { restartPending = false; }, 1000);
};
2015-11-15 21:08:26 +01:00
app.openWindow = function(opts) {
return new BrowserWindow(opts);
};
2015-11-21 08:29:49 +01:00
app.minimizeApp = function() {
if (process.platform === 'win32') {
mainWindow.minimize();
mainWindow.setSkipTaskbar(true);
appIcon = new Tray(path.join(__dirname, 'icon.png'));
appIcon.on('clicked', restoreMainWindow);
var contextMenu = Menu.buildFromTemplate([
{ label: 'Open KeeWeb', click: restoreMainWindow },
{ label: 'Quit KeeWeb', click: closeMainWindow }
]);
appIcon.setContextMenu(contextMenu);
2015-11-21 08:29:49 +01:00
appIcon.setToolTip('KeeWeb');
}
};
2015-10-24 21:06:44 +02:00
function restoreMainWindow() {
appIcon.destroy();
appIcon = null;
mainWindow.restore();
mainWindow.setSkipTaskbar(false);
}
function closeMainWindow() {
appIcon.destroy();
appIcon = null;
mainWindow.webContents.executeJavaScript('Backbone.trigger("launcher-exit-request");');
}
2015-11-02 18:35:56 +01:00
function setMenu() {
if (process.platform === 'darwin') {
var name = require('app').getName();
var template = [
{
label: name,
submenu: [
{ label: 'About ' + name, role: 'about' },
{ type: 'separator' },
{ label: 'Services', role: 'services', submenu: [] },
{ type: 'separator' },
{ label: 'Hide ' + name, accelerator: 'Command+H', role: 'hide' },
{ label: 'Hide Others', accelerator: 'Command+Shift+H', role: 'hideothers' },
{ label: 'Show All', role: 'unhide' },
{ type: 'separator' },
{ label: 'Quit', accelerator: 'Command+Q', click: function() { app.quit(); } }
]
},
{
label: 'Edit',
submenu: [
{ label: 'Undo', accelerator: 'CmdOrCtrl+Z', role: 'undo' },
{ label: 'Redo', accelerator: 'Shift+CmdOrCtrl+Z', role: 'redo' },
{ type: 'separator' },
{ label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' },
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', role: 'copy' },
{ label: 'Paste', accelerator: 'CmdOrCtrl+V', role: 'paste' },
{ label: 'Select All', accelerator: 'CmdOrCtrl+A', role: 'selectall' }
]
}
];
var menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
}
2015-10-24 21:06:44 +02:00
function notifyOpenFile() {
if (ready && openFile && mainWindow) {
2015-10-25 08:53:05 +01:00
openFile = openFile.replace(/"/g, '\\"').replace(/\\/g, '\\\\');
2015-10-24 21:06:44 +02:00
mainWindow.webContents.executeJavaScript('if (window.launcherOpen) { window.launcherOpen("' + openFile + '"); } ' +
' else { window.launcherOpenedFile="' + openFile + '"; }');
2015-10-25 08:53:05 +01:00
openFile = null;
2015-10-24 21:06:44 +02:00
}
}