Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Win32 darkmode and fix title bar's context menu #11543

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

Pixelsuft
Copy link

Description

Use hidden uxtheme.dll and user32.dll functions for darkmode instead of using DwmSetWindowAttribute.
Fixes context menu on title bar.
image

Notes

I couldn't find function to get Windows build number so I import RtlGetVersion from ntdll to get it.

Existing Issue(s)

#11535

@Pixelsuft Pixelsuft changed the title Improve Win32 darkmode and fix #11535 Improve Win32 darkmode and fix title bar's context menu Nov 27, 2024
if (os_info.dwBuildNumber < 18362)
SetProp(hwnd, TEXT("UseImmersiveDarkModeColors"), SDL_reinterpret_cast(HANDLE, SDL_static_cast(INT_PTR, value)));
else {
SDL_SharedObject *user32 = SDL_LoadObject("user32.dll");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be replaced with GetModuleHandle() and GetProcAddress() if you want to avoid a separate load.

if (SetPreferredAppModeFunc)
SetPreferredAppModeFunc(UXTHEME_APPMODE_ALLOW_DARK);
}
if (RefreshImmersiveColorPolicyStateFunc)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SDL style is:

if (true) {
    statement;
} else {
    statement;
}

UXTHEME_APPMODE_FORCE_LIGHT,
UXTHEME_APPMODE_MAX
} WinPreferredAppMode;
typedef enum {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add blank lines between declarations.

@slouken
Copy link
Collaborator

slouken commented Nov 28, 2024

I'm not thrilled about using undocumented functions to implement dark mode, but other than that, this looks good to me.

@icculus, thoughts?

@slouken slouken added this to the 3.2.0 milestone Nov 28, 2024
@icculus
Copy link
Collaborator

icculus commented Nov 28, 2024

I'm not thrilled about using undocumented functions

Especially because we're looking them up by ordinal instead of symbol name! Undocumentedness aside, are these numbers likely to change in later builds?

@Pixelsuft
Copy link
Author

I'm not thrilled about using undocumented functions

Especially because we're looking them up by ordinal instead of symbol name! Undocumentedness aside, are these numbers likely to change in later builds?

Some functions were changed/added only in some subsequent win10 builds after dark mode appeared first, and then they haven't been changing for a long time. I don't think microsoft will change them for compatibility reasons.

@icculus
Copy link
Collaborator

icculus commented Nov 29, 2024

I'm fine with adding this, but we probably need some switch to turn this off in case a future build of Windows starts making this crash in the field.

@Pixelsuft
Copy link
Author

I'm fine with adding this, but we probably need some switch to turn this off in case a future build of Windows starts making this crash in the field.

I agree, but I don't think I will be able to implement it.

@Pixelsuft
Copy link
Author

Just found some apps already use hidden uxtheme functions:
MPV Player
System Informer
Notepad++ (likely modified darkmode example app code)
Chromium

@bitRAKE
Copy link

bitRAKE commented Dec 9, 2024

Another option is to get the build number from KUSER_SHARED_DATA.NtBuildNumber. Not only is it mapped into all processes, but it's the same on 32/64-bit. Reduces to two instructions to exclude earlier builds.

Edit: I've overstated - KUSER_SHARED_DATA.NtBuildNumber was defined with Win10. This means excluding KUSER_SHARED_DATA.NtMajorVersion less than 10 prior to examining the build number. Good back to NT4 that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants