简体   繁体   English

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

[英]How do you restrict the macOS windowing management from restoring a specific window?

I have an app that has a few windows defined as a windows group in the structure conforming to App in the main scene:我有一个应用程序,其中有几个 windows 定义为 windows 组,其结构符合主场景中的应用程序:

 WindowGroup("StandingsView") { StandingsView().environmentObject(appServices) }.handlesExternalEvents(matching: Set(arrayLiteral: "StandingsView"))

The appServices take some time to be configured, so I do not want to automatically restore the windows at start. appServices 需要一些时间来配置,所以我不想在启动时自动恢复 windows。 I create the windows upon user selections being valid, the services being fully configured, and the user pressing a 'start' SwiftUI button:在用户选择有效、服务完全配置并且用户按下“开始”SwiftUI 按钮后,我创建了 windows:

 if let standingsURL = URL(string: "raceStratLiteApp://StandingsView") { NSWorkspace.shared.open(standingsURL) }

I've tried closing the windows in the appDelegate's applicationShouldTerminate().我尝试在 appDelegate 的 applicationShouldTerminate() 中关闭 windows。 I've also tried setting the isRestorable to false in applicationShouldTerminate:我还尝试在 applicationShouldTerminate 中将 isRestorable 设置为 false:

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

Are there any other methods to not restore a window?有没有其他方法可以不恢复 window? or better yet, to be able to programmatically restore it with its previous size, etc but launch only on user direction to 'start'或者更好的是,能够以编程方式将其恢复为以前的大小等,但仅在用户指示“开始”时启动

TIA TIA

From the tip provide by @Asperi, writing the following to defaults will cease the writing of the window states:根据@Asperi 提供的提示,将以下内容写入默认值将停止写入 window 状态:

$ defaults write <bundleid> NSQuitAlwaysKeepsWindows -bool false

So this is not a code change to the app, but rather an environment config the would be done on install.所以这不是对应用程序的代码更改,而是在安装时完成的环境配置。

I also deleted the savedState directory located at ~/Library/Saved Application State/<bundleid>.savedState for archive builds and at ~/Library/Containers/<App Name>/Data/Library/Saved Application State/<bundleid>.savedState for debug builds.我还删除了位于~/Library/Saved Application State/<bundleid>.savedState用于存档构建和~/Library/Containers/<App Name>/Data/Library/Saved Application State/<bundleid>.savedState用于调试构建。 Am not sure if that mattered but once doing these steps it solved the problem.我不确定这是否重要,但是一旦执行了这些步骤,它就解决了问题。 Thanks @Asperi谢谢@Asperi

The code solution as @Asperi suggests in the later comment: @Asperi 在后面的评论中建议的代码解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM