Browse Source

PosPercent -> int vs. float

Tig 1 year ago
parent
commit
896912719d

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

@@ -41,7 +41,7 @@ namespace Terminal.Gui;
 ///             </item>
 ///             <item>
 ///                 <term>
-///                     <see cref="Dim.Percent(float, bool)"/>
+///                     <see cref="Dim.Percent(int, bool)"/>
 ///                 </term>
 ///                 <description>
 ///                     Creates a <see cref="Dim"/> object that is a percentage of the width or height of the

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

@@ -35,7 +35,7 @@ namespace Terminal.Gui;
 ///             </item>
 ///             <item>
 ///                 <term>
-///                     <see cref="Pos.Percent(float)"/>
+///                     <see cref="Pos.Percent(int)"/>
 ///                 </term>
 ///                 <description>
 ///                     Creates a <see cref="Pos"/> object that is a percentage of the width or height of the
@@ -212,14 +212,14 @@ public abstract class Pos
     ///  };
     ///  </code>
     /// </example>
-    public static Pos Percent (float percent)
+    public static Pos Percent (int percent)
     {
-        if (percent is < 0 or > 100)
+        if (percent is < 0)
         {
-            throw new ArgumentException ("Percent value must be between 0 and 100.");
+            throw new ArgumentException ("Percent value must be positive.");
         }
 
-        return new PosPercent (percent / 100);
+        return new PosPercent (percent);
     }
 
     /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>

+ 5 - 5
Terminal.Gui/View/Layout/PosPercent.cs

@@ -11,15 +11,15 @@ namespace Terminal.Gui;
 ///     </para>
 /// </remarks>
 /// <param name="percent"></param>
-public class PosPercent (float percent) : Pos
+public class PosPercent (int percent) : Pos
 {
     /// <summary>
-    ///     Gets the factor that represents the percentage of the width or height of the SuperView.
+    ///     Gets the percentage of the width or height of the SuperView.
     /// </summary>
-    public new float Percent { get; } = percent;
+    public new int Percent { get; } = percent;
 
     /// <inheritdoc/>
-    public override bool Equals (object? other) { return other is PosPercent f && f.Percent == Percent; }
+    public override bool Equals (object? other) { return other is PosPercent i && i.Percent == Percent; }
 
     /// <inheritdoc/>
     public override int GetHashCode () { return Percent.GetHashCode (); }
@@ -27,5 +27,5 @@ public class PosPercent (float percent) : Pos
     /// <inheritdoc/>
     public override string ToString () { return $"Percent({Percent})"; }
 
-    internal override int GetAnchor (int size) { return (int)(size * Percent); }
+    internal override int GetAnchor (int size) { return (int)(size * (Percent / 100f)); }
 }

+ 11 - 7
Terminal.Gui/Views/TileView.cs

@@ -996,18 +996,22 @@ public class TileView : View
         ///     </para>
         ///     <para>
         ///         Effectively turning any <see cref="Pos"/> into a <see cref="PosPercent"/> (as if created with
-        ///         <see cref="Pos.Percent(float)"/>)
+        ///         <see cref="Pos.Percent(int)"/>)
         ///     </para>
         /// </summary>
-        /// <param name="p">The <see cref="Pos"/> to convert to <see cref="Pos.Percent(float)"/></param>
+        /// <param name="p">The <see cref="Pos"/> to convert to <see cref="Pos.Percent(int)"/></param>
         /// <param name="parentLength">The Height/Width that <paramref name="p"/> lies within</param>
         /// <returns></returns>
-        private Pos ConvertToPosFactor (Pos p, int parentLength)
+        private Pos ConvertToPosPercent (Pos p, int parentLength)
         {
-            // calculate position in the 'middle' of the cell at p distance along parentLength
+            // Calculate position in the 'middle' of the cell at p distance along parentLength
             float position = p.GetAnchor (parentLength) + 0.5f;
 
-            return new PosPercent (position / parentLength);
+            // Calculate the percentage
+            int percent = (int)Math.Round ((position / parentLength) * 100);
+
+            // Return a new PosPercent object
+            return Pos.Percent (percent);
         }
 
         /// <summary>
