Eclipse isn't a perfect app on Mac platform. Right from unzipping to "install" to not being packaged as a single .app file, its far from being accepted as a perfect application. But beyond these things, Eclipse does provides some support for Mac platform. Lets look at those features and look at some more general guidelines on creating RCP apps that mingle with native Mac apps.

 

I'm going to take the RCP mail as a base for this tutorial. Lets first create one using the PDE's wizard. Here is what it looks like:


RCP Mac App

It all looks good, till I click the File menu:


Mac RCP File Menu


Two things wrong about this. First the keybindings use Ctrl as the primary modifier. In other platforms Ctrl is the primary modifier - (Ctrl+S, Ctrl+C, Ctrl+V, ...) Although Macs do have the Ctrl key, Cmd is the primary modifier. We use Cmd+S, Cmd+C, Cmd+V and so on. So when you define you binding don't use Ctrl directly. Instead use M1. This is automatically translated into Cmd in Mac and Ctrl in other platforms. See the documentation of the bindings for more info. So now we change the bindings from:

   <extension
point="org.eclipse.ui.bindings">
<key
commandId="com.eclipse-tips.mac.open"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="CTRL+2">
</key>
<key
commandId="com.eclipse-tips.mac.openMessage"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="CTRL+3">
</key>
<key
commandId="org.eclipse.ui.file.exit"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="CTRL+X">
</key>
</extension>



to

 

   <extension
point="org.eclipse.ui.bindings">
<key
commandId="com.eclipse-tips.mac.open"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M1+2">
</key>
<key
commandId="com.eclipse-tips.mac.openMessage"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M1+3">
</key>
<key
commandId="org.eclipse.ui.file.exit"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M1+X">
</key>
</extension>


Now lets look at the menu again:



Good. The same menu will have the bindings with the Ctrl key in other platforms.

Mac RCP File Menu

The Application Menu:


Now the second problem is the Exit menu item. It should go into the Application Menu. Right next to the Apple menu (which is system managed and displayed by the Apple icon) is the Application menu. Its the left most Menu for a Mac application and contains global menu items that are not specific to any window. Items such as 'About', 'Exit' (or rather 'Quit' in Mac terms) goes here. The Application Menu is easily distinguishable, because its displayed in bold and usually the menu text is the name of the application.

Lets look at our Application menu:



Ouch, that isn't right. Our application is written in Java, but that shouldn't be the name of the application. This can be easily changed with a .product file. Lets use the PDE's Product Configuration Wizard to create one. Eclipse takes the Name that you specify in the product configuration and updates the application's name.

RCP Mac Application Menu

 

Now how to push the menu items into the Application menu? Simple, if you are using the ActionFactory class to create the actions. Eclipse automatically maps the About, Preferences and Quit actions to the Application Menu. But its your responsibility to hide these menu items in other places. You can use JFace Util class to determine whether you are on Mac or not. The advantage of using the standard ActionFactory items is that the menu items are formatted as 'About <app name>' and 'Quit <app name>'. In case you want to use your own actions, you have to make sure that you use the same actionId as the standard commands ('preferences', 'quit', etc)


		// in your  ActionBarAdvisor.makeActions()
preferencesAction = new Action("Preferences") {
public void run() {
MessageDialog.openInformation(window.getShell(), "Preferences", "Some settings to show");
};
};
// make sure to use the same id as the ActionFactory one
preferencesAction.setId("preferences");
register(preferencesAction);

// and then in ActionBarAdvisor.fillMenuBar

// File
fileMenu.add(newWindowAction);
fileMenu.add(new Separator());
fileMenu.add(messagePopupAction);
fileMenu.add(openViewAction);
fileMenu.add(new Separator());

ActionContributionItem preferencesActionItem = new ActionContributionItem(preferencesAction);
fileMenu.add(preferencesActionItem);
ActionContributionItem exitActionItem = new ActionContributionItem(exitAction);
fileMenu.add(exitActionItem);

// Help
ActionContributionItem aboutActionItem = new ActionContributionItem(aboutAction);
helpMenu.add(aboutActionItem);

if(Util.isMac()) {
preferencesActionItem.setVisible(false);
exitActionItem.setVisible(false);
aboutActionItem.setVisible(false);
}





If you look at the above code, I open the preferences dialog like with the normal style. While its all good in other platforms, not-so-great in Mac. Why? Because Macs support the concept of Sheets. Sheets are dialogs that are attached to the window. Whenever you are opening a Modal Dialog, prefer to use the SWT.SHEET style. It is a special behaviour in Macs.

Normal Border style:

 

Sheet style:

 

 

As seen above Sheets will not have a title bar, so don't have any must-to-read information in the title of the dialog.


Search & Cancel:


Let us add a text box in the tool bar, that enables to search through all the mails. So in the ActionBarAdvisor.fillCoolBar(), we have:

		toolbar.add(openViewAction);
toolbar.add(messagePopupAction);
toolbar.add(new ControlContribution("search") {

@Override
protected Control createControl(Composite parent) {
return new Text(parent, SWT.BORDER);
}

});




This is how it looks:

 



it looks good for a Windows user, but its ugly for a Mac guy. Whenever you have a search field, try to use the SWT.SEARCH and SWT.ICON_SEARCH and SWT.ICON_CANCEL styles. It beautifies the search widget like:


Lets try to create a new Mail in a separate window. It looks like this

Nice? but again, not in a Mac. In Macs, if a window has any unsaved changes, it should be notified with a dot in the close button. SWT enables this by the method Shell.setModified(). When any of the fields in a window is changed, then use this API to denote it:

 

 

Keybindings


Most of the keybindings in Mac are similar to that of other platforms, except the Cmd key instead of Ctrl. This is easily handled by the M1 modifier. But there are certain keybindings that are unique to Mac. You need to stick to it to. For example, Redo is not Cmd+Y, its Cmd+Shift+Z. Properties is not Alt+Enter, its Cmd+I (and BTW, its called as 'Get Info'), Cmd+M minimizes a window and so on. Check here for a complete list of standard shortcuts in Mac and use them in your key scheme (http://developer.apple.com/documentation/Carbon/Conceptual/Carbon64BitGuide/Introduction/Introduction.html)

Subscribe To

Unless stated, all the text contents of this site is available under Eclipse Public License