FlatLaf icon indicating copy to clipboard operation
FlatLaf copied to clipboard

Automatic detection of dark mode on Windows 10 >= 20H1 and 11

Open patric42 opened this issue 3 years ago • 1 comments

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!

patric42 avatar Aug 04 '22 09:08 patric42

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;
        }
    }

derreisende77 avatar Dec 21 '22 19:12 derreisende77