Browse Source

Replace magic value with named constant.

Replace macros with constexpr.
Replace c style casts with static_cast.
pull/198/head
weisj 4 years ago
parent
commit
d6fd046276
  1. 2
      macos/src/main/objcpp/ThemeInfo.mm
  2. 69
      windows/src/main/cpp/ThemeInfo.cpp

2
macos/src/main/objcpp/ThemeInfo.mm

@ -191,7 +191,7 @@ JNIEXPORT jlong JNICALL
Java_com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS_createPreferenceChangeListener(JNIEnv *env, jclass obj, jobject callback) { Java_com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS_createPreferenceChangeListener(JNIEnv *env, jclass obj, jobject callback) {
JNF_COCOA_DURING(env); // We dont want an auto release pool. JNF_COCOA_DURING(env); // We dont want an auto release pool.
JavaVM *jvm; JavaVM *jvm;
if (env->GetJavaVM(&jvm) == 0) { if (env->GetJavaVM(&jvm) == JNI_OK) {
jobject callbackRef = env->NewGlobalRef(callback); jobject callbackRef = env->NewGlobalRef(callback);
PreferenceChangeListener *listener = [[PreferenceChangeListener alloc] initWithJVM:jvm andCallBack: callbackRef]; PreferenceChangeListener *listener = [[PreferenceChangeListener alloc] initWithJVM:jvm andCallBack: callbackRef];
[listener retain]; [listener retain];

69
windows/src/main/cpp/ThemeInfo.cpp

@ -32,23 +32,23 @@
#include <winreg.h> #include <winreg.h>
#include <winuser.h> #include <winuser.h>
#define DARK_MODE_PATH ("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize") constexpr auto DARK_MODE_PATH = "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
#define DARK_MODE_KEY ("AppsUseLightTheme") constexpr auto DARK_MODE_KEY = "AppsUseLightTheme";
#define FONT_SCALE_PATH ("Software\\Microsoft\\Accessibility") constexpr auto FONT_SCALE_PATH = "Software\\Microsoft\\Accessibility";
#define FONT_SCALE_KEY ("TextScaleFactor") constexpr auto FONT_SCALE_KEY = "TextScaleFactor";
#define HIGH_CONTRAST_PATH ("Control Panel\\Accessibility\\HighContrast") constexpr auto HIGH_CONTRAST_PATH = "Control Panel\\Accessibility\\HighContrast";
#define HIGH_CONTRAST_THEME_KEY ("High Contrast Scheme") constexpr auto HIGH_CONTRAST_THEME_KEY = "High Contrast Scheme";
#define HIGH_CONTRAST_LIGHT_THEME ("High Contrast White") constexpr auto HIGH_CONTRAST_LIGHT_THEME = "High Contrast White";
#define ACCENT_COLOR_PATH "SOFTWARE\\Microsoft\\Windows\\DWM" constexpr auto ACCENT_COLOR_PATH = "SOFTWARE\\Microsoft\\Windows\\DWM";
#define ACCENT_COLOR_KEY "ColorizationColor" constexpr auto ACCENT_COLOR_KEY = "ColorizationColor";
#define DARK_MODE_DEFAULT_VALUE false constexpr auto DARK_MODE_DEFAULT_VALUE = false;
#define HIGH_CONTRAST_DEFAULT_VALUE false constexpr auto HIGH_CONTRAST_DEFAULT_VALUE = false;
#define FONT_SCALE_DEFAULT_VALUE 100 constexpr auto FONT_SCALE_DEFAULT_VALUE = 100;
#define ACCENT_COLOR_DEFAULT_VALUE 0 constexpr auto ACCENT_COLOR_DEFAULT_VALUE = 0;
void ModifyFlags(DWORD &flags) { void ModifyFlags(DWORD &flags) {
#ifdef _WIN64 #ifdef _WIN64
@ -97,13 +97,11 @@ bool IsHighContrastMode() {
bool IsDarkMode() { bool IsDarkMode() {
try { try {
bool appsUseDark = (0 == RegGetDword(HKEY_CURRENT_USER, DARK_MODE_PATH, bool appsUseDark = (0 == RegGetDword(HKEY_CURRENT_USER, DARK_MODE_PATH, DARK_MODE_KEY));
DARK_MODE_KEY));
bool isHighContrast = IsHighContrastMode(); bool isHighContrast = IsHighContrastMode();
if (!isHighContrast) return appsUseDark; if (!isHighContrast) return appsUseDark;
std::string themeValue = RegGetString(HKEY_CURRENT_USER, std::string themeValue = RegGetString(HKEY_CURRENT_USER, HIGH_CONTRAST_PATH, HIGH_CONTRAST_THEME_KEY);
HIGH_CONTRAST_PATH, HIGH_CONTRAST_THEME_KEY);
return (strcmp(themeValue.c_str(), HIGH_CONTRAST_LIGHT_THEME) != 0); return (strcmp(themeValue.c_str(), HIGH_CONTRAST_LIGHT_THEME) != 0);
} catch (LONG e) { } catch (LONG e) {
return DARK_MODE_DEFAULT_VALUE; return DARK_MODE_DEFAULT_VALUE;
@ -133,35 +131,30 @@ bool RegisterRegistryEvent(const LPCSTR subKey, HANDLE event) {
int GetAccentColor() { int GetAccentColor() {
try { try {
return RegGetDword(HKEY_CURRENT_USER, ACCENT_COLOR_PATH, return RegGetDword(HKEY_CURRENT_USER, ACCENT_COLOR_PATH, ACCENT_COLOR_KEY);
ACCENT_COLOR_KEY);
} catch (LONG e) { } catch (LONG e) {
return ACCENT_COLOR_DEFAULT_VALUE; return ACCENT_COLOR_DEFAULT_VALUE;
} }
} }
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_isDarkThemeEnabled(JNIEnv *env, jclass obj) Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_isDarkThemeEnabled(JNIEnv *env, jclass obj) {
{ return static_cast<jboolean>(IsDarkMode());
return (jboolean) IsDarkMode();
} }
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_isHighContrastEnabled(JNIEnv *env, jclass obj) Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_isHighContrastEnabled(JNIEnv *env, jclass obj) {
{ return static_cast<jboolean>(IsHighContrastMode());
return (jboolean) IsHighContrastMode();
} }
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_getFontScaleFactor(JNIEnv *env, jclass obj) Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_getFontScaleFactor(JNIEnv *env, jclass obj) {
{ return static_cast<jlong>(GetTextScaleFactor());
return (jlong) GetTextScaleFactor();
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_getAccentColor(JNIEnv *env, jclass obj) Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_getAccentColor(JNIEnv *env, jclass obj) {
{ return static_cast<jint>(GetAccentColor());
return (jint) GetAccentColor();
} }
struct EventHandler { struct EventHandler {
@ -223,25 +216,21 @@ struct EventHandler {
}; };
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_createEventHandler(JNIEnv *env, jclass obj, jobject callback) Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_createEventHandler(JNIEnv *env, jclass obj, jobject callback) {
{
JavaVM *jvm; JavaVM *jvm;
if (env->GetJavaVM(&jvm) == 0) if (env->GetJavaVM(&jvm) == JNI_OK) {
{
jobject callbackRef = env->NewGlobalRef(callback); jobject callbackRef = env->NewGlobalRef(callback);
HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL); HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
EventHandler* eventHandler = new EventHandler(jvm, callbackRef, event); EventHandler* eventHandler = new EventHandler(jvm, callbackRef, event);
return reinterpret_cast<jlong>(eventHandler); return reinterpret_cast<jlong>(eventHandler);
} }
return (jlong) 0; return static_cast<jlong>(0);
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_deleteEventHandler(JNIEnv *env, jclass obj, jlong eventHandler) Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_deleteEventHandler(JNIEnv *env, jclass obj, jlong eventHandler) {
{
EventHandler *handler = reinterpret_cast<EventHandler *>(eventHandler); EventHandler *handler = reinterpret_cast<EventHandler *>(eventHandler);
if (handler) if (handler) {
{
env->DeleteGlobalRef(handler->callback); env->DeleteGlobalRef(handler->callback);
handler->stop(); handler->stop();
delete handler; delete handler;

Loading…
Cancel
Save