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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top