DocumentWindow::closeButtonPressed() doesn't get called on Mac when selecting "Quit" from the menu

When you choose Quit from Mac’s application menu, DocumentWindow::closeButtonPressed() doesn’t get called.

The documentation states that the method will be called when user clicks “OS menu for getting rid of a window”. But that doesn’t happen. This creates an issue on Mac computers since selecting “Quit” from the menu doesn’t allow for safety measures, such as asking the user if he wants to save the project before quitting, etc.

How to fix this issue, or should it be fixed from JUCE itself?

I think that the menu item to close a window would normally be called “Close Window (cmd + w)”.

When quit is selected in the menu, your app should receive a call to JUCEApplicationBase::systemRequestedQuit(). You can override this function to display a dialog asking the user to confirm.

That lead to another problem which doesn’t seem to match what the JUCE documentation says:

When I try to ask in the systemRequestedQuit() method if the user wants to save or not, obviously I need a blocking dialog box. for this I use AlertWindow::showYesNoCancelBox().

Documentation states that if the last parameter is nullptr, the method will block, which is what’s needed to get this “save at quit” working. But the method doesn’t block.

So another inconsistency with the documentation vs. how things are implemented. Preferably I’d want this Yes/No/Cancel box and the Save file chooser to be blocking.

The documentation is slightly out-of-date. It should also read “If JUCE_MODAL_LOOPS_PERMITTED is not defined, this function will always return immediately. In this case, the object passed as the callback argument will receive the result of the alert window asynchronously.”

Generally, blocking modal loops are a bad idea, which is why they’re disabled by default. The recommended approach is to use AlertWindow::showScopedAsync instead.

The docs have been updated on develop:

1 Like