Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IFocusHandler #639

Merged
merged 9 commits into from
Nov 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion CefSharp.Core/Internals/ClientAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,41 @@ namespace CefSharp
}
}

void ClientAdapter::OnGotFocus(CefRefPtr<CefBrowser> browser)
{
IFocusHandler^ handler = _browserControl->FocusHandler;

if (handler == nullptr)
{
return;
}

handler->OnGotFocus();
}

bool ClientAdapter::OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source)
{
IFocusHandler^ handler = _browserControl->FocusHandler;

if (handler == nullptr)
{
// Allow the focus to be set by default.
return false;
}

return handler->OnSetFocus((CefFocusSource)source);
}

void ClientAdapter::OnTakeFocus(CefRefPtr<CefBrowser> browser, bool next)
{
_browserControl->OnTakeFocus(next);
IFocusHandler^ handler = _browserControl->FocusHandler;

if (handler == nullptr)
{
return;
}

handler->OnTakeFocus(next);
}

bool ClientAdapter::OnJSDialog(CefRefPtr<CefBrowser> browser, const CefString& origin_url, const CefString& accept_lang,
Expand Down
2 changes: 2 additions & 0 deletions CefSharp.Core/Internals/ClientAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ namespace CefSharp
CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model) OVERRIDE;

// CefFocusHandler
virtual DECL void OnGotFocus(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual DECL bool OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source) OVERRIDE;
virtual DECL void OnTakeFocus(CefRefPtr<CefBrowser> browser, bool next) OVERRIDE;

// CefKeyboardHandler
Expand Down
6 changes: 1 addition & 5 deletions CefSharp.OffScreen/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public Task<Bitmap> 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<LoadErrorEventArgs> LoadError;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions CefSharp.WinForms.Example/BrowserTabUserControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Compile Include="BrowserTabUserControl.Designer.cs">
<DependentUpon>BrowserTabUserControl.cs</DependentUpon>
</Compile>
<Compile Include="FocusHandler.cs" />
<Compile Include="MenuHandler.cs" />
<Compile Include="Minimal\SimpleBrowserForm.cs">
<SubType>Form</SubType>
Expand Down
41 changes: 41 additions & 0 deletions CefSharp.WinForms.Example/FocusHandler.cs
Original file line number Diff line number Diff line change
@@ -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.Control.BeginInvoke(new MethodInvoker(() => urlTextBox.Focus()));
}
}
}
34 changes: 29 additions & 5 deletions CefSharp.WinForms/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -54,6 +55,8 @@ public ChromiumWebBrowser(string address)
Address = address;

Dock = DockStyle.Fill;

FocusHandler = new DefaultFocusHandler(this);
}

protected override void Dispose(bool disposing)
Expand Down Expand Up @@ -208,11 +211,6 @@ void IWebBrowserInternal.OnFrameLoadEnd(string url, bool isMainFrame, int httpSt
}
}

void IWebBrowserInternal.OnTakeFocus(bool next)
{
SelectNextControl(this, next, true, true, true);
}

void IWebBrowserInternal.OnConsoleMessage(string message, string source, int line)
{
var handler = ConsoleMessage;
Expand Down Expand Up @@ -358,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
}
}
6 changes: 1 addition & 5 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConsoleMessageEventArgs> ConsoleMessage;
public event EventHandler<StatusMessageEventArgs> StatusMessage;
Expand Down Expand Up @@ -1125,11 +1126,6 @@ void IWebBrowserInternal.OnFrameLoadEnd(string url, bool isMainFrame, int httpSt
}
}

void IWebBrowserInternal.OnTakeFocus(bool next)
{
throw new NotImplementedException();
}

void IWebBrowserInternal.OnConsoleMessage(string message, string source, int line)
{
var handler = ConsoleMessage;
Expand Down
21 changes: 21 additions & 0 deletions CefSharp/CefFocusSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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
{
/// <summary>
/// Focus Source
/// </summary>
public enum CefFocusSource
{
/// <summary>
/// The source is explicit navigation via the API (LoadURL(), etc).
/// </summary>
FocusSourceNavigation = 0,
/// <summary>
/// The source is a system-generated focus event.
/// </summary>
FocusSourceSystem
}
}
2 changes: 2 additions & 0 deletions CefSharp/CefSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CefFileDialogMode.cs" />
<Compile Include="IFocusHandler.cs" />
<Compile Include="IsBrowserInitializedChangedEventArgs.cs" />
<Compile Include="CefTerminationStatus.cs" />
<Compile Include="IDialogHandler.cs" />
Expand All @@ -114,6 +115,7 @@
<Compile Include="TransitionType.cs" />
<Compile Include="NavStateChangedEventArgs.cs" />
<Compile Include="ICompletionHandler.cs" />
<Compile Include="CefFocusSource.cs" />
<Compile Include="Internals\BitmapInfo.cs" />
<Compile Include="Internals\IBrowserProcess.cs" />
<Compile Include="Internals\JavascriptMethod.cs" />
Expand Down
29 changes: 29 additions & 0 deletions CefSharp/IFocusHandler.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Called when the browser component has received focus.
/// </summary>
void OnGotFocus();

/// <summary>
/// Called when the browser component is requesting focus.
/// </summary>
/// <param name="source">Indicates where the focus request is originating from.</param>
/// <returns>Return false to allow the focus to be set or true to cancel setting the focus.</returns>
bool OnSetFocus(CefFocusSource source);

/// <summary>
/// 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.
/// </summary>
/// <param name="next">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.</param>
void OnTakeFocus(bool next);
}
}
5 changes: 5 additions & 0 deletions CefSharp/IWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public interface IWebBrowser : IDisposable
/// </summary>
IMenuHandler MenuHandler { get; set; }

/// <summary>
/// Implement <see cref="IFocusHandler"/> and assign to handle events related to the browser component's focus
/// </summary>
IFocusHandler FocusHandler { get; set; }

/// <summary>
/// A flag that indicates whether the WebBrowser is initialized (true) or not (false).
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion CefSharp/Internals/IWebBrowserInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public interface IWebBrowserInternal : IWebBrowser

void OnFrameLoadStart(string url, bool isMainFrame);
void OnFrameLoadEnd(string url, bool isMainFrame, int httpStatusCode);
void OnTakeFocus(bool next);
void OnConsoleMessage(string message, string source, int line);
void OnStatusMessage(string value);
void OnLoadError(string url, CefErrorCode errorCode, string errorText);
Expand Down