3

I'm using a view-based NSTableView with a custom NSTableRowView. I would like to use custom row background drawing via drawBackgroundInRect, based upon mouse location using trackingAreas. The goal is to draw a custom background for the unselected row the mouse is currently hovering over.

This is virtually identical to the HoverTableView example from the WWDC 2011 session View Based NSTableView Basic to Advanced. You can see that behavior in action in the Mail, Contacts & Calendars System Preferences Pane in the account types table view on the right.

Unlike the examples, I have thousands of rows in my table view. Everything works as in the examples unless I scroll the table view rapidly (e.g., with a two-finger flick via trackpad). In this case, it seems that updateTrackingAreas is not called fast enough. Rows that scroll under the mouse get highlighted but are never notified that the mouse left their tracking area and therefore remain highlighted. The result is mulitple rows showing the mouse-over highlight and, due to the reuse queue, these will scroll off one end of the table view and reappear on the other (with different data of course) still highlighted as if they are moused-over. Scrolling slowly eliminates the problem; but considering I expect to scroll thousands and thousands of rows, scrolling slowly is not an expected user behavior.

I've tried various combinations of NSTrackingAreaOptions to no avail and am now stumped. Any suggestions on to solve this issue would be appreciated.

JefferyRPrice
  • 895
  • 5
  • 17

2 Answers2

2

I think the answer to the question is "you cannot," i.e., that updateTrackingAreas for NSTableRowView in a fast-scrolling NSTableView does not happen consistently fast enough on the run loop to rely upon it for determining if the pointer is inside a row view or not. Again, see the HoverTableView example code to see where updateTrackingAreas is being used.

I do think I have a suitable solution though. I noticed that Twitter for Mac (RIP) has mouse-over views that appear with mouse movement but disappear on scroll, very similar to the mouse-over highglight I was hoping to achieve.

To execute this, I basically made my custom NSTableRowView have a delegate (my custom NSTableViewController) whom it would ask if it should highlight on hover. I used a custom NSScrollView for my NSTableView and called

    [self.contentView setPostsBoundsChangedNotifications:YES];

in its awakeFromNib and also made it register self as the observer of that notification. On receiving that notification, which implies that my table view is scrolling, my custom NSScrollView forwards a message to my NSTableViewController.

When my NSTableViewController receives the message that the table view is scrolling, it disables highlighting on mouse-over and, if there is not already a valid timer running from a previous notification, fires a short timer to reenable highlight on mouse-over once scrolling has stopped. As an extra precaution, at state transitions between enable and disable highlight on mouse-over, my NSTableViewController uses enumerateAvailableRowViewsUsingBlock to clear mouseInside for every row view.

Not sure if this is necessarily the best way, but it achieves the effect I wanted.

JefferyRPrice
  • 895
  • 5
  • 17
1

The solution for this issue is described here: mouseExited isn't called when mouse leaves trackingArea while scrolling

My updateTrackingAreas method now looks like:

- (void)updateTrackingAreas {
    if (trackingArea)
        [self removeTrackingArea:trackingArea];

    [trackingArea release];

    trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
                                                options:NSTrackingInVisibleRect |
                                                        NSTrackingActiveAlways |
                                                        NSTrackingMouseEnteredAndExited
                                                  owner:self
                                               userInfo:nil];

    [self addTrackingArea:trackingArea];

    NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream];
    mouseLocation = [self convertPoint: mouseLocation fromView: nil];

    if (NSPointInRect(mouseLocation, [self bounds]))
        [self mouseEntered:nil];
    else
        [self mouseExited:nil];

    [super updateTrackingAreas];
}
Community
  • 1
  • 1
mutexre
  • 31
  • 4
  • The fact that the mouse pointer isn't entering/leaving the cell because it's actually stationary didn't cross my mind. That makes more sense than my (apparently incorrect) claim that `updateTrackingAreas` isn't called consistently fast enough. If I get the chance to return to this I'll try out that approach. – JefferyRPrice May 29 '14 at 23:44
  • @JefferyRPrice did you get this method to work with view-based table view rows? I have implemented it but I still have the same behaviour of the highlight not working on scroll. Any ideas? – Daniel Farrell Aug 10 '14 at 15:10
  • @boyfarrell Unfortunately, I have been unable to try this solution. The project I made and use still uses my own (more convoluted) answer. Sorry I couldn't be of more help. – JefferyRPrice Aug 11 '14 at 01:54
  • @boyfarrell Also, to be clear, my original solution was indeed applied to view-based table view rows. – JefferyRPrice Aug 11 '14 at 01:55
  • That's ok just came up with a fairly clean solution I'm happy with. I make the row view respond to the clip views "I'm scrolling" notification. I then make the row views redraw if the mouse is over them. – Daniel Farrell Aug 11 '14 at 01:57