0

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?
MartinW
  • 4,966
  • 2
  • 24
  • 60

1 Answers1

1

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;
}
MartinW
  • 4,966
  • 2
  • 24
  • 60
Jim
  • 475
  • 2
  • 7
  • This works in principle, but gives an error, when I click at an area within the IKImageBrowserView but outside of an actual image: `-[NSIndexSet initWithIndexesInRange:]: Range {9223372036854775807, 1} exceeds maximum index value of NSNotFound - 1` – MartinW Jan 10 '16 at 15:56
  • Ok, `9223372036854775807` seems to be the value of `NSNotFound`, and I can just test `(idx != NSNotFound)` before changing the index. I updated your answer. – MartinW Jan 10 '16 at 16:06
  • Ahh...I had adapted it from use in a tableView where rows couldn't be clicked between, and didn't try that on the IKImageBrowserView. – Jim Jan 12 '16 at 02:42