From fe5c52d1f55a9521c189c2ab456ddf55eb67576e Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 24 Mar 2016 02:54:41 -0700 Subject: [PATCH] Show, unminimize, and focus window after opening magnet link (fix #210) Requires a workaround for this Electron issue: https://github.com/atom/electron/issues/4338 --- main/index.js | 12 +++++++----- main/windows.js | 10 +++++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/main/index.js b/main/index.js index 34ab4729..cea72c7c 100644 --- a/main/index.js +++ b/main/index.js @@ -18,15 +18,11 @@ var shouldQuit = app.makeSingleInstance(function (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) }) - - if (windows.main.isMinimized()) { - windows.main.restore() - } - windows.main.focus() } else { argv.push(...newArgv) } @@ -89,6 +85,12 @@ 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) } diff --git a/main/windows.js b/main/windows.js index fa851ac6..c3541ec7 100644 --- a/main/windows.js +++ b/main/windows.js @@ -1,6 +1,7 @@ var windows = module.exports = { main: null, - createMainWindow: createMainWindow + createMainWindow: createMainWindow, + focusMainWindow: focusMainWindow } var electron = require('electron') @@ -52,3 +53,10 @@ function createMainWindow () { windows.main = null }) } + +function focusMainWindow () { + if (windows.main.isMinimized()) { + windows.main.restore() + } + windows.main.show() // shows and gives focus +}