4

问题:我必须按照它下方的应用程序的 NSStatusItem 弹出窗口/窗口。

对此的一般解决方案(就像这里这里建议的那样)是将自定义视图添加到 NSStatusItem 并获取视图的窗口框架(statusItem.view.window.frame),并通过 statusItem 的 NSWindowDidMoveNotification 通知连接到移动事件。视图.窗口。

除非用户连接比前一个显示器高的外接显示器(例如 Mac Book 用户连接外接显示器),否则此解决方案可工作 99%,在这种情况下 statusItem.view.window.frame 将不正确,X 坐标将实际上是正确的,但 Y 坐标将与小屏幕中的相同!

我已经检查过,当您单击状态项时,大多数具有弹出窗口的菜单栏应用程序都像我所描述的那样放错了位置。

我的解决方案是不使用此帧的 Y 坐标,而是使用相应的 NSScreen 的 visibleFrame 高度,如下所示:

NSScreen *screenWithMenuBar = [[NSScreen screens] objectAtIndex:0];
    if( screenWithMenuBar ) {
        // correct the top position by the 'screen with the MenuBar's height
        //  workaround: without this the window will be off-placed when the user plugs in an external display,
        //      a display with more height
        NSRect visibleRect = [screenWithMenuBar visibleFrame];
        originPoint.y = visibleRect.size.height + visibleRect.origin.y;
    }

正如Apple官方文档中所述,第一个屏幕应该始终是包含菜单栏的屏幕,因此它应该可以工作,但对我来说这似乎太过分了。

各位有遇到过这个bug吗?你找到更好的解决方案了吗?

4

0 回答 0