private Point PlacePopup(Rect window, Point[] target, Point[] flyout, PlacementMode placement)
{
Point[] pointArray;
double y = 0.0;
double x = 0.0;
Rect bounds = GetBounds(target);
Rect rect2 = GetBounds(flyout);
double width = rect2.Width;
double height = rect2.Height;
if (placement == PlacementMode.Right)
{
double num5 = Math.Max(0.0, target[0].X);
double num6 = window.Width - Math.Min(window.Width, target[1].X + 1.0);
if ((num6 < width) && (num6 < num5))
{
placement = PlacementMode.Left;
}
}
else if (placement == PlacementMode.Left)
{
double num7 = window.Width - Math.Min(window.Width, target[1].X + 1.0);
double num8 = Math.Max(0.0, target[0].X);
if ((num8 < width) && (num8 < num7))
{
placement = PlacementMode.Right;
}
}
else if (placement == PlacementMode.Top)
{
double num9 = Math.Max(0.0, target[0].Y);
double num10 = window.Height - Math.Min(window.Height, target[2].Y + 1.0);
if ((num9 < height) && (num9 < num10))
{
placement = PlacementMode.Bottom;
}
}
else if (placement == PlacementMode.Bottom)
{
double num11 = Math.Max(0.0, target[0].Y);
double num12 = window.Height - Math.Min(window.Height, target[2].Y + 1.0);
if ((num12 < height) && (num12 < num11))
{
placement = PlacementMode.Top;
}
}
switch (placement)
{
case PlacementMode.Bottom:
pointArray = new Point[] { new Point(target[2].X, Math.Max((double)0.0, (double)(target[2].Y + 1.0))), new Point((target[3].X - width) + 1.0, Math.Max((double)0.0, (double)(target[2].Y + 1.0))), new Point(0.0, Math.Max((double)0.0, (double)(target[2].Y + 1.0))) };
break;
case PlacementMode.Right:
pointArray = new Point[] { new Point(Math.Max((double)0.0, (double)(target[1].X + 1.0)), target[1].Y), new Point(Math.Max((double)0.0, (double)(target[3].X + 1.0)), (target[3].Y - height) + 1.0), new Point(Math.Max((double)0.0, (double)(target[1].X + 1.0)), 0.0) };
break;
case PlacementMode.Left:
pointArray = new Point[] { new Point(Math.Min(window.Width, target[0].X) - width, target[1].Y), new Point(Math.Min(window.Width, target[2].X) - width, (target[3].Y - height) + 1.0), new Point(Math.Min(window.Width, target[0].X) - width, 0.0) };
break;
case PlacementMode.Top:
pointArray = new Point[] { new Point(target[0].X, Math.Min(target[0].Y, window.Height) - height), new Point((target[1].X - width) + 1.0, Math.Min(target[0].Y, window.Height) - height), new Point(0.0, Math.Min(target[0].Y, window.Height) - height) };
break;
default:
pointArray = new Point[] { new Point(0.0, 0.0) };
break;
}
double num13 = width * height;
int index = 0;
double num15 = 0.0;
for (int i = 0; i < pointArray.Length; i++)
{
Rect rect3 = new Rect(pointArray[i].X, pointArray[i].Y, width, height);
rect3.Intersect(window);
double d = rect3.Width * rect3.Height;
if (double.IsInfinity(d))
{
index = pointArray.Length - 1;
break;
}
if (d > num15)
{
index = i;
num15 = d;
}
if (d == num13)
{
index = i;
break;
}
}
// x = pointArray[index].X;
x = pointArray[0].X; // TODO: taking this solves my horizontal nudging, but is a hack...keeping it though until a better solution
y = pointArray[index].Y;
if (index > 1)
{
if ((placement == PlacementMode.Left) || (placement == PlacementMode.Right))
{
if (((y != target[0].Y) && (y != target[1].Y)) && (((y + height) != target[0].Y) && ((y + height) != target[1].Y)))
{
double num18 = bounds.Top + (bounds.Height / 2.0);
if ((num18 > 0.0) && ((num18 - 0.0) > (window.Height - num18)))
{
y = window.Height - height;
}
else
{
y = 0.0;
}
}
}
else if (((placement == PlacementMode.Top) || (placement == PlacementMode.Bottom)) && (((x != target[0].X) && (x != target[1].X)) && (((x + width) != target[0].X) && ((x + width) != target[1].X))))
{
double num19 = bounds.Left + (bounds.Width / 2.0);
if ((num19 > 0.0) && ((num19 - 0.0) > (window.Width - num19)))
{
x = window.Width - width;
}
else
{
x = 0.0;
}
}
}
_realizedPlacement = placement;
return new Point(x, y);
}