addCursorRect fails on NSButton after NSPopover loses focus

140 views Asked by At

I have a NSPopover that contains two buttons. When I open the popover, the following code works to change the cursor to a pointing hand when hovering over the buttons and on clicking the button, 'Button pressed' appears in the console and an NSColorPanel appears as is desired.

class Button: NSButton {
    override func resetCursorRects() {
        super.resetCursorRects()
        addCursorRect(bounds, cursor: .pointingHand)
    }
}

@IBAction func buttonTapped(action:Any) {

    print("Button pressed")

    let cp = NSColorPanel.shared
    cp.setTarget(self)
    NSColorPanel.setPickerMode(.none)
    cp.setAction(#selector(colorDidChange))
    cp.isContinuous = false
    cp.level = NSWindow.Level.statusBar
    cp.makeKeyAndOrderFront(self)

}

However if I click anywhere else on the screen, and then go back to the NSPopover, the pointing hand cursor no longer appears when hovering over the button, and while the onClick event is still fired (as evidenced by 'Button pressed' logged in the console), the NSColorPanel doesn't open.

Any suggestions as to why this might be the case?

1

There are 1 answers

0
cnotethegr8 On

I ran into this issue and what solved it for me was adding the below code to the popover view controller viewWillAppear function.

NSApplication.shared.activate(ignoringOtherApps: true)