macoscocoamouseikimagebrowserviewtrackpad

Control-click does not change selection in IKImageBrowserView, right-click or two-finger-click does


I added a context menu to an IKImageBrowserView.

When a user right-clicks (mouse) or two-finger-clicks (trackpad) an image in the IKImageBrowserView, the selection changes to this image, and the context menu appears.

When a user control-clicks (mouse or trackpad), the selection does not change, and the context menu appears.

As the context menu is relative to the selected image, I prefer, that the selection changes, when the context menu is invoked.


Solution

  • If you make a subclass of IKImageBrowserView and override menuForEvent:, you can accomplish this:

    - (NSMenu *)menuForEvent:(NSEvent *)event {
        NSUInteger idx = [self indexOfItemAtPoint:[self convertPoint:[event locationInWindow] fromView:nil]];
    
        if (idx == NSNotFound) {return nil;}
    
        if (![self.selectionIndexes containsIndex:idx]) {
            [self setSelectionIndexes:[NSIndexSet indexSetWithIndex:idx] byExtendingSelection:NO];
    
        }
    
        return self.menu;
    }