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

90 Views Asked by At

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.

  • Can I make the IKImageBrowserView change selection also on control-click (mouse and trackpad)?
  • Can I attach the context menu not to the IKImageBrowserView but to a single element/image of an IKImageBrowserView?
1

There are 1 best solutions below

3
Jim On BEST ANSWER

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;
}