Skip to content

Commit

Permalink
WPF - Black browser in TabControl/When Hidden
Browse files Browse the repository at this point in the history
Hack for issue #2779
  • Loading branch information
amaitland committed Jul 26, 2019
1 parent 4f2e721 commit 0d67b05
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public class ChromiumWebBrowser : Control, IRenderWebBrowser, IWpfWebBrowser
/// </summary>
private static bool DesignMode;

private bool resizeHackForIssue2779Enabled;
private CefSharp.Structs.Size? resizeHackForIssue2779Size;

/// <summary>
/// The value for disposal, if it's 1 (one) then this instance is either disposed
/// or in the process of getting disposed
Expand Down Expand Up @@ -743,7 +746,7 @@ private void InternalDispose(bool disposing)

var screenInfo = new ScreenInfo
{
DeviceScaleFactor = (float)DpiScaleFactor,
DeviceScaleFactor = DpiScaleFactor,
Rect = rect,
AvailableRect = availableRect
};
Expand All @@ -768,7 +771,14 @@ Rect IRenderWebBrowser.GetViewRect()
/// <returns>View Rectangle</returns>
protected virtual Rect GetViewRect()
{
return viewRect;
if (resizeHackForIssue2779Size == null)
{
return viewRect;
}

var size = resizeHackForIssue2779Size.Value;

This comment has been minimized.

Copy link
@mol

mol Aug 11, 2019

Contributor

@amaitland I have reports that resizeHackForIssue2779Size.Value can be null here, resulting in:

System.InvalidOperationException: Nullable object must have a value. 
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) 
at CefSharp.Wpf.ChromiumWebBrowser.GetViewRect() 
at CefSharp.Internals.RenderClientAdapter.GetViewRect(RenderClientAdapter* , scoped_refptr<CefBrowser>* browser, CefRect* rect)

So it must have changed between the check and usage. It might be related to our reuse of the browsers so I don't know if I should create a pull request, but in Mailbird I'm going to set a local variable to resizeHackForIssue2779Size and then use that for checking and the value.

This comment has been minimized.

Copy link
@amaitland

amaitland Aug 11, 2019

Author Member

Thanks, a local copy seems very reasonable, assignment and usage are on different threads.

Pull request would be welcomed.

This comment has been minimized.

Copy link
@amaitland

amaitland Aug 11, 2019

Author Member

Commit e5749ad should hopefully resolve this, thanks 👍


return new Rect(0, 0, size.Width, size.Height);
}

bool IRenderWebBrowser.GetScreenPoint(int viewX, int viewY, out int screenX, out int screenY)
Expand Down Expand Up @@ -910,6 +920,11 @@ void IRenderWebBrowser.OnPaint(PaintElementType type, Rect dirtyRect, IntPtr buf
/// <param name="height">height</param>
protected virtual void OnPaint(bool isPopup, Rect dirtyRect, IntPtr buffer, int width, int height)
{
if (resizeHackForIssue2779Enabled)
{
return;
}

var paint = Paint;
if (paint != null)
{
Expand Down Expand Up @@ -1737,10 +1752,15 @@ private void OnWindowStateChanged(object sender, EventArgs e)
{
browser.GetHost().WasHidden(false);
}

ResizeHackFor2779();

break;
}
case WindowState.Minimized:
{
resizeHackForIssue2779Enabled = true;

if (browser != null)
{
browser.GetHost().WasHidden(true);
Expand Down Expand Up @@ -1892,6 +1912,8 @@ private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArg

if (isVisible)
{
ResizeHackFor2779();

//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
Expand All @@ -1907,6 +1929,10 @@ private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArg
TaskContinuationOptions.OnlyOnRanToCompletion,
TaskScheduler.FromCurrentSynchronizationContext());
}
else
{
resizeHackForIssue2779Enabled = true;
}
}
}

Expand Down Expand Up @@ -2357,5 +2383,38 @@ private bool InternalIsBrowserInitialized()
// 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 void ResizeHackFor2779()
{
const int delayInMs = 50;

Task.Run(async () =>
{
await Task.Delay(delayInMs);
if (browser != null)
{
resizeHackForIssue2779Size = new Structs.Size(viewRect.Width - 1, viewRect.Height - 1);
browser.GetHost().WasResized();
}
await Task.Delay(delayInMs);
if (browser != null)
{
resizeHackForIssue2779Size = null;
browser.GetHost().WasResized();
}
await Task.Delay(delayInMs);
if (browser != null)
{
resizeHackForIssue2779Enabled = false;
browser.GetHost().Invalidate(PaintElementType.View);
}
});
}
}
}

0 comments on commit 0d67b05

Please sign in to comment.