Skip to content

Commit

Permalink
Revert "WPF - Resize Hack for when browser switches from hidden to vi…
Browse files Browse the repository at this point in the history
…sible (#2779)"

This reverts commit a83c8b3.
  • Loading branch information
amaitland committed Feb 9, 2021
1 parent c566906 commit 47a3fe1
Showing 1 changed file with 22 additions and 184 deletions.
206 changes: 22 additions & 184 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,12 @@ public partial class ChromiumWebBrowser : Control, IRenderWebBrowser, IWpfWebBro
/// </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
/// </summary>
private int disposeSignaled;

/// <summary>
/// Hack to work around issue https://github.com/cefsharp/CefSharp/issues/2779
/// Enabled by default
/// </summary>
public bool EnableResizeHackForIssue2779 { get; set; }

/// <summary>
/// Number of miliseconds to wait after resizing the browser when it first
/// becomes visible. After the delay the browser will revert to it's
/// original size.
/// Hack to work around issue https://github.com/cefsharp/CefSharp/issues/2779
/// </summary>
public int ResizeHackForIssue2279DelayInMs { get; set; }

/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
Expand Down Expand Up @@ -510,9 +493,6 @@ public ChromiumWebBrowser(string initialAddress)
[MethodImpl(MethodImplOptions.NoInlining)]
private void NoInliningConstructor()
{
EnableResizeHackForIssue2779 = true;
ResizeHackForIssue2279DelayInMs = 50;

//Initialize CEF if it hasn't already been initialized
if (!Cef.IsInitialized)
{
Expand Down Expand Up @@ -779,18 +759,7 @@ Rect IRenderWebBrowser.GetViewRect()
/// <returns>View Rectangle</returns>
protected virtual Rect GetViewRect()
{
//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);
return viewRect;
}

bool IRenderWebBrowser.GetScreenPoint(int viewX, int viewY, out int screenX, out int screenY)
Expand Down Expand Up @@ -935,11 +904,6 @@ 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 @@ -1668,7 +1632,7 @@ private void PresentationSourceChangedHandler(object sender, SourceChangedEventA
}
}

private async void OnWindowStateChanged(object sender, EventArgs e)
private void OnWindowStateChanged(object sender, EventArgs e)
{
var window = (Window)sender;

Expand All @@ -1679,54 +1643,16 @@ private async void OnWindowStateChanged(object sender, EventArgs e)
{
if (previousWindowState == WindowState.Minimized && browser != null)
{
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
}
}
});
browser.GetHost().WasHidden(false);
}

break;
}
case WindowState.Minimized:
{
await CefUiThreadRunAsync(() =>
if (browser != null)
{
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
}
}
});

browser.GetHost().WasHidden(true);
}
break;
}
}
Expand Down Expand Up @@ -1869,24 +1795,6 @@ internal void UiThreadRunAsync(Action action, DispatcherPriority priority = Disp
}
}

protected async Task CefUiThreadRunAsync(Action action)
{
if (!IsDisposed && InternalIsBrowserInitialized())
{
if (Cef.CurrentlyOnThread(CefThreadIds.TID_UI))
{
action();
}
else
{
await Cef.UIThreadTaskFactory.StartNew(delegate
{
action();
});
}
}
}

/// <summary>
/// Runs the specific Action on the Dispatcher in an sync fashion
/// </summary>
Expand All @@ -1909,97 +1817,52 @@ private void UiThreadRunSync(Action action, DispatcherPriority priority = Dispat
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="SizeChangedEventArgs"/> instance containing the event data.</param>
private async void OnActualSizeChanged(object sender, SizeChangedEventArgs e)
private 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();
}
}

/// <summary>
/// Handles the <see cref="E:IsVisibleChanged" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="args">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private async void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var isVisible = (bool)args.NewValue;

if (browser != null)
{
await CefUiThreadRunAsync(async () =>
{
var host = browser?.GetHost();
if (host != null && !host.IsDisposed)
{
try
{
host.WasHidden(!isVisible);
var host = browser.GetHost();
host.WasHidden(!isVisible);

if (isVisible)
{
await ResizeHackFor2779();
}
else if (EnableResizeHackForIssue2779)
{
resizeHackForIssue2779Enabled = true;
}
}
catch (ObjectDisposedException)
{
// See comment in catch in OnWindowStateChanged
}
}
});

if (browser != null)
if (isVisible)
{
//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
var zoomLevel = await browser.GetHost().GetZoomLevelAsync();

UiThreadRunAsync(() =>
host.GetZoomLevelAsync().ContinueWith(t =>
{
if (!IsDisposed)
{
SetCurrentValue(ZoomLevelProperty, zoomLevel);
SetCurrentValue(ZoomLevelProperty, t.Result);
}
});
},
CancellationToken.None,
TaskContinuationOptions.OnlyOnRanToCompletion,
TaskScheduler.FromCurrentSynchronizationContext());
}
}
}
Expand Down Expand Up @@ -2631,30 +2494,5 @@ public IBrowser GetBrowser()

return browser;
}

private async Task ResizeHackFor2779()
{
if (EnableResizeHackForIssue2779)
{
var host = browser?.GetHost();
if (host != null && !host.IsDisposed)
{
resizeHackForIssue2779Size = new Structs.Size(viewRect.Width + 1, viewRect.Height + 1);
host.WasResized();

await Task.Delay(ResizeHackForIssue2279DelayInMs);

if (!host.IsDisposed)
{
resizeHackForIssue2779Size = null;
host.WasResized();

resizeHackForIssue2779Enabled = false;

host.Invalidate(PaintElementType.View);
}
}
}
}
}
}

0 comments on commit 47a3fe1

Please sign in to comment.