开发者

Modify NSMenu structure on each open?

开发者 https://www.devze.com 2023-02-17 01:33 出处:网络
I need to be able to dynamically modify an开发者_Go百科 NSMenu hierarchy each time it is shown (add/remove items etc).For example:

I need to be able to dynamically modify an开发者_Go百科 NSMenu hierarchy each time it is shown (add/remove items etc). For example:

  1. user starts a tracking session on a main menu and selects a submenu
  2. detect submenu is about to open and run code to modify it
  3. keep tracking , user tracks over the same submenu again: goto 2

So to do this I have an object implementing the NSMenuDelegate protocol. The method menuNeedsUpdate works the first time (2), but does not work for 2nd time the submenu is opened. (Only called once per tracking session)

The method menuWillOpen is called each time, but has docs have the following warning which seems to disqualify using this approach:

Do not modify the structure of the menu or the menu items during this method.

Is there any way to accomplish this ?


You could subclass NSMenu and override submenuAction:.

Or you could just subscribe to the NSMenuWillSendActionNotification.

And while doesn't sound like it will work for you, just for reference, NSMenuValidation is a good place to update menu items on an item by item basis.


menuWillOpen will only be called once, the first time you track over the submenu. At that point, you populate the menu.

After that, menuWillOpen will not be called again. However, any changes to the menu will happen live. So while the main parent menu is open, whenever the source data changes (or periodically if you can't detect changes), update the menu using the normal NSMenu API.

Make sure whatever method you use to update the menu will run while the system is tracking your menu.

0

精彩评论

暂无评论...
验证码 换一张
取 消