NSPopover transiency when popover is in status bar

前端 未结 2 1869
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 18:31

I\'m making an app which lives in status bar. When status item is clicked, NSPopover pops up.

It looks like this:

2条回答
  •  悲哀的现实
    2021-01-30 19:04

    It turned out to be easy:

    - (IBAction)openPopover:(id)sender
    {
        // (open popover)
    
        if(popoverTransiencyMonitor == nil)
        {
            popoverTransiencyMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDownMask|NSRightMouseDownMask handler:^(NSEvent* event)
                                        {
                                            [self closePopover:sender];
                                        }];
        }
    }
    
    - (IBAction)closePopover:(id)sender
    {
        if(popoverTransiencyMonitor)
        {
            [NSEvent removeMonitor:popoverTransiencyMonitor];
    
            popoverTransiencyMonitor = nil;
        }
    
        // (close popover)
    }
    

    What wasn't easy, though, is that there are nasty issues with having a popover pop out of NSStatusItem (it didn't behave as desired when Mission Control was invoked or space switched to a full-screen window). I had to implement a custom window that always floats above the NSStatusItem and deals with switching to a full-screen window etc. It seemed easy, but clearly status items weren't designed for something like that ;)

提交回复
热议问题