Move --squirrel-xxxx handling to new file

This commit is contained in:
Feross Aboukhadijeh
2016-03-25 00:26:48 -07:00
parent 27f9bf1cc6
commit 212cd23c4f
2 changed files with 132 additions and 95 deletions

View File

@@ -9,73 +9,47 @@ var ipc = require('./ipc')
var log = require('./log')
var menu = require('./menu')
var shortcuts = require('./shortcuts')
var squirrelWin32 = require('./squirrel-win32')
var windows = require('./windows')
var shouldQuit = false
var argv = sliceArgv(process.argv)
if (process.platform === 'win32') {
var squirrelCmd = argv[0]
if (squirrelCmd === '--squirrel-install' || squirrelCmd === '--squirrel-updated') {
handlers.init()
shouldQuit = squirrelWin32.handleArgv(argv[0])
// TODO:
// - Install desktop and start menu shortcuts
// - Add explorer context menus
// Always quit when done
app.quit()
}
if (squirrelCmd === '--squirrel-uninstall') {
// Undo anything we did in the --squirrel-install and --squirrel-updated handlers
// TODO: implement this
// Always quit when done
app.quit()
}
if (squirrelCmd === '--squirrel-obsolete') {
// This is called on the outgoing version of your app before we update to the new
// version - it's the opposite of --squirrel-updated
// Always quit when done
app.quit()
}
if (squirrelCmd === '--squirrel-firstrun') {
// Remove any --squirrel-xxxx arguments
argv.shift()
}
}
// Prevent multiple instances of the app from running at the same time. New instances
// signal this instance and exit.
var shouldQuit = app.makeSingleInstance(function (newArgv) {
newArgv = sliceArgv(newArgv)
if (app.ipcReady) {
log('Second app instance attempted to open but was prevented')
windows.focusMainWindow()
newArgv.forEach(function (torrentId) {
windows.main.send('dispatch', 'onOpen', torrentId)
})
} else {
argv.push(...newArgv)
}
})
if (!shouldQuit) {
// Prevent multiple instances of app from running at same time. New instances signal
// this instance and quit.
shouldQuit = app.makeSingleInstance(onAppOpen)
if (shouldQuit) {
app.quit()
}
}
if (!shouldQuit) {
init()
}
function init () {
app.ipcReady = false // main window has finished loading and IPC is ready
app.isQuitting = false
// Open handlers must be added as early as possible
app.on('open-file', onOpen)
app.on('open-url', onOpen)
ipc.init()
app.on('will-finish-launching', function () {
autoUpdater.init()
setupCrashReporter()
})
app.ipcReady = false // main window has finished loading and IPC is ready
app.isQuitting = false
app.on('ready', function () {
menu.init()
windows.createMainWindow()
@@ -84,10 +58,7 @@ app.on('ready', function () {
})
app.on('ipcReady', function () {
log('IS_PRODUCTION:', config.IS_PRODUCTION)
if (argv.length) {
log('command line args:', process.argv)
}
log('Command line args:', argv)
argv.forEach(function (torrentId) {
windows.main.send('dispatch', 'onOpen', torrentId)
})
@@ -101,7 +72,7 @@ app.on('activate', function () {
if (windows.main) {
windows.main.show()
} else {
windows.createMainWindow(menu)
windows.createMainWindow()
}
})
@@ -110,17 +81,17 @@ app.on('window-all-closed', function () {
app.quit()
}
})
ipc.init()
}
function onOpen (e, torrentId) {
e.preventDefault()
if (app.ipcReady) {
windows.main.send('dispatch', 'onOpen', torrentId)
setTimeout(function () {
// Required for magnet links opened from Chrome otherwise the confirmation dialog
// that Chrome shows causes Chrome to steal back the focus.
// Magnet links opened from Chrome won't focus the app without a setTimeout. The
// confirmation dialog Chrome shows causes Chrome to steal back the focus.
// Electron issue: https://github.com/atom/electron/issues/4338
setTimeout(function () {
windows.focusMainWindow()
}, 100)
} else {
@@ -128,6 +99,21 @@ function onOpen (e, torrentId) {
}
}
function onAppOpen (newArgv) {
newArgv = sliceArgv(newArgv)
if (app.ipcReady) {
log('Second app instance opened, but was prevented:', newArgv)
windows.focusMainWindow()
newArgv.forEach(function (torrentId) {
windows.main.send('dispatch', 'onOpen', torrentId)
})
} else {
argv.push(...newArgv)
}
}
function sliceArgv (argv) {
return argv.slice(config.IS_PRODUCTION ? 1 : 2)
}

51
main/squirrel-win32.js Normal file
View File

@@ -0,0 +1,51 @@
module.exports = {
handleArgv
}
var electron = require('electron')
var app = electron.app
var handlers = require('./handlers')
function handleArgv (cmd) {
if (cmd === '--squirrel-install' || cmd === '--squirrel-updated') {
handlers.init()
// TODO:
// - Install desktop and start menu shortcuts
// - Add explorer context menus
// Always quit when done
app.quit()
return true
}
if (cmd === '--squirrel-uninstall') {
// Undo anything we did in the --squirrel-install and --squirrel-updated handlers
// TODO: implement this
// Always quit when done
app.quit()
return true
}
if (cmd === '--squirrel-obsolete') {
// This is called on the outgoing version of your app before we update to the new
// version - it's the opposite of --squirrel-updated
// Always quit when done
app.quit()
return true
}
if (cmd === '--squirrel-firstrun') {
// This is called on the first run of the app.
// Do not quit the app
return false
}
return false
}