PosAnchorEnd.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. namespace Terminal.Gui.ViewBase;
  2. /// <summary>
  3. /// Represents a position anchored to the end (right side or bottom).
  4. /// </summary>
  5. /// <remarks>
  6. /// <para>
  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="Pos"/> class to create <see cref="Pos"/> objects instead.
  9. /// </para>
  10. /// </remarks>
  11. public record PosAnchorEnd : Pos
  12. {
  13. /// <summary>
  14. /// Gets the offset of the position from the right/bottom.
  15. /// </summary>
  16. public int Offset { get; }
  17. /// <summary>
  18. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  19. /// minus the respective dimension of the View. This is equivalent to using <see cref="PosAnchorEnd(int)"/>,
  20. /// with an offset equivalent to the View's respective dimension.
  21. /// </summary>
  22. public PosAnchorEnd () { UseDimForOffset = true; }
  23. /// <summary>
  24. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  25. /// </summary>
  26. /// <param name="offset"></param>
  27. public PosAnchorEnd (int offset) { Offset = offset; }
  28. /// <summary>
  29. /// If true, the offset is the width of the view, if false, the offset is the offset value.
  30. /// </summary>
  31. public bool UseDimForOffset { get; }
  32. /// <inheritdoc/>
  33. public override string ToString () { return UseDimForOffset ? "AnchorEnd" : $"AnchorEnd({Offset})"; }
  34. internal override int GetAnchor (int size)
  35. {
  36. if (UseDimForOffset)
  37. {
  38. return size;
  39. }
  40. return size - Offset;
  41. }
  42. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  43. {
  44. int newLocation = GetAnchor (superviewDimension);
  45. if (UseDimForOffset)
  46. {
  47. newLocation -= dim.GetAnchor (superviewDimension);
  48. }
  49. return newLocation;
  50. }
  51. }