Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is alternative to NSStatusItem.popUpMenu?

NSStatusItem.popUpMenu has been deprecated in macOS 10.14, but I can't find a nice alternative.

let m = statusItem.menu!
statusItem.popUpMenu(m) // deprecated

I tried direct pop-up using menu and the button, but it doesn't position properly.

let m1 = m.items.first!
m.popUp(positioning: m1, at: .zero, in: statusItem.button!)
like image 702
eonil Avatar asked Oct 01 '18 05:10

eonil


1 Answers

Xcode suggests to use menu property instead of popupMenu. But once you set the menu property, every click on the item will only show the menu.

Instead, if you want to control when the menu is shown, say only in response to a right click, then a simple way to manually trigger the menu is by calling performClick on NSStatusBarButton in your handler.

statusItem.menu = myMenu
statusItem.button?.performClick(nil)
statusItem.menu = nil

You have to set menu back to nil, if you want to keep handling clicks yourself.

like image 184
a2bk Avatar answered Sep 16 '22 17:09

a2bk