From b2ddad3cdfd7cc5865da47cda3cf571532eaa56c Mon Sep 17 00:00:00 2001 From: brock8503 Date: Sat, 12 Apr 2014 18:48:34 -0700 Subject: [PATCH] Fixing popup focus behavior and issue #213 --- CefSharp.Wpf/WebView.cs | 99 ++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 36 deletions(-) diff --git a/CefSharp.Wpf/WebView.cs b/CefSharp.Wpf/WebView.cs index 5f2d1bbcf..c307dad91 100644 --- a/CefSharp.Wpf/WebView.cs +++ b/CefSharp.Wpf/WebView.cs @@ -38,6 +38,7 @@ namespace CefSharp.Wpf 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 @@ namespace CefSharp.Wpf { 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); - - popup = CreatePopup(); - AddSourceHookIfNotAlreadyPresent(); - RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor); + CheckIsNonStandardDpi(); + + // Create main window + Content = image = CreateImage(); + Transform(image); + + popup = CreatePopup(); + Transform(popup); + } - image.Stretch = Stretch.None; - image.HorizontalAlignment = HorizontalAlignment.Left; - image.VerticalAlignment = VerticalAlignment.Top; + private Image CreateImage() + { + Image temp = new Image(); + + RenderOptions.SetBitmapScalingMode(temp, BitmapScalingMode.NearestNeighbor); + + 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 @@ namespace CefSharp.Wpf { 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 @@ namespace CefSharp.Wpf 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 @@ namespace CefSharp.Wpf ); } + 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 @@ namespace CefSharp.Wpf Mouse.Capture(null); } + protected override void OnMouseEnter(MouseEventArgs e) + { + base.OnMouseEnter(e); + } + protected override void OnMouseLeave(MouseEventArgs e) { var modifiers = GetModifiers(e);