Skip to content

Commit

Permalink
Fixing popup focus behavior and issue cefsharp#213
Browse files Browse the repository at this point in the history
  • Loading branch information
brock8503 committed Apr 21, 2014
1 parent 0f1e188 commit 4be44f1
Showing 1 changed file with 55 additions and 34 deletions.
89 changes: 55 additions & 34 deletions CefSharp.Wpf/WebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDisposable> disposables = new List<IDisposable>();

public BrowserSettings BrowserSettings { get; set; }
Expand Down Expand Up @@ -425,38 +426,63 @@ 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;

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)
Expand Down Expand Up @@ -583,18 +609,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);
});
}

Expand Down Expand Up @@ -653,14 +668,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)
Expand Down Expand Up @@ -776,6 +786,12 @@ protected override void OnMouseWheel(MouseWheelEventArgs e)
);
}

protected void PopupMouseEnter(object sender, MouseEventArgs e)
{
Focus();
Mouse.Capture(this);
}

protected override void OnMouseDown(MouseButtonEventArgs e)
{
Focus();
Expand All @@ -789,6 +805,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);
Expand Down

0 comments on commit 4be44f1

Please sign in to comment.