Get notified when closing window containing SwiftUI view

I'm opening a new window and setting its contentView to an NSHostingView, to which I give my SwiftUI view. This view can edit certain things, and I want to be able to ask the user if they want to save their changes when they close the window. How can this be accomplished?