admin管理员组

文章数量:1405508

I have a MacOS application I'm trying to migrate to the SwiftUI scene instead of doing the Windows management by myself. I connect my scene with a AppDelegate.

import Foundation
import SwiftUI

@main
struct iMarketApp: App {    
    @NSApplicationDelegateAdaptor var delegate: AppDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

If the user closes the main window, I want to be able to reopen it programmatically from my AppDelegate (doing programmatically the equivalent of clicking on the App Icon again). I don't want to use openWindow with an id as it tries to recreate a new window every time and I want this window to be unique and reuse if it already exists.
From my delegate, I just want to put the window in the foreground if it exists or reopen it and put it on the foreground if it doesn't exist.
I would like to also avoid parsing NSApp.windows and searching for my window.
Is it possible to do it in a simple SwiftUI way nowadays or do I have to manage it the old school way with NSWindow manipulation, check if my window exists, makeKeyAndOrderFront, etc...

本文标签: SwiftUI Reactivate scene after window close on MacOSStack Overflow