DimPercent.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  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="Percentage">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 record DimPercent (int Percentage, DimPercentMode Mode = DimPercentMode.ContentSize) : Dim
  16. {
  17. /// <summary>
  18. /// </summary>
  19. /// <returns></returns>
  20. public override string ToString () { return $"Percent({Percentage},{Mode})"; }
  21. /// <summary>
  22. /// Gets whether the dimension is computed using the View's position or GetContentSize ().
  23. /// </summary>
  24. public DimPercentMode Mode { get; } = Mode;
  25. internal override int GetAnchor (int size) { return (int)(size * (Percentage / 100f)); }
  26. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  27. {
  28. return Mode == DimPercentMode.Position ? Math.Max (GetAnchor (superviewContentSize - location), 0) : GetAnchor (superviewContentSize);
  29. }
  30. }