Pārlūkot izejas kodu

DimPercentMode vs UsePosition

Tig 1 gadu atpakaļ
vecāks
revīzija
61c9b5c42b

+ 2 - 2
Terminal.Gui/View/Layout/Dim.cs

@@ -168,14 +168,14 @@ public abstract class Dim
     ///  };
     ///  };
     ///  </code>
     ///  </code>
     /// </example>
     /// </example>
-    public static Dim? Percent (int percent, bool usePosition = false)
+    public static Dim? Percent (int percent, DimPercentMode mode = DimPercentMode.ContentSize)
     {
     {
         if (percent is < 0 /*or > 100*/)
         if (percent is < 0 /*or > 100*/)
         {
         {
             throw new ArgumentException ("Percent value must be positive.");
             throw new ArgumentException ("Percent value must be positive.");
         }
         }
 
 
-        return new DimPercent (percent, usePosition);
+        return new DimPercent (percent, mode);
     }
     }
 
 
     /// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>
     /// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>

+ 8 - 9
Terminal.Gui/View/Layout/DimPercent.cs

@@ -9,15 +9,14 @@ namespace Terminal.Gui;
 ///     methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
 ///     methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
 /// </remarks>
 /// </remarks>
 /// <param name="percent">The percentage.</param>
 /// <param name="percent">The percentage.</param>
-/// <param name="usePosition">
-///     If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
-///     <see cref="View.Y"/>).
-///     If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
+/// <param name="mode">
+///     If <see cref="DimPercentMode.Position"/> the dimension is computed using the View's position (<see cref="View.X"/> or
+///     <see cref="View.Y"/>); otherwise, the dimension is computed using the View's <see cref="View.ContentSize"/>.
 /// </param>
 /// </param>
-public class DimPercent (int percent, bool usePosition = false) : Dim
+public class DimPercent (int percent, DimPercentMode mode = DimPercentMode.ContentSize) : Dim
 {
 {
     /// <inheritdoc/>
     /// <inheritdoc/>
-    public override bool Equals (object? other) { return other is DimPercent f && f.Percent == Percent && f.UsePosition == UsePosition; }
+    public override bool Equals (object? other) { return other is DimPercent f && f.Percent == Percent && f.Mode == Mode; }
 
 
     /// <inheritdoc/>
     /// <inheritdoc/>
     public override int GetHashCode () { return Percent.GetHashCode (); }
     public override int GetHashCode () { return Percent.GetHashCode (); }
@@ -30,17 +29,17 @@ public class DimPercent (int percent, bool usePosition = false) : Dim
     /// <summary>
     /// <summary>
     /// </summary>
     /// </summary>
     /// <returns></returns>
     /// <returns></returns>
-    public override string ToString () { return $"Percent({Percent},{UsePosition})"; }
+    public override string ToString () { return $"Percent({Percent},{Mode})"; }
 
 
     /// <summary>
     /// <summary>
     ///     Gets whether the dimension is computed using the View's position or ContentSize.
     ///     Gets whether the dimension is computed using the View's position or ContentSize.
     /// </summary>
     /// </summary>
-    public bool UsePosition { get; } = usePosition;
+    public DimPercentMode Mode { get; } = mode;
 
 
     internal override int GetAnchor (int size) { return (int)(size * (Percent / 100f)); }
     internal override int GetAnchor (int size) { return (int)(size * (Percent / 100f)); }
 
 
     internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
     internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
     {
     {
-        return UsePosition ? Math.Max (GetAnchor (superviewContentSize - location), 0) : GetAnchor (superviewContentSize);
+        return Mode == DimPercentMode.Position ? Math.Max (GetAnchor (superviewContentSize - location), 0) : GetAnchor (superviewContentSize);
     }
     }
 }
 }

+ 17 - 0
Terminal.Gui/View/Layout/DimPercentMode.cs

@@ -0,0 +1,17 @@
+namespace Terminal.Gui;
+
+/// <summary>
+/// Indicates the mode for a <see cref="DimPercent"/> object.
+/// </summary>
+public enum DimPercentMode
+{
+    /// <summary>
+    /// The dimension is computed using the View's position (<see cref="View.X"/> or <see cref="View.Y"/>).
+    /// </summary>
+    Position = 0,
+
+    /// <summary>
+    /// The dimension is computed using the View's <see cref="View.ContentSize"/>.
+    /// </summary>
+    ContentSize = 1
+}

+ 6 - 6
UICatalog/Scenarios/TextAlignmentsAndDirection.cs

