Skip to content

Commit f375b4c

Browse files
committed
Minor refactoring
Minor refactoring
1 parent 3f9cfe0 commit f375b4c

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# DarkMode Mod for UnityEditor on Windows
22
<a style="text-decoration:none" href="https://github.com/0x7c13/UnityEditor-DarkMode/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/0x7c13/UnityEditor-DarkMode/ci.yml" alt="Size" /></a>
33

4-
A fully working dark mode mod for Unity Editor on Windows with:
4+
A fully working runtime dark mode mod for Unity Editor on Windows with:
55
- Dark title bar
66
- Dark menu bar
77
- Dark context menu
88
- And more...
99

10-
This works 100% on Windows 11 and should work on Windows 10 as well (most likely, not tested).
10+
This works 100% on Windows 11 and should work on Windows 10 1903+ as well.
1111

1212
![Screenshot](screenshot.png?raw=true)
1313

@@ -33,6 +33,7 @@ This works 100% on Windows 11 and should work on Windows 10 as well (most likely
3333
}
3434
}
3535
```
36+
> **NOTE:** You could also inject the dll using `withdll.exe` from [Detours](https://github.com/microsoft/Detours). But I prefer this approach as it is clean and doesn't require any external tools. Instructions are provided in the later sections for the use of `withdll.exe`.
3637
- Restart Unity Editor and you are done!
3738
- Now enjoy the immersive dark mode in Unity Editor!
3839

UnityEditorDarkMode.cpp

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ enum
4444
WM_UAHNCPAINTMENUPOPUP = 0x0095
4545
};
4646

47+
// undocumented app mode enum for the private SetPreferredAppMode API
48+
enum class PreferredAppMode
49+
{
50+
Default,
51+
AllowDark,
52+
ForceDark,
53+
ForceLight,
54+
Max
55+
};
56+
4757
// describes the sizes of the menu bar or menu item
4858
typedef union tagUAHMENUITEMMETRICS
4959
{
@@ -116,11 +126,10 @@ typedef struct {
116126
HBRUSH menubaritem_bgbrush_selected;
117127
} theme_cfg;
118128

129+
// global variables
119130
static HTHEME g_menuTheme = nullptr;
120131
HHOOK g_hook = nullptr;
121132

122-
LRESULT CALLBACK CallWndSubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
123-
124133
bool IsWndClass(HWND hWnd, const TCHAR* classname) {
125134
TCHAR buf[512];
126135
GetClassName(hWnd, buf, 512);
@@ -169,8 +178,8 @@ const theme_cfg* LoadThemeConfig() {
169178
configFile << "menubar_textcolor_disabled = 160,160,160" << std::endl;
170179
configFile << "menubar_bgcolor = 48,48,48" << std::endl;
171180
configFile << "menubaritem_bgcolor = 48,48,48" << std::endl;
172-
configFile << "menubaritem_bgcolor_hot = 48,48,48" << std::endl;
173-
configFile << "menubaritem_bgcolor_selected = 48,48,48" << std::endl;
181+
configFile << "menubaritem_bgcolor_hot = 62,62,62" << std::endl;
182+
configFile << "menubaritem_bgcolor_selected = 62,62,62" << std::endl;
174183
configFile.close();
175184
}
176185

@@ -208,38 +217,41 @@ const theme_cfg* LoadThemeConfig() {
208217
// https://stackoverflow.com/questions/39261826/change-the-color-of-the-title-bar-caption-of-a-win32-application
209218
// https://gist.github.com/rounk-ctrl/b04e5622e30e0d62956870d5c22b7017
210219
// https://github.com/microsoft/WindowsAppSDK/issues/41
220+
// https://gist.github.com/ericoporto/1745f4b912e22f9eabfce2c7166d979b
211221
void EnableDarkMode(HWND hWnd) {
212222

213-
const BOOL USE_DARK_MODE = true;
214-
215-
DwmSetWindowAttribute(hWnd,
216-
DWMWA_USE_IMMERSIVE_DARK_MODE,
217-
&USE_DARK_MODE,
218-
sizeof(USE_DARK_MODE));
223+
// apply dark mode to the window
224+
{
225+
const BOOL USE_DARK_MODE = true;
219226

220-
static HMODULE hUxtheme = nullptr;
227+
DwmSetWindowAttribute(hWnd,
228+
DWMWA_USE_IMMERSIVE_DARK_MODE,
229+
&USE_DARK_MODE,
230+
sizeof(USE_DARK_MODE));
231+
}
221232

222-
enum class PreferredAppMode
233+
// apply dark mode to the context menus
223234
{
224-
Default,
225-
AllowDark,
226-
ForceDark,
227-
ForceLight,
228-
Max
229-
};
230-
231-
// ordinal 135, in 1903
232-
using fnSetPreferredAppMode = PreferredAppMode(WINAPI*)(PreferredAppMode appMode);
233-
fnSetPreferredAppMode SetPreferredAppMode;
234-
235-
if (!hUxtheme) {
236-
hUxtheme = LoadLibraryExW(L"uxtheme.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
237-
}
235+
using fnSetPreferredAppMode = PreferredAppMode(WINAPI*)(PreferredAppMode appMode);
236+
fnSetPreferredAppMode SetPreferredAppMode;
237+
238+
static HMODULE hUxtheme = nullptr;
239+
240+
if (!hUxtheme) {
241+
hUxtheme = LoadLibraryExW(L"uxtheme.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
242+
}
238243

239-
SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
240-
SetPreferredAppMode(PreferredAppMode::ForceDark);
244+
if (hUxtheme) {
245+
// #135 is the ordinal for SetPreferredAppMode private API
246+
// which is available in uxtheme.dll since Windows 10 1903+
247+
SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
248+
SetPreferredAppMode(PreferredAppMode::ForceDark);
249+
}
250+
}
241251
}
242252

253+
LRESULT CALLBACK CallWndSubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
254+
243255
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) {
244256
switch (nCode) {
245257
case HCBT_CREATEWND:
@@ -417,6 +429,7 @@ LRESULT CALLBACK CallWndSubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
417429
return TRUE;
418430
}
419431
}
432+
break;
420433
}
421434
case WM_NCACTIVATE:
422435
{
@@ -600,6 +613,7 @@ bool APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpRes) {
600613
switch (reason)
601614
{
602615
case DLL_PROCESS_ATTACH: {
616+
// enable dark mode for the current process window
603617
EnableDarkMode(nullptr);
604618

605619
// catch windows that have been created before we set up the CBTProc hook
@@ -611,6 +625,7 @@ bool APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpRes) {
611625
EnableDarkMode(hWnd);
612626
}
613627

628+
// set up the CBTProc hook to catch new windows
614629
g_hook = SetWindowsHookEx(WH_CBT, CBTProc, nullptr, GetCurrentThreadId());
615630
break;
616631
}

0 commit comments

Comments
 (0)