Any idea what could cause a menu handler to stop working?
File > Save
File > Save As
Were working.
Stopped working.
Set a breakpoint in the Menu Handler code, first line.
Menu Handlers never get called.
File Menu still flashes highlighted on the key commands.
Selecting the menu items manually also does nothing.
Quit the IDE, restarted.
Same.
Deleted the menu items and the menu handlers.
Added new menu items and menu handlers.
Same behavior.
Only tried rebuilding the menu in this project. Window still has its menubar. I think it must have to do with the enabled property as you guessed first, but I have no idea why it worked and then stopped working - I didn’t change anything about the EnableMenuItems event.
This was the problem! Seems kind of ridiculous, but here’s what caused it:
EnableMenuItems in the App object set these items enabled property to False if there are no App windows open. With an App window open, clicking on the File menu shows that the menu items are enabled. But apparently they aren’t actually enabled.
Why? Apparently because EnableMenuItems in the App object was not setting the enabled properties to True if an App window existed. It only set the properties to False if it didn’t exist.
Now, I understand my mistake, but why the menus should appear enabled when they aren’t is what is still baffling me.
As far as I know this is not the correct way to do it. Always enable or disable menu items for a window in the window’s own EnableMenuItems event, never in the AppEnableMenuItems event. In other words: enable or disable a menu item in the EnableMenuItems of the object which contains the menu handler (which is true for controls also).
My 2¢ is that EnableMenuItems should rarely be used and if it is used, care should be taken to only update what is required and do a minimal amount of work (definitely not something that can block, like disk IO). This can be a very performance sensitive event because it gets invoked for almost keystroke on OS X.
Instead, I recommend using AutoEnableMenuItems. This enables the MenuItem automatically if anything in the chain has a corresponding menu handler and is very efficient.
Thanks, but I don’t think so. There are legitimate reasons for using the App object rather than the window to do this. What seems to matter is keeping the Menu handlers together with EnableMenuItems event that enables or disables them.
[quote=238256:@Joe Ranieri]My 2¢ is that EnableMenuItems should rarely be used and if it is used, care should be taken to only update what is required and do a minimal amount of work (definitely not something that can block, like disk IO). This can be a very performance sensitive event because it gets invoked for almost keystroke on OS X.
Instead, I recommend using AutoEnableMenuItems. This enables the MenuItem automatically if anything in the chain has a corresponding menu handler and is very efficient.[/quote]
I understand, and have run into problems before because of too much code in EnableMenuItems. A problem is that without using this event, enabling the menu items no longer becomes event driven. It is much harder to be sure that the states of all the menu items are correct at the moment they are called, if they can’t be rebuilt / enabled only when they are called upon. It’s too bad that the event gets called so often, when it doesn’t seem necessary to do so.
I’d tend to agree. Back when I was working on the initial Cocoa port, I spent three or four days on the problem and sadly there’s not much to be done about it.