-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathWebView2Control.h
More file actions
371 lines (294 loc) · 10.9 KB
/
WebView2Control.h
File metadata and controls
371 lines (294 loc) · 10.9 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#ifndef UI_WEBVIEW2_WEBVIEW2_CONTROL_H_
#define UI_WEBVIEW2_WEBVIEW2_CONTROL_H_
#include "duilib/Core/Control.h"
#if defined (DUILIB_BUILD_FOR_WIN) && defined (DUILIB_BUILD_FOR_WEBVIEW2)
#include "ComPtr.h"
#include <combaseapi.h>
#include "duilib/third_party/Microsoft.Web.WebView2/build/native/include/WebView2.h"
#include <functional>
#include <memory>
namespace ui {
class Window;
class IBitmap;
/** WebView2控件的C++封装类
*/
class WebView2Control: public Control
{
typedef Control BaseClass;
public:
/** 导航状态枚举, 表示WebView2的导航状态
*/
enum class NavigationState
{
Started, /**< 导航开始 */
Completed, /**< 导航完成 */
Failed /**< 导航失败 */
};
/** 初始化完成回调函数类型
* @param result 初始化结果(S_OK表示成功)
*/
using InitializeCompletedCallback = std::function<void(HRESULT result)>;
/** Web消息接收回调函数类型
* @param message 接收到的消息内容
*/
using WebMessageReceivedCallback = std::function<void(const DString& url,
const DString& webMessageAsJson,
const DString& webMessageAsString)>;
/** 导航状态变化回调函数类型
* @param state 新的导航状态
* @param errorCode 错误码(如果导航失败)
*/
using NavigationStateChangedCallback = std::function<void(NavigationState state, HRESULT errorCode)>;
/** 文档标题变化回调函数类型
* @param title 新的文档标题
*/
using DocumentTitleChangedCallback = std::function<void(const DString& title)>;
/** 源URL变化回调函数类型
* @param uri 新的源URL
*/
using SourceChangedCallback = std::function<void(const DString& uri)>;
/** 新窗口请求回调函数类型
* @param sourceUrl 源的URL
* @param sourceFrame 源框架名称
* @param targetUrl 请求的URL
* @param targetFrame 目标框架名称
* @param bUserInitiated 是否由用户触发的弹窗
* @return 返回true表示允许创建弹窗页面,但新的页面在当前页面中导航,不会弹出新窗口;返回false表示拦截页面弹窗页面,由回调函数内托管新页面的显示逻辑
*/
using NewWindowRequestedCallback = std::function<bool(const DString& sourceUrl, const DString& sourceFrame,
const DString& targetUrl, const DString& targetFrame,
bool bUserInitiated)>;
/** 导航历史变化事件回调函数类型
*/
using HistoryChangedCallback = std::function<void()>;
/** 页面缩放比变化事件回调函数类型
* @param [in] zoomFactor 当前的缩放比例
*/
using ZoomFactorChangedCallback = std::function<void(double zoomFactor)>;
/** 网站图标变化事件回调函数类型
* @param [in] nWidth 图片宽度
* @param [in] nHeight 图片高度
* @param [in] imageData 网站图标的图片数据
*/
using FavIconChangedCallback = std::function<void(int32_t nWidth, int32_t nHeight, const std::vector<uint8_t>& imageData)>;
public:
/** 构造函数
*/
explicit WebView2Control(Window* pWindow);
/** 析构函数
*/
virtual ~WebView2Control() override;
/** 异步初始化WebView
* @param userDataFolder 用户数据文件夹路径(可选)
* @param callback 初始化完成回调函数(可选)
*/
bool InitializeAsync(const DString& userDataFolder = _T(""),
InitializeCompletedCallback callback = nullptr);
/** 检查WebView是否正在初始化
* @return 是否正在初始化
*/
bool IsInitializing() const;
/** 检查WebView是否已完成初始化
* @return 是否已初始化
*/
bool IsInitialized() const;
/** 导航到指定URL(可能是异步完成)
* @param url 要导航的URL
*/
bool Navigate(const DString& url);
/** 导航到上一页
*/
bool NavigateBack();
/** 导航到下一页
*/
bool NavigateForward();
/** 刷新当前页面
*/
bool Refresh();
/** 停止加载
* @return HRESULT错误码
*/
bool Stop();
/** 执行JavaScript脚本
* @param script 要执行的JavaScript脚本
* @param callback 执行完成后的回调函数(可选)
*/
bool ExecuteScript(const DString& script, std::function<void(const DString& result, HRESULT hr)> callback = nullptr);
/** 以JSON格式发送Web消息
* @param json 要发送的JSON字符串
*/
bool PostWebMessageAsJson(const DString& json);
/** 以字符串格式发送Web消息
* @param message 要发送的消息字符串
*/
bool PostWebMessageAsString(const DString& message);
/** 设置User-Agent
* @param userAgent 要设置的User-Agent字符串
*/
bool SetUserAgent(const DString& userAgent);
/** 获取User-Agent
*/
DString GetUserAgent() const;
/** 设置缩放因子
* @param zoomFactor 缩放因子
*/
bool SetZoomFactor(double zoomFactor);
/** 获取缩放因子
*/
double GetZoomFactor() const;
/** 设置JavaScript是否启用
*/
bool SetScriptEnabled(bool enabled);
bool IsScriptEnabled() const;
/** 设置Web消息是否启用
*/
bool SetWebMessageEnabled(bool enabled);
bool IsWebMessageEnabled() const;
/** 是否启用默认脚本对话框
*/
bool SetAreDefaultScriptDialogsEnabled(bool enabled);
bool AreDefaultScriptDialogsEnabled() const;
/** 是否允许默认的右键菜单
*/
bool SetAreDefaultContextMenusEnabled(bool enabled);
bool AreDefaultContextMenusEnabled() const;
/** 是否禁用缩放控制
*/
bool SetZoomControlEnabled(bool enabled);
bool IsZoomControlEnabled() const;
/** 设置Web消息接收回调函数
* @param callback 回调函数
*/
bool SetWebMessageReceivedCallback(WebMessageReceivedCallback callback);
/** 设置导航状态变化回调函数
* @param callback 回调函数
*/
bool SetNavigationStateChangedCallback(NavigationStateChangedCallback callback);
/** 设置文档标题变化回调函数
* @param callback 回调函数
*/
bool SetDocumentTitleChangedCallback(DocumentTitleChangedCallback callback);
/** 设置源URL变化回调函数
* @param callback 回调函数
*/
bool SetSourceChangedCallback(SourceChangedCallback callback);
/** 设置新窗口请求回调函数
* @param callback 回调函数
*/
bool SetNewWindowRequestedCallback(NewWindowRequestedCallback callback);
/** 设置导航历史变化回调函数
* @param callback 回调函数
*/
bool SetHistoryChangedCallback(HistoryChangedCallback callback);
/** 设置页面缩放比变化事件回调函数
*/
bool SetZoomFactorChangedCallback(ZoomFactorChangedCallback callback);
/** 设置网站图标变化事件回调函数类型
*/
void SetFavIconChangedCallback(FavIconChangedCallback callback);
/** 捕获当前页面的预览图(异步完成),保存为PNG或者JPG格式
* @param filePath 保存预览图的文件路径,根据保存的图片文件名后缀自动判断保存的格式
* @param callback 操作完成回调函数(可选)
*/
bool CapturePreview(const DString& filePath,
std::function<void(const DString& filePath, HRESULT hr)> callback = nullptr);
/** 获取当前URL
* @return 当前URL
*/
DString GetUrl() const;
/** 获取当前文档标题
* @return 当前文档标题
*/
DString GetTitle() const;
/** 是否正在导航中
*/
bool IsNavigating() const;
/** 检查是否可以导航到上一页
* @return 是否可以导航到上一页
*/
bool CanGoBack() const;
/** 检查是否可以导航到下一页
* @return 是否可以导航到下一页
*/
bool CanGoForward() const;
/** 获取最后一次操作的错误码
*/
HRESULT GetLastErrorCode() const;
public:
/** 设置是否启用开发者工具
*/
void SetAreDevToolsEnabled(bool bAreDevToolsEnabled);
/** 获取是否启用开发者工具
*/
bool AreDevToolsEnabled() const;
/** 打开开发者工具
*/
bool OpenDevToolsWindow();
public:
/** 获取ICoreWebView2Environment接口
*/
ui::ComPtr<ICoreWebView2Environment> GetWebView2Environment() const;
/** 获取ICoreWebView2Controller接口
*/
ui::ComPtr<ICoreWebView2Controller> GetWebView2Controller() const;
/** 获取ICoreWebView2接口
*/
ui::ComPtr<ICoreWebView2> GetWebView2() const;
public:
// 控件类型相关的属性
virtual DString GetType() const override;
virtual void SetAttribute(const DString& strName, const DString& strValue) override;
virtual void OnInit() override;
virtual void SetPos(UiRect rc) override;
virtual bool OnSetFocus(const EventArgs& msg) override;
virtual bool OnKillFocus(const EventArgs& msg) override;
virtual void SetWindow(Window* pWindow) override;
/** 设置是否允许F12快捷键(开发者工具)
*/
void SetEnableF12(bool bEnableF12);
/** 是否允许F12快捷键(开发者工具)
*/
bool IsEnableF12() const;
/** 设置是否允许F11快捷键(页面全屏/页面退出全屏)
*/
void SetEnableF11(bool bEnableF11);
/** 是否允许F11快捷键(页面全屏/页面退出全屏)
*/
bool IsEnableF11() const;
/** 设置初始加载的URL(仅在控件初始化前调用有效)
*/
void SetInitURL(const DString& url);
/** 获取初始加载的URL
*/
DString GetInitURL() const;
/** 设置初始加载的URL是否为本地文件
*/
void SetInitUrlIsLocalFile(bool bUrlIsLocalFile);
/** 获取初始加载的URL是否为本地文件
*/
bool IsInitUrlIsLocalFile() const;
/** 下载网站图标(前提条件:已经调用过SetFavIconChangedCallback设置了回调)
*/
bool DownloadFavIconImage();
/** 将网页保存为一张图片, 图片大小与控件大小相同
*/
std::shared_ptr<IBitmap> MakeImageSnapshot();
protected:
/** 设置可见状态事件
* @param [in] bChanged true表示状态发生变化,false表示状态未发生变化
*/
virtual void OnSetVisible(bool bChanged) override;
private:
// PIMPL实现
class Impl;
std::unique_ptr<Impl> m_pImpl;
/** 初始化加载的网址
*/
UiString m_initUrl;
/** 设置初始加载的URL是否为本地文件
*/
bool m_bUrlIsLocalFile;
};
} //namespace ui
#endif //DUILIB_BUILD_FOR_WEBVIEW2
#endif //UI_WEBVIEW2_WEBVIEW2_CONTROL_H_