Skip to content

Commit

Permalink
Update projects to use latest WebView2 SDK 1.0.781-prerelease (Micros…
Browse files Browse the repository at this point in the history
  • Loading branch information
oggy22 authored Jan 30, 2021
1 parent 062c3b3 commit 10db37a
Show file tree
Hide file tree
Showing 41 changed files with 759 additions and 295 deletions.
2 changes: 1 addition & 1 deletion SampleApps/WebView2APISample/AppStartPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ std::wstring ResolvePathAndTrimFile(std::wstring path)
PathCchCanonicalize(resultPath, ARRAYSIZE(resultPath), path.c_str());
PathCchRemoveFileSpec(resultPath, ARRAYSIZE(resultPath));
#else
// Surpress compiler warning for PathCanonicalize. It is only used on Win7
// Supress compiler warning for PathCanonicalize. It is only used on Win7
// where PathCchCanonicalize doesn't exist.
#pragma warning(suppress : 4995)
PathCanonicalize(resultPath, path.c_str());
Expand Down
78 changes: 59 additions & 19 deletions SampleApps/WebView2APISample/AppWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ AppWindow::AppWindow(
0, CW_USEDEFAULT, 0, nullptr, nullptr, g_hInstance, nullptr);
}

m_appBackgroundImageHandle = (HBITMAP)LoadImage(
NULL, L"AppBackground.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(m_appBackgroundImageHandle, sizeof(m_appBackgroundImage), &m_appBackgroundImage);
m_memHdc = CreateCompatibleDC(GetDC(m_mainWindow));
SelectObject(m_memHdc, m_appBackgroundImageHandle);

SetWindowLongPtr(m_mainWindow, GWLP_USERDATA, (LONG_PTR)this);

#ifdef USE_WEBVIEW2_WIN10
Expand Down Expand Up @@ -260,7 +266,22 @@ bool AppWindow::HandleWindowMessage(
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc;
RECT mainWindowBounds;
RECT webViewBounds = {0};
BeginPaint(hWnd, &ps);

hdc = GetDC(hWnd);
GetClientRect(hWnd, &mainWindowBounds);

if (auto viewComponent = GetComponent<ViewComponent>())
{
webViewBounds = viewComponent->GetBounds();
}

StretchBlt(hdc, webViewBounds.left, webViewBounds.top, webViewBounds.right, webViewBounds.bottom,
m_memHdc, 0, 0, m_appBackgroundImage.bmWidth, m_appBackgroundImage.bmHeight, SRCCOPY);

EndPaint(hWnd, &ps);
return true;
}
Expand All @@ -282,6 +303,8 @@ bool AppWindow::HandleWindowMessage(
{
PostQuitMessage(retValue);
}
DeleteObject(m_appBackgroundImageHandle);
DeleteDC(m_memHdc);
}
break;
//! [RestartManager]
Expand Down Expand Up @@ -630,23 +653,23 @@ HRESULT AppWindow::OnCreateEnvironmentCompleted(
CHECK_FAILURE(result);
m_webViewEnvironment = environment;

auto webViewExperimentalEnvironment =
m_webViewEnvironment.try_query<ICoreWebView2ExperimentalEnvironment>();
auto webViewEnvironment3 =
m_webViewEnvironment.try_query<ICoreWebView2Environment3>();
#ifdef USE_WEBVIEW2_WIN10
if (webViewExperimentalEnvironment && (m_dcompDevice || m_wincompCompositor))
if (webViewEnvironment3 && (m_dcompDevice || m_wincompCompositor))
#else
if (webViewExperimentalEnvironment && m_dcompDevice)
if (webViewEnvironment3 && m_dcompDevice)
#endif
{
CHECK_FAILURE(webViewExperimentalEnvironment->CreateCoreWebView2CompositionController(
CHECK_FAILURE(webViewEnvironment3->CreateCoreWebView2CompositionController(
m_mainWindow,
Callback<
ICoreWebView2ExperimentalCreateCoreWebView2CompositionControllerCompletedHandler>(
ICoreWebView2CreateCoreWebView2CompositionControllerCompletedHandler>(
[this](
HRESULT result,
ICoreWebView2ExperimentalCompositionController* compositionController) -> HRESULT {
ICoreWebView2CompositionController* compositionController) -> HRESULT {
auto controller =
wil::com_ptr<ICoreWebView2ExperimentalCompositionController>(compositionController)
wil::com_ptr<ICoreWebView2CompositionController>(compositionController)
.query<ICoreWebView2Controller>();
return OnCreateCoreWebView2ControllerCompleted(result, controller.get());
})
Expand Down Expand Up @@ -693,13 +716,16 @@ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(HRESULT result, ICore
m_creationModeId == IDM_CREATION_MODE_TARGET_DCOMP);
NewComponent<ControlComponent>(this, &m_toolbar);

wil::com_ptr<ICoreWebView2Experimental2> webview2;
webview2 = coreWebView2.query<ICoreWebView2Experimental2>();
//! [AddVirtualHostNameToFolderMapping]
// Setup host resource mapping for local files.
webview2->SetVirtualHostNameToFolderMapping(
L"appassets.example", L"assets", COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY_CORS);
//! [AddVirtualHostNameToFolderMapping]
m_webView3 = coreWebView2.query<ICoreWebView2_3>();
if (m_webView3)
{
//! [AddVirtualHostNameToFolderMapping]
// Setup host resource mapping for local files.
m_webView3->SetVirtualHostNameToFolderMapping(
L"appassets.example", L"assets",
COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY_CORS);
//! [AddVirtualHostNameToFolderMapping]
}

// We have a few of our own event handlers to register here as well
RegisterEventHandlers();
Expand Down Expand Up @@ -1096,10 +1122,24 @@ std::wstring AppWindow::GetLocalPath(std::wstring relativePath, bool keep_exe_pa
}
std::wstring AppWindow::GetLocalUri(std::wstring relativePath)
{
//! [LocalUrlUsage]
const std::wstring localFileRootUrl = L"https://appassets.example/";
return localFileRootUrl + regex_replace(relativePath, std::wregex(L"\\\\"), L"/");
//! [LocalUrlUsage]
if (m_webView3)
{
//! [LocalUrlUsage]
const std::wstring localFileRootUrl = L"https://appassets.example/";
return localFileRootUrl + regex_replace(relativePath, std::wregex(L"\\\\"), L"/");
//! [LocalUrlUsage]
}
else
{
std::wstring path = GetLocalPath(L"assets\\" + relativePath, false);

wil::com_ptr<IUri> uri;
CHECK_FAILURE(CreateUri(path.c_str(), Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &uri));

wil::unique_bstr uriBstr;
CHECK_FAILURE(uri->GetAbsoluteUri(&uriBstr));
return std::wstring(uriBstr.get());
}
}

void AppWindow::RunAsync(std::function<void()> callback)
Expand Down
6 changes: 6 additions & 0 deletions SampleApps/WebView2APISample/AppWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class AppWindow
wil::com_ptr<ICoreWebView2Environment> m_webViewEnvironment;
wil::com_ptr<ICoreWebView2Controller> m_controller;
wil::com_ptr<ICoreWebView2> m_webView;
wil::com_ptr<ICoreWebView2_3> m_webView3;

// All components are deleted when the WebView is closed.
std::vector<std::unique_ptr<ComponentBase>> m_components;
Expand Down Expand Up @@ -157,6 +158,11 @@ class AppWindow
winrtComp::Compositor m_wincompCompositor{ nullptr };
winrt::Windows::UI::ViewManagement::UISettings m_uiSettings{ nullptr };
#endif

// Background Image members
HBITMAP m_appBackgroundImageHandle;
BITMAP m_appBackgroundImage;
HDC m_memHdc;
};

template <class ComponentType, class... Args> void AppWindow::NewComponent(Args&&... args)
Expand Down
7 changes: 7 additions & 0 deletions SampleApps/WebView2APISample/CheckFailure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ void CheckFailure(HRESULT hr, const std::wstring& message)
FAIL_FAST();
}
}

void ExperimentalFeatureNotAvailable()
{
MessageBox(nullptr,
L"This experimental feature is not available in the browser version currently being used.",
L"Feature Not Available", MB_OK);
}
8 changes: 7 additions & 1 deletion SampleApps/WebView2APISample/CheckFailure.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ void ShowFailure(HRESULT hr, const std::wstring& message = L"Error");
// If something failed, show the error code and fail fast.
void CheckFailure(HRESULT hr, const std::wstring& message = L"Error");


// Needs to be a separate macro because the preprocessor is weird
#define CHECK_FAILURE_STRINGIFY(arg) #arg

Expand All @@ -26,3 +25,10 @@ void CheckFailure(HRESULT hr, const std::wstring& message = L"Error");
#define CHECK_FAILURE_FILE_LINE(file, line) ([](HRESULT hr){ CheckFailure(hr, L"Failure at " CHECK_FAILURE_STRINGIFY(file) L"(" CHECK_FAILURE_STRINGIFY(line) L")"); })
#define CHECK_FAILURE CHECK_FAILURE_FILE_LINE(__FILE__, __LINE__)
#define CHECK_FAILURE_BOOL(value) CHECK_FAILURE((value) ? S_OK : E_UNEXPECTED)

// Show a message box indicating that an experimental interface isn't available in this browser version.
// Only call this in direct response to a specific user action.
void ExperimentalFeatureNotAvailable();

// Wraps the above in a conditional.
#define CHECK_FEATURE_RETURN(feature, ret) { if (!feature) { ExperimentalFeatureNotAvailable(); return (ret); } }
Loading

0 comments on commit 10db37a

Please sign in to comment.