Просмотр исходного кода

Pos/DimCombine now use AddOrSubtract enum instead of bool

Tig 1 год назад
Родитель
Сommit
d4d2c92142

+ 23 - 8
Terminal.Gui/View/Layout/Dim.cs

@@ -304,7 +304,7 @@ public abstract class Dim
             return new DimAbsolute (left.GetAnchor (0) + right.GetAnchor (0));
         }
 
-        var newDim = new DimCombine (true, left, right);
+        var newDim = new DimCombine (AddOrSubtract.Add, left, right);
         (left as DimView)?.Target.SetNeedsLayout ();
 
         return newDim;
@@ -329,7 +329,7 @@ public abstract class Dim
             return new DimAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
         }
 
-        var newDim = new DimCombine (false, left, right);
+        var newDim = new DimCombine (AddOrSubtract.Subtract, left, right);
         (left as DimView)?.Target.SetNeedsLayout ();
 
         return newDim;
@@ -535,7 +535,6 @@ public class DimAuto () : Dim
                    _ => throw new ArgumentOutOfRangeException (nameof (dimension), dimension, null)
                };
 
-        // If max: is set, clamp the return - BUGBUG: Not tested
         return int.Min (max, MaximumContentDim?.GetAnchor (superviewContentSize) ?? max);
     }
 
@@ -566,6 +565,22 @@ public class DimAuto () : Dim
 
 }
 
+/// <summary>
+/// Describes whether an operation should add or subtract values.
+/// </summary>
+public enum AddOrSubtract
+{
+    /// <summary>
+    /// The operation should use addition.
+    /// </summary>
+    Add = 0,
+
+    /// <summary>
+    /// The operation should use subtraction.
+    /// </summary>
+    Subtract = 1
+}
+
 /// <summary>
 ///     Represents a dimension that is a combination of two other dimensions.
 /// </summary>
@@ -579,12 +594,12 @@ public class DimAuto () : Dim
 /// </remarks>
 /// <param name="left">The left dimension.</param>
 /// <param name="right">The right dimension.</param>
-public class DimCombine (bool add, Dim? left, Dim? right) : Dim
+public class DimCombine (AddOrSubtract add, Dim? left, Dim? right) : Dim
 {
     /// <summary>
     ///     Gets whether the two dimensions are added or subtracted.
     /// </summary>
-    public bool Add { get; } = add;
+    public AddOrSubtract Add { get; } = add;
 
     /// <summary>
     ///     Gets the left dimension.
@@ -597,14 +612,14 @@ public class DimCombine (bool add, Dim? left, Dim? right) : Dim
     public Dim? Right { get; } = right;
 
     /// <inheritdoc/>
-    public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
+    public override string ToString () { return $"Combine({Left}{(Add == AddOrSubtract.Add ? '+' : '-')}{Right})"; }
 
     internal override int GetAnchor (int size)
     {
         int la = Left!.GetAnchor (size);
         int ra = Right!.GetAnchor (size);
 
-        if (Add)
+        if (Add == AddOrSubtract.Add)
         {
             return la + ra;
         }
@@ -619,7 +634,7 @@ public class DimCombine (bool add, Dim? left, Dim? right) : Dim
 
         int newDimension;
 
-        if (Add)
+        if (Add == AddOrSubtract.Add)
         {
             newDimension = leftNewDim + rightNewDim;
         }

+ 7 - 7
Terminal.Gui/View/Layout/Pos.cs

@@ -344,7 +344,7 @@ public abstract class Pos
             return new PosAbsolute (left.GetAnchor (0) + right.GetAnchor (0));
         }
 
-        var newPos = new PosCombine (true, left, right);
+        var newPos = new PosCombine (AddOrSubtract.Add, left, right);
 
         if (left is PosView view)
         {
@@ -373,7 +373,7 @@ public abstract class Pos
             return new PosAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
         }
 
-        var newPos = new PosCombine (false, left, right);
+        var newPos = new PosCombine (AddOrSubtract.Subtract, left, right);
 
         if (left is PosView view)
         {
@@ -515,13 +515,13 @@ public class PosCenter : Pos
 /// </param>
 /// <param name="left">The left position.</param>
 /// <param name="right">The right position.</param>
-public class PosCombine (bool add, Pos left, Pos right) : Pos
+public class PosCombine (AddOrSubtract add, Pos left, Pos right) : Pos
 {
     /// <summary>
     ///     Gets whether the two positions are added or subtracted. If <see langword="true"/>, the positions are added,
     ///     otherwise they are subtracted.
     /// </summary>
-    public bool Add { get; } = add;
+    public AddOrSubtract Add { get; } = add;
 
     /// <summary>
     ///     Gets the left position.
@@ -534,14 +534,14 @@ public class PosCombine (bool add, Pos left, Pos right) : Pos
     public new Pos Right { get; } = right;
 
     /// <inheritdoc/>
-    public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
+    public override string ToString () { return $"Combine({Left}{(Add == AddOrSubtract.Add ? '+' : '-')}{Right})"; }
 
     internal override int GetAnchor (int size)
     {
         int la = Left.GetAnchor (size);
         int ra = Right.GetAnchor (size);
 
-        if (Add)
+        if (Add == AddOrSubtract.Add)
         {
             return la + ra;
         }
@@ -554,7 +554,7 @@ public class PosCombine (bool add, Pos left, Pos right) : Pos
         int left = Left.Calculate (superviewDimension, dim, us, dimension);
         int right = Right.Calculate (superviewDimension, dim, us, dimension);
 
-        if (Add)
+        if (Add == AddOrSubtract.Add)
         {
             return left + right;
         }

+ 3 - 3
UnitTests/View/Layout/Dim.AutoTests.cs

@@ -451,7 +451,7 @@ public class DimAutoTests (ITestOutputHelper output)
         subView.Height = 0;
 
         // Tests nested Combine
-        subView.Height = 5 + new DimCombine (true, 3, new DimCombine (true, Dim.Percent (10), 9));
+        subView.Height = 5 + new DimCombine (AddOrSubtract.Add, 3, new DimCombine (AddOrSubtract.Add, Dim.Percent (10), 9));
         Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
     }
 
@@ -497,7 +497,7 @@ public class DimAutoTests (ITestOutputHelper output)
         superView.SetRelativeLayout (new (0, 0)); // no throw
         superView.LayoutSubviews (); // no throw
 
-        subView.X = new PosCombine (true, Pos.Right (subView2), new PosCombine (true, 7, 9));
+        subView.X = new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, 7, 9));
         superView.SetRelativeLayout (new (0, 0)); // no throw
 
         subView.X = Pos.Center () + 3;
@@ -521,7 +521,7 @@ public class DimAutoTests (ITestOutputHelper output)
         subView.X = 0;
 
         // Tests nested Combine
-        subView.X = 5 + new PosCombine (true, Pos.Right (subView2), new PosCombine (true, Pos.Center (), 9));
+        subView.X = 5 + new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, Pos.Center (), 9));
         Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
         subView.X = 0;
     }

