我有一个派生NSButtonCell
自我绘制挡板的类:
override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {
let path = NSBezierPath(bound: frame.insetBy(dx: CGFloat(config.buttonInset), dy: CGFloat(config.buttonInset)), withCorners: corners, withRadius: CGFloat(config.cornerRadius), flip: flipIt)
path.lineWidth = config.borderWidth
if(isEnabled)
{
if(isHighlighted)
{
print("isHighlighted true")
let fillColor: NSColor = colorMap.buttonHighlightColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
else
{
print("isHighlighted false")
if(showsStateBy.contains(.changeGrayCellMask))
{
print(".changeGrayCellMask")
if(state == .on)
{
print(".on")
let fillColor: NSColor = colorMap.buttonOnColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
else
{
print(".off")
let fillColor: NSColor = colorMap.buttonBackgroundColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
}
else
{
print("!.changeGrayCellMask")
let fillColor: NSColor = colorMap.buttonBackgroundColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
}
}
else
{
let fillColor: NSColor = colorMap.buttonBackgroundDisabledColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
}
此外,我已keyEquivalent
使用自定义单元格分配给按钮。
在macOS High Sierra上使用鼠标单击或按键都可以正常工作。仅当鼠标或键按下时才会显示突出显示。
日志输出如下所示:
**after click with mouse**
isHighlighted true
isHighlighted false
!.changeGrayCellMask
**after shortcut key**
isHighlighted true
isHighlighted false
!.changeGrayCellMask
但是,在Mojave上,按键行为是不同的。按键后突出显示状态保持不变,而使用鼠标时,突出显示按预期工作。
Mojave 的日志输出:
**Mojave click with mouse**
isHighlighted true
isHighlighted false
!.changeGrayCellMask
**Mojave after shortcut key**
isHighlighted false
!.changeGrayCellMask
isHighlighted true <----- this is odd
莫哈韦有什么改变吗?如您所见,drawBezel
调用顺序完全出乎意料。奇怪的是为什么它只在使用键盘时发生。
如何使用类似于鼠标单击 Mojave 的键盘实现按钮突出显示行为?
更新
我能够在 XCode Playground 中创建最小的项目来演示该问题。你可以在这里下载