Skip to content

Commit 0d67b05

Browse files
committed
WPF - Black browser in TabControl/When Hidden
Hack for issue #2779
1 parent 4f2e721 commit 0d67b05

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ public class ChromiumWebBrowser : Control, IRenderWebBrowser, IWpfWebBrowser
129129
/// </summary>
130130
private static bool DesignMode;
131131

132+
private bool resizeHackForIssue2779Enabled;
133+
private CefSharp.Structs.Size? resizeHackForIssue2779Size;
134+
132135
/// <summary>
133136
/// The value for disposal, if it's 1 (one) then this instance is either disposed
134137
/// or in the process of getting disposed
@@ -743,7 +746,7 @@ private void InternalDispose(bool disposing)
743746

744747
var screenInfo = new ScreenInfo
745748
{
746-
DeviceScaleFactor = (float)DpiScaleFactor,
749+
DeviceScaleFactor = DpiScaleFactor,
747750
Rect = rect,
748751
AvailableRect = availableRect
749752
};
@@ -768,7 +771,14 @@ Rect IRenderWebBrowser.GetViewRect()
768771
/// <returns>View Rectangle</returns>
769772
protected virtual Rect GetViewRect()
770773
{
771-
return viewRect;
774+
if (resizeHackForIssue2779Size == null)
775+
{
776+
return viewRect;
777+
}
778+
779+
var size = resizeHackForIssue2779Size.Value;
780+
781+
return new Rect(0, 0, size.Width, size.Height);
772782
}
773783

774784
bool IRenderWebBrowser.GetScreenPoint(int viewX, int viewY, out int screenX, out int screenY)
@@ -910,6 +920,11 @@ void IRenderWebBrowser.OnPaint(PaintElementType type, Rect dirtyRect, IntPtr buf
910920
/// <param name="height">height</param>
911921
protected virtual void OnPaint(bool isPopup, Rect dirtyRect, IntPtr buffer, int width, int height)
912922
{
923+
if (resizeHackForIssue2779Enabled)
924+
{
925+
return;
926+
}
927+
913928
var paint = Paint;
914929
if (paint != null)
915930
{
@@ -1737,10 +1752,15 @@ private void OnWindowStateChanged(object sender, EventArgs e)
17371752
{
17381753
browser.GetHost().WasHidden(false);
17391754
}
1755+
1756+
ResizeHackFor2779();
1757+
17401758
break;
17411759
}
17421760
case WindowState.Minimized:
17431761
{
1762+
resizeHackForIssue2779Enabled = true;
1763+
17441764
if (browser != null)
17451765
{
17461766
browser.GetHost().WasHidden(true);
@@ -1892,6 +1912,8 @@ private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArg
18921912

18931913
if (isVisible)
18941914
{
1915+
ResizeHackFor2779();
1916+
18951917
//Fix for #1778 - When browser becomes visible we update the zoom level
18961918
//browsers of the same origin will share the same zoomlevel and
18971919
//we need to track the update, so our ZoomLevelProperty works
@@ -1907,6 +1929,10 @@ private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArg
19071929
TaskContinuationOptions.OnlyOnRanToCompletion,
19081930
TaskScheduler.FromCurrentSynchronizationContext());
19091931
}
1932+
else
1933+
{
1934+
resizeHackForIssue2779Enabled = true;
1935+
}
19101936
}
19111937
}
19121938

@@ -2357,5 +2383,38 @@ private bool InternalIsBrowserInitialized()
23572383
// Volatile.Read would likely use a memory barrier which I believe is unnecessary in this scenario
23582384
return Interlocked.CompareExchange(ref browserInitialized, 0, 0) == 1;
23592385
}
2386+
2387+
private void ResizeHackFor2779()
2388+
{
2389+
const int delayInMs = 50;
2390+
2391+
Task.Run(async () =>
2392+
{
2393+
await Task.Delay(delayInMs);
2394+
2395+
if (browser != null)
2396+
{
2397+
resizeHackForIssue2779Size = new Structs.Size(viewRect.Width - 1, viewRect.Height - 1);
2398+
browser.GetHost().WasResized();
2399+
}
2400+
2401+
await Task.Delay(delayInMs);
2402+
2403+
if (browser != null)
2404+
{
2405+
resizeHackForIssue2779Size = null;
2406+
browser.GetHost().WasResized();
2407+
}
2408+
2409+
await Task.Delay(delayInMs);
2410+
2411+
if (browser != null)
2412+
{
2413+
resizeHackForIssue2779Enabled = false;
2414+
2415+
browser.GetHost().Invalidate(PaintElementType.View);
2416+
}
2417+
});
2418+
}
23602419
}
23612420
}

0 commit comments

Comments
 (0)