diff --git a/CefSharp.Wpf/WebView.cs b/CefSharp.Wpf/WebView.cs index 5f2d1bbcf3..c307dad915 100644 --- a/CefSharp.Wpf/WebView.cs +++ b/CefSharp.Wpf/WebView.cs @@ -38,6 +38,7 @@ public class WebView : ContentControl, IRenderWebBrowser, IWpfWebBrowser private Image image; private Image popupImage; private Popup popup; + private ScaleTransform dpiTransform; private readonly List disposables = new List(); public BrowserSettings BrowserSettings { get; set; } @@ -425,38 +426,64 @@ public override void OnApplyTemplate() { base.OnApplyTemplate(); - Content = image = new Image(); - - // If the display properties is set to 125%, M11 and M22 will be 1.25. - var factorX = matrix.M11; - var factorY = matrix.M22; - var scaleX = 1 / factorX; - var scaleY = 1 / factorY; - image.LayoutTransform = new ScaleTransform(scaleX, scaleY); + AddSourceHookIfNotAlreadyPresent(); + CheckIsNonStandardDpi(); + + // Create main window + Content = image = CreateImage(); + Transform(image); + popup = CreatePopup(); + Transform(popup); + } - AddSourceHookIfNotAlreadyPresent(); + private Image CreateImage() + { + Image temp = new Image(); - RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor); + RenderOptions.SetBitmapScalingMode(temp, BitmapScalingMode.NearestNeighbor); - image.Stretch = Stretch.None; - image.HorizontalAlignment = HorizontalAlignment.Left; - image.VerticalAlignment = VerticalAlignment.Top; + temp.Stretch = Stretch.None; + temp.HorizontalAlignment = HorizontalAlignment.Left; + temp.VerticalAlignment = VerticalAlignment.Top; + return temp; } private Popup CreatePopup() { var popup = new Popup { - Child = popupImage = new Image(), + Child = popupImage = CreateImage(), PlacementTarget = this, - Placement = PlacementMode.Relative + Placement = PlacementMode.Relative, }; + popup.MouseEnter += this.PopupMouseEnter; + popup.MouseLeave += this.PopupMouseLeave; + return popup; } + private void Transform(FrameworkElement element) + { + if (dpiTransform != null) + { + element.LayoutTransform = dpiTransform; + } + } + + private void CheckIsNonStandardDpi() + { + if (matrix != null) // make sure it's connected + { + dpiTransform = new ScaleTransform( + 1 / matrix.M11, + 1 / matrix.M22 + ); + } + } + private void AddSourceHookIfNotAlreadyPresent() { if (source != null) @@ -583,18 +610,7 @@ public void SetPopupSizeAndPosition(int width, int height, int x, int y) { DoInUi(() => { - popup.Width = width; - popup.Height = height; - - var popupOffset = new Point(x, y); - // TODO: Port over this from CefSharp1. - //if (popupOffsetTransform != null) - //{ - // popupOffset = popupOffsetTransform->GeneralTransform::Transform(popupOffset); - //} - - popup.HorizontalOffset = popupOffset.X; - popup.VerticalOffset = popupOffset.Y; + this.SetPopupSizeAndPositionImpl(width, height, x, y); }); } @@ -653,14 +669,9 @@ private void SetPopupSizeAndPositionImpl(int width, int height, int x, int y) popup.Height = height; var popupOffset = new Point(x, y); - // TODO: Port over this from CefSharp1. - //if (popupOffsetTransform != null) - //{ - // popupOffset = popupOffsetTransform->GeneralTransform::Transform(popupOffset); - //} - popup.HorizontalOffset = popupOffset.X; - popup.VerticalOffset = popupOffset.Y; + popup.HorizontalOffset = popupOffset.X / matrix.M11; + popup.VerticalOffset = popupOffset.Y / matrix.M22; } private void OnTooltipTimerTick(object sender, EventArgs e) @@ -776,6 +787,17 @@ protected override void OnMouseWheel(MouseWheelEventArgs e) ); } + protected void PopupMouseEnter(object sender, MouseEventArgs e) + { + Focus(); + Mouse.Capture(this); + } + + protected void PopupMouseLeave(object sender, MouseEventArgs e) + { + Mouse.Capture(null); + } + protected override void OnMouseDown(MouseButtonEventArgs e) { Focus(); @@ -789,6 +811,11 @@ protected override void OnMouseUp(MouseButtonEventArgs e) Mouse.Capture(null); } + protected override void OnMouseEnter(MouseEventArgs e) + { + base.OnMouseEnter(e); + } + protected override void OnMouseLeave(MouseEventArgs e) { var modifiers = GetModifiers(e);