diff --git a/src/WebWindow.Native/WebWindow.Native.vcxproj b/src/WebWindow.Native/WebWindow.Native.vcxproj index e8d4df2..4064a9a 100644 --- a/src/WebWindow.Native/WebWindow.Native.vcxproj +++ b/src/WebWindow.Native/WebWindow.Native.vcxproj @@ -159,14 +159,14 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + \ No newline at end of file diff --git a/src/WebWindow.Native/WebWindow.Windows.cpp b/src/WebWindow.Native/WebWindow.Windows.cpp index 5a5393f..ad824e6 100644 --- a/src/WebWindow.Native/WebWindow.Windows.cpp +++ b/src/WebWindow.Native/WebWindow.Windows.cpp @@ -142,11 +142,11 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) void WebWindow::RefitContent() { - if (_webviewWindow) + if (_webviewController) { RECT bounds; GetClientRect(_hWnd, &bounds); - _webviewWindow->put_Bounds(bounds); + _webviewController->put_Bounds(bounds); } } @@ -162,7 +162,7 @@ void WebWindow::Show() // Strangely, it only works to create the webview2 *after* the window has been shown, // so defer it until here. This unfortunately means you can't call the Navigate methods // until the window is shown. - if (!_webviewWindow) + if (!_webviewController) { AttachWebView(); } @@ -206,9 +206,10 @@ void WebWindow::AttachWebView() std::atomic_flag flag = ATOMIC_FLAG_INIT; flag.test_and_set(); - HRESULT envResult = CreateWebView2EnvironmentWithDetails(nullptr, nullptr, nullptr, - Callback( - [&, this](HRESULT result, IWebView2Environment* env) -> HRESULT { + HRESULT envResult = CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, nullptr, + Callback( + [&, this](HRESULT result, ICoreWebView2Environment* env) -> HRESULT { + if (result != S_OK) { return result; } HRESULT envResult = env->QueryInterface(&_webviewEnvironment); if (envResult != S_OK) { @@ -216,15 +217,21 @@ void WebWindow::AttachWebView() } // Create a WebView, whose parent is the main window hWnd - env->CreateWebView(_hWnd, Callback( - [&, this](HRESULT result, IWebView2WebView* webview) -> HRESULT { - if (result != S_OK) { return result; } - result = webview->QueryInterface(&_webviewWindow); + env->CreateCoreWebView2Controller(_hWnd, Callback( + [&, this](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT { + if (result != S_OK) { return result; } + HRESULT envResult = controller->QueryInterface(&_webviewController); + if (envResult != S_OK) + { + return envResult; + } + _webviewController->get_CoreWebView2(&_webviewWindow); + // Add a few settings for the webview // this is a redundant demo step as they are the default settings values - IWebView2Settings* Settings; + ICoreWebView2Settings* Settings; _webviewWindow->get_Settings(&Settings); Settings->put_IsScriptEnabled(TRUE); Settings->put_AreDefaultScriptDialogsEnabled(TRUE); @@ -233,20 +240,20 @@ void WebWindow::AttachWebView() // Register interop APIs EventRegistrationToken webMessageToken; _webviewWindow->AddScriptToExecuteOnDocumentCreated(L"window.external = { sendMessage: function(message) { window.chrome.webview.postMessage(message); }, receiveMessage: function(callback) { window.chrome.webview.addEventListener(\'message\', function(e) { callback(e.data); }); } };", nullptr); - _webviewWindow->add_WebMessageReceived(Callback( - [this](IWebView2WebView* webview, IWebView2WebMessageReceivedEventArgs* args) -> HRESULT { + _webviewWindow->add_WebMessageReceived(Callback( + [this](ICoreWebView2* webview, ICoreWebView2WebMessageReceivedEventArgs* args) -> HRESULT { wil::unique_cotaskmem_string message; - args->get_WebMessageAsString(&message); + args->TryGetWebMessageAsString(&message); _webMessageReceivedCallback(message.get()); return S_OK; }).Get(), &webMessageToken); EventRegistrationToken webResourceRequestedToken; - _webviewWindow->AddWebResourceRequestedFilter(L"*", WEBVIEW2_WEB_RESOURCE_CONTEXT_ALL); - _webviewWindow->add_WebResourceRequested(Callback( - [this](IWebView2WebView* sender, IWebView2WebResourceRequestedEventArgs* args) + _webviewWindow->AddWebResourceRequestedFilter(L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL); + _webviewWindow->add_WebResourceRequested(Callback( + [this](ICoreWebView2* sender, ICoreWebView2WebResourceRequestedEventArgs* args) { - IWebView2WebResourceRequest* req; + ICoreWebView2WebResourceRequest* req; args->get_Request(&req); wil::unique_cotaskmem_string uri; @@ -268,7 +275,7 @@ void WebWindow::AttachWebView() std::wstring contentTypeWS = contentType; IStream* dataStream = SHCreateMemStream((BYTE*)dotNetResponse.get(), numBytes); - wil::com_ptr response; + wil::com_ptr response; _webviewEnvironment->CreateWebResourceResponse( dataStream, 200, L"OK", (L"Content-Type: " + contentTypeWS).c_str(), &response); diff --git a/src/WebWindow.Native/WebWindow.h b/src/WebWindow.Native/WebWindow.h index ff9e5b5..0c92dcd 100644 --- a/src/WebWindow.Native/WebWindow.h +++ b/src/WebWindow.Native/WebWindow.h @@ -3,7 +3,8 @@ #ifdef _WIN32 #include -#include +#include +#include #include #include #include @@ -42,8 +43,9 @@ class WebWindow static HINSTANCE _hInstance; HWND _hWnd; WebWindow* _parent; - wil::com_ptr _webviewEnvironment; - wil::com_ptr _webviewWindow; + wil::com_ptr _webviewEnvironment; + wil::com_ptr _webviewWindow; + wil::com_ptr _webviewController; std::map _schemeToRequestHandler; void AttachWebView(); #elif OS_LINUX diff --git a/src/WebWindow.Native/packages.config b/src/WebWindow.Native/packages.config index f10b389..080b0b9 100644 --- a/src/WebWindow.Native/packages.config +++ b/src/WebWindow.Native/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file