2

I have popover buttons on custom table cell view and when the mouse cursor moves over one cell, these buttons of the cell will be displayed and only this one cell should show the buttons. If i move the mouse cursor slowly, everything work correctly but when i scroll the table view with middle mouse rad faster there are too many cells are showing with popover buttons, what really should be avoided. Somehow the mouse event is not tracked correctly while scrolling. I got this tracking code from library of Apple Examples. Could you give some suggestion for this issue ?

#import "BasisCellView.h"

@implementation BasisCellView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    // Drawing code here.
    [[NSImage imageNamed:@"background"] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.1];
}

- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle {
    [super setBackgroundStyle: NSBackgroundStyleLight];
}

- (void)setMouseInside:(BOOL)value {
    if (mouseInside != value) {
        mouseInside = value;
        [self.deleteButton setHidden:!value];
        [self.bookmarkButton setHidden:!value];
        [self setNeedsDisplay:YES];
        NSLog(@"redrawn");
    }
}

- (BOOL)mouseInside {
    return mouseInside;
}

- (void)ensureTrackingArea {
    if (trackingArea == nil) {
        trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:NSTrackingInVisibleRect | NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited owner:self userInfo:nil];
    }
}

- (void)updateTrackingAreas {
    [super updateTrackingAreas];
    [self ensureTrackingArea];
    if (![[self trackingAreas] containsObject:trackingArea]) {
        [self addTrackingArea:trackingArea];
    }
}

- (void)mouseEntered:(NSEvent *)theEvent {
    NSLog(@"1");
    self.mouseInside = YES;
}

- (void)mouseExited:(NSEvent *)theEvent {
    NSLog(@"0");
    self.mouseInside = NO;
}

@end

And here is the printed out log:

2015-02-05 08:59:33.267 Clever[1286:25969] 1
2015-02-05 08:59:33.267 Clever[1286:25969] redrawn
2015-02-05 08:59:33.299 Clever[1286:25969] 0
2015-02-05 08:59:33.299 Clever[1286:25969] redrawn
2015-02-05 08:59:33.333 Clever[1286:25969] 1
2015-02-05 08:59:33.333 Clever[1286:25969] redrawn
2015-02-05 08:59:33.350 Clever[1286:25969] 0
2015-02-05 08:59:33.350 Clever[1286:25969] redrawn
2015-02-05 08:59:33.382 Clever[1286:25969] 1
2015-02-05 08:59:33.383 Clever[1286:25969] redrawn
2015-02-05 08:59:33.669 Clever[1286:25969] 1
2015-02-05 08:59:33.669 Clever[1286:25969] redrawn
2015-02-05 08:59:33.736 Clever[1286:25969] 1
2015-02-05 08:59:33.736 Clever[1286:25969] redrawn
2015-02-05 08:59:33.769 Clever[1286:25969] 0
2015-02-05 08:59:33.769 Clever[1286:25969] redrawn
2015-02-05 08:59:33.769 Clever[1286:25969] 1
2015-02-05 08:59:33.770 Clever[1286:25969] redrawn
2015-02-05 08:59:34.101 Clever[1286:25969] 1
2015-02-05 08:59:34.101 Clever[1286:25969] redrawn
2015-02-05 08:59:34.102 Clever[1286:25969] 0
2015-02-05 08:59:34.102 Clever[1286:25969] redrawn
2015-02-05 08:59:34.136 Clever[1286:25969] 0
2015-02-05 08:59:34.136 Clever[1286:25969] redrawn
2015-02-05 08:59:34.150 Clever[1286:25969] 1
2015-02-05 08:59:34.150 Clever[1286:25969] redrawn
2015-02-05 08:59:34.187 Clever[1286:25969] 1
2015-02-05 08:59:34.187 Clever[1286:25969] redrawn
2015-02-05 08:59:34.235 Clever[1286:25969] 1
2015-02-05 08:59:34.272 Clever[1286:25969] 0
Quang Nguyen
  • 354
  • 1
  • 5
  • 20
  • I can suggest you one workaround for this issue . When mouse entered to one cell view , you can make other views mouse inside NO. – Sheen Vempeny Feb 05 '15 at 10:24
  • Thank you for your suggestion, it got it fixed now with this your solution but is it the only one solution ? I think about performance because i need nested loops to get the cell view from table view ( TableView -> RowView -> CellView ). – Quang Nguyen Feb 06 '15 at 07:59
  • Otherwise you can track the table view rect and with mouseMoved event you can assign the value to mouseInside for cell view. – Sheen Vempeny Feb 06 '15 at 08:39

3 Answers3

3

Here's a swift version of the code required to set up a tracking area:

class MyCustomTableCellView: NSTableCellView {

    func setUpTrackingArea()
    {
        let trackingArea = NSTrackingArea(rect: self.frame, options: [NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways], owner: self, userInfo: nil)
        self.addTrackingArea(trackingArea)
    }

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)

        setUpTrackingArea()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)

        setUpTrackingArea()
    }

    override func mouseEntered(theEvent: NSEvent) {
        Swift.print("mouse Entered")
    }

    override func mouseExited(theEvent: NSEvent) {
        Swift.print("mouse exited")
    }
}
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
1

