Skip to content

Commit

Permalink
Fix for SpartanJ/ecode#385. Window will now use the currently user co…
Browse files Browse the repository at this point in the history
…nfigured theme by default. Dark window titlte color will be respected.
  • Loading branch information
SpartanJ committed Jan 25, 2025
1 parent 54d2308 commit e95ff11
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/eepp/window/backend/SDL2/windowsdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LPBYTE>(&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<DwmSetWindowAttributeFunc>(
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 <signal.h>
#include <unistd.h>
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e95ff11

Please sign in to comment.