1212import "core-js/stable" ;
1313import "regenerator-runtime/runtime" ;
1414import path from "path" ;
15- import { app , BrowserWindow , shell , dialog , Tray , Menu , clipboard , ipcMain } from "electron" ;
15+ import { app , BrowserWindow , shell , dialog , Tray , Menu , clipboard } from "electron" ;
1616import log from "electron-log" ;
1717import MenuBuilder from "./menu" ;
1818import {
@@ -24,7 +24,9 @@ import {
2424/** Storage - State */
2525import "./actions/initGlobalState" ;
2626import AutoUpdate from "../lib/autoupdate" ;
27- import { getReadyToQuitApp } from "./actions/cleanup" ;
27+ import { cleanupAndQuit } from "./actions/cleanup" ;
28+ import { trackEventViaWebApp } from "./actions/events" ;
29+ import EVENTS from "./actions/events/constants" ;
2830import fs from "fs" ;
2931import logger from "../utils/logger" ;
3032import { setupIPCForwardingToWebApp } from "./actions/setupIPCForwarding" ;
@@ -83,7 +85,7 @@ export default function createTrayMenu(ip?: string, port?: number) {
8385 {
8486 label : "Show Requestly" ,
8587 click : ( ) => {
86- if ( webAppWindow && ! webAppWindow . isDestroyed ( ) ) {
88+ if ( webAppWindow ) {
8789 if ( webAppWindow . isMinimized ( ) ) {
8890 webAppWindow . restore ( )
8991 }
@@ -149,7 +151,7 @@ export default function createTrayMenu(ip?: string, port?: number) {
149151 {
150152 label : "Quit" ,
151153 click : ( ) => {
152- webAppWindow ?. close ( ) ;
154+ app . quit ( ) ;
153155 } ,
154156 } ,
155157 ]
@@ -168,7 +170,7 @@ export default function createTrayMenu(ip?: string, port?: number) {
168170 tray . setContextMenu ( trayMenu ) ;
169171}
170172
171- let closingAccepted = false
173+
172174const createWindow = async ( ) => {
173175 if ( isDevelopment ) {
174176 await installExtensions ( ) ;
@@ -256,19 +258,63 @@ const createWindow = async () => {
256258 }
257259 } ) ;
258260
259- webAppWindow . on ( 'close' , async ( event ) => {
260- if ( ! closingAccepted ) {
261- event . preventDefault ( ) ;
262- webAppWindow ?. webContents . send ( "initiate-app-close" )
261+ // webAppWindow.on('closed', () => {
262+ // webAppWindow = null;
263+ // });
264+
265+ webAppWindow . on ( "close" , ( e ) => {
266+ // Check if user has already asked to Quit app from here or somewhere else
267+ // @ts -expect-error
268+ if ( global . isQuitActionConfirmed ) {
269+ saveCookies ( ) ;
270+ app . quit ( ) ;
271+ return ;
263272 }
264- } )
265273
266- webAppWindow . on ( 'closed' , async ( ) => {
267- saveCookies ( ) ;
268- await getReadyToQuitApp ( ) ;
269- webAppWindow = null ;
270- return ;
271- } )
274+ if ( webAppWindow ) {
275+ let message =
276+ "Do you really want to quit? This would also stop the proxy server." ;
277+
278+ // @ts -expect-error
279+ if ( global . quitAndInstall ) {
280+ message = "Confirm to restart & install update" ;
281+ // @ts -expect-error
282+ global . quitAndInstall = false ;
283+ }
284+
285+ const choice = dialog . showMessageBoxSync ( webAppWindow , {
286+ type : "question" ,
287+ buttons : [ "Yes, quit Requestly" , "Minimize instead" , "Cancel" ] ,
288+ title : "Quit Requestly" ,
289+ message : message ,
290+ } ) ;
291+
292+ switch ( choice ) {
293+ // If Quit is clicked
294+ case 0 :
295+ // Set flag to check next iteration
296+ trackEventViaWebApp ( webAppWindow , EVENTS . QUIT_APP )
297+ // @ts -expect-error
298+ global . isQuitActionConfirmed = true ;
299+ // Calling app.quit() would again invoke this function
300+ e . preventDefault ( ) ;
301+ cleanupAndQuit ( ) ;
302+ break ;
303+ // If Minimize is clicked
304+ case 1 :
305+ webAppWindow . minimize ( ) ;
306+ e . preventDefault ( ) ;
307+ break ;
308+ // If cancel is clicked
309+ case 2 :
310+ e . preventDefault ( ) ;
311+ break ;
312+ default :
313+ break ;
314+ }
315+ }
316+ } ) ;
317+
272318 const enableBGWindowDebug = ( ) => {
273319 // Show bg window and toggle the devtools
274320 try {
@@ -322,7 +368,7 @@ function handleCustomProtocolURL(urlString: string) {
322368
323369// custom protocol (requestly) handler
324370app . on ( "open-url" , ( _event , rqUrl ) => {
325- if ( webAppWindow && ! webAppWindow . isDestroyed ( ) ) {
371+ if ( webAppWindow ) {
326372 handleCustomProtocolURL ( rqUrl )
327373 } else {
328374 onWebAppReadyHandlers . push ( ( ) => handleCustomProtocolURL ( rqUrl ) )
@@ -355,7 +401,7 @@ async function handleFileOpen(filePath: string, webAppWindow?: BrowserWindow) {
355401
356402app . on ( 'open-file' , async ( event , filePath ) => {
357403 event . preventDefault ( ) ;
358- if ( webAppWindow && ! webAppWindow . isDestroyed ( ) ) {
404+ if ( webAppWindow ) {
359405 handleFileOpen ( filePath , webAppWindow ) ;
360406 } else {
361407 logger . log ( "webAppWindow not ready" )
438484 . catch ( ( err ) => {
439485 console . log ( err ) ;
440486 } ) ;
441-
442- ipcMain . handle ( "quit-app" , ( _event ) => {
443- closingAccepted = true
444- webAppWindow ?. close ( ) ;
445- } )
446-
447- app . on ( "before-quit" , ( ) => {
448- // cleanup when quitting has been finalised
449- ipcMain . removeAllListeners ( ) ;
450- webAppWindow ?. removeAllListeners ( ) ;
451- // @ts -expect-error BrowserWindow types are not being enforced for this variable
452- backgroundWindow ?. removeAllListeners ( ) ;
453-
454- ipcMain . removeAllListeners ( ) ;
455- process . on ( 'uncaughtException' , ( err ) => {
456- logger . error ( 'Unhandled Exception while quitting:' , err ) ;
457- } ) ;
458- process . on ( 'unhandledRejection' , ( err ) => {
459- logger . error ( 'Unhandled Rejection while quitting:' , err ) ;
460- } ) ;
461- } )
0 commit comments