swiftUI how to Hide window instead of closing it when click the close button on MacOS

swiftUI how to Hide window instead of closing it when click the close button on MacOS.

Accepted Reply

after all i find solution.

first AppDelegate implement NSWindowDelegate then catch you main window in applicationDidFinishLaunching, then point mainWindow.delegate = self

    func applicationDidFinishLaunching(_ notification: Notification) {
        mainWindow = NSApp.windows[0]
        mainWindow!.delegate = self
        setUpMacMenu()

    func windowShouldClose(_ sender: NSWindow) -> Bool {
        NSApp.hide(nil)
        return false
    }
    }```



Replies

after all i find solution.

first AppDelegate implement NSWindowDelegate then catch you main window in applicationDidFinishLaunching, then point mainWindow.delegate = self

    func applicationDidFinishLaunching(_ notification: Notification) {
        mainWindow = NSApp.windows[0]
        mainWindow!.delegate = self
        setUpMacMenu()

    func windowShouldClose(_ sender: NSWindow) -> Bool {
        NSApp.hide(nil)
        return false
    }
    }```