Contextual Menu does not appear

143 Views Asked by At

I tried to create a contextual menu that displays when the user right-clicks on a NSMenuItem which contains a custom View. In the subclassed custom View the code looks as follows:

override func rightMouseDown(with event: NSEvent) {
    let context_menu = NSMenu()
    let testitem = NSMenuItem()
    testitem.title = "Test"
    context_menu.addItem(testitem)
    //popUpContextMenu is not displayed
    NSMenu.popUpContextMenu(context_menu, with: event, for: self)
    print(event.description)
}

The print is executed, and I can see the right click event in the console. But the context menu does not appear, nor are there any errors printed in the console.

If it matters, the view is contained inside another view. Setting the "for:" attribute to self.superview does not change the mentioned behavior.

Edit: I now tried to see if the menu is opening in the background or offscreen by using a delegate that prints to console, and it seems that the "context_menu" is never loaded. I created a small project that only draws one NSMenuItem which is supposed to show a context menu when right-clicked. Similarly to the above, this does not happen although the right click is registered: https://github.com/Tardik/stackov_contextmenunotloading

0

There are 0 best solutions below