Search code examples
swiftxcodemacosdocument-based

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


I have an macOS xcode swiftui document-based app. I can see that if I, "view->show tab bar" in the app, I get multiple tabs for free. However, if I do a "file open ...", it creates a new window. Now I eventually found that holding down option when selecting the file will open it in a new tab of an existing window. Does anyone know if it is possible to configure a document-based app to always open in a new tab rather than a new window each time?


Solution

  • Set window.tabbingMode to preferred in makeWindowControllers().

    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)
    }