diff --git a/src/main/cancelRun.ts b/src/main/cancelRun.ts index 04d82c6f..9b0bb985 100644 --- a/src/main/cancelRun.ts +++ b/src/main/cancelRun.ts @@ -42,6 +42,15 @@ export function stopRun( id: TrackedId ): void { console.log( `Killed RAxML process ${id}` ); } +export function hasRuns(): boolean { + return Object.keys( processes ).length > 0; +} + +// The app has a single window, so every tracked run belongs to it. +export function stopAllRuns(): void { + Object.keys( processes ).forEach( cancelRun ); +} + export function cancelRun( id: TrackedId ): void { const key = String( id ); generation[key] = ( generation[key] || 0 ) + 1; diff --git a/src/main/confirmClose.test.ts b/src/main/confirmClose.test.ts new file mode 100644 index 00000000..489260f9 --- /dev/null +++ b/src/main/confirmClose.test.ts @@ -0,0 +1,76 @@ +import { afterEach, expect, it, vi } from 'vitest'; +import { EventEmitter } from 'events'; +import type { App, BrowserWindow, Dialog } from 'electron'; +import { stopAllRuns, trackRun } from './cancelRun'; +import { confirmCloseWithRuns } from './confirmClose'; + +function setup( response: number ) { + const win = Object.assign( new EventEmitter(), { close: vi.fn() } ); + const app = Object.assign( new EventEmitter(), { quit: vi.fn() } ); + const choice = Promise.resolve( { response } ); + const dialog = { showMessageBox: vi.fn( () => choice ) }; + confirmCloseWithRuns( win as unknown as BrowserWindow, dialog as unknown as Dialog, app as unknown as App ); + const close = () => { + const event = { preventDefault: vi.fn() }; + win.emit( 'close', event ); + return event; + }; + return { win, app, dialog, choice, close }; +} + +function running() { + const proc = { kill: vi.fn() }; + trackRun( 5, proc ); + return proc; +} + +afterEach( () => stopAllRuns() ); + +it( 'closes with no dialog when no run is in progress', () => { + const { dialog, close } = setup( 1 ); + expect( close().preventDefault ).not.toHaveBeenCalled(); + expect( dialog.showMessageBox ).not.toHaveBeenCalled(); +} ); + +it( 'Cancel keeps the window open and the run untouched', async () => { + const proc = running(); + const { win, dialog, choice, close } = setup( 0 ); + expect( close().preventDefault ).toHaveBeenCalled(); + await choice; + expect( dialog.showMessageBox ).toHaveBeenCalledTimes( 1 ); + expect( proc.kill ).not.toHaveBeenCalled(); + expect( win.close ).not.toHaveBeenCalled(); +} ); + +it( 'Stop kills the run and closes the window', async () => { + const proc = running(); + const { win, app, choice, close } = setup( 1 ); + close(); + await choice; + expect( proc.kill ).toHaveBeenCalled(); + expect( win.close ).toHaveBeenCalled(); + expect( app.quit ).not.toHaveBeenCalled(); +} ); + +it( 'a quit while the dialog is open asks once and quits on Stop', async () => { + running(); + const { win, app, dialog, choice, close } = setup( 1 ); + close(); + app.emit( 'before-quit' ); + expect( close().preventDefault ).toHaveBeenCalled(); + await choice; + expect( dialog.showMessageBox ).toHaveBeenCalledTimes( 1 ); + expect( app.quit ).toHaveBeenCalled(); + expect( win.close ).not.toHaveBeenCalled(); +} ); + +it( 'Stop during a quit kills the run and quits again', async () => { + const proc = running(); + const { win, app, choice, close } = setup( 1 ); + app.emit( 'before-quit' ); + close(); + await choice; + expect( proc.kill ).toHaveBeenCalled(); + expect( app.quit ).toHaveBeenCalled(); + expect( win.close ).not.toHaveBeenCalled(); +} ); diff --git a/src/main/confirmClose.ts b/src/main/confirmClose.ts new file mode 100644 index 00000000..a227e98f --- /dev/null +++ b/src/main/confirmClose.ts @@ -0,0 +1,51 @@ +import type { App, BrowserWindow, Dialog, Event } from 'electron'; +import { hasRuns, stopAllRuns } from './cancelRun'; + +/** + * Asks before closing `win` (or quitting, which closes it) while a run is in progress. + * Stop kills the runs, then closes or quits; Cancel leaves window and runs as they were. + */ +const STOP = 1; + +export function confirmCloseWithRuns( win: BrowserWindow, dialog: Dialog, app: App ): void { + // A prevented close also cancels a quit; the flag remembers to quit again on Stop. + let quitting = false; + let asking = false; + const onBeforeQuit = (): void => { + quitting = true; + }; + app.on( 'before-quit', onBeforeQuit ); + win.once( 'closed', () => app.off( 'before-quit', onBeforeQuit ) ); + win.on( 'close', ( event: Event ) => { + if ( !hasRuns() ) { + return; + } + event.preventDefault(); + if ( asking ) { + return; + } + asking = true; + void dialog.showMessageBox( win, { + type: 'warning', + title: 'Run in progress', + message: 'A run is in progress. Do you really want to stop the run by closing the window?', + buttons: ['Cancel', 'Stop run and close'], + defaultId: 0, + cancelId: 0, + } ).then( ( { response } ) => { + const wasQuitting = quitting; + asking = false; + quitting = false; + if ( response !== STOP ) { + return; + } + stopAllRuns(); + if ( wasQuitting ) { + app.quit(); + } + else { + win.close(); + } + } ); + } ); +} diff --git a/src/main/index.ts b/src/main/index.ts index fb14ee70..f67686cc 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -9,6 +9,7 @@ import MenuBuilder from './menu'; import { is } from '../common/utils'; import { pageWindowPreferences } from './pageWindowPreferences'; import { applyUpdateDownloadBar, checkForUpdates, listenForUpdates } from './updates'; +import { confirmCloseWithRuns } from './confirmClose'; // TODO: Have removed electron-debug dependency, because it used remote module. Maybe we need some alternative. //------------------------------------------------------------------- // From https://github.com/iffy/electron-updater-example/blob/master/main.js @@ -82,6 +83,7 @@ function initialize(): void { if ( isDevMode && !isE2eMode ) { win.webContents.openDevTools(); } + confirmCloseWithRuns( win, dialog, app ); // Emitted when the window is closed. win.on( 'closed', function () { // Dereference the window object, usually you would store windows