Skip to content

Commit

Permalink
WinForms - Improve hosting of popups using ChromiumHostControl
Browse files Browse the repository at this point in the history
- Add IChromiumWebBrowserBase which is can be used for ChromiumWebBrowser instances and ChromiumHostControl
- Reduce code duplication for extensions methods
- Update WinForms Example
  • Loading branch information
amaitland committed Nov 30, 2021
1 parent 6e211bc commit b72d413
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 847 deletions.
3 changes: 2 additions & 1 deletion CefSharp.OffScreen/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ protected virtual void Dispose(bool disposing)
FocusHandler = new NoFocusHandler();

browser = null;
BrowserCore = null;

if (managedCefBrowserAdapter != null)
{
Expand Down Expand Up @@ -656,7 +657,7 @@ public IJavascriptObjectRepository JavascriptObjectRepository
/// Has Focus - Always False
/// </summary>
/// <returns>returns false</returns>
bool IWebBrowser.Focus()
bool IChromiumWebBrowserBase.Focus()
{
// no control to focus for offscreen browser
return false;
Expand Down
62 changes: 31 additions & 31 deletions CefSharp.WinForms.Example/BrowserForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private void UndoMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.Undo();
control.Browser.Undo();
}
}

Expand All @@ -238,7 +238,7 @@ private void RedoMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.Redo();
control.Browser.Redo();
}
}

Expand All @@ -247,7 +247,7 @@ private void CutMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.Cut();
control.Browser.Cut();
}
}

Expand All @@ -256,7 +256,7 @@ private void CopyMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.Copy();
control.Browser.Copy();
}
}

Expand All @@ -265,7 +265,7 @@ private void PasteMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.Paste();
control.Browser.Paste();
}
}

Expand All @@ -274,7 +274,7 @@ private void DeleteMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.Delete();
control.Browser.Delete();
}
}

Expand All @@ -283,7 +283,7 @@ private void SelectAllMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.SelectAll();
control.Browser.SelectAll();
}
}

Expand All @@ -292,7 +292,7 @@ private void PrintToolStripMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.Print();
control.Browser.Print();
}
}

Expand All @@ -304,7 +304,7 @@ private async void ShowDevToolsMenuItemClick(object sender, EventArgs e)
var isDevToolsOpen = await control.CheckIfDevToolsIsOpenAsync();
if (!isDevToolsOpen)
{
control.BrowserControl.ShowDevTools();
control.Browser.ShowDevTools();
}
}
}
Expand All @@ -317,7 +317,7 @@ private async void ShowDevToolsDockedMenuItemClick(object sender, EventArgs e)
var isDevToolsOpen = await control.CheckIfDevToolsIsOpenAsync();
if (!isDevToolsOpen)
{
var chromiumWebBrowser = control.BrowserControl as ChromiumWebBrowser;
var chromiumWebBrowser = control.Browser as ChromiumWebBrowser;
if (chromiumWebBrowser != null && chromiumWebBrowser.LifeSpanHandler != null)
{
control.ShowDevToolsDocked();
Expand All @@ -337,7 +337,7 @@ private async void CloseDevToolsMenuItemClick(object sender, EventArgs e)
var isDevToolsOpen = await control.CheckIfDevToolsIsOpenAsync();
if (isDevToolsOpen)
{
control.BrowserControl.CloseDevTools();
control.Browser.CloseDevTools();
}
}
}
Expand All @@ -347,14 +347,14 @@ private void ZoomInToolStripMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
var task = control.BrowserControl.GetZoomLevelAsync();
var task = control.Browser.GetZoomLevelAsync();

