簡體   English   中英

NSButtonCell突出顯示了Mojave中的按鍵

[英]NSButtonCell highlighting remains on keypress in Mojave

我有一個派生自NSButtonCell的類,我繪制了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()
        }
    }

另外,我使用自定義單元格將keyEquivalent分配給按鈕。

無論是在macOS High Sierra上使用鼠標點擊還是按鍵,這都可以完美地運行 僅在鼠標或鍵關閉時才會顯示突出顯示。

日志輸出如下所示:

**after click with mouse**
isHighlighted true
isHighlighted false
!.changeGrayCellMask

**after shortcut key**
isHighlighted true
isHighlighted false
!.changeGrayCellMask

但是,在Mojave上,按鍵的行為是不同的。 按下按鍵后,突出顯示的狀態仍然存在,而在使用鼠標時,突出顯示按預期運行。

Mojave的日志輸出:

**Mojave click with mouse**
isHighlighted true
isHighlighted false
!.changeGrayCellMask

**Mojave after shortcut key**
isHighlighted false
!.changeGrayCellMask
isHighlighted true <----- this is odd

莫哈韋也有變化的東西。 正如您所看到的, drawBezel調用順序完全出乎意料。 奇怪的是它只在使用鍵盤時才會發生。

如何使用鍵盤類似於鼠標點擊Mojave來實現按鈕突出顯示行為?

UPDATE

我能夠在XCode Playground中創建最小的項目來演示這個問題。 你可以在這里下載

內部動作:

[button display]; 

這可能是一個非常優雅的解決方案。 但它對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM