From c2904f3f689f72f8f68c38a4769c1fd0e88f02fb Mon Sep 17 00:00:00 2001 From: rafael-aero Date: Wed, 15 Sep 2021 10:58:43 +0200 Subject: [PATCH] Change second instance activation to raise event instead of trying to blindly open any window --- ElectronNET.API/App.cs | 31 +++++++++++++++++++++++++++++++ ElectronNET.Host/api/tray.ts | 30 +++++++++++++++--------------- ElectronNET.Host/main.js | 35 +++++++++++++++++++++-------------- 3 files changed, 67 insertions(+), 29 deletions(-) diff --git a/ElectronNET.API/App.cs b/ElectronNET.API/App.cs index e133c5c..eff18e4 100644 --- a/ElectronNET.API/App.cs +++ b/ElectronNET.API/App.cs @@ -60,6 +60,37 @@ namespace ElectronNET.API private event Action _appActivate; + /// + /// Emitted on the first instance when the user opens a second instance of the app, and the app is single instance + /// + /// + public event Action ActivateFromSecondInstance + { + add + { + if (_appActivateFromSecondInstance == null) + { + BridgeConnector.On("app-activate-from-second-instance", (args) => + { + _appActivateFromSecondInstance(args); + }); + } + _appActivateFromSecondInstance += value; + } + remove + { + _appActivateFromSecondInstance -= value; + + if (_appActivateFromSecondInstance == null) + { + BridgeConnector.Off("app-activate-from-second-instance"); + } + } + } + + private event Action _appActivateFromSecondInstance; + + /// /// Emitted when all windows have been closed. /// diff --git a/ElectronNET.Host/api/tray.ts b/ElectronNET.Host/api/tray.ts index af9f6cf..72d0c21 100644 --- a/ElectronNET.Host/api/tray.ts +++ b/ElectronNET.Host/api/tray.ts @@ -6,7 +6,7 @@ let electronSocket; export = (socket: Socket) => { electronSocket = socket; socket.on('register-tray-click', (id) => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { tray.value.on('click', (event, bounds) => { electronSocket.emit('tray-click-event' + id, [(event).__proto__, bounds]); }); @@ -14,7 +14,7 @@ export = (socket: Socket) => { }); socket.on('register-tray-right-click', (id) => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { tray.value.on('right-click', (event, bounds) => { electronSocket.emit('tray-right-click-event' + id, [(event).__proto__, bounds]); }); @@ -22,7 +22,7 @@ export = (socket: Socket) => { }); socket.on('register-tray-double-click', (id) => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { tray.value.on('double-click', (event, bounds) => { electronSocket.emit('tray-double-click-event' + id, [(event).__proto__, bounds]); }); @@ -30,7 +30,7 @@ export = (socket: Socket) => { }); socket.on('register-tray-balloon-show', (id) => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { tray.value.on('balloon-show', () => { electronSocket.emit('tray-balloon-show-event' + id); }); @@ -38,7 +38,7 @@ export = (socket: Socket) => { }); socket.on('register-tray-balloon-click', (id) => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { tray.value.on('balloon-click', () => { electronSocket.emit('tray-balloon-click-event' + id); }); @@ -46,7 +46,7 @@ export = (socket: Socket) => { }); socket.on('register-tray-balloon-closed', (id) => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { tray.value.on('balloon-closed', () => { electronSocket.emit('tray-balloon-closed-event' + id); }); @@ -69,51 +69,51 @@ export = (socket: Socket) => { }); socket.on('tray-destroy', () => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { tray.value.destroy(); } }); socket.on('tray-setImage', (image) => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { tray.value.setImage(image); } }); socket.on('tray-setPressedImage', (image) => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { const img = nativeImage.createFromPath(image); tray.value.setPressedImage(img); } }); socket.on('tray-setToolTip', (toolTip) => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { tray.value.setToolTip(toolTip); } }); socket.on('tray-setTitle', (title) => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { tray.value.setTitle(title); } }); socket.on('tray-displayBalloon', (options) => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { tray.value.displayBalloon(options); } }); socket.on('tray-isDestroyed', () => { - if (tray.value) { + if (tray.value && !tray.value.isDestroyed()) { const isDestroyed = tray.value.isDestroyed(); electronSocket.emit('tray-isDestroyedCompleted', isDestroyed); } }); socket.on('register-tray-on-event', (eventName, listenerName) => { - if (tray.value){ + if (tray.value && !tray.value.isDestroyed()){ tray.value.on(eventName, (...args) => { if (args.length > 1) { electronSocket.emit(listenerName, args[1]); @@ -125,7 +125,7 @@ export = (socket: Socket) => { }); socket.on('register-tray-once-event', (eventName, listenerName) => { - if (tray.value){ + if (tray.value && !tray.value.isDestroyed()){ tray.value.once(eventName, (...args) => { if (args.length > 1) { electronSocket.emit(listenerName, args[1]); diff --git a/ElectronNET.Host/main.js b/ElectronNET.Host/main.js index 201d534..ae255b3 100644 --- a/ElectronNET.Host/main.js +++ b/ElectronNET.Host/main.js @@ -57,23 +57,30 @@ const manifestJsonFile = require(manifestJsonFilePath); if (manifestJsonFile.singleInstance || manifestJsonFile.aspCoreBackendPort) { const mainInstance = app.requestSingleInstanceLock(); app.on('second-instance', (events, args = []) => { - args.forEach(parameter => { - const words = parameter.split('='); - if(words.length > 1) { - app.commandLine.appendSwitch(words[0].replace('--', ''), words[1]); - } else { - app.commandLine.appendSwitch(words[0].replace('--', '')); - } - }); + let socket = global['electronsocket']; - const windows = BrowserWindow.getAllWindows(); - if (windows.length) { - if (windows[0].isMinimized()) { - windows[0].restore(); - } - windows[0].focus(); + if (socket) { + socket.emit('app-activate-from-second-instance', args); } + + //args.forEach(parameter => { + // const words = parameter.split('='); + + // if(words.length > 1) { + // app.commandLine.appendSwitch(words[0].replace('--', ''), words[1]); + // } else { + // app.commandLine.appendSwitch(words[0].replace('--', '')); + // } + //}); + + //const windows = BrowserWindow.getAllWindows(); + //if (windows.length) { + // if (windows[0].isMinimized()) { + // windows[0].restore(); + // } + // windows[0].focus(); + //} }); if (!mainInstance) {