Skip to content

Commit

Permalink
WinForms Example - Update LifeSpanHandler example to host popup in Ta…
Browse files Browse the repository at this point in the history
…b (notify popup of move and resize)
  • Loading branch information
amaitland committed Jan 30, 2018
1 parent 6bcca7a commit fbce2d6
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 47 deletions.
25 changes: 24 additions & 1 deletion CefSharp.WinForms.Example/BrowserForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ private void BrowserFormLoad(object sender, EventArgs e)
AddTab(CefExample.DefaultUrl);
}

/// <summary>
/// Used to add a Popup browser as a Tab
/// </summary>
/// <param name="browserHostControl"></param>
public void AddTab(Control browserHostControl, string url)
{
browserTabControl.SuspendLayout();

var tabPage = new TabPage(url)
{
Dock = DockStyle.Fill
};

tabPage.Controls.Add(browserHostControl);

browserTabControl.TabPages.Add(tabPage);

//Make newly created tab active
browserTabControl.SelectedTab = tabPage;

browserTabControl.ResumeLayout(true);
}

private void AddTab(string url, int? insertIndex = null)
{
browserTabControl.SuspendLayout();
Expand Down Expand Up @@ -117,7 +140,7 @@ private BrowserTabUserControl GetCurrentTabControl()
}

var tabPage = browserTabControl.Controls[browserTabControl.SelectedIndex];
var control = (BrowserTabUserControl)tabPage.Controls[0];
var control = tabPage.Controls[0] as BrowserTabUserControl;

return control;
}
Expand Down
2 changes: 1 addition & 1 deletion CefSharp.WinForms.Example/BrowserTabUserControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public BrowserTabUserControl(Action<string, int?> openNewTab, string url, bool m
{
browser.KeyboardHandler = new KeyboardHandler();
}
browser.LifeSpanHandler = new LifeSpanHandler();
//browser.LifeSpanHandler = new LifeSpanHandler();
browser.LoadingStateChanged += OnBrowserLoadingStateChanged;
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 @@ -90,6 +90,7 @@
<Compile Include="Handlers\MenuHandler.cs" />
<Compile Include="Handlers\WinFormsBrowserProcessHandler.cs" />
<Compile Include="Handlers\WinFormsRequestHandler.cs" />
<Compile Include="Helper\PopupAsChildHelper.cs" />
<Compile Include="InputBox.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
83 changes: 38 additions & 45 deletions CefSharp.WinForms.Example/Handlers/LifeSpanHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,61 @@
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CefSharp.WinForms.Example.Helper;

namespace CefSharp.WinForms.Example.Handlers
{
public class LifeSpanHandler : ILifeSpanHandler
{
private Dictionary<int, PopupAsChildHelper> popupasChildHelpers = new Dictionary<int, PopupAsChildHelper>();

bool ILifeSpanHandler.OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
// Set newBrowser to null unless your attempting to host the popup in a new instance of ChromiumWebBrowser
// This should only be used in WPF/OffScreen
//Set newBrowser to null unless your attempting to host the popup in a new instance of ChromiumWebBrowser
newBrowser = null;

return false; //Return true to cancel the popup creation

// Hosting the popup in your own control/window
// Use IWindowInfo.SetAsChild to specify the parent handle
// NOTE: Window resize not yet handled - you need to get the
// IBrowserHost from the newly created IBrowser instance that represents the popup
// Then subscribe to window resize notifications and call NotifyMoveOrResizeStarted().
// Also any chances in width/height you need to call SetWindowPos on the browsers HWND
// Use NativeMethodWrapper.SetWindowPosition to achieve this - you can get the HWND using
// IBrowserHost method

//var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

//var windowX = windowInfo.X;
//var windowY = windowInfo.Y;
//var windowWidth = (windowInfo.Width == int.MinValue) ? 600 : windowInfo.Width;
//var windowHeight = (windowInfo.Height == int.MinValue) ? 800 : windowInfo.Height;

//chromiumWebBrowser.Invoke(new Action(() =>
//{
// var owner = chromiumWebBrowser.FindForm();

// var popup = new Form
// {
// Left = windowX,
// Top = windowY,
// Width = windowWidth,
// Height = windowHeight,
// Text = targetFrameName
// };
//Use IWindowInfo.SetAsChild to specify the parent handle
//NOTE: user PopupAsChildHelper to handle with Form move and Control resize
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

// popup.CreateControl();

// owner.AddOwnedForm(popup);
chromiumWebBrowser.Invoke(new Action(() =>
{
var owner = chromiumWebBrowser.FindForm() as BrowserForm;

// var control = new Control();
// control.Dock = DockStyle.Fill;
// control.CreateControl();
if(owner != null)
{
var control = new Control();
control.Dock = DockStyle.Fill;
control.CreateControl();

// popup.Controls.Add(control);
owner.AddTab(control, targetUrl);

// popup.Show();
var rect = control.ClientRectangle;

// var rect = control.ClientRectangle;
windowInfo.SetAsChild(control.Handle, rect.Left, rect.Top, rect.Right, rect.Bottom);
}
}));

// windowInfo.SetAsChild(control.Handle, rect.Left, rect.Top, rect.Right, rect.Bottom);
//}));
return false;
}

void ILifeSpanHandler.OnAfterCreated(IWebBrowser browserControl, IBrowser browser)
{
if (browser.IsPopup)
{
var interceptor = new PopupAsChildHelper(browser);

popupasChildHelpers.Add(browser.Identifier, interceptor);
}
}

bool ILifeSpanHandler.DoClose(IWebBrowser browserControl, IBrowser browser)
{
//We need to allow popups to close
//If the browser has been disposed then we'll just let the default behaviour take place
if(browser.IsDisposed || browser.IsPopup)
if (browser.IsDisposed || browser.IsPopup)
{
return false;
}
Expand All @@ -84,9 +68,18 @@ bool ILifeSpanHandler.DoClose(IWebBrowser browserControl, IBrowser browser)
return true;
}

public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
void ILifeSpanHandler.OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
{
if (!browser.IsDisposed && browser.IsPopup)
{
var interceptor = popupasChildHelpers[browser.Identifier];

if (interceptor != null)
{
popupasChildHelpers[browser.Identifier] = null;
interceptor.Dispose();
}
}
}
}
}
Loading

0 comments on commit fbce2d6

Please sign in to comment.