diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 70ac83208..9e6baf639 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -375,31 +375,39 @@ namespace CefSharp void ClientAdapter::OnGotFocus(CefRefPtr browser) { - auto winFormsControl = dynamic_cast((IWebBrowserInternal^)_browserControl); - if (winFormsControl != nullptr) - { - winFormsControl->OnGotFocus(); - } + IFocusHandler^ handler = _browserControl->FocusHandler; + + if (handler == nullptr) + { + return; + } + + handler->OnGotFocus(); } - bool ClientAdapter::OnSetFocus(CefRefPtr browser, FocusSource source) + bool ClientAdapter::OnSetFocus(CefRefPtr browser, FocusSource source) { - auto winFormsControl = dynamic_cast((IWebBrowserInternal^)_browserControl); - if (winFormsControl == nullptr) - { - return false; - } + IFocusHandler^ handler = _browserControl->FocusHandler; - return winFormsControl->OnSetFocus((CefFocusSource)source); + if (handler == nullptr) + { + // Allow the focus to be set by default. + return false; + } + + return handler->OnSetFocus((CefFocusSource)source); } void ClientAdapter::OnTakeFocus(CefRefPtr browser, bool next) { - auto winFormsControl = dynamic_cast((IWebBrowserInternal^)_browserControl); - if (winFormsControl != nullptr) - { - winFormsControl->OnTakeFocus(next); - } + IFocusHandler^ handler = _browserControl->FocusHandler; + + if (handler == nullptr) + { + return; + } + + handler->OnTakeFocus(next); } bool ClientAdapter::OnJSDialog(CefRefPtr browser, const CefString& origin_url, const CefString& accept_lang, diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index 3c938db12..46dba1c9f 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -176,6 +176,7 @@ namespace CefSharp.OffScreen public IKeyboardHandler KeyboardHandler { get; set; } public ILifeSpanHandler LifeSpanHandler { get; set; } public IMenuHandler MenuHandler { get; set; } + public IFocusHandler FocusHandler { get; set; } public IRequestHandler RequestHandler { get; set; } public event EventHandler LoadError; @@ -451,11 +452,6 @@ namespace CefSharp.OffScreen } } - void IWebBrowserInternal.OnTakeFocus(bool next) - { - throw new NotImplementedException(); - } - void IWebBrowserInternal.OnStatusMessage(string value) { var handler = StatusMessage; diff --git a/CefSharp.WinForms.Example/BrowserTabUserControl.cs b/CefSharp.WinForms.Example/BrowserTabUserControl.cs index 34568c639..4698f17e2 100644 --- a/CefSharp.WinForms.Example/BrowserTabUserControl.cs +++ b/CefSharp.WinForms.Example/BrowserTabUserControl.cs @@ -24,6 +24,7 @@ namespace CefSharp.WinForms.Example Browser = browser; browser.MenuHandler = new MenuHandler(); + browser.FocusHandler = new FocusHandler(browser, urlTextBox); browser.NavStateChanged += OnBrowserNavStateChanged; browser.ConsoleMessage += OnBrowserConsoleMessage; browser.TitleChanged += OnBrowserTitleChanged; diff --git a/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj b/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj index 7f85586d6..23ad8ebd8 100644 --- a/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj +++ b/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj @@ -70,6 +70,7 @@ BrowserTabUserControl.cs + Form diff --git a/CefSharp.WinForms.Example/FocusHandler.cs b/CefSharp.WinForms.Example/FocusHandler.cs new file mode 100644 index 000000000..95fc004ac --- /dev/null +++ b/CefSharp.WinForms.Example/FocusHandler.cs @@ -0,0 +1,41 @@ +// Copyright © 2010-2014 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. + +using System.Windows.Forms; +namespace CefSharp.WinForms.Example +{ + internal class FocusHandler : IFocusHandler + { + private ChromiumWebBrowser browser; + private ToolStripTextBox urlTextBox; + + public FocusHandler(ChromiumWebBrowser browser, ToolStripTextBox urlTextBox) + { + this.browser = browser; + this.urlTextBox = urlTextBox; + } + + public void OnGotFocus() + { + // Fired when Chromium finally receives focus. + } + + public bool OnSetFocus(CefFocusSource source) + { + // Returning false means that we allow Chromium to take the focus away from our WinForms control. + // You could also return true to keep focus in the address bar. + return false; + } + + public void OnTakeFocus(bool next) + { + // Fired when Chromium is giving up focus because the user + // pressed Tab on the last link/field in the HTML document (in which case 'next' is true) + // or because they pressed Shift+Tab on the first link/field (in which case 'next' is false). + + // Here we always focus on the address bar. + urlTextBox.Focus(); + } + } +} \ No newline at end of file diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index f45e7c5c3..0eb8cb50c 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -26,6 +26,7 @@ namespace CefSharp.WinForms public IDownloadHandler DownloadHandler { get; set; } public ILifeSpanHandler LifeSpanHandler { get; set; } public IMenuHandler MenuHandler { get; set; } + public IFocusHandler FocusHandler { get; set; } public bool CanGoForward { get; private set; } public bool CanGoBack { get; private set; } @@ -208,21 +209,6 @@ namespace CefSharp.WinForms } } - void IWinFormsWebBrowser.OnGotFocus() - { - - } - - bool IWinFormsWebBrowser.OnSetFocus(CefFocusSource source) - { - return false; - } - - void IWinFormsWebBrowser.OnTakeFocus(bool next) - { - SelectNextControl(this, next, true, true, true); - } - void IWebBrowserInternal.OnConsoleMessage(string message, string source, int line) { var handler = ConsoleMessage; diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 08464e79f..a12b2444b 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -51,6 +51,7 @@ namespace CefSharp.Wpf public IDownloadHandler DownloadHandler { get; set; } public ILifeSpanHandler LifeSpanHandler { get; set; } public IMenuHandler MenuHandler { get; set; } + public IFocusHandler FocusHandler { get; set; } public event EventHandler ConsoleMessage; public event EventHandler StatusMessage; diff --git a/CefSharp/CefFocusSource.cs b/CefSharp/CefFocusSource.cs index 9855c4348..2d40d503a 100644 --- a/CefSharp/CefFocusSource.cs +++ b/CefSharp/CefFocusSource.cs @@ -9,13 +9,13 @@ namespace CefSharp /// public enum CefFocusSource { - /// - // The source is explicit navigation via the API (LoadURL(), etc). - /// + /// + /// The source is explicit navigation via the API (LoadURL(), etc). + /// FocusSourceNavigation = 0, - /// - // The source is a system-generated focus event. - /// + /// + /// The source is a system-generated focus event. + /// FocusSourceSystem } } diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index e1e6a90e4..2e22f4a10 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -89,6 +89,7 @@ + diff --git a/CefSharp/IFocusHandler.cs b/CefSharp/IFocusHandler.cs new file mode 100644 index 000000000..2eaa183ea --- /dev/null +++ b/CefSharp/IFocusHandler.cs @@ -0,0 +1,29 @@ +// Copyright © 2010-2014 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 +{ + public interface IFocusHandler + { + /// + /// Called when the browser component has received focus. + /// + void OnGotFocus(); + + /// + /// Called when the browser component is requesting focus. + /// + /// Indicates where the focus request is originating from. + /// Return false to allow the focus to be set or true to cancel setting the focus. + bool OnSetFocus(CefFocusSource source); + + /// + /// Called when the browser component is about to lose focus. + /// For instance, if focus was on the last HTML element and the user pressed the TAB key. + /// + /// Will be true if the browser is giving focus to the next component + /// and false if the browser is giving focus to the previous component. + void OnTakeFocus(bool next); + } +} diff --git a/CefSharp/IWebBrowser.cs b/CefSharp/IWebBrowser.cs index 4594cf7f2..a43a8c3f9 100644 --- a/CefSharp/IWebBrowser.cs +++ b/CefSharp/IWebBrowser.cs @@ -110,6 +110,11 @@ namespace CefSharp /// IMenuHandler MenuHandler { get; set; } + /// + /// Implement and assign to handle events related to the browser component's focus + /// + IFocusHandler FocusHandler { get; set; } + /// /// A flag that indicates whether the WebBrowser is initialized (true) or not (false). /// diff --git a/CefSharp/IWinFormsWebBrowser.cs b/CefSharp/IWinFormsWebBrowser.cs index f579bd620..322efb48c 100644 --- a/CefSharp/IWinFormsWebBrowser.cs +++ b/CefSharp/IWinFormsWebBrowser.cs @@ -20,9 +20,5 @@ namespace CefSharp void Paste(); void Delete(); void SelectAll(); - - void OnGotFocus(); - bool OnSetFocus(CefFocusSource source); - void OnTakeFocus(bool next); } }