fix broken second instance cli args

fixes #1387
pull/1413/head
Mikael Finstad 4 years ago
parent 58a68e58a5
commit fdcfac27ce
No known key found for this signature in database
GPG Key ID: 25AB36E3E81CBC26

@ -149,26 +149,29 @@ function parseCliArgs(rawArgv = process.argv) {
const argv = parseCliArgs(); const argv = parseCliArgs();
function safeRequestSingleInstanceLock() { function safeRequestSingleInstanceLock(additionalData) {
if (process.mas) return true; // todo remove when fixed https://github.com/electron/electron/issues/35540 if (process.mas) return true; // todo remove when fixed https://github.com/electron/electron/issues/35540
return app.requestSingleInstanceLock();
// using additionalData because the built in "argv" passing is a bit broken:
// https://github.com/electron/electron/issues/20322
return app.requestSingleInstanceLock(additionalData);
} }
if (!argv.allowMultipleInstances && !safeRequestSingleInstanceLock()) { if (!argv.allowMultipleInstances && !safeRequestSingleInstanceLock({ argv: process.argv })) {
app.quit(); app.quit();
} else { } else {
// On macOS, the system enforces single instance automatically when users try to open a second instance of your app in Finder, and the open-file and open-url events will be emitted for that. // On macOS, the system enforces single instance automatically when users try to open a second instance of your app in Finder, and the open-file and open-url events will be emitted for that.
// However when users start your app in command line, the system's single instance mechanism will be bypassed, and you have to use this method to ensure single instance. // However when users start your app in command line, the system's single instance mechanism will be bypassed, and you have to use this method to ensure single instance.
// This can be tested with one terminal: npx electron . // This can be tested with one terminal: npx electron .
// and another terminal: npx electron . path/to/file.mp4 // and another terminal: npx electron . path/to/file.mp4
app.on('second-instance', (event, commandLine) => { app.on('second-instance', (event, commandLine, workingDirectory, additionalData) => {
// Someone tried to run a second instance, we should focus our window. // Someone tried to run a second instance, we should focus our window.
if (mainWindow) { if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore(); if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus(); mainWindow.focus();
} }
const argv2 = parseCliArgs(commandLine); const argv2 = parseCliArgs(additionalData.argv);
if (argv2._) openFilesEventually(argv2._); if (argv2._) openFilesEventually(argv2._);
}); });

Loading…
Cancel
Save