Skip to content

Commit

Permalink
WPF - Change from storing a reference to the Matrix to having a DpiSc…
Browse files Browse the repository at this point in the history
…aleFactor property, can be used to manually set the scale factor.

If the scale factor is set after the browser has been created then it's necessary to manually call IBrowserHost.NotifyScreenInfoChanged
  • Loading branch information
amaitland committed May 30, 2017
1 parent fe026a8 commit e94f063
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrow
/// </summary>
private int browserInitialized;
/// <summary>
/// The matrix
/// </summary>
private Matrix matrix;
/// <summary>
/// The image that represents this browser instances
/// </summary>
private Image image;
Expand Down Expand Up @@ -339,6 +335,13 @@ public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrow
/// </summary>
public bool CanExecuteJavascriptInMainFrame { get; private set; }

/// <summary>
/// The dpi scale factor, if the browser has already been initialized
/// you must manually call IBrowserHost.NotifyScreenInfoChanged for the
/// browser to be notified of the change.
/// </summary>
public double DpiScaleFactor { get; set; }

/// <summary>
/// Initializes static members of the <see cref="ChromiumWebBrowser"/> class.
/// </summary>
Expand Down Expand Up @@ -568,7 +571,7 @@ ScreenInfo IRenderWebBrowser.GetScreenInfo()
/// <returns>ScreenInfo containing the current DPI scale factor</returns>
protected virtual ScreenInfo GetScreenInfo()
{
var screenInfo = new ScreenInfo(scaleFactor: (float)matrix.M11);
var screenInfo = new ScreenInfo(scaleFactor: (float)DpiScaleFactor);

return screenInfo;
}
Expand Down Expand Up @@ -636,7 +639,7 @@ protected virtual BitmapInfo CreateBitmapInfo(bool isPopup)
{
throw new Exception("BitmapFactory cannot be null");
}
return BitmapFactory.CreateBitmap(isPopup, matrix.M11);
return BitmapFactory.CreateBitmap(isPopup, DpiScaleFactor);
}

/// <summary>
Expand Down Expand Up @@ -1514,18 +1517,16 @@ private void PresentationSourceChangedHandler(object sender, SourceChangedEventA

if (source != null)
{
var notifyDpiChanged = !matrix.Equals(source.CompositionTarget.TransformToDevice);
var matrix = source.CompositionTarget.TransformToDevice;
var notifyDpiChanged = DpiScaleFactor > 0 && !DpiScaleFactor.Equals(matrix.M11);

matrix = source.CompositionTarget.TransformToDevice;
DpiScaleFactor = source.CompositionTarget.TransformToDevice.M11;
sourceHook = SourceHook;
source.AddHook(sourceHook);

if (notifyDpiChanged)
if (notifyDpiChanged && browser != null)
{
if(browser != null)
{
browser.GetHost().NotifyScreenInfoChanged();
}
browser.GetHost().NotifyScreenInfoChanged();
}

var window = source.RootVisual as Window;
Expand Down Expand Up @@ -1860,8 +1861,8 @@ private void SetPopupSizeAndPositionImpl(int width, int height, int x, int y)

var popupOffset = new Point(x, y);
var locationFromScreen = PointToScreen(popupOffset);
popup.HorizontalOffset = locationFromScreen.X / matrix.M11;
popup.VerticalOffset = locationFromScreen.Y / matrix.M22;
popup.HorizontalOffset = locationFromScreen.X / DpiScaleFactor;
popup.VerticalOffset = locationFromScreen.Y / DpiScaleFactor;
}

/// <summary>
Expand Down

0 comments on commit e94f063

Please sign in to comment.