Jacob
Jacob

Reputation: 1310

How do I make a Menubar Application with a NSPopover?

I have seen a lot of applications with a Menubar Item or applications with only a Menubar interface.

There are some tutorials and stuff on the internet showing you how to accomplish that. But the thing is, those do only have clickable index rows in them.

I would want to have a NSPopover appear when you click the Menubar Icon / Item. Anybody who knows how to make this?

Upvotes: 20

Views: 7662

Answers (1)

djromero
djromero

Reputation: 19641

I don't know if it can be done with a standard status bar item. Using a custom view for the menulet it's relatively easy.

Create a status bar item with a custom view:

item = [[NSStatusBar systemStatusBar] statusItemWithLength:thickness];
view = [[CustomView alloc] initWithFrame:(NSRect){.size={thickness, thickness}}];
[item setView:view];        

Your custom view needs to detect mouse clicks:

- (void)mouseDown:(NSEvent *)event {
   ...
}

And finally, at some point after detecting the mouse click, show/hide the popover.

if (/* menulet is active */) {
    [popover showRelativeToRect:/* menulet view frame */
                         ofView:/* menulet view */
                  preferredEdge:NSMinYEdge];
} else {
    [popover performClose:nil];
}

You need a bit of NSWindow swizzling to get text fields working inside the popover.

I've prepared a minimal Xcode project with these ideas and some glue: PopoverMenulet.

Upvotes: 57

Related Questions