Thomas
Thomas

Reputation: 8968

Cursor weird behavior: resets to default

On a mouse move on an NSView, I change the cursor to a hand [[NSCursor pointingHandCursor] set]. I do NOT reset anywhere in the code the cursor to arrow.

This works pretty well, however if I move the mouse slowly I can see at times it reverts to the arrow. This is unwanted. Is that a bug in Cocoa or is there a work around?

-(void) mouseMoved:(NSEvent*) event {
  [[NSCursor pointingHandCursor] set];
}

Again, I do not play with the cursor anywhere else in the code. I do not have other views overlaying on my NSView. Thanks in advance!

Upvotes: 0

Views: 148

Answers (2)

Thomas
Thomas

Reputation: 8968

Answering my own question based on @seth's suggestion. This is what I used to set or unset a cursor. It does not have the issue I described above. I did not use mouseEntered or mouseExit.

-(void)setCursor:(NSCursor *)cursor {  // nil to reset to the arrow
    if (cursor == _cursor)
        return;
    _cursor = cursor;
    [self discardCursorRects];
    [self.window invalidateCursorRectsForView:self];  // this is needed in Catalina
    [self resetCursorRects];
}

-(void)resetCursorRects {
    if (_cursor != nil) {
        [self addCursorRect:self.bounds cursor:_cursor];
    }
    [super resetCursorRects];
}

Upvotes: 0

seth
seth

Reputation: 1718

If you can make use of purely rectangular areas, using addCursorRect:cursor: etc within resetCursorRects is an easy way to do this.

Otherwise, you can make use of NSTrackingAreas with NSTrackingCursorUpdate set as the option, and in cursorUpdate: use the set method on a cursor like you are.

Using set/push/pop etc on their own isn't stable because it's not cooperative with other views which set the cursor.

Upvotes: 1

Related Questions