gpt4 book ai didi

objective-c - 关闭 NSStatusItem 的自定义窗口

转载 作者:行者123 更新时间:2023-12-03 16:55:24 25 4
gpt4 key购买 nike

当单击 NSStatusItem 时,会弹出一个自定义窗口。代码基于MAAtachedwindow 。一切都工作得很好,但当用户单击其他状态栏项目或其他应用程序等其他内容时,我无法找到关闭窗口的方法。

这是我创建窗口的代码:

    statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:width] retain];

//setup custom status menu view
CGFloat height = [[NSStatusBar systemStatusBar] thickness];
NSRect viewFrame = NSMakeRect(0.0f, 0.0f, width, height);
statusMenuView = [[[_ISStatusMenuView alloc] initWithFrame:viewFrame] retain];
statusMenuView.offset = aOffset;
statusItem.view = statusMenuView;

//setup the window to show when clicked
NSRect contentRect = NSZeroRect;
contentRect.size = aView.frame.size;
statusMenuWindow = [[[NSWindow alloc] initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO] retain];

[statusMenuWindow setLevel:NSPopUpMenuWindowLevel];
[statusMenuWindow setBackgroundColor:[NSColor clearColor]];
[statusMenuWindow setMovableByWindowBackground:NO];
[statusMenuWindow setExcludedFromWindowsMenu:YES];
[statusMenuWindow setOpaque:NO];
[statusMenuWindow setHasShadow:NO];
[statusMenuWindow useOptimizedDrawing:YES];
[[statusMenuWindow contentView] addSubview:aView];
[statusMenuWindow setDelegate:self];

statusMenuView.statusMenuWindow = statusMenuWindow;

这是我显示窗口的方式:

- (void)centerView{
NSRect menuFrame = self.window.frame;
NSRect windowFrame = self.statusMenuWindow.frame;
NSPoint menuPoint = NSMakePoint(NSMidX(menuFrame), NSMinY(menuFrame));
menuPoint.x -= windowFrame.size.width*0.5f;
menuPoint.y -= windowFrame.size.height+self.offset;
[self.statusMenuWindow setFrameOrigin:menuPoint];
[self.statusMenuWindow makeKeyAndOrderFront:self];
}

我希望 windowDidResignKey 委托(delegate)方法能够解决问题,但此配置不会成功。委托(delegate)正在工作,因为 windowDidMove 确实运行了。

- (void)windowDidResignKey:(NSNotification *)notification{
NSLog(@"windowDidResignKey");
[statusMenuView hideView];
}

- (void)windowDidResignMain:(NSNotification *)notification{
NSLog(@"windowDidResignMain");
}

- (void)windowDidMove:(NSNotification *)notification{
NSLog(@"windowDidMove");
}

回顾一下,当用户单击其他任何内容时,如何隐藏我的自定义窗口,就像标准状态栏菜​​单的工作方式一样?

编辑看完Popup例如,我唯一缺少的是我必须子类化 NSPanel 并使其成为关键窗口。

@interface Panel : NSPanel
@end

@implementation Panel

- (BOOL)canBecomeKeyWindow{
return YES;
}

@end

最佳答案

您需要确保您的窗口可以成为关键窗口,并在退出关键窗口时调用您的窗口的 orderOut: 方法。如果您还没有使用自定义 NSWindowController,您应该使用自定义 NSWindowController,在这种情况下,您只需调用其“close”方法即可关闭窗口。

我建议您不要发布一堆代码,而是看一下最近发布的这个优秀的将窗口附加到状态项的示例: Shpakovski Popup Window Example

关于objective-c - 关闭 NSStatusItem 的自定义窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6786453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com