LSUIElement 的行为与 activateIgnoringOtherApps 不一致
具体来说,它在文本字段焦点方面的行为不一致。
我有一个 LSUIElement 弹出状态菜单。该菜单中有一个包含文本字段的视图。文本字段需要是可选择的——不一定是默认选择的,但可以选择任意一个。
单击状态项时,它会触发
[NSApp activateIgnoringOtherApps:YES];
并且大约有一半的时间有效。*状态菜单的另一半似乎认为自己“在后台”,即使单击也不会让我将焦点放在文本字段上它。 (我知道状态项点击触发正在触发,因为上面有一个 NSLog。)
这是 Apple 处理这些状态项的方式中的错误,还是我对 activateIgnoringOtherApps 处理不当?
*事实上,似乎只有在激活另一个应用程序后第一次才会失败。之后就可以正常工作了。
完整的片段:
-(void)statusItemClicked:(id)sender {
//show the popup menu associated with the status item.
[statusItem popUpStatusItemMenu:statusMenu];
//activate *after* showing the popup menu to obtain focus for the text field.
[NSApp activateIgnoringOtherApps:YES];
}
Specifically, it behaves inconsistently regarding text field focus.
I have an LSUIElement popping up a status menu. Within that menu there is a view containing a text field. The text field needs to be selectable -- not necessarily selected by default, but whichever.
When the status item is clicked, it triggers
[NSApp activateIgnoringOtherApps:YES];
And it works, about half the time.* The other half the status menu seems to consider itself "in the background" and won't let me put focus on the text field even by clicking on it. (I know the status item click-trigger is firing b/c there's an NSLog on it.)
Is this a bug in the way Apple handles these status items, or am I mishandling activateIgnoringOtherApps?
*In fact, it seems to fail only the first time after another app is activated. After that it works fine.
The complete snippet:
-(void)statusItemClicked:(id)sender {
//show the popup menu associated with the status item.
[statusItem popUpStatusItemMenu:statusMenu];
//activate *after* showing the popup menu to obtain focus for the text field.
[NSApp activateIgnoringOtherApps:YES];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据经验,我知道您必须在弹出包含文本字段的菜单后调用
activateIgnoringOtherApps:
。因此,您需要按以下顺序执行此操作:根据您所说的,听起来您的应用程序激活得太晚了,因此在您第一次单击该项目时它没有被激活,但它已经在后续点击。
I know from experience that you have to call
activateIgnoringOtherApps:
after you've popped up the menu that contains your text field. So you would need to do it in this order:Based on what you've said, it sounds like your application is activating too late, so that it's not getting activated the first time you click on the item, but it is already activated on subsequent clicks.
最后想出了一个解决方法。
不要在点击处理程序中弹出菜单,而是激活应用程序,然后安排一个无延迟的 NSTimer 来弹出菜单:
pop:
在下一帧上调用,因此延迟是难以察觉的,但足够长 < code>activateIgnoringOtherApps: 执行在同一帧中弹出菜单时阻止其按预期工作的任何操作。Finally came up with a workaround for this.
Instead of popping the menu in your click handler, activate the app then schedule an NSTimer with no delay that pops the menu:
pop:
is called on the next frame so the delay is imperceptible but long enough foractivateIgnoringOtherApps:
to do whatever was preventing it from working as expected when popping the menu in the same frame.