diff --git a/src/main/index.ts b/src/main/index.ts index 56c7642..3030dc2 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -19,6 +19,9 @@ let shuttingDown = false let endWorkWait: ((quit: boolean) => void) | null = null // the still working question is open. Quit and close don't open a second one meanwhile let askingAboutWork = false +// a question about running VMs is open (still running, or Force off at the end of a shut down). +// Same rule: no second copy from the tray or the window, and closing the window leaves it up +let askingAboutVms = false // a second copy would start the same VMs again and fight over their disks, so it only // brings this window to the front @@ -79,10 +82,12 @@ function createWindow(): void { // and closing it with only a compact or a profile save going cut that off win.on('close', (e) => { if (quitting || !backend) return + // a question is already up on this window, it gets answered there. This comes first because + // the last guest can power itself off while the question is up, and closing the window then + // takes the question with it and quits through window-all-closed + if (askingAboutWork || askingAboutVms) return e.preventDefault() if (backend.runner.runningNames().length === 0 && !hasWork()) return e.preventDefault() - // the still working question is up on this window, it gets answered there - if (askingAboutWork) return // already waiting for the guests or the work: just put the window away again if (shuttingDown || endWorkWait) win.hide() else if (settings.closeBehavior === 'tray') hideToTray() @@ -176,7 +181,9 @@ function hideToTray(announce = true): void { /** Quit from the tray: same question as closing the window, minus the tray option */ async function requestQuit(): Promise { - if (shuttingDown || endWorkWait || askingAboutWork) return + if (shuttingDown || endWorkWait) return + // a question is already up: show it instead of a second copy, so the click isn't a no-op + if (askingAboutWork || askingAboutVms) return showWindow() if (backend && hasWork() && !(await workFirst())) return if (!backend || backend.runner.runningNames().length === 0) { quitting = true @@ -274,7 +281,7 @@ const KEEP = 'Keep them running in the tray' const SHUT = 'Shut down VMs and quit' async function askAndQuit(fromClose: boolean): Promise { - if (shuttingDown) return + if (shuttingDown || askingAboutVms) return // after Quit when it's done the window is in the tray showWindow() const names = backend!.runner.runningNames() @@ -293,7 +300,13 @@ async function askAndQuit(fromClose: boolean): Promise { checkboxLabel: fromClose ? "Don't ask again (change this in Settings)" : undefined, noLink: true } - const r = mainWindow ? await dialog.showMessageBox(mainWindow, opts) : await dialog.showMessageBox(opts) + askingAboutVms = true + let r: Electron.MessageBoxReturnValue + try { + r = mainWindow ? await dialog.showMessageBox(mainWindow, opts) : await dialog.showMessageBox(opts) + } finally { + askingAboutVms = false + } const choice = buttons[r.response] if (r.checkboxChecked && (choice === KEEP || choice === SHUT)) rememberClose(choice === KEEP ? 'tray' : 'quit') if (choice === KEEP) return hideToTray() @@ -381,7 +394,14 @@ async function shutDownAllAndQuit(): Promise { noLink: true } showWindow() - const r = mainWindow ? await dialog.showMessageBox(mainWindow, opts) : await dialog.showMessageBox(opts) + // same as the other questions: closing the window leaves this one up instead of hiding it + askingAboutVms = true + let r: Electron.MessageBoxReturnValue + try { + r = mainWindow ? await dialog.showMessageBox(mainWindow, opts) : await dialog.showMessageBox(opts) + } finally { + askingAboutVms = false + } if (r.response !== 0) { shuttingDown = false updateTrayMenu()