Skip to content

Commit 3bbb01c

Browse files
committed
Added IFocusHandler and WinForms example.
1 parent 87667f2 commit 3bbb01c

File tree

12 files changed

+112
-47
lines changed

12 files changed

+112
-47
lines changed

CefSharp.Core/Internals/ClientAdapter.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -375,31 +375,39 @@ namespace CefSharp
375375

376376
void ClientAdapter::OnGotFocus(CefRefPtr<CefBrowser> browser)
377377
{
378-
auto winFormsControl = dynamic_cast<IWinFormsWebBrowser^>((IWebBrowserInternal^)_browserControl);
379-
if (winFormsControl != nullptr)
380-
{
381-
winFormsControl->OnGotFocus();
382-
}
378+
IFocusHandler^ handler = _browserControl->FocusHandler;
379+
380+
if (handler == nullptr)
381+
{
382+
return;
383+
}
384+
385+
handler->OnGotFocus();
383386
}
384387

385-
bool ClientAdapter::OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source)
388+
bool ClientAdapter::OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source)
386389
{
387-
auto winFormsControl = dynamic_cast<IWinFormsWebBrowser^>((IWebBrowserInternal^)_browserControl);
388-
if (winFormsControl == nullptr)
389-
{
390-
return false;
391-
}
390+
IFocusHandler^ handler = _browserControl->FocusHandler;
392391

393-
return winFormsControl->OnSetFocus((CefFocusSource)source);
392+
if (handler == nullptr)
393+
{
394+
// Allow the focus to be set by default.
395+
return false;
396+
}
397+
398+
return handler->OnSetFocus((CefFocusSource)source);
394399
}
395400

396401
void ClientAdapter::OnTakeFocus(CefRefPtr<CefBrowser> browser, bool next)
397402
{
398-
auto winFormsControl = dynamic_cast<IWinFormsWebBrowser^>((IWebBrowserInternal^)_browserControl);
399-
if (winFormsControl != nullptr)
400-
{
401-
winFormsControl->OnTakeFocus(next);
402-
}
403+
IFocusHandler^ handler = _browserControl->FocusHandler;
404+
405+
if (handler == nullptr)
406+
{
407+
return;
408+
}
409+
410+
handler->OnTakeFocus(next);
403411
}
404412

405413
bool ClientAdapter::OnJSDialog(CefRefPtr<CefBrowser> browser, const CefString& origin_url, const CefString& accept_lang,

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public Task<Bitmap> ScreenshotAsync()
176176
public IKeyboardHandler KeyboardHandler { get; set; }
177177
public ILifeSpanHandler LifeSpanHandler { get; set; }
178178
public IMenuHandler MenuHandler { get; set; }
179+
public IFocusHandler FocusHandler { get; set; }
179180
public IRequestHandler RequestHandler { get; set; }
180181

181182
public event EventHandler<LoadErrorEventArgs> LoadError;
@@ -451,11 +452,6 @@ void IWebBrowserInternal.OnLoadError(string url, CefErrorCode errorCode, string
451452
}
452453
}
453454

454-
void IWebBrowserInternal.OnTakeFocus(bool next)
455-
{
456-
throw new NotImplementedException();
457-
}
458-
459455
void IWebBrowserInternal.OnStatusMessage(string value)
460456
{
461457
var handler = StatusMessage;

CefSharp.WinForms.Example/BrowserTabUserControl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public BrowserTabUserControl(string url)
2424
Browser = browser;
2525

2626
browser.MenuHandler = new MenuHandler();
27+
browser.FocusHandler = new FocusHandler(browser, urlTextBox);
2728
browser.NavStateChanged += OnBrowserNavStateChanged;
2829
browser.ConsoleMessage += OnBrowserConsoleMessage;
2930
browser.TitleChanged += OnBrowserTitleChanged;

CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Compile Include="BrowserTabUserControl.Designer.cs">
7171
<DependentUpon>BrowserTabUserControl.cs</DependentUpon>
7272
</Compile>
73+
<Compile Include="FocusHandler.cs" />
7374
<Compile Include="MenuHandler.cs" />
7475
<Compile Include="Minimal\SimpleBrowserForm.cs">
7576
<SubType>Form</SubType>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright © 2010-2014 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System.Windows.Forms;
6+
namespace CefSharp.WinForms.Example
7+
{
8+
internal class FocusHandler : IFocusHandler
9+
{
10+
private ChromiumWebBrowser browser;
11+
private ToolStripTextBox urlTextBox;
12+
13+
public FocusHandler(ChromiumWebBrowser browser, ToolStripTextBox urlTextBox)
14+
{
15+
this.browser = browser;
16+
this.urlTextBox = urlTextBox;
17+
}
18+
19+
public void OnGotFocus()
20+
{
21+
// Fired when Chromium finally receives focus.
22+
}
23+
24+
public bool OnSetFocus(CefFocusSource source)
25+
{
26+
// Returning false means that we allow Chromium to take the focus away from our WinForms control.
27+
// You could also return true to keep focus in the address bar.
28+
return false;
29+
}
30+
31+
public void OnTakeFocus(bool next)
32+
{
33+
// Fired when Chromium is giving up focus because the user
34+
// pressed Tab on the last link/field in the HTML document (in which case 'next' is true)
35+
// or because they pressed Shift+Tab on the first link/field (in which case 'next' is false).
36+
37+
// Here we always focus on the address bar.
38+
urlTextBox.Focus();
39+
}
40+
}
41+
}

CefSharp.WinForms/ChromiumWebBrowser.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinFormsWebBrow
2626
public IDownloadHandler DownloadHandler { get; set; }
2727
public ILifeSpanHandler LifeSpanHandler { get; set; }
2828
public IMenuHandler MenuHandler { get; set; }
29+
public IFocusHandler FocusHandler { get; set; }
2930

3031
public bool CanGoForward { get; private set; }
3132
public bool CanGoBack { get; private set; }
@@ -208,21 +209,6 @@ void IWebBrowserInternal.OnFrameLoadEnd(string url, bool isMainFrame, int httpSt
208209
}
209210
}
210211

