keeweb/electron/app.js

296 lines
9.4 KiB
JavaScript
Raw Normal View History

2015-10-21 22:32:02 +02:00
'use strict';
2016-05-13 14:07:46 +02:00
var electron = require('electron'),
app = electron.app,
2015-10-21 22:32:02 +02:00
path = require('path'),
2016-05-13 14:07:46 +02:00
fs = require('fs');
2015-10-21 22:32:02 +02:00
2015-10-24 21:06:44 +02:00
var mainWindow = null,
appIcon = null,
2016-07-17 13:30:38 +02:00
openFile = process.argv.filter(arg => /\.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'),
mainWindowPosition = {},
updateMainWindowPositionTimeout = null,
windowPositionFileName = path.join(app.getPath('userData'), 'window-position.json');
2015-10-25 20:26:33 +01:00
2016-07-17 13:30:38 +02:00
process.argv.forEach(arg => {
2016-07-16 18:39:52 +02:00
if (arg.lastIndexOf('--htmlpath=', 0) === 0) {
htmlPath = path.resolve(arg.replace('--htmlpath=', ''), 'index.html');
}
});
2015-10-21 22:32:02 +02:00
2016-07-17 13:30:38 +02:00
app.on('window-all-closed', () => {
2016-07-16 18:39:52 +02:00
if (restartPending) {
// unbind all handlers, load new app.js module and pass control to it
2016-05-13 14:07:46 +02:00
electron.globalShortcut.unregisterAll();
2016-07-16 18:39:52 +02:00
app.removeAllListeners('window-all-closed');
app.removeAllListeners('ready');
app.removeAllListeners('open-file');
app.removeAllListeners('activate');
var userDataAppFile = path.join(app.getPath('userData'), 'app.js');
delete require.cache[require.resolve('./app.js')];
require(userDataAppFile);
app.emit('ready');
} else {
2016-04-16 22:34:16 +02:00
if (process.platform !== 'darwin') {
2016-07-16 18:39:52 +02:00
app.quit();
2016-04-16 22:34:16 +02:00
}
2016-07-16 18:39:52 +02:00
}
});
2016-07-17 13:30:38 +02:00
app.on('ready', () => {
2016-07-16 18:39:52 +02:00
if (!checkSingleInstance()) {
setAppOptions();
createMainWindow();
setGlobalShortcuts();
}
});
2016-07-17 13:30:38 +02:00
app.on('open-file', (e, path) => {
2016-07-16 18:39:52 +02:00
e.preventDefault();
openFile = path;
notifyOpenFile();
});
2016-07-17 13:30:38 +02:00
app.on('activate', () => {
2016-07-16 18:39:52 +02:00
if (process.platform === 'darwin') {
if (!mainWindow) {
createMainWindow();
}
}
});
2016-07-17 13:30:38 +02:00
app.on('will-quit', () => {
2016-07-16 18:39:52 +02:00
electron.globalShortcut.unregisterAll();
});
app.restartApp = function () {
restartPending = true;
mainWindow.close();
2016-07-17 13:30:38 +02:00
setTimeout(() => {
2016-07-16 18:39:52 +02:00
restartPending = false;
}, 1000);
};
app.openWindow = function (opts) {
return new electron.BrowserWindow(opts);
};
app.minimizeApp = function () {
if (process.platform !== 'darwin') {
mainWindow.minimize();
mainWindow.setSkipTaskbar(true);
appIcon = new electron.Tray(path.join(__dirname, 'icon.png'));
appIcon.on('click', restoreMainWindow);
var contextMenu = electron.Menu.buildFromTemplate([
{label: 'Open KeeWeb', click: restoreMainWindow},
{label: 'Quit KeeWeb', click: closeMainWindow}
]);
appIcon.setContextMenu(contextMenu);
appIcon.setToolTip('KeeWeb');
}
};
app.getMainWindow = function () {
return mainWindow;
};
2016-07-24 19:11:25 +02:00
app.emitBackboneEvent = emitBackboneEvent;
2015-10-24 21:06:44 +02:00
function checkSingleInstance() {
2016-07-17 13:30:38 +02:00
var shouldQuit = app.makeSingleInstance((/* commandLine, workingDirectory */) => {
restoreMainWindow();
});
if (shouldQuit) {
app.quit();
}
return shouldQuit;
}
2016-04-10 09:37:55 +02:00
function setAppOptions() {
app.commandLine.appendSwitch('disable-background-timer-throttling');
}
function createMainWindow() {
2016-05-13 14:07:46 +02:00
mainWindow = new electron.BrowserWindow({
show: false,
2016-07-24 23:53:51 +02:00
width: 1000, height: 700, minWidth: 700, minHeight: 400,
2016-07-24 22:58:21 +02:00
icon: path.join(__dirname, 'icon.png'),
webPreferences: {
backgroundThrottling: false
}
});
setMenu();
2016-01-11 19:01:12 +01:00
mainWindow.loadURL('file://' + htmlPath);
2016-07-24 19:11:25 +02:00
mainWindow.setContentProtection(true);
2016-07-17 13:30:38 +02:00
mainWindow.webContents.on('dom-ready', () => {
setTimeout(() => {
mainWindow.show();
ready = true;
notifyOpenFile();
}, 50);
});
mainWindow.on('resize', delaySaveMainWindowPosition);
mainWindow.on('move', delaySaveMainWindowPosition);
mainWindow.on('close', updateMainWindowPositionIfPending);
2016-07-24 23:10:03 +02:00
mainWindow.on('blur', mainWindowBlur);
2016-07-17 13:30:38 +02:00
mainWindow.on('closed', () => {
mainWindow = null;
saveMainWindowPosition();
});
2016-07-17 13:30:38 +02:00
mainWindow.on('minimize', () => {
emitBackboneEvent('launcher-minimize');
});
restoreMainWindowPosition();
}
function restoreMainWindow() {
destroyAppIcon();
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.setSkipTaskbar(false);
mainWindow.focus();
}
function closeMainWindow() {
destroyAppIcon();
emitBackboneEvent('launcher-exit-request');
}
function destroyAppIcon() {
if (appIcon) {
appIcon.destroy();
appIcon = null;
}
}
function delaySaveMainWindowPosition() {
if (updateMainWindowPositionTimeout) {
clearTimeout(updateMainWindowPositionTimeout);
}
updateMainWindowPositionTimeout = setTimeout(updateMainWindowPosition, 500);
}
function updateMainWindowPositionIfPending() {
if (updateMainWindowPositionTimeout) {
clearTimeout(updateMainWindowPositionTimeout);
updateMainWindowPosition();
}
}
function updateMainWindowPosition() {
if (!mainWindow) {
return;
}
updateMainWindowPositionTimeout = null;
var bounds = mainWindow.getBounds();
if (!mainWindow.isMaximized() && !mainWindow.isMinimized() && !mainWindow.isFullScreen()) {
mainWindowPosition.x = bounds.x;
mainWindowPosition.y = bounds.y;
mainWindowPosition.width = bounds.width;
mainWindowPosition.height = bounds.height;
}
mainWindowPosition.maximized = mainWindow.isMaximized();
mainWindowPosition.fullScreen = mainWindow.isFullScreen();
2016-05-13 14:07:46 +02:00
mainWindowPosition.displayBounds = require('electron').screen.getDisplayMatching(bounds).bounds;
mainWindowPosition.changed = true;
}
function saveMainWindowPosition() {
if (!mainWindowPosition.changed) {
return;
}
delete mainWindowPosition.changed;
try {
fs.writeFileSync(windowPositionFileName, JSON.stringify(mainWindowPosition), 'utf8');
} catch (e) {}
}
function restoreMainWindowPosition() {
2016-07-17 13:30:38 +02:00
fs.readFile(windowPositionFileName, 'utf8', (e, data) => {
if (data) {
mainWindowPosition = JSON.parse(data);
2016-02-14 14:24:49 +01:00
if (mainWindow && mainWindowPosition) {
if (mainWindowPosition.width && mainWindowPosition.height) {
2016-05-13 14:07:46 +02:00
var displayBounds = require('electron').screen.getDisplayMatching(mainWindowPosition).bounds;
var db = mainWindowPosition.displayBounds;
if (displayBounds.x === db.x && displayBounds.y === db.y &&
displayBounds.width === db.width && displayBounds.height === db.height) {
mainWindow.setBounds(mainWindowPosition);
}
}
if (mainWindowPosition.maximized) { mainWindow.maximize(); }
if (mainWindowPosition.fullScreen) { mainWindow.setFullScreen(true); }
}
}
});
}
2016-07-24 23:10:03 +02:00
function mainWindowBlur() {
emitBackboneEvent('main-window-blur');
}
2016-07-24 19:11:25 +02:00
function emitBackboneEvent(e, arg) {
arg = JSON.stringify(arg);
mainWindow.webContents.executeJavaScript(`Backbone.trigger('${e}', ${arg});`);
}
2015-11-02 18:35:56 +01:00
function setMenu() {
if (process.platform === 'darwin') {
2016-05-13 14:07:46 +02:00
var name = require('electron').app.getName();
2015-11-02 18:35:56 +01:00
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' }
]
}
];
2016-05-13 14:07:46 +02:00
var menu = electron.Menu.buildFromTemplate(template);
electron.Menu.setApplicationMenu(menu);
2015-11-02 18:35:56 +01:00
}
}
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
}
}
function setGlobalShortcuts() {
var shortcutModifiers = process.platform === 'darwin' ? 'Ctrl+Alt+' : 'Shift+Alt+';
var shortcuts = {
C: 'copy-password',
B: 'copy-user',
2016-04-10 09:02:32 +02:00
U: 'copy-url',
T: 'auto-type'
};
2016-07-17 13:30:38 +02:00
Object.keys(shortcuts).forEach(key => {
var shortcut = shortcutModifiers + key;
var eventName = shortcuts[key];
try {
2016-07-17 13:30:38 +02:00
electron.globalShortcut.register(shortcut, () => {
emitBackboneEvent(eventName);
});
} catch (e) {}
});
}