PosView.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.ToString ();
  32. if (Target == null)
  33. {
  34. throw new NullReferenceException (nameof (Target));
  35. }
  36. return $"View(Side={sideString},Target={Target})";
  37. }
  38. internal override int GetAnchor (int size)
  39. {
  40. return Side switch
  41. {
  42. Side.Left => Target.Frame.X,
  43. Side.Top => Target.Frame.Y,
  44. Side.Right => Target.Frame.Right,
  45. Side.Bottom => Target.Frame.Bottom,
  46. _ => 0
  47. };
  48. }
  49. internal override bool ReferencesOtherViews () { return true; }
  50. }