FlatLaf
FlatLaf copied to clipboard
Automatic detection of dark mode on Windows 10 >= 20H1 and 11
Hi,
I just stumbled upon this and thought it might be a nice addition to this already fantastic LaF:
https://github.com/dsanders11/electron/commit/f3911f117d5a400b8b8bf1d7e588dfe9679d444b
Thanks for this wonderful project and your passion for details!
You can use the following code to easily detect dark mode on windows:
private static final String REGQUERY_UTIL = "reg query ";
private static final String REGDWORD_TOKEN = "REG_DWORD";
private static final String DARK_THEME_CMD = REGQUERY_UTIL + "\"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize\"" + " /v AppsUseLightTheme";
private static boolean isWindowsDarkMode() {
try {
Process process = Runtime.getRuntime().exec(DARK_THEME_CMD);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String result = reader.getResult();
int p = result.indexOf(REGDWORD_TOKEN);
if (p == -1) { return false; }
// 1 == Light Mode, 0 == Dark Mode
String temp = result.substring(p + REGDWORD_TOKEN.length()).trim();
return ((Integer.parseInt(temp.substring("0x".length()), 16))) == 0;
}
catch (Exception e) {
return false;
}
}