跳到主要內容

SWT now supports Mac Application Menu (About, Preferences, and Quit)


Hooking Mac's application menu is no longer a trouble issue for SWT (standard widget toolkits) developers.  Display.getSystemMenu() is a new API returns the system-provided menu for the application.  All you have to do is comparing a MenuItem's id with various SWT.ID_* constants.  You can even change default system MenuItem's name.

  • SWT.ID_QUIT
  • SWT.ID_HIDE
  • SWT.ID_HIDE_OTHERS
  • SWT.ID_PREFERENCES
  • SWT.ID_ABOUT

Display.getSystemMenu() returns null if not running on Mac OS X.  Here's a sample usage:

Menu systemMenu = Display.getDefault().getSystemMenu();
if (systemMenu != null) {
    MenuItem sysItem = getSystemItem(systemMenu, SWT.ID_PREFERENCES);
    sysItem.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            System.out.println("Preferences");
        }
    });
    sysItem.setText("My Preferences");
}

You need a getSystemItem() function to make above code work.

static MenuItem getSystemItem(Menu menu, int id) {
    for (MenuItem item : menu.getItems()) {
        if (item.getID() == id) return item;
    }
    return null;
}

That's all.  Check SWT snippet354 for a detailed example of how you can use this API in a cross-platform application.  It's much easier than my previous way hooking application menu by CocoaUIEnhancer.

留言

張貼留言

熱門文章

差不多食譜:搖元宵 Yuan Xiao

元宵節就要到囉!除了放天燈、猜燈謎之外,這天還要做什麼呢?當然就是吃元宵啦~

「抓烏龜」的麻將遊戲

今天要和大家分享一個打發時間的簡單遊戲——抓烏龜。這可是我老爸老媽特別從美國學回來的,是個名符其實的「海歸」遊戲,據說是在下雪時無聊打發時間用的。

差不多食譜:壽桃 Birthday Bunns

「壽桃」可不是老人家生日的專利,小巧玲瓏的壽桃超級受到小朋友歡迎,直說「好可愛喔!」其實壽桃就是一種造型饅頭/包子,只要掌握了這些方法,要做其他的造型都沒問題。