How do I make a Menubar Application with a NSPopover?

筅森魡賤 提交于 2019-11-30 10:33:29

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7837439/how-do-i-make-a-menubar-application-with-a-nspopover

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!