task.ContinueWith(previous =>
{
if (previous.Status == TaskStatus.RanToCompletion)
{
var currentLevel = previous.Result;
control.BrowserControl.SetZoomLevel(currentLevel + ZoomIncrement);
control.Browser.SetZoomLevel(currentLevel + ZoomIncrement);
}
else
{
Expand All @@ -369,13 +369,13 @@ private void ZoomOutToolStripMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
var task = control.BrowserControl.GetZoomLevelAsync();
var task = control.Browser.GetZoomLevelAsync();
task.ContinueWith(previous =>
{
if (previous.Status == TaskStatus.RanToCompletion)
{
var currentLevel = previous.Result;
control.BrowserControl.SetZoomLevel(currentLevel - ZoomIncrement);
control.Browser.SetZoomLevel(currentLevel - ZoomIncrement);
}
else
{
Expand All @@ -390,7 +390,7 @@ private void CurrentZoomLevelToolStripMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
var task = control.BrowserControl.GetZoomLevelAsync();
var task = control.Browser.GetZoomLevelAsync();
task.ContinueWith(previous =>
{
if (previous.Status == TaskStatus.RanToCompletion)
Expand All @@ -411,7 +411,7 @@ private void DoesActiveElementAcceptTextInputToolStripMenuItemClick(object sende
var control = GetCurrentTabControl();
if (control != null)
{
var frame = control.BrowserControl.GetFocusedFrame();
var frame = control.Browser.GetFocusedFrame();

//Execute extension method
frame.ActiveElementAcceptsTextInput().ContinueWith(task =>
Expand Down Expand Up @@ -450,7 +450,7 @@ private void DoesElementWithIdExistToolStripMenuItemClick(object sender, EventAr
var control = GetCurrentTabControl();
if (control != null)
{
var frame = control.BrowserControl.GetFocusedFrame();
var frame = control.Browser.GetFocusedFrame();
//Execute extension method
frame.ElementWithIdExists(dialog.Value).ContinueWith(task =>
Expand Down Expand Up @@ -486,7 +486,7 @@ private void GoToDemoPageToolStripMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.LoadUrl("custom://cefsharp/ScriptedMethodsTest.html");
control.Browser.LoadUrl("custom://cefsharp/ScriptedMethodsTest.html");
}
}

Expand All @@ -495,7 +495,7 @@ private void InjectJavascriptCodeToolStripMenuItemClick(object sender, EventArgs
var control = GetCurrentTabControl();
if (control != null)
{
var frame = control.BrowserControl.GetFocusedFrame();
var frame = control.Browser.GetFocusedFrame();

//Execute extension method
frame.ListenForEvent("test-button", "click");
Expand All @@ -515,7 +515,7 @@ private async void PrintToPdfToolStripMenuItemClick(object sender, EventArgs e)

if (dialog.ShowDialog() == DialogResult.OK)
{
var success = await control.BrowserControl.PrintToPdfAsync(dialog.FileName, new PdfPrintSettings
var success = await control.Browser.PrintToPdfAsync(dialog.FileName, new PdfPrintSettings
{
MarginType = CefPdfPrintMarginType.Custom,
MarginBottom = 10,
Expand Down Expand Up @@ -544,7 +544,7 @@ private void OpenDataUrlToolStripMenuItemClick(object sender, EventArgs e)
if (control != null)
{
const string html = "<html><head><title>Test</title></head><body><h1>Html Encoded in URL!</h1></body></html>";
control.BrowserControl.LoadHtml(html, false);
control.Browser.LoadHtml(html, false);
}
}

Expand All @@ -553,7 +553,7 @@ private void OpenHttpBinOrgToolStripMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.LoadUrl("https://httpbin.org/");
control.Browser.LoadUrl("https://httpbin.org/");
}
}

Expand All @@ -562,7 +562,7 @@ private void RunFileDialogToolStripMenuItemClick(object sender, EventArgs e)
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.GetBrowserHost().RunFileDialog(CefFileDialogMode.Open, "Open", null, new List<string> { "*.*" }, 0, new RunFileDialogCallback());
control.Browser.GetBrowserHost().RunFileDialog(CefFileDialogMode.Open, "Open", null, new List<string> { "*.*" }, 0, new RunFileDialogCallback());
}
}

Expand All @@ -572,9 +572,9 @@ private void LoadExtensionsToolStripMenuItemClick(object sender, EventArgs e)
if (control != null)
{
//The sample extension only works for http(s) schemes
if (control.BrowserControl.GetMainFrame().Url.StartsWith("http"))
if (control.Browser.GetMainFrame().Url.StartsWith("http"))
{
var requestContext = control.BrowserControl.GetBrowserHost().RequestContext;
var requestContext = control.Browser.GetBrowserHost().RequestContext;

const string cefSharpExampleResourcesFolder =
#if !NETCOREAPP
Expand Down Expand Up @@ -612,7 +612,7 @@ private void LoadExtensionsToolStripMenuItemClick(object sender, EventArgs e)
GetActiveBrowser = (extension, isIncognito) =>
{
//Return the active browser for which the extension will act upon
return control.BrowserControl.BrowserCore;
return control.Browser.BrowserCore;
}
};

Expand All @@ -630,16 +630,16 @@ private void JavascriptBindingStressTestToolStripMenuItemClick(object sender, Ev
var control = GetCurrentTabControl();
if (control != null)
{
control.BrowserControl.LoadUrl(CefExample.BindingTestUrl);
control.BrowserControl.LoadingStateChanged += (o, args) =>
control.Browser.LoadUrl(CefExample.BindingTestUrl);
control.Browser.LoadingStateChanged += (o, args) =>
{
if (args.IsLoading == false)
{
Task.Delay(10000).ContinueWith(t =>
{
if (control.BrowserControl != null)
if (control.Browser != null)
{
control.BrowserControl.Reload();
control.Browser.Reload();
}
});
}
Expand Down
30 changes: 15 additions & 15 deletions CefSharp.WinForms.Example/BrowserTabUserControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ namespace CefSharp.WinForms.Example
{
public partial class BrowserTabUserControl : UserControl
{
public IChromiumHostControl BrowserControl { get; private set; }
public IChromiumWebBrowserBase Browser { get; private set; }
private ChromiumWidgetNativeWindow messageInterceptor;
private bool multiThreadedMessageLoopEnabled;

public BrowserTabUserControl(ChromiumHostControl chromiumHostControl)
{
InitializeComponent();

BrowserControl = chromiumHostControl;
Browser = chromiumHostControl;

browserPanel.Controls.Add(chromiumHostControl);

Expand All @@ -52,7 +52,7 @@ public BrowserTabUserControl(Action<string, int?> openNewTab, string url, bool m

browserPanel.Controls.Add(browser);

BrowserControl = browser;
Browser = browser;

browser.MenuHandler = new MenuHandler();
browser.RequestHandler = new WinFormsRequestHandler(openNewTab);
Expand Down Expand Up @@ -288,7 +288,7 @@ private void SetIsLoading(bool isLoading)
private void OnIsBrowserInitializedChanged(object sender, EventArgs e)
{
//Get the underlying browser host wrapper
var browserHost = BrowserControl.BrowserCore.GetHost();
var browserHost = Browser.BrowserCore.GetHost();
var requestContext = browserHost.RequestContext;
string errorMessage;
// Browser must be initialized before getting/setting preferences
Expand Down Expand Up @@ -335,9 +335,9 @@ private void SetupMessageInterceptor()
while (true)
{
IntPtr chromeWidgetHostHandle;
if (ChromiumRenderWidgetHandleFinder.TryFindHandle(BrowserControl.BrowserCore, out chromeWidgetHostHandle))
if (ChromiumRenderWidgetHandleFinder.TryFindHandle(Browser.BrowserCore, out chromeWidgetHostHandle))
{
messageInterceptor = new ChromiumWidgetNativeWindow((Control)BrowserControl, chromeWidgetHostHandle);
messageInterceptor = new ChromiumWidgetNativeWindow((Control)Browser, chromeWidgetHostHandle);
messageInterceptor.OnWndProc(message =>
{
Expand Down Expand Up @@ -434,12 +434,12 @@ private void GoButtonClick(object sender, EventArgs e)

private void BackButtonClick(object sender, EventArgs e)
{
BrowserControl.GoBack();
Browser.Back();
}

private void ForwardButtonClick(object sender, EventArgs e)
{
BrowserControl.GoForward();
Browser.Forward();
}

private void UrlTextBoxKeyUp(object sender, KeyEventArgs e)
Expand All @@ -456,20 +456,20 @@ private void LoadUrl(string url)
{
if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
{
BrowserControl.LoadUrl(url);
Browser.LoadUrl(url);
}
else
{
var searchUrl = "https://www.google.com/search?q=" + Uri.EscapeDataString(url);

BrowserControl.LoadUrl(searchUrl);
Browser.LoadUrl(searchUrl);
}

}

public async void CopySourceToClipBoardAsync()
{
var htmlSource = await BrowserControl.GetSourceAsync();
var htmlSource = await Browser.GetSourceAsync();

Clipboard.SetText(htmlSource);
DisplayOutput("HTML Source copied to clipboard");
Expand All @@ -479,7 +479,7 @@ private void ToggleBottomToolStrip()
{
if (toolStrip2.Visible)
{
BrowserControl.StopFinding(true);
Browser.StopFinding(true);
toolStrip2.Visible = false;
}
else
Expand All @@ -503,7 +503,7 @@ private void Find(bool next)
{
if (!string.IsNullOrEmpty(findTextBox.Text))
{
BrowserControl.Find(0, findTextBox.Text, next, false, false);
Browser.Find(0, findTextBox.Text, next, false, false);
}
}

Expand Down Expand Up @@ -542,7 +542,7 @@ public void ShowDevToolsDocked()

if (devToolsControl == null || devToolsControl.IsDisposed)
{
devToolsControl = BrowserControl.ShowDevToolsDocked(
devToolsControl = Browser.ShowDevToolsDocked(
parentControl: browserSplitContainer.Panel2,
controlName: nameof(devToolsControl));

Expand All @@ -563,7 +563,7 @@ public Task<bool> CheckIfDevToolsIsOpenAsync()
{
return Cef.UIThreadTaskFactory.StartNew(() =>
{
return BrowserControl.GetBrowserHost().HasDevTools;
return Browser.GetBrowserHost().HasDevTools;
});
}
}
Expand Down
Loading

0 comments on commit b72d413

Please sign in to comment.