Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to detect main menu being opened



Hello all,

I handled this in a less-than-perfect way that uses some undocumented functionality. It now works as expected, except it does not work if focus is merely put onto the menu bar (e.g., with Control-F2).

The relevant code snippets from my application's main controller object are below. (This is archived and restored in the MainMenu.nib, initial NIB.)

Clearly, one should have a const NSString *menuTrackingNotificationName defined up top and used for good style and practice.

Cheers,

Doug

/** Set up some notification watchers **/
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSLog(@"applicationDidFinishLaunching");

// Watch for the main menu being opened
// Determined this using Notification Watcher
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self
selector:@selector(menuOpened:)
name:@"com.apple.HIToolbox.beginMenuTrackingNotification"
object:nil];
}


/** Removes some notifications **/
- (void)applicationWillTerminate:(NSNotification *)aNotification
{
// Undo the registration from above
[[NSDistributedNotificationCenter defaultCenter]
removeObserver:self
name:@"com.apple.HIToolbox.beginMenuTrackingNotification"
object:nil];
}


/** We get this from a distributed notification set up at app launch **/
// This works, but seems to be undocumented
- (void)menuOpened:(id)dontKnowWhatThisParameterIs
{
    NSLog(@"Menu opened");
    [gameModel pause];
}

On Mar 20, 2007, at 1:17 PM, Doug F wrote:

Hello all,

Is there any API that will tell me when the user has opened any of the main menu bar's menus (including the Apple menu)? I want to "pause" my application's timers and update some state when this occurs, but I haven't figured out a reliable way.

My first idea almost worked for all menus except the system-Apple menu. I set up a delegate for each menu and implemented
- (void)menuNeedsUpdate:(NSMenu *)menu
which then called my "pause" routine. (Incidentally, setting the delegate of the whole main menu did NOT ever call the above routine; I had to set the delegate of each menu individually in Interface Builder.)


This failed because one of my menu items was a "Pause" menu with a hotkey. As it turns out, hitting this hotkey causes "menuNeedsUpdate" to be invoked prior to the target/action being called, which pauses things and updates the menu to modify the menu item to be a "resume" (with the same hotkey (modifies the title only)), so when the hotkey target/action was called, that turned right around and unpaused things! That was a fun thing to figure out.

Bigger picture, though: It also fails because there is no way to determine if the system-Apple menu has been opened.

So, I need a way to generate some sort of event or target/action when ANY menu is opened (via click or, even, Control-F2 to focus on the menu bar) so I can pause my application.

Would anyone have any ideas, please? I've looked at the PDFs of Apple docs, Googled and Cocoadev'd and Cocoabuildered and either my search abilities suck or I'm just oblivious.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >How to detect main menu being opened (From: Doug F <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.