mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 04:00:07 +00:00
WPF - Resize Hack for when browser switches from hidden to visible (#2779)
This commit is contained in:
@@ -134,12 +134,21 @@ namespace CefSharp.Wpf
|
||||
/// </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>
|
||||
/// Gets a value indicating whether this instance is disposed.
|
||||
/// </summary>
|
||||
@@ -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
|
||||
/// <returns>View Rectangle</returns>
|
||||
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
|
||||
/// <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)
|
||||
{
|
||||
@@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the specific Action on the Dispatcher in an sync fashion
|
||||
/// </summary>
|
||||
@@ -1938,20 +2021,44 @@ namespace CefSharp.Wpf
|
||||
/// </summary>
|
||||
/// <param name="sender">The sender.</param>
|
||||
/// <param name="e">The <see cref="SizeChangedEventArgs"/> instance containing the event data.</param>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1959,31 +2066,52 @@ namespace CefSharp.Wpf
|
||||
/// </summary>
|
||||
/// <param name="sender">The sender.</param>
|
||||
/// <param name="args">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user