211-
void IWinFormsWebBrowser.OnGotFocus()
212-
{
213-
214-
}
215-
216-
bool IWinFormsWebBrowser.OnSetFocus(CefFocusSource source)
217-
{
218-
return false;
219-
}
220-
221-
void IWinFormsWebBrowser.OnTakeFocus(bool next)
222-
{
223-
SelectNextControl(this, next, true, true, true);
224-
}
225-
226212
void IWebBrowserInternal.OnConsoleMessage(string message, string source, int line)
227213
{
228214
var handler = ConsoleMessage;

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrow
5151
public IDownloadHandler DownloadHandler { get; set; }
5252
public ILifeSpanHandler LifeSpanHandler { get; set; }
5353
public IMenuHandler MenuHandler { get; set; }
54+
public IFocusHandler FocusHandler { get; set; }
5455

5556
public event EventHandler<ConsoleMessageEventArgs> ConsoleMessage;
5657
public event EventHandler<StatusMessageEventArgs> StatusMessage;

CefSharp/CefFocusSource.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace CefSharp
99
/// </summary>
1010
public enum CefFocusSource
1111
{
12-
///
13-
// The source is explicit navigation via the API (LoadURL(), etc).
14-
///
12+
/// <summary>
13+
/// The source is explicit navigation via the API (LoadURL(), etc).
14+
/// </summary>
1515
FocusSourceNavigation = 0,
16-
///
17-
// The source is a system-generated focus event.
18-
///
16+
/// <summary>
17+
/// The source is a system-generated focus event.
18+
/// </summary>
1919
FocusSourceSystem
2020
}
2121
}

CefSharp/CefSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
</ItemGroup>
9090
<ItemGroup>
9191
<Compile Include="CefFileDialogMode.cs" />
92+
<Compile Include="IFocusHandler.cs" />
9293
<Compile Include="IsBrowserInitializedChangedEventArgs.cs" />
9394
<Compile Include="CefTerminationStatus.cs" />
9495
<Compile Include="IDialogHandler.cs" />

CefSharp/IFocusHandler.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright © 2010-2014 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
namespace CefSharp
6+
{
7+
public interface IFocusHandler
8+
{
9+
/// <summary>
10+
/// Called when the browser component has received focus.
11+
/// </summary>
12+
void OnGotFocus();
13+
14+
/// <summary>
15+
/// Called when the browser component is requesting focus.
16+
/// </summary>
17+
/// <param name="source">Indicates where the focus request is originating from.</param>
18+
/// <returns>Return false to allow the focus to be set or true to cancel setting the focus.</returns>
19+
bool OnSetFocus(CefFocusSource source);
20+
21+
/// <summary>
22+
/// Called when the browser component is about to lose focus.
23+
/// For instance, if focus was on the last HTML element and the user pressed the TAB key.
24+
/// </summary>
25+
/// <param name="next">Will be true if the browser is giving focus to the next component
26+
/// and false if the browser is giving focus to the previous component.</param>
27+
void OnTakeFocus(bool next);
28+
}
29+
}

0 commit comments

Comments
 (0)