|
| 1 | +/* |
| 2 | + * AUI Framework - Declarative UI toolkit for modern C++20 |
| 3 | + * Copyright (C) 2020-2025 Alex2772 and Contributors |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: MPL-2.0 |
| 6 | + * |
| 7 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 8 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 9 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 10 | + */ |
| 11 | + |
| 12 | +// |
| 13 | +// Created by Nelonn on 12/17/2025. |
| 14 | +// |
| 15 | + |
| 16 | +// Credit: SDL3 |
| 17 | + |
| 18 | +#include "Theme.h" |
| 19 | + |
| 20 | +#include <dwmapi.h> |
| 21 | + |
| 22 | +typedef enum { |
| 23 | + UXTHEME_APPMODE_DEFAULT, |
| 24 | + UXTHEME_APPMODE_ALLOW_DARK, |
| 25 | + UXTHEME_APPMODE_FORCE_DARK, |
| 26 | + UXTHEME_APPMODE_FORCE_LIGHT, |
| 27 | + UXTHEME_APPMODE_MAX |
| 28 | +} UxthemePreferredAppMode; |
| 29 | + |
| 30 | +typedef enum { |
| 31 | + WCA_UNDEFINED = 0, |
| 32 | + WCA_USEDARKMODECOLORS = 26, |
| 33 | + WCA_LAST = 27 |
| 34 | +} WINDOWCOMPOSITIONATTRIB; |
| 35 | + |
| 36 | +typedef struct { |
| 37 | + WINDOWCOMPOSITIONATTRIB Attrib; |
| 38 | + PVOID pvData; |
| 39 | + SIZE_T cbData; |
| 40 | +} WINDOWCOMPOSITIONATTRIBDATA; |
| 41 | + |
| 42 | +typedef struct { |
| 43 | + ULONG dwOSVersionInfoSize; |
| 44 | + ULONG dwMajorVersion; |
| 45 | + ULONG dwMinorVersion; |
| 46 | + ULONG dwBuildNumber; |
| 47 | + ULONG dwPlatformId; |
| 48 | + WCHAR szCSDVersion[128]; |
| 49 | +} NT_OSVERSIONINFOW; |
| 50 | + |
| 51 | +typedef bool (WINAPI *ShouldAppsUseDarkMode_t)(void); |
| 52 | +typedef void (WINAPI *AllowDarkModeForWindow_t)(HWND, bool); |
| 53 | +typedef void (WINAPI *AllowDarkModeForApp_t)(bool); |
| 54 | +typedef void (WINAPI *RefreshImmersiveColorPolicyState_t)(void); |
| 55 | +typedef UxthemePreferredAppMode (WINAPI *SetPreferredAppMode_t)(UxthemePreferredAppMode); |
| 56 | +typedef BOOL (WINAPI *SetWindowCompositionAttribute_t)(HWND, const WINDOWCOMPOSITIONATTRIBDATA *); |
| 57 | +typedef void (NTAPI *RtlGetVersion_t)(NT_OSVERSIONINFOW *); |
| 58 | + |
| 59 | +namespace aui { |
| 60 | + |
| 61 | +bool IsWindowsBuildVersionAtLeast(DWORD dwBuildNumber) { |
| 62 | + static DWORD BuildNumber = 0; |
| 63 | + if (BuildNumber != 0) { |
| 64 | + return (BuildNumber >= dwBuildNumber); |
| 65 | + } |
| 66 | + |
| 67 | + HMODULE ntdll = LoadLibrary(TEXT("ntdll.dll")); |
| 68 | + if (!ntdll) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + // There is no function to get Windows build number, so let's get it here via RtlGetVersion |
| 72 | + RtlGetVersion_t RtlGetVersionFunc = (RtlGetVersion_t)GetProcAddress(ntdll, "RtlGetVersion"); |
| 73 | + NT_OSVERSIONINFOW os_info; |
| 74 | + os_info.dwOSVersionInfoSize = sizeof(NT_OSVERSIONINFOW); |
| 75 | + os_info.dwBuildNumber = 0; |
| 76 | + if (RtlGetVersionFunc) { |
| 77 | + RtlGetVersionFunc(&os_info); |
| 78 | + } |
| 79 | + FreeLibrary(ntdll); |
| 80 | + |
| 81 | + BuildNumber = (os_info.dwBuildNumber & ~0xF0000000); |
| 82 | + return (BuildNumber >= dwBuildNumber); |
| 83 | +} |
| 84 | + |
| 85 | +void UpdateDarkModeForHWND(HWND hwnd) { |
| 86 | + if (!IsWindowsBuildVersionAtLeast(17763)) return; |
| 87 | + |
| 88 | + HMODULE uxtheme = LoadLibrary(TEXT("uxtheme.dll")); |
| 89 | + if (!uxtheme) return; |
| 90 | + |
| 91 | + RefreshImmersiveColorPolicyState_t RefreshImmersiveColorPolicyStateFunc = (RefreshImmersiveColorPolicyState_t)GetProcAddress(uxtheme, MAKEINTRESOURCEA(104)); |
| 92 | + ShouldAppsUseDarkMode_t ShouldAppsUseDarkModeFunc = (ShouldAppsUseDarkMode_t)GetProcAddress(uxtheme, MAKEINTRESOURCEA(132)); |
| 93 | + AllowDarkModeForWindow_t AllowDarkModeForWindowFunc = (AllowDarkModeForWindow_t)GetProcAddress(uxtheme, MAKEINTRESOURCEA(133)); |
| 94 | + if (!IsWindowsBuildVersionAtLeast(18362)) { |
| 95 | + AllowDarkModeForApp_t AllowDarkModeForAppFunc = (AllowDarkModeForApp_t)GetProcAddress(uxtheme, MAKEINTRESOURCEA(135)); |
| 96 | + if (AllowDarkModeForAppFunc) { |
| 97 | + AllowDarkModeForAppFunc(true); |
| 98 | + } |
| 99 | + } else { |
| 100 | + SetPreferredAppMode_t SetPreferredAppModeFunc = (SetPreferredAppMode_t)GetProcAddress(uxtheme, MAKEINTRESOURCEA(135)); |
| 101 | + if (SetPreferredAppModeFunc) { |
| 102 | + SetPreferredAppModeFunc(UXTHEME_APPMODE_ALLOW_DARK); |
| 103 | + } |
| 104 | + } |
| 105 | + if (RefreshImmersiveColorPolicyStateFunc) { |
| 106 | + RefreshImmersiveColorPolicyStateFunc(); |
| 107 | + } |
| 108 | + if (AllowDarkModeForWindowFunc) { |
| 109 | + AllowDarkModeForWindowFunc(hwnd, true); |
| 110 | + } |
| 111 | + BOOL value; |
| 112 | + if (ShouldAppsUseDarkModeFunc) { |
| 113 | + value = ShouldAppsUseDarkModeFunc() ? TRUE : FALSE; |
| 114 | + } else { |
| 115 | + value = TRUE; |
| 116 | + } |
| 117 | + FreeLibrary(uxtheme); |
| 118 | + if (!IsWindowsBuildVersionAtLeast(18362)) { |
| 119 | + SetProp(hwnd, TEXT("UseImmersiveDarkModeColors"), reinterpret_cast<HANDLE>(static_cast<INT_PTR>(value))); |
| 120 | + } else { |
| 121 | + HMODULE user32 = GetModuleHandle(TEXT("user32.dll")); |
| 122 | + if (user32) { |
| 123 | + SetWindowCompositionAttribute_t SetWindowCompositionAttributeFunc = (SetWindowCompositionAttribute_t)GetProcAddress(user32, "SetWindowCompositionAttribute"); |
| 124 | + if (SetWindowCompositionAttributeFunc) { |
| 125 | + WINDOWCOMPOSITIONATTRIBDATA data = { WCA_USEDARKMODECOLORS, &value, sizeof(value) }; |
| 126 | + SetWindowCompositionAttributeFunc(hwnd, &data); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +} |
0 commit comments