PosAnchorEnd.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 class 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. /// <inheritdoc/>
  30. public override bool Equals (object? other) { return other is PosAnchorEnd anchorEnd && anchorEnd.Offset == Offset; }
  31. /// <inheritdoc/>
  32. public override int GetHashCode () { return Offset.GetHashCode (); }
  33. /// <summary>
  34. /// If true, the offset is the width of the view, if false, the offset is the offset value.
  35. /// </summary>
  36. public bool UseDimForOffset { get; }
  37. /// <inheritdoc/>
  38. public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({Offset})"; }
  39. internal override int GetAnchor (int size)
  40. {
  41. if (UseDimForOffset)
  42. {
  43. return size;
  44. }
  45. return size - Offset;
  46. }
  47. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  48. {
  49. int newLocation = GetAnchor (superviewDimension);
  50. if (UseDimForOffset)
  51. {
  52. newLocation -= dim.GetAnchor (superviewDimension);
  53. }
  54. return newLocation;
  55. }
  56. }