Skip to content

Commit 1471497

Browse files
committed
Add SetLogCallback() and GetLogCallback() functions
1 parent 0a7baad commit 1471497

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

src/WinWebDiffLib/WebDiffWindow.hpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class CWebDiffWindow : public IWebDiffWindow
8484
{
8585
std::wstring userDataFolder = GetUserDataFolderPath(i);
8686
ComPtr<IWebDiffCallback> callback2(callback);
87-
hr = m_webWindow[i].Create(m_hInstance, m_hWnd, urls[i], userDataFolder.c_str(),
87+
hr = m_webWindow[i].Create(this, m_hInstance, m_hWnd, urls[i], userDataFolder.c_str(),
8888
m_size, m_fitToWindow, m_zoom, m_userAgent, nullptr,
8989
[this, i, callback2](WebDiffEvent::EVENT_TYPE event, IUnknown* sender, IUnknown* args)
9090
{
@@ -580,6 +580,16 @@ class CWebDiffWindow : public IWebDiffWindow
580580
}
581581
}
582582

583+
LogCallback GetLogCallback() const
584+
{
585+
return m_logCallback;
586+
}
587+
588+
void SetLogCallback(LogCallback logCallback)
589+
{
590+
m_logCallback = logCallback;
591+
}
592+
583593
int GetDiffCount() const override
584594
{
585595
return static_cast<int>(m_diffInfos.size());
@@ -1543,4 +1553,5 @@ class CWebDiffWindow : public IWebDiffWindow
15431553
RGB(241, 226, 173), RGB(255, 170, 130), RGB(0, 0, 0),
15441554
RGB(255, 160, 160), RGB(200, 129, 108), RGB(0, 0, 0),
15451555
};
1556+
LogCallback m_logCallback;
15461557
};

src/WinWebDiffLib/WebWindow.hpp

+16-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class CWebWindow
5656
[this, url2, zoom, userAgent, args, deferral, callback2](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
5757
if (FAILED(result))
5858
{
59-
m_parent->SetToolTipText(L"Failed to create WebView2 controller");
59+
m_parent->SetErrorToolTipText(L"Failed to create WebView2 controller");
6060
m_parent->ShowToolTip(true);
6161
}
6262

@@ -267,10 +267,11 @@ class CWebWindow
267267
return m_hWnd;
268268
}
269269

270-
HRESULT Create(HINSTANCE hInstance, HWND hWndParent, const wchar_t* url, const wchar_t* userDataFolder,
270+
HRESULT Create(IWebDiffWindow* pDiffWindow, HINSTANCE hInstance, HWND hWndParent, const wchar_t* url, const wchar_t* userDataFolder,
271271
const SIZE& size, bool fitToWindow, double zoom, std::wstring& userAgent,
272272
IWebDiffCallback* callback, std::function<void(WebDiffEvent::EVENT_TYPE, IUnknown*, IUnknown*)> eventHandler)
273273
{
274+
m_pDiffWindow = pDiffWindow;
274275
m_fitToWindow = fitToWindow;
275276
m_size = size;
276277
m_eventHandler = eventHandler;
@@ -1049,7 +1050,7 @@ class CWebWindow
10491050
[this, callback2, msg](HRESULT errorCode, LPCWSTR returnObjectAsJson) -> HRESULT {
10501051
if (FAILED(errorCode))
10511052
{
1052-
SetToolTipText(*msg + returnObjectAsJson);
1053+
SetErrorToolTipText(*msg + returnObjectAsJson);
10531054
ShowToolTip(true, TOOLTIP_TIMEOUT);
10541055
}
10551056
if (callback2)
@@ -1069,7 +1070,7 @@ class CWebWindow
10691070
[this, callback2](HRESULT errorCode, LPCWSTR resultObjectAsJson) -> HRESULT {
10701071
if (FAILED(errorCode))
10711072
{
1072-
SetToolTipText(resultObjectAsJson);
1073+
SetErrorToolTipText(resultObjectAsJson);
10731074
ShowToolTip(true, TOOLTIP_TIMEOUT);
10741075
}
10751076
if (callback2)
@@ -1092,7 +1093,7 @@ class CWebWindow
10921093
[this, callback2](HRESULT errorCode, LPCWSTR resultObjectAsJson) -> HRESULT {
10931094
if (FAILED(errorCode))
10941095
{
1095-
SetToolTipText(resultObjectAsJson);
1096+
SetErrorToolTipText(resultObjectAsJson);
10961097
ShowToolTip(true, TOOLTIP_TIMEOUT);
10971098
}
10981099
if (callback2)
@@ -1107,7 +1108,7 @@ class CWebWindow
11071108
[this, callback2](HRESULT errorCode, LPCWSTR resultObjectAsJson) -> HRESULT {
11081109
if (FAILED(errorCode))
11091110
{
1110-
SetToolTipText(resultObjectAsJson);
1111+
SetErrorToolTipText(resultObjectAsJson);
11111112
ShowToolTip(true, TOOLTIP_TIMEOUT);
11121113
}
11131114
if (callback2)
@@ -1309,6 +1310,14 @@ class CWebWindow
13091310
return webView2Profile2->ClearBrowsingData(static_cast<COREWEBVIEW2_BROWSING_DATA_KINDS>(dataKinds), nullptr);
13101311
}
13111312

1313+
HRESULT SetErrorToolTipText(const std::wstring& text)
1314+
{
1315+
IWebDiffWindow::LogCallback logCallback = m_pDiffWindow->GetLogCallback();
1316+
if (logCallback)
1317+
logCallback(IWebDiffWindow::LogLevel::ERR, text.c_str());
1318+
return SetToolTipText(text);
1319+
}
1320+
13121321
HRESULT SetToolTipText(const std::wstring& text)
13131322
{
13141323
if (!m_hToolTip)
@@ -2089,5 +2098,6 @@ class CWebWindow
20892098
return reinterpret_cast<decltype(&::GetDpiForWindow)>(
20902099
::GetProcAddress(hUser32, "GetDpiForWindow"));
20912100
}();
2101+
IWebDiffWindow* m_pDiffWindow = nullptr;
20922102
};
20932103

src/WinWebDiffLib/WinWebDiffLib.h

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ struct IWebDiffWindow
6969
COMPARING,
7070
COMPARED,
7171
};
72+
enum class LogLevel : int { ERR, WARN, INFO };
73+
7274
struct DiffOptions
7375
{
7476
enum DiffAlgorithm {
@@ -115,6 +117,7 @@ struct IWebDiffWindow
115117
COLORREF clrSelWordDiffDeleted; /**< Selected word difference deleted color */
116118
COLORREF clrSelWordDiffText; /**< Selected word difference text color */
117119
};
120+
using LogCallback = void(*)(LogLevel level, const wchar_t *msg);
118121

119122
virtual bool IsWebView2Installed() const = 0;
120123
virtual bool DownloadWebView2() const = 0;
@@ -193,6 +196,8 @@ struct IWebDiffWindow
193196
virtual void SetSyncEventFlag(EventType event, bool flag) = 0;
194197
virtual CompareState GetCompareState() const = 0;
195198
virtual void RaiseEvent(const WebDiffEvent& e) = 0;
199+
virtual LogCallback GetLogCallback() const = 0;
200+
virtual void SetLogCallback(LogCallback logCallback) = 0;
196201
};
197202

198203
struct IWebToolWindow

0 commit comments

Comments
 (0)