From e95ff11e1db8fb70e122ae922de0c36dbe4de0c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sat, 25 Jan 2025 20:24:53 -0300 Subject: [PATCH] Fix for SpartanJ/ecode#385. Window will now use the currently user configured theme by default. Dark window titlte color will be respected. --- src/eepp/window/backend/SDL2/windowsdl2.cpp | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/eepp/window/backend/SDL2/windowsdl2.cpp b/src/eepp/window/backend/SDL2/windowsdl2.cpp index 3370007c3..1fee506b4 100644 --- a/src/eepp/window/backend/SDL2/windowsdl2.cpp +++ b/src/eepp/window/backend/SDL2/windowsdl2.cpp @@ -148,6 +148,44 @@ int hideOSK() { WIN_OSK_VISIBLE = false; return PostMessage( GetDesktopWindow(), WM_SYSCOMMAND, (int)SC_CLOSE, 0 ); } + +bool isDarkModeEnabled() { + HKEY hKey; + DWORD value = 1; // Default to light theme + DWORD valueSize = sizeof(value); + + if (RegOpenKeyEx(HKEY_CURRENT_USER, + "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", + 0, KEY_READ, &hKey) == ERROR_SUCCESS) { + RegQueryValueEx(hKey, "AppsUseLightTheme", nullptr, nullptr, reinterpret_cast(&value), &valueSize); + RegCloseKey(hKey); + } + + return value == 0; // 0 means dark theme is enabled +} + +typedef HRESULT(WINAPI *DwmSetWindowAttributeFunc)(HWND, DWORD, LPCVOID, DWORD); + +constexpr DWORD DWMWA_USE_IMMERSIVE_DARK_MODE = 20; + +void setUserTheme(HWND hwnd) { + HMODULE hDwmapi = LoadLibrary("dwmapi.dll"); + if (!hDwmapi) { + return; + } + + auto DwmSetWindowAttribute = reinterpret_cast( + GetProcAddress(hDwmapi, "DwmSetWindowAttribute")); + if (!DwmSetWindowAttribute) { + FreeLibrary(hDwmapi); + return; + } + + BOOL darkMode = isDarkModeEnabled() ? TRUE : FALSE; + DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &darkMode, sizeof(darkMode)); + + FreeLibrary(hDwmapi); +} #elif defined( EE_X11_PLATFORM ) #include #include @@ -415,6 +453,10 @@ bool WindowSDL::create( WindowSettings Settings, ContextSettings Context ) { mCursorManager->set( Cursor::SysArrow ); +#if EE_PLATFORM == EE_PLATFORM_WIN + setUserTheme( (HWND)getWindowHandler() ); +#endif + logSuccessfulInit( getVersion() ); return true;