SOLVED (4th AUGUST):
After much mucking around, I eventually solved this with an undocumented API argument:
// Adds undocumented "appearance" argument to "popUpMenuPositioningItem":
@interface NSMenu (MISSINGOrder)
- (BOOL)popUpMenuPositioningItem:(id)arg1 atLocation:(struct CGPoint)arg2 inView:(id)arg3 appearance:(id)arg4;
@end
I can now use this hidden appearance
argument without having to create a invisible NSWindow
which is great!
UPDATE (3rd AUGUST):
Interestingly, when I create a completely new Xcode project, the following code works as you'd expect:
#import <Cocoa/Cocoa.h>
int main(int argc, const char * argv[]) {
NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];
[menu insertItemWithTitle:@"Beep" action:nil keyEquivalent:@"" atIndex:0];
[menu insertItemWithTitle:@"Honk" action:nil keyEquivalent:@"" atIndex:1];
NSPoint mouseLocation = [NSEvent mouseLocation];
NSRect contentRect = NSMakeRect(mouseLocation.x, mouseLocation.y, 0, 0) ;
NSWindow *tmpWindow = [[NSWindow alloc] initWithContentRect:contentRect
styleMask:0
backing:NSBackingStoreBuffered
defer:NO] ;
tmpWindow.releasedWhenClosed = NO ;
tmpWindow.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantDark] ;
[tmpWindow orderFront:nil] ;
[menu popUpMenuPositioningItem:nil atLocation:NSMakePoint(0,0) inView:tmpWindow.contentView] ;
[tmpWindow close] ;
}
So the question is, why doesn't this exact code work within the Hammerspoon code-base? Any ideas?
Short Story:
How can I display a NSMenu
popup with a dark appearance at a specific system coordinate location that work with multiple monitors?
Long Story:
I'm currently trying to make an addition to Hammerspoon which allows popup menus to have a dark appearance.
Here's the unmodified original code, which works great (albeit, it's always has a "light" appearance).
Here's my work-in-progress modifications.
I'm currently using a hidden NSWindow
which "contains" the NSMenu
popup, so that I can make the NSWindow
a dark appearance.
This works great on a single screen, but when you have dual screens, the popup is only visible on the screen which has the macOS Menubar (as selected in the Arrangement tab of macOS's Display Preferences).
I've tried a bunch of different things to solve (see here), but haven't had any luck yet.
I know the NSWindow
is displaying correctly on both screens - it's just the popup that's not revealing in the correct position, so I'm certain I just have the maths wrong in terms of the popup location (in the context of the NSWindow
).
Any help would be HUGELY appreciated!