NSButtonCell突出显示仍然在莫哈韦的按键上



我有一个从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 click with mouse**
isHighlighted true
isHighlighted false
!.changeGrayCellMask
**Mojave after shortcut key**
isHighlighted false
!.changeGrayCellMask
isHighlighted true <----- this is odd

那么莫哈韦有什么变化吗?如您所见drawBezel调用顺序完全出乎意料。奇怪的是为什么它只发生在使用键盘时。

如何使用类似于鼠标单击Mojave的键盘实现按钮突出显示行为?

更新

我能够在XCode Playground中创建最小的项目来演示这个问题。您可以在此处下载

在操作中:

[button display]; 

这可能是一个不优雅的解决方案。但它对我有用。

最新更新