WinForms/WPF/OffScreen - Use custom FocusHandler when disposing to block taking focus

This was added as a workaround for Issue #3715 which is specific to WinForms
and will hopefully be fixed upstream.

I've added the logic to all three implementations as I think the behaviour should be
consistent across all implementations.
This commit is contained in:
Alex Maitland
2021-07-27 13:13:17 +10:00
parent af593f214c
commit 12a0045dc8
6 changed files with 52 additions and 5 deletions
+5 -1
View File
@@ -286,7 +286,11 @@ namespace CefSharp.OffScreen
// Release reference to handlers, except LifeSpanHandler which is done after Disposing
// ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked.
FreeHandlersExceptLifeSpan();
// We also leave FocusHandler and override with a NoFocusHandler implementation as
// it so we can block taking Focus (we're dispoing afterall). Issue #3715
FreeHandlersExceptLifeSpanAndFocus();
FocusHandler = new NoFocusHandler();
browser = null;
+5 -1
View File
@@ -417,7 +417,11 @@ namespace CefSharp.WinForms
// Release reference to handlers, except LifeSpanHandler which is done after Disposing
// ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked.
FreeHandlersExceptLifeSpan();
// We also leave FocusHandler and override with a NoFocusHandler implementation as
// it so we can block taking Focus (we're dispoing afterall). Issue #3715
FreeHandlersExceptLifeSpanAndFocus();
FocusHandler = new NoFocusHandler();
browser = null;
+5 -1
View File
@@ -667,7 +667,11 @@ namespace CefSharp.Wpf
// Release reference to handlers, except LifeSpanHandler which is done after Disposing
// ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked.
this.FreeHandlersExceptLifeSpan();
// We also leave FocusHandler and override with a NoFocusHandler implementation as
// it so we can block taking Focus (we're dispoing afterall). Issue #3715
FreeHandlersExceptLifeSpanAndFocus();
FocusHandler = new NoFocusHandler();
browser = null;
+1
View File
@@ -127,6 +127,7 @@
<Compile Include="Internals\FreezableBase.cs" />
<Compile Include="Internals\IBrowserRefCounter.cs" />
<Compile Include="Internals\MimeTypeMapping.cs" />
<Compile Include="Internals\NoFocusHandler.cs" />
<Compile Include="Internals\Tasks\SyncContextTaskCompletionSource.cs" />
<Compile Include="IRegistration.cs" />
<Compile Include="LoadUrlAsyncResponse.cs" />
+35
View File
@@ -0,0 +1,35 @@
// Copyright © 2021 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
namespace CefSharp.Internals
{
/// <summary>
/// NoFocusHandler - Used when disposing of the ChromiumWebBrowser controls
/// Doesn't take focus for the main browser (leaves default behaviour for popup).
/// OnGotFocus and OnTakeFocus are both noops.
/// </summary>
public class NoFocusHandler : IFocusHandler
{
void IFocusHandler.OnGotFocus(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
//NOOP
}
bool IFocusHandler.OnSetFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFocusSource source)
{
if(browser.IsPopup)
{
return false;
}
//Don't take focus
return true;
}
void IFocusHandler.OnTakeFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, bool next)
{
//NOOP
}
}
}
@@ -304,7 +304,7 @@ namespace CefSharp.WinForms
/// Sets the handler references to null.
/// Where required also calls Dispose().
/// </summary>
private void FreeHandlersExceptLifeSpan()
private void FreeHandlersExceptLifeSpanAndFocus()
{
AudioHandler?.Dispose();
AudioHandler = null;
@@ -318,7 +318,6 @@ namespace CefSharp.WinForms
DragHandler = null;
DownloadHandler = null;
MenuHandler = null;
FocusHandler = null;
ResourceRequestHandlerFactory = null;
RenderProcessMessageHandler = null;
}