I have a class derived from NSButtonCell
where I draw bezel:
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()
}
}
Additionally I have keyEquivalent
assigned to the button with my custom cell.
This works perfectly fine either using mouse click or keypress on macOS High Sierra. The highlight is shown only when the mouse or key is down.
Log output looks like this:
**after click with mouse**
isHighlighted true
isHighlighted false
!.changeGrayCellMask
**after shortcut key**
isHighlighted true
isHighlighted false
!.changeGrayCellMask
However, on Mojave the behaviour on keypress is different. After keypress the highlighted state remains, while when using mouse, the highlight acts as expected.
The log output from Mojave:
**Mojave click with mouse**
isHighlighted true
isHighlighted false
!.changeGrayCellMask
**Mojave after shortcut key**
isHighlighted false
!.changeGrayCellMask
isHighlighted true <----- this is odd
So is there something that has been changed in Mojave. As you can see drawBezel
call order is totally unexpected. Strange thing is why it happens only when using keyboard.
How to achieve button highlight behaviour with keyboard similar to mouse click on Mojave?
UPDATE
I was able to create minimal project in XCode Playground demonstrating the problem. You can download it here