Skip to content

Commit d7d6965

Browse files
committed
Add interface to get/set dark background state in WinWebDiffLib
1 parent 77e58d9 commit d7d6965

File tree

4 files changed

+75
-8
lines changed

4 files changed

+75
-8
lines changed

src/WinWebDiffLib/WebDiffWindow.hpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ class CWebDiffWindow : public IWebDiffWindow
1212
public:
1313
CWebDiffWindow()
1414
{
15+
s_hbrBackground = CreateSolidBrush(m_bDarkBackgroundEnabled ? RGB(0, 0, 0) : GetSysColor(COLOR_3DFACE));
16+
}
17+
18+
~CWebDiffWindow()
19+
{
20+
if (s_hbrBackground)
21+
DeleteObject(s_hbrBackground);
1522
}
1623

1724
bool Create(HINSTANCE hInstance, HWND hWndParent, int nID, const RECT& rc)
@@ -85,7 +92,7 @@ class CWebDiffWindow : public IWebDiffWindow
8592
std::wstring userDataFolder = GetUserDataFolderPath(i);
8693
ComPtr<IWebDiffCallback> callback2(callback);
8794
hr = m_webWindow[i].Create(this, m_hInstance, m_hWnd, urls[i], userDataFolder.c_str(),
88-
m_size, m_fitToWindow, m_zoom, m_userAgent, nullptr,
95+
m_size, m_fitToWindow, m_zoom, m_bDarkBackgroundEnabled, m_userAgent, nullptr,
8996
[this, i, callback2](WebDiffEvent::EVENT_TYPE event, IUnknown* sender, IUnknown* args)
9097
{
9198
WebDiffEvent ev{};
@@ -433,6 +440,25 @@ class CWebDiffWindow : public IWebDiffWindow
433440
m_colorSettings = settings;
434441
}
435442

443+
bool IsDarkBackgroundEnabled() const
444+
{
445+
return m_bDarkBackgroundEnabled;
446+
}
447+
448+
void SetDarkBackgroundEnabled(bool enabled)
449+
{
450+
m_bDarkBackgroundEnabled = enabled;
451+
if (m_hWnd)
452+
{
453+
for (int pane = 0; pane < m_nPanes; ++pane)
454+
m_webWindow[pane].SetDarkBackgroundEnabled(m_bDarkBackgroundEnabled);
455+
DeleteObject(s_hbrBackground);
456+
s_hbrBackground = CreateSolidBrush(m_bDarkBackgroundEnabled ? RGB(0, 0, 0) : GetSysColor(COLOR_3DFACE));
457+
SetClassLongPtr(m_hWnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_hbrBackground);
458+
InvalidateRect(m_hWnd, NULL, TRUE);
459+
}
460+
}
461+
436462
double GetZoom() const override
437463
{
438464
return m_zoom;
@@ -1486,7 +1512,7 @@ class CWebDiffWindow : public IWebDiffWindow
14861512
wcex.cbWndExtra = 0;
14871513
wcex.hInstance = hInstance;
14881514
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
1489-
wcex.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
1515+
wcex.hbrBackground = s_hbrBackground;
14901516
wcex.lpszClassName = L"WinWebDiffWindowClass";
14911517
return RegisterClassExW(&wcex);
14921518
}
@@ -1518,6 +1544,7 @@ class CWebDiffWindow : public IWebDiffWindow
15181544
int m_nPanes = 0;
15191545
HWND m_hWnd = nullptr;
15201546
HINSTANCE m_hInstance = nullptr;
1547+
HBRUSH s_hbrBackground = nullptr;
15211548
CWebWindow m_webWindow[3];
15221549
int m_nDraggingSplitter = -1;
15231550
bool m_bHorizontalSplit = false;
@@ -1535,6 +1562,7 @@ class CWebDiffWindow : public IWebDiffWindow
15351562
std::vector<DiffInfo> m_diffInfos;
15361563
DiffLocation m_diffLocation[3];
15371564
DiffOptions m_diffOptions{};
1565+
bool m_bDarkBackgroundEnabled;
15381566
bool m_bShowDifferences = true;
15391567
bool m_bShowWordDifferences = true;
15401568
bool m_bSynchronizeEvents = true;

src/WinWebDiffLib/WebToolWindow.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ class CWebToolWindow : public IWebToolWindow, IWebDiffEventHandler
450450

451451
void DrawDiffMap(HDC hdcMem, const RECT& rc)
452452
{
453-
FillSolidRect(hdcMem, { 0, 0, rc.right, rc.bottom }, RGB(255, 255, 255));
453+
const bool dark = m_pWebDiffWindow->IsDarkBackgroundEnabled();
454+
FillSolidRect(hdcMem, { 0, 0, rc.right, rc.bottom }, dark ? RGB(0, 0, 0) : RGB(255, 255, 255));
454455

455456
const int paneCount = m_pWebDiffWindow->GetPaneCount();
456457
if (!m_pWebDiffWindow || paneCount == 0)
@@ -489,6 +490,7 @@ class CWebToolWindow : public IWebToolWindow, IWebDiffEventHandler
489490
}
490491
}
491492
}
493+
HPEN hOldPen = SelectPen(hdcMem, GetStockPen(dark ? WHITE_PEN : BLACK_PEN));
492494
HBRUSH hOldBrush = SelectBrush(hdcMem, GetStockBrush(NULL_BRUSH));
493495
for (int i = 0; i < m_containerRects[pane].size(); ++i)
494496
{
@@ -504,10 +506,10 @@ class CWebToolWindow : public IWebToolWindow, IWebDiffEventHandler
504506
rcContainer.top += static_cast<int>(visibleArea.top * scaleY);
505507
rcContainer.bottom = static_cast<int>(rcContainer.top + visibleArea.height * scaleY);
506508
DrawTransparentRectangle(hdcMem,
507-
rcContainer.left, rcContainer.top, rcContainer.right, rcContainer.bottom,
508-
RGB(96, 96, 255), 64);
509+
rcContainer.left, rcContainer.top, rcContainer.right, rcContainer.bottom, RGB(96, 96, 255), 64);
509510

510511
SelectBrush(hdcMem, hOldBrush);
512+
SelectPen(hdcMem, hOldPen);
511513
}
512514
}
513515

src/WinWebDiffLib/WebWindow.hpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,26 @@ class CWebWindow
7272
return result;
7373
}
7474

75-
m_webviewController->put_ZoomFactor(zoom);
75+
auto webView2_13 = m_webview.try_query<ICoreWebView2_13>();
76+
if (webView2_13)
77+
{
78+
wil::com_ptr<ICoreWebView2Profile> webView2Profile;
79+
webView2_13->get_Profile(&webView2Profile);
80+
if (webView2Profile)
81+
{
82+
auto webView2Profile2 = webView2Profile.try_query<ICoreWebView2Profile2>();
83+
webView2Profile2->put_PreferredColorScheme(
84+
m_parent->m_bDarkBackgroundEnabled ? COREWEBVIEW2_PREFERRED_COLOR_SCHEME_DARK : COREWEBVIEW2_PREFERRED_COLOR_SCHEME_LIGHT);
85+
}
86+
}
7687

88+
wil::com_ptr<ICoreWebView2Controller2> controller2 = m_webviewController.query<ICoreWebView2Controller2>();
89+
if (controller2)
90+
{
91+
const COREWEBVIEW2_COLOR bg = { 255, 255, 255, 255 };
92+
controller2->put_DefaultBackgroundColor(bg);
93+
}
94+
m_webviewController->put_ZoomFactor(zoom);
7795
m_webviewController->add_AcceleratorKeyPressed(
7896
Callback<ICoreWebView2AcceleratorKeyPressedEventHandler>(
7997
[this](ICoreWebView2Controller* sender, ICoreWebView2AcceleratorKeyPressedEventArgs* args) {
@@ -268,12 +286,13 @@ class CWebWindow
268286
}
269287

270288
HRESULT Create(IWebDiffWindow* pDiffWindow, HINSTANCE hInstance, HWND hWndParent, const wchar_t* url, const wchar_t* userDataFolder,
271-
const SIZE& size, bool fitToWindow, double zoom, std::wstring& userAgent,
289+
const SIZE& size, bool fitToWindow, double zoom, bool darkBackgroundEnabled, std::wstring& userAgent,
272290
IWebDiffCallback* callback, std::function<void(WebDiffEvent::EVENT_TYPE, IUnknown*, IUnknown*)> eventHandler)
273291
{
274292
m_pDiffWindow = pDiffWindow;
275293
m_fitToWindow = fitToWindow;
276294
m_size = size;
295+
m_bDarkBackgroundEnabled = darkBackgroundEnabled;
277296
m_eventHandler = eventHandler;
278297
MyRegisterClass(hInstance);
279298
m_hWnd = CreateWindowExW(0, L"WinWebWindowClass", nullptr,
@@ -1357,6 +1376,20 @@ class CWebWindow
13571376
return m_webmessage;
13581377
}
13591378

1379+
bool IsDarkBackgroundEnabled() const
1380+
{
1381+
return m_bDarkBackgroundEnabled;
1382+
}
1383+
1384+
void SetDarkBackgroundEnabled(bool enabled)
1385+
{
1386+
m_bDarkBackgroundEnabled = enabled;
1387+
DeleteObject(s_hbrBackground);
1388+
s_hbrBackground = CreateSolidBrush(m_bDarkBackgroundEnabled ? RGB(40, 40, 60) : RGB(206, 215, 230));
1389+
SetClassLongPtr(m_hWebViewParent, GCLP_HBRBACKGROUND, (LONG_PTR)s_hbrBackground);
1390+
InvalidateRect(m_hWebViewParent, NULL, TRUE);
1391+
}
1392+
13601393
private:
13611394

13621395
HRESULT InitializeWebView(const wchar_t* url, double zoom, const std::wstring& userAgent, const wchar_t* userDataFolder, IWebDiffCallback* callback)
@@ -1661,7 +1694,7 @@ class CWebWindow
16611694
wcex.cbWndExtra = 0;
16621695
wcex.hInstance = hInstance;
16631696
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
1664-
wcex.hbrBackground = CreateSolidBrush(RGB(206, 215, 230));
1697+
wcex.hbrBackground = s_hbrBackground;
16651698
wcex.lpszClassName = L"WebViewParentClass";
16661699
}
16671700
return RegisterClassExW(&wcex) != 0;
@@ -2083,6 +2116,7 @@ class CWebWindow
20832116
HWND m_hWebViewParent = nullptr;
20842117
HFONT m_hToolbarFont = nullptr;
20852118
HFONT m_hEditFont = nullptr;
2119+
inline static HBRUSH s_hbrBackground = CreateSolidBrush(RGB(206, 215, 230));
20862120
WNDPROC m_oldTabCtrlWndProc = nullptr;
20872121
WNDPROC m_oldEditWndProc = nullptr;
20882122
TOOLINFO m_toolItem{};
@@ -2097,6 +2131,7 @@ class CWebWindow
20972131
std::wstring m_currentUrl;
20982132
std::wstring m_toolTipText = L"test";
20992133
bool m_showToolTip = false;
2134+
bool m_bDarkBackgroundEnabled = false;
21002135
std::wstring m_webmessage;
21012136
inline static const auto GetDpiForWindowFunc = []() {
21022137
HMODULE hUser32 = GetModuleHandle(L"user32.dll");

src/WinWebDiffLib/WinWebDiffLib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ struct IWebDiffWindow
198198
virtual void RaiseEvent(const WebDiffEvent& e) = 0;
199199
virtual LogCallback GetLogCallback() const = 0;
200200
virtual void SetLogCallback(LogCallback logCallback) = 0;
201+
virtual bool IsDarkBackgroundEnabled() const = 0;
202+
virtual void SetDarkBackgroundEnabled(bool enabled) = 0;
201203
};
202204

203205
struct IWebToolWindow

0 commit comments

Comments
 (0)