Rebuilding menus (menuNeedsUpdate:) and System Preferences keyboard shortcuts.
Rebuilding menus (menuNeedsUpdate:) and System Preferences keyboard shortcuts.
- Subject: Rebuilding menus (menuNeedsUpdate:) and System Preferences keyboard shortcuts.
- From: Keith Blount <email@hidden>
- Date: Mon, 1 Dec 2008 12:49:07 -0800 (PST)
Hi,
My application has several menus that get built dynamically, depending on user settings. For instance, there is a "Styles" menu, which lists styles the user has defined, and a "Script Elements" menu, which again, provides a list of user-defined options that apply a certain format to a range of text.
However, it seems that dynamically-built menus have a problem when it comes to keyboard shortcuts defined via the System Preferences - as in, the keyboard shortcuts don't always work.
At first I thought the solution was obvious: I was building these menus as needed, in the NSMenu delegate method, -menuNeedsUpdate: This method only gets called when the user goes to look at the menu. So, if these dynamic menus were only built in -menuNeedsUpdate:, then clearly the keyboard shortcuts defined in System Preferences for any items in such menus would note work until after the user had opened the menu at least once (thus populating it in -menuNeedsUpdate: and allowing the defined shortcut actually to apply to something, because before this the menu wouldn't have existed).
My solution was to build these menus whenever the user settings changed. So, if the user added or deleted a style, the menu would get rebuilt, as well as rebuilding it in -menuNeedsUpdate:. However, this still doesn't seem reliable. When opening another window, the keyboard shortcuts defined via System Preferences still don't operate in the new window until after the menu has been opened - and yet NSLogging shows that the menu is getting built before this and even seems to have a keyboard shortcut attached... Very strange.
So, my question: what is the best way of building menus dynamically whilst ensuring that any keyboard shortcuts defined in System Preferences will catch them and apply to them?
I am implementing -menuHasKeyEquivalent:... and returning NO. It seems that if I don't implement this method, then keyboard shortcuts work for dynamically built methods... But not implementing this method makes all the keyboard shortcuts in my app work very, very slowly, so leaving it out isn't an option...
Thanks in advance for any help or pointers.
All the best,
Keith
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden