Search code examples
macosappkitnstoolbarnstoolbaritemmacos-big-sur

Big Sur Toolbar Items in the Sidebar


In Big Sur, Xcode and Calendar have toolbar items that stay over the sidebar when open but remain visible on the left side when the sidebar's collapsed.

Sidebar open: enter image description here

Sidebar collapsed: enter image description here

In "Adopt the New Look of macOS" at 13:55, John says "items placed before the separator [sidebarTrackingSeparator] will appear over the full-height sidebar", just as they are in Xcode and Calendar. I haven't been able to make this work.

Here's a sample project that demonstrates the issue. I used the IB-defined "Window Controller with Sidebar" and added a toolbar item for toggling the sidebar. In a subclass of NSWindowController I insert .sidebarTrackingSeparator after the .toggleSidebar item:

override func windowDidLoad() {
    // Sometimes the toolbar items aren't loaded yet--async is a quick and dirty way to prevent a crash
    DispatchQueue.main.async {
        self.window?.toolbar?.insertItem(withItemIdentifier: .sidebarTrackingSeparator, at: 1)
    }
}

Sometimes this has no effect (the toggle button remains to the right of the sidebar). Sometimes the sidebar toggle get put in an overflow menu:

enter image description here

I haven't seen any discussion of implementing this toolbar design outside that WWDC session. Has anyone been able to get this to work?


Solution

  • This is a IB/Code timing disagreement. Interface Builder configures and installs the toolbar before you add the .sidebarTrackingSeparator toolbar item.

    So you're doing the right thing, just too late. And too later with the dispatch. I think the important thing is to have the item in there before the toolbar is set on the window.

    Unfortunately, that isn't really possible with IB, unless I believe, you create a whole new toolbar and reassign it. But that's a bad idea, because then you may run into trouble auto-saving the state of your toolbar.

    The trick is to configure the separator in Interface Builder. If you look at the ObjC documentation for this constant, you'll see a longer name: NSToolbarSidebarTrackingSeparatorItemIdentifier.

    The best we can do here is hope that the symbol's name is the same value as the identifier. If you really want to verify this, you can just print the symbol's value in the debugger:

    (lldb) po NSToolbarSidebarTrackingSeparatorItemIdentifier
    NSToolbarSidebarTrackingSeparatorItemIdentifier
    

    If we create a custom toolbar item in IB, and add that according to John's video...

    Interface Builder with a custom toolbar item positioned in the toolbar. The item has the identifier NSToolbarSidebarTrackingSeparatorItemIdentifier set in the attributes inspector

    low and behold: enter image description here