问题描述
我在做一个应用程序,它居住在状态栏。点击状态项时,NSPopover弹出。
I'm making an app which lives in status bar. When status item is clicked, NSPopover pops up.
它看起来像这样:
这里是问题:我想要的是瞬态,也就是说,如果我点击popover之外的任何地方,它将关闭。
Here's the problem: I want it to be "transient", that is if I click anywhere outside of the popover, it will close. And while NSPopoverBehaviorTransient works fine when popover is in a window, it doesn't work when it's in status bar.
我自己如何实现这样的行为?
How can I implement such behavior myself?
推荐答案
原来很容易:
- (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)
}
然而,不容易的是,有一个令人讨厌的问题与弹出的NSStatusItem popover弹出(当任务控制被调用或空间切换到一个全屏窗口时,它的行为不符合要求)。我不得不实现一个自定义窗口,它总是漂浮在NSStatusItem之上,并处理切换到一个全屏窗口等。这看起来很容易,但显然状态项目不是为这样的东西设计的);
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 ;)
这篇关于popover在状态栏中时的NSPopover瞬态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!