单击外部时隐藏 MAAttachedWindow

发布于 2024-10-12 13:04:07 字数 815 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-10-19 13:04:07

您应该能够通过窗口的委托方法来执行此操作:

- (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.

时光沙漏 2024-10-19 13:04:07

这是基于Carter Allen的回答,但也许会对某人有所帮助,因为我花了几个小时试图找出EXEC_BAD_ACCESS背后的原因,总之你可以' t release 在他的 windowDidResignKey 通知中的 attachedWindow,所以使用 autorelease

- (void)windowDidResignKey:(NSNotification *)aNotification {
    NSLog(@"MainWinDelegate::windowDidResignKey: %@", [aNotification object]);

    if (fAttachedWindow && [aNotification object] == fAttachedWindow) {
        [window removeChildWindow:fAttachedWindow];
        [fAttachedWindow orderOut:self];
        [fAttachedWindow autorelease];
        fAttachedWindow = nil;
    }
}

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't release the attachedWindow inside his windowDidResignKey notification, so use autorelease:

- (void)windowDidResignKey:(NSNotification *)aNotification {
    NSLog(@"MainWinDelegate::windowDidResignKey: %@", [aNotification object]);

    if (fAttachedWindow && [aNotification object] == fAttachedWindow) {
        [window removeChildWindow:fAttachedWindow];
        [fAttachedWindow orderOut:self];
        [fAttachedWindow autorelease];
        fAttachedWindow = nil;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文