From ae8b35e68561bac34a2066a049551a258a5bbc28 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Mon, 12 Feb 2018 14:04:50 +0300 Subject: [PATCH] feat(app): allow run only single application at same time --- src/background/background.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/background/background.js b/src/background/background.js index b2de677..9aa2f9e 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -48,6 +48,7 @@ const resourcesPath = env.name === "production" ? process.resourcesPath : 'resou const spiderCall = require('./spider') const appConfig = require('./config') +let mainWindow = undefined let sphinx = undefined let spider = undefined @@ -74,6 +75,22 @@ console.log('CPU Logic cores:', os.cpus().length) console.log('Total memory:', (os.totalmem() / (1024 * 1024)).toFixed(2), 'MB') console.log('Free memory:', (os.freemem() / (1024 * 1024)).toFixed(2), 'MB') + +const shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) { + // Someone tried to run a second instance, we should focus our window. + console.log('openned second application, just focus this one') + if (mainWindow) { + if (mainWindow.isMinimized()) + mainWindow.restore(); + mainWindow.focus(); + } +}); + +if (shouldQuit) { + console.log('closed because of second application') + app.exit(0); +} + const writeSphinxConfig = (path, dbPath) => { let config = ` index torrents @@ -241,7 +258,7 @@ app.on("ready", () => { startSphinx(() => { setApplicationMenu(); - const mainWindow = createWindow("main", { + mainWindow = createWindow("main", { width: 1000, height: 600 }); @@ -268,7 +285,10 @@ app.on("ready", () => { }) mainWindow.on('hide', () => { tray.setHighlightMode('never') - }) + }) + mainWindow.on('closed', () => { + mainWindow = undefined + }) mainWindow.on('minimize', (event) => { event.preventDefault();