Better window theme handling using windows registry

This commit is contained in:
Santiago 2023-08-07 19:55:48 +02:00
parent 6ee3153f9c
commit d63766c99f
5 changed files with 41 additions and 9 deletions

View File

@ -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

View File

@ -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.
*

View File

@ -408,7 +408,7 @@ struct _GLFWwndconfig
GLFWbool focusOnShow;
GLFWbool mousePassthrough;
GLFWbool scaleToMonitor;
GLFWbool lightTheme;
int theme;
struct {
GLFWbool retina;
char frameName[256];

View File

@ -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)

View File

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