跳到主要內容

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.

留言

張貼留言

熱門文章

差不多食譜:萬聖節的蛋白霜幽靈們 Halloween Meringue Ghosts

說到十月,月底的萬聖節肯定是驚嚇指數最高的一天,到處飄忽的幽靈們更是西洋鬼節的主角。不甘心這天總是被 Trick-or-Treat(不給糖就搗蛋)威脅,差不多食譜今年要開始來反擊,邀請大家來當 Ghostbusters 魔鬼剋星,將這群調皮搗蛋的幽靈們一網打盡。 抓鬼的先決條件就是⋯⋯得有些幽靈鬼怪來抓。這可是練習擠花的好時機,因為幽靈本來就是奇形怪狀的,什麼樣的怪樣子都不奇怪,太精緻反而有點奇怪呢!做好直送嘴巴「喀嚓」,看它還敢不敢來 Trick-or-Treat!

差不多食譜:蜂蜜烤南瓜 Roasted Pumpkin with Honey

還記得差不多食譜分享過用煎的南瓜吧!那是屬於家常菜的做法,現在差不多食譜推出另外一種做法,淋上蜂蜜,用烤箱加上精緻的裝飾,讓家常菜變身成為派對食物。相信當你端出這盤「蜂蜜烤南瓜」時,肯定會吸引不少眼球的注意。

金門。毋忘在莒勒石 “Wu Wang Zai Jyu” Inscription

金門有塊石頭,不去看看感覺怪怪的,看了又覺得實在沒什麼。聰明的你應該猜到了,就是那塊出現在教科書裡頭的「毋忘在莒」,金門的象徵。印象中,刻著「毋忘在莒」的那塊大石頭旁邊光禿禿的。或許從小學畢業二十多年後,我頭一次有機會來拜見這塊石頭,旁的小樹苗已經長成這副模樣了吧!再過幾年,恐怕要在林蔭中尋找「毋忘在莒」這四個字。但真要問起「毋忘在莒」這四個字是什麼意思,我相信不少人需要稍微想一下。