From cc8e51dcedcd2ac23abe524d39a8876ebd56b523 Mon Sep 17 00:00:00 2001 From: rassilon Date: Fri, 6 Mar 2015 02:31:05 -0500 Subject: [PATCH] Don't .Activate ChromiumWebBrowser during activation if the ChromiumWebBrowser was the .ActiveControl before the app was deactivated. This will allow WinForm's .ActiveControl logic to occur properly. Update address bar logic to deal with BackColor being in yellow whenever the control has focus. --- .../Minimal/TabulationDemoForm.cs | 39 ++++++++++++- CefSharp.WinForms.Example/Program.cs | 4 +- CefSharp.WinForms/ChromiumWebBrowser.cs | 9 +-- .../Internals/ControlExtensions.cs | 19 ++++++- .../Internals/DefaultFocusHandler.cs | 56 +++++++++++++++++-- .../Internals/ParentFormMessageInterceptor.cs | 24 +++++++- 6 files changed, 132 insertions(+), 19 deletions(-) diff --git a/CefSharp.WinForms.Example/Minimal/TabulationDemoForm.cs b/CefSharp.WinForms.Example/Minimal/TabulationDemoForm.cs index e83eee2ce..9c36ca62f 100644 --- a/CefSharp.WinForms.Example/Minimal/TabulationDemoForm.cs +++ b/CefSharp.WinForms.Example/Minimal/TabulationDemoForm.cs @@ -4,6 +4,7 @@ using CefSharp.Internals; using System; +using System.Diagnostics; using System.Drawing; using System.Windows.Forms; @@ -11,7 +12,9 @@ namespace CefSharp.WinForms.Example.Minimal { public partial class TabulationDemoForm : Form { - private readonly ChromiumWebBrowser chromiumWebBrowser; + private readonly ChromiumWebBrowser chromiumWebBrowser; + private Color focusColor = Color.Yellow; + private Color nonFocusColor = Color.White; public TabulationDemoForm() { @@ -21,9 +24,35 @@ namespace CefSharp.WinForms.Example.Minimal userControl.Enter += userControl_Enter; userControl.Leave += userControl_Leave; userControl.Controls.Add(chromiumWebBrowser); + txtURL.GotFocus += TxtURLGotFocus; + txtURL.LostFocus += TxtUrlLostFocus; grpBrowser.Controls.Add(userControl); } + private void TxtUrlLostFocus(object sender, EventArgs e) + { + // Uncomment this if you want the address bar to go white + // during deactivation: + //UpdateUrlColor(nonFocusColor); + Kernel32.OutputDebugString("URLLostFocus: Deactivating: " + this.chromiumWebBrowser.deactivating.ToString() + " \r\n"); + } + + void TxtURLGotFocus(object sender, EventArgs e) + { + // Ensure the control turns yellow on form + // activation (since Enter events don't fire then) + UpdateUrlColor(focusColor); + Kernel32.OutputDebugString("URLGotFocus: Activating: " + this.chromiumWebBrowser.activating.ToString() + " \r\n"); + } + + private void UpdateUrlColor(Color color) + { + if (txtURL.BackColor != color) + { + txtURL.BackColor = color; + } + } + void userControl_Leave(object sender, EventArgs e) { txtDummy.Text = "UserControl OnLeave"; @@ -41,12 +70,14 @@ namespace CefSharp.WinForms.Example.Minimal private void TxtUrlLeave(object sender, EventArgs e) { - txtURL.BackColor = Color.White; + UpdateUrlColor(nonFocusColor); + Kernel32.OutputDebugString("UrlLeave: Deactivating: " + this.chromiumWebBrowser.deactivating + "\r\n"); } private void TxtUrlEnter(object sender, EventArgs e) { - txtURL.BackColor = Color.Yellow; + UpdateUrlColor(focusColor); + Debug.WriteLine("UrlEnter Activating: " + this.chromiumWebBrowser.activating + "\r\n"); } protected override void OnActivated(EventArgs e) @@ -56,6 +87,7 @@ namespace CefSharp.WinForms.Example.Minimal chromiumWebBrowser.Handle, ActiveControl != null ? ActiveControl.GetType().FullName : "null")); base.OnActivated(e); + Kernel32.OutputDebugString("Form OnActivated End\r\n"); } protected override void OnDeactivate(EventArgs e) @@ -65,6 +97,7 @@ namespace CefSharp.WinForms.Example.Minimal chromiumWebBrowser.Handle, ActiveControl != null ? ActiveControl.GetType().FullName : "null")); base.OnDeactivate(e); + Kernel32.OutputDebugString("Form OnDeActivated End\r\n"); } } } diff --git a/CefSharp.WinForms.Example/Program.cs b/CefSharp.WinForms.Example/Program.cs index 1e6ea6b47..265a71342 100644 --- a/CefSharp.WinForms.Example/Program.cs +++ b/CefSharp.WinForms.Example/Program.cs @@ -16,9 +16,9 @@ namespace CefSharp.WinForms.Example { CefExample.Init(); - var browser = new BrowserForm(); + //var browser = new BrowserForm(); //var browser = new SimpleBrowserForm(); - //var browser = new TabulationDemoForm(); + var browser = new TabulationDemoForm(); Application.Run(browser); } } diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index e81669023..d6c72bff6 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -495,12 +495,12 @@ namespace CefSharp.WinForms /// /// Set to true while handing an activating WM_ACTIVATE /// - internal bool activating = false; + public bool activating = false; /// /// Set to true while handling a deactivating via WM_ACTIVATE /// - internal bool deactivating = false; + public bool deactivating = false; /// /// Set to false during owning Form activation to @@ -513,9 +513,10 @@ namespace CefSharp.WinForms //// NOTE: Form OnActivated event happens AFTER this gets called for the alt-tab back //// case., but no OnEnter/or OnLeave fired. //// NOTE: Form activation from minimize occurs AFTER OnActivated. - Kernel32.OutputDebugString(String.Format("OnGotFocus for: {0}\r\n", Handle)); + Kernel32.OutputDebugString(String.Format("OnGotFocus Browser for: {0}\r\n", Handle)); Kernel32.OutputDebugString(String.Format("isActivating: {0}\r\n", activating)); Kernel32.OutputDebugString(String.Format("isFormDeactivated: {0}\r\n", isFormDeactivated)); + Kernel32.OutputDebugString(String.Format("Form.ActiveControl == this: " + this.IsActiveControl() + "\r\n")); //// If we're either processing the consequences of WM_ACTIVATE //// or we're receiving focus while the top level window is @@ -536,7 +537,7 @@ namespace CefSharp.WinForms protected override void OnLostFocus(EventArgs e) { - Kernel32.OutputDebugString(String.Format("OnLostFocus for: {0}\r\n", Handle)); + Kernel32.OutputDebugString(String.Format("OnLostFocus Browser for: {0}\r\n", Handle)); Kernel32.OutputDebugString(String.Format("isActivating: {0}\r\n", activating)); base.OnLostFocus(e); } diff --git a/CefSharp.WinForms/Internals/ControlExtensions.cs b/CefSharp.WinForms/Internals/ControlExtensions.cs index 0fba09a4f..0abf3e8d9 100644 --- a/CefSharp.WinForms/Internals/ControlExtensions.cs +++ b/CefSharp.WinForms/Internals/ControlExtensions.cs @@ -26,14 +26,29 @@ namespace CefSharp.WinForms.Internals } } - public static void Activate(this Control control) + public static bool Activate(this Control control) { // Notify WinForms world that inner browser window got focus. This will trigger Leave event to previous focused control var containerControl = control.GetContainerControl(); if (containerControl != null) { - containerControl.ActivateControl(control); + return containerControl.ActivateControl(control); } + return false; + } + + public static bool IsActiveControl(this Control control) + { + Control activeControl = control.FindForm().ActiveControl; + ContainerControl containerControl; + while (activeControl != null + && (activeControl as ContainerControl != null) + && !Object.ReferenceEquals(control, activeControl)) + { + containerControl = activeControl as ContainerControl; + activeControl = containerControl.ActiveControl; + } + return Object.ReferenceEquals(control, activeControl); } public static void SelectNextControl(this Control control, bool next) diff --git a/CefSharp.WinForms/Internals/DefaultFocusHandler.cs b/CefSharp.WinForms/Internals/DefaultFocusHandler.cs index 1577ef912..9d8307a4b 100644 --- a/CefSharp.WinForms/Internals/DefaultFocusHandler.cs +++ b/CefSharp.WinForms/Internals/DefaultFocusHandler.cs @@ -18,13 +18,57 @@ namespace CefSharp.WinForms.Internals public virtual void OnGotFocus() { - browser.InvokeOnUiThreadIfRequired(() => + // During application activation, CEF receives a WM_SETFOCUS + // message from Windows because it is the top window + // on the CEF UI thread. + // + // If the WinForm ChromiumWebBrowser control is the + // current .ActiveControl before app activation + // then we MUST NOT try to reactivate the WinForm + // control during activation because that will + // start a race condition between reactivating + // the CEF control AND having another control + // that should be the new .ActiveControl. + // + // For example: + // * CEF control has focus, and thus ChromiumWebBrowser + // is the current .ActiveControl + // * Alt-Tab to another application + // * Click a non CEF control in the WinForms application. + // * This begins the Windows activation process. + // * The WM_ACTIVATE process on the WinForm UI thread + // will update .ActiveControl to the clicked control. + // The clicked control will receive WM_SETFOCUS as well. + // (i.e. OnGotFocus) + // If the ChromiumWebBrowser was the previous .ActiveControl, + // then we set .Activating = true. + // * The WM_ACTIVATE process on the CEF thread will + // send WM_SETFOCUS to CEF thus staring the race of + // which will end first, the WndProc WM_ACTIVATE process + // on the WinForm UI thread or the WM_ACTIVATE process + // on the CEF UI thread. + // * CEF will then call this method on the CEF UI thread + // due to WM_SETFOCUS. + // * This method will clear the activation state (if any) + // on the ChromiumWebBrowser control, due to the race + // condition the WinForm UI thread cannot. + if (browser.activating) { - browser.Activate(); - Kernel32.OutputDebugString("POST .Activate()\r\n"); - Kernel32.OutputDebugString(String.Format("CCActiveControlType: {0}\r\n", ((IContainerControl)browser.Parent).ActiveControl.GetType().FullName)); - Kernel32.OutputDebugString(String.Format("CCActiveControlType: {0}\r\n", ((IContainerControl)browser.FindForm()).ActiveControl.GetType().FullName)); - }); + browser.activating = false; + Kernel32.OutputDebugString("BrowserOnGotFocus: during activate, doing nothing, WinForms .ActiveControl processing will correct if necessary.\r\n"); + } + else + { + Kernel32.OutputDebugString("DFH: Queuing .Activate() call\r\n"); + browser.InvokeOnUiThreadIfRequired(() => + { + Kernel32.OutputDebugString("Before browser.Activate()\r\n"); + Kernel32.OutputDebugString("Activate result: " + browser.Activate().ToString() + "\r\n"); + Kernel32.OutputDebugString("POST .Activate()\r\n"); + Kernel32.OutputDebugString(String.Format("CCActiveControlType: {0}\r\n", ((IContainerControl)browser.Parent).ActiveControl.GetType().FullName)); + Kernel32.OutputDebugString(String.Format("CCActiveControlType: {0}\r\n", ((IContainerControl)browser.FindForm()).ActiveControl.GetType().FullName)); + }); + } } public virtual bool OnSetFocus(CefFocusSource source) diff --git a/CefSharp.WinForms/Internals/ParentFormMessageInterceptor.cs b/CefSharp.WinForms/Internals/ParentFormMessageInterceptor.cs index ba21b826b..e897a34ec 100644 --- a/CefSharp.WinForms/Internals/ParentFormMessageInterceptor.cs +++ b/CefSharp.WinForms/Internals/ParentFormMessageInterceptor.cs @@ -2,7 +2,9 @@ // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. +using CefSharp.Internals; using System; +using System.Diagnostics; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; @@ -116,9 +118,14 @@ namespace CefSharp.WinForms.Internals // we won't get a call to OnLostFocus on ChromiumWebBrowser. // However, it doesn't matter so much since the CEF // browser will receive it instead. + + // Paranoia about making sure the state is correct now. + browser.activating = false; browser.deactivating = true; + Kernel32.OutputDebugString("Begin DeActivating WndProc\r\n"); DefWndProc(ref m); browser.deactivating = false; + Kernel32.OutputDebugString("End DeActivating WndProc\r\n"); browser.isFormDeactivated = true; } else // WA_ACTIVE or WA_CLICKACTIVE @@ -126,9 +133,22 @@ namespace CefSharp.WinForms.Internals // NOTE: Transition from minimized to normal // Doesn't call OnGetFocus until AFTER OnActivated completes. browser.isFormDeactivated = false; - browser.activating = true; + Kernel32.OutputDebugString("Begin Activating WndProc\r\n"); + // Only set activating if the ChromiumWebBrowser was the form's + // ActiveControl before the last deactivation. + browser.activating = browser.IsActiveControl(); DefWndProc(ref m); - browser.activating = false; + // During activation, WM_SETFOCUS gets sent to + // to the CEF control since it's the root window + // of the CEF UI thread. + // + // Therefore, don't set .activating to false here + // instead do so in DefaultFocusHandler.OnGotFocus. + // Otherwise there's a race condition between this + // thread setting activating to false and + // the CEF DefaultFocusHandler executing to determine + // it shouldn't Activate() the control. + Kernel32.OutputDebugString("End Activating WndProc\r\n"); } return; case NativeMethods.WM_MOVING: