From d63766c99f670db43a788d4b95cf1fde2fd3477d Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 7 Aug 2023 19:55:48 +0200 Subject: [PATCH] Better window theme handling using windows registry --- docs/window.dox | 6 +++--- include/GLFW/glfw3.h | 8 ++++++-- src/internal.h | 2 +- src/win32_window.c | 30 +++++++++++++++++++++++++++++- src/window.c | 4 ++-- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/docs/window.dox b/docs/window.dox index ff1cd225..405cca5a 100644 --- a/docs/window.dox +++ b/docs/window.dox @@ -249,9 +249,9 @@ This hint only has an effect on platforms where screen coordinates and pixels always map 1:1 such as Windows and X11. On platforms like macOS the resolution of the framebuffer is changed independently of the window size. -@anchor GLFW_LIGHT_THEME -__GLFW_LIGHT_THEME__ specifies whether the window should use light theme or not. -Possible values are `GLFW_TRUE` and `GLFW_FALSE`. This hint only has effect +@anchor GLFW_THEME +__GLFW_THEME__ specifies whether the window should use light theme or not. +Possible values are `GLFW_THEME_AUTO`, `GLFW_THEME_DARK`, and `GLFW_THEME_LIGHT`. This hint only has effect on windows operating system. @anchor GLFW_MOUSE_PASSTHROUGH_hint diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 39bc17f4..b1e5cf2b 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -1099,10 +1099,10 @@ extern "C" { /*! @brief macOS specific * [window hint](@ref GLFW_COCOA_RETINA_FRAMEBUFFER_hint). */ -#define GLFW_LIGHT_THEME 0x0002200D +#define GLFW_THEME 0x0002200D /*! @brief windows specific * - * Allows specifying whether light theme should be used. + * Allows specifying whether light or dark theme should be used. */ #define GLFW_COCOA_RETINA_FRAMEBUFFER 0x00023001 /*! @brief macOS specific @@ -1174,6 +1174,10 @@ extern "C" { #define GLFW_ANY_POSITION 0x80000000 +#define GLFW_THEME_AUTO 0x00000000 +#define GLFW_THEME_DARK 0x00000001 +#define GLFW_THEME_LIGHT 0x00000002 + /*! @defgroup shapes Standard cursor shapes * @brief Standard system cursor shapes. * diff --git a/src/internal.h b/src/internal.h index 98c889a3..9dace49c 100644 --- a/src/internal.h +++ b/src/internal.h @@ -408,7 +408,7 @@ struct _GLFWwndconfig GLFWbool focusOnShow; GLFWbool mousePassthrough; GLFWbool scaleToMonitor; - GLFWbool lightTheme; + int theme; struct { GLFWbool retina; char frameName[256]; diff --git a/src/win32_window.c b/src/win32_window.c index f6a8b0b7..e029fc7a 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -1344,7 +1344,35 @@ static int createNativeWindow(_GLFWwindow* window, _glfw.win32.instance, (LPVOID) wndconfig); - _glfwSetWindowTheme(!(wndconfig->lightTheme), window->win32.handle); + BOOL should_use_light_mode; + if (wndconfig->theme == GLFW_THEME_AUTO) + { + BOOL success = FALSE; + HKEY hRootKey = HKEY_CURRENT_USER; + const wchar_t* lpSubKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; + const wchar_t* lpValueName = L"AppsUseLightTheme"; + DWORD result; + { + HKEY hKey = 0; + if (RegOpenKeyExW(hRootKey, lpSubKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS) + { + DWORD dwBufferSize = sizeof(DWORD); + DWORD dwData = 0; + if (RegQueryValueExW(hKey, lpValueName, 0, NULL, (LPBYTE)(&dwData), &dwBufferSize) == ERROR_SUCCESS) + { + result = dwData; + success = TRUE; + } + RegCloseKey(hKey); + } + } + should_use_light_mode = success && result != 0; + } + else + should_use_light_mode = wndconfig->theme == GLFW_THEME_LIGHT; + + _glfwSetWindowTheme(!should_use_light_mode, window->win32.handle); + _glfw_free(wideTitle); if (!window->win32.handle) diff --git a/src/window.c b/src/window.c index c601bee0..5f212a98 100644 --- a/src/window.c +++ b/src/window.c @@ -388,8 +388,8 @@ GLFWAPI void glfwWindowHint(int hint, int value) case GLFW_SCALE_TO_MONITOR: _glfw.hints.window.scaleToMonitor = value ? GLFW_TRUE : GLFW_FALSE; return; - case GLFW_LIGHT_THEME: - _glfw.hints.window.lightTheme = value ? GLFW_TRUE : GLFW_FALSE; + case GLFW_THEME: + _glfw.hints.window.theme = value; return; case GLFW_CENTER_CURSOR: _glfw.hints.window.centerCursor = value ? GLFW_TRUE : GLFW_FALSE;