DimPercent -> int vs. float

This commit is contained in:
Tig
2024-05-17 13:12:34 -04:00
parent 7ece3bc3c9
commit dfc8c015eb
6 changed files with 49 additions and 60 deletions

View File

@@ -168,14 +168,14 @@ public abstract class Dim
/// };
/// </code>
/// </example>
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);
}
/// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>

View File

@@ -14,7 +14,7 @@ namespace Terminal.Gui;
/// <see cref="View.Y"/>).
/// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
/// </param>
public class DimPercent (float percent, bool usePosition = false) : Dim
public class DimPercent (int percent, bool usePosition = false) : Dim
{
/// <inheritdoc/>
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
/// <summary>
/// Gets the percentage.
/// </summary>
public new float Percent { get; } = percent;
public new int Percent { get; } = percent;
/// <summary>
/// </summary>
@@ -37,7 +37,7 @@ public class DimPercent (float percent, bool usePosition = false) : Dim
/// </summary>
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)
{

View File

@@ -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,

View File

@@ -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<ArgumentException> (() => dim = Dim.Percent (-1));
Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
//Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1000001));
//Assert.Throws<ArgumentException> (() => 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 ());
}
}

View File

@@ -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
};

View File

@@ -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:" };