Skip to content

Commit ed196a9

Browse files
mrsteamfistChristopher Wang
andauthored
User/chwan/update api (#12)
* Update enumeration due to api change * Updated the package and fixed few missed instances * Updated the readme file too with the api changes * Added the changes from details ot options in readme Co-authored-by: Christopher Wang <[email protected]>
1 parent 9501cf6 commit ed196a9

File tree

7 files changed

+59
-59
lines changed

7 files changed

+59
-59
lines changed

BrowserWindow.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -236,24 +236,24 @@ HRESULT BrowserWindow::InitUIWebViews()
236236

237237
HRESULT BrowserWindow::CreateBrowserControlsWebView()
238238
{
239-
return m_uiEnv->CreateCoreWebView2Host(m_hWnd, Callback<ICoreWebView2CreateCoreWebView2HostCompletedHandler>(
240-
[this](HRESULT result, ICoreWebView2Host* host) -> HRESULT
239+
return m_uiEnv->CreateCoreWebView2Controller(m_hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
240+
[this](HRESULT result, ICoreWebView2Controller* host) -> HRESULT
241241
{
242242
if (!SUCCEEDED(result))
243243
{
244244
OutputDebugString(L"Controls WebView creation failed\n");
245245
return result;
246246
}
247247
// WebView created
248-
m_controlsHost = host;
249-
CheckFailure(m_controlsHost->get_CoreWebView2(&m_controlsWebView), L"");
248+
m_controlsController = host;
249+
CheckFailure(m_controlsController->get_CoreWebView2(&m_controlsWebView), L"");
250250

251251
wil::com_ptr<ICoreWebView2Settings> settings;
252252
RETURN_IF_FAILED(m_controlsWebView->get_Settings(&settings));
253253
RETURN_IF_FAILED(settings->put_AreDevToolsEnabled(FALSE));
254254

255-
RETURN_IF_FAILED(m_controlsHost->add_ZoomFactorChanged(Callback<ICoreWebView2ZoomFactorChangedEventHandler>(
256-
[](ICoreWebView2Host* host, IUnknown* args) -> HRESULT
255+
RETURN_IF_FAILED(m_controlsController->add_ZoomFactorChanged(Callback<ICoreWebView2ZoomFactorChangedEventHandler>(
256+
[](ICoreWebView2Controller* host, IUnknown* args) -> HRESULT
257257
{
258258
host->put_ZoomFactor(1.0);
259259
return S_OK;
@@ -272,37 +272,37 @@ HRESULT BrowserWindow::CreateBrowserControlsWebView()
272272

273273
HRESULT BrowserWindow::CreateBrowserOptionsWebView()
274274
{
275-
return m_uiEnv->CreateCoreWebView2Host(m_hWnd, Callback<ICoreWebView2CreateCoreWebView2HostCompletedHandler>(
276-
[this](HRESULT result, ICoreWebView2Host* host) -> HRESULT
275+
return m_uiEnv->CreateCoreWebView2Controller(m_hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
276+
[this](HRESULT result, ICoreWebView2Controller* host) -> HRESULT
277277
{
278278
if (!SUCCEEDED(result))
279279
{
280280
OutputDebugString(L"Options WebView creation failed\n");
281281
return result;
282282
}
283283
// WebView created
284-
m_optionsHost = host;
285-
CheckFailure(m_optionsHost->get_CoreWebView2(&m_optionsWebView), L"");
284+
m_optionsController = host;
285+
CheckFailure(m_optionsController->get_CoreWebView2(&m_optionsWebView), L"");
286286

287287
wil::com_ptr<ICoreWebView2Settings> settings;
288288
RETURN_IF_FAILED(m_optionsWebView->get_Settings(&settings));
289289
RETURN_IF_FAILED(settings->put_AreDevToolsEnabled(FALSE));
290290

291-
RETURN_IF_FAILED(m_optionsHost->add_ZoomFactorChanged(Callback<ICoreWebView2ZoomFactorChangedEventHandler>(
292-
[](ICoreWebView2Host* host, IUnknown* args) -> HRESULT
291+
RETURN_IF_FAILED(m_optionsController->add_ZoomFactorChanged(Callback<ICoreWebView2ZoomFactorChangedEventHandler>(
292+
[](ICoreWebView2Controller* host, IUnknown* args) -> HRESULT
293293
{
294294
host->put_ZoomFactor(1.0);
295295
return S_OK;
296296
}
297297
).Get(), &m_optionsZoomToken));
298298

299299
// Hide by default
300-
RETURN_IF_FAILED(m_optionsHost->put_IsVisible(FALSE));
300+
RETURN_IF_FAILED(m_optionsController->put_IsVisible(FALSE));
301301
RETURN_IF_FAILED(m_optionsWebView->add_WebMessageReceived(m_uiMessageBroker.Get(), &m_optionsUIMessageBrokerToken));
302302

303303
// Hide menu when focus is lost
304-
RETURN_IF_FAILED(m_optionsHost->add_LostFocus(Callback<ICoreWebView2FocusChangedEventHandler>(
305-
[this](ICoreWebView2Host* sender, IUnknown* args) -> HRESULT
304+
RETURN_IF_FAILED(m_optionsController->add_LostFocus(Callback<ICoreWebView2FocusChangedEventHandler>(
305+
[this](ICoreWebView2Controller* sender, IUnknown* args) -> HRESULT
306306
{
307307
web::json::value jsonObj = web::json::value::parse(L"{}");
308308
jsonObj[L"message"] = web::json::value(MG_OPTIONS_LOST_FOCUS);
@@ -363,7 +363,7 @@ void BrowserWindow::SetUIMessageBroker()
363363
}
364364
else
365365
{
366-
m_tabs.at(id)->m_contentHost->Close();
366+
m_tabs.at(id)->m_contentController->Close();
367367
it->second = std::move(newTab);
368368
}
369369
}
@@ -428,7 +428,7 @@ void BrowserWindow::SetUIMessageBroker()
428428
case MG_CLOSE_TAB:
429429
{
430430
size_t id = args.at(L"tabId").as_number().to_uint32();
431-
m_tabs.at(id)->m_contentHost->Close();
431+
m_tabs.at(id)->m_contentController->Close();
432432
m_tabs.erase(id);
433433
}
434434
break;
@@ -439,18 +439,18 @@ void BrowserWindow::SetUIMessageBroker()
439439
break;
440440
case MG_SHOW_OPTIONS:
441441
{
442-
CheckFailure(m_optionsHost->put_IsVisible(TRUE), L"");
443-
m_optionsHost->MoveFocus(CORE_WEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC);
442+
CheckFailure(m_optionsController->put_IsVisible(TRUE), L"");
443+
m_optionsController->MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC);
444444
}
445445
break;
446446
case MG_HIDE_OPTIONS:
447447
{
448-
CheckFailure(m_optionsHost->put_IsVisible(FALSE), L"Something went wrong when trying to close the options dropdown.");
448+
CheckFailure(m_optionsController->put_IsVisible(FALSE), L"Something went wrong when trying to close the options dropdown.");
449449
}
450450
break;
451451
case MG_OPTION_SELECTED:
452452
{
453-
m_tabs.at(m_activeTabId)->m_contentHost->MoveFocus(CORE_WEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC);
453+
m_tabs.at(m_activeTabId)->m_contentController->MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC);
454454
}
455455
break;
456456
case MG_GET_FAVORITES:
@@ -480,12 +480,12 @@ HRESULT BrowserWindow::SwitchToTab(size_t tabId)
480480
size_t previousActiveTab = m_activeTabId;
481481

482482
RETURN_IF_FAILED(m_tabs.at(tabId)->ResizeWebView());
483-
RETURN_IF_FAILED(m_tabs.at(tabId)->m_contentHost->put_IsVisible(TRUE));
483+
RETURN_IF_FAILED(m_tabs.at(tabId)->m_contentController->put_IsVisible(TRUE));
484484
m_activeTabId = tabId;
485485

486486
if (previousActiveTab != INVALID_TAB_ID && previousActiveTab != m_activeTabId)
487487
{
488-
RETURN_IF_FAILED(m_tabs.at(previousActiveTab)->m_contentHost->put_IsVisible(FALSE));
488+
RETURN_IF_FAILED(m_tabs.at(previousActiveTab)->m_contentController->put_IsVisible(FALSE));
489489
}
490490

491491
return S_OK;
@@ -824,7 +824,7 @@ HRESULT BrowserWindow::ResizeUIWebViews()
824824
bounds.bottom = bounds.top + GetDPIAwareBound(c_uiBarHeight);
825825
bounds.bottom += 1;
826826

827-
RETURN_IF_FAILED(m_controlsHost->put_Bounds(bounds));
827+
RETURN_IF_FAILED(m_controlsController->put_Bounds(bounds));
828828
}
829829

830830
if (m_optionsWebView != nullptr)
@@ -835,7 +835,7 @@ HRESULT BrowserWindow::ResizeUIWebViews()
835835
bounds.bottom = bounds.top + GetDPIAwareBound(c_optionsDropdownHeight);
836836
bounds.left = bounds.right - GetDPIAwareBound(c_optionsDropdownWidth);
837837

838-
RETURN_IF_FAILED(m_optionsHost->put_Bounds(bounds));
838+
RETURN_IF_FAILED(m_optionsController->put_Bounds(bounds));
839839
}
840840

841841
// Workaround for black controls WebView issue in Windows 7

BrowserWindow.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class BrowserWindow
4242

4343
Microsoft::WRL::ComPtr<ICoreWebView2Environment> m_uiEnv;
4444
Microsoft::WRL::ComPtr<ICoreWebView2Environment> m_contentEnv;
45-
Microsoft::WRL::ComPtr<ICoreWebView2Host> m_controlsHost;
46-
Microsoft::WRL::ComPtr<ICoreWebView2Host> m_optionsHost;
45+
Microsoft::WRL::ComPtr<ICoreWebView2Controller> m_controlsController;
46+
Microsoft::WRL::ComPtr<ICoreWebView2Controller> m_optionsController;
4747
Microsoft::WRL::ComPtr<ICoreWebView2> m_controlsWebView;
4848
Microsoft::WRL::ComPtr<ICoreWebView2> m_optionsWebView;
4949
std::map<size_t,std::unique_ptr<Tab>> m_tabs;

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ WebView2Browser makes use of a handful of the APIs available in WebView2. For th
8686

8787
API | Feature(s)
8888
:--- | :---
89-
CreateCoreWebView2EnvironmentWithDetails | Used to create the environments for UI and content WebViews. Different user data directories are passed to isolate UI from web content. |
89+
CreateCoreWebView2EnvironmentWithOptions | Used to create the environments for UI and content WebViews. Different user data directories are passed to isolate UI from web content. |
9090
ICoreWebView2 | There are several WebViews in WebView2Browser and most features make use of members in this interface, the table below shows how they're used.
9191
ICoreWebView2DevToolsProtocolEventReceivedEventHandler | Used along with add_DevToolsProtocolEventReceived to listen for CDP security events to update the lock icon in the browser UI. |
9292
ICoreWebView2DevToolsProtocolEventReceiver | Used along with add_DevToolsProtocolEventReceived to listen for CDP security events to update the lock icon in the browser UI. |
9393
ICoreWebView2ExecuteScriptCompletedHandler | Used along with ExecuteScript to get the title and favicon from the visited page. |
9494
ICoreWebView2FocusChangedEventHandler | Used along with add_LostFocus to hide the browser options dropdown when it loses focus.
9595
ICoreWebView2HistoryChangedEventHandler | Used along with add_HistoryChanged to udpate the navigation buttons in the browser UI. |
96-
ICoreWebView2Host | There are several WebViewHosts in WebView2Browser and we fetch the associated WebViews from them.
96+
ICoreWebView2Controller | There are several WebViewControllers in WebView2Browser and we fetch the associated WebViews from them.
9797
ICoreWebView2NavigationCompletedEventHandler | Used along with add_NavigationCompleted to udpate the reload button in the browser UI.
9898
ICoreWebView2Settings | Used to disable DevTools in the browser UI.
9999
ICoreWebView2SourceChangedEventHandler | Used along with add_SourceChanged to udpate the address bar in the browser UI. |
@@ -110,9 +110,9 @@ PostWebMessageAsJson | Used to communicate WebViews. All messages use JSON to pa
110110
add_WebMessageReceived | Used to handle web messages posted to the WebView.
111111
CallDevToolsProtocolMethod | Used to enable listening for security events, which will notify of security status changes in a document.
112112

113-
ICoreWebView2Host API | Feature(s)
113+
ICoreWebView2Controller API | Feature(s)
114114
:--- | :---
115-
get_CoreWebView2 | Used to get the CoreWebView2 associated with this CoreWebView2Host.
115+
get_CoreWebView2 | Used to get the CoreWebView2 associated with this CoreWebView2Controller.
116116
add_LostFocus | Used to hide the options dropdown when the user clicks away of it.
117117
<br />
118118

@@ -133,7 +133,7 @@ The sections below describe how some of the features in WebView2Browser were imp
133133

134134
## The basics
135135
### Set up the environment, create a WebView
136-
WebView2 allows you to host web content in your Windows app. It exposes the globals [CreateCoreWebView2Environment](https://docs.microsoft.com/microsoft-edge/hosting/webview2/reference/webview2.idl#createcorewebview2environment) and [CreateCoreWebView2EnvironmentWithDetails](https://docs.microsoft.com/microsoft-edge/hosting/webview2/reference/webview2.idl#createcorewebview2environmentwithdetails) from which we can create the two separate environments for the browser's UI and content.
136+
WebView2 allows you to host web content in your Windows app. It exposes the globals [CreateCoreWebView2Environment](https://docs.microsoft.com/microsoft-edge/hosting/webview2/reference/webview2.idl#createcorewebview2environment) and [CreateCoreWebView2EnvironmentWithOptions](https://docs.microsoft.com/microsoft-edge/hosting/webview2/reference/webview2.idl#createcorewebview2environmentwithoptions) from which we can create the two separate environments for the browser's UI and content.
137137

138138
```cpp
139139
// Get directory for user data. This will be kept separated from the
@@ -145,7 +145,7 @@ WebView2 allows you to host web content in your Windows app. It exposes the glob
145145
// tabs will be created from this environment and kept isolated from the
146146
// browser UI. This enviroment is created first so the UI can request new
147147
// tabs when it's ready.
148-
HRESULT hr = CreateCoreWebView2EnvironmentWithDetails(nullptr, userDataDirectory.c_str(),
148+
HRESULT hr = CreateCoreWebView2EnvironmentWithOptions(nullptr, userDataDirectory.c_str(),
149149
L"", Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
150150
[this](HRESULT result, ICoreWebView2Environment* env) -> HRESULT
151151
{
@@ -171,7 +171,7 @@ HRESULT BrowserWindow::InitUIWebViews()
171171
172172
// Create WebView environment for browser UI. A separate data directory is
173173
// used to isolate the browser UI from web content requested by the user.
174-
return CreateCoreWebView2EnvironmentWithDetails(nullptr, browserDataDirectory.c_str(),
174+
return CreateCoreWebView2EnvironmentWithOptions(nullptr, browserDataDirectory.c_str(),
175175
L"", Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
176176
[this](HRESULT result, ICoreWebView2Environment* env) -> HRESULT
177177
{
@@ -191,26 +191,26 @@ We use the [ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler](https://
191191
```cpp
192192
HRESULT BrowserWindow::CreateBrowserControlsWebView()
193193
{
194-
return m_uiEnv->CreateCoreWebView2Host(m_hWnd, Callback<ICoreWebView2CreateCoreWebView2HostCompletedHandler>(
195-
[this](HRESULT result, ICoreWebView2Host* host) -> HRESULT
194+
return m_uiEnv->CreateCoreWebView2Controller(m_hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
195+
[this](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT
196196
{
197197
if (!SUCCEEDED(result))
198198
{
199199
OutputDebugString(L"Controls WebView creation failed\n");
200200
return result;
201201
}
202202
// WebView created
203-
m_controlsHost = host;
204-
CheckFailure(m_controlsHost->get_CoreWebView2(&m_controlsWebView), L"");
203+
m_controlsController = controller;
204+
CheckFailure(m_controlsController->get_CoreWebView2(&m_controlsWebView), L"");
205205

206206
wil::com_ptr<ICoreWebView2Settings> settings;
207207
RETURN_IF_FAILED(m_controlsWebView->get_Settings(&settings));
208208
RETURN_IF_FAILED(settings->put_AreDevToolsEnabled(FALSE));
209209

210-
RETURN_IF_FAILED(m_controlsHost->add_ZoomFactorChanged(Callback<ICoreWebView2ZoomFactorChangedEventHandler>(
211-
[](ICoreWebView2Host* host, IUnknown* args) -> HRESULT
210+
RETURN_IF_FAILED(m_controlsController->add_ZoomFactorChanged(Callback<ICoreWebView2ZoomFactorChangedEventHandler>(
211+
[](ICoreWebView2Controller* controller, IUnknown* args) -> HRESULT
212212
{
213-
host->put_ZoomFactor(1.0);
213+
controller->put_ZoomFactor(1.0);
214214
return S_OK;
215215
}
216216
).Get(), &m_controlsZoomToken));
@@ -419,8 +419,8 @@ We need to communicate the WebViews powering tabs and UI so that user interactio
419419
```cpp
420420
HRESULT BrowserWindow::CreateBrowserControlsWebView()
421421
{
422-
return m_uiEnv->CreateCoreWebView2Host(m_hWnd, Callback<ICoreWebView2CreateCoreWebView2HostCompletedHandler>(
423-
[this](HRESULT result, ICoreWebView2Host* host) -> HRESULT
422+
return m_uiEnv->CreateCoreWebView2Controller(m_hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
423+
[this](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT
424424
{
425425
// ...
426426
@@ -544,15 +544,15 @@ std::unique_ptr<Tab> Tab::CreateNewTab(HWND hWnd, ICoreWebView2Environment* env,
544544
545545
HRESULT Tab::Init(ICoreWebView2Environment* env, bool shouldBeActive)
546546
{
547-
return env->CreateCoreWebView2Host(m_parentHWnd, Callback<ICoreWebView2CreateCoreWebView2HostCompletedHandler>(
548-
[this, shouldBeActive](HRESULT result, ICoreWebView2Host* host) -> HRESULT {
547+
return env->CreateCoreWebView2Controller(m_parentHWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
548+
[this, shouldBeActive](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
549549
if (!SUCCEEDED(result))
550550
{
551551
OutputDebugString(L"Tab WebView creation failed\n");
552552
return result;
553553
}
554-
m_contentHost = host;
555-
BrowserWindow::CheckFailure(m_contentHost->get_CoreWebView2(&m_contentWebView), L"");
554+
m_contentController = controller;
555+
BrowserWindow::CheckFailure(m_contentController->get_CoreWebView2(&m_contentWebView), L"");
556556
BrowserWindow* browserWindow = reinterpret_cast<BrowserWindow*>(GetWindowLongPtr(m_parentHWnd, GWLP_USERDATA));
557557
RETURN_IF_FAILED(m_contentWebView->add_WebMessageReceived(m_messageBroker.Get(), &m_messageBrokerToken));
558558

Tab.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ std::unique_ptr<Tab> Tab::CreateNewTab(HWND hWnd, ICoreWebView2Environment* env,
2121

2222
HRESULT Tab::Init(ICoreWebView2Environment* env, bool shouldBeActive)
2323
{
24-
return env->CreateCoreWebView2Host(m_parentHWnd, Callback<ICoreWebView2CreateCoreWebView2HostCompletedHandler>(
25-
[this, shouldBeActive](HRESULT result, ICoreWebView2Host* host) -> HRESULT {
24+
return env->CreateCoreWebView2Controller(m_parentHWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
25+
[this, shouldBeActive](HRESULT result, ICoreWebView2Controller* host) -> HRESULT {
2626
if (!SUCCEEDED(result))
2727
{
2828
OutputDebugString(L"Tab WebView creation failed\n");
2929
return result;
3030
}
31-
m_contentHost = host;
32-
BrowserWindow::CheckFailure(m_contentHost->get_CoreWebView2(&m_contentWebView), L"");
31+
m_contentController = host;
32+
BrowserWindow::CheckFailure(m_contentController->get_CoreWebView2(&m_contentWebView), L"");
3333
BrowserWindow* browserWindow = reinterpret_cast<BrowserWindow*>(GetWindowLongPtr(m_parentHWnd, GWLP_USERDATA));
3434
RETURN_IF_FAILED(m_contentWebView->add_WebMessageReceived(m_messageBroker.Get(), &m_messageBrokerToken));
3535

@@ -106,5 +106,5 @@ HRESULT Tab::ResizeWebView()
106106
BrowserWindow* browserWindow = reinterpret_cast<BrowserWindow*>(GetWindowLongPtr(m_parentHWnd, GWLP_USERDATA));
107107
bounds.top += browserWindow->GetDPIAwareBound(BrowserWindow::c_uiBarHeight);
108108

109-
return m_contentHost->put_Bounds(bounds);
109+
return m_contentController->put_Bounds(bounds);
110110
}

Tab.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Tab
1010
{
1111
public:
12-
Microsoft::WRL::ComPtr<ICoreWebView2Host> m_contentHost;
12+
Microsoft::WRL::ComPtr<ICoreWebView2Controller> m_contentController;
1313
Microsoft::WRL::ComPtr<ICoreWebView2> m_contentWebView;
1414
Microsoft::WRL::ComPtr<ICoreWebView2DevToolsProtocolEventReceiver> m_securityStateChangedReceiver;
1515

0 commit comments

Comments
 (0)