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 24, 2023
1 parent a45995f commit d2c2c82
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
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 @@ -54,6 +54,10 @@ EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define WM_DPICHANGED (0x02E0)
#endif

#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif

#ifndef DPI_ENUMS_DECLARED
typedef enum MONITOR_DPI_TYPE {
MDT_EFFECTIVE_DPI = 0,
Expand Down Expand Up @@ -1000,6 +1004,43 @@ 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)
{
HMODULE 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);

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
9 changes: 8 additions & 1 deletion wscript
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,14 @@ main_dependencies = [
'name': 'win32-desktop',
'desc': 'win32 desktop APIs',
'deps': '(os-win32 || os-cygwin) && !uwp',
'func': check_cc(lib=['winmm', 'gdi32', 'ole32', 'uuid', 'avrt', 'dwmapi', 'version']),
'func': check_cc(lib=['avrt',
'dwmapi',
'gdi32',
'ole32',
'uuid',
'uxtheme',
'version',
'winmm']),
}, {
'name': '--win32-internal-pthreads',
'desc': 'internal pthread wrapper for win32 (Vista+)',
Expand Down

0 comments on commit d2c2c82

Please sign in to comment.