Added SDL_GetSystemTheme() to return whether the system is using a dark or light color theme, and SDL_EVENT_SYSTEM_THEME_CHANGED is sent when this changes

Fixes https://github.com/libsdl-org/SDL/issues/5334
Fixes https://github.com/libsdl-org/SDL/issues/6958
Closes https://github.com/libsdl-org/SDL/pull/6440
This commit is contained in:
Sam Lantinga
2023-03-07 00:01:34 -08:00
parent fb0c3197e0
commit 8994878767
27 changed files with 243 additions and 21 deletions

View File

@@ -40,6 +40,12 @@
#include <SDL3/SDL_syswm.h>
/* Dark mode support */
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif
typedef HRESULT (WINAPI *DwmSetWindowAttribute_t)(HWND hwnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute);
/* Windows CE compatibility */
#ifndef SWP_NOCOPYBITS
#define SWP_NOCOPYBITS 0
@@ -511,6 +517,8 @@ int WIN_CreateWindow(_THIS, SDL_Window *window)
return WIN_SetError("Couldn't create window");
}
WIN_UpdateDarkModeForHWND(hwnd);
WIN_PumpEvents(_this);
if (SetupWindowData(_this, window, hwnd, parent, SDL_TRUE) < 0) {
@@ -1459,4 +1467,18 @@ int WIN_FlashWindow(_THIS, SDL_Window *window, SDL_FlashOperation operation)
}
#endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
void WIN_UpdateDarkModeForHWND(HWND hwnd)
{
void *handle = SDL_LoadObject("dwmapi.dll");
if (handle) {
DwmSetWindowAttribute_t DwmSetWindowAttributeFunc = (DwmSetWindowAttribute_t)SDL_LoadFunction(handle, "DwmSetWindowAttribute");
if (DwmSetWindowAttributeFunc) {
/* FIXME: Do we need to traverse children? */
BOOL value = (SDL_GetSystemTheme() == SDL_SYSTEM_THEME_DARK) ? TRUE : FALSE;
DwmSetWindowAttributeFunc(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value));
}
SDL_UnloadObject(handle);
}
}
#endif /* SDL_VIDEO_DRIVER_WINDOWS */