Skip to content

Commit 5c2dabc

Browse files
committed
Prevent doublestarting the app (Windooze)
1 parent 6c2cf4b commit 5c2dabc

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

main.js

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const http = require('http');
55
const xml = require("xml2js");
66
const net = require('net');
77

8+
const gotTheLock = app.requestSingleInstanceLock();
9+
810
let powerSaveBlockerId;
911
let tray;
1012
let s_mainWindow;
@@ -199,14 +201,27 @@ app.on('will-quit', () => {
199201
powerSaveBlocker.stop(powerSaveBlockerId);
200202
});
201203

202-
app.whenReady().then(() => {
203-
powerSaveBlockerId = powerSaveBlocker.start('prevent-app-suspension');
204-
s_mainWindow=createWindow();
205-
createAdvancedWindow(s_mainWindow);
206-
globalShortcut.register('Control+Shift+I', () => { return false; });
207-
app.on('activate', function () {
208-
if (BrowserWindow.getAllWindows().length === 0) createWindow()
204+
if (!gotTheLock) {
205+
app.quit();
206+
} else {
207+
app.on('second-instance', (event, commandLine, workingDirectory) => {
208+
// This event is emitted when a second instance is started
209+
// You can focus your app window here instead of starting a new one
210+
if (mainWindow) {
211+
if (mainWindow.isMinimized()) mainWindow.restore();
212+
mainWindow.focus();
213+
}
209214
});
215+
216+
217+
app.whenReady().then(() => {
218+
powerSaveBlockerId = powerSaveBlocker.start('prevent-app-suspension');
219+
s_mainWindow=createWindow();
220+
createAdvancedWindow(s_mainWindow);
221+
globalShortcut.register('Control+Shift+I', () => { return false; });
222+
app.on('activate', function () {
223+
if (BrowserWindow.getAllWindows().length === 0) createWindow()
224+
});
210225
s_mainWindow.webContents.once('dom-ready', function() {
211226
if (msgbacklog.length>0) {
212227
s_mainWindow.webContents.send('updateMsg',msgbacklog.pop());
@@ -216,36 +231,37 @@ app.whenReady().then(() => {
216231
// Create the tray icon
217232
const path = require('path');
218233
const iconPath = path.join(__dirname, 'icon1616.png');
219-
tray = new Tray(iconPath);
234+
tray = new Tray(iconPath);
220235

221236
const contextMenu = Menu.buildFromTemplate([
222237
{ label: 'Show App', click: () => s_mainWindow.show() },
223-
{ label: 'Quit', click: () => {
238+
{ label: 'Quit', click: () => {
224239
console.log("Exiting");
225240
app.isQuitting = true;
226241
app.quit();
227242
}
228243
},
229244
]);
230245

231-
tray.setContextMenu(contextMenu);
232-
tray.setToolTip(require('./package.json').name + " V" + require('./package.json').version);
246+
tray.setContextMenu(contextMenu);
247+
tray.setToolTip(require('./package.json').name + " V" + require('./package.json').version);
233248

234-
s_mainWindow.on('minimize', (event) => {
235-
event.preventDefault();
236-
s_mainWindow.hide(); // Hides the window instead of minimizing it to the taskbar
237-
});
249+
s_mainWindow.on('minimize', (event) => {
250+
event.preventDefault();
251+
s_mainWindow.hide(); // Hides the window instead of minimizing it to the taskbar
252+
});
238253

239-
s_mainWindow.on('close', (event) => {
240-
if (!app.isQuitting) {
241-
event.preventDefault();
242-
s_mainWindow.hide();
243-
}
244-
});
245-
if (process.platform === 'darwin') {
246-
app.dock.hide();
254+
s_mainWindow.on('close', (event) => {
255+
if (!app.isQuitting) {
256+
event.preventDefault();
257+
s_mainWindow.hide();
247258
}
248-
})
259+
});
260+
if (process.platform === 'darwin') {
261+
app.dock.hide();
262+
}
263+
})
264+
}
249265

250266
app.on('window-all-closed', function () {
251267
if (process.platform !== 'darwin') app.quit();

0 commit comments

Comments
 (0)