Skip to content

Commit

Permalink
win32: follow Windows settings and update dark mode state
Browse files Browse the repository at this point in the history
Microsoft documented how to enable dark mode for title bar:

https://learn.microsoft.com/windows/apps/desktop/modernize/apply-windows-themes
https://learn.microsoft.com/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute

Documentation says to set the DWMWA_USE_IMMERSIVE_DARK_MODE attribute to
TRUE to honor dark mode for the window, FALSE to always use light mode.
While in fact setting it to TRUE causes dark mode to be always enabled,
regardless of the settings. Since it is quite unlikely that it will be
fixed, just use UxTheme API to check if dark mode should be applied and
while at it enable it fully. Ideally this function should only call the
DwmSetWindowAttribute(), but it just doesn't work as documented.

Fixes: mpv-player#6901
  • Loading branch information
kasper93 committed Mar 19, 2023
1 parent a45995f commit 945accf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ if features['win32-desktop']
cc.find_library('gdi32'),
cc.find_library('ole32'),
cc.find_library('uuid'),
cc.find_library('uxtheme'),
cc.find_library('version'),
cc.find_library('winmm')]
dependencies += win32_desktop_libs
Expand Down
46 changes: 46 additions & 0 deletions video/out/w32_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,47 @@ static void reinit_window_state(struct vo_w32_state *w32)
update_window_state(w32);
}

// Follow Windows settings and update dark mode state
// Microsoft documented how to enable dark mode for title bar:
// https://learn.microsoft.com/windows/apps/desktop/modernize/apply-windows-themes
// https://learn.microsoft.com/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
// Documentation says to set the DWMWA_USE_IMMERSIVE_DARK_MODE attribute to
// TRUE to honor dark mode for the window, FALSE to always use light mode. While
// in fact setting it to TRUE causes dark mode to be always enabled, regardless
// of the settings. Since it is quite unlikely that it will be fixed, just use
// UxTheme API to check if dark mode should be applied and while at it enable it
// fully. Ideally this function should only call the DwmSetWindowAttribute(),
// but it just doesn't work as documented.
static void update_dark_mode(HWND hWnd)
{
HANDLE uxtheme = GetModuleHandle(L"uxtheme.dll");
BOOLEAN (WINAPI *pShouldAppsUseDarkMode)(void);
DWORD (WINAPI *pSetPreferredAppMode)(DWORD mode);

pShouldAppsUseDarkMode = (void *) GetProcAddress(uxtheme, MAKEINTRESOURCEA(132));
pSetPreferredAppMode = (void *) GetProcAddress(uxtheme, MAKEINTRESOURCEA(135));

if (pSetPreferredAppMode)
pSetPreferredAppMode(1); // allow dark mode

HIGHCONTRAST hc = {sizeof(hc)};
SystemParametersInfo(SPI_GETHIGHCONTRAST, sizeof(hc), &hc, 0);
bool high_contrast = hc.dwFlags & HCF_HIGHCONTRASTON;

// if pShouldAppsUseDarkMode is not available, just assume it to be true
const BOOL use_dark_mode = !high_contrast && (!pShouldAppsUseDarkMode ||
pShouldAppsUseDarkMode());

SetWindowTheme(hWnd, use_dark_mode ? L"DarkMode_Explorer" : L"", NULL);

#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif

DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE,
&use_dark_mode, sizeof(use_dark_mode));
}

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
Expand Down Expand Up @@ -1293,6 +1334,9 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
case WM_DISPLAYCHANGE:
force_update_display_info(w32);
break;
case WM_SETTINGCHANGE:
update_dark_mode(w32->window);
break;
}

if (message == w32->tbtnCreatedMsg) {
Expand Down Expand Up @@ -1543,6 +1587,8 @@ static void *gui_thread(void *ptr)
goto done;
}

update_dark_mode(w32->window);

if (SUCCEEDED(OleInitialize(NULL))) {
ole_ok = true;

Expand Down

0 comments on commit 945accf

Please sign in to comment.