Question

I've an app that opens a menu when clicking on the dock icon (- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag)

The menu "stops" the execution of the current thread until it's closed. Therefore, I only receive calls of applicationShouldHandleReopen: once the menu is closed.

I thought that with a timer I could check if there is such method calls in the queue. But is it this possible ? Or is there an other way to handle my problem ? (I want to close my menu on the second click on the dock icon)

Was it helpful?

Solution

The solution I found to my problem :

I used this code Getting the position of my application's dock icon using Cocoa's Accessibility API to get access to the position and size of my dock icon.

Then with NSEvent + (id)addGlobalMonitorForEventsMatchingMask:(NSEventMask)mask handler:(void (^)(NSEvent*))block with NSLeftMouseUp as mask, I manage to catch every click on the screen

Finally, I just had to check if my click was with the CGRect of my dockIcon. (I had to trick a little, because after the menu is dismissed, applicationShouldHandleReopen: is called because I click on it)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top