DimFactor -> PosPercent -> public

This commit is contained in:
Tig
2024-05-14 19:55:14 -07:00
parent 2c4fd4a28b
commit 3d2e8d9eb3
4 changed files with 43 additions and 19 deletions

View File

@@ -228,7 +228,7 @@ public class Dim
throw new ArgumentException ("Percent value must be between 0 and 100");
}
return new DimFactor (percent / 100, usePosition);
return new DimPercent (percent / 100, usePosition);
}
/// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
@@ -571,20 +571,44 @@ public class DimCombine (bool add, Dim left, Dim right) : Dim
}
}
internal class DimFactor (float factor, bool remaining = false) : Dim
/// <summary>
/// Represents a dimension that is a percentage of the width or height of the SuperView.
/// </summary>
/// <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>
public class DimPercent (float percent, bool usePosition = false) : Dim
{
private readonly float _factor = factor;
private readonly bool _remaining = remaining;
/// <summary>
/// Gets the percentage.
/// </summary>
public new float Percent { get; } = percent;
public override bool Equals (object other) { return other is DimFactor f && f._factor == _factor && f._remaining == _remaining; }
public override int GetHashCode () { return _factor.GetHashCode (); }
public bool IsFromRemaining () { return _remaining; }
public override string ToString () { return $"Percent({_factor},{_remaining})"; }
internal override int Anchor (int width) { return (int)(width * _factor); }
/// <summary>
/// Gets whether the dimension is computed using the View's position or ContentSize.
/// </summary>
public bool UsePosition { get; } = usePosition;
/// <inheritdoc />
public override bool Equals (object other) { return other is DimPercent f && f.Percent == Percent && f.UsePosition == UsePosition; }
/// <inheritdoc />
public override int GetHashCode () { return Percent.GetHashCode (); }
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString () { return $"Percent({Percent},{UsePosition})"; }
internal override int Anchor (int width) { return (int)(width * Percent); }
internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
{
return _remaining ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
return UsePosition ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
}
}

View File

@@ -552,24 +552,24 @@ public class PosCombine (bool add, Pos left, Pos right) : Pos
/// <summary>
/// Represents a position that is a percentage of the width or height of the SuperView.
/// </summary>
/// <param name="factor"></param>
public class PosPercent (float factor) : Pos
/// <param name="percent"></param>
public class PosPercent (float percent) : Pos
{
/// <summary>
/// Gets the factor that represents the percentage of the width or height of the SuperView.
/// </summary>
public float Factor { get; } = factor;
public new float Percent { get; } = percent;
/// <inheritdoc />
public override bool Equals (object other) { return other is PosPercent f && f.Factor == Factor; }
public override bool Equals (object other) { return other is PosPercent f && f.Percent == Percent; }
/// <inheritdoc />
public override int GetHashCode () { return Factor.GetHashCode (); }
public override int GetHashCode () { return Percent.GetHashCode (); }
/// <inheritdoc />
public override string ToString () { return $"Percent({Factor})"; }
public override string ToString () { return $"Percent({Percent})"; }
internal override int Anchor (int width) { return (int)(width * Factor); }
internal override int Anchor (int width) { return (int)(width * Percent); }
}
/// <summary>

View File

@@ -12,7 +12,7 @@ public class DimPercentTests
[Fact]
public void DimFactor_Calculate_ReturnsCorrectValue ()
{
var dim = new DimFactor (0.5f);
var dim = new DimPercent (0.5f);
var result = dim.Calculate (0, 100, null, Dimension.None);
Assert.Equal (50, result);
}

View File

@@ -345,7 +345,7 @@ public class DimTests
[TestRespondersDisposed]
public void Internal_Tests ()
{
var dimFactor = new DimFactor (0.10F);
var dimFactor = new DimPercent (0.10F);
Assert.Equal (10, dimFactor.Anchor (100));
var dimAbsolute = new DimAbsolute (10);