如何限制 macOS 窗口管理恢复特定窗口?

Sco*_*ott 4 macos nswindowrestoration swiftui swiftui-windowgroup

我有一个应用程序,它有几个窗口定义为窗口组,其结构符合主场景中的应用程序:

WindowGroup("StandingsView") {
    
    StandingsView()
        .environmentObject(appServices)
}
.handlesExternalEvents(matching: Set(arrayLiteral: "StandingsView"))
Run Code Online (Sandbox Code Playgroud)

appServices 需要一些时间来配置,所以我不想在启动时自动恢复窗口。我在用户选择有效、服务完全配置并且用户按下“开始”SwiftUI 按钮时创建窗口:

if let standingsURL = URL(string: "raceStratLiteApp://StandingsView") {
    NSWorkspace.shared.open(standingsURL)
}
Run Code Online (Sandbox Code Playgroud)

我尝试关闭 appDelegate 的 applicationShouldTerminate() 中的窗口。我还尝试在 applicationShouldTerminate 中将 isRestorable 设置为 false:

func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
    
    for window in NSApplication.shared.windows {
        window.isRestorable = false
    }
    return .terminateNow
}
Run Code Online (Sandbox Code Playgroud)

还有其他方法不恢复窗口吗?或者更好的是,能够以编程方式将其恢复为之前的大小等,但仅在用户指示“启动”时启动

TIA

Sco*_*ott 5

@Asperi 在后面的评论中建议的代码解决方案:

func applicationDidFinishLaunching(_ notification: Notification) {
    UserDefaults.standard.register(defaults: ["NSQuitAlwaysKeepsWindows" : false])
}
Run Code Online (Sandbox Code Playgroud)