From df03d31e43afd0621d59c74a2a2c1491a9dc98b1 Mon Sep 17 00:00:00 2001 From: Jannis Weis <31143295+weisJ@users.noreply.github.com> Date: Mon, 27 Dec 2021 22:10:04 +0100 Subject: [PATCH] Windows: Set correct value for isWindows11 in SystemInfo through native code Currently, the JDK doesn't correctly report the current operating system. For now, we query the value ourselves using native code. This will be updated once the JDK backport is available: https://bugs.openjdk.java.net/browse/JDK-8274840 --- .../darklaf/task/ThemeDefaultsInitTask.java | 2 +- .../github/weisj/darklaf/util/SystemInfo.java | 18 +++++++++-- windows/src/main/cpp/Decorations.cpp | 30 ++++++++++++++----- .../windows/JNIDecorationsWindows.java | 2 ++ .../windows/WindowsDecorationsProvider.java | 2 +- .../platform/windows/WindowsLibrary.java | 1 + 6 files changed, 43 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/task/ThemeDefaultsInitTask.java b/core/src/main/java/com/github/weisj/darklaf/task/ThemeDefaultsInitTask.java index f846803e..d5c8f87c 100644 --- a/core/src/main/java/com/github/weisj/darklaf/task/ThemeDefaultsInitTask.java +++ b/core/src/main/java/com/github/weisj/darklaf/task/ThemeDefaultsInitTask.java @@ -160,7 +160,7 @@ public class ThemeDefaultsInitTask implements DefaultsInitTask { PropertyLoader.loadProperties(DarkLaf.class, osName, "platform/"), uiProps, defaults, iconResolver); osPlatformLoader.accept(getOsName()); - if (SystemInfo.isWindows11OrGreater) { + if (SystemInfo.isWindows11()) { osPlatformLoader.accept("windows11"); } currentTheme.customizePlatformProperties(uiProps, defaults, iconResolver); diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/SystemInfo.java b/utils/src/main/java/com/github/weisj/darklaf/util/SystemInfo.java index 892a033e..1819c751 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/SystemInfo.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/SystemInfo.java @@ -69,7 +69,7 @@ public final class SystemInfo { public static final boolean isMacOSMojave; public static final boolean isMacOSCatalina; public static final boolean isMacOSYosemite; - public static final boolean isWindows11OrGreater; + private static boolean isWindows11; public static final boolean isWindows10OrGreater; public static final boolean isWindows7; public static final boolean isWindowsVista; @@ -97,7 +97,7 @@ public final class SystemInfo { isMacOSYosemite = isMacOSCatalina || (isMac && isOsVersionAtLeast("10.10")); isWindows10OrGreater = isWindows && isOsVersionAtLeast("10.0"); - isWindows11OrGreater = isWindows && _OS_NAME.contains("windows 11"); + isWindows11 = isWindows && _OS_NAME.contains("windows 11"); isWindows7 = isWindows10OrGreater || (isWindows && isOsVersionAtLeast("6.1")); isWindowsVista = isWindows7 || (isWindows && isOsVersionAtLeast("6.0")); @@ -148,6 +148,20 @@ public final class SystemInfo { } } + public static boolean isWindows11() { + return isWindows11; + } + + /** + * This function is temporary for the time being as the JDK doesn't report whether we are running on + * Windows 11. Don't call this method if you aren't sure that you are passing the correct value. + * + * @param isWindows11 Whether the OS is Windows 11 + */ + public static void setWindows11State(final boolean isWindows11) { + SystemInfo.isWindows11 = isWindows11; + } + public static boolean isJavaVersionAtLeast(final String v) { return compareVersionNumbers(JAVA_RUNTIME_VERSION, v) >= 0; } diff --git a/windows/src/main/cpp/Decorations.cpp b/windows/src/main/cpp/Decorations.cpp index 620eae97..799b48cd 100644 --- a/windows/src/main/cpp/Decorations.cpp +++ b/windows/src/main/cpp/Decorations.cpp @@ -558,14 +558,28 @@ Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_restore(JNI ShowWindow(handle, SW_RESTORE); } +static bool checkIsWindows11() { + static bool local_is_windows_11 = []() { + try { + auto buildVersion = RegGetString(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", + "CurrentBuild"); + auto buildInt = std::stoi(buildVersion); + return buildInt >= 22000; + } catch (LONG) { + return false; + } + }(); + return local_is_windows_11; +} + 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) {} + // We setup this global value here once to avoid the lock associated with querying the local static + // in checkIsWindows11(). + is_windows_11 = checkIsWindows11(); +} + +JNIEXPORT jboolean JNICALL +Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_isWindows11(JNIEnv*, jclass) { + return checkIsWindows11(); } 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 a89b5f9c..9690aa8b 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 @@ -52,4 +52,6 @@ public final class JNIDecorationsWindows { public static native boolean installPopupMenuDecorations(final long hwnd); public static native void init(); + + public static native boolean isWindows11(); } diff --git a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsDecorationsProvider.java b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsDecorationsProvider.java index 3eb588e9..fd8d641c 100644 --- a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsDecorationsProvider.java +++ b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsDecorationsProvider.java @@ -107,7 +107,7 @@ public class WindowsDecorationsProvider implements DecorationsProvider { properties, currentDefaults, iconLoader); loadProps.accept("windows_icons"); loadProps.accept("windows_decorations"); - if (SystemInfo.isWindows11OrGreater) { + if (SystemInfo.isWindows11()) { loadProps.accept("windows_11_decorations"); } } 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 b44c251e..2ef2d6d6 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 @@ -43,6 +43,7 @@ public class WindowsLibrary extends AbstractLibrary { @Override protected void afterLoad() { JNIDecorationsWindows.init(); + SystemInfo.setWindows11State(JNIDecorationsWindows.isWindows11()); } @Override