Alex Rivera | Logout

Menu Bar App Never Becomes Reactivated

Asked 2013-02-28T03:44:59.737
8

I'm building a Mac app that only sits in the menu bar with no dock item and no key window and no main menu (it's LSUIElement in the info.plist is set to YES). When I first launch the app, applicationDidBecomeActive: is called, as I expect. However, once another app gains focus, applicationDidBecomeActive: is never called again.

This prevents a text field I have within my app from becoming the first responder. When I first open the app, the text field is editable:

before another app gains focus

But after another app comes to the foreground, the text field is not editable:

after another app gains focus

What I've tried:

When the menu is opened, menuWillOpen: is called on the NSMenu's delegate. I've tried placing the following with no success:

[NSApp unhide];
[NSApp arrangeInFront:self];
[NSApp activateIgnoringOtherApps:YES];
[NSApp requestUserAttention:NSCriticalRequest];
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
[[NSRunningApplication currentApplication] unhide];

I think the issue is probably related to not having any windows to bring to the front. I feel like I'm grasping at straws here. Any help would be greatly appreciated.

Edit
Report

1 Answer

3

I think the issue is with that how the runloop operates when a NSMenu is open, so you should try activating the app before you display the menu. If you're having the NSStatusItem display it, I'd suggest doing it yourself like this:

- (void)toggleMenu:(id)sender
{
  // App might already be active
  if ([NSApp isActive]) {
    [self.statusItem popUpStatusItemMenu:self.menu];
  } else {
    [NSApp activateIgnoringOtherApps:YES];
  }
}

- (void)applicationDidBecomeActive:(NSNotification *)notification
{
  [self.statusItem popUpStatusItemMenu:self.menu];
}

That should work, but I think though in general you'll have better luck with an actual window instead of a menu.

answered 2013-02-28T16:37:05.320

Your Answer