0

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!

Thomas
  • 8,306
  • 8
  • 53
  • 92
  • Does this answer your question? [Cocoa nsview change cursor](https://stackoverflow.com/questions/29911989/cocoa-nsview-change-cursor) – Willeke Feb 17 '20 at 10:45
  • @Willeke thanks I did see that but it didn't work. seth's answer really put me on track – Thomas Feb 19 '20 at 02:32

2 Answers2

1

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.

seth
  • 1,647
  • 1
  • 12
  • 20
0

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];
}
Thomas
  • 8,306
  • 8
  • 53
  • 92