Skip to content

Instantly share code, notes, and snippets.

@bobergj
Created December 4, 2020 13:13
Show Gist options
  • Save bobergj/1f658556b37774c05391373cb6834ea4 to your computer and use it in GitHub Desktop.
Save bobergj/1f658556b37774c05391373cb6834ea4 to your computer and use it in GitHub Desktop.
ToolbarItemWithControlValidating
// View-based toolbar items do not auto-validate by default, since the toolbar item view may
// be of any type. This subclass validates a toolbar item NSControl view against its action target.
class ToolbarItemWithControlValidating: NSToolbarItem {
override func validate() {
// validate with views view
if let action = self.action {
let validator = NSApp.target(forAction: action, to: self.target, from: self) as AnyObject?
switch validator {
case let validator as NSToolbarItemValidation:
isEnabled = validator.validateToolbarItem(self)
case let validator as NSUserInterfaceValidations:
isEnabled = validator.validateUserInterfaceItem(self)
default:
isEnabled = false
}
} else {
super.validate()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment