Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
Minor refactoring
  • Loading branch information
0x7c13 committed Apr 7, 2024
1 parent 3f9cfe0 commit f375b4c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# DarkMode Mod for UnityEditor on Windows
<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>

A fully working dark mode mod for Unity Editor on Windows with:
A fully working runtime dark mode mod for Unity Editor on Windows with:
- Dark title bar
- Dark menu bar
- Dark context menu
- And more...

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

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

Expand All @@ -33,6 +33,7 @@ This works 100% on Windows 11 and should work on Windows 10 as well (most likely
}
}
```
> **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`.
- Restart Unity Editor and you are done!
- Now enjoy the immersive dark mode in Unity Editor!

Expand Down
71 changes: 43 additions & 28 deletions UnityEditorDarkMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ enum
WM_UAHNCPAINTMENUPOPUP = 0x0095
};

// undocumented app mode enum for the private SetPreferredAppMode API
enum class PreferredAppMode
{
Default,
AllowDark,
ForceDark,
ForceLight,
Max
};

// describes the sizes of the menu bar or menu item
typedef union tagUAHMENUITEMMETRICS
{
Expand Down Expand Up @@ -116,11 +126,10 @@ typedef struct {
HBRUSH menubaritem_bgbrush_selected;
} theme_cfg;

// global variables
static HTHEME g_menuTheme = nullptr;
HHOOK g_hook = nullptr;

LRESULT CALLBACK CallWndSubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);

bool IsWndClass(HWND hWnd, const TCHAR* classname) {
TCHAR buf[512];
GetClassName(hWnd, buf, 512);
Expand Down Expand Up @@ -169,8 +178,8 @@ const theme_cfg* LoadThemeConfig() {
configFile << "menubar_textcolor_disabled = 160,160,160" << std::endl;
configFile << "menubar_bgcolor = 48,48,48" << std::endl;
configFile << "menubaritem_bgcolor = 48,48,48" << std::endl;
configFile << "menubaritem_bgcolor_hot = 48,48,48" << std::endl;
configFile << "menubaritem_bgcolor_selected = 48,48,48" << std::endl;
configFile << "menubaritem_bgcolor_hot = 62,62,62" << std::endl;
configFile << "menubaritem_bgcolor_selected = 62,62,62" << std::endl;
configFile.close();
}

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

const BOOL USE_DARK_MODE = true;

DwmSetWindowAttribute(hWnd,
DWMWA_USE_IMMERSIVE_DARK_MODE,
&USE_DARK_MODE,
sizeof(USE_DARK_MODE));
// apply dark mode to the window
{
const BOOL USE_DARK_MODE = true;

static HMODULE hUxtheme = nullptr;
DwmSetWindowAttribute(hWnd,
DWMWA_USE_IMMERSIVE_DARK_MODE,
&USE_DARK_MODE,
sizeof(USE_DARK_MODE));
}

enum class PreferredAppMode
// apply dark mode to the context menus
{
Default,
AllowDark,
ForceDark,
ForceLight,
Max
};

// ordinal 135, in 1903
using fnSetPreferredAppMode = PreferredAppMode(WINAPI*)(PreferredAppMode appMode);
fnSetPreferredAppMode SetPreferredAppMode;

if (!hUxtheme) {
hUxtheme = LoadLibraryExW(L"uxtheme.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
}
using fnSetPreferredAppMode = PreferredAppMode(WINAPI*)(PreferredAppMode appMode);
fnSetPreferredAppMode SetPreferredAppMode;

static HMODULE hUxtheme = nullptr;

if (!hUxtheme) {
hUxtheme = LoadLibraryExW(L"uxtheme.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
}

SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
SetPreferredAppMode(PreferredAppMode::ForceDark);
if (hUxtheme) {
// #135 is the ordinal for SetPreferredAppMode private API
// which is available in uxtheme.dll since Windows 10 1903+
SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
SetPreferredAppMode(PreferredAppMode::ForceDark);
}
}
}

LRESULT CALLBACK CallWndSubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);

LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) {
switch (nCode) {
case HCBT_CREATEWND:
Expand Down Expand Up @@ -417,6 +429,7 @@ LRESULT CALLBACK CallWndSubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
return TRUE;
}
}
break;
}
case WM_NCACTIVATE:
{
Expand Down Expand Up @@ -600,6 +613,7 @@ bool APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpRes) {
switch (reason)
{
case DLL_PROCESS_ATTACH: {
// enable dark mode for the current process window
EnableDarkMode(nullptr);

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

// set up the CBTProc hook to catch new windows
g_hook = SetWindowsHookEx(WH_CBT, CBTProc, nullptr, GetCurrentThreadId());
break;
}
Expand Down

0 comments on commit f375b4c

Please sign in to comment.