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.
This commit is contained in:
rassilon
2015-03-06 02:31:05 -05:00
parent 340d671b1f
commit cc8e51dced
6 changed files with 132 additions and 19 deletions
@@ -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");
}
}
}
+2 -2
View File
@@ -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);
}
}
+5 -4
View File
@@ -495,12 +495,12 @@ namespace CefSharp.WinForms
/// <summary>
/// Set to true while handing an activating WM_ACTIVATE
/// </summary>
internal bool activating = false;
public bool activating = false;
/// <summary>
/// Set to true while handling a deactivating via WM_ACTIVATE
/// </summary>
internal bool deactivating = false;
public bool deactivating = false;
/// <summary>
/// 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);
}
@@ -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)
@@ -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)
@@ -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: