Xcode 可以将基于文档的应用设置为始终在新选项卡中打开吗

Can Xcode Document-based app be set to always open in a new tab

我有一个基于 macOS xcode swiftui 文档的应用程序。我可以看到,如果我在应用程序中“查看->显示标签栏”,我可以免费获得多个标签。但是,如果我执行“文件打开...”,它会创建一个新的 window。现在我最终发现在选择文件时按住选项将在现有 window 的新选项卡中打开它。有谁知道是否可以将基于文档的应用程序配置为始终在新选项卡中打开而不是每次都在新的 window 中打开?

好的,对于所有来到这里的人。感谢 El Tomato 的提示,我已经(大部分)实现了我想要的。关键步骤是:

  1. 在应用程序中获取 window 的 tabGroup 属性 不是 nil
  2. 完成后,使用 addTabbedWindow 函数将您的 window 添加到 tabGroup
  3. 如果应用程序没有 window 和填充的 tabGroup 属性,那么您就没有选项卡,所以一旦您有一个新的 window,调用 toggleTabBar 函数来显示标签栏。对 makeViewControllers 的后续调用将找到包含选项卡的 window。

我的 makeWindowControllers 代码是...

let tabsWindow = NSApplication.shared.windows.filter({[=10=].tabGroup != nil}).first
let window = NSWindow(
  contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
  styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
  backing: .buffered, defer: false)
window.isReleasedWhenClosed = false
window.center()
window.contentView = NSHostingView(rootView: contentView)
if tabsWindow != nil {tabsWindow!.addTabbedWindow(window, ordered: .below)}
else { window.toggleTabBar(self) }

makeWindowControllers()中将window.tabbingMode设置为preferred

override func makeWindowControllers() {
    // Create the SwiftUI view that provides the window contents.
    let contentView = ContentView()

    // Create the window and set the content view.
    let window = NSWindow(
        contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
        styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
        backing: .buffered, defer: false)
    window.isReleasedWhenClosed = false
    window.center()
    window.contentView = NSHostingView(rootView: contentView)
    
    window.tabbingMode = .preferred // open new document in tab
    
    let windowController = NSWindowController(window: window)
    self.addWindowController(windowController)
}