18

我有一个可变长度NSStatusItem,我希望尽可能保持可见,即使这意味着只显示部分内容,但是当我的项目足够宽以运行到应用程序的菜单栏时,它会完全隐藏。有没有办法判断这种情况何时发生,以便我可以缩小视图以适应可用空间?

我已经尝试过自定义视图,覆盖所有viewWill*方法、框架设置器和显示方法,并定期检查包含窗口是否已移动或隐藏。我找不到任何方法来判断我的物品何时太长。

4

2 回答 2

1

This depends on whether your status item application can detect the number of menu items in the OS X menu bar. A quick search through apple documentation shows that there are no public APIs provided by Apple for the purpose of doing this. To my knowledge, there are no private ones available too.

So I would recommend instead that you make your status item small by default and expanded when clicked by the user.

Edit: Actually look at the discussion here: a really clever way to detect if your status item is being hidden. So once you have detected that it is being hidden, you can shrink it so that it reappears.

于 2011-11-16T15:13:24.780 回答
1

这是一个基于hollow7引用的讨论的完整工作示例:

self.statusItem.title = @"Message that will be truncated as necessary.";
while (self.statusItem.title.length > 0) {
    CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenAboveWindow, (CGWindowID)self.statusItemWindow.windowNumber);
    if (CFArrayGetCount(windowList) > 1) {
        CFRelease(windowList);
        self.statusItem.title = [self.statusItem.title substringToIndex:self.statusItem.title.length - 1];
    } else {
        CFRelease(windowList);
        break;
    }
}

剩下的一个棘手的部分是获取 NSStatusItem 窗口。到目前为止,我已经找到了两种获取它的方法。

1 - 有一个私有方法称为_window. 您可以像这样使用它:

self.statusItemWindow = [self.statusItem performSelector:@selector(_window)];

2 - 这有点复杂,但我认为这更有可能通过 Apple 对 Mac App Store 中私有方法使用的静态分析:

将 的目标和操作设置为NSStatusItem您控制的方法,如下所示:

self.statusItem.target = self;
self.statusItem.action = @selector(itemClicked:);

然后在调用的方法中访问窗口:

- (void)itemClicked:(id)sender {
    self.statusItemWindow = [[NSApp currentEvent] window];
}
于 2014-01-27T22:24:56.217 回答