fix: handle multiple instances

This commit is contained in:
cv
2021-06-06 14:52:08 +02:00
parent 1a80fab59d
commit d4ee386cde
+26 -16
View File
@@ -41,18 +41,6 @@ const nodePath =
// TODO: Icons appear pixelated // TODO: Icons appear pixelated
const trayIcon = path.join(__dirname, './assets/images/logos/LOGO-512.png'); const trayIcon = path.join(__dirname, './assets/images/logos/LOGO-512.png');
const appIcon = path.join(__dirname, './assets/images/logos/LOGO-512.png'); const appIcon = path.join(__dirname, './assets/images/logos/LOGO-512.png');
// Ensure there isn't another instance of the app running already
var lock = app.requestSingleInstanceLock();
if (!lock) {
dialog.showErrorBox(
'Multiple instances',
'Another instance is already running. Please close the other instance first.'
);
app.quit();
return;
}
function showNotification(text) { function showNotification(text) {
new Notification({ new Notification({
title: 'ontime', title: 'ontime',
@@ -64,6 +52,25 @@ let win;
let splash; let splash;
let tray = null; let tray = null;
// Ensure there isn't another instance of the app running already
const lock = app.requestSingleInstanceLock();
if (!lock) {
dialog.showErrorBox(
'Multiple instances',
'An instance if the App is already running.'
);
app.quit();
return;
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (win) {
if (win.isMinimized()) win.restore();
win.show();
}
});
}
function createWindow() { function createWindow() {
// create a new `splash`-Window // create a new `splash`-Window
splash = new BrowserWindow({ splash = new BrowserWindow({
@@ -150,9 +157,14 @@ app.whenReady().then(() => {
win.once('ready-to-show', () => { win.once('ready-to-show', () => {
setTimeout(() => { setTimeout(() => {
// window stuff
win.show(); win.show();
splash.destroy(); splash.destroy();
showNotification(loaded.toString()); showNotification(loaded.toString());
// tray stuff
// TODO: get IP Address
tray.setToolTip(loaded);
}, 2000); }, 2000);
}); });
@@ -185,10 +197,7 @@ app.whenReady().then(() => {
]; ];
const trayContextMenu = Menu.buildFromTemplate(trayMenuTemplate); const trayContextMenu = Menu.buildFromTemplate(trayMenuTemplate);
tray.setContextMenu(trayContextMenu); tray.setContextMenu(trayContextMenu);
// TODO: get IP Address
tray.setToolTip('ontime running on http://localhost:4001/');
}); });
// unregister shortcuts before quitting // unregister shortcuts before quitting
@@ -213,6 +222,7 @@ ipcMain.on('shutdown', (event, arg) => {
await shutdown(); await shutdown();
})(); })();
tray.remove();
win.destroy(); win.destroy();
app.quit(); app.quit();
}); });
@@ -227,7 +237,7 @@ ipcMain.on('set-window', (event, arg) => {
win.setPosition(0, 0); win.setPosition(0, 0);
} else if (arg === 'to-tray') { } else if (arg === 'to-tray') {
// window to tray // window to tray
win.close(); win.hide();
} }
}); });