DimPercent.cs 1.6 KB

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