Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide NSMenu programmatically from NSStatusItem

Tags:

macos

cocoa

I have this application that shows an item in the system's status bar, and one of the items is a custom view with a NSTextField and a NSButton. When the user clicks on the status bar item, it shows the menu, the user inputs some text and presses the button. This triggers an action that displays a window.

The problem I'm having now is, when the button is pressed it does trigger the action, but the menu remains visible. I want to hide the menu, because the action has already been processed.

I've searched through the API, but couldn't find how to do it.

Any ideas?

This is how I'm creating the menu:

NSStatusBar *bar = [NSStatusBar systemStatusBar];

self.statusItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[statusItem setImage:[NSImage imageNamed:@"icon_status_bar.png"]];
[statusItem setHighlightMode:YES];

NSMenuItem *textInputItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
[textInputItem setView:myCustomView];  // created on the Nib file...

NSMenu *menu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"statusBarMenuTitle", @"")];
[menu addItem:textInputItem];

[statusItem setMenu:menu];

[textInputItem release];
[menu release];
like image 909
Marcos Crispino Avatar asked Apr 11 '11 14:04

Marcos Crispino


1 Answers

It's not obvious in the docs, but [menu cancelTracking] is what you want.

cancelTracking
Dismisses the menu and ends all menu tracking.
- (void)cancelTracking
like image 152
Nick Moore Avatar answered Sep 22 '22 17:09

Nick Moore