Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSButtonCell highlighting remains on keypress in Mojave

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

like image 801
Pablo Avatar asked Oct 22 '18 07:10

Pablo


1 Answers

Inside the action:

[button display]; 

This can be a non elegant solution. But it works for me.

like image 148
SOPONEXT Avatar answered Nov 13 '22 22:11

SOPONEXT