The application I'm developing needs to be able to veto menubar clicks. There are a number of ways to be notified of when a menu is clicked (NSMenu
notifications and NSMenu
delegation, mostly), but as far as I can see, none of them will let you refuse the action.
The solution I've been thinking of is to use a borderless and transparent window, and overlay it on the menu bar. By setting the window level to a high enough value (NSStatusWindowLevel
in this case), it is effectively positioned and drawn above the menu bar, catching mouse events that would otherwise go to it. That way, I have what I'm looking for.
The problem with that solution is that it also masks events for the status bar (the right-aligned, global menu items), and I don't need, or want, to veto those. To fix that issue, I would need to be able to tell either the span of the status bar, or the span of the menu bar, but I found no obvious way for either. NSApp.mainMenu.size
returns the size of the main menu as if it was laid out in a popup menu, which is obviously not what I need.
Is there any better way to mask events to the menu bar, or is there any way to find its width excluding the status bar?
This probably all sounds very dirty, and it is. I'm doing it for an application-level emulator for an old platform where the application was responsible for showing menus when the user clicks in the menu bar, and therefore could do pretty much anything it wants when that happens.