diff --git a/SampleApps/WebView2APISample/AppWindow.cpp b/SampleApps/WebView2APISample/AppWindow.cpp index 1bb95c0f..906e1009 100644 --- a/SampleApps/WebView2APISample/AppWindow.cpp +++ b/SampleApps/WebView2APISample/AppWindow.cpp @@ -39,6 +39,7 @@ #include "ScenarioExtensionsManagement.h" #include "ScenarioIFrameDevicePermission.h" #include "ScenarioNavigateWithWebResourceRequest.h" +#include "ScenarioAcceleratorKeyPressed.h" #include "ScenarioNotificationReceived.h" #include "ScenarioPermissionManagement.h" #include "ScenarioSharedBuffer.h" @@ -651,6 +652,11 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam) { return PrintToPdfStream(); } + case IDM_SCENARIO_ACCELERATOR_KEY_PRESSED: + { + NewComponent(this); + return true; + } } return false; } diff --git a/SampleApps/WebView2APISample/ProcessComponent.cpp b/SampleApps/WebView2APISample/ProcessComponent.cpp index 9919d1b1..72dda647 100644 --- a/SampleApps/WebView2APISample/ProcessComponent.cpp +++ b/SampleApps/WebView2APISample/ProcessComponent.cpp @@ -244,17 +244,17 @@ void ProcessComponent::AppendFrameInfo( CHECK_FAILURE(frameInfo->get_Source(&sourceRaw)); std::wstring source = sourceRaw.get()[0] ? sourceRaw.get() : L"none"; - wil::com_ptr frameInfoExperimental; - CHECK_FAILURE(frameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental))); - frameInfoExperimental->get_FrameId(&frameId); - frameInfoExperimental->get_FrameKind(&frameKind); + wil::com_ptr frameInfo2; + CHECK_FAILURE(frameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2))); + frameInfo2->get_FrameId(&frameId); + frameInfo2->get_FrameKind(&frameKind); wil::com_ptr parentFrameInfo; - CHECK_FAILURE(frameInfoExperimental->get_ParentFrameInfo(&parentFrameInfo)); + CHECK_FAILURE(frameInfo2->get_ParentFrameInfo(&parentFrameInfo)); if (parentFrameInfo) { - CHECK_FAILURE(parentFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental))); - CHECK_FAILURE(frameInfoExperimental->get_FrameId(&parentFrameId)); + CHECK_FAILURE(parentFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2))); + CHECK_FAILURE(frameInfo2->get_FrameId(&parentFrameId)); } wil::com_ptr mainFrameInfo = GetAncestorMainFrameInfo(frameInfo); @@ -262,8 +262,8 @@ void ProcessComponent::AppendFrameInfo( { type = L"main frame"; } - CHECK_FAILURE(mainFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental))); - CHECK_FAILURE(frameInfoExperimental->get_FrameId(&mainFrameId)); + CHECK_FAILURE(mainFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2))); + CHECK_FAILURE(frameInfo2->get_FrameId(&mainFrameId)); wil::com_ptr childFrameInfo = GetAncestorMainFrameDirectChildFrameInfo(frameInfo); @@ -273,8 +273,8 @@ void ProcessComponent::AppendFrameInfo( } if (childFrameInfo) { - CHECK_FAILURE(childFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental))); - CHECK_FAILURE(frameInfoExperimental->get_FrameId(&childFrameId)); + CHECK_FAILURE(childFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2))); + CHECK_FAILURE(frameInfo2->get_FrameId(&childFrameId)); } result << L"{frame name:" << name << L" | frame Id:" << frameId << L" | parent frame Id:" @@ -293,12 +293,12 @@ wil::com_ptr ProcessComponent::GetAncestorMainFrameInfo( wil::com_ptr frameInfo) { wil::com_ptr mainFrameInfo; - wil::com_ptr frameInfoExperimental; + wil::com_ptr frameInfo2; while (frameInfo) { mainFrameInfo = frameInfo; - CHECK_FAILURE(frameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental))); - CHECK_FAILURE(frameInfoExperimental->get_ParentFrameInfo(&frameInfo)); + CHECK_FAILURE(frameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2))); + CHECK_FAILURE(frameInfo2->get_ParentFrameInfo(&frameInfo)); } return mainFrameInfo; } @@ -320,30 +320,28 @@ wil::com_ptr ProcessComponent::GetAncestorMainFrameDirec { wil::com_ptr mainFrameInfo; wil::com_ptr childFrameInfo; - wil::com_ptr frameInfoExperimental; + wil::com_ptr frameInfo2; while (frameInfo) { childFrameInfo = mainFrameInfo; mainFrameInfo = frameInfo; - CHECK_FAILURE(frameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental))); - CHECK_FAILURE(frameInfoExperimental->get_ParentFrameInfo(&frameInfo)); + CHECK_FAILURE(frameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2))); + CHECK_FAILURE(frameInfo2->get_ParentFrameInfo(&frameInfo)); } return childFrameInfo; } void ProcessComponent::ShowProcessExtendedInfo() { - auto environmentExperimental13 = - m_webViewEnvironment.try_query(); - if (environmentExperimental13) + auto environment13 = m_webViewEnvironment.try_query(); + if (environment13) { //! [GetProcessExtendedInfos] - CHECK_FAILURE(environmentExperimental13->GetProcessExtendedInfos( - Callback( + CHECK_FAILURE(environment13->GetProcessExtendedInfos( + Callback( [this]( HRESULT error, - ICoreWebView2ExperimentalProcessExtendedInfoCollection* processCollection) - -> HRESULT + ICoreWebView2ProcessExtendedInfoCollection* processCollection) -> HRESULT { UINT32 processCount = 0; UINT32 rendererProcessCount = 0; @@ -352,7 +350,7 @@ void ProcessComponent::ShowProcessExtendedInfo() std::wstringstream rendererProcessInfos; for (UINT32 i = 0; i < processCount; i++) { - Microsoft::WRL::ComPtr + Microsoft::WRL::ComPtr processExtendedInfo; CHECK_FAILURE( processCollection->GetValueAtIndex(i, &processExtendedInfo)); diff --git a/SampleApps/WebView2APISample/ScenarioAcceleratorKeyPressed.cpp b/SampleApps/WebView2APISample/ScenarioAcceleratorKeyPressed.cpp new file mode 100644 index 00000000..dadaf811 --- /dev/null +++ b/SampleApps/WebView2APISample/ScenarioAcceleratorKeyPressed.cpp @@ -0,0 +1,145 @@ +// 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 "ScenarioAcceleratorKeyPressed.h" + +#include "AppWindow.h" +#include "CheckFailure.h" + +using namespace Microsoft::WRL; + +static constexpr WCHAR c_samplePath[] = L"ScenarioAcceleratorKeyPressed.html"; +ScenarioAcceleratorKeyPressed::ScenarioAcceleratorKeyPressed(AppWindow* appWindow) + : m_appWindow(appWindow), m_controller(appWindow->GetWebViewController()), + m_webView(appWindow->GetWebView()) +{ + m_sampleUri = m_appWindow->GetLocalUri(c_samplePath); + wil::com_ptr settings; + CHECK_FAILURE(m_webView->get_Settings(&settings)); + m_settings3 = settings.try_query(); + // Setup the web message received event handler before navigating to + // ensure we don't miss any messages. + CHECK_FAILURE(m_webView->add_WebMessageReceived( + Microsoft::WRL::Callback( + [this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args) + { + wil::unique_cotaskmem_string uri; + CHECK_FAILURE(args->get_Source(&uri)); + + // Always validate that the origin of the message is what you expect. + if (uri.get() != m_sampleUri) + { + return S_OK; + } + wil::unique_cotaskmem_string messageRaw; + CHECK_FAILURE(args->TryGetWebMessageAsString(&messageRaw)); + std::wstring message = messageRaw.get(); + std::wstring reply; + + if (message.compare(0, 26, L"DisableBrowserAccelerators") == 0) + { + CHECK_FAILURE(m_settings3->put_AreBrowserAcceleratorKeysEnabled(FALSE)); + MessageBox( + nullptr, + L"Browser-specific accelerator keys, for example: \n" + L"Ctrl+F and F3 for Find on Page\n" + L"Ctrl+P for Print\n" + L"Ctrl+R and F5 for Reload\n" + L"Ctrl+Plus and Ctrl+Minus for zooming\n" + L"Ctrl+Shift-C and F12 for DevTools\n" + L"Special keys for browser functions, such as Back, Forward, and " + L"Search \n" + L"will be disabled after the next navigation except for F7.", + L"Settings change", MB_OK); + } + else if (message.compare(0, 25, L"EnableBrowserAccelerators") == 0) + { + CHECK_FAILURE(m_settings3->put_AreBrowserAcceleratorKeysEnabled(TRUE)); + MessageBox( + nullptr, + L"Browser-specific accelerator keys, for example: \n" + L"Ctrl+F and F3 for Find on Page\n" + L"Ctrl+R and F5 for Reload\n" + L"Ctrl+Plus and Ctrl+Minus for zooming\n" + L"Ctrl+Shift-C and F12 for DevTools\n" + L"Special keys for browser functions, such as Back, Forward, and " + L"Search \n" + L"will be enabled after the next navigation except for Ctr + P.", + L"Settings change", MB_OK); + } + return S_OK; + }) + .Get(), + &m_webMessageReceivedToken)); + + //! [IsBrowserAcceleratorKeyEnabled] + if (m_settings3) + { + // Register a handler for the AcceleratorKeyPressed event. + CHECK_FAILURE(m_controller->add_AcceleratorKeyPressed( + Callback( + [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)); + + wil::com_ptr args2; + + args->QueryInterface(IID_PPV_ARGS(&args2)); + if (args2) + { + if (key == 'P' && (GetKeyState(VK_CONTROL) < 0)) + { + // tell the browser to skip the key + CHECK_FAILURE(args2->put_IsBrowserAcceleratorKeyEnabled(FALSE)); + } + if (key == VK_F7) + { + // tell the browser to process the key + CHECK_FAILURE(args2->put_IsBrowserAcceleratorKeyEnabled(TRUE)); + } + } + } + return S_OK; + }) + .Get(), + &m_acceleratorKeyPressedToken)); + } + //! [IsBrowserAcceleratorKeyEnabled] + + // Turn off this scenario if we navigate away from the sample page + CHECK_FAILURE(m_webView->add_ContentLoading( + Callback( + [this](ICoreWebView2* sender, ICoreWebView2ContentLoadingEventArgs* args) -> HRESULT + { + wil::unique_cotaskmem_string uri; + sender->get_Source(&uri); + if (uri.get() != m_sampleUri) + { + m_appWindow->DeleteComponent(this); + } + return S_OK; + }) + .Get(), + &m_contentLoadingToken)); + + CHECK_FAILURE(m_webView->Navigate(m_sampleUri.c_str())); +} + +ScenarioAcceleratorKeyPressed::~ScenarioAcceleratorKeyPressed() +{ + m_webView->remove_WebMessageReceived(m_webMessageReceivedToken); + m_controller->remove_AcceleratorKeyPressed(m_acceleratorKeyPressedToken); + m_webView->remove_ContentLoading(m_contentLoadingToken); +} diff --git a/SampleApps/WebView2APISample/ScenarioAcceleratorKeyPressed.h b/SampleApps/WebView2APISample/ScenarioAcceleratorKeyPressed.h new file mode 100644 index 00000000..da287090 --- /dev/null +++ b/SampleApps/WebView2APISample/ScenarioAcceleratorKeyPressed.h @@ -0,0 +1,29 @@ +// 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. + +#pragma once +#include "stdafx.h" + +#include + +#include "AppWindow.h" +#include "ComponentBase.h" + +class ScenarioAcceleratorKeyPressed : public ComponentBase +{ +public: + ScenarioAcceleratorKeyPressed(AppWindow* appWindow); + ~ScenarioAcceleratorKeyPressed() override; + +private: + EventRegistrationToken m_acceleratorKeyPressedToken = {}; + EventRegistrationToken m_contentLoadingToken = {}; + EventRegistrationToken m_webMessageReceivedToken = {}; + + AppWindow* m_appWindow = nullptr; + std::wstring m_sampleUri; + wil::com_ptr m_controller; + wil::com_ptr m_webView; + wil::com_ptr m_settings3; +}; \ No newline at end of file diff --git a/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp b/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp index d9c26d24..29dfc91d 100644 --- a/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp +++ b/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp @@ -993,21 +993,20 @@ void ScenarioWebViewEventMonitor::InitializeEventView(ICoreWebView2* webviewEven L"{ \"kind\": \"event\", \"name\": \"FrameCreated\", \"args\": {"; message += L"\"frame\": " + EncodeQuote(name.get()); - auto webView2Experimental23 = wil::com_ptr(sender) - .try_query(); - if (webView2Experimental23) + auto webView2_20 = + wil::com_ptr(sender).try_query(); + if (webView2_20) { UINT32 frameId = 0; - CHECK_FAILURE(webView2Experimental23->get_FrameId(&frameId)); + CHECK_FAILURE(webView2_20->get_FrameId(&frameId)); message += L",\"sender main frame id\": " + std::to_wstring((int)frameId); } - auto experimentalFrame5 = - webviewFrame.try_query(); - if (experimentalFrame5) + auto frame5 = webviewFrame.try_query(); + if (frame5) { UINT32 frameId = 0; - CHECK_FAILURE(experimentalFrame5->get_FrameId(&frameId)); + CHECK_FAILURE(frame5->get_FrameId(&frameId)); message += L",\"frame id\": " + std::to_wstring((int)frameId); } message += diff --git a/SampleApps/WebView2APISample/WebView2APISample.rc b/SampleApps/WebView2APISample/WebView2APISample.rc index dcd89070..e23e7fd2 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.rc +++ b/SampleApps/WebView2APISample/WebView2APISample.rc @@ -292,6 +292,7 @@ BEGIN MENUITEM "Web Messaging", IDM_SCENARIO_POST_WEB_MESSAGE MENUITEM "WebView Event Monitor", IDM_SCENARIO_WEB_VIEW_EVENT_MONITOR MENUITEM "Shared Buffer", IDM_SCENARIO_SHARED_BUFFER + MENUITEM "Accelerator Key Pressed", IDM_SCENARIO_ACCELERATOR_KEY_PRESSED END POPUP "&Audio" BEGIN diff --git a/SampleApps/WebView2APISample/WebView2APISample.vcxproj b/SampleApps/WebView2APISample/WebView2APISample.vcxproj index dc4f6515..b840516d 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.vcxproj +++ b/SampleApps/WebView2APISample/WebView2APISample.vcxproj @@ -1,4 +1,4 @@ - + @@ -225,6 +225,7 @@ + @@ -272,6 +273,7 @@ + @@ -336,6 +338,9 @@ $(OutDir)\assets + + $(OutDir)\assets + $(OutDir)\assets @@ -434,4 +439,4 @@ - + \ No newline at end of file diff --git a/SampleApps/WebView2APISample/assets/ScenarioAcceleratorKeyPressed.html b/SampleApps/WebView2APISample/assets/ScenarioAcceleratorKeyPressed.html new file mode 100644 index 00000000..1b848b88 --- /dev/null +++ b/SampleApps/WebView2APISample/assets/ScenarioAcceleratorKeyPressed.html @@ -0,0 +1,24 @@ + + + + ScenarioAcceleratorKeyPressed + + + + +

