在 AppKit 上,菜单项和工具栏项分别有 validateMenuItem(_:)
和 validateToolbarItem(_:)
。然而,对于新的触摸栏项目,没有这种方便的方法来在正确的时刻验证适当的项目。
我现在每次更改相关值并调用 didSet
中的验证方法时都会验证触摸栏项目(请参阅以下示例代码)。但我觉得这不是一个好方法,因为相关值必须知道有一个依赖它的触摸栏项目。
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 绑定(bind)和 KVO,但是,它并不总是有效。
所以,我很好奇是否有任何推荐的或事实上的标准方法来验证触摸栏项目,尤其是包含 NSButton 和 NSSegmentedControl 的项目。我不仅想改变元素的可用性,有时还想根据情况改变它们的图像或颜色。你们如何验证触摸栏项目?
最佳答案
我自己改进了我的touch bar验证系统,做了如下协议(protocol)和扩展。
@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(_:)
来验证您自己的触摸条项目!
我想这至少能满足我的要求。
关于swift - 验证 NSTouchBar 项目的最佳实用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40799131/