Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
New padding adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Higgins committed Dec 6, 2020
1 parent 783a8fa commit 652af12
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions CenterTaskbar/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ private bool PositionLoop(AutomationElement trayWnd)
}

// Right bounds check
var rightBounds = SideBoundary(false, horizontal, taskList);
var rightBounds = SideBoundary(false, horizontal, taskList, scale, trayBounds);
if (targetPos + size > rightBounds)
{
// Shift off center when the bar is too big
Expand All @@ -411,7 +411,7 @@ private bool PositionLoop(AutomationElement trayWnd)
}

// Left bounds check
var leftBounds = SideBoundary(true, horizontal, taskList);
var leftBounds = SideBoundary(true, horizontal, taskList, scale, trayBounds);
if (targetPos <= leftBounds)
{
// Prevent X position ending up beyond the normal left aligned position
Expand All @@ -424,31 +424,31 @@ private bool PositionLoop(AutomationElement trayWnd)

if (horizontal)
{
SetWindowPos(taskListPtr, IntPtr.Zero, RelativePos(targetPos, horizontal, taskList), 0, 0, 0,
SetWindowPos(taskListPtr, IntPtr.Zero, RelativePos(targetPos, horizontal, taskList, scale, trayBounds), 0, 0, 0,
SWP_NOZORDER | SWP_NOSIZE | SWP_ASYNCWINDOWPOS);
Debug.Write("Final X Position: ");
Debug.WriteLine(taskList.Current.BoundingRectangle.X);
Debug.Write(taskList.Current.BoundingRectangle.X == targetPos ? "Move hit target" : "Move missed target");
Debug.WriteLine(" (diff: " + Math.Abs(taskList.Current.BoundingRectangle.X - targetPos) + ")");
Debug.WriteLine(((first.Current.BoundingRectangle.Left - trayBounds.Left) / scale) + trayBounds.Left);
Debug.Write(Math.Round(((first.Current.BoundingRectangle.Left - trayBounds.Left) / scale) + trayBounds.Left) == Math.Round(targetPos) ? "Move hit target" : "Move missed target");
Debug.WriteLine(" (diff: " + Math.Abs((((first.Current.BoundingRectangle.Left - trayBounds.Left) / scale) + trayBounds.Left) - targetPos) + ")");
}
else
{
SetWindowPos(taskListPtr, IntPtr.Zero, 0, RelativePos(targetPos, horizontal, taskList), 0, 0,
SetWindowPos(taskListPtr, IntPtr.Zero, 0, RelativePos(targetPos, horizontal, taskList, scale, trayBounds), 0, 0,
SWP_NOZORDER | SWP_NOSIZE | SWP_ASYNCWINDOWPOS);
Debug.Write("Final Y Position: ");
Debug.WriteLine(taskList.Current.BoundingRectangle.Y);
Debug.Write(taskList.Current.BoundingRectangle.Y == targetPos ? "Move hit target" : "Move missed target");
Debug.WriteLine(" (diff: " + Math.Abs(taskList.Current.BoundingRectangle.Y - targetPos) + ")");
Debug.WriteLine(((first.Current.BoundingRectangle.Top - trayBounds.Top) / scale) + trayBounds.Top);
Debug.Write(Math.Round(((first.Current.BoundingRectangle.Top - trayBounds.Top) / scale) + trayBounds.Top) == Math.Round(targetPos) ? "Move hit target" : "Move missed target");
Debug.WriteLine(" (diff: " + Math.Abs((((first.Current.BoundingRectangle.Top - trayBounds.Top) / scale) + trayBounds.Top) - targetPos) + ")");
}

_lasts[trayWnd] = horizontal ? last.Current.BoundingRectangle.Left : last.Current.BoundingRectangle.Top;

return true;
}

private static int RelativePos(double x, bool horizontal, AutomationElement element)
private static int RelativePos(double x, bool horizontal, AutomationElement element, double scale, System.Windows.Rect trayBounds)
{
var adjustment = SideBoundary(true, horizontal, element);
var adjustment = SideBoundary(true, horizontal, element, scale, trayBounds);

var newPos = x - adjustment;

Expand All @@ -461,30 +461,46 @@ private static int RelativePos(double x, bool horizontal, AutomationElement elem
return (int) newPos;
}

private static int SideBoundary(bool left, bool horizontal, AutomationElement element)
private static int SideBoundary(bool left, bool horizontal, AutomationElement element, double scale, System.Windows.Rect trayBounds)
{
double adjustment = 0;
var prevSibling = TreeWalker.ControlViewWalker.GetPreviousSibling(element);
var nextSibling = TreeWalker.ControlViewWalker.GetNextSibling(element);
var parent = TreeWalker.ControlViewWalker.GetParent(element);
Debug.WriteLine("Boundary calc for " + element.Current.ClassName);
var prevSibling = TreeWalker.RawViewWalker.GetPreviousSibling(element);
var nextSibling = TreeWalker.RawViewWalker.GetNextSibling(element);
var first = TreeWalker.RawViewWalker.GetFirstChild(element);
var parent = TreeWalker.RawViewWalker.GetParent(element);

var padding = horizontal? (trayBounds.Left - element.Current.BoundingRectangle.Left) - ((trayBounds.Left - first.Current.BoundingRectangle.Left) / scale): (trayBounds.Top - element.Current.BoundingRectangle.Top) - ((trayBounds.Top - first.Current.BoundingRectangle.Top) / scale);

Debug.Write(horizontal ? "Horizontal Padding: ": "Vertical Padding: ");
Debug.WriteLine(Math.Round(padding));
if (padding < 0)
{
Debug.WriteLine("Padding should not be less than 0, setting to 0");
padding = 0;
}

if (left && prevSibling != null && !prevSibling.Current.BoundingRectangle.IsEmpty)
{
Debug.WriteLine("Left sibling calc " + prevSibling.Current.ClassName);
adjustment = horizontal
? prevSibling.Current.BoundingRectangle.Right
: prevSibling.Current.BoundingRectangle.Bottom;
}
else if (!left && nextSibling != null && !nextSibling.Current.BoundingRectangle.IsEmpty)
{
Debug.WriteLine("Right sibling calc " + nextSibling.Current.ClassName);
adjustment = horizontal
? nextSibling.Current.BoundingRectangle.Left
: nextSibling.Current.BoundingRectangle.Top;
}
else if (parent != null)
{
Debug.WriteLine("Parent calc " + parent.Current.ClassName);
if (horizontal)
adjustment = left ? parent.Current.BoundingRectangle.Left : parent.Current.BoundingRectangle.Right;
adjustment = left ? parent.Current.BoundingRectangle.Left + padding : parent.Current.BoundingRectangle.Right;
else
adjustment = left ? parent.Current.BoundingRectangle.Top : parent.Current.BoundingRectangle.Bottom;
adjustment = left ? parent.Current.BoundingRectangle.Top + padding : parent.Current.BoundingRectangle.Bottom;
}

if (horizontal)
Expand Down

0 comments on commit 652af12

Please sign in to comment.