0

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)

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
  • It's not clear what your problem is? What about the default behavior is not desirable? The `applicationShouldHandleReopen:` method is called whenever the user single clicks on the dock icon immediately before your app is made the active app. The dock menu for your app is displayed whenever the user click and holds or right clicks on your dock icon. A second single click on your dock icon when the menu is displayed will close it without bringing your app to the foreground. You can specify custom items to appear in this dock menu, but not with `applicationShouldHandleReopen:`. – torrey.lyons Jul 05 '12 at 01:17
  • Sorry I wasn't clear enough : I using a custum menu that shows up when clicking left. – Matthieu Riegler Jul 05 '12 at 01:19
  • I see. Will the standard right click Dock menu customization used by the Finder and other applications work for you? Adding static or dynamic menu items to this menu is described [here](http://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/customizing_docktile/docktasks_cocoa/docktasks_cocoa.html#//apple_ref/doc/uid/TP30000986-CH3-SW6). Popping up a Dock-like menu when the user left clicks will surprise some users and will have the UI weirdness that left and right click present different menus. – torrey.lyons Jul 05 '12 at 01:49

1 Answers1

0

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)

Community
  • 1
  • 1
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134