Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Tabs in a non-document-based app under macOS?

Tags:

xcode

macos

tabs

I like to build an app in Swift 3 with Xcode 8 and it should enable the Apple tab bar. It is not document-based. I learned here, that the tabs can be enabled if I override the method @IBAction func newWindowForTab(_ sender: Any?) for example in the window controller. To test this, I created a new project in Xcode using storyboard, added a subclass of NSWindowController and assigned it in the storyboard. I then implemented

@IBAction override func newWindowForTab(_ sender: Any?) {}

and the tab bar appears when the app is build. Once rebuild, I noticed that the "+"-button only appears if the tab bar was not visible when the app is closed prior to build. Is that a bug? How would I add new tabs?

like image 614
DaPhil Avatar asked Oct 23 '16 11:10

DaPhil


2 Answers

Conceptually, this is what happens:

  • It suffices to call NSWindow.addTabbedWindow(_:ordered:) to add a window to the native tab bars.
  • Once you put NSResponder.newWindowForTab(_:) into the responder chain of the main window, the "+" button will be visible.
  • When you set window.tabbingMode = .preferred, the tab bar will always be visible.

However, there are some caveats when implementing these methods.

Implement newWindowForTab

So where to add @IBAction override func newWindowForTab(_ sender: Any?) so you can call NSWindow.addTabbedWindow(_:ordered:)?

  • If you use Storyboards, then put this into a NSWindowController subclass you own. That's the simplest way to get to an NSWindow to call addTabbedWindow.
  • If you use Xibs, the AppDelegate will have a reference to the main window. You can put the method here.
  • If you use a programmatic setup, put it wherever you know the main NSWindow instance.

Make the "+" Button Work

TL;DR: When you initialize a new window, store the window's windowController somewhere. You need to maintain a strong reference in order for window events from being handled (in the controller).

I wrote a sample app with a TabManager that takes care of this: https://github.com/DivineDominion/NSWindow-Tabbing

And a blog post with details: https://christiantietze.de/posts/2019/07/nswindow-tabbing-multiple-nswindowcontroller/

Take into account how events are dispatched. Main Menu messages are sent down the responder chain, and so is newWindowForTab. NSApp.sendAction will fail for standard events if the source of the call doesn't connect up all the way -- that means, at least up to your NSWindowController, maybe even up to your AppDelegate.

You have to make sure any additional window you add is, in fact, part of the same responder chain as the original window, or else the menu items will stop working (and be greyed-out/disabled). Similarly, the "+" button stops to work when you click on it.

This is what @JohnV in the comments of the other answer called: "without the subview variable, you can't create more than two tabs". That's the effect, but it's not a real explanation. You can always create more tabs, but only from the original window/tab, not the new one; that's because the other tab is not responding to newWindowForTab.

"The other tab" itself is just an NSWindow. Your newWindowForTab implementation resides in the controller, though. That's up one level.

Adapting the code by @Peter Ahlberg, this will work:

class WindowController: NSWindowController {

    @IBAction override func newWindowForTab(_ sender: Any?) {

        let windowController: WindowController = self.storyboard?.instantiateInitialController() as! WindowController
        let newWindow = windowController.window

        self.window?.addTabbedWindow(newWindow, ordered: .above)

        newWindow.orderFront(self.window)
        newWindow.makeKey()

        // Store the windowController in a collection of sorts
        // to keep a strong reference and make it handle events:
        // (NSApp.delegate as? AppDelegate).addManagedWindowController(windowController)
    }
}

I didn't need to add newWindowForTab to AppDelegate to make everything work using Storyboards -- because this way the window controllers keep doing their job and don't need a fallback!

like image 178
ctietze Avatar answered Nov 09 '22 02:11

ctietze


Ok here are new files,

Appdelegate

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Insert code here to initialize your application
}

func applicationWillTerminate(_ aNotification: Notification) {
    // Insert code here to tear down your application
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
    return true
}

@IBAction func newWindowForTab(_ sender: Any?){
} // without this the + button doesn't show from start

}

ViewController

import Cocoa

class ViewController: NSViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override var representedObject: Any? {
    didSet {
    // Update the view, if already loaded.
    }
}
}

and WindowController

import Cocoa

class WindowController: NSWindowController {

var subview: WindowController?

override func windowDidLoad() {
    super.windowDidLoad()
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

@IBAction override func newWindowForTab(_ sender: Any?) {

    let story = self.storyboard
    let windowVC: WindowController = story?.instantiateInitialController() as! WindowController

    self.window?.addTabbedWindow(windowVC.window!, ordered: .above)
    self.subview = windowVC

    windowVC.window?.orderFront(self.window)
    windowVC.window?.makeKey()
}

}

you have to add menu item and connect it to FirstResponder in menu view to newWindowForTab: action, assign key, say cmd+t to work, this example as is just adds tab from + button and window menu options work, "move tab to new window" and "merge all windows". You can drag tab out and drop back , move tabs horizontally. Look like it works.

done with Xcode Version 8.2 beta (8C30a)

like image 26
Peter Ahlberg Avatar answered Nov 09 '22 02:11

Peter Ahlberg