关于Objective C:popUpMenuPositioningItem:atLocation:inView:切换到另一个应用程序时挂起

popUpMenuPositioningItem:atLocation:inView: hangs when switching to another application

我正在开发无坞站应用程序(LSUIElement是)。当用户单击相应的NSStatusItem或使用键盘快捷键时,该应用程序将弹出菜单。

我的问题是,每当用户切换到另一个应用程序时(使用?-TAB),所有以编程方式弹出菜单的方法都将挂起,而无需先关闭菜单。我已经尝试过popUpMenuPositioningItem:atLocation:inView:popUpContextMenu:withEvent:forView:NSStatusItem(popUpStatusItemMenu:)上的相应方法。

如果用户使用ESC键关闭菜单,则一切正常,但是,如果用户切换到其他应用程序,则上述方法将永远不会返回(它们似乎同步运行并在关闭菜单时返回)。该应用程序不会崩溃,并且有一些技巧可以重新获得控制权(调用曝光或单击弹出菜单的任何NSStatusItem)。

如果应用程序具有停靠图标(即将LSUIElement设置为false),则问题消失。

这是使用键盘快捷键时弹出菜单的代码:

1
2
3
[mainMenu popUpMenuPositioningItem:[mainMenu itemAtIndex:0]
                        atLocation:[NSEvent mouseLocation]
                            inView:nil];

这是单击NSStatusItem时弹出菜单的代码:

1
2
3
4
- (void)mouseDown:(NSEvent *)event
{
    [statusItem popUpStatusItemMenu:[statusItem menu]];
}

mouseDown:方法位于附加到NSStatusItem的自定义NSView中。

关于如何解决此问题的任何想法?

感谢您的帮助。

更新

此问题还与激活的应用程序有关(我在显示菜单之前使用[NSApp activateIgnoringOtherApps:YES];,或者在某些情况下无法使用键盘导航菜单)。


问题似乎是由于应用程序激活和popUpMenu在同一事件周期中发生的。我找到了一种解决方法,在这篇文章中有更详细的描述。

是摘要,您需要先激活应用程序(使用[NSApp activateIgnoringOtherApps:YES];),然后再弹出菜单,确保此操作在新的事件周期中发生:您可以使用NSTimer触发菜单来实现。铅>

1
2
3
4
5
6
7
8
9
10
11
- (void) someMethod {
    // Some code
    [NSApp activateIgnoringOtherApps:YES];
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(showMenu) userInfo:nil repeats:NO];
    //Some other code
}

- (void) showMenu {
    // This will show the menu at the current mouse position
    [aMenu popUpMenuPositioningItem:[mainMenu itemAtIndex:0] atLocation:[NSEvent mouseLocation] inView:nil];
}