Reminders.app uses NSTrackingArea for cellView + controller observes NSScrollViewWillStartLiveScrollNotification and loops through visible cells to hide the button.

If you don't need live updates and you are ok to hide views/de-highlight immediately use NSScrollViewWillStartLiveScrollNotification

For live updates:

- (void)touchesMovedWithEvent:(NSEvent *)event;
[self setAcceptsTouchEvents:YES];

Anything else is custom with multiple solutions: e.g. use of NSScrollViewWillStartLiveScrollNotification + NSScrollViewDidEndLiveScrollNotification in your controller or you override scrollWheel method and fire mouse events how you need:

CustomScrollView is the one sending mouseEvents to CustomTableRowView and CustomTableRowView forwards it to its subviews.

#import <Cocoa/Cocoa.h>

@interface CustomScrollView : NSScrollView

@end

#import "CustomScrollView.h"

@implementation CustomScrollView

- (void)scrollWheel:(NSEvent *)theEvent
{
    NSPoint mouseLocation;
    NSInteger rowBefore = -1, rowAfter = -1;
    mouseLocation = [[self documentView] convertPoint:[theEvent locationInWindow] fromView:nil];
    rowBefore = [(NSTableView *)[self documentView] rowAtPoint:mouseLocation];

    @autoreleasepool {
        while ((theEvent = [[self window] nextEventMatchingMask:(NSScrollWheelMask)
                                                      untilDate:[NSDate distantFuture]
                                                         inMode:NSEventTrackingRunLoopMode
                                                        dequeue:YES]) &&
               !(([theEvent phase] & NSEventPhaseCancelled) || ([theEvent phase] & NSEventPhaseEnded))) {
            [super scrollWheel:theEvent];
        }
    }
    [super scrollWheel:theEvent];

    mouseLocation = [[self documentView] convertPoint:[theEvent locationInWindow] fromView:nil];
    rowAfter = [(NSTableView *)[self documentView] rowAtPoint:mouseLocation];
        if (rowBefore != -1) {
            NSTableRowView *rowViewBefore = [(NSTableView *)[self documentView] rowViewAtRow:rowBefore makeIfNecessary:NO];
            [rowViewBefore mouseExited:[NSApp currentEvent]];
        }
        if (rowAfter != -1) {
            NSTableRowView *rowViewAfter = [(NSTableView *)[self documentView] rowViewAtRow:rowAfter makeIfNecessary:NO];
            [rowViewAfter mouseEntered:[NSApp currentEvent]];
        }
}

@end

CustomTableRowView:

- (void)mouseEntered:(NSEvent *)event
{
    if (_inMouseEntered == NO) {
        _inMouseEntered = YES;
        [self setHighlighted:YES];
        for (NSView *view in [self subviews]) {
            if ([view isKindOfClass:[NSTableCellView class]]) {
                [view mouseEntered:event];
            }
        }
        [self setNeedsDisplay:YES];
        _inMouseEntered = NO;
    }
}

- (void)mouseExited:(NSEvent*)event
{
    if (_inMouseExited == NO) {
        _inMouseExited = YES;
        [self setHighlighted:NO];
        for (NSView *view in [self subviews]) {
            if ([view isKindOfClass:[NSTableCellView class]]) {
                [(NSTableCellView *)view mouseExited:event];
            }
        }
        [self setNeedsDisplay:YES];
        _inMouseExited = NO;
    }
}

Don't forget NSTrackingArea to get original mouseEvents

Marek H
  • 5,173
  • 3
  • 31
  • 42
  • There is undocumented NSTrackingRollover (NSTrackingArea) with value 0x800 but I wasn't able to trigger mouseExit event with it. – Marek H Dec 30 '17 at 13:10
-1

This code does exactly what you want like in the Apple's Reminders App. If look carefully in the Reminders App, They put some delay before make the button visible. I add many rows to the table and test while scroling.

  #import "OTratingListTableCellView.h"

    @implementation OTratingListTableCellView

    @synthesize boatNameTextField,boatRatingTextField,boatStartTimeTextField,boatFinishTimeTextField,classTextField,popUpButton;

    - (void)drawRect:(NSRect)dirtyRect {
        [super drawRect:dirtyRect];


        NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.frame
                                                                    options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow )
                                                                      owner:self userInfo:nil];
        [self addTrackingArea:trackingArea];
        // Drawing code here.
    }

    - (void)mouseEntered:(NSEvent *)theEvent
    {
        NSLog(@"mouseEntered");
        popUpButton.hidden=false;

    }
    - (void)mouseExited:(NSEvent *)theEvent
    {
        popUpButton.hidden=true;

        NSLog(@"mouseExited");  
    }

    @end
codezero
  • 99
  • 1
  • 7
  • Welcome to SO. Please explain your answer instead just posting some code. – bastelflp Apr 20 '16 at 22:28
  • 1
    Your code example has a performance bug in it where you keep adding tracking areas over and over again via the `drawRect:` function (which gets called very often). Better to just create the trackingArea once (e.g. in an `init`) – Michael Dautermann Sep 09 '16 at 01:31