Skip to content

Commit 8fcab0a

Browse files
committed
Implement dark mode support for dialog backgrounds and controls; enhance transparency dialog functionality
1 parent 16dc36a commit 8fcab0a

2 files changed

Lines changed: 172 additions & 16 deletions

File tree

src/modules/background.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,31 @@
1818
#include "theme.h"
1919
#include "resource.h"
2020
#include <commdlg.h>
21+
#include <uxtheme.h>
2122
#include <algorithm>
2223

24+
static HBRUSH GetBackgroundDialogBrush()
25+
{
26+
static HBRUSH brush = CreateSolidBrush(RGB(32, 32, 32));
27+
return brush;
28+
}
29+
30+
static HBRUSH GetBackgroundDialogEditBrush()
31+
{
32+
static HBRUSH brush = CreateSolidBrush(RGB(45, 45, 45));
33+
return brush;
34+
}
35+
36+
static void ApplyBackgroundDialogDarkMode(HWND hDlg)
37+
{
38+
if (!IsDarkMode())
39+
return;
40+
SetTitleBarDark(hDlg, TRUE);
41+
SetWindowTheme(hDlg, L"DarkMode_Explorer", nullptr);
42+
for (HWND h = GetWindow(hDlg, GW_CHILD); h; h = GetWindow(h, GW_HWNDNEXT))
43+
SetWindowTheme(h, L"DarkMode_Explorer", nullptr);
44+
}
45+
2346
void LoadBackgroundImage(const std::wstring &path)
2447
{
2548
if (g_bgImage)
@@ -257,6 +280,7 @@ static INT_PTR CALLBACK OpacityDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARA
257280
{
258281
case WM_INITDIALOG:
259282
{
283+
ApplyBackgroundDialogDarkMode(hDlg);
260284
HFONT hFont = reinterpret_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
261285
CreateWindowExW(0, L"STATIC", L"Opacity (0-100%):", WS_CHILD | WS_VISIBLE, 10, 15, 110, 20, hDlg, nullptr, nullptr, nullptr);
262286
hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", L"", WS_CHILD | WS_VISIBLE | ES_NUMBER, 125, 12, 60, 22, hDlg, reinterpret_cast<HMENU>(1001), nullptr, nullptr);
@@ -272,6 +296,26 @@ static INT_PTR CALLBACK OpacityDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARA
272296
SetFocus(hEdit);
273297
return FALSE;
274298
}
299+
case WM_CTLCOLOREDIT:
300+
if (IsDarkMode())
301+
{
302+
HDC hdc = reinterpret_cast<HDC>(wParam);
303+
SetTextColor(hdc, RGB(240, 240, 240));
304+
SetBkColor(hdc, RGB(45, 45, 45));
305+
return reinterpret_cast<INT_PTR>(GetBackgroundDialogEditBrush());
306+
}
307+
break;
308+
case WM_CTLCOLORSTATIC:
309+
case WM_CTLCOLORBTN:
310+
case WM_CTLCOLORDLG:
311+
if (IsDarkMode())
312+
{
313+
HDC hdc = reinterpret_cast<HDC>(wParam);
314+
SetTextColor(hdc, RGB(240, 240, 240));
315+
SetBkColor(hdc, RGB(32, 32, 32));
316+
return reinterpret_cast<INT_PTR>(GetBackgroundDialogBrush());
317+
}
318+
break;
275319
case WM_COMMAND:
276320
if (LOWORD(wParam) == IDOK)
277321
{

src/modules/dialog.cpp

Lines changed: 128 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,104 @@
1717
#include "core/globals.h"
1818
#include "editor.h"
1919
#include "ui.h"
20+
#include "theme.h"
2021
#include "settings.h"
2122
#include "lang/lang.h"
2223
#include <commdlg.h>
2324
#include <richedit.h>
25+
#include <uxtheme.h>
2426
#include <algorithm>
2527
#include <cwctype>
2628

29+
static HWND g_transparencyEdit = nullptr;
30+
31+
static HBRUSH GetDialogBackgroundBrush()
32+
{
33+
static HBRUSH brush = CreateSolidBrush(RGB(32, 32, 32));
34+
return brush;
35+
}
36+
37+
static HBRUSH GetDialogEditBrush()
38+
{
39+
static HBRUSH brush = CreateSolidBrush(RGB(45, 45, 45));
40+
return brush;
41+
}
42+
43+
static void ApplyDialogDarkMode(HWND hDlg)
44+
{
45+
if (!IsDarkMode())
46+
return;
47+
SetTitleBarDark(hDlg, TRUE);
48+
SetWindowTheme(hDlg, L"DarkMode_Explorer", nullptr);
49+
for (HWND h = GetWindow(hDlg, GW_CHILD); h; h = GetWindow(h, GW_HWNDNEXT))
50+
SetWindowTheme(h, L"DarkMode_Explorer", nullptr);
51+
}
52+
53+
static INT_PTR HandleDialogDarkColors(UINT msg, WPARAM wParam)
54+
{
55+
if (!IsDarkMode())
56+
return 0;
57+
HDC hdc = reinterpret_cast<HDC>(wParam);
58+
switch (msg)
59+
{
60+
case WM_CTLCOLOREDIT:
61+
SetTextColor(hdc, RGB(240, 240, 240));
62+
SetBkColor(hdc, RGB(45, 45, 45));
63+
return reinterpret_cast<INT_PTR>(GetDialogEditBrush());
64+
case WM_CTLCOLORSTATIC:
65+
case WM_CTLCOLORBTN:
66+
case WM_CTLCOLORDLG:
67+
SetTextColor(hdc, RGB(240, 240, 240));
68+
SetBkColor(hdc, RGB(32, 32, 32));
69+
return reinterpret_cast<INT_PTR>(GetDialogBackgroundBrush());
70+
}
71+
return 0;
72+
}
73+
74+
static INT_PTR CALLBACK TransparencyDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM)
75+
{
76+
switch (msg)
77+
{
78+
case WM_INITDIALOG:
79+
ApplyDialogDarkMode(hDlg);
80+
return TRUE;
81+
case WM_COMMAND:
82+
if (LOWORD(wParam) == IDOK)
83+
{
84+
wchar_t buf[32] = {};
85+
GetWindowTextW(g_transparencyEdit, buf, 32);
86+
int val = _wtoi(buf);
87+
val = (val < 10) ? 10 : (val > 100) ? 100
88+
: val;
89+
g_state.windowOpacity = static_cast<BYTE>(val * 255 / 100);
90+
SetWindowLongW(g_hwndMain, GWL_EXSTYLE, GetWindowLongW(g_hwndMain, GWL_EXSTYLE) | WS_EX_LAYERED);
91+
SetLayeredWindowAttributes(g_hwndMain, 0, g_state.windowOpacity, LWA_ALPHA);
92+
EndDialog(hDlg, IDOK);
93+
return TRUE;
94+
}
95+
if (LOWORD(wParam) == IDCANCEL)
96+
{
97+
EndDialog(hDlg, IDCANCEL);
98+
return TRUE;
99+
}
100+
break;
101+
case WM_CTLCOLOREDIT:
102+
case WM_CTLCOLORSTATIC:
103+
case WM_CTLCOLORBTN:
104+
case WM_CTLCOLORDLG:
105+
{
106+
INT_PTR colorResult = HandleDialogDarkColors(msg, wParam);
107+
if (colorResult)
108+
return colorResult;
109+
break;
110+
}
111+
case WM_CLOSE:
112+
EndDialog(hDlg, IDCANCEL);
113+
return TRUE;
114+
}
115+
return FALSE;
116+
}
117+
27118
void DoFind(bool forward)
28119
{
29120
if (g_state.findText.empty())
@@ -66,6 +157,7 @@ INT_PTR CALLBACK FindDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
66157
switch (msg)
67158
{
68159
case WM_INITDIALOG:
160+
ApplyDialogDarkMode(hDlg);
69161
SetWindowTextW(GetDlgItem(hDlg, 1001), g_state.findText.c_str());
70162
if (GetDlgItem(hDlg, 1002))
71163
SetWindowTextW(GetDlgItem(hDlg, 1002), g_state.replaceText.c_str());
@@ -148,16 +240,23 @@ INT_PTR CALLBACK FindDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
148240
{
149241
PAINTSTRUCT ps;
150242
HDC hdc = BeginPaint(hDlg, &ps);
151-
HBRUSH hBrush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
243+
HBRUSH hBrush = IsDarkMode() ? GetDialogBackgroundBrush() : CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
152244
FillRect(hdc, &ps.rcPaint, hBrush);
153-
DeleteObject(hBrush);
245+
if (!IsDarkMode())
246+
DeleteObject(hBrush);
154247
EndPaint(hDlg, &ps);
155248
return FALSE;
156249
}
250+
case WM_CTLCOLOREDIT:
157251
case WM_CTLCOLORSTATIC:
158252
case WM_CTLCOLORBTN:
159253
case WM_CTLCOLORDLG:
254+
{
255+
INT_PTR colorResult = HandleDialogDarkColors(msg, wParam);
256+
if (colorResult)
257+
return colorResult;
160258
return reinterpret_cast<INT_PTR>(GetSysColorBrush(COLOR_BTNFACE));
259+
}
161260
case WM_CLOSE:
162261
DestroyWindow(hDlg);
163262
g_hwndFindDlg = nullptr;
@@ -191,6 +290,7 @@ void EditFind()
191290
for (HWND h = GetWindow(g_hwndFindDlg, GW_CHILD); h; h = GetWindow(h, GW_HWNDNEXT))
192291
SendMessageW(h, WM_SETFONT, reinterpret_cast<WPARAM>(hFont), TRUE);
193292
SetWindowLongPtrW(g_hwndFindDlg, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(FindDlgProc));
293+
ApplyDialogDarkMode(g_hwndFindDlg);
194294
InvalidateRect(g_hwndFindDlg, nullptr, FALSE);
195295
UpdateWindow(g_hwndFindDlg);
196296
}
@@ -233,6 +333,7 @@ void EditReplace()
233333
for (HWND h = GetWindow(g_hwndFindDlg, GW_CHILD); h; h = GetWindow(h, GW_HWNDNEXT))
234334
SendMessageW(h, WM_SETFONT, reinterpret_cast<WPARAM>(hFont), TRUE);
235335
SetWindowLongPtrW(g_hwndFindDlg, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(FindDlgProc));
336+
ApplyDialogDarkMode(g_hwndFindDlg);
236337
InvalidateRect(g_hwndFindDlg, nullptr, FALSE);
237338
UpdateWindow(g_hwndFindDlg);
238339
}
@@ -242,6 +343,19 @@ INT_PTR CALLBACK GotoDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
242343
{
243344
switch (msg)
244345
{
346+
case WM_INITDIALOG:
347+
ApplyDialogDarkMode(hDlg);
348+
return TRUE;
349+
case WM_CTLCOLOREDIT:
350+
case WM_CTLCOLORSTATIC:
351+
case WM_CTLCOLORBTN:
352+
case WM_CTLCOLORDLG:
353+
{
354+
INT_PTR colorResult = HandleDialogDarkColors(msg, wParam);
355+
if (colorResult)
356+
return colorResult;
357+
break;
358+
}
245359
case WM_COMMAND:
246360
if (LOWORD(wParam) == IDOK)
247361
{
@@ -306,6 +420,7 @@ void EditGoto()
306420
SendMessageW(h, WM_SETFONT, reinterpret_cast<WPARAM>(hFont), TRUE);
307421

308422
SetWindowLongPtrW(hDlg, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(GotoDlgProc));
423+
ApplyDialogDarkMode(hDlg);
309424
SetFocus(hEdit);
310425
}
311426
}
@@ -363,35 +478,32 @@ void ViewTransparency()
363478
HFONT hFont = reinterpret_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
364479
CreateWindowExW(0, L"STATIC", lang.dialogOpacityLabel.c_str(), WS_CHILD | WS_VISIBLE, 10, 18, 110, 20, hDlg, nullptr, nullptr, nullptr);
365480
HWND hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", buf, WS_CHILD | WS_VISIBLE | ES_NUMBER, 125, 15, 60, 22, hDlg, reinterpret_cast<HMENU>(1001), nullptr, nullptr);
366-
HWND hOk = CreateWindowExW(0, L"BUTTON", lang.dialogOK.c_str(), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 50, 50, 70, 26, hDlg, reinterpret_cast<HMENU>(IDOK), nullptr, nullptr);
367-
HWND hCancel = CreateWindowExW(0, L"BUTTON", lang.dialogCancel.c_str(), WS_CHILD | WS_VISIBLE, 130, 50, 70, 26, hDlg, reinterpret_cast<HMENU>(IDCANCEL), nullptr, nullptr);
481+
CreateWindowExW(0, L"BUTTON", lang.dialogOK.c_str(), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 50, 50, 70, 26, hDlg, reinterpret_cast<HMENU>(IDOK), nullptr, nullptr);
482+
CreateWindowExW(0, L"BUTTON", lang.dialogCancel.c_str(), WS_CHILD | WS_VISIBLE, 130, 50, 70, 26, hDlg, reinterpret_cast<HMENU>(IDCANCEL), nullptr, nullptr);
368483
for (HWND h = GetWindow(hDlg, GW_CHILD); h; h = GetWindow(h, GW_HWNDNEXT))
369484
SendMessageW(h, WM_SETFONT, reinterpret_cast<WPARAM>(hFont), TRUE);
485+
g_transparencyEdit = hEdit;
486+
SetWindowLongPtrW(hDlg, DWLP_DLGPROC, reinterpret_cast<LONG_PTR>(TransparencyDlgProc));
487+
TransparencyDlgProc(hDlg, WM_INITDIALOG, 0, 0);
370488
MSG msg;
371489
while (GetMessageW(&msg, nullptr, 0, 0))
372490
{
373491
if (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE)
374-
break;
375-
if (msg.hwnd == hOk && msg.message == WM_LBUTTONUP)
376492
{
377-
GetWindowTextW(hEdit, buf, 32);
378-
int val = _wtoi(buf);
379-
val = (val < 10) ? 10 : (val > 100) ? 100
380-
: val;
381-
g_state.windowOpacity = static_cast<BYTE>(val * 255 / 100);
382-
SetWindowLongW(g_hwndMain, GWL_EXSTYLE, GetWindowLongW(g_hwndMain, GWL_EXSTYLE) | WS_EX_LAYERED);
383-
SetLayeredWindowAttributes(g_hwndMain, 0, g_state.windowOpacity, LWA_ALPHA);
493+
SendMessageW(hDlg, WM_COMMAND, IDCANCEL, 0);
384494
break;
385495
}
386-
if (msg.hwnd == hCancel && msg.message == WM_LBUTTONUP)
387-
break;
388496
if (!IsDialogMessageW(hDlg, &msg))
389497
{
390498
TranslateMessage(&msg);
391499
DispatchMessageW(&msg);
392500
}
501+
if (!IsWindow(hDlg))
502+
break;
393503
}
394-
DestroyWindow(hDlg);
504+
if (IsWindow(hDlg))
505+
DestroyWindow(hDlg);
506+
g_transparencyEdit = nullptr;
395507
}
396508
}
397509

0 commit comments

Comments
 (0)