Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update projects to use latest WebView2 SDK 1.0.2730-prerelease #251

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions SampleApps/WebView2APISample/AppWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "ScenarioScreenCapture.h"
#include "ScenarioSharedBuffer.h"
#include "ScenarioSharedWorkerWRR.h"
#include "ScenarioThrottlingControl.h"
#include "ScenarioFileTypePolicy.h"
#include "ScenarioVirtualHostMappingForPopUpWindow.h"
#include "ScenarioVirtualHostMappingForSW.h"
Expand Down Expand Up @@ -426,6 +427,7 @@ bool AppWindow::HandleWindowMessage(
break;
//! [RestartManager]
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
{
// If bit 30 is set, it means the WM_KEYDOWN message is autorepeated.
// We want to ignore it in that case.
Expand Down Expand Up @@ -667,6 +669,11 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
NewComponent<ScenarioAcceleratorKeyPressed>(this);
return true;
}
case IDM_SCENARIO_THROTTLING_CONTROL:
{
NewComponent<ScenarioThrottlingControl>(this);
return true;
}
case IDM_SCENARIO_SCREEN_CAPTURE:
{
NewComponent<ScenarioScreenCapture>(this);
Expand Down Expand Up @@ -1249,6 +1256,14 @@ std::function<void()> AppWindow::GetAcceleratorKeyFunction(UINT key)
return [this] { CloseWebView(); };
}
}
if (GetKeyState(VK_MENU) < 0) // VK_MENU == Alt key
{
switch (key)
{
case 'D': // Alt+D focuses and selects the address bar, like the browser.
return [this] { m_toolbar.SelectAddressBar(); };
}
}
return nullptr;
}

Expand Down
15 changes: 9 additions & 6 deletions SampleApps/WebView2APISample/AppWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,15 @@ class AppWindow
{
public:
AppWindow(
UINT creationModeId, const WebViewCreateOption& opt,
const std::wstring& initialUri = L"", const std::wstring& userDataFolderParam = L"",
bool isMainWindow = false, std::function<void()> webviewCreatedCallback = nullptr,
bool customWindowRect = false, RECT windowRect = {0}, bool shouldHaveToolbar = true,
UINT creationModeId,
const WebViewCreateOption& opt,
const std::wstring& initialUri = L"",
const std::wstring& userDataFolderParam = L"",
bool isMainWindow = false,
std::function<void()> webviewCreatedCallback = nullptr,
bool customWindowRect = false,
RECT windowRect = {0},
bool shouldHaveToolbar = true,
bool isPopup = false);

~AppWindow();
Expand Down Expand Up @@ -210,7 +215,6 @@ class AppWindow
SamplePrintSettings GetSelectedPrinterPrintSettings(std::wstring printerName);
bool PrintToPdfStream();
void ToggleTrackingPrevention();

std::wstring GetLocalPath(std::wstring path, bool keep_exe_path);
void DeleteAllComponents();

Expand Down Expand Up @@ -273,7 +277,6 @@ class AppWindow
bool m_isPopupWindow = false;
void EnterFullScreen();
void ExitFullScreen();

// Compositor creation helper methods
HRESULT DCompositionCreateDevice2(IUnknown* renderingDevice, REFIID riid, void** ppv);
HRESULT TryCreateDispatcherQueue();
Expand Down
8 changes: 7 additions & 1 deletion SampleApps/WebView2APISample/ControlComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ bool ControlComponent::HandleChildWindowMessage(
// 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)
if (message == WM_KEYDOWN || message == WM_SYSKEYDOWN)
{
//! [MoveFocus1]
if (wParam == VK_TAB)
Expand Down Expand Up @@ -320,6 +320,12 @@ bool ControlComponent::HandleChildWindowMessage(
NavigateToAddressBar();
return true;
}
// Ctrl+A is SelectAll
else if ((GetKeyState(VK_CONTROL) < 0) && ((UINT)wParam == 'A'))
{
m_toolbar->SelectAll();
return true;
}
else
{
// If bit 30 is set, it means the WM_KEYDOWN message is autorepeated.
Expand Down
34 changes: 15 additions & 19 deletions SampleApps/WebView2APISample/ScenarioNotificationReceived.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ ScenarioNotificationReceived::ScenarioNotificationReceived(AppWindow* appWindow)
: m_appWindow(appWindow), m_webView(appWindow->GetWebView())
{
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
m_webView2Experimental22 = m_webView.try_query<ICoreWebView2Experimental22>();
if (!m_webView2Experimental22)
m_webView2_24 = m_webView.try_query<ICoreWebView2_24>();
if (!m_webView2_24)
return;
//! [NotificationReceived]
// Register a handler for the NotificationReceived event.
CHECK_FAILURE(m_webView2Experimental22->add_NotificationReceived(
Callback<ICoreWebView2ExperimentalNotificationReceivedEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2ExperimentalNotificationReceivedEventArgs* args) -> HRESULT
CHECK_FAILURE(m_webView2_24->add_NotificationReceived(
Callback<ICoreWebView2NotificationReceivedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2NotificationReceivedEventArgs* args)
-> HRESULT
{
// Block notifications from specific URIs and set Handled to
// true so the the default notification UI will not be
Expand All @@ -45,14 +44,13 @@ ScenarioNotificationReceived::ScenarioNotificationReceived(AppWindow* appWindow)
wil::unique_cotaskmem_string origin;
CHECK_FAILURE(args->get_SenderOrigin(&origin));
std::wstring originString = origin.get();
Microsoft::WRL::ComPtr<ICoreWebView2ExperimentalNotification> notification;
Microsoft::WRL::ComPtr<ICoreWebView2Notification> notification;
CHECK_FAILURE(args->get_Notification(&notification));

notification->add_CloseRequested(
Callback<ICoreWebView2ExperimentalNotificationCloseRequestedEventHandler>(
Callback<ICoreWebView2NotificationCloseRequestedEventHandler>(
[this, &sender](
ICoreWebView2ExperimentalNotification* notification,
IUnknown* args) -> HRESULT
ICoreWebView2Notification* notification, IUnknown* args) -> HRESULT
{
// Remove the notification from the list of active
// notifications.
Expand All @@ -64,8 +62,8 @@ ScenarioNotificationReceived::ScenarioNotificationReceived(AppWindow* appWindow)

m_appWindow->RunAsync(
[this,
notificationCom = wil::make_com_ptr<ICoreWebView2ExperimentalNotification>(
notification.Get()),
notificationCom =
wil::make_com_ptr<ICoreWebView2Notification>(notification.Get()),
deferral, originString]()
{
ShowNotification(notificationCom.get(), originString);
Expand Down Expand Up @@ -95,7 +93,7 @@ bool ScenarioNotificationReceived::HandleWindowMessage(
}

void ScenarioNotificationReceived::ShowNotification(
ICoreWebView2ExperimentalNotification* notification, std::wstring origin)
ICoreWebView2Notification* notification, std::wstring origin)
{
ICoreWebView2* webView = m_webView.get();
wil::unique_cotaskmem_string title;
Expand Down Expand Up @@ -139,8 +137,7 @@ void ScenarioNotificationReceived::ShowNotification(
(response == IDOK) ? notification->ReportClicked() : notification->ReportClosed();
}

void ScenarioNotificationReceived::RemoveNotification(
ICoreWebView2ExperimentalNotification* notification)
void ScenarioNotificationReceived::RemoveNotification(ICoreWebView2Notification* notification)
{
// Close custom notification.

Expand All @@ -155,9 +152,8 @@ void ScenarioNotificationReceived::NavigateToNotificationPage()

ScenarioNotificationReceived::~ScenarioNotificationReceived()
{
if (m_webView2Experimental22)
if (m_webView2_24)
{
CHECK_FAILURE(
m_webView2Experimental22->remove_NotificationReceived(m_notificationReceivedToken));
CHECK_FAILURE(m_webView2_24->remove_NotificationReceived(m_notificationReceivedToken));
}
}
7 changes: 3 additions & 4 deletions SampleApps/WebView2APISample/ScenarioNotificationReceived.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ class ScenarioNotificationReceived : public ComponentBase

private:
void NavigateToNotificationPage();
void ShowNotification(
ICoreWebView2ExperimentalNotification* notification, std::wstring origin);
void RemoveNotification(ICoreWebView2ExperimentalNotification* notification);
void ShowNotification(ICoreWebView2Notification* notification, std::wstring origin);
void RemoveNotification(ICoreWebView2Notification* notification);

AppWindow* m_appWindow = nullptr;
wil::com_ptr<ICoreWebView2> m_webView;
wil::com_ptr<ICoreWebView2Experimental22> m_webView2Experimental22;
wil::com_ptr<ICoreWebView2_24> m_webView2_24;
std::wstring m_sampleUri;
EventRegistrationToken m_notificationReceivedToken = {};
EventRegistrationToken m_notificationCloseRequestedToken = {};
Expand Down
25 changes: 12 additions & 13 deletions SampleApps/WebView2APISample/ScenarioSaveAs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ScenarioSaveAs::ScenarioSaveAs(AppWindow* appWindow)
{
if (m_webView)
{
m_webView2Experimental25 = m_webView.try_query<ICoreWebView2Experimental25>();
m_webView2_25 = m_webView.try_query<ICoreWebView2_25>();
}
}

Expand All @@ -39,17 +39,16 @@ std::array<std::wstring, 5> saveAsUIResultString{
// This example hides the default save as dialog and shows a customized dialog.
bool ScenarioSaveAs::ToggleSilent()
{
if (!m_webView2Experimental25)
if (!m_webView2_25)
return false;
m_silentSaveAs = !m_silentSaveAs;
if (m_silentSaveAs && m_saveAsUIShowingToken.value == 0)
{
// Register a handler for the `SaveAsUIShowing` event.
m_webView2Experimental25->add_SaveAsUIShowing(
Callback<ICoreWebView2ExperimentalSaveAsUIShowingEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2ExperimentalSaveAsUIShowingEventArgs* args) -> HRESULT
m_webView2_25->add_SaveAsUIShowing(
Callback<ICoreWebView2SaveAsUIShowingEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2SaveAsUIShowingEventArgs* args)
-> HRESULT
{
// Hide the system default save as dialog.
CHECK_FAILURE(args->put_SuppressDefaultDialog(TRUE));
Expand Down Expand Up @@ -111,7 +110,7 @@ bool ScenarioSaveAs::ToggleSilent()
else
{
// Unregister the handler for the `SaveAsUIShowing` event.
m_webView2Experimental25->remove_SaveAsUIShowing(m_saveAsUIShowingToken);
m_webView2_25->remove_SaveAsUIShowing(m_saveAsUIShowingToken);
m_saveAsUIShowingToken.value = 0;
}
MessageBox(
Expand All @@ -126,10 +125,10 @@ bool ScenarioSaveAs::ToggleSilent()
// Call ShowSaveAsUI method to trigger the programmatic save as.
bool ScenarioSaveAs::ProgrammaticSaveAs()
{
if (!m_webView2Experimental25)
if (!m_webView2_25)
return false;
m_webView2Experimental25->ShowSaveAsUI(
Callback<ICoreWebView2ExperimentalShowSaveAsUICompletedHandler>(
m_webView2_25->ShowSaveAsUI(
Callback<ICoreWebView2ShowSaveAsUICompletedHandler>(
[this](HRESULT errorCode, COREWEBVIEW2_SAVE_AS_UI_RESULT result) -> HRESULT
{
// Show ShowSaveAsUI returned result, optional.
Expand Down Expand Up @@ -165,9 +164,9 @@ bool ScenarioSaveAs::HandleWindowMessage(

ScenarioSaveAs::~ScenarioSaveAs()
{
if (m_webView2Experimental25)
if (m_webView2_25)
{
m_webView2Experimental25->remove_SaveAsUIShowing(m_saveAsUIShowingToken);
m_webView2_25->remove_SaveAsUIShowing(m_saveAsUIShowingToken);
}
}

Expand Down
2 changes: 1 addition & 1 deletion SampleApps/WebView2APISample/ScenarioSaveAs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ScenarioSaveAs : public ComponentBase
~ScenarioSaveAs() override;
AppWindow* m_appWindow = nullptr;
wil::com_ptr<ICoreWebView2> m_webView;
wil::com_ptr<ICoreWebView2Experimental25> m_webView2Experimental25;
wil::com_ptr<ICoreWebView2_25> m_webView2_25;
EventRegistrationToken m_saveAsUIShowingToken = {};
bool m_silentSaveAs = false;
};
Expand Down
Loading
Loading