LSUIElement 的行为与 activateIgnoringOtherApps 不一致

发布于 2024-08-14 04:28:07 字数 727 浏览 3 评论 0原文

具体来说,它在文本字段焦点方面的行为不一致。

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卸妝后依然美 2024-08-21 04:28:08

根据经验,我知道您必须在弹出包含文本字段的菜单后调用 activateIgnoringOtherApps: 。因此,您需要按以下顺序执行此操作:

- (void)statusItemClicked:sender {
    [statusItem popUpStatusItemMenu:theMenu];
    [NSApp activateIgnoringOtherApps:YES]; // FYI, NSApp is shorthand for [NSApplication sharedApplication]
}

根据您所说的,听起来您的应用程序激活得太晚了,因此在您第一次单击该项目时它没有被激活,但它已经在后续点击。

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:

- (void)statusItemClicked:sender {
    [statusItem popUpStatusItemMenu:theMenu];
    [NSApp activateIgnoringOtherApps:YES]; // FYI, NSApp is shorthand for [NSApplication sharedApplication]
}

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.

肥爪爪 2024-08-21 04:28:07

最后想出了一个解决方法。

不要在点击处理程序中弹出菜单,而是激活应用程序,然后安排一个无延迟的 NSTimer 来弹出菜单:

-(void)pop:(NSTimer *)timer {
    [statusItem popUpStatusItemMenu:theMenu];
}

-(void)statusItemClicked:sender {
    [NSApp activateIgnoringOtherApps:YES];
    [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(pop:) userInfo:nil repeats:NO];
}

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:

-(void)pop:(NSTimer *)timer {
    [statusItem popUpStatusItemMenu:theMenu];
}

-(void)statusItemClicked:sender {
    [NSApp activateIgnoringOtherApps:YES];
    [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(pop:) userInfo:nil repeats:NO];
}

pop: is called on the next frame so the delay is imperceptible but long enough for activateIgnoringOtherApps: to do whatever was preventing it from working as expected when popping the menu in the same frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文