PosView.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Represents a position that is anchored to the side of another view.
  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. /// <param name="view">The View the position is anchored to.</param>
  13. /// <param name="side">The side of the View the position is anchored to.</param>
  14. public class PosView (View view, Side side) : Pos
  15. {
  16. /// <summary>
  17. /// Gets the View the position is anchored to.
  18. /// </summary>
  19. public View Target { get; } = view;
  20. /// <summary>
  21. /// Gets the side of the View the position is anchored to.
  22. /// </summary>
  23. public Side Side { get; } = side;
  24. /// <inheritdoc/>
  25. public override bool Equals (object? other) { return other is PosView abs && abs.Target == Target && abs.Side == Side; }
  26. /// <inheritdoc/>
  27. public override int GetHashCode () { return Target.GetHashCode (); }
  28. /// <inheritdoc/>
  29. public override string ToString ()
  30. {
  31. string sideString = Side switch
  32. {
  33. Side.Left => "left",
  34. Side.Top => "top",
  35. Side.Right => "right",
  36. Side.Bottom => "bottom",
  37. _ => "unknown"
  38. };
  39. if (Target == null)
  40. {
  41. throw new NullReferenceException (nameof (Target));
  42. }
  43. return $"View(side={sideString},target={Target})";
  44. }
  45. internal override int GetAnchor (int size)
  46. {
  47. return Side switch
  48. {
  49. Side.Left => Target.Frame.X,
  50. Side.Top => Target.Frame.Y,
  51. Side.Right => Target.Frame.Right,
  52. Side.Bottom => Target.Frame.Bottom,
  53. _ => 0
  54. };
  55. }
  56. internal override bool ReferencesOtherViews () { return true; }
  57. }