+ 1 - 1
UnitTests/View/Layout/Dim.Tests.cs

@@ -294,7 +294,7 @@ public class DimTests
         var dimFill = new DimFill (1);
         Assert.Equal (99, dimFill.GetAnchor (100));
 
-        var dimCombine = new DimCombine (true, dimFactor, dimAbsolute);
+        var dimCombine = new DimCombine (AddOrSubtract.Add, dimFactor, dimAbsolute);
         Assert.Equal (dimCombine.Left, dimFactor);
         Assert.Equal (dimCombine.Right, dimAbsolute);
         Assert.Equal (20, dimCombine.GetAnchor (100));

+ 3 - 3
UnitTests/View/Layout/Pos.Tests.cs

@@ -40,7 +40,7 @@ public class PosTests ()
     [Fact]
     public void PosCombine_Calculate_ReturnsExpectedValue ()
     {
-        var posCombine = new PosCombine (true, new PosAbsolute (5), new PosAbsolute (3));
+        var posCombine = new PosCombine (AddOrSubtract.Add, new PosAbsolute (5), new PosAbsolute (3));
         var result = posCombine.Calculate (10, new DimAbsolute (2), null, Dimension.None);
         Assert.Equal (8, result);
     }
@@ -192,12 +192,12 @@ public class PosTests ()
         var posAbsolute = new PosAbsolute (10);
         Assert.Equal (10, posAbsolute.GetAnchor (0));
 
-        var posCombine = new PosCombine (true, posFactor, posAbsolute);
+        var posCombine = new PosCombine (AddOrSubtract.Add, posFactor, posAbsolute);
         Assert.Equal (posCombine.Left, posFactor);
         Assert.Equal (posCombine.Right, posAbsolute);
         Assert.Equal (20, posCombine.GetAnchor (100));
 
-        posCombine = new (true, posAbsolute, posFactor);
+        posCombine = new (AddOrSubtract.Add, posAbsolute, posFactor);
         Assert.Equal (posCombine.Left, posAbsolute);
         Assert.Equal (posCombine.Right, posFactor);
         Assert.Equal (20, posCombine.GetAnchor (100));