Move --squirrel-xxxx handling to new file

This commit is contained in:
Feross Aboukhadijeh
2016-03-25 00:26:48 -07:00
parent 9d35ece954
commit bdf7110135
2 changed files with 132 additions and 95 deletions

View File

@@ -9,49 +9,101 @@ 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
// Remove any --squirrel-xxxx arguments
argv.shift()
}
// Always quit when done
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 (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') {
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) {
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.on('ready', function () {
menu.init()
windows.createMainWindow()
shortcuts.init()
if (process.platform !== 'win32') handlers.init()
})
app.on('ipcReady', function () {
log('Command line args:', argv)
argv.forEach(function (torrentId) {
windows.main.send('dispatch', 'onOpen', torrentId)
})
})
app.on('before-quit', function () {
app.isQuitting = true
})
app.on('activate', function () {
if (windows.main) {
windows.main.show()
} else {
windows.createMainWindow()
}
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
}
function onOpen (e, torrentId) {
e.preventDefault()
if (app.ipcReady) {
windows.main.send('dispatch', 'onOpen', torrentId)
// 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 {
argv.push(torrentId)
}
}
function onAppOpen (newArgv) {
newArgv = sliceArgv(newArgv)
if (app.ipcReady) {
log('Second app instance attempted to open but was prevented')
log('Second app instance opened, but was prevented:', newArgv)
windows.focusMainWindow()
newArgv.forEach(function (torrentId) {
@@ -60,72 +112,6 @@ var shouldQuit = app.makeSingleInstance(function (newArgv) {
} else {
argv.push(...newArgv)
}
})
if (shouldQuit) {
app.quit()
}
app.on('open-file', onOpen)
app.on('open-url', onOpen)
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()
shortcuts.init()
if (process.platform !== 'win32') handlers.init()
})
app.on('ipcReady', function () {
log('IS_PRODUCTION:', config.IS_PRODUCTION)
if (argv.length) {
log('command line args:', process.argv)
}
argv.forEach(function (torrentId) {
windows.main.send('dispatch', 'onOpen', torrentId)
})
})
app.on('before-quit', function () {
app.isQuitting = true
})
app.on('activate', function () {
if (windows.main) {
windows.main.show()
} else {
windows.createMainWindow(menu)
}
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
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.
// Electron issue: https://github.com/atom/electron/issues/4338
windows.focusMainWindow()
}, 100)
} else {
argv.push(torrentId)
}
}
function sliceArgv (argv) {

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
}