Search code examples
objective-ccocoansmenuitemnsprogressindicator

How to create the NSMenuItem view for a menu item with an embedded NSProgressIndicator?


I would like to create a menu item with an embedded NSProgressIndicator, similar to the "Wi-Fi: Looking for Networks…" menu item of the Wi-Fi status menu:

Screenshot of opened Wi-Fi status menu on Mac showing "Wi-Fi: Looking for Networks…" menu item

I think that I will need to use the setView: method as discussed at: Views in Menu Items. However, when I call setView: and pass an NSProgressIndicator, only the NSProgressIndicator is displayed.

How can the custom menu item view be created to get a similar result as the "Wi-Fi: Looking for Networks…" menu item of the Wi-Fi status menu?


Solution

  • As noted in the document to which you linked:

    A menu item with a view does not draw its title, state, font, or other standard drawing attributes, and assigns drawing responsibility entirely to the view.

    So, if you want your menu item to look like a normal menu item plus some other stuff, your view has to draw the normal menu item features in addition to the other stuff. So, you could set the menu item's view to a custom view. The progress indicator would be one subview of that custom view, but you would need other subviews for the other features. For example, a text field for the text of the menu item and an image view for a state indicator (if your item shows state).

    You'll have to draw highlighting as the item is selected or tracked. You'll also have to do mouse tracking. Apple has some sample code which demonstrates various parts of this:

    MenuItemView
    CustomMenus
    GridMenu