In AppKit, menu items and toolbar items have validateMenuItem(_:), validateToolbarItem(_:)respectively. However, with the help of new elements of the touch panel, there is no such convenient method for checking the corresponding elements at the right time.
Now I check the elements of the touchpad every time I change the associated values and the verification method is called in didSet(see the following code example). But I feel that this is not very good, because the related values should know that depending on this there is an element of the touch panel.
var foo: Foo? {
didSet {
if #available(macOS 10.12.1, *), NSClassFromString("NSTouchBar") != nil {
self.validateTouchBarItem(identifier: .foo)
}
}
}
@available(macOS 10.12.1, *)
func validateTouchBarItem(identifier: NSTouchBarItemIdentifier) {
guard
let item = self.touchBar?.item(forIdentifier: identifier),
let button = item.view as? NSButton
else { return }
switch identifier {
case NSTouchBarItemIdentifier.foo:
button.isEnabled = (self.foo != nil)
default: break
}
}
Another way that I use is Cocoa-binding and KVO, however it does not always work well.
, , - - , NSButton NSSegmentedControl. , . , , ?