Detect if menu bar is hidden or visible

大憨熊 提交于 2019-12-10 15:16:33

问题


Is there a way in Cocoa to receive a notification (or something similar) when the menu bar becomes hidden or visible? I tried looking around and have not found any information on this topic.

Thanks


回答1:


I solved this by using Carbon's menu event handlers.

I registered for events kEventMenuBarHidden and kEventMenuBarShown under the class kEventClassMenu.




回答2:


Optionally watch out for (Cocoa) notifications for an object of class NSStatusBarWindow and notifications like

  • NSWindowDidChangeOcclusionStateNotification
  • NSWindowWillCloseNotification

to get notified when the menu bar showing or hiding.




回答3:


If you only need the current state of the menu bar, another approach is to use the visibleFrame property of NSScreen:

The returned rectangle is always based on the current user-interface settings and does not include the area currently occupied by the dock and menu bar.

However, this won't be sufficient by itself if you need to be notified of menu bar visibility changes.




回答4:


I believe the correct approach to this is to use Key-Value Observing (KVO) to observe the presentationOptions or currentSystemPresentationOptions property of the application object (NSApp or [NSApplication sharedApplication]). When that changes, check its value to see if it includes NSApplicationPresentationHideMenuBar or NSApplicationPresentationAutoHideMenuBar. If it does, then the menu is hidden (or hides when the cursor is not near the top of the main screen.

The difference between presentationOptions and currentSystemPresentationOptions is whether you're interested in whether the calling app has hidden its menu bar or whether the active app (which may be another app) has hidden its menu bar. The latter indicates whether the user can see any menu bar.



来源:https://stackoverflow.com/questions/35008501/detect-if-menu-bar-is-hidden-or-visible

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