Browse Source

Windows: Detect Windows 11 in native code.

spotless
Jannis Weis 3 years ago
parent
commit
e9be5b9f67
No known key found for this signature in database
GPG Key ID: 7C9D8D4B558049AB
  1. 3
      native-utils/src/main/java/com/github/weisj/darklaf/nativeutil/AbstractLibrary.java
  2. 15
      windows/src/main/cpp/Decorations.cpp
  3. 39
      windows/src/main/cpp/ThemeInfo.cpp
  4. 2
      windows/src/main/java/com/github/weisj/darklaf/platform/windows/JNIDecorationsWindows.java
  5. 5
      windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsLibrary.java

3
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()) {

15
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<HWND, WindowWrapper*> wrapper_map = std::map<HWND, WindowWrapper*>();
static bool Maximized(HWND hwnd) {
@ -462,3 +465,15 @@ Java_com_github_weisj_darklaf_platform_windows_JNIDecorationsWindows_restore(JNI
HWND handle = reinterpret_cast<HWND>(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) {}
}

39
windows/src/main/cpp/ThemeInfo.cpp

@ -23,12 +23,12 @@
*
*/
#include "com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows.h"
#include "Registry.h"
#include <string>
#include <thread>
#include <atomic>
#include <windows.h>
#include <winreg.h>
#include <winuser.h>
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;

2
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();
}

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

Loading…
Cancel
Save