From f59a54782a712a2ea4b8a12b8e90068af035c7de Mon Sep 17 00:00:00 2001 From: Borewit Date: Mon, 17 Jun 2019 21:05:37 +0200 Subject: [PATCH] Fix processing arguments from second instance --- src/main/index.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/index.js b/src/main/index.js index 04ad2bdd..9014e0f6 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -16,6 +16,9 @@ const windows = require('./windows') let shouldQuit = false let argv = sliceArgv(process.argv) +// allow electron/chromium to play startup sounds (without user interaction) +app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required') + // Start the app without showing the main window when auto launching on login // (On Windows and Linux, we get a flag. On MacOS, we get special API.) const hidden = argv.includes('--hidden') || @@ -31,6 +34,12 @@ if (process.platform === 'win32') { const squirrelWin32 = require('./squirrel-win32') shouldQuit = squirrelWin32.handleEvent(argv[0]) argv = argv.filter((arg) => !arg.includes('--squirrel')) + + if (shouldQuit) { + app.quit() + } else { + app.on('second-instance', (event, commandLine) => onAppOpen(commandLine)) + } } if (!shouldQuit && !config.IS_PORTABLE) { @@ -39,11 +48,16 @@ if (!shouldQuit && !config.IS_PORTABLE) { // %APPDATA%\Roaming\WebTorrent so we do not do it for the Portable App since // we want to be "silent" as well as "portable". if (!app.requestSingleInstanceLock()) { - app.quit() shouldQuit = true } } +if (shouldQuit) { + app.quit() +} else { + app.on('second-instance', (event, commandLine, workingDirectory) => onAppOpen(commandLine)) +} + if (!shouldQuit) { init() } @@ -168,6 +182,19 @@ function onOpen (e, torrentId) { } } +function onAppOpen (newArgv) { + newArgv = sliceArgv(newArgv) + + if (app.ipcReady) { + log('Second app instance opened, but was prevented:', newArgv) + windows.main.show() + + processArgv(newArgv) + } else { + argv.push(...newArgv) + } +} + // Remove leading args. // Production: 1 arg, eg: /Applications/WebTorrent.app/Contents/MacOS/WebTorrent // Development: 2 args, eg: electron .