Search code examples
macosswiftuiterminate

How to terminate programmatically a MacOS app when clicking on the top left red dot?


Good Day!

By default when we click on the top left red dot of our window app, the window closes but not the app and the menu bar stays displayed on top.

What I would like to achieve by code is to terminate (close) the app when we click on the red dot... by doing this the menu bar will disapears hence the app is terminated.

Thanks in advance for your help.


Solution

  • Add the following callback in your app delegate

    class AppDelegate: NSObject, NSApplicationDelegate {
    
        //... other methods here
    
        func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
            return true
        }
    }