Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiprocessing Qt app: How can I limit it to a single icon in the MacOS/X Dock?

I've got a Qt application that (for reasons that are outside the scope of this question) launches each of its windows as a separate process.

This works fine, but under MacOS/X, each window/process shows up as a separate entry in the Dock. Thus when the user has a dozen windows open, there are a dozen identical icons in the Docks, which isn't very helpful (since it's hard to tell which is which, and the icons start to get small).

Is there some way to tell the Dock to group all of these processes together under a single Dock icon? (a Mac-specific API would be fine)

like image 962
Jeremy Friesner Avatar asked Jun 07 '13 23:06

Jeremy Friesner


People also ask

Is there a way to keep an icon of each window separately on Mac Dock?

Right click on the Dock, select "Dock Preferences", and from there, uncheck the box that says "Minimize windows over the icon of the application". That will make that every instance of an app have its dedicated icon on the right side of the Dock (to the left of the Trash App icon).

How to add an icon to the Dock on a Mac?

Add an item to the Dock: Drag apps to the left side of (or above) the line that separates the recently used apps. Drag files and folders to the right side of (or below) the other line that separates recently used apps. An alias for the item is placed in the Dock.

How to remove icon from Dock Mac?

Use the right-click menu instead. You can also use a drop down menu to remove an item from the Dock: Right-click the icon (or hold down Control and click). Hover over "Options." Select "Remove from Dock."


2 Answers

You can use the following snippet to hide the Dock tile of a process that is not active:

 - (void)applicationWillResignActive:(NSNotification *)notification
{
    ProcessSerialNumber psn = {0, kCurrentProcess};
    TransformProcessType(&psn, kProcessTransformToBackgroundApplication);
    if([self.window isVisible])
    {
        [self.window performSelector:@selector(orderFrontRegardless) withObject:nil afterDelay:0.05];
    }
}

- (void)applicationWillBecomeActive:(NSNotification *)notification
{
    ProcessSerialNumber psn = {0, kCurrentProcess};
    TransformProcessType(&psn, kProcessTransformToForegroundApplication);
    SetFrontProcessWithOptions(&psn, kSetFrontProcessCausedByUser);
}

Just add the above code to your app delegate and the app's Dock icon will vanish when the process resigns active.
To keep the window visible after the process was turned into an UIElement application, send a orderFrontRegardless message. (Very hacky, I know - but that must be the price for the non-standard window/process handling)

Probably you should also maintain a Dock menu that allows your users to select hidden windows.
You could dynamically add entries from your "main" app. Details can be found in the "Dynamically Adding Menu Items With the Application Delegate" section of the Dock Tile Programming Guide.

Update:
I slightly changed the code sample above as the previous approach led to a non-responsive main menu after re-activating the app.

like image 131
Thomas Zoechling Avatar answered Sep 22 '22 16:09

Thomas Zoechling


Call QSystemTrayIcon::hide().

If that doesn't work, here are some things you can try:


Use NSApplication's setActivationPolicy: method

Way 1: [NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory];

Way 2: [NSApp setActivationPolicy: NSApplicationActivationPolicyProhibited];


OR

Use LSUIElement (LSUIElement=1).

You can also put this in your pfile: <key>LSUIElement</key> <string>1</string> or make Qt do it for you.


There is also some discussion about OSX menus on the Qt forums. In addition, you could try to use QFocusEvent to check when the app loses/gains focus and then update menus accordingly.


Sources

  • Start a GUI process in Mac OS X without dock icon

  • How to create a helper application (LSUIElement) that also has a (removable) dock icon

  • How to hide the Dock icon

  • http://www.cocoabuilder.com/archive/cocoa/229461-uielement-off-on.html#229461

  • QT on OSX: Tray Icon - Icon Dock Problem

like image 27
user2448027 Avatar answered Sep 22 '22 16:09

user2448027