Created
          December 4, 2020 13:13 
        
      - 
      
 - 
        
Save bobergj/1f658556b37774c05391373cb6834ea4 to your computer and use it in GitHub Desktop.  
    ToolbarItemWithControlValidating
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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