Java中OS X上暗模式的MenuBar图标 [英] MenuBar Icon for Dark Mode on OS X in Java
问题描述
我无法找到有关如何在OS X(在OS X 10.10 Yosemite中引入)中为暗模式设置菜单栏图标的任何信息。我见过这篇文章 http:// mail.openjdk.java.net/pipermail/macosx-port-dev/2014-October/006740.html 但没有任何答案。我知道这已经在这里讨论了如何在Yosemite中检测暗模式以更改Objective-C的状态栏菜单图标,但不确定是否可以在Java中进行类似操作。
I can't find any information on how to set the menubar icon for dark mode on OS X (introduced in OS X 10.10 Yosemite) in Java. I've seen this post http://mail.openjdk.java.net/pipermail/macosx-port-dev/2014-October/006740.html but without any answer. I know this has been discussed here How to detect dark mode in Yosemite to change the status bar menu icon for Objective-C, but not sure if this can be done similarly in Java.
有没有办法实现这个目标?
Is there some way to achieve this?
推荐答案
我遇到了同样的问题,通过调用<解决它code>默认读取命令并分析退出代码:
I’ve had the same issue, solving it by invoking the defaults read
command and analyzing the exit code:
/**
* @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 :
Now you can load different images and use them in a 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"));
}
这篇关于Java中OS X上暗模式的MenuBar图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!