DimPercent.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Represents a dimension that is a percentage of the width or height of the SuperView.
  5. /// </summary>
  6. /// <remarks>
  7. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  8. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  9. /// </remarks>
  10. /// <param name="percent">The percentage.</param>
  11. /// <param name="usePosition">
  12. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  13. /// <see cref="View.Y"/>).
  14. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  15. /// </param>
  16. public class DimPercent (float percent, bool usePosition = false) : Dim
  17. {
  18. /// <inheritdoc/>
  19. public override bool Equals (object? other) { return other is DimPercent f && f.Percent == Percent && f.UsePosition == UsePosition; }
  20. /// <inheritdoc/>
  21. public override int GetHashCode () { return Percent.GetHashCode (); }
  22. /// <summary>
  23. /// Gets the percentage.
  24. /// </summary>
  25. public new float Percent { get; } = percent;
  26. /// <summary>
  27. /// </summary>
  28. /// <returns></returns>
  29. public override string ToString () { return $"Percent({Percent},{UsePosition})"; }
  30. /// <summary>
  31. /// Gets whether the dimension is computed using the View's position or ContentSize.
  32. /// </summary>
  33. public bool UsePosition { get; } = usePosition;
  34. internal override int GetAnchor (int size) { return (int)(size * Percent); }
  35. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  36. {
  37. return UsePosition ? Math.Max (GetAnchor (superviewContentSize - location), 0) : GetAnchor (superviewContentSize);
  38. }
  39. }