NSTrackingArea doesn't always update cursor

421 views Asked by At

I am having a hard time using NSTrackingArea to update the cursor to show where my custom draggable NSView can be resized. Here's an example:

The NSRect for the handle:

var handleBottomLeft: NSRect {
    NSRect(x: self.bounds.origin.x,
           y: self.bounds.origin.y,
           width: handleMargin + borderWidth,
           height: handleMargin + borderWidth)
}

The tracking area:

var bottomLeftTrackingArea: NSTrackingArea?

override func updateTrackingAreas() {
    if let area = bottomLeftTrackingArea {
        self.removeTrackingArea(area)
    }
    bottomLeftTrackingArea = nil
    bottomLeftTrackingArea = NSTrackingArea(rect: handleBottomLeft, options: [.activeInKeyWindow, .cursorUpdate], owner: self, userInfo: nil)
    addTrackingArea(bottomLeftTrackingArea!)
    super.updateTrackingAreas()
}

And finally, the cursor update:

override func cursorUpdate(with event: NSEvent) {
    switch event.trackingArea {
    case bottomLeftTrackingArea:
        print("pointing hand")
        NSCursor.pointingHand.set()
    default:
        print("arrow")
        super.cursorUpdate(with: event)
    }
}

The cursor should change when it enters or leaves the blue box, but it only happens 50-75% of the time. Here's an example of the cursor not changing when it enters, but changing when it leaves.

https://i.imgur.com/fbqqzGy.mp4

1

There are 1 answers

0
Evan Moran On

Resizing my tracking areas to not overlap with each other helped resolve the issue for me. I can't tell from your sample code if you have this issue, but figured I'd share just in case!