How do I make a Menubar Application with a NSPopover?

后端 未结 1 1941
时光取名叫无心
时光取名叫无心 2020-12-23 15:21

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

相关标签:
1条回答
  • 2020-12-23 15:37

    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.

    0 讨论(0)
提交回复
热议问题