I subclassed NSView and created NSTrackingArea using the following:
-(void)setUpTrackingArea { if(trackingArea != nil) { [self removeTrackingArea:trackingArea]; } int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingEnabledDuringMouseDrag); trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds] options:opts owner:self userInfo:nil]; [self addTrackingArea:trackingArea]; NSLog(@"update tracking area %@", trackingArea); NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream]; mouseLocation = [self convertPoint: mouseLocation fromView: nil]; if (NSPointInRect(mouseLocation, [self bounds])) { [self mouseEntered: nil]; } else { [self mouseExited: nil]; } }
I also redefine:
- (void)mouseEntered:(NSEvent *)theEvent - (void)mouseExited:(NSEvent *)theEvent
to set the selection property, which then calls
[self setNeedsDisplay:YES]
which calls drawrect to highlight the menu, as you would expect a menu.
The problem is that the event that leaves the mouse does not always fire, leaving some custom views highlighted after the mouse is gone.
Any ideas what I'm doing wrong?
I created a demo project that presents this problem.
see https://github.com/antokne/APGCustomMenuItemView
Thants.
source share