From 28a094c9f571423e40d22b160048b9e7ca97e09e Mon Sep 17 00:00:00 2001 From: Dmitriy Komin Date: Mon, 31 Jan 2022 11:41:06 -0800 Subject: [PATCH 1/3] Basic change --- dev/WebView2/WebView2.cpp | 15 +++++++++++++-- dev/WebView2/WebView2.h | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/dev/WebView2/WebView2.cpp b/dev/WebView2/WebView2.cpp index 1fa99d6fbf..c15ce15bf5 100644 --- a/dev/WebView2/WebView2.cpp +++ b/dev/WebView2/WebView2.cpp @@ -1255,14 +1255,25 @@ void WebView2::FireCoreWebView2Initialized(winrt::hresult exception) m_coreWebView2InitializedEventSource(*this, *eventArgs); } -void WebView2::HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, const winrt::RoutedEventArgs&) noexcept +void WebView2::HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, const winrt::RoutedEventArgs&) { if (m_coreWebView && m_xamlFocusChangeInfo.m_isPending) { // In current CoreWebView2 API, MoveFocus is not expected to fail, so set m_webHasFocus here rather than // in asynchronous (MOJO/cross-proc) CoreWebView2 GotFocus event. m_webHasFocus = true; - m_coreWebViewController.MoveFocus(m_xamlFocusChangeInfo.m_storedMoveFocusReason); + try + { + m_coreWebViewController.MoveFocus(m_xamlFocusChangeInfo.m_storedMoveFocusReason); + } + catch (winrt::hresult_error e) + { + if (e.code().value != E_INVALIDARG) + { + throw; + } + } + m_xamlFocusChangeInfo.m_isPending = false; } } diff --git a/dev/WebView2/WebView2.h b/dev/WebView2/WebView2.h index 905c40b6e4..dfa4a69d21 100644 --- a/dev/WebView2/WebView2.h +++ b/dev/WebView2/WebView2.h @@ -146,7 +146,7 @@ class WebView2 : void HandlePointerCaptureLost(const winrt::Windows::Foundation::IInspectable&, const winrt::PointerRoutedEventArgs& args); void HandleKeyDown(const winrt::Windows::Foundation::IInspectable&, const winrt::KeyRoutedEventArgs& e); void HandleGettingFocus(const winrt::Windows::Foundation::IInspectable&, const winrt::GettingFocusEventArgs& args) noexcept; - void HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, const winrt::RoutedEventArgs&) noexcept; + void HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, const winrt::RoutedEventArgs&); void HandleAcceleratorKeyActivated(const winrt::Windows::UI::Core::CoreDispatcher&, const winrt::AcceleratorKeyEventArgs& args) noexcept; void HandleXamlRootChanged(); void HandleSizeChanged(const winrt::IInspectable& /*sender*/, const winrt::SizeChangedEventArgs& args); From 330fefade38ef04077eaf5ba6ac94f3d15de76c7 Mon Sep 17 00:00:00 2001 From: Dmitriy Komin Date: Wed, 2 Feb 2022 11:18:44 -0800 Subject: [PATCH 2/3] Add comments / clean up --- dev/WebView2/WebView2.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dev/WebView2/WebView2.cpp b/dev/WebView2/WebView2.cpp index c15ce15bf5..155a68faad 100644 --- a/dev/WebView2/WebView2.cpp +++ b/dev/WebView2/WebView2.cpp @@ -1259,8 +1259,6 @@ void WebView2::HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, c { if (m_coreWebView && m_xamlFocusChangeInfo.m_isPending) { - // In current CoreWebView2 API, MoveFocus is not expected to fail, so set m_webHasFocus here rather than - // in asynchronous (MOJO/cross-proc) CoreWebView2 GotFocus event. m_webHasFocus = true; try { @@ -1268,6 +1266,12 @@ void WebView2::HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, c } catch (winrt::hresult_error e) { + // Occasionally, a request to restore the minimized window does not complete. This triggers + // FocusManager to set Xaml Focus to WV2 and consequently into CWV2 MoveFocus() call above, + // which in turn will attempt ::SetFocus() on InputHWND, and that will fail with E_INVALIDARG + // since that HWND remains minimized. Work around by ignoring this error here. Sine the app + // is minimized, focus state is not relevant - the next (successful) attempt to restrore the app + // will set focus into WV2/CWV2 correctly. if (e.code().value != E_INVALIDARG) { throw; From ecf5c7b9a37e882250f323e36a9caa94831a9a82 Mon Sep 17 00:00:00 2001 From: Dmitriy Komin Date: Wed, 2 Feb 2022 11:49:58 -0800 Subject: [PATCH 3/3] CR Feedback --- dev/WebView2/WebView2.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dev/WebView2/WebView2.cpp b/dev/WebView2/WebView2.cpp index 155a68faad..8c30b788c9 100644 --- a/dev/WebView2/WebView2.cpp +++ b/dev/WebView2/WebView2.cpp @@ -1259,17 +1259,17 @@ void WebView2::HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, c { if (m_coreWebView && m_xamlFocusChangeInfo.m_isPending) { - m_webHasFocus = true; try { m_coreWebViewController.MoveFocus(m_xamlFocusChangeInfo.m_storedMoveFocusReason); + m_webHasFocus = true; } catch (winrt::hresult_error e) { // Occasionally, a request to restore the minimized window does not complete. This triggers // FocusManager to set Xaml Focus to WV2 and consequently into CWV2 MoveFocus() call above, // which in turn will attempt ::SetFocus() on InputHWND, and that will fail with E_INVALIDARG - // since that HWND remains minimized. Work around by ignoring this error here. Sine the app + // since that HWND remains minimized. Work around by ignoring this error here. Since the app // is minimized, focus state is not relevant - the next (successful) attempt to restrore the app // will set focus into WV2/CWV2 correctly. if (e.code().value != E_INVALIDARG) @@ -1277,7 +1277,6 @@ void WebView2::HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, c throw; } } - m_xamlFocusChangeInfo.m_isPending = false; } }