@@ -1029,10 +1033,10 @@ public class TileView : View
             {
                 if (Orientation == Orientation.Horizontal)
                 {
-                    return Parent.SetSplitterPos (Idx, ConvertToPosFactor (newValue, Parent.Viewport.Height));
+                    return Parent.SetSplitterPos (Idx, ConvertToPosPercent (newValue, Parent.Viewport.Height));
                 }
 
-                return Parent.SetSplitterPos (Idx, ConvertToPosFactor (newValue, Parent.Viewport.Width));
+                return Parent.SetSplitterPos (Idx, ConvertToPosPercent (newValue, Parent.Viewport.Width));
             }
 
             return Parent.SetSplitterPos (Idx, newValue);

+ 7 - 8
UnitTests/View/Layout/Pos.PercentTests.cs

@@ -50,15 +50,15 @@ public class PosPercentTests (ITestOutputHelper output)
     [Fact]
     public void PosPercent_SetsValue ()
     {
-        float f = 0;
+        int f = 0;
         Pos pos = Pos.Percent (f);
-        Assert.Equal ($"Percent({f / 100:0.###})", pos.ToString ());
-        f = 0.5F;
+        Assert.Equal ($"Percent({f})", pos.ToString ());
+        f = 50;
         pos = Pos.Percent (f);
-        Assert.Equal ($"Percent({f / 100:0.###})", pos.ToString ());
+        Assert.Equal ($"Percent({f})", pos.ToString ());
         f = 100;
         pos = Pos.Percent (f);
-        Assert.Equal ($"Percent({f / 100:0.###})", pos.ToString ());
+        Assert.Equal ($"Percent({f})", pos.ToString ());
     }
 
     [Fact]
@@ -66,9 +66,8 @@ public class PosPercentTests (ITestOutputHelper output)
     {
         Pos pos = Pos.Percent (0);
         Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
-        Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
-        Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
-        Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
+        //Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
+        //Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
     }
 
 }

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

@@ -48,7 +48,7 @@ public class PosTests ()
     [Fact]
     public void PosFactor_Calculate_ReturnsExpectedValue ()
     {
-        var posFactor = new PosPercent (0.5f);
+        var posFactor = new PosPercent (50);
         var result = posFactor.Calculate (10, new DimAbsolute (2), null, Dimension.None);
         Assert.Equal (5, result);
     }
@@ -180,7 +180,7 @@ public class PosTests ()
     [TestRespondersDisposed]
     public void Internal_Tests ()
     {
-        var posFactor = new PosPercent (0.10F);
+        var posFactor = new PosPercent (10);
         Assert.Equal (10, posFactor.GetAnchor (100));
 
         var posAnchorEnd = new PosAnchorEnd (1);
@@ -303,8 +303,8 @@ public class PosTests ()
     [Fact]
     public void PosPercent_Equal ()
     {
-        float n1 = 0;
-        float n2 = 0;
+        int n1 = 0;
+        int n2 = 0;
         Pos pos1 = Pos.Percent (n1);
         Pos pos2 = Pos.Percent (n2);
         Assert.Equal (pos1, pos2);
@@ -314,12 +314,12 @@ public class PosTests ()
         pos2 = Pos.Percent (n2);
         Assert.Equal (pos1, pos2);
 
-        n1 = n2 = 0.5f;
+        n1 = n2 = 50;
         pos1 = Pos.Percent (n1);
         pos2 = Pos.Percent (n2);
         Assert.Equal (pos1, pos2);
 
-        n1 = n2 = 100f;
+        n1 = n2 = 100;
         pos1 = Pos.Percent (n1);
         pos2 = Pos.Percent (n2);
         Assert.Equal (pos1, pos2);
@@ -330,8 +330,8 @@ public class PosTests ()
         pos2 = Pos.Percent (n2);
         Assert.NotEqual (pos1, pos2);
 
-        n1 = 0.5f;
-        n2 = 1.5f;
+        n1 = 50;
+        n2 = 150;
         pos1 = Pos.Percent (n1);
         pos2 = Pos.Percent (n2);
         Assert.NotEqual (pos1, pos2);

+ 1 - 1
UnitTests/Views/TileViewTests.cs

@@ -1758,7 +1758,7 @@ public class TileViewTests
         Assert.False (tileView.SetSplitterPos (0, 0));
 
         // position should remain where it was, at 50%
-        Assert.Equal (Pos.Percent (50f), tileView.SplitterDistances.ElementAt (0));
+        Assert.Equal (Pos.Percent (50), tileView.SplitterDistances.ElementAt (0));
 
         tileView.Draw ();