单击外部时隐藏 MAAttachedWindow
我正在使用 MAAttachedWindow 在 NSStatusItem 下显示自定义窗口菜单栏。 一切工作正常,但当用户在窗口外单击时,我找不到隐藏它的简单方法。我想实现这种行为,因为这是用户所期望的。
这是用于显示 MAAttachedWindow
的代码:
- (void)toggleAttachedWindowAtPoint:(NSPoint)pt {
if (!self.attachedWindow) {
self.attachedWindow = [[MAAttachedWindow alloc] initWithView:logView
attachedToPoint:pt
inWindow:nil
onSide:MAPositionBottom
atDistance:5.0];
[self.attachedWindow setLevel:kCGMaximumWindowLevel];
}
if(isVisible)
[self.attachedWindow makeKeyAndOrderFront:self];
else
[self.attachedWindow orderOut];
}
此代码由带有自定义视图的 NSStatusItem
触发,该视图会拦截对其的点击。
I'm using an MAAttachedWindow to display a custom window under a NSStatusItem in the Menubar.
Everything works fine, but I can't find an easy way to hide it when the user clicks outside of the window. I want to implement this behavior because it's what the user expects.
This is the code used to display the MAAttachedWindow
:
- (void)toggleAttachedWindowAtPoint:(NSPoint)pt {
if (!self.attachedWindow) {
self.attachedWindow = [[MAAttachedWindow alloc] initWithView:logView
attachedToPoint:pt
inWindow:nil
onSide:MAPositionBottom
atDistance:5.0];
[self.attachedWindow setLevel:kCGMaximumWindowLevel];
}
if(isVisible)
[self.attachedWindow makeKeyAndOrderFront:self];
else
[self.attachedWindow orderOut];
}
This code gets triggered by an NSStatusItem
with a custom view which intercepts a click on it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够通过窗口的委托方法来执行此操作:
- (void)windowDidResignKey:(NSNotification *)notification
将您自己设置为窗口的委托,并实现它以调用您的切换方法。
You should be able to do this via the window's delegate method:
- (void)windowDidResignKey:(NSNotification *)notification
Set yourself as the window's delegate, and implement that to call through to your toggle method.
这是基于Carter Allen的回答,但也许会对某人有所帮助,因为我花了几个小时试图找出
EXEC_BAD_ACCESS
背后的原因,总之你可以' trelease
在他的windowDidResignKey
通知中的attachedWindow
,所以使用autorelease
:This is based on Carter Allen answer, but maybe will be helpfull to someone as i lost couple of hours trying to figure out the reason behind an
EXEC_BAD_ACCESS
, in short you can'trelease
theattachedWindow
inside hiswindowDidResignKey
notification, so useautorelease
: