【问题标题】:Best practical way to validate NSTouchBar items验证 NSTouchBar 项目的最佳实用方法
【发布时间】:2023-04-01 09:27:01
【问题描述】:

在 AppKit 上,菜单项和工具栏项分别具有 validateMenuItem(_:)validateToolbarItem(_:)。但是,对于新的触摸栏项目,没有这种方便的方法可以在正确的时刻验证适当的项目。

我现在每次更改相关值并调用didSet 中的验证方法时都会验证触摸栏项目(请参阅以下示例代码)。但是我觉得这不是一个好方法,因为相关值必须知道有一个touch bar item依赖它。

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
    }
}

我使用的另一种方法是 Cocoa-binding 和 KVO,但是,它并不总是很好用。

所以,我很好奇是否有任何推荐或事实上的标准方法来验证触摸栏项目,尤其是包含 NSButton 和 NSSegmentedControl。我不仅想改变物品的可用性,有时还想根据情况改变它们的图像或颜色。你们如何验证触摸栏项目?

【问题讨论】:

    标签: swift cocoa appkit macos-sierra nstouchbar


    【解决方案1】:

    我自己改进了我的触控栏验证系统,并做了以下协议和扩展。

    @available(macOS 10.12.1, *)
    protocol TouchBarItemValidations: class {
    
        func validateTouchBarItem(_ item: NSTouchBarItem) -> Bool
    }
    
    
    
    @available(macOS 10.12.1, *)
    extension NSTouchBarProvider {
    
        func validateTouchBarItems() {
    
            guard NSClassFromString("NSTouchBar") != nil else { return }  // run-time check
    
            guard let touchBar = self.touchBar else { return }
    
            // validate currently visible touch bar items
            for identifier in touchBar.itemIdentifiers {
                guard let item = touchBar.item(forIdentifier: identifier) as? NSCustomTouchBarItem else { continue }
    
                item.validate()
            }
        }
    
    }
    
    
    @available(macOS 10.12.1, *)
    extension NSCustomTouchBarItem: NSValidatedUserInterfaceItem {
    
        func validate() {
    
            // validate content control
            if let control = self.control,
                let action = control.action,
                let validator = NSApp.target(forAction: action, to: control.target, from: self)
            {
                if let validator = validator as? TouchBarItemValidations {
                    control.isEnabled = validator.validateTouchBarItem(self)
    
                } else if let validator = validator as? NSUserInterfaceValidations {
                    control.isEnabled = (validator as AnyObject).validateUserInterfaceItem(self)
                }
            }
        }
    
    
    
        // MARK: Validated User Interface Item Protocol
    
        public var action: Selector? {
    
            return self.control?.action
        }
    
    
        public var tag: Int {
    
            return self.control?.tag ?? 0
        }
    
    
    
        // MARK: Private Methods
    
        private var control: NSControl? {
    
            return self.view as? NSControl
        }
    
    }
    
    
    @available(macOS 10.12.1, *)
    extension AppDelegate {
    
        func applicationDidUpdate(_ notification: Notification) {
    
            // validate touch bar items
            if #available(macOS 10.12.1, *) {
                if let window = NSApp.mainWindow {
                    for responder in sequence(first: window.firstResponder, next: { $0.nextResponder }) {
                        responder.validateTouchBarItems()
                    }
                }
            }
        }
    }
    

    如果您已经有 AppDelegate 的 applicationDidUpdate(:_),则应该修改它,但除此之外,无需添加任何复杂内容。您现在可以使用validateTouchBarItem(_:) 或普通validateUserInterfaceItem(_:) 来验证您自己的触控栏项目!

    我想这至少符合我的要求。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    相关资源
    最近更新 更多