|
| 1 | +// Copyright © 2019 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 | +using CefSharp.WinForms.Internals; |
| 6 | + |
| 7 | +namespace CefSharp.WinForms.Example.Handlers |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Implementation of <see cref="IFocusHandler"/> that shows a workaround |
| 11 | + /// for https://github.com/cefsharp/CefSharp/issues/2928 |
| 12 | + /// This version is based on the old behaviour seen in version 71 etc |
| 13 | + /// </summary> |
| 14 | + /// <seealso cref="CefSharp.IFocusHandler" /> |
| 15 | + public class MultiFormFocusHandler : IFocusHandler |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Called when the browser component has received focus. |
| 19 | + /// </summary> |
| 20 | + /// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param> |
| 21 | + /// <param name="browser">the browser object</param> |
| 22 | + /// <remarks>Try to avoid needing to override this logic in a subclass. The implementation in |
| 23 | + /// DefaultFocusHandler relies on very detailed behavior of how WinForms and |
| 24 | + /// Windows interact during window activation.</remarks> |
| 25 | + void IFocusHandler.OnGotFocus(IWebBrowser chromiumWebBrowser, IBrowser browser) |
| 26 | + { |
| 27 | + //We don't deal with popups as they're rendered by default entirely by CEF |
| 28 | + //For print dialogs the browser will be null, we don't want to deal with that either. |
| 29 | + if (browser == null || browser.IsPopup) |
| 30 | + { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + var winFormsChromiumWebBrowser = (ChromiumWebBrowser)chromiumWebBrowser; |
| 35 | + // During application activation, CEF receives a WM_SETFOCUS |
| 36 | + // message from Windows because it is the top window |
| 37 | + // on the CEF UI thread. |
| 38 | + // |
| 39 | + // If the WinForm ChromiumWebBrowser control is the |
| 40 | + // current .ActiveControl before app activation |
| 41 | + // then we MUST NOT try to reactivate the WinForm |
| 42 | + // control during activation because that will |
| 43 | + // start a race condition between reactivating |
| 44 | + // the CEF control AND having another control |
| 45 | + // that should be the new .ActiveControl. |
| 46 | + // |
| 47 | + // For example: |
| 48 | + // * CEF control has focus, and thus ChromiumWebBrowser |
| 49 | + // is the current .ActiveControl |
| 50 | + // * Alt-Tab to another application |
| 51 | + // * Click a non CEF control in the WinForms application. |
| 52 | + // * This begins the Windows activation process. |
| 53 | + // * The WM_ACTIVATE process on the WinForm UI thread |
| 54 | + // will update .ActiveControl to the clicked control. |
| 55 | + // The clicked control will receive WM_SETFOCUS as well. |
| 56 | + // (i.e. OnGotFocus) |
| 57 | + // If the ChromiumWebBrowser was the previous .ActiveControl, |
| 58 | + // then we set .Activating = true. |
| 59 | + // * The WM_ACTIVATE process on the CEF thread will |
| 60 | + // send WM_SETFOCUS to CEF thus staring the race of |
| 61 | + // which will end first, the WndProc WM_ACTIVATE process |
| 62 | + // on the WinForm UI thread or the WM_ACTIVATE process |
| 63 | + // on the CEF UI thread. |
| 64 | + // * CEF will then call this method on the CEF UI thread |
| 65 | + // due to WM_SETFOCUS. |
| 66 | + // * This method will clear the activation state (if any) |
| 67 | + // on the ChromiumWebBrowser control, due to the race |
| 68 | + // condition the WinForm UI thread cannot. |
| 69 | + if (winFormsChromiumWebBrowser.IsActivating) |
| 70 | + { |
| 71 | + winFormsChromiumWebBrowser.IsActivating = false; |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + // Otherwise, we're not being activated |
| 76 | + // so we must activate the ChromiumWebBrowser control |
| 77 | + // for WinForms focus tracking. |
| 78 | + winFormsChromiumWebBrowser.InvokeOnUiThreadIfRequired(() => |
| 79 | + { |
| 80 | + winFormsChromiumWebBrowser.Activate(); |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Called when the browser component is requesting focus. |
| 87 | + /// </summary> |
| 88 | + /// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param> |
| 89 | + /// <param name="browser">the browser object</param> |
| 90 | + /// <param name="source">Indicates where the focus request is originating from.</param> |
| 91 | + /// <returns>Return false to allow the focus to be set or true to cancel setting the focus.</returns> |
| 92 | + bool IFocusHandler.OnSetFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFocusSource source) |
| 93 | + { |
| 94 | + //We don't deal with popups as they're rendered by default entirely by CEF |
| 95 | + if (browser.IsPopup) |
| 96 | + { |
| 97 | + return false; |
| 98 | + } |
| 99 | + // Do not let the browser take focus when a Load method has been called |
| 100 | + return source == CefFocusSource.FocusSourceNavigation; |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Called when the browser component is about to lose focus. |
| 105 | + /// For instance, if focus was on the last HTML element and the user pressed the TAB key. |
| 106 | + /// </summary> |
| 107 | + /// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param> |
| 108 | + /// <param name="browser">the browser object</param> |
| 109 | + /// <param name="next">Will be true if the browser is giving focus to the next component |
| 110 | + /// and false if the browser is giving focus to the previous component.</param> |
| 111 | + void IFocusHandler.OnTakeFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, bool next) |
| 112 | + { |
| 113 | + //We don't deal with popups as they're rendered by default entirely by CEF |
| 114 | + if (browser.IsPopup) |
| 115 | + { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + var winFormsChromiumWebBrowser = (ChromiumWebBrowser)chromiumWebBrowser; |
| 120 | + |
| 121 | + // NOTE: OnTakeFocus means leaving focus / not taking focus |
| 122 | + winFormsChromiumWebBrowser.InvokeOnUiThreadIfRequired(() => winFormsChromiumWebBrowser.SelectNextControl(next)); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
0 commit comments