Browse Source

Added Thickness.Add + unit tests

Tig 1 year ago
parent
commit
a8e9fcfca5

+ 11 - 0
Terminal.Gui/Drawing/Thickness.cs

@@ -100,6 +100,17 @@ public class Thickness : IEquatable<Thickness>
         return outside.Contains (x, y) && !inside.Contains (x, y);
         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>
     /// <summary>Draws the <see cref="Thickness"/> rectangle with an optional diagnostics label.</summary>
     /// <remarks>
     /// <remarks>
     ///     If <see cref="ConsoleDriver.DiagnosticFlags"/> is set to
     ///     If <see cref="ConsoleDriver.DiagnosticFlags"/> is set to

+ 0 - 12
Terminal.Gui/View/Adornment/Adornment.cs

@@ -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/>
     /// <inheritdoc/>
     public override Rectangle FrameToScreen ()
     public override Rectangle FrameToScreen ()
     {
     {

+ 12 - 10
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -181,8 +181,9 @@ public partial class View
                 return Rectangle.Empty with { Size = Frame.Size };
                 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) };
             return Rectangle.Empty with { Size = new (width, height) };
         }
         }
@@ -198,18 +199,19 @@ public partial class View
                                 );
                                 );
             }
             }
 #endif // DEBUG
 #endif // DEBUG
+            Thickness totalThickness = Margin.Thickness.Add (Border.Thickness.Add (Padding.Thickness));
             Frame = Frame with
             Frame = Frame with
             {
             {
                 Size =
                 Size =
                 new (
                 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
                     )
                     )
             };
             };
         }
         }

+ 91 - 0
UnitTests/Drawing/ThicknessTests.cs

@@ -785,4 +785,95 @@ public class ThicknessTests
         Assert.Equal (0, t.Bottom);
         Assert.Equal (0, t.Bottom);
         Assert.Equal (0, t.Horizontal);
         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);
+    }
 }
 }