Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drag NSStatusItems

You all know the menu bar (or better said NSStatusBar) in Mac OS X.
There are some items which I can move and other which not.

I would like to be able to drag the NSStatusItem of my app.

Any idea how to implement this?

like image 758
papr Avatar asked Mar 08 '09 16:03

papr


2 Answers

Although NSStatusItems appear near Apple's internal "menu extras", they are distinct and behave differently. It would be nice if Apple unified the items that can appear in the right-hand area of the menu bar, but for now the section is split into distinct "apple internal" (on the right), and "app-provided (NSStatusItem)" on the left.

You can visualize the distinction by putting your computer into screen capture mode (cmd-shift-4), and pressing the space bar to switch to "capture whole window". When you hover over Apple's menu icons, you'll see that they all live in a single window. This explains their ability to be easily managed and dragged about. Hovering over the other items reveals that each NSStatusItem is in fact living in a single window of its own (which happens to be owned by the application that installed it).

It's best to stick with NSStatusItem even though you can't drag them. It's a shortcoming from Apple which most users will understand, even if it's annoying. Emphasizing the positive tradeoffs of offering a more stable application for the long term will usually soften the opinions of your customers (or managers?) who are pushing for the draggability.

like image 178
danielpunkass Avatar answered Oct 27 '22 23:10

danielpunkass


You'll have to use NSMenuExtra, not NSStatusItem, and make the menu item a bundle running inside the SystemUIServer process, not your own app. You'll also need code like that supplied by MenuCracker to get this to work.

NSMenuExtra is undocumented and unsupported, and therefore considered a "hack".

My guess for there being two APIs in the first place: a menu extra crashing (or memory leaking) means the entire SystemUIServer process crashing or memory leaking — including other third party modules as well as system-supplied ones. With a status item, on the other hand, such a problem would only affect your own code.

like image 22
Sören Kuklau Avatar answered Oct 27 '22 21:10

Sören Kuklau