4

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?

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193

1 Answers1

5

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

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Thank you for your answer. I am looking at the CustomMenus sample now. That seems closest to what I am trying to do. In particular, there is a `suggestionprototype.xib` with which I am trying to follow along... – Daniel Trebbien Sep 21 '14 at 18:19
  • 1
    First, that sample has an accompanying [WWDC video](https://developer.apple.com/videos/wwdc/2010/#145-video) which you would watch. Also, part of it shows a view in a menu but part of it shows how to construct a window that looks and works like a menu but isn't actually an `NSMenu`. So, be mindful of which part you're looking at. – Ken Thomases Sep 21 '14 at 18:49
  • 1
    That video is very helpful. The session name is "Key Event Handling in Cocoa Applications" but it covers custom menus starting at 19:13. – nschum Oct 25 '14 at 08:48
  • @KenThomases Couldn't I just associate an `NSImage` with the menu item that fills the whole item? – phimuemue Feb 14 '17 at 17:20