2015-11-02 43 views
4

我無法找到關於如何在Java中設置OS X(在OS X 10.10 Yosemite中引入)的暗模式菜單欄圖標的任何信息。我看過這個帖子http://mail.openjdk.java.net/pipermail/macosx-port-dev/2014-October/006740.html,但沒有任何答案。我知道這已經在Objective-C的How to detect dark mode in Yosemite to change the status bar menu icon這裏討論過了,但不確定這是否可以在Java中以相似的方式完成。MenuBar Java中OS X上Dark模式的圖標

有沒有辦法實現這個?

回答

7

我有同樣的問題,通過調用defaults read命令和分析退出代碼解決它:

/** 
* @return true if <code>defaults read -g AppleInterfaceStyle</code> has an exit status of <code>0</code> (i.e. _not_ returning "key not found"). 
*/ 
private boolean isMacMenuBarDarkMode() { 
    try { 
     // check for exit status only. Once there are more modes than "dark" and "default", we might need to analyze string contents.. 
     final Process proc = Runtime.getRuntime().exec(new String[] {"defaults", "read", "-g", "AppleInterfaceStyle"}); 
     proc.waitFor(100, TimeUnit.MILLISECONDS); 
     return proc.exitValue() == 0; 
    } catch (IOException | InterruptedException | IllegalThreadStateException ex) { 
     // IllegalThreadStateException thrown by proc.exitValue(), if process didn't terminate 
     LOG.warn("Could not determine, whether 'dark mode' is being used. Falling back to default (light) mode."); 
     return false; 
    } 
} 

現在您可以加載不同的圖像,並在java.awt.TrayIcon使用它們:

// java.awt.* controls are well suited for displaying menu bar icons on OS X 
final Image image; 
if (isMacMenuBarDarkMode()) { 
    image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/tray_icon_darkmode.png")); 
} else { 
    image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/tray_icon_default.png")); 
} 
+0

我們可以用python做同樣的事嗎? – impossible

+1

@ArewegoodQ當然,以上並不限於Java。只需執行一個新的shell進程並評估退出值。 –

+0

感謝您的建議。你介意回答這個問題嗎? http://stackoverflow.com/questions/37108924/python-code-to-detect-dark-mode-in-yosemite-to-change-the-status-bar-menu-icon – impossible