-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDarkModeHelper.cpp
201 lines (179 loc) · 8.67 KB
/
DarkModeHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// sktoolslib - common files for SK tools
// Copyright (C) 2019-2021 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "DarkModeHelper.h"
#include "StringUtils.h"
#include "PathUtils.h"
#include <vector>
#include <Shlobj.h>
DarkModeHelper& DarkModeHelper::Instance()
{
static DarkModeHelper helper;
return helper;
}
bool DarkModeHelper::CanHaveDarkMode() const
{
return m_bCanHaveDarkMode;
}
void DarkModeHelper::AllowDarkModeForApp(BOOL allow) const
{
if (m_pAllowDarkModeForApp)
m_pAllowDarkModeForApp(allow ? 1 : 0);
if (m_pSetPreferredAppMode)
m_pSetPreferredAppMode(allow ? ForceDark : Default);
}
void DarkModeHelper::AllowDarkModeForWindow(HWND hwnd, BOOL allow) const
{
if (m_pAllowDarkModeForWindow)
m_pAllowDarkModeForWindow(hwnd, allow);
}
BOOL DarkModeHelper::ShouldAppsUseDarkMode() const
{
if (m_pShouldAppsUseDarkMode && m_pAllowDarkModeForApp)
return m_pShouldAppsUseDarkMode() & 0x01;
return ShouldSystemUseDarkMode();
}
BOOL DarkModeHelper::IsDarkModeAllowedForWindow(HWND hwnd) const
{
if (m_pIsDarkModeAllowedForWindow)
return m_pIsDarkModeAllowedForWindow(hwnd);
return FALSE;
}
BOOL DarkModeHelper::IsDarkModeAllowedForApp() const
{
if (m_pIsDarkModeAllowedForApp)
return m_pIsDarkModeAllowedForApp();
return FALSE;
}
BOOL DarkModeHelper::ShouldSystemUseDarkMode() const
{
if (m_pShouldSystemUseDarkMode)
return m_pShouldSystemUseDarkMode();
return FALSE;
}
void DarkModeHelper::RefreshImmersiveColorPolicyState() const
{
if (m_pRefreshImmersiveColorPolicyState)
m_pRefreshImmersiveColorPolicyState();
}
BOOL DarkModeHelper::GetIsImmersiveColorUsingHighContrast(IMMERSIVE_HC_CACHE_MODE mode) const
{
if (m_pGetIsImmersiveColorUsingHighContrast)
return m_pGetIsImmersiveColorUsingHighContrast(mode);
return FALSE;
}
void DarkModeHelper::FlushMenuThemes() const
{
if (m_pFlushMenuThemes)
m_pFlushMenuThemes();
}
BOOL DarkModeHelper::SetWindowCompositionAttribute(HWND hWnd, WINDOWCOMPOSITIONATTRIBDATA* data) const
{
if (m_pSetWindowCompositionAttribute)
return m_pSetWindowCompositionAttribute(hWnd, data);
return FALSE;
}
void DarkModeHelper::RefreshTitleBarThemeColor(HWND hWnd, BOOL dark) const
{
WINDOWCOMPOSITIONATTRIBDATA data = {WCA_USEDARKMODECOLORS, &dark, sizeof(dark)};
SetWindowCompositionAttribute(hWnd, &data);
}
DarkModeHelper::DarkModeHelper()
{
INITCOMMONCONTROLSEX used = {
sizeof(INITCOMMONCONTROLSEX),
ICC_STANDARD_CLASSES | ICC_BAR_CLASSES | ICC_COOL_CLASSES};
InitCommonControlsEx(&used);
m_bCanHaveDarkMode = false;
PWSTR sysPath = nullptr;
long micro = 0;
std::wstring dllPath;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_System, 0, nullptr, &sysPath)))
{
dllPath = sysPath;
CoTaskMemFree(sysPath);
dllPath += L"\\uxtheme.dll";
auto version = CPathUtils::GetVersionFromFile(dllPath);
std::vector<std::wstring> tokens;
stringtok(tokens, version, false, L".");
if (tokens.size() == 4)
{
auto major = std::stol(tokens[0]);
auto minor = std::stol(tokens[1]);
micro = std::stol(tokens[2]);
//auto build = std::stol(tokens[3]);
// the windows 10 update 1809 has the version
// number as 10.0.17763.1
if (major > 10)
m_bCanHaveDarkMode = true;
else if (major == 10)
{
if (minor > 0)
m_bCanHaveDarkMode = true;
else if (micro > 17762)
m_bCanHaveDarkMode = true;
}
}
}
if (dllPath.empty())
m_hUxthemeLib = LoadLibrary(L"uxtheme.dll");
else
m_hUxthemeLib = LoadLibrary(dllPath.c_str());
if (m_hUxthemeLib && m_bCanHaveDarkMode)
{
// Note: these functions are undocumented! Which meas I shouldn't even use them.
// So, since MS decided to keep this new feature to themselves, I have to use
// undocumented functions to adjust.
// Let's just hope they change their minds and document these functions one day...
// first try with the names, just in case MS decides to properly export these functions
if (micro < 18362)
m_pAllowDarkModeForApp = reinterpret_cast<AllowDarkModeForAppFpn>(GetProcAddress(m_hUxthemeLib, "AllowDarkModeForApp"));
else
m_pSetPreferredAppMode = reinterpret_cast<SetPreferredAppModeFpn>(GetProcAddress(m_hUxthemeLib, "SetPreferredAppMode"));
m_pAllowDarkModeForWindow = reinterpret_cast<AllowDarkModeForWindowFpn>(GetProcAddress(m_hUxthemeLib, "AllowDarkModeForWindow"));
m_pShouldAppsUseDarkMode = reinterpret_cast<ShouldAppsUseDarkModeFpn>(GetProcAddress(m_hUxthemeLib, "ShouldAppsUseDarkMode"));
m_pIsDarkModeAllowedForWindow = reinterpret_cast<IsDarkModeAllowedForWindowFpn>(GetProcAddress(m_hUxthemeLib, "IsDarkModeAllowedForWindow"));
m_pIsDarkModeAllowedForApp = reinterpret_cast<IsDarkModeAllowedForAppFpn>(GetProcAddress(m_hUxthemeLib, "IsDarkModeAllowedForApp"));
m_pShouldSystemUseDarkMode = reinterpret_cast<ShouldSystemUseDarkModeFpn>(GetProcAddress(m_hUxthemeLib, "ShouldSystemUseDarkMode"));
m_pRefreshImmersiveColorPolicyState = reinterpret_cast<RefreshImmersiveColorPolicyStateFn>(GetProcAddress(m_hUxthemeLib, "RefreshImmersiveColorPolicyState"));
m_pGetIsImmersiveColorUsingHighContrast = reinterpret_cast<GetIsImmersiveColorUsingHighContrastFn>(GetProcAddress(m_hUxthemeLib, "GetIsImmersiveColorUsingHighContrast"));
m_pFlushMenuThemes = reinterpret_cast<FlushMenuThemesFn>(GetProcAddress(m_hUxthemeLib, "FlushMenuThemes"));
m_pSetWindowCompositionAttribute = reinterpret_cast<SetWindowCompositionAttributeFpn>(GetProcAddress(GetModuleHandleW(L"user32.dll"), "SetWindowCompositionAttribute"));
if (m_pAllowDarkModeForApp == nullptr && micro < 18362)
m_pAllowDarkModeForApp = reinterpret_cast<AllowDarkModeForAppFpn>(GetProcAddress(m_hUxthemeLib, MAKEINTRESOURCEA(135)));
if (m_pSetPreferredAppMode == nullptr && micro >= 18362)
m_pSetPreferredAppMode = reinterpret_cast<SetPreferredAppModeFpn>(GetProcAddress(m_hUxthemeLib, MAKEINTRESOURCEA(135)));
if (m_pAllowDarkModeForWindow == nullptr)
m_pAllowDarkModeForWindow = reinterpret_cast<AllowDarkModeForWindowFpn>(GetProcAddress(m_hUxthemeLib, MAKEINTRESOURCEA(133)));
if (m_pShouldAppsUseDarkMode == nullptr)
m_pShouldAppsUseDarkMode = reinterpret_cast<ShouldAppsUseDarkModeFpn>(GetProcAddress(m_hUxthemeLib, MAKEINTRESOURCEA(132)));
if (m_pIsDarkModeAllowedForWindow == nullptr)
m_pIsDarkModeAllowedForWindow = reinterpret_cast<IsDarkModeAllowedForWindowFpn>(GetProcAddress(m_hUxthemeLib, MAKEINTRESOURCEA(137)));
if (m_pIsDarkModeAllowedForApp == nullptr)
m_pIsDarkModeAllowedForApp = reinterpret_cast<IsDarkModeAllowedForAppFpn>(GetProcAddress(m_hUxthemeLib, MAKEINTRESOURCEA(139)));
if (m_pShouldSystemUseDarkMode == nullptr)
m_pShouldSystemUseDarkMode = reinterpret_cast<ShouldSystemUseDarkModeFpn>(GetProcAddress(m_hUxthemeLib, MAKEINTRESOURCEA(138)));
if (m_pRefreshImmersiveColorPolicyState == nullptr)
m_pRefreshImmersiveColorPolicyState = reinterpret_cast<RefreshImmersiveColorPolicyStateFn>(GetProcAddress(m_hUxthemeLib, MAKEINTRESOURCEA(104)));
if (m_pGetIsImmersiveColorUsingHighContrast == nullptr)
m_pGetIsImmersiveColorUsingHighContrast = reinterpret_cast<GetIsImmersiveColorUsingHighContrastFn>(GetProcAddress(m_hUxthemeLib, MAKEINTRESOURCEA(106)));
if (m_pFlushMenuThemes == nullptr)
m_pFlushMenuThemes = reinterpret_cast<FlushMenuThemesFn>(GetProcAddress(m_hUxthemeLib, MAKEINTRESOURCEA(136)));
}
}
DarkModeHelper::~DarkModeHelper()
{
FreeLibrary(m_hUxthemeLib);
}