NSButton state management

289 Views Asked by At

My NSButton is using custom NSButtonCell class, which in turn is overriding drawBezel:

override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {
        let path = NSBezierPath(bound: frame.insetBy(dx: CGFloat(NumKeypadConstants.buttonInset), dy: CGFloat(NumKeypadConstants.buttonInset)), withCorners: corners, withRadius: CGFloat(NumKeypadConstants.cornerRadius), flip: flipIt)

        path.lineWidth = NumKeypadConstants.borderWidth
        if(isEnabled)
        {
            if(isHighlighted)
            {
                let fillColor: NSColor = NumKeypadConstants.buttonHighlightColor
                let strokeColor: NSColor = NumKeypadConstants.buttonBorderColor
                fillColor.setFill()
                strokeColor.setStroke()
                path.fill()
                path.stroke()
            }
            else
            {
                if(state == .on)
                {
                    let fillColor: NSColor = NumKeypadConstants.buttonOnColor
                    let strokeColor: NSColor = NumKeypadConstants.buttonBorderColor
                    fillColor.setFill()
                    strokeColor.setStroke()
                    path.fill()
                    path.stroke()
                }
                else
                {
                    let fillColor: NSColor = NumKeypadConstants.buttonBackgroundColor
                    let strokeColor: NSColor = NumKeypadConstants.buttonBorderColor
                    fillColor.setFill()
                    strokeColor.setStroke()
                    path.fill()
                    path.stroke()
                }
            }
        }
        else
        {
            let fillColor: NSColor = NumKeypadConstants.buttonBackgroundDisabledColor
            let strokeColor: NSColor = NumKeypadConstants.buttonBorderColor
            fillColor.setFill()
            strokeColor.setStroke()
            path.fill()
            path.stroke()
        }
    }

Now my button supports both types 1) .momentaryChange 2) .pushOnPushOff. I was expecting that depending on button types, the state will be altered or not. But no matter what I set using setButtonType, the .state member of cell is always changing on button press.

Do I understand correct, that it is my responsibility to check the type of button and either regard or disregard .state? Could it be that certain combination of bezelStyle and button type will tell the cell to internally ignore .state when using momentary types?

If I have to check for the button type, then how I check for the type in NSButtonCell? The type is not passed to it. Should I create own property and pass type?

0

There are 0 best solutions below