Fix crash when launching a second instance using option --single-instance (Fixes #664)

This refactors the deprecated call to `app.makeSingleInstance()`.
See [electron / app.requestSingleInstanceLock()](https://electronjs.org/docs/all?q=app.requestSingleInstanceLock).
This commit is contained in:
Ying 2019-03-25 08:16:28 -07:00 committed by Ronan Jouchet
parent a27bb0fd35
commit 479611ecc4
1 changed files with 32 additions and 33 deletions

View File

@ -127,21 +127,41 @@ if (appArgs.crashReporter) {
});
}
app.on('ready', () => {
mainWindow = createMainWindow(appArgs, app.quit, setDockBadge);
createTrayIcon(appArgs, mainWindow);
// quit if singleInstance mode and there's already another instance running
const shouldQuit = appArgs.singleInstance && !app.requestSingleInstanceLock();
if (shouldQuit) {
app.quit();
} else {
app.on('second-instance', () => {
if (mainWindow) {
if (!mainWindow.isVisible()) {
// try
mainWindow.show();
}
if (mainWindow.isMinimized()) {
// minimized
mainWindow.restore();
}
mainWindow.focus();
}
});
// Register global shortcuts
if (appArgs.globalShortcuts) {
appArgs.globalShortcuts.forEach((shortcut) => {
globalShortcut.register(shortcut.key, () => {
shortcut.inputEvents.forEach((inputEvent) => {
mainWindow.webContents.sendInputEvent(inputEvent);
app.on('ready', () => {
mainWindow = createMainWindow(appArgs, app.quit, setDockBadge);
createTrayIcon(appArgs, mainWindow);
// Register global shortcuts
if (appArgs.globalShortcuts) {
appArgs.globalShortcuts.forEach((shortcut) => {
globalShortcut.register(shortcut.key, () => {
shortcut.inputEvents.forEach((inputEvent) => {
mainWindow.webContents.sendInputEvent(inputEvent);
});
});
});
});
}
});
}
});
}
app.on('new-window-for-tab', () => {
mainWindow.emit('new-tab');
@ -160,24 +180,3 @@ app.on('login', (event, webContents, request, authInfo, callback) => {
createLoginWindow(callback);
}
});
if (appArgs.singleInstance) {
const shouldQuit = app.makeSingleInstance(() => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (!mainWindow.isVisible()) {
// tray
mainWindow.show();
}
if (mainWindow.isMinimized()) {
// minimized
mainWindow.restore();
}
mainWindow.focus();
}
});
if (shouldQuit) {
app.quit();
}
}