I'm currently implementing the NSTouchBar api to my macOS application.
At this moment, the only touch bar I have has the main View Controller as its delegate and I can add items to it fine. The catch is, I need some of those items to appear only when a certain condition is met (a row is selected in a table).
Consider I have a boolean that indicates whether or not the button should be visible. How do I update the NSTouchBar on the fly to show/hide this button in case my boolean changes? (I don't need to observe this boolean, I could simply make the call to update in another method I already implemented)
What I did for now is the following: in
defaultItemIdentifiersto the set of item identifiers that should be shown. Note that this would be problematic if the user has customized the touch bar, but I think swapping items on-demand and customizing the touch bar doesn't go well together anyway. This also has the advantage that you don't need to return nil in
makeTouchBar() will create a new
NSTouchBar instance, but not change the
touchBar property. Try something like
viewController.touchBar = viewController.makeTouchBar()
viewController.touchBar = nil
touchBar property on the
NSTableRowView that should show extra items when selected, and make sure to include the
otherItemsProxy in your
defaultItemIdentifiers. As the contents of the touch bar are comprised of all elements in the responder chain, this might include the
touchBar property of the table row (provided it can become first responder).
Are you sure that these items should be hidden when the row is not selected? Consider disabling them instead (e.g. by setting the
enabled property of the buttons they contain to