NSButtonCell highlighting remains on keypress in Mojave
Asked Answered
T

1

11

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

Tain answered 22/10, 2018 at 7:39 Comment(4)
Sounds like a bug introduced with Mojave. Is there any workaround?Tain
Updated with Playground demo project, showing the problemTain
set NSRequiresAquaSystemAppearance = YES in your info.plist then see what happenVermilion
@RonGahlot, setting that key in info.plist doesn't solve the problem.Tain
K
1

Inside the action:

[button display]; 

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

Krys answered 15/2, 2019 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.