@@ -292,7 +292,7 @@ public class TextAlignmentsAndDirections : Scenario
         {
         {
             X = Pos.Right (txtLabelTC) + 2,
             X = Pos.Right (txtLabelTC) + 2,
             Y = 1,
             Y = 1,
-            Width = Dim.Percent (100, true),
+            Width = Dim.Percent (100, DimPercentMode.Position),
             Height = Dim.Percent (33),
             Height = Dim.Percent (33),
             TextAlignment = TextAlignment.Right,
             TextAlignment = TextAlignment.Right,
             VerticalTextAlignment = VerticalTextAlignment.Top,
             VerticalTextAlignment = VerticalTextAlignment.Top,
@@ -331,7 +331,7 @@ public class TextAlignmentsAndDirections : Scenario
         {
         {
             X = Pos.X (txtLabelTR),
             X = Pos.X (txtLabelTR),
             Y = Pos.Bottom (txtLabelTR) + 1,
             Y = Pos.Bottom (txtLabelTR) + 1,
-            Width = Dim.Percent (100, true),
+            Width = Dim.Percent (100, DimPercentMode.Position),
             Height = Dim.Percent (33),
             Height = Dim.Percent (33),
             TextAlignment = TextAlignment.Right,
             TextAlignment = TextAlignment.Right,
             VerticalTextAlignment = VerticalTextAlignment.Middle,
             VerticalTextAlignment = VerticalTextAlignment.Middle,
@@ -345,7 +345,7 @@ public class TextAlignmentsAndDirections : Scenario
             X = Pos.X (txtLabelML),
             X = Pos.X (txtLabelML),
             Y = Pos.Bottom (txtLabelML) + 1,
             Y = Pos.Bottom (txtLabelML) + 1,
             Width = Dim.Width (txtLabelML),
             Width = Dim.Width (txtLabelML),
-            Height = Dim.Percent (100, true),
+            Height = Dim.Percent (100, DimPercentMode.Position),
             TextAlignment = TextAlignment.Left,
             TextAlignment = TextAlignment.Left,
             VerticalTextAlignment = VerticalTextAlignment.Bottom,
             VerticalTextAlignment = VerticalTextAlignment.Bottom,
             ColorScheme = color1,
             ColorScheme = color1,
@@ -358,7 +358,7 @@ public class TextAlignmentsAndDirections : Scenario
             X = Pos.X (txtLabelMC),
             X = Pos.X (txtLabelMC),
             Y = Pos.Bottom (txtLabelMC) + 1,
             Y = Pos.Bottom (txtLabelMC) + 1,
             Width = Dim.Width (txtLabelMC),
             Width = Dim.Width (txtLabelMC),
-            Height = Dim.Percent (100, true),
+            Height = Dim.Percent (100, DimPercentMode.Position),
             TextAlignment = TextAlignment.Centered,
             TextAlignment = TextAlignment.Centered,
             VerticalTextAlignment = VerticalTextAlignment.Bottom,
             VerticalTextAlignment = VerticalTextAlignment.Bottom,
             ColorScheme = color1,
             ColorScheme = color1,
@@ -370,8 +370,8 @@ public class TextAlignmentsAndDirections : Scenario
         {
         {
             X = Pos.X (txtLabelMR),
             X = Pos.X (txtLabelMR),
             Y = Pos.Bottom (txtLabelMR) + 1,
             Y = Pos.Bottom (txtLabelMR) + 1,
-            Width = Dim.Percent (100, true),
-            Height = Dim.Percent (100, true),
+            Width = Dim.Percent (100, DimPercentMode.Position),
+            Height = Dim.Percent (100, DimPercentMode.Position),
             TextAlignment = TextAlignment.Right,
             TextAlignment = TextAlignment.Right,
             VerticalTextAlignment = VerticalTextAlignment.Bottom,
             VerticalTextAlignment = VerticalTextAlignment.Bottom,
             ColorScheme = color1,
             ColorScheme = color1,

+ 1 - 1
UICatalog/Scenarios/WindowsAndFrameViews.cs

@@ -121,7 +121,7 @@ public class WindowsAndFrameViews : Scenario
             {
             {
                 X = Pos.Percent (50),
                 X = Pos.Percent (50),
                 Y = 1,
                 Y = 1,
-                Width = Dim.Percent (100, true), // Or Dim.Percent (50)
+                Width = Dim.Percent (100, DimPercentMode.Position), // Or Dim.Percent (50)
                 Height = 5,
                 Height = 5,
                 ColorScheme = Colors.ColorSchemes ["Base"],
                 ColorScheme = Colors.ColorSchemes ["Base"],
                 Text = "The Text in the FrameView",
                 Text = "The Text in the FrameView",

+ 20 - 20
UnitTests/View/Layout/Dim.PercentTests.cs

@@ -43,13 +43,13 @@ public class DimPercentTests
         Assert.Equal (dim1, dim2);
         Assert.Equal (dim1, dim2);
 
 
         n1 = n2 = 30;
         n1 = n2 = 30;
-        dim1 = Dim.Percent (n1, true);
-        dim2 = Dim.Percent (n2, true);
+        dim1 = Dim.Percent (n1, DimPercentMode.Position);
+        dim2 = Dim.Percent (n2, DimPercentMode.Position);
         Assert.Equal (dim1, dim2);
         Assert.Equal (dim1, dim2);
 
 
         n1 = n2 = 30;
         n1 = n2 = 30;
         dim1 = Dim.Percent (n1);
         dim1 = Dim.Percent (n1);
-        dim2 = Dim.Percent (n2, true);
+        dim2 = Dim.Percent (n2, DimPercentMode.Position);
         Assert.NotEqual (dim1, dim2);
         Assert.NotEqual (dim1, dim2);
 
 
         n1 = 0;
         n1 = 0;
@@ -76,20 +76,20 @@ public class DimPercentTests
     }
     }
 
 
     [Theory]
     [Theory]
-    [InlineData (0, false, true, 12)]
-    [InlineData (0, false, false, 12)]
-    [InlineData (1, false, true, 12)]
-    [InlineData (1, false, false, 12)]
-    [InlineData (2, false, true, 12)]
-    [InlineData (2, false, false, 12)]
-
-    [InlineData (0, true, true, 12)]
-    [InlineData (0, true, false, 12)]
-    [InlineData (1, true, true, 12)]
-    [InlineData (1, true, false, 12)]
-    [InlineData (2, true, true, 11)]
-    [InlineData (2, true, false, 11)]
-    public void DimPercent_Position (int position, bool usePosition, bool width, int expected)
+    [InlineData (0, DimPercentMode.ContentSize, true, 12)]
+    [InlineData (0, DimPercentMode.ContentSize, false, 12)]
+    [InlineData (1, DimPercentMode.ContentSize, true, 12)]
+    [InlineData (1, DimPercentMode.ContentSize, false, 12)]
+    [InlineData (2, DimPercentMode.ContentSize, true, 12)]
+    [InlineData (2, DimPercentMode.ContentSize, false, 12)]
+
+    [InlineData (0, DimPercentMode.Position, true, 12)]
+    [InlineData (0, DimPercentMode.Position, false, 12)]
+    [InlineData (1, DimPercentMode.Position, true, 12)]
+    [InlineData (1, DimPercentMode.Position, false, 12)]
+    [InlineData (2, DimPercentMode.Position, true, 11)]
+    [InlineData (2, DimPercentMode.Position, false, 11)]
+    public void DimPercent_Position (int position, DimPercentMode mode, bool width, int expected)
     {
     {
         var super = new View { Width = 25, Height = 25 };
         var super = new View { Width = 25, Height = 25 };
 
 
@@ -97,8 +97,8 @@ public class DimPercentTests
         {
         {
             X = width ? position : 0,
             X = width ? position : 0,
             Y = width ? 0 : position,
             Y = width ? 0 : position,
-            Width = width ? Dim.Percent (50, usePosition) : 1,
-            Height = width ? 1 : Dim.Percent (50, usePosition)
+            Width = width ? Dim.Percent (50, mode) : 1,
+            Height = width ? 1 : Dim.Percent (50, mode)
         };
         };
 
 
         super.Add (view);
         super.Add (view);
@@ -168,7 +168,7 @@ public class DimPercentTests
     public void DimPercent_SetsValue (int percent)
     public void DimPercent_SetsValue (int percent)
     {
     {
         Dim dim = Dim.Percent (percent);
         Dim dim = Dim.Percent (percent);
-        Assert.Equal ($"Percent({percent},{false})", dim.ToString ());
+        Assert.Equal ($"Percent({percent},ContentSize)", dim.ToString ());
     }
     }
 
 
 }
 }

+ 2 - 3
UnitTests/View/Layout/Dim.Tests.cs

@@ -385,8 +385,8 @@ public class DimTests
         {
         {
             X = Pos.X (f2),
             X = Pos.X (f2),
             Y = Pos.Bottom (f2) + 2,
             Y = Pos.Bottom (f2) + 2,
-            Width = Dim.Percent (20, true),
-            Height = Dim.Percent (20, true),
+            Width = Dim.Percent (20, DimPercentMode.Position),
+            Height = Dim.Percent (20, DimPercentMode.Position),
             ValidatePosDim = true,
             ValidatePosDim = true,
             Text = "v6"
             Text = "v6"
         };
         };
@@ -401,7 +401,6 @@ public class DimTests
                        Assert.Equal (100, w.Frame.Width);
                        Assert.Equal (100, w.Frame.Width);
                        Assert.Equal (100, w.Frame.Height);
                        Assert.Equal (100, w.Frame.Height);
 
 
-                       Assert.Equal ("Percent(50,False)", f1.Width.ToString ());
                        Assert.Equal ("Absolute(5)", f1.Height.ToString ());
                        Assert.Equal ("Absolute(5)", f1.Height.ToString ());
                        Assert.Equal (49, f1.Frame.Width); // 50-1=49
                        Assert.Equal (49, f1.Frame.Width); // 50-1=49
                        Assert.Equal (5, f1.Frame.Height);
                        Assert.Equal (5, f1.Frame.Height);