Using the same NSMenu multiple times
Using the same NSMenu multiple times
- Subject: Using the same NSMenu multiple times
- From: Greg Hurrell <email@hidden>
- Date: Wed, 7 Jun 2006 16:20:21 +0200
I have an NSMenu that I would like to be able to show in several
different places in my user interface. For example:
- in the main menu bar
- as a submenu in an NSStatusItem menu
- in a contextual menu
I would like to re-use this menu rather than have multiple instances
of it hanging around in memory and having to worry about keeping them
in sync with each other. The memory use problem is a real issue when
talking about potentially massive hierarchical menus (like browsing
the file system via submenus, for example). However, the Cocoa menu
architecture seems to strongly discourage (really, prohibit) re-use
of the same NSMenu (and NSMenuItem) objects in multiple places. For
example:
- NSMenu has a supermenu method and thus any given menu can only have
a single supermenu at one time; ie. it cannot belong to multiple
supermenus.
- NSMenuItem has setSubmenu: method and the docs say, "The default
implementation raises an exception if aSubmenu already has a
supermenu.", meaning that you can't attach the same menu as a submenu
at different places in the UI.
Despite this limitation, I know that my menu will only ever be
visible and active in the UI at one place at any given moment (ie. it
is not possible to navigate the main menu at the same time as a
contextual menu is showing, or an NSStatusItem is showing a menu etc)
so I can't see anything inherently dangerous in re-using the same
NSMenu object.
So what is the best way to workaround this limitation?
(1) Forget re-use and just keep three separate copies of the menu in
memory. I'd need to make sure all three copies were kept in sync.
(2) Don't keep any copies of the menu in memory at all and try to
build them on the fly from the delegate whenever menuNeedsUpdate: is
called. I could cache the menu items in an NSMutableArray to at least
avoid having to re-create them every single time. But still,
potentially slow for very large hierarchies that are accessed often.
(3) Keep a single copy in memory and do some trickery in
menuNeedsUpdate: to remove the menu from its supermenu before adding
it to any other menu.
(4) Come up with an NSMenu and/or NSMenuItem subclass that remove the
one-super-menu-only limitation.
It seems to me that (1) is downright inefficient and awkward; (2) has
some elegance but it is not very efficient; and (3) might work but it
doesn't feel right. I haven't tried (4) yet, although if it can be
done with a minimum of code I suspect it would be both the most
elegant and the most efficient.
I would have thought that this re-use question would be a fairly
common one but I can't find anything relevant in the list archives.
Thoughts, anyone?
Cheers,
Greg
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden