forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlComponent.cpp
More file actions
437 lines (409 loc) · 16.4 KB
/
ControlComponent.cpp
File metadata and controls
437 lines (409 loc) · 16.4 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include "ControlComponent.h"
#include "ScenarioWebViewEventMonitor.h"
#include "App.h"
#include "CheckFailure.h"
using namespace Microsoft::WRL;
ControlComponent::ControlComponent(AppWindow* appWindow, Toolbar* toolbar)
: m_appWindow(appWindow), m_controller(appWindow->GetWebViewController()),
m_webView(appWindow->GetWebView()), m_toolbar(toolbar)
{
m_toolbar->SetItemEnabled(Toolbar::Item_AddressBar, true);
m_toolbar->SetItemEnabled(Toolbar::Item_GoButton, true);
// Register a handler for the NavigationStarting event.
// This handler just enables the Cancel button.
CHECK_FAILURE(m_webView->add_NavigationStarting(
Callback<ICoreWebView2NavigationStartingEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2NavigationStartingEventArgs* args)
-> HRESULT {
m_toolbar->SetItemEnabled(Toolbar::Item_CancelButton, true);
return S_OK;
})
.Get(),
&m_navigationStartingToken));
//! [SourceChanged]
// Register a handler for the SourceChanged event.
// This handler will read the webview's source URI and update
// the app's address bar.
CHECK_FAILURE(m_webView->add_SourceChanged(
Callback<ICoreWebView2SourceChangedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2SourceChangedEventArgs* args)
-> HRESULT {
wil::unique_cotaskmem_string uri;
sender->get_Source(&uri);
if (wcscmp(uri.get(), L"about:blank") == 0)
{
uri = wil::make_cotaskmem_string(L"");
}
SetWindowText(GetAddressBar(), uri.get());
return S_OK;
})
.Get(),
&m_sourceChangedToken));
//! [SourceChanged]
//! [HistoryChanged]
// Register a handler for the HistoryChanged event.
// Update the Back, Forward buttons.
CHECK_FAILURE(m_webView->add_HistoryChanged(
Callback<ICoreWebView2HistoryChangedEventHandler>(
[this](ICoreWebView2* sender, IUnknown* args) -> HRESULT {
BOOL canGoBack;
BOOL canGoForward;
sender->get_CanGoBack(&canGoBack);
sender->get_CanGoForward(&canGoForward);
m_toolbar->SetItemEnabled(Toolbar::Item_BackButton, canGoBack);
m_toolbar->SetItemEnabled(Toolbar::Item_ForwardButton, canGoForward);
return S_OK;
})
.Get(),
&m_historyChangedToken));
//! [HistoryChanged]
//! [NavigationCompleted]
// Register a handler for the NavigationCompleted event.
// Check whether the navigation succeeded, and if not, do something.
// Also update the Cancel buttons.
CHECK_FAILURE(m_webView->add_NavigationCompleted(
Callback<ICoreWebView2NavigationCompletedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2NavigationCompletedEventArgs* args)
-> HRESULT {
BOOL success;
CHECK_FAILURE(args->get_IsSuccess(&success));
if (!success)
{
COREWEBVIEW2_WEB_ERROR_STATUS webErrorStatus;
CHECK_FAILURE(args->get_WebErrorStatus(&webErrorStatus));
if (webErrorStatus == COREWEBVIEW2_WEB_ERROR_STATUS_DISCONNECTED)
{
// Do something here if you want to handle a specific error case.
// In most cases this isn't necessary, because the WebView will
// display its own error page automatically.
}
}
m_toolbar->SetItemEnabled(Toolbar::Item_CancelButton, false);
m_toolbar->SetItemEnabled(Toolbar::Item_ReloadButton, true);
return S_OK;
})
.Get(),
&m_navigationCompletedToken));
//! [NavigationCompleted]
//! [FrameNavigationCompleted]
// Register a handler for the FrameNavigationCompleted event.
// Check whether the navigation succeeded, and if not, do something.
CHECK_FAILURE(m_webView->add_FrameNavigationCompleted(
Callback<ICoreWebView2NavigationCompletedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2NavigationCompletedEventArgs* args)
-> HRESULT {
BOOL success;
CHECK_FAILURE(args->get_IsSuccess(&success));
if (!success)
{
COREWEBVIEW2_WEB_ERROR_STATUS webErrorStatus;
CHECK_FAILURE(args->get_WebErrorStatus(&webErrorStatus));
std::wstring error_msg = WebErrorStatusToString(webErrorStatus);
MessageBox(nullptr,
(std::wstring(L"IFrame navigation failed with the ") +
L"give in error " + error_msg).c_str(),
L"Navigation Failure", MB_OK);
if (webErrorStatus == COREWEBVIEW2_WEB_ERROR_STATUS_DISCONNECTED)
{
// Do something here if you want to handle a specific error case.
// In most cases this isn't necessary, because the WebView will
// display its own error page automatically.
}
}
return S_OK;
})
.Get(),
&m_frameNavigationCompletedToken));
//! [FrameNavigationCompleted]
//! [MoveFocusRequested]
// Register a handler for the MoveFocusRequested event.
// This event will be fired when the user tabs out of the webview.
// The handler will focus another window in the app, depending on which
// direction the focus is being shifted.
CHECK_FAILURE(m_controller->add_MoveFocusRequested(
Callback<ICoreWebView2MoveFocusRequestedEventHandler>(
[this](
ICoreWebView2Controller* sender,
ICoreWebView2MoveFocusRequestedEventArgs* args) -> HRESULT {
if (!g_autoTabHandle)
{
COREWEBVIEW2_MOVE_FOCUS_REASON reason;
CHECK_FAILURE(args->get_Reason(&reason));
if (reason == COREWEBVIEW2_MOVE_FOCUS_REASON_NEXT)
{
TabForwards(-1);
}
else if (reason == COREWEBVIEW2_MOVE_FOCUS_REASON_PREVIOUS)
{
TabBackwards(int(m_tabbableWindows.size()));
}
CHECK_FAILURE(args->put_Handled(TRUE));
}
return S_OK;
})
.Get(),
&m_moveFocusRequestedToken));
//! [MoveFocusRequested]
// Replace the window procs on some toolbar elements to customize their behavior
for (auto hwnd : m_toolbar->GetItems())
{
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
auto originalWndProc =
(WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)ChildWndProcStatic);
m_tabbableWindows.emplace_back(hwnd, originalWndProc);
}
//! [AcceleratorKeyPressed]
// Register a handler for the AcceleratorKeyPressed event.
CHECK_FAILURE(m_controller->add_AcceleratorKeyPressed(
Callback<ICoreWebView2AcceleratorKeyPressedEventHandler>(
[this](
ICoreWebView2Controller* sender,
ICoreWebView2AcceleratorKeyPressedEventArgs* args) -> HRESULT {
COREWEBVIEW2_KEY_EVENT_KIND kind;
CHECK_FAILURE(args->get_KeyEventKind(&kind));
// We only care about key down events.
if (kind == COREWEBVIEW2_KEY_EVENT_KIND_KEY_DOWN ||
kind == COREWEBVIEW2_KEY_EVENT_KIND_SYSTEM_KEY_DOWN)
{
UINT key;
CHECK_FAILURE(args->get_VirtualKey(&key));
// Check if the key is one we want to handle.
if (std::function<void()> action =
m_appWindow->GetAcceleratorKeyFunction(key))
{
// Keep the browser from handling this key, whether it's autorepeated or
// not.
CHECK_FAILURE(args->put_Handled(TRUE));
// Filter out autorepeated keys.
COREWEBVIEW2_PHYSICAL_KEY_STATUS status;
CHECK_FAILURE(args->get_PhysicalKeyStatus(&status));
if (!status.WasKeyDown)
{
// Perform the action asynchronously to avoid blocking the
// browser process's event queue.
m_appWindow->RunAsync(action);
}
}
}
return S_OK;
})
.Get(),
&m_acceleratorKeyPressedToken));
//! [AcceleratorKeyPressed]
}
bool ControlComponent::HandleWindowMessage(
HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* result)
{
if (message == WM_COMMAND)
{
switch (LOWORD(wParam))
{
case IDM_FOCUS_SET:
CHECK_FAILURE(m_controller->MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC));
return true;
case IDM_FOCUS_TAB_IN:
CHECK_FAILURE(m_controller->MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_NEXT));
return true;
case IDM_FOCUS_REVERSE_TAB_IN:
CHECK_FAILURE(m_controller->MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PREVIOUS));
return true;
case IDM_TOGGLE_TAB_HANDLING:
g_autoTabHandle = !g_autoTabHandle;
return true;
case IDE_ADDRESSBAR_GO:
if (HIWORD(wParam) == BN_CLICKED)
{
NavigateToAddressBar();
return true;
}
case IDE_BACK:
CHECK_FAILURE(m_webView->GoBack());
return true;
case IDE_FORWARD:
CHECK_FAILURE(m_webView->GoForward());
return true;
case IDE_ADDRESSBAR_RELOAD:
CHECK_FAILURE(m_webView->Reload());
return true;
case IDE_CANCEL:
CHECK_FAILURE(m_webView->Stop());
return true;
}
}
return false;
}
// We replace the address bar and go buttons' wndproc with this.
LRESULT CALLBACK
ControlComponent::ChildWndProcStatic(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (auto self = reinterpret_cast<ControlComponent*>(GetWindowLongPtr(hWnd, GWLP_USERDATA)))
{
LRESULT result = 0;
if (self->HandleChildWindowMessage(hWnd, message, wParam, lParam, &result))
{
return result;
}
// Continue with original window proc if we didn't handle this message
for (auto pair : self->m_tabbableWindows)
{
if (hWnd == pair.first)
{
return CallWindowProc(pair.second, hWnd, message, wParam, lParam);
}
}
// This should never be called on a window we don't know about.
FAIL_FAST();
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
bool ControlComponent::HandleChildWindowMessage(
HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* result)
{
// If not calling IsDialogMessage to handle tab traversal automatically,
// detect tab traversal and cycle focus through address bar, go button, and
// elements in WebView.
if (message == WM_KEYDOWN)
{
//! [MoveFocus1]
if (wParam == VK_TAB)
{
// Find out if the window is one we've customized for tab handling
for (size_t i = 0; i < m_tabbableWindows.size(); i++)
{
if (m_tabbableWindows[i].first == hWnd)
{
if (GetKeyState(VK_SHIFT) < 0)
{
TabBackwards(i);
}
else
{
TabForwards(i);
}
return true;
}
}
}
//! [MoveFocus1]
else if ((wParam == VK_RETURN) && (hWnd == GetAddressBar()))
{
// Handle pressing Enter in address bar
NavigateToAddressBar();
return true;
}
else
{
// If bit 30 is set, it means the WM_KEYDOWN message is autorepeated.
// We want to ignore it in that case.
if (!(lParam & (1 << 30)))
{
if (auto action = m_appWindow->GetAcceleratorKeyFunction((UINT)wParam))
{
action();
return true;
}
}
}
}
else if ((message == WM_CHAR) && ((wParam == VK_TAB) || (wParam == VK_RETURN)))
{
// Ignore Tab and return char messages to avoid the ding sound.
return true;
}
else if ((message == WM_GETDLGCODE) && (wParam == VK_RETURN))
{
// When calling IsDialogMessage to handle tab traversal automatically, tell
// the system that we want to handle the VK_RETURN. This let's the app to
// Navigate the WebView when Enter is pressed in the address bar.
*result = DLGC_WANTALLKEYS;
return true;
}
return false;
}
//! [Navigate]
void ControlComponent::NavigateToAddressBar()
{
int length = GetWindowTextLength(GetAddressBar());
std::wstring uri(length, 0);
PWSTR buffer = const_cast<PWSTR>(uri.data());
GetWindowText(GetAddressBar(), buffer, length + 1);
HRESULT hr = m_webView->Navigate(uri.c_str());
if (hr == E_INVALIDARG)
{
// An invalid URI was provided.
if (uri.find(L' ') == std::wstring::npos
&& uri.find(L'.') != std::wstring::npos)
{
// If it contains a dot and no spaces, try tacking http:// on the front.
hr = m_webView->Navigate((L"http://" + uri).c_str());
}
else
{
// Otherwise treat it as a web search. We aren't bothering to escape
// URL syntax characters such as & and #
hr = m_webView->Navigate((L"https://bing.com/search?q=" + uri).c_str());
}
}
if (hr != E_INVALIDARG) {
CHECK_FAILURE(hr);
}
}
//! [Navigate]
//! [MoveFocus2]
void ControlComponent::TabForwards(int currentIndex)
{
// Find first enabled window after the active one
for (size_t i = currentIndex + 1; i < m_tabbableWindows.size(); i++)
{
HWND hwnd = m_tabbableWindows.at(i).first;
if (IsWindowEnabled(hwnd))
{
SetFocus(hwnd);
return;
}
}
// If this is the last enabled window, tab forwards into the WebView.
m_controller->MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_NEXT);
}
void ControlComponent::TabBackwards(int currentIndex)
{
// Find first enabled window before the active one
for (int i = currentIndex - 1; i >= 0; i--)
{
HWND hwnd = m_tabbableWindows.at(i).first;
if (IsWindowEnabled(hwnd))
{
SetFocus(hwnd);
return;
}
}
// If this is the last enabled window, tab forwards into the WebView.
CHECK_FAILURE(m_controller->MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PREVIOUS));
}
//! [MoveFocus2]
ControlComponent::~ControlComponent()
{
m_webView->remove_NavigationStarting(m_navigationStartingToken);
m_webView->remove_SourceChanged(m_sourceChangedToken);
m_webView->remove_HistoryChanged(m_historyChangedToken);
m_webView->remove_NavigationCompleted(m_navigationCompletedToken);
m_webView->remove_FrameNavigationCompleted(m_frameNavigationCompletedToken);
m_controller->remove_MoveFocusRequested(m_moveFocusRequestedToken);
m_controller->remove_AcceleratorKeyPressed(m_acceleratorKeyPressedToken);
// Undo our modifications to the toolbar elements
for (auto pair : m_tabbableWindows)
{
SetWindowLongPtr(pair.first, GWLP_USERDATA, (LONG_PTR)nullptr);
SetWindowLongPtr(pair.first, GWLP_WNDPROC, (LONG_PTR)pair.second);
}
SetWindowText(GetAddressBar(), L"");
m_toolbar->DisableAllItems();
}
HWND ControlComponent::GetAddressBar()
{
return m_toolbar->GetItem(Toolbar::Item_AddressBar);
}