NSView's context NSMenu is never shown even though all the right methods are being called

850 views Asked by At

I have an NSCollectionView with a bunch of NSViews in it, stacked vertically, to make it look a bit like UIKit's UITableView. Everything works as expected, except for one thing:

When right-clicking any one of the NSViews, I expect the NSMenu I set to be view's menu to be shown, but alas - nothing happens.

The crazy part is all the right methods are being called, exactly as could be expected: -rightMouseDown:, -menuForEvent: and finally -menu.

When I set up any object as the NSMenu's delegate, menuWillOpen: is not called, so it seems to me something fails over on Apple's side of things, just in between asking for the menu, and actually showing it.

Would anyone be able to shed a light on this?

Thanks in advance.

PS. For what it's worth, NSMenus I present manually (without relying on Apple's right-click handling) using popUpMenuPositioningItem:atLocation:inView: are shown.

Edit / Update / Clarification

The NSCollectionView in question is inside an NSWindow that's being shown when an NSStatusItem is clicked, like CoverSutra/TicToc/what have you. Some code from the MyWindow NSWindow subclass:

- (void)awakeFromNib {
    [self setStyleMask:NSBorderlessWindowMask];
    [self setExcludedFromWindowsMenu:YES];
}

- (BOOL)canBecomeMainWindow {
    return YES;
}

- (BOOL)canBecomeKeyWindow {
    return YES;
}

- (BOOL)isMovable {
    return NO;
}

- (void)presentFromPoint:(NSPoint)point {
    point.y -= self.frame.size.height;
    point.x -= self.frame.size.width / 2;
    [self setFrameOrigin:point];
    [self makeMainWindow];
    [self makeKeyAndOrderFront:self];
}

presentFromPoint: is the method I use to present it from any point I like, in my case from just below the NSStatusItem. (Not really relevant to this problem)

My application has LSUIElement in its Info.plist set to YES by the way, so it doesn't show a menu bar or a Dock icon. It lives in the status bar, and has a window that's shown when the NSStatusItem is clicked.

The view hierarchy is as follows:

MyWindow => contentView => NSScrollView => NSCollectionView

The NSCollectionView has an NSCollectionViewItem subclass connected to its itemPrototype property, and the NSCollectionViewItem subclass has an NSView subclass connected to its view property. The NSView subclass, in turn, has an NSMenu connected to its menu property. And last but not least: This NSMenu has one NSMenuItem sitting inside it.

Both the NSCollectionViewItem subclass and the NSView subclass do nothing interesting as of now, they're just empty subclasses.

The NSMenu connected to the NSView's menu property is what should be shown when the NSView is right-clicked, but as I hope I have made clear: It isn't actually shown.

Update

I still have no idea what caused this problem, but I've decided to 'move on' from NSCollectionView, as it wasn't really fit for what I was trying to do anyway, and I am now using TDListView which works like a charm.

0

There are 0 answers