DimPercent.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. namespace Terminal.Gui.ViewBase;
  2. /// <summary>
  3. /// Represents a dimension that is a percentage of the width or height of the SuperView.
  4. /// </summary>
  5. /// <remarks>
  6. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  7. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  8. /// </remarks>
  9. /// <param name="Percentage">The percentage.</param>
  10. /// <param name="Mode">
  11. /// If <see cref="DimPercentMode.Position"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  12. /// <see cref="View.Y"/>); otherwise, the dimension is computed using the View's <see cref="View.GetContentSize ()"/>.
  13. /// </param>
  14. public record DimPercent (int Percentage, DimPercentMode Mode = DimPercentMode.ContentSize) : Dim
  15. {
  16. /// <summary>
  17. /// </summary>
  18. /// <returns></returns>
  19. public override string ToString () { return $"Percent({Percentage},{Mode})"; }
  20. /// <summary>
  21. /// Gets whether the dimension is computed using the View's position or GetContentSize ().
  22. /// </summary>
  23. public DimPercentMode Mode { get; } = Mode;
  24. internal override int GetAnchor (int size) { return (int)(size * (Percentage / 100f)); }
  25. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  26. {
  27. return Mode == DimPercentMode.Position ? Math.Max (GetAnchor (superviewContentSize - location), 0) : GetAnchor (superviewContentSize);
  28. }
  29. }