diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs
index e954f3ff9..12f84d109 100644
--- a/CefSharp.Wpf/ChromiumWebBrowser.cs
+++ b/CefSharp.Wpf/ChromiumWebBrowser.cs
@@ -134,12 +134,21 @@ namespace CefSharp.Wpf
///
private static bool DesignMode;
+ private bool resizeHackForIssue2779Enabled;
+ private CefSharp.Structs.Size? resizeHackForIssue2779Size;
+
///
/// The value for disposal, if it's 1 (one) then this instance is either disposed
/// or in the process of getting disposed
///
private int disposeSignaled;
+ ///
+ /// Hack to work around issue https://github.com/cefsharp/CefSharp/issues/2779
+ /// Enabled by default
+ ///
+ public bool EnableResizeHackForIssue2779 { get; set; }
+
///
/// Gets a value indicating whether this instance is disposed.
///
@@ -557,6 +566,8 @@ namespace CefSharp.Wpf
[MethodImpl(MethodImplOptions.NoInlining)]
private void NoInliningConstructor()
{
+ EnableResizeHackForIssue2779 = true;
+
//Initialize CEF if it hasn't already been initialized
if (!Cef.IsInitialized)
{
@@ -822,7 +833,18 @@ namespace CefSharp.Wpf
/// View Rectangle
protected virtual Rect GetViewRect()
{
- return viewRect;
+ //Take a local copy as the value is set on a different thread,
+ //Its possible the struct is set to null after our initial check.
+ var resizeRect = resizeHackForIssue2779Size;
+
+ if (resizeRect == null)
+ {
+ return viewRect;
+ }
+
+ var size = resizeRect.Value;
+
+ return new Rect(0, 0, size.Width, size.Height);
}
bool IRenderWebBrowser.GetScreenPoint(int viewX, int viewY, out int screenX, out int screenY)
@@ -967,6 +989,11 @@ namespace CefSharp.Wpf
/// height
protected virtual void OnPaint(bool isPopup, Rect dirtyRect, IntPtr buffer, int width, int height)
{
+ if (resizeHackForIssue2779Enabled)
+ {
+ return;
+ }
+
var paint = Paint;
if (paint != null)
{
@@ -1759,7 +1786,7 @@ namespace CefSharp.Wpf
}
}
- private void OnWindowStateChanged(object sender, EventArgs e)
+ private async void OnWindowStateChanged(object sender, EventArgs e)
{
var window = (Window)sender;
@@ -1770,16 +1797,54 @@ namespace CefSharp.Wpf
{
if (previousWindowState == WindowState.Minimized && browser != null)
{
- browser.GetHost().WasHidden(false);
+ await CefUiThreadRunAsync(async () =>
+ {
+ var host = browser?.GetHost();
+ if (host != null && !host.IsDisposed)
+ {
+ try
+ {
+ host.WasHidden(false);
+
+ await ResizeHackFor2779();
+ }
+ catch (ObjectDisposedException)
+ {
+ // Because Dispose runs in another thread there's a race condition between
+ // that and this code running on the CEF UI thread, so the host could be disposed
+ // between the check and using it. We can either synchronize access using locking
+ // (potentially blocking the UI thread in Dispose) or catch the extremely rare
+ // exception, which is what we do here
+ }
+ }
+ });
}
+
break;
}
case WindowState.Minimized:
{
- if (browser != null)
+ await CefUiThreadRunAsync(() =>
{
- browser.GetHost().WasHidden(true);
- }
+ var host = browser?.GetHost();
+ if (host != null && !host.IsDisposed)
+ {
+ if (EnableResizeHackForIssue2779)
+ {
+ resizeHackForIssue2779Enabled = true;
+ }
+
+ try
+ {
+ host.WasHidden(true);
+ }
+ catch (ObjectDisposedException)
+ {
+ // See comment in catch in OnWindowStateChanged
+ }
+ }
+ });
+
break;
}
}
@@ -1916,6 +1981,24 @@ namespace CefSharp.Wpf
}
}
+ protected async Task CefUiThreadRunAsync(Action action)
+ {
+ if (!IsDisposed && InternalIsBrowserInitialized())
+ {
+ if (Cef.CurrentlyOnThread(CefThreadIds.TID_UI))
+ {
+ action();
+ }
+ else
+ {
+ await Cef.UIThreadTaskFactory.StartNew(delegate
+ {
+ action();
+ });
+ }
+ }
+ }
+
///
/// Runs the specific Action on the Dispatcher in an sync fashion
///
@@ -1938,20 +2021,44 @@ namespace CefSharp.Wpf
///
/// The sender.
/// The instance containing the event data.
- private void OnActualSizeChanged(object sender, SizeChangedEventArgs e)
+ private async void OnActualSizeChanged(object sender, SizeChangedEventArgs e)
{
// Initialize RenderClientAdapter when WPF has calculated the actual size of current content.
CreateOffscreenBrowser(e.NewSize);
+ if (InternalIsBrowserInitialized())
+ {
+ await CefUiThreadRunAsync(() =>
+ {
+ SetViewRect(e);
+
+ var host = browser?.GetHost();
+ if (host != null && !host.IsDisposed)
+ {
+ try
+ {
+ host.WasResized();
+ }
+ catch (ObjectDisposedException)
+ {
+ // See comment in catch in OnWindowStateChanged
+ }
+ }
+ });
+ }
+ else
+ {
+ //If the browser hasn't been created yet then directly update the viewRect
+ SetViewRect(e);
+ }
+ }
+
+ private void SetViewRect(SizeChangedEventArgs e)
+ {
//NOTE: Previous we used Math.Ceiling to round the sizing up, we
//now set UseLayoutRounding = true; on the control so the sizes are
//already rounded to a whole number for us.
viewRect = new Rect(0, 0, (int)e.NewSize.Width, (int)e.NewSize.Height);
-
- if (browser != null)
- {
- browser.GetHost().WasResized();
- }
}
///
@@ -1959,31 +2066,52 @@ namespace CefSharp.Wpf
///
/// The sender.
/// The instance containing the event data.
- private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
+ private async void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var isVisible = (bool)args.NewValue;
if (browser != null)
{
- var host = browser.GetHost();
- host.WasHidden(!isVisible);
+ await CefUiThreadRunAsync(async () =>
+ {
+ var host = browser?.GetHost();
+ if (host != null && !host.IsDisposed)
+ {
+ try
+ {
+ host.WasHidden(!isVisible);
- if (isVisible)
+ if (isVisible)
+ {
+ await ResizeHackFor2779();
+ }
+ else if (EnableResizeHackForIssue2779)
+ {
+ resizeHackForIssue2779Enabled = true;
+ }
+ }
+ catch (ObjectDisposedException)
+ {
+ // See comment in catch in OnWindowStateChanged
+ }
+ }
+ });
+
+ if (browser != null)
{
//Fix for #1778 - When browser becomes visible we update the zoom level
//browsers of the same origin will share the same zoomlevel and
//we need to track the update, so our ZoomLevelProperty works
//properly
- host.GetZoomLevelAsync().ContinueWith(t =>
+ var zoomLevel = await browser.GetHost().GetZoomLevelAsync();
+
+ UiThreadRunAsync(() =>
{
if (!IsDisposed)
{
- SetCurrentValue(ZoomLevelProperty, t.Result);
+ SetCurrentValue(ZoomLevelProperty, zoomLevel);
}
- },
- CancellationToken.None,
- TaskContinuationOptions.OnlyOnRanToCompletion,
- TaskScheduler.FromCurrentSynchronizationContext());
+ });
}
}
}
@@ -2625,5 +2753,32 @@ namespace CefSharp.Wpf
// Volatile.Read would likely use a memory barrier which I believe is unnecessary in this scenario
return Interlocked.CompareExchange(ref browserInitialized, 0, 0) == 1;
}
+
+ private async Task ResizeHackFor2779()
+ {
+ if (EnableResizeHackForIssue2779)
+ {
+ const int delayInMs = 50;
+
+ var host = browser?.GetHost();
+ if (host != null && !host.IsDisposed)
+ {
+ resizeHackForIssue2779Size = new Structs.Size(viewRect.Width - 1, viewRect.Height - 1);
+ host.WasResized();
+
+ await Task.Delay(delayInMs);
+
+ if (!host.IsDisposed)
+ {
+ resizeHackForIssue2779Size = null;
+ host.WasResized();
+
+ resizeHackForIssue2779Enabled = false;
+
+ host.Invalidate(PaintElementType.View);
+ }
+ }
+ }
+ }
}
}