From dfc8c015eb69ebbf0678ad8aaebd1464f474c5c5 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 17 May 2024 13:12:34 -0400 Subject: [PATCH] DimPercent -> int vs. float --- Terminal.Gui/View/Layout/Dim.cs | 8 ++-- Terminal.Gui/View/Layout/DimPercent.cs | 6 +-- .../Scenarios/TextAlignmentsAndDirection.cs | 28 ++++++------ UnitTests/View/Layout/Dim.PercentTests.cs | 45 +++++++++---------- UnitTests/View/Layout/Dim.Tests.cs | 14 +----- UnitTests/Views/ToplevelTests.cs | 8 ++-- 6 files changed, 49 insertions(+), 60 deletions(-) diff --git a/Terminal.Gui/View/Layout/Dim.cs b/Terminal.Gui/View/Layout/Dim.cs index dc4b612a9..31e2c8e26 100644 --- a/Terminal.Gui/View/Layout/Dim.cs +++ b/Terminal.Gui/View/Layout/Dim.cs @@ -168,14 +168,14 @@ public abstract class Dim /// }; /// /// - public static Dim? Percent (float percent, bool usePosition = false) + public static Dim? Percent (int percent, bool usePosition = false) { - if (percent is < 0 or > 100) + if (percent is < 0 /*or > 100*/) { - throw new ArgumentException ("Percent value must be between 0 and 100"); + throw new ArgumentException ("Percent value must be positive."); } - return new DimPercent (percent / 100, usePosition); + return new DimPercent (percent, usePosition); } /// Creates a object that tracks the Width of the specified . diff --git a/Terminal.Gui/View/Layout/DimPercent.cs b/Terminal.Gui/View/Layout/DimPercent.cs index 7e535d308..89eb60729 100644 --- a/Terminal.Gui/View/Layout/DimPercent.cs +++ b/Terminal.Gui/View/Layout/DimPercent.cs @@ -14,7 +14,7 @@ namespace Terminal.Gui; /// ). /// If the dimension is computed using the View's . /// -public class DimPercent (float percent, bool usePosition = false) : Dim +public class DimPercent (int percent, bool usePosition = false) : Dim { /// public override bool Equals (object? other) { return other is DimPercent f && f.Percent == Percent && f.UsePosition == UsePosition; } @@ -25,7 +25,7 @@ public class DimPercent (float percent, bool usePosition = false) : Dim /// /// Gets the percentage. /// - public new float Percent { get; } = percent; + public new int Percent { get; } = percent; /// /// @@ -37,7 +37,7 @@ public class DimPercent (float percent, bool usePosition = false) : Dim /// public bool UsePosition { get; } = usePosition; - internal override int GetAnchor (int size) { return (int)(size * Percent); } + internal override int GetAnchor (int size) { return (int)(size * (Percent / 100f)); } internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension) { diff --git a/UICatalog/Scenarios/TextAlignmentsAndDirection.cs b/UICatalog/Scenarios/TextAlignmentsAndDirection.cs index 1f70f7d7a..8e012844a 100644 --- a/UICatalog/Scenarios/TextAlignmentsAndDirection.cs +++ b/UICatalog/Scenarios/TextAlignmentsAndDirection.cs @@ -266,8 +266,8 @@ public class TextAlignmentsAndDirections : Scenario { X = 1 /* */, Y = 1, - Width = Dim.Percent (100f / 3f), - Height = Dim.Percent (100f / 3f), + Width = Dim.Percent (100 / 3), + Height = Dim.Percent (100 / 3), TextAlignment = TextAlignment.Left, VerticalTextAlignment = VerticalTextAlignment.Top, ColorScheme = color1, @@ -279,8 +279,8 @@ public class TextAlignmentsAndDirections : Scenario { X = Pos.Right (txtLabelTL) + 2, Y = 1, - Width = Dim.Percent (100f / 3f), - Height = Dim.Percent (100f / 3f), + Width = Dim.Percent (33), + Height = Dim.Percent (33), TextAlignment = TextAlignment.Centered, VerticalTextAlignment = VerticalTextAlignment.Top, ColorScheme = color1, @@ -292,8 +292,8 @@ public class TextAlignmentsAndDirections : Scenario { X = Pos.Right (txtLabelTC) + 2, Y = 1, - Width = Dim.Percent (100f, true), - Height = Dim.Percent (100f / 3f), + Width = Dim.Percent (100, true), + Height = Dim.Percent (33), TextAlignment = TextAlignment.Right, VerticalTextAlignment = VerticalTextAlignment.Top, ColorScheme = color1, @@ -306,7 +306,7 @@ public class TextAlignmentsAndDirections : Scenario X = Pos.X (txtLabelTL), Y = Pos.Bottom (txtLabelTL) + 1, Width = Dim.Width (txtLabelTL), - Height = Dim.Percent (100f / 3f), + Height = Dim.Percent (33), TextAlignment = TextAlignment.Left, VerticalTextAlignment = VerticalTextAlignment.Middle, ColorScheme = color1, @@ -319,7 +319,7 @@ public class TextAlignmentsAndDirections : Scenario X = Pos.X (txtLabelTC), Y = Pos.Bottom (txtLabelTC) + 1, Width = Dim.Width (txtLabelTC), - Height = Dim.Percent (100f / 3f), + Height = Dim.Percent (33), TextAlignment = TextAlignment.Centered, VerticalTextAlignment = VerticalTextAlignment.Middle, ColorScheme = color1, @@ -331,8 +331,8 @@ public class TextAlignmentsAndDirections : Scenario { X = Pos.X (txtLabelTR), Y = Pos.Bottom (txtLabelTR) + 1, - Width = Dim.Percent (100f, true), - Height = Dim.Percent (100f / 3f), + Width = Dim.Percent (100, true), + Height = Dim.Percent (33), TextAlignment = TextAlignment.Right, VerticalTextAlignment = VerticalTextAlignment.Middle, ColorScheme = color1, @@ -345,7 +345,7 @@ public class TextAlignmentsAndDirections : Scenario X = Pos.X (txtLabelML), Y = Pos.Bottom (txtLabelML) + 1, Width = Dim.Width (txtLabelML), - Height = Dim.Percent (100f, true), + Height = Dim.Percent (100, true), TextAlignment = TextAlignment.Left, VerticalTextAlignment = VerticalTextAlignment.Bottom, ColorScheme = color1, @@ -358,7 +358,7 @@ public class TextAlignmentsAndDirections : Scenario X = Pos.X (txtLabelMC), Y = Pos.Bottom (txtLabelMC) + 1, Width = Dim.Width (txtLabelMC), - Height = Dim.Percent (100f, true), + Height = Dim.Percent (100, true), TextAlignment = TextAlignment.Centered, VerticalTextAlignment = VerticalTextAlignment.Bottom, ColorScheme = color1, @@ -370,8 +370,8 @@ public class TextAlignmentsAndDirections : Scenario { X = Pos.X (txtLabelMR), Y = Pos.Bottom (txtLabelMR) + 1, - Width = Dim.Percent (100f, true), - Height = Dim.Percent (100f, true), + Width = Dim.Percent (100, true), + Height = Dim.Percent (100, true), TextAlignment = TextAlignment.Right, VerticalTextAlignment = VerticalTextAlignment.Bottom, ColorScheme = color1, diff --git a/UnitTests/View/Layout/Dim.PercentTests.cs b/UnitTests/View/Layout/Dim.PercentTests.cs index 095a96f0d..fa3a7e0c8 100644 --- a/UnitTests/View/Layout/Dim.PercentTests.cs +++ b/UnitTests/View/Layout/Dim.PercentTests.cs @@ -12,7 +12,7 @@ public class DimPercentTests [Fact] public void DimFactor_Calculate_ReturnsCorrectValue () { - var dim = new DimPercent (0.5f); + var dim = new DimPercent (50); var result = dim.Calculate (0, 100, null, Dimension.None); Assert.Equal (50, result); } @@ -21,8 +21,8 @@ public class DimPercentTests [Fact] public void DimPercent_Equals () { - float n1 = 0; - float n2 = 0; + int n1 = 0; + int n2 = 0; Dim dim1 = Dim.Percent (n1); Dim dim2 = Dim.Percent (n2); Assert.Equal (dim1, dim2); @@ -32,22 +32,22 @@ public class DimPercentTests dim2 = Dim.Percent (n2); Assert.Equal (dim1, dim2); - n1 = n2 = 0.5f; + n1 = n2 = 50; dim1 = Dim.Percent (n1); dim2 = Dim.Percent (n2); Assert.Equal (dim1, dim2); - n1 = n2 = 100f; + n1 = n2 = 100; dim1 = Dim.Percent (n1); dim2 = Dim.Percent (n2); Assert.Equal (dim1, dim2); - n1 = n2 = 0.3f; + n1 = n2 = 30; dim1 = Dim.Percent (n1, true); dim2 = Dim.Percent (n2, true); Assert.Equal (dim1, dim2); - n1 = n2 = 0.3f; + n1 = n2 = 30; dim1 = Dim.Percent (n1); dim2 = Dim.Percent (n2, true); Assert.NotEqual (dim1, dim2); @@ -58,8 +58,8 @@ public class DimPercentTests dim2 = Dim.Percent (n2); Assert.NotEqual (dim1, dim2); - n1 = 0.5f; - n2 = 1.5f; + n1 = 50; + n2 = 150; dim1 = Dim.Percent (n1); dim2 = Dim.Percent (n2); Assert.NotEqual (dim1, dim2); @@ -70,9 +70,9 @@ public class DimPercentTests { Dim dim = Dim.Percent (0); Assert.Throws (() => dim = Dim.Percent (-1)); - Assert.Throws (() => dim = Dim.Percent (101)); - Assert.Throws (() => dim = Dim.Percent (100.0001F)); - Assert.Throws (() => dim = Dim.Percent (1000001)); + //Assert.Throws (() => dim = Dim.Percent (101)); + Assert.Throws (() => dim = Dim.Percent (-1000001)); + //Assert.Throws (() => dim = Dim.Percent (1000001)); } [Theory] @@ -158,18 +158,17 @@ public class DimPercentTests } } - [Fact] - public void DimPercent_SetsValue () + [Theory] + [InlineData(0)] + [InlineData (1)] + [InlineData (50)] + [InlineData (100)] + [InlineData (101)] + + public void DimPercent_SetsValue (int percent) { - float f = 0; - Dim dim = Dim.Percent (f); - Assert.Equal ($"Percent({f / 100:0.###},{false})", dim.ToString ()); - f = 0.5F; - dim = Dim.Percent (f); - Assert.Equal ($"Percent({f / 100:0.###},{false})", dim.ToString ()); - f = 100; - dim = Dim.Percent (f); - Assert.Equal ($"Percent({f / 100:0.###},{false})", dim.ToString ()); + Dim dim = Dim.Percent (percent); + Assert.Equal ($"Percent({percent},{false})", dim.ToString ()); } } diff --git a/UnitTests/View/Layout/Dim.Tests.cs b/UnitTests/View/Layout/Dim.Tests.cs index 3575d7960..1ac2227a4 100644 --- a/UnitTests/View/Layout/Dim.Tests.cs +++ b/UnitTests/View/Layout/Dim.Tests.cs @@ -285,7 +285,7 @@ public class DimTests [TestRespondersDisposed] public void Internal_Tests () { - var dimFactor = new DimPercent (0.10F); + var dimFactor = new DimPercent (10); Assert.Equal (10, dimFactor.GetAnchor (100)); var dimAbsolute = new DimAbsolute (10); @@ -401,7 +401,7 @@ public class DimTests Assert.Equal (100, w.Frame.Width); Assert.Equal (100, w.Frame.Height); - Assert.Equal ("Percent(0.5,False)", f1.Width.ToString ()); + Assert.Equal ("Percent(50,False)", f1.Width.ToString ()); Assert.Equal ("Absolute(5)", f1.Height.ToString ()); Assert.Equal (49, f1.Frame.Width); // 50-1=49 Assert.Equal (5, f1.Frame.Height); @@ -438,8 +438,6 @@ public class DimTests Assert.Equal (47, v2.Frame.Width); // 49-2=47 Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89 - Assert.Equal ("Percent(0.1,False)", v3.Width.ToString ()); - Assert.Equal ("Percent(0.1,False)", v3.Height.ToString ()); Assert.Equal (9, v3.Frame.Width); // 98*10%=9 Assert.Equal (9, v3.Frame.Height); // 98*10%=9 @@ -455,8 +453,6 @@ public class DimTests Assert.Equal (38, v5.Frame.Width); // 47-9=38 Assert.Equal (80, v5.Frame.Height); // 89-9=80 - Assert.Equal ("Percent(0.2,True)", v6.Width.ToString ()); - Assert.Equal ("Percent(0.2,True)", v6.Height.ToString ()); Assert.Equal (9, v6.Frame.Width); // 47*20%=9 Assert.Equal (18, v6.Frame.Height); // 89*20%=18 @@ -471,8 +467,6 @@ public class DimTests Assert.Equal (200, w.Frame.Height); f1.Text = "Frame1"; - Assert.Equal ("Percent(0.5,False)", f1.Width.ToString ()); - Assert.Equal ("Absolute(5)", f1.Height.ToString ()); Assert.Equal (99, f1.Frame.Width); // 100-1=99 Assert.Equal (5, f1.Frame.Height); @@ -504,8 +498,6 @@ public class DimTests Assert.Equal (189, v2.Frame.Height); // 198-2-7=189 v3.Text = "Button3"; - Assert.Equal ("Percent(0.1,False)", v3.Width.ToString ()); - Assert.Equal ("Percent(0.1,False)", v3.Height.ToString ()); // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width Assert.Equal (19, v3.Frame.Width); @@ -534,8 +526,6 @@ public class DimTests Assert.Equal (170, v5.Frame.Height); // 189-19=170 v6.Text = "Button6"; - Assert.Equal ("Percent(0.2,True)", v6.Width.ToString ()); - Assert.Equal ("Percent(0.2,True)", v6.Height.ToString ()); Assert.Equal (19, v6.Frame.Width); // 99*20%=19 Assert.Equal (38, v6.Frame.Height); // 198-7*20=18 }; diff --git a/UnitTests/Views/ToplevelTests.cs b/UnitTests/Views/ToplevelTests.cs index e6e66586a..b17c3cbf0 100644 --- a/UnitTests/Views/ToplevelTests.cs +++ b/UnitTests/Views/ToplevelTests.cs @@ -397,7 +397,7 @@ public class ToplevelTests { var isRunning = false; - var win1 = new Window { Id = "win1", Width = Dim.Percent (50f), Height = Dim.Fill () }; + var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () }; var lblTf1W1 = new Label { Id = "lblTf1W1", Text = "Enter text in TextField on Win1:" }; var tf1W1 = new TextField @@ -428,7 +428,7 @@ public class ToplevelTests var win2 = new Window { - Id = "win2", X = Pos.Right (win1) + 1, Width = Dim.Percent (50f), Height = Dim.Fill () + Id = "win2", X = Pos.Right (win1) + 1, Width = Dim.Percent (50), Height = Dim.Fill () }; var lblTf1W2 = new Label { Id = "lblTf1W2", Text = "Enter text in TextField on Win2:" }; @@ -568,7 +568,7 @@ public class ToplevelTests var isRunning = true; - var win1 = new Window { Id = "win1", Width = Dim.Percent (50f), Height = Dim.Fill () }; + var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () }; var lblTf1W1 = new Label { Text = "Enter text in TextField on Win1:" }; var tf1W1 = new TextField { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill (), Text = "Text1 on Win1" }; var lblTvW1 = new Label { Y = Pos.Bottom (lblTf1W1) + 1, Text = "Enter text in TextView on Win1:" }; @@ -581,7 +581,7 @@ public class ToplevelTests var tf2W1 = new TextField { X = Pos.Left (tf1W1), Width = Dim.Fill (), Text = "Text2 on Win1" }; win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1); - var win2 = new Window { Id = "win2", Width = Dim.Percent (50f), Height = Dim.Fill () }; + var win2 = new Window { Id = "win2", Width = Dim.Percent (50), Height = Dim.Fill () }; var lblTf1W2 = new Label { Text = "Enter text in TextField on Win2:" }; var tf1W2 = new TextField { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill (), Text = "Text1 on Win2" }; var lblTvW2 = new Label { Y = Pos.Bottom (lblTf1W2) + 1, Text = "Enter text in TextView on Win2:" };