Highlight NSStatusItem when triggered programmatically

风格不统一 提交于 2019-12-07 06:59:05

问题


I have an NSStatusItem with an attached menu that I'm triggering with a global hotkey. If I click the menu item I get the highlight as usual, if I use the hotkey the highlight isn't triggered. Does anyone know a way to trigger the highlight?

I've tried overriding the view and drawing it myself in drawRect but if there is a nicer way to do it I'd love to hear. Thanks!

The reason I don't want to override the view is then I have to handle icon positioning, clicking to activate the menu, etc.


回答1:


This does the magic in macOS 10.13.6 with Xcode 10.

guard let m = statusItem.menu else { return }
statusItem.button?.isHighlighted = true
statusItem.popUpMenu(m)
statusItem.button?.isHighlighted = false

Please note that last line is required to de-highlight the icon when the menu gets closed.




回答2:


Use:

[[statusItem button] highlight:true];

As it turns out setHighlighted: and highlight don't do the same thing:
NSStatusBarButton keep highlighted



来源:https://stackoverflow.com/questions/24854217/highlight-nsstatusitem-when-triggered-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!