From 12a0045dc8f1da565832c968de10ee9749722cf9 Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Tue, 27 Jul 2021 13:13:17 +1000 Subject: [PATCH] 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. --- CefSharp.OffScreen/ChromiumWebBrowser.cs | 6 +++- CefSharp.WinForms/ChromiumWebBrowser.cs | 6 +++- CefSharp.Wpf/ChromiumWebBrowser.cs | 6 +++- CefSharp/CefSharp.csproj | 1 + CefSharp/Internals/NoFocusHandler.cs | 35 +++++++++++++++++++ .../Partial/ChromiumWebBrowser.Partial.cs | 3 +- 6 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 CefSharp/Internals/NoFocusHandler.cs diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index 57a0d723f..2f5cda30d 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -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; diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 8c31d1dbf..da22d354e 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -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; diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index b1b194847..805925777 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -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; diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 138c24cbf..3a7eb3ba9 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -127,6 +127,7 @@ + diff --git a/CefSharp/Internals/NoFocusHandler.cs b/CefSharp/Internals/NoFocusHandler.cs new file mode 100644 index 000000000..de7aff021 --- /dev/null +++ b/CefSharp/Internals/NoFocusHandler.cs @@ -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 +{ + /// + /// 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. + /// + 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 + } + } +} diff --git a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs index 624820ae9..4dc2ef99a 100644 --- a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs +++ b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs @@ -304,7 +304,7 @@ namespace CefSharp.WinForms /// Sets the handler references to null. /// Where required also calls Dispose(). /// - 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; }