Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the flag now covers two questions, this one and the Force off question in shutDownAllAndQuit, so the comment reads a bit narrow. The second one also behaves slightly differently from what requestQuit promises: shuttingDown is true by then, so Quit from the tray returns on the line above instead of showing the window with the question on it. Not worth another flag, but the comment could say the flag means "a question about running VMs is open".


// a second copy would start the same VMs again and fight over their disks, so it only
// brings this window to the front
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This early return runs before the new question checks below, so it decides on state that can have changed while the question was up. If the last guest powers itself off while "VMs are still running" is open (the question is asked about VMs that are running, and a guest shutting down from its own menu is normal), runningNames() is empty and hasWork() is false, so the X closes the window with the message box still attached to it, and window-all-closed quits the app. Moving the askingAboutWork || askingAboutVms check above this line keeps the window alive until the question is answered, which is what the rest of the handler now assumes.

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()
Expand Down Expand Up @@ -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<void> {
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
Expand Down Expand Up @@ -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<void> {
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()
Expand All @@ -293,7 +300,13 @@ async function askAndQuit(fromClose: boolean): Promise<void> {
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()
Expand Down Expand Up @@ -381,7 +394,14 @@ async function shutDownAllAndQuit(): Promise<void> {
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()
Expand Down
Loading