mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 04:00:07 +00:00
WinForms - Allow creation of custom ChromiumHostControl instance
- Add new CreatePopupChromiumHostControl delegate which can be passed to
LifeSpanHandler.Create to create a new ChromiumHostControl instance.
You can create a class that inherits from ChromiumHostControl and add
additional functionality.
LifeSpanHandler
.Create(() =>
{
return new ChromiumHostControl
{
Dock = DockStyle.Fill
};
})
This commit is contained in:
@@ -37,6 +37,13 @@ namespace CefSharp.WinForms.Handler
|
||||
/// <param name="browser">browser</param>
|
||||
public delegate void OnPopupDestroyedDelegate(ChromiumHostControl control, IBrowser browser);
|
||||
|
||||
/// <summary>
|
||||
/// Called to create a new instance of <see cref="ChromiumHostControl"/>. Allows creation of a derived
|
||||
/// implementation of <see cref="ChromiumHostControl"/>.
|
||||
/// </summary>
|
||||
/// <returns>A custom instance of <see cref="ChromiumHostControl"/>.</returns>
|
||||
public delegate ChromiumHostControl CreatePopupChromiumHostControl();
|
||||
|
||||
/// <summary>
|
||||
/// A WinForms Specific <see cref="ILifeSpanHandler"/> implementation that simplifies
|
||||
/// the process of hosting a Popup as a Control/Tab.
|
||||
@@ -49,7 +56,13 @@ namespace CefSharp.WinForms.Handler
|
||||
private OnPopupDestroyedDelegate onPopupDestroyed;
|
||||
private OnPopupBrowserCreatedDelegate onPopupBrowserCreated;
|
||||
private OnPopupCreatedDelegate onPopupCreated;
|
||||
|
||||
private CreatePopupChromiumHostControl chromiumHostControlCreatedDelegate;
|
||||
|
||||
public LifeSpanHandler(CreatePopupChromiumHostControl chromiumHostControlCreatedDelegate)
|
||||
{
|
||||
this.chromiumHostControlCreatedDelegate = chromiumHostControlCreatedDelegate;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
|
||||
{
|
||||
@@ -168,10 +181,15 @@ namespace CefSharp.WinForms.Handler
|
||||
//We need to execute sync here so IWindowInfo.SetAsChild is called before we return false;
|
||||
webBrowser.InvokeSyncOnUiThreadIfRequired(new Action(() =>
|
||||
{
|
||||
var control = new ChromiumHostControl
|
||||
ChromiumHostControl control = chromiumHostControlCreatedDelegate?.Invoke();
|
||||
|
||||
if (control == null)
|
||||
{
|
||||
Dock = DockStyle.Fill
|
||||
};
|
||||
control = new ChromiumHostControl
|
||||
{
|
||||
Dock = DockStyle.Fill
|
||||
};
|
||||
}
|
||||
control.CreateControl();
|
||||
|
||||
onPopupCreated?.Invoke(control, targetUrl);
|
||||
@@ -238,10 +256,14 @@ namespace CefSharp.WinForms.Handler
|
||||
/// of implementing directly you will need to inherit from <see cref="CefSharp.WinForms.Handler.LoadHandler"/>.
|
||||
/// As it provides base functionality required to make <see cref="ChromiumHostControl"/> events work correctly.
|
||||
/// </summary>
|
||||
/// <returns>LifeSpanHandlerBuilder</returns>
|
||||
public static LifeSpanHandlerBuilder Create()
|
||||
/// <returns>
|
||||
/// A <see cref="LifeSpanHandlerBuilder"/> which can be used to fluently create an <see cref="ILifeSpanHandler"/>.
|
||||
/// Call <see cref="LifeSpanHandlerBuilder.Build"/> to create the actual instance after you have call
|
||||
/// <see cref="LifeSpanHandlerBuilder.OnPopupCreated(OnPopupCreatedDelegate)"/> etc.
|
||||
/// </returns>
|
||||
public static LifeSpanHandlerBuilder Create(CreatePopupChromiumHostControl chromiumHostControlCreatedDelegate = null)
|
||||
{
|
||||
return new LifeSpanHandlerBuilder();
|
||||
return new LifeSpanHandlerBuilder(chromiumHostControlCreatedDelegate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,12 @@ namespace CefSharp.WinForms.Handler
|
||||
/// </summary>
|
||||
public class LifeSpanHandlerBuilder
|
||||
{
|
||||
private readonly LifeSpanHandler handler = new LifeSpanHandler();
|
||||
private readonly LifeSpanHandler handler;
|
||||
|
||||
public LifeSpanHandlerBuilder(CreatePopupChromiumHostControl chromiumHostControlCreatedDelegate)
|
||||
{
|
||||
handler = new LifeSpanHandler(chromiumHostControlCreatedDelegate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="OnPopupCreatedDelegate"/> will be called when the<see cref="ChromiumHostControl"/> has been
|
||||
|
||||
Reference in New Issue
Block a user