Added Thickness.Add + unit tests

This commit is contained in:
Tig
2024-03-06 20:54:35 -07:00
parent 40768b5bf8
commit a8e9fcfca5
4 changed files with 114 additions and 22 deletions

View File

@@ -100,6 +100,17 @@ public class Thickness : IEquatable<Thickness>
return outside.Contains (x, y) && !inside.Contains (x, y);
}
/// <summary>
/// Adds the thickness widths of another <see cref="Thickness"/> to the current <see cref="Thickness"/>, returning a new <see cref="Thickness"/>.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public Thickness Add (Thickness other)
{
return new Thickness (Left + other.Left, Top + other.Top, Right + other.Right, Bottom + other.Bottom);
}
/// <summary>Draws the <see cref="Thickness"/> rectangle with an optional diagnostics label.</summary>
/// <remarks>
/// If <see cref="ConsoleDriver.DiagnosticFlags"/> is set to

View File

@@ -94,18 +94,6 @@ public class Adornment : View
}
}
///// <inheritdoc/>
//public override Rectangle BoundsToScreen (Rectangle bounds)
//{
// // Adornments are *Children* of a View, not SubViews. Thus View.BoundsToScreen will not work.
// // To get the screen-relative coordinates of a Adornment, we need to know who
// // the Parent is
// Rectangle parentFrame = Parent?.Frame ?? Frame;
// bounds.Offset (parentFrame.X, parentFrame.Y);
// return Parent?.SuperView?.BoundsToScreen (bounds) ?? bounds;
//}
/// <inheritdoc/>
public override Rectangle FrameToScreen ()
{

View File

@@ -181,8 +181,9 @@ public partial class View
return Rectangle.Empty with { Size = Frame.Size };
}
int width = Math.Max (0, Frame.Size.Width - Margin.Thickness.Horizontal - Border.Thickness.Horizontal - Padding.Thickness.Horizontal);
int height = Math.Max (0, Frame.Size.Height - Margin.Thickness.Vertical - Border.Thickness.Vertical - Padding.Thickness.Vertical);
Thickness totalThickness = Margin.Thickness.Add (Border.Thickness.Add (Padding.Thickness));
int width = Math.Max (0, Frame.Size.Width - totalThickness.Horizontal);
int height = Math.Max (0, Frame.Size.Height - totalThickness.Vertical);
return Rectangle.Empty with { Size = new (width, height) };
}
@@ -198,18 +199,19 @@ public partial class View
);
}
#endif // DEBUG
Thickness totalThickness = Margin.Thickness.Add (Border.Thickness.Add (Padding.Thickness));
Frame = Frame with
{
Size =
new (
value.Size.Width
+ Margin.Thickness.Horizontal
+ Border.Thickness.Horizontal
+ Padding.Thickness.Horizontal,
value.Size.Height
+ Margin.Thickness.Vertical
+ Border.Thickness.Vertical
+ Padding.Thickness.Vertical
value.Size.Width + totalThickness.Horizontal,
//+ Margin.Thickness.Horizontal
//+ Border.Thickness.Horizontal
//+ Padding.Thickness.Horizontal,
value.Size.Height + totalThickness.Vertical
//+ Margin.Thickness.Vertical
//+ Border.Thickness.Vertical
//+ Padding.Thickness.Vertical
)
};
}

View File

@@ -785,4 +785,95 @@ public class ThicknessTests
Assert.Equal (0, t.Bottom);
Assert.Equal (0, t.Horizontal);
}
// Test Thickness.Add
[Theory]
[InlineData (
1,
2,
3,
4,
1,
2,
3,
4,
2,
4,
6,
8)]
[InlineData (
1,
2,
3,
4,
0,
0,
0,
0,
1,
2,
3,
4)]
[InlineData (
1,
2,
3,
4,
-1,
-2,
-3,
-4,
0,
0,
0,
0)]
[InlineData (
1,
2,
3,
4,
1,
1,
1,
1,
2,
3,
4,
5)]
[InlineData (
1,
2,
3,
4,
1,
1,
1,
1,
2,
3,
4,
5)]
public void AddTest (
int left,
int top,
int right,
int bottom,
int left2,
int top2,
int right2,
int bottom2,
int expectedLeft,
int expectedTop,
int expectedRight,
int expectedBottom
)
{
var t = new Thickness (left, top, right, bottom);
var t2 = new Thickness (left2, top2, right2, bottom2);
var result = t.Add (t2);
Assert.Equal (expectedLeft, result.Left);
Assert.Equal (expectedTop, result.Top);
Assert.Equal (expectedRight, result.Right);
Assert.Equal (expectedBottom, result.Bottom);
}
}