6

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

Matthew S.
  • 711
  • 1
  • 5
  • 22

4 Answers4

3

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

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

Matthew S.
  • 711
  • 1
  • 5
  • 22
2

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

to get notified when the menu bar showing or hiding.

Jay
  • 6,572
  • 3
  • 37
  • 65
  • Thanks for your reply. Could you please elaborate on how to implement notifications for the `NSStatusBarWindow`; I could not find find that class. – Matthew S. Jan 30 '16 at 05:14
  • So NSStatusBarWindow is part of Apple's private framework, is there a way to do this so that the app can be posted to the appstore? – Matthew S. Feb 22 '16 at 03:43
  • No guarantee of course but all we're doing in our app is checking for the string `NSStatusBarWindow` to identify the class… no harm comparing strings :-) – Jay Feb 27 '16 at 10:03
  • I like your thinking :D. But how do you observe these notifications on all windows to check for the `NSStatusBarWindow `? I only can get notifications on my application's window. (Thanks for all your help) – Matthew S. Mar 03 '16 at 00:00
2

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.

Andrew
  • 7,630
  • 3
  • 42
  • 51
-1

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • 1
    This doesn't seem to work, the presence of the AutoHideMenuBar options seem to correspond only to whether the auto hide button in macOS settings is checked, and I can't seem to find a circumstance where the HideMenuBar option is present, regardless of whether or not the menu bar is on screen. – Josh Jan 24 '19 at 23:21