From e9be5b9f67952be2f5505ce8feaf760db6dd1eb3 Mon Sep 17 00:00:00 2001 From: Jannis Weis <31143295+weisJ@users.noreply.github.com> Date: Sun, 26 Dec 2021 23:38:21 +0100 Subject: [PATCH] Windows: Detect Windows 11 in native code. --- .../darklaf/nativeutil/AbstractLibrary.java | 3 ++ windows/src/main/cpp/Decorations.cpp | 15 +++++++ windows/src/main/cpp/ThemeInfo.cpp | 39 +------------------ .../windows/JNIDecorationsWindows.java | 2 + .../platform/windows/WindowsLibrary.java | 5 +++ 5 files changed, 27 insertions(+), 37 deletions(-) diff --git a/native-utils/src/main/java/com/github/weisj/darklaf/nativeutil/AbstractLibrary.java b/native-utils/src/main/java/com/github/weisj/darklaf/nativeutil/AbstractLibrary.java index a7ee5fde..0009441a 100644 --- a/native-utils/src/main/java/com/github/weisj/darklaf/nativeutil/AbstractLibrary.java +++ b/native-utils/src/main/java/com/github/weisj/darklaf/nativeutil/AbstractLibrary.java @@ -46,9 +46,12 @@ public abstract class AbstractLibrary { public void updateLibrary() { if (!isLoaded() && !attemptedLoad) { loadLibrary(); + afterLoad(); } } + protected void afterLoad() {} + private void loadLibrary() { attemptedLoad = true; if (!canLoad() || isLoaded()) { diff --git a/windows/src/main/cpp/Decorations.cpp b/windows/src/main/cpp/Decorations.cpp index 6bb06b90..5cf093dc 100644 --- a/windows/src/main/cpp/Decorations.cpp +++ b/windows/src/main/cpp/Decorations.cpp @@ -23,6 +23,7 @@ * */ #include "Decorations.h" +#include "Registry.h" #include "com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows.h" #ifndef WM_NCUAHDRAWCAPTION @@ -32,6 +33,8 @@ #define WM_NCUAHDRAWFRAME (0x00AF) #endif +static bool is_windows_11 = false; + std::map wrapper_map = std::map(); static bool Maximized(HWND hwnd) { @@ -462,3 +465,15 @@ Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_restore(JNI HWND handle = reinterpret_cast(hwnd); ShowWindow(handle, SW_RESTORE); } + +JNIEXPORT void JNICALL +Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_init(JNIEnv*, jclass) { + try { + auto buildVersion = RegGetString( + HKEY_LOCAL_MACHINE, + "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", + "CurrentBuild"); + auto buildInt = std::stoi(buildVersion); + is_windows_11 = buildInt >= 22000; + } catch (LONG) {} +} diff --git a/windows/src/main/cpp/ThemeInfo.cpp b/windows/src/main/cpp/ThemeInfo.cpp index dd4681f4..971669d9 100644 --- a/windows/src/main/cpp/ThemeInfo.cpp +++ b/windows/src/main/cpp/ThemeInfo.cpp @@ -23,12 +23,12 @@ * */ #include "com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows.h" +#include "Registry.h" #include #include #include #include -#include #include constexpr auto DARK_MODE_PATH = "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; @@ -49,41 +49,6 @@ constexpr auto HIGH_CONTRAST_DEFAULT_VALUE = false; constexpr auto FONT_SCALE_DEFAULT_VALUE = 100; constexpr auto ACCENT_COLOR_DEFAULT_VALUE = 0; -static void ModifyFlags(DWORD &flags) { -#ifdef _WIN64 - flags |= RRF_SUBKEY_WOW6464KEY; -#else - flags |= RRF_SUBKEY_WOW6432KEY; -#endif -} - -static DWORD RegGetDword(HKEY hKey, const LPCSTR subKey, const LPCSTR value) { - DWORD data {}; - DWORD dataSize = sizeof(data); - DWORD flags = RRF_RT_REG_DWORD; - ModifyFlags(flags); - LONG retCode = ::RegGetValue(hKey, subKey, value, flags, nullptr, &data, &dataSize); - if (retCode != ERROR_SUCCESS) throw retCode; - - return data; -} - -static std::string RegGetString(HKEY hKey, const LPCSTR subKey, const LPCSTR value) { - DWORD dataSize {}; - DWORD flags = RRF_RT_REG_SZ; - ModifyFlags(flags); - LONG retCode = ::RegGetValue(hKey, subKey, value, flags, nullptr, nullptr, &dataSize); - if (retCode != ERROR_SUCCESS) throw retCode; - - std::string data; - DWORD stringLengthInChars = dataSize / sizeof(char); - data.resize(stringLengthInChars); - retCode = ::RegGetValue(hKey, subKey, value, flags, nullptr, &data[0], &dataSize); - if (retCode != ERROR_SUCCESS) throw retCode; - - return data; -} - static bool IsHighContrastMode() { HIGHCONTRAST info = { 0, 0, 0 }; info.cbSize = sizeof(HIGHCONTRAST); @@ -119,7 +84,7 @@ static bool RegisterRegistryEvent(const LPCSTR subKey, HANDLE event) { HKEY hKey; REGSAM flags = KEY_NOTIFY; ModifyFlags(flags); - DWORD res = RegOpenKeyEx(HKEY_CURRENT_USER, subKey, 0, flags, &hKey); + DWORD res = RegOpenKeyExA(HKEY_CURRENT_USER, subKey, 0, flags, &hKey); if (res == ERROR_SUCCESS) { LSTATUS status = RegNotifyChangeKeyValue(hKey, FALSE, REG_NOTIFY_CHANGE_LAST_SET, event, TRUE); return status == ERROR_SUCCESS; diff --git a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/JNIDecorationsWindows.java b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/JNIDecorationsWindows.java index e6593468..af99df46 100644 --- a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/JNIDecorationsWindows.java +++ b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/JNIDecorationsWindows.java @@ -49,4 +49,6 @@ public final class JNIDecorationsWindows { public static native void uninstallDecorations(final long hwnd, final boolean decorated); public static native boolean installPopupMenuDecorations(final long hwnd); + + public static native void init(); } diff --git a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsLibrary.java b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsLibrary.java index 7929c047..b44c251e 100644 --- a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsLibrary.java +++ b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsLibrary.java @@ -40,6 +40,11 @@ public class WindowsLibrary extends AbstractLibrary { super("darklaf-windows", LogUtil.getLogger(WindowsLibrary.class)); } + @Override + protected void afterLoad() { + JNIDecorationsWindows.init(); + } + @Override final protected Class getLoaderClass() { return WindowsLibrary.class;