diff --git a/ElectronNET.API/App.cs b/ElectronNET.API/App.cs index 61254b9..b14ccb8 100644 --- a/ElectronNET.API/App.cs +++ b/ElectronNET.API/App.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; using System; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -31,7 +32,7 @@ namespace ElectronNET.API { BridgeConnector.Socket.On("app-window-all-closed" + GetHashCode(), () => { - if (!Electron.WindowManager.IsQuitOnWindowAllClosed) + if (!Electron.WindowManager.IsQuitOnWindowAllClosed || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { _windowAllClosed(); } diff --git a/ElectronNET.Host/api/app.js b/ElectronNET.Host/api/app.js index 1f02358..6dd3ab5 100644 --- a/ElectronNET.Host/api/app.js +++ b/ElectronNET.Host/api/app.js @@ -1,23 +1,28 @@ "use strict"; let isQuitWindowAllClosed = true, electronSocket; +let appWindowAllClosedEventId; module.exports = (socket, app) => { electronSocket = socket; - // Quit when all windows are closed. + // By default, quit when all windows are closed app.on('window-all-closed', () => { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q - if (process.platform !== 'darwin' && - isQuitWindowAllClosed) { + if (process.platform !== 'darwin' && isQuitWindowAllClosed) { app.quit(); } + else if (appWindowAllClosedEventId) { + // If the user is on macOS + // - OR - + // If the user has indicated NOT to quit when all windows are closed, + // emit the event. + electronSocket.emit('app-window-all-closed' + appWindowAllClosedEventId); + } }); socket.on('quit-app-window-all-closed-event', (quit) => { isQuitWindowAllClosed = quit; }); socket.on('register-app-window-all-closed-event', (id) => { - app.on('window-all-closed', () => { - electronSocket.emit('app-window-all-closed' + id); - }); + appWindowAllClosedEventId = id; }); socket.on('register-app-before-quit-event', (id) => { app.on('before-quit', (event) => { diff --git a/ElectronNET.Host/api/app.ts b/ElectronNET.Host/api/app.ts index ccf3c03..c5710fa 100644 --- a/ElectronNET.Host/api/app.ts +++ b/ElectronNET.Host/api/app.ts @@ -1,13 +1,20 @@ let isQuitWindowAllClosed = true, electronSocket; +let appWindowAllClosedEventId; export = (socket: SocketIO.Socket, app: Electron.App) => { electronSocket = socket; - // Quit when all windows are closed. + + // By default, quit when all windows are closed app.on('window-all-closed', () => { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q - if (process.platform !== 'darwin' && - isQuitWindowAllClosed) { + if (process.platform !== 'darwin' && isQuitWindowAllClosed) { app.quit(); + } else if (appWindowAllClosedEventId) { + // If the user is on macOS + // - OR - + // If the user has indicated NOT to quit when all windows are closed, + // emit the event. + electronSocket.emit('app-window-all-closed' + appWindowAllClosedEventId); } }); @@ -16,9 +23,7 @@ export = (socket: SocketIO.Socket, app: Electron.App) => { }); socket.on('register-app-window-all-closed-event', (id) => { - app.on('window-all-closed', () => { - electronSocket.emit('app-window-all-closed' + id); - }); + appWindowAllClosedEventId = id; }); socket.on('register-app-before-quit-event', (id) => { diff --git a/ElectronNET.Host/main.js b/ElectronNET.Host/main.js index 60b4037..2db6bb1 100644 --- a/ElectronNET.Host/main.js +++ b/ElectronNET.Host/main.js @@ -62,6 +62,11 @@ app.on('ready', () => { }); +app.on('quit', async (event, exitCode) => { + await server.close(); + apiProcess.kill(); +}); + function isSplashScreenEnabled() { if (manifestJsonFile.hasOwnProperty('splashscreen')) { if (manifestJsonFile.splashscreen.hasOwnProperty('imageFile')) {