From f5072163119d5bbd999dfff31355bfe844a48cfa Mon Sep 17 00:00:00 2001 From: amaitland Date: Tue, 29 Apr 2014 11:51:56 +1000 Subject: [PATCH 1/8] Add OnGotFocus and OnSetFocus Add CefFocusSource enum for mapping from cef_focus_source_t enum --- CefSharp.Core/Internals/ClientAdapter.cpp | 10 ++++++++++ CefSharp.Core/Internals/ClientAdapter.h | 2 ++ CefSharp.WinForms/ChromiumWebBrowser.cs | 10 ++++++++++ CefSharp.Wpf/ChromiumWebBrowser.cs | 12 +++++++++++- CefSharp/CefFocusSource.cs | 17 +++++++++++++++++ CefSharp/CefSharp.csproj | 1 + CefSharp/Internals/IWebBrowserInternal.cs | 2 ++ 7 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 CefSharp/CefFocusSource.cs diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 743240886f..0dac3f2f97 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -394,6 +394,16 @@ namespace CefSharp } } + void ClientAdapter::OnGotFocus(CefRefPtr browser) + { + _browserControl->OnGotFocus(); + } + + bool ClientAdapter::OnSetFocus(CefRefPtr browser, FocusSource source) + { + return _browserControl->OnSetFocus((CefFocusSource)source); + } + void ClientAdapter::OnTakeFocus(CefRefPtr browser, bool next) { _browserControl->OnTakeFocus(next); diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index a4964919a0..5d529fd495 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -103,6 +103,8 @@ namespace CefSharp CefRefPtr params, CefRefPtr model) OVERRIDE; // CefFocusHandler + virtual DECL void OnGotFocus(CefRefPtr browser) OVERRIDE; + virtual DECL bool OnSetFocus(CefRefPtr browser, FocusSource source) OVERRIDE; virtual DECL void OnTakeFocus(CefRefPtr browser, bool next) OVERRIDE; // CefKeyboardHandler diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index f84d0a8a62..1fe634511c 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -196,6 +196,16 @@ void IWebBrowserInternal.OnFrameLoadEnd(string url, bool isMainFrame, int httpSt } } + void IWebBrowserInternal. OnGotFocus() + { + + } + + void IWebBrowserInternal. OnSetFocus(CefFocusSource source) + { + return false; + } + void IWebBrowserInternal.OnTakeFocus(bool next) { SelectNextControl(this, next, true, true, true); diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 5e3ba5a175..296c493b85 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -1028,9 +1028,19 @@ void IWebBrowserInternal.OnFrameLoadEnd(string url, bool isMainFrame, int httpSt } } + void IWebBrowserInternal.OnGotFocus() + { + + } + + bool IWebBrowserInternal.OnSetFocus(CefFocusSource source) + { + return false; + } + void IWebBrowserInternal.OnTakeFocus(bool next) { - throw new NotImplementedException(); + } void IWebBrowserInternal.OnConsoleMessage(string message, string source, int line) diff --git a/CefSharp/CefFocusSource.cs b/CefSharp/CefFocusSource.cs new file mode 100644 index 0000000000..41f09ca5f3 --- /dev/null +++ b/CefSharp/CefFocusSource.cs @@ -0,0 +1,17 @@ +namespace CefSharp +{ + /// + /// Focus Source + /// + public enum CefFocusSource + { + /// + // The source is explicit navigation via the API (LoadURL(), etc). + /// + FocusSourceNavigation = 0, + /// + // The source is a system-generated focus event. + /// + FocusSourceSystem + } +} diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index efcd314246..9af97162f8 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -98,6 +98,7 @@ + diff --git a/CefSharp/Internals/IWebBrowserInternal.cs b/CefSharp/Internals/IWebBrowserInternal.cs index 4e42828ed1..465c414a2f 100644 --- a/CefSharp/Internals/IWebBrowserInternal.cs +++ b/CefSharp/Internals/IWebBrowserInternal.cs @@ -22,6 +22,8 @@ public interface IWebBrowserInternal : IWebBrowser void OnFrameLoadStart(string url, bool isMainFrame); void OnFrameLoadEnd(string url, bool isMainFrame, int httpStatusCode); + void OnGotFocus(); + bool OnSetFocus(CefFocusSource source); void OnTakeFocus(bool next); void OnConsoleMessage(string message, string source, int line); void OnLoadError(string url, CefErrorCode errorCode, string errorText); From 435910df6c3bbe16221a3b72bb3813f4d0913916 Mon Sep 17 00:00:00 2001 From: amaitland Date: Wed, 9 Jul 2014 11:23:06 +1000 Subject: [PATCH 2/8] Add license disclaimer --- CefSharp/CefFocusSource.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CefSharp/CefFocusSource.cs b/CefSharp/CefFocusSource.cs index 41f09ca5f3..9855c4348d 100644 --- a/CefSharp/CefFocusSource.cs +++ b/CefSharp/CefFocusSource.cs @@ -1,4 +1,8 @@ -namespace CefSharp +// 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 { /// /// Focus Source From df1c713555ece0f8b87fac1f62ba176592a32f26 Mon Sep 17 00:00:00 2001 From: amaitland Date: Wed, 9 Jul 2014 11:30:20 +1000 Subject: [PATCH 3/8] Fix return type and minor formatting --- CefSharp.WinForms/ChromiumWebBrowser.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 1fe634511c..9c0dd2411a 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -196,12 +196,12 @@ void IWebBrowserInternal.OnFrameLoadEnd(string url, bool isMainFrame, int httpSt } } - void IWebBrowserInternal. OnGotFocus() + void IWebBrowserInternal.OnGotFocus() { } - void IWebBrowserInternal. OnSetFocus(CefFocusSource source) + bool IWebBrowserInternal.OnSetFocus(CefFocusSource source) { return false; } From a2debed38acfd73712f02cdd4a6204dfb21fe639 Mon Sep 17 00:00:00 2001 From: amaitland Date: Wed, 9 Jul 2014 11:37:10 +1000 Subject: [PATCH 4/8] Move Focus methods from IWebBrowserInternal to IWinFormsWebBrowser and only call them in the context of the WinForms ChromiumWebBrowser --- CefSharp.Core/Internals/ClientAdapter.cpp | 20 +++++++++++++++++--- CefSharp.WinForms/ChromiumWebBrowser.cs | 6 +++--- CefSharp.Wpf/ChromiumWebBrowser.cs | 15 --------------- CefSharp/IWinFormsWebBrowser.cs | 4 ++++ CefSharp/Internals/IWebBrowserInternal.cs | 3 --- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 0dac3f2f97..b7fb5538c1 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -396,17 +396,31 @@ namespace CefSharp void ClientAdapter::OnGotFocus(CefRefPtr browser) { - _browserControl->OnGotFocus(); + auto winFormsControl = dynamic_cast((IWebBrowserInternal^)_browserControl); + if (winFormsControl != nullptr) + { + winFormsControl->OnGotFocus(); + } } bool ClientAdapter::OnSetFocus(CefRefPtr browser, FocusSource source) { - return _browserControl->OnSetFocus((CefFocusSource)source); + auto winFormsControl = dynamic_cast((IWebBrowserInternal^)_browserControl); + if (winFormsControl == nullptr) + { + return false; + } + + return winFormsControl->OnSetFocus((CefFocusSource)source); } void ClientAdapter::OnTakeFocus(CefRefPtr browser, bool next) { - _browserControl->OnTakeFocus(next); + auto winFormsControl = dynamic_cast((IWebBrowserInternal^)_browserControl); + if (winFormsControl != nullptr) + { + winFormsControl->OnTakeFocus(next); + } } bool ClientAdapter::OnJSDialog(CefRefPtr browser, const CefString& origin_url, const CefString& accept_lang, diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 9c0dd2411a..626a2402cd 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -196,17 +196,17 @@ void IWebBrowserInternal.OnFrameLoadEnd(string url, bool isMainFrame, int httpSt } } - void IWebBrowserInternal.OnGotFocus() + void IWinFormsWebBrowser.OnGotFocus() { } - bool IWebBrowserInternal.OnSetFocus(CefFocusSource source) + bool IWinFormsWebBrowser.OnSetFocus(CefFocusSource source) { return false; } - void IWebBrowserInternal.OnTakeFocus(bool next) + void IWinFormsWebBrowser.OnTakeFocus(bool next) { SelectNextControl(this, next, true, true, true); } diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 296c493b85..6317c1af34 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -1028,21 +1028,6 @@ void IWebBrowserInternal.OnFrameLoadEnd(string url, bool isMainFrame, int httpSt } } - void IWebBrowserInternal.OnGotFocus() - { - - } - - bool IWebBrowserInternal.OnSetFocus(CefFocusSource source) - { - return false; - } - - void IWebBrowserInternal.OnTakeFocus(bool next) - { - - } - void IWebBrowserInternal.OnConsoleMessage(string message, string source, int line) { var handler = ConsoleMessage; diff --git a/CefSharp/IWinFormsWebBrowser.cs b/CefSharp/IWinFormsWebBrowser.cs index 23bbeb9fa3..ac023f2026 100644 --- a/CefSharp/IWinFormsWebBrowser.cs +++ b/CefSharp/IWinFormsWebBrowser.cs @@ -9,5 +9,9 @@ namespace CefSharp public interface IWinFormsWebBrowser : IWebBrowser { IMenuHandler MenuHandler { get; set; } + + void OnGotFocus(); + bool OnSetFocus(CefFocusSource source); + void OnTakeFocus(bool next); } } diff --git a/CefSharp/Internals/IWebBrowserInternal.cs b/CefSharp/Internals/IWebBrowserInternal.cs index 465c414a2f..a183531295 100644 --- a/CefSharp/Internals/IWebBrowserInternal.cs +++ b/CefSharp/Internals/IWebBrowserInternal.cs @@ -22,9 +22,6 @@ public interface IWebBrowserInternal : IWebBrowser void OnFrameLoadStart(string url, bool isMainFrame); void OnFrameLoadEnd(string url, bool isMainFrame, int httpStatusCode); - void OnGotFocus(); - bool OnSetFocus(CefFocusSource source); - void OnTakeFocus(bool next); void OnConsoleMessage(string message, string source, int line); void OnLoadError(string url, CefErrorCode errorCode, string errorText); } From 3bbb01c6ef84178a2816e20775bf0e23245aa2ef Mon Sep 17 00:00:00 2001 From: jankurianski Date: Tue, 25 Nov 2014 22:29:35 +1100 Subject: [PATCH 5/8] Added IFocusHandler and WinForms example. --- CefSharp.Core/Internals/ClientAdapter.cpp | 42 +++++++++++-------- CefSharp.OffScreen/ChromiumWebBrowser.cs | 6 +-- .../BrowserTabUserControl.cs | 1 + .../CefSharp.WinForms.Example.csproj | 1 + CefSharp.WinForms.Example/FocusHandler.cs | 41 ++++++++++++++++++ CefSharp.WinForms/ChromiumWebBrowser.cs | 16 +------ CefSharp.Wpf/ChromiumWebBrowser.cs | 1 + CefSharp/CefFocusSource.cs | 12 +++--- CefSharp/CefSharp.csproj | 1 + CefSharp/IFocusHandler.cs | 29 +++++++++++++ CefSharp/IWebBrowser.cs | 5 +++ CefSharp/IWinFormsWebBrowser.cs | 4 -- 12 files changed, 112 insertions(+), 47 deletions(-) create mode 100644 CefSharp.WinForms.Example/FocusHandler.cs create mode 100644 CefSharp/IFocusHandler.cs diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 70ac832082..9e6baf639a 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 3c938db125..46dba1c9f8 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -176,6 +176,7 @@ public Task ScreenshotAsync() 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 @@ void IWebBrowserInternal.OnLoadError(string url, CefErrorCode errorCode, string } } - 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 34568c6391..4698f17e28 100644 --- a/CefSharp.WinForms.Example/BrowserTabUserControl.cs +++ b/CefSharp.WinForms.Example/BrowserTabUserControl.cs @@ -24,6 +24,7 @@ public BrowserTabUserControl(string url) 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 7f85586d6e..23ad8ebd8d 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 0000000000..95fc004ac9 --- /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 f45e7c5c3b..0eb8cb50cd 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -26,6 +26,7 @@ public class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinFormsWebBrow 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 @@ void IWebBrowserInternal.OnFrameLoadEnd(string url, bool isMainFrame, int httpSt } } - 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 08464e79fe..a12b2444b5 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -51,6 +51,7 @@ public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrow 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 9855c4348d..2d40d503a9 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 e1e6a90e4b..2e22f4a107 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 0000000000..2eaa183eaa --- /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 4594cf7f20..a43a8c3f98 100644 --- a/CefSharp/IWebBrowser.cs +++ b/CefSharp/IWebBrowser.cs @@ -110,6 +110,11 @@ public interface IWebBrowser : IDisposable /// 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 f579bd6206..322efb48ce 100644 --- a/CefSharp/IWinFormsWebBrowser.cs +++ b/CefSharp/IWinFormsWebBrowser.cs @@ -20,9 +20,5 @@ public interface IWinFormsWebBrowser : IWebBrowser void Paste(); void Delete(); void SelectAll(); - - void OnGotFocus(); - bool OnSetFocus(CefFocusSource source); - void OnTakeFocus(bool next); } } From 25bfc512f1b34304c055b74f730bd4cb6b6bda83 Mon Sep 17 00:00:00 2001 From: jankurianski Date: Tue, 25 Nov 2014 22:33:12 +1100 Subject: [PATCH 6/8] Untabify --- CefSharp.Core/Internals/ClientAdapter.cpp | 32 +++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 9e6baf639a..f0bb272b15 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -375,39 +375,39 @@ namespace CefSharp void ClientAdapter::OnGotFocus(CefRefPtr browser) { - IFocusHandler^ handler = _browserControl->FocusHandler; + IFocusHandler^ handler = _browserControl->FocusHandler; if (handler == nullptr) - { - return; - } + { + return; + } - handler->OnGotFocus(); + handler->OnGotFocus(); } bool ClientAdapter::OnSetFocus(CefRefPtr browser, FocusSource source) { - IFocusHandler^ handler = _browserControl->FocusHandler; + IFocusHandler^ handler = _browserControl->FocusHandler; if (handler == nullptr) - { - // Allow the focus to be set by default. - return false; - } + { + // Allow the focus to be set by default. + return false; + } - return handler->OnSetFocus((CefFocusSource)source); + return handler->OnSetFocus((CefFocusSource)source); } void ClientAdapter::OnTakeFocus(CefRefPtr browser, bool next) { - IFocusHandler^ handler = _browserControl->FocusHandler; + IFocusHandler^ handler = _browserControl->FocusHandler; if (handler == nullptr) - { - return; - } + { + return; + } - handler->OnTakeFocus(next); + handler->OnTakeFocus(next); } bool ClientAdapter::OnJSDialog(CefRefPtr browser, const CefString& origin_url, const CefString& accept_lang, From 1207e7b522757ebfdd110bc171e316ff6ad09cdc Mon Sep 17 00:00:00 2001 From: jankurianski Date: Tue, 25 Nov 2014 22:47:58 +1100 Subject: [PATCH 7/8] Oops, forgot to set focus to address bar on UI thread. --- CefSharp.WinForms.Example/FocusHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CefSharp.WinForms.Example/FocusHandler.cs b/CefSharp.WinForms.Example/FocusHandler.cs index 95fc004ac9..3b0630950e 100644 --- a/CefSharp.WinForms.Example/FocusHandler.cs +++ b/CefSharp.WinForms.Example/FocusHandler.cs @@ -35,7 +35,7 @@ public void OnTakeFocus(bool next) // 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(); + urlTextBox.Control.BeginInvoke(new MethodInvoker(() => urlTextBox.Focus())); } } } \ No newline at end of file From 9c8fab9f8958b09c191599ecc43f84649c731698 Mon Sep 17 00:00:00 2001 From: jankurianski Date: Wed, 26 Nov 2014 00:08:35 +1100 Subject: [PATCH 8/8] Added DefaultFocusHandler with SelectNextControl behaviour. --- .../BrowserTabUserControl.cs | 2 +- CefSharp.WinForms/ChromiumWebBrowser.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CefSharp.WinForms.Example/BrowserTabUserControl.cs b/CefSharp.WinForms.Example/BrowserTabUserControl.cs index 4698f17e28..10e07a3837 100644 --- a/CefSharp.WinForms.Example/BrowserTabUserControl.cs +++ b/CefSharp.WinForms.Example/BrowserTabUserControl.cs @@ -24,7 +24,7 @@ public BrowserTabUserControl(string url) Browser = browser; browser.MenuHandler = new MenuHandler(); - browser.FocusHandler = new FocusHandler(browser, urlTextBox); + //browser.FocusHandler = new FocusHandler(browser, urlTextBox); browser.NavStateChanged += OnBrowserNavStateChanged; browser.ConsoleMessage += OnBrowserConsoleMessage; browser.TitleChanged += OnBrowserTitleChanged; diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 0eb8cb50cd..3ed8b452e2 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -55,6 +55,8 @@ public ChromiumWebBrowser(string address) Address = address; Dock = DockStyle.Fill; + + FocusHandler = new DefaultFocusHandler(this); } protected override void Dispose(bool disposing) @@ -354,5 +356,31 @@ private void ResizeBrowser() managedCefBrowserAdapter.Resize(Width, Height); } } + + #region DefaultFocusHandler + private class DefaultFocusHandler : IFocusHandler + { + private ChromiumWebBrowser browser; + + public DefaultFocusHandler(ChromiumWebBrowser browser) + { + this.browser = browser; + } + + public void OnGotFocus() + { + } + + public bool OnSetFocus(CefFocusSource source) + { + return false; + } + + public void OnTakeFocus(bool next) + { + browser.BeginInvoke(new MethodInvoker(() => browser.SelectNextControl(browser, next, true, true, true))); + } + } + #endregion } }