Disable all Browser Accelerator keys but F7 remains enabled

+ + + +

Enable all Browser Accelerator keys but Ctrl + P remains disabled

+ + + + \ No newline at end of file diff --git a/SampleApps/WebView2APISample/resource.h b/SampleApps/WebView2APISample/resource.h index 9da8d1cb..b0bd30b0 100644 --- a/SampleApps/WebView2APISample/resource.h +++ b/SampleApps/WebView2APISample/resource.h @@ -176,6 +176,7 @@ #define IDM_PERMISSION_MANAGEMENT 2036 #define IDM_SCENARIO_CLEAR_CUSTOM_DATA_PARTITION 2037 #define IDM_SCENARIO_NOTIFICATION 2039 +#define IDM_SCENARIO_ACCELERATOR_KEY_PRESSED 2042 #define IDM_CREATION_MODE_WINDOWED 3000 #define IDM_CREATION_MODE_VISUAL_DCOMP 3001 #define IDM_CREATION_MODE_TARGET_DCOMP 3002 diff --git a/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.Designer.cs b/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.Designer.cs index b56843db..cf403323 100644 --- a/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.Designer.cs +++ b/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.Designer.cs @@ -338,6 +338,7 @@ private void InitializeComponent() this.acceleratorKeysEnabledToolStripMenuItem.Text = "Toggle AcceleratorKeys"; this.acceleratorKeysEnabledToolStripMenuItem.Checked = true; this.acceleratorKeysEnabledToolStripMenuItem.CheckOnClick = true; + this.acceleratorKeysEnabledToolStripMenuItem.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; // // allowExternalDropMenuItem // @@ -347,6 +348,7 @@ private void InitializeComponent() this.allowExternalDropMenuItem.Checked = true; this.allowExternalDropMenuItem.CheckOnClick = true; this.allowExternalDropMenuItem.Click += new System.EventHandler(this.allowExternalDropMenuItem_Click); + this.allowExternalDropMenuItem.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; // // serverCertificateErrorMenuItem // @@ -370,6 +372,7 @@ private void InitializeComponent() this.toggleCustomServerCertificateSupportMenuItem.Size = new System.Drawing.Size(359, 44); this.toggleCustomServerCertificateSupportMenuItem.Text = "Toggle Custom Server Certificate Support"; this.toggleCustomServerCertificateSupportMenuItem.Click += new System.EventHandler(this.toggleCustomServerCertificateSupportMenuItem_Click); + this.toggleCustomServerCertificateSupportMenuItem.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; // // clearServerCertificateErrorActionsMenuItem // @@ -384,6 +387,7 @@ private void InitializeComponent() this.toggleDefaultScriptDialogsMenuItem.Size = new System.Drawing.Size(359, 44); this.toggleDefaultScriptDialogsMenuItem.Text = "Toggle Default Script Dialogs"; this.toggleDefaultScriptDialogsMenuItem.Click += new System.EventHandler(this.toggleDefaultScriptDialogsMenuItem_Click); + this.toggleDefaultScriptDialogsMenuItem.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; // // blockedDomainsMenuItem // @@ -449,6 +453,7 @@ private void InitializeComponent() this.toggleVisibilityMenuItem.Checked = true; this.toggleVisibilityMenuItem.CheckOnClick = true; this.toggleVisibilityMenuItem.Click += new System.EventHandler(this.toggleVisibilityMenuItem_Click); + this.toggleVisibilityMenuItem.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; // // zoomToolStripMenuItem // @@ -696,6 +701,7 @@ private void InitializeComponent() this.toggleMuteStateMenuItem.Click += new System.EventHandler(this.toggleMuteStateMenuItem_Click); this.toggleMuteStateMenuItem.Checked = true; this.toggleMuteStateMenuItem.CheckOnClick = true; + this.toggleMuteStateMenuItem.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; // // helpToolStripMenuItem // diff --git a/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample (Package)/WebView2_WinUI3_Sample (Package).assets.cache b/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample (Package)/WebView2_WinUI3_Sample (Package).assets.cache deleted file mode 100644 index ee1e39cd..00000000 Binary files a/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample (Package)/WebView2_WinUI3_Sample (Package).assets.cache and /dev/null differ diff --git a/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/MainWindow.xaml.cs b/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/MainWindow.xaml.cs index 2a2c7c0d..3c651972 100644 --- a/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/MainWindow.xaml.cs +++ b/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/MainWindow.xaml.cs @@ -4,7 +4,6 @@ using System; using Microsoft.Web.WebView2.Core; using System.Diagnostics; - // To learn more about WinUI, the WinUI project structure, // and more about our project templates, see: http://aka.ms/winui-project-info. @@ -46,7 +45,7 @@ private void WebView2_CoreWebView2Initialized(WebView2 sender, CoreWebView2Initi } } - private async void WebView2_NavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args) + private void WebView2_NavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args) { StatusUpdate("Navigation complete");