Skip to content

Commit

Permalink
WinForms - Add new IWinFormsChromiumWebBrowser interface
Browse files Browse the repository at this point in the history
- Common interface of ChromiumHostControl and ChromiumWebBrowser
- WinForms Example Fix removing of Tab
  • Loading branch information
amaitland committed Feb 10, 2022
1 parent 7906919 commit 870dbb5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 36 deletions.
18 changes: 7 additions & 11 deletions CefSharp.WinForms.Example/BrowserForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,18 @@ private void AboutToolStripMenuItemClick(object sender, EventArgs e)
new AboutBox().ShowDialog();
}

public void RemoveTab(IntPtr windowHandle)
public void RemoveTab(ChromiumHostControl ctrl)
{
var parentControl = FromChildHandle(windowHandle);
if (!parentControl.IsDisposed)
if (!ctrl.IsDisposed)
{
if (parentControl.Parent is TabPage tabPage)
var tabPage = ctrl.GetParentOfType<TabPage>();

if(tabPage == null)
{
browserTabControl.TabPages.Remove(tabPage);
throw new Exception("Unable to find parent TabPage");
}
else if (parentControl.Parent is Panel panel)
{
var browserTabUserControl = (BrowserTabUserControl)panel.Parent;

var tab = (TabPage)browserTabUserControl.Parent;
browserTabControl.TabPages.Remove(tab);
}
browserTabControl.TabPages.Remove(tabPage);
}
}

Expand Down
6 changes: 2 additions & 4 deletions CefSharp.WinForms.Example/BrowserTabUserControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace CefSharp.WinForms.Example
{
public partial class BrowserTabUserControl : UserControl
{
public IChromiumWebBrowserBase Browser { get; private set; }
public IWinFormsChromiumWebBrowser Browser { get; private set; }
private ChromiumWidgetNativeWindow messageInterceptor;
private bool multiThreadedMessageLoopEnabled;

Expand Down Expand Up @@ -109,9 +109,7 @@ public BrowserTabUserControl(Action<string, int?> openNewTab, string url, bool m
{
if (ctrl.FindForm() is BrowserForm owner)
{
var windowHandle = popupBrowser.GetHost().GetWindowHandle();
owner.RemoveTab(windowHandle);
owner.RemoveTab(ctrl);
}
ctrl.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion CefSharp.WinForms/Host/ChromiumHostControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace CefSharp.WinForms.Host
/// <seealso cref="Control" />
[Docking(DockingBehavior.AutoDock), ToolboxBitmap(typeof(ChromiumHostControl)),
Designer(typeof(ChromiumWebBrowserDesigner))]
public class ChromiumHostControl : ChromiumHostControlBase, IChromiumWebBrowserBase
public class ChromiumHostControl : ChromiumHostControlBase, IWinFormsChromiumWebBrowser
{
/// <summary>
/// Get access to the core <see cref="IBrowser"/> instance.
Expand Down
42 changes: 42 additions & 0 deletions CefSharp.WinForms/IWinFormsChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright © 2022 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 CefSharp.WinForms.Host;
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace CefSharp.WinForms
{
/// <summary>
/// Winforms Specific Chromium browser implementation, differs from <see cref="IWinFormsWebBrowser"/> in that
/// this interface is implemented by both <see cref="ChromiumWebBrowser"/> and <see cref="ChromiumHostControl"/>
/// where <see cref="IWinFormsWebBrowser"/> is only implemented by <see cref="ChromiumWebBrowser"/>
/// </summary>
public interface IWinFormsChromiumWebBrowser : IChromiumWebBrowserBase, IWin32Window, IComponent, ISynchronizeInvoke
{
/// <summary>
/// Occurs when the browser title changed.
/// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
/// </summary>
event EventHandler<TitleChangedEventArgs> TitleChanged;
/// <summary>
/// Occurs when the browser address changed.
/// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
/// </summary>
event EventHandler<AddressChangedEventArgs> AddressChanged;

/// <summary>
/// Event called after the underlying CEF browser instance has been created.
/// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
/// </summary>
event EventHandler IsBrowserInitializedChanged;
}
}
23 changes: 3 additions & 20 deletions CefSharp.WinForms/IWinFormsWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,15 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace CefSharp.WinForms
{
/// <summary>
/// WinForms specific implementation, has events the
/// <see cref="ChromiumWebBrowser" /> implementation exposes.
/// </summary>
/// <seealso cref="CefSharp.IWebBrowser" />
public interface IWinFormsWebBrowser : IWebBrowser, IWin32Window, IComponent, ISynchronizeInvoke
/// <seealso cref="IWebBrowser" /> and <seealso cref="IChromiumWebBrowserBase"/>
public interface IWinFormsWebBrowser : IWebBrowser, IWinFormsChromiumWebBrowser
{
/// <summary>
/// Occurs when the browser title changed.
/// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
/// </summary>
event EventHandler<TitleChangedEventArgs> TitleChanged;
/// <summary>
/// Occurs when the browser address changed.
/// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
/// </summary>
event EventHandler<AddressChangedEventArgs> AddressChanged;

}
}

0 comments on commit 870dbb5

Please sign in to comment.