Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa application menu bar not clickable

I'm building a menu bar in my cocoa application with the following code in the @implementation of my custom application CustomApplication:

+(void) setUpMenuBar
{
  [CustomApplication sharedApplication];

  // Main menu
  NSMenu* mainMenu = [NSApp mainMenu];
  if (mainMenu != nil) return; // We set it already
  mainMenu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
  [NSApp setMainMenu:mainMenu];

  // Application menu
  NSMenuItem* appleItem = [mainMenu addItemWithTitle:@""
                                              action:nil
                                       keyEquivalent:@""];

  NSString* appName = @"MyApp";

  NSMenu* appleMenu = [[NSMenu alloc] initWithTitle:@""];

  // Apple menu
  [appleMenu addItemWithTitle:[@"About " stringByAppendingString:appName]
                       action:@selector(orderFrontStandardAboutPanel:)
                keyEquivalent:@""];

  // Quit
  [appleMenu addItemWithTitle:[@"Quit " stringByAppendingString:appName]
                                        action:@selector(terminate:)
                                        keyEquivalent:@"q"];

  [appleItem setSubmenu:[appleMenu autorelease]];
}

At launch, my application gets the focus, but the menu bar is not clikable. However, if I click out the window and in again (giving the focus back to the application), it becomes clickable and working correctly.

Did I miss something?


UPDATE

This method is called when I'm creating the application as follows. [UPDATE] This is what I'm starting my application with. It is actually called first thing from an ocaml binding outside any @implementation of a class.

CustomApplicationDelegate* delegate = [CustomApplicationDelegate new];

[CustomApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES];

[[NSApplication sharedApplication] setDelegate:delegate];

[CustomApplication setUpMenuBar];

[[CustomApplication sharedApplication] finishLaunching];
like image 619
Théo Winterhalter Avatar asked Oct 26 '15 12:10

Théo Winterhalter


3 Answers

Okay, thanks to the remarks of @bhaller I was able to solve my problem.

I actually transferred my calls to the delegate as follows.

-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
  [CustomApplication sharedApplication];
  [CustomApplication setUpMenuBar];
  [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
}

-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
  [CustomApplication sharedApplication];

  [NSApp activateIgnoringOtherApps:YES];
}
like image 75
Théo Winterhalter Avatar answered Oct 23 '22 01:10

Théo Winterhalter


I had this issue and the reason was because my call to [NSApp activateIgnoringOtherApps:YES] was in applicationWillFinishLaunching: instead of applicationDidFinishLaunching:.

As soon as I moved it, the menubar worked on first-launch.

like image 38
EpaL Avatar answered Oct 23 '22 03:10

EpaL


I fixed by removing LSUIElement in Info.plist if the app has a Window and Menu Bar.

The old solution is that we override this config with [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];, but in Catalina, it doesn't work anymore.

like image 24
Nghia Tran Avatar answered Oct 23 '22 02:10

Nghia Tran