Repository navigation
Expand file tree
/
Copy pathWebView2View.cpp
More file actions
164 lines (140 loc) · 5.69 KB
/
Copy pathWebView2View.cpp
File metadata and controls
164 lines (140 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "WebView2View.h"
#include <QQuickWindow>
#include <QDebug>
#include <windowsx.h>
#include <QStandardPaths>
WebView2View::WebView2View() {
setFlag(ItemHasContents, false);
setVisible(true);
}
WebView2View::~WebView2View() {
if (m_controller) {
m_controller->Close();
}
}
void WebView2View::componentComplete() {
QQuickItem::componentComplete();
initializeWebView();
}
void WebView2View::initializeWebView() {
if (!window()) return;
HWND parent = (HWND)window()->winId();
RECT bounds;
GetClientRect(parent, &bounds);
QString userData = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/wxfilehelperclientData";
std::wstring userDataW = std::wstring(reinterpret_cast<const wchar_t*>(userData.utf16()));;
CreateCoreWebView2EnvironmentWithOptions(
nullptr, userDataW.c_str(), nullptr,
Microsoft::WRL::Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
[this, parent, bounds](HRESULT result, ICoreWebView2Environment* env) -> HRESULT {
if (FAILED(result)) {
qWarning() << "Failed to create WebView2 environment:" << result;
return result;
}
env->CreateCoreWebView2Controller(parent,
Microsoft::WRL::Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
[this, bounds](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
if (FAILED(result)) {
qWarning() << "Failed to create WebView2 controller:" << result;
return result;
}
m_controller = controller;
m_controller->get_CoreWebView2(&m_webview);
if (m_webview) {
m_webview->add_NavigationCompleted(
Microsoft::WRL::Callback<ICoreWebView2NavigationCompletedEventHandler>(
[this](ICoreWebView2*, ICoreWebView2NavigationCompletedEventArgs* args) -> HRESULT {
BOOL success;
args->get_IsSuccess(&success);
emit loadingChanged(success);
return S_OK;
}).Get(),
nullptr);
}
resizeWebView();
if (!m_url.isEmpty())
m_webview->Navigate(m_url.toStdWString().c_str());
return S_OK;
}).Get());
return S_OK;
}).Get());
}
void WebView2View::resizeWebView() {
if (!m_controller) return;
QPointF topLeft = mapToItem(window()->contentItem(), QPointF(0,0));
qreal dpr = window()->devicePixelRatio();
LONG left = qRound(topLeft.x() * dpr);
LONG top = qRound(topLeft.y() * dpr);
LONG right = qRound((topLeft.x() + width()) * dpr);
LONG bottom = qRound((topLeft.y() + height()) * dpr);
RECT bounds{ left, top, right, bottom };
HRESULT hr = m_controller->put_Bounds(bounds);
if (FAILED(hr)) qWarning() << "put_Bounds failed:" << hr;
m_controller->put_IsVisible(isVisible);
}
void WebView2View::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) {
QQuickItem::geometryChange(newGeometry, oldGeometry);
resizeWebView();
}
void WebView2View::releaseResources() {
if (m_controller)
m_controller->Close();
m_controller = nullptr;
m_webview = nullptr;
}
void WebView2View::setUrl(const QString &url) {
if (url == m_url) return;
m_url = url;
if (m_webview)
m_webview->Navigate(url.toStdWString().c_str());
emit urlChanged();
}
void WebView2View::runJavaScript(const QString &script) {
if (!m_webview) {
qWarning() << "WebView2 not initialized. Cannot execute script.";
return;
}
HRESULT hr = m_webview->ExecuteScript(
script.toStdWString().c_str(),
Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
[script](HRESULT errorCode, LPCWSTR resultObjectAsJson) -> HRESULT {
if (FAILED(errorCode)) {
qWarning().noquote()
<< QStringLiteral("runJavaScript failed (HRESULT=0x%1) executing script: %2")
.arg(QString::number(errorCode, 16).rightJustified(8, '0'))
.arg(script);
} else if (resultObjectAsJson) {
QString result = QString::fromWCharArray(resultObjectAsJson);
if (result.contains("Error") || result.contains("Exception")) {
qWarning().noquote()
<< QStringLiteral("runJavaScript returned error result: %1")
.arg(result);
}
}
return S_OK;
}).Get());
if (FAILED(hr)) {
qWarning().noquote()
<< QStringLiteral("ExecuteScript() call failed immediately, HRESULT=0x%1")
.arg(QString::number(hr, 16).rightJustified(8, '0'));
}
}
void WebView2View::resetZoom()
{
if (m_controller)
m_controller->put_ZoomFactor(1.0);
}
void WebView2View::show()
{
if (m_controller) {
m_controller->put_IsVisible(TRUE);
isVisible = true;
}
}
void WebView2View::hide()
{
if (m_controller) {
m_controller->put_IsVisible(FALSE);
isVisible = true;
}
}