Think And Build

Tips&tricks

Opening the Main Window from the Dockbar Menu Icon

Posted on .

Opening the Main Window from the Dockbar Menu Icon

Introduction

In this simple tip I will show you how to open the main window of your application using the Dockbar icon.

Your users probably expect the main window of your application to become visible through a click over its Dockbar icon, but, unless you’ve managed this behavior, when they click the icon nothing happens.

Since we don’t want to disappoint the users’ expectations, let’s see how to do it:

The message applicationShouldHandleReopen:hasVisibileWindows: is sent to the Application Delegate when users click over the Dockbar icon and when users double click the application executable. We can implement this method (that is part of the Application Delegate protocol) to handle that action.

This is an example snippet you can add to your Application Delegate to open the main window whenever a user clicks on the Dockbar icon.

[code lang=”Obj-C”]
– (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag{
if(!flag){
[[self window] makeKeyAndOrderFront:self];
}

return YES;
}

Return YES if you want the application to reopen itself or NO if you want the application to do nothing.
The Bool flag indicates if the NSApplication object found any visible windows in your application.

This example is extremely simple: if no window is visible, the code only calls the function makeKeyAndOrderFront showing the main window. Warning: miniaturized windows and windows in the dock are considered visible.

Yari D'areglia

Yari D'areglia

https://www.thinkandbuild.it

Senior iOS developer @ Neato Robotics by day, game developer and wannabe artist @ Black Robot Games by night.

Navigation