PosAnchorEnd.cs 1.9 KB

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