DJH
DJH

Reputation: 2449

Incorrect NSCursor originating from background NSView

I wanted to change the cursor in my cocoa application. I've followed the example in this answer, with success.

I have an NSView, let's call it NSViewA, behind another NSView, NSViewB. NSViewA contains a subclassed NSButton where the cursor has been changed. NSViewA and NSViewB have the same superview. So something like this:

- NSWindow
    - NSViewA
        - NSButtonSubclass
    - NSViewB

My problem is that when NSViewB is shown, and the cursor is ontop of NSViewB, but in the same x y coordinates of the NSButton behind NSViewB, the cursor changes to that specified in the NSButton subclass.

How do I stop this behaviour?

The reason for this layout is that I'm creating a 'lightbox' control. If you think something along the lines of the NSWindow greying out, and a centred box appearing showing an error message. That sort of thing.

I previously had a problem where you could still click buttons, etc, behind NSViewB. This was solved by suppressing mouseDown: and mouseUp:. I've tried doing something similar with other mouse-related events, such as mouseEntered: and mouseExited: with no luck.

Upvotes: 1

Views: 309

Answers (1)

Paul Patterson
Paul Patterson

Reputation: 6918

Could you make the addition of your custom cursor rectangle contingent on the enabled status of your button? In other words, your resetCursorRects would look like this:

// MyButton.swift
override func resetCursorRects() {
    if enabled {
        addCursorRect(bounds, cursor: NSCursor.pointingHandCursor())
    }
}

Whenever viewB is about to be shown, disable the button, and call for the rects belonging to your button to be invalidated. If you're using Swift, you could do this second bit in a property observer attached to the enabled property itself:

// MyButton.swift
override var enabled: Bool {
    didSet {
        window!.invalidateCursorRectsForView(self)
    }
}

If you don't want your button to take on a disabled look, make the addCursorRect call contingent on some other flag.

Upvotes: 1

Related Questions