Files
cefsharp/CefSharp.Wpf/HwndHost/FocusHandler.cs
Alex Maitland f43cd64c55 Import CefSharp.Wpf.HwndHost into main repository. (#4981)
* HwndHost - Initial import

* HwndHost - Add Example to solution and use common IWpfChromiumWebBrowser interface

* HwndHost - Update UpdateNugetPackages.ps1

* HwndHost - Add Nuspec

* HwndHost Example - Upgrade version

* HwndHost Example - Try non self contained build for debug and release

* Test - Add basic HwndHost tests

Probably need to revisit these, little bit hacky creating the window

* HwndHost - Allow any RequestContext that inherits from Core.RequestContext

Cheery pick of https://github.com/cefsharp/CefSharp.Wpf.HwndHost/pull/31

* HwndHost Example - Disable arm64

Example will be removed shortly, so it doesn't matter too much now.
It will be moved to MinimalExample and the Wpf Example expanded

* HwndHost - Delete delegate command

* Upgrade packages

* Nuget - Move Nuspec file to PackageReference folder

---------

Co-authored-by: amaitland <307872+amaitland@users.noreply.github.com>
2025-01-18 06:40:48 +10:00

85 lines
3.5 KiB
C#

// Copyright © 2020 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.Input;
namespace CefSharp.Wpf.HwndHost
{
/// <summary>
/// Focus Handler
/// The methods of this class will be called on the CEF UI thread.
/// </summary>
public class FocusHandler : IFocusHandler
{
void IFocusHandler.OnGotFocus(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
OnGotFocus(chromiumWebBrowser, browser);
}
/// <summary>
/// Called when the browser component has received focus.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
protected virtual void OnGotFocus(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
bool IFocusHandler.OnSetFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFocusSource source)
{
return OnSetFocus(chromiumWebBrowser, browser, source);
}
/// <summary>
/// Called when the browser component is requesting focus.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object, do not keep a reference to this object outside of this method</param>
/// <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>
protected virtual bool OnSetFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFocusSource source)
{
//We don't deal with popups as they're rendered by default entirely by CEF
if (browser.IsPopup)
{
return false;
}
// Do not let the browser take focus when a Load method has been called
return source == CefFocusSource.FocusSourceNavigation;
}
void IFocusHandler.OnTakeFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, bool next)
{
OnTakeFocus(chromiumWebBrowser, browser, next);
}
/// <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="chromiumWebBrowser">the ChromiumWebBrowser control</param>
/// <param name="browser">the browser object</param>
/// <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>
protected virtual void OnTakeFocus(IWebBrowser chromiumWebBrowser, IBrowser browser, bool next)
{
//We don't deal with popups as they're rendered by default entirely by CEF
if (browser.IsPopup)
{
return;
}
var hwndChromiumWebBrowser = (ChromiumWebBrowser)chromiumWebBrowser;
var request = new TraversalRequest(next ? FocusNavigationDirection.Next : FocusNavigationDirection.Previous);
// NOTE: OnTakeFocus means leaving focus / not taking focus
hwndChromiumWebBrowser.UiThreadRunAsync(() => hwndChromiumWebBrowser.MoveFocus(request));
}
}
}