DimPercentMode vs UsePosition

This commit is contained in:
Tig
2024-05-17 13:54:33 -04:00
parent 896912719d
commit 61c9b5c42b
7 changed files with 55 additions and 40 deletions

View File

@@ -168,14 +168,14 @@ public abstract class Dim
/// };
/// </code>
/// </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*/)
{
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>

View File

@@ -9,15 +9,14 @@ namespace Terminal.Gui;
/// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
/// </remarks>
/// <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>
public class DimPercent (int percent, bool usePosition = false) : Dim
public class DimPercent (int percent, DimPercentMode mode = DimPercentMode.ContentSize) : Dim
{
/// <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/>
public override int GetHashCode () { return Percent.GetHashCode (); }
@@ -30,17 +29,17 @@ public class DimPercent (int percent, bool usePosition = false) : Dim
/// <summary>
/// </summary>
/// <returns></returns>
public override string ToString () { return $"Percent({Percent},{UsePosition})"; }
public override string ToString () { return $"Percent({Percent},{Mode})"; }
/// <summary>
/// Gets whether the dimension is computed using the View's position or ContentSize.
/// </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 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);
}
}

View File

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

View File

@@ -292,7 +292,7 @@ public class TextAlignmentsAndDirections : Scenario
{
X = Pos.Right (txtLabelTC) + 2,
Y = 1,
Width = Dim.Percent (100, true),
Width = Dim.Percent (100, DimPercentMode.Position),
Height = Dim.Percent (33),
TextAlignment = TextAlignment.Right,
VerticalTextAlignment = VerticalTextAlignment.Top,
@@ -331,7 +331,7 @@ public class TextAlignmentsAndDirections : Scenario
{
X = Pos.X (txtLabelTR),
Y = Pos.Bottom (txtLabelTR) + 1,
Width = Dim.Percent (100, true),
Width = Dim.Percent (100, DimPercentMode.Position),
Height = Dim.Percent (33),
TextAlignment = TextAlignment.Right,
VerticalTextAlignment = VerticalTextAlignment.Middle,
@@ -345,7 +345,7 @@ public class TextAlignmentsAndDirections : Scenario
X = Pos.X (txtLabelML),
Y = Pos.Bottom (txtLabelML) + 1,
Width = Dim.Width (txtLabelML),
Height = Dim.Percent (100, true),
Height = Dim.Percent (100, DimPercentMode.Position),
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 (100, true),
Height = Dim.Percent (100, DimPercentMode.Position),
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 (100, true),
Height = Dim.Percent (100, true),
Width = Dim.Percent (100, DimPercentMode.Position),
Height = Dim.Percent (100, DimPercentMode.Position),
TextAlignment = TextAlignment.Right,
VerticalTextAlignment = VerticalTextAlignment.Bottom,
ColorScheme = color1,

View File

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

View File

@@ -43,13 +43,13 @@ public class DimPercentTests
Assert.Equal (dim1, dim2);
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);
n1 = n2 = 30;
dim1 = Dim.Percent (n1);
dim2 = Dim.Percent (n2, true);
dim2 = Dim.Percent (n2, DimPercentMode.Position);
Assert.NotEqual (dim1, dim2);
n1 = 0;
@@ -76,20 +76,20 @@ public class DimPercentTests
}
[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, 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, 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.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 };
@@ -97,8 +97,8 @@ public class DimPercentTests
{
X = width ? position : 0,
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);
@@ -168,7 +168,7 @@ public class DimPercentTests
public void DimPercent_SetsValue (int percent)
{
Dim dim = Dim.Percent (percent);
Assert.Equal ($"Percent({percent},{false})", dim.ToString ());
Assert.Equal ($"Percent({percent},ContentSize)", dim.ToString ());
}
}

View File

@@ -385,8 +385,8 @@ public class DimTests
{
X = Pos.X (f2),
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,
Text = "v6"
};
@@ -401,7 +401,6 @@ public class DimTests
Assert.Equal (100, w.Frame.Width);
Assert.Equal (100, w.Frame.Height);
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);