How to detect when a SwiftUI WindowGroup is closed?

I want to be able to detect when an auxiliary window is closed in order to display a confirmation dialog box. Apple's Mail app on MacOS exhibits this exact behaviour when a user closes a message they were composing, a dialog box appears asking to save, don't save or cancel.

I am programmatically opening the auxiliary windows using the openWindow action in combination with the corresponding WindowGroup initialiser as described in this article Presenting windows and spaces and the WWDC video Bring multiple windows to your SwiftUI app.