Skip to content

Commit 12a0045

Browse files
committed
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.
1 parent af593f2 commit 12a0045

File tree

6 files changed

+52
-5
lines changed

6 files changed

+52
-5
lines changed

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ protected virtual void Dispose(bool disposing)
286286

287287
// Release reference to handlers, except LifeSpanHandler which is done after Disposing
288288
// ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked.
289-
FreeHandlersExceptLifeSpan();
289+
// We also leave FocusHandler and override with a NoFocusHandler implementation as
290+
// it so we can block taking Focus (we're dispoing afterall). Issue #3715
291+
FreeHandlersExceptLifeSpanAndFocus();
292+
293+
FocusHandler = new NoFocusHandler();
290294

291295
browser = null;
292296

CefSharp.WinForms/ChromiumWebBrowser.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,11 @@ private void InternalDispose(bool disposing)
417417

418418
// Release reference to handlers, except LifeSpanHandler which is done after Disposing
419419
// ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked.
420-
FreeHandlersExceptLifeSpan();
420+
// We also leave FocusHandler and override with a NoFocusHandler implementation as
421+
// it so we can block taking Focus (we're dispoing afterall). Issue #3715
422+
FreeHandlersExceptLifeSpanAndFocus();
423+
424+
FocusHandler = new NoFocusHandler();
421425

422426
browser = null;
423427

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,11 @@ private void InternalDispose(bool disposing)
667667

668668
// Release reference to handlers, except LifeSpanHandler which is done after Disposing
669669
// ManagedCefBrowserAdapter otherwise the ILifeSpanHandler.DoClose will not be invoked.
670-
this.FreeHandlersExceptLifeSpan();
670+
// We also leave FocusHandler and override with a NoFocusHandler implementation as
671+
// it so we can block taking Focus (we're dispoing afterall). Issue #3715
672+
FreeHandlersExceptLifeSpanAndFocus();
673+
674+
FocusHandler = new NoFocusHandler();
671675

672676
browser = null;
673677

CefSharp/CefSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
<Compile Include="Internals\FreezableBase.cs" />
128128
<Compile Include="Internals\IBrowserRefCounter.cs" />
129129
<Compile Include="Internals\MimeTypeMapping.cs" />
130+
<Compile Include="Internals\NoFocusHandler.cs" />
130131
<Compile Include="Internals\Tasks\SyncContextTaskCompletionSource.cs" />
131132
<Compile Include="IRegistration.cs" />
132133
<Compile Include="LoadUrlAsyncResponse.cs" />
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright © 2021 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
namespace CefSharp.Internals
6+
{
7+
/// <summary>
8+
/// NoFocusHandler - Used when disposing of the ChromiumWebBrowser controls
9+
/// Doesn't take focus for the main browser (leaves default behaviour for popup).
10+
/// OnGotFocus and OnTakeFocus are both noops.
11+
/// </summary>
12+
public class NoFocusHandler : IFocusHandler
13+
{
14+
void IFocusHandler.OnGotFocus(IWebBrowser chromiumWebBrowser, IBrowser browser)
15+
{
16+
//NOOP
17+
}
18+
19+
bool IFocusHandler.OnSetFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFocusSource source)
20+
{
21+
if(browser.IsPopup)
22+
{
23+
return false;
24+
}
25+
26+
//Don't take focus
27+
return true;
28+
}
29+
30+
void IFocusHandler.OnTakeFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, bool next)
31+
{
32+
//NOOP
33+
}
34+
}
35+
}

CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public Task<LoadUrlAsyncResponse> LoadUrlAsync(string url = null, Synchronizatio
304304
/// Sets the handler references to null.
305305
/// Where required also calls Dispose().
306306
/// </summary>
307-
private void FreeHandlersExceptLifeSpan()
307+
private void FreeHandlersExceptLifeSpanAndFocus()
308308
{
309309
AudioHandler?.Dispose();
310310
AudioHandler = null;
@@ -318,7 +318,6 @@ private void FreeHandlersExceptLifeSpan()
318318
DragHandler = null;
319319
DownloadHandler = null;
320320
MenuHandler = null;
321-
FocusHandler = null;
322321
ResourceRequestHandlerFactory = null;
323322
RenderProcessMessageHandler = null;
324323
}

0 commit comments

Comments
 (0)