DimPercent.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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="mode">
  12. /// If <see cref="DimPercentMode.Position"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  13. /// <see cref="View.Y"/>); otherwise, the dimension is computed using the View's <see cref="View.GetContentSize ()"/>.
  14. /// </param>
  15. public class DimPercent (int percent, DimPercentMode mode = DimPercentMode.ContentSize) : Dim
  16. {
  17. /// <inheritdoc/>
  18. public override bool Equals (object? other) { return other is DimPercent f && f.Percent == Percent && f.Mode == Mode; }
  19. /// <inheritdoc/>
  20. public override int GetHashCode () { return Percent.GetHashCode (); }
  21. /// <summary>
  22. /// Gets the percentage.
  23. /// </summary>
  24. public new int Percent { get; } = percent;
  25. /// <summary>
  26. /// </summary>
  27. /// <returns></returns>
  28. public override string ToString () { return $"Percent({Percent},{Mode})"; }
  29. /// <summary>
  30. /// Gets whether the dimension is computed using the View's position or GetContentSize ().
  31. /// </summary>
  32. public DimPercentMode Mode { get; } = mode;
  33. internal override int GetAnchor (int size) { return (int)(size * (Percent / 100f)); }
  34. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  35. {
  36. return Mode == DimPercentMode.Position ? Math.Max (GetAnchor (superviewContentSize - location), 0) : GetAnchor (superviewContentSize);
  37. }
  38. }