PosView.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Diagnostics;
  2. namespace Terminal.Gui.ViewBase;
  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. public record PosView : Pos
  13. {
  14. /// <summary>
  15. /// Represents a position that is anchored to the side of another view.
  16. /// </summary>
  17. /// <remarks>
  18. /// <para>
  19. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  20. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  21. /// </para>
  22. /// </remarks>
  23. /// <param name="view">The View the position is anchored to.</param>
  24. /// <param name="side">The side of the View the position is anchored to.</param>
  25. public PosView (View view, Side side)
  26. {
  27. ArgumentNullException.ThrowIfNull (view);
  28. Target = view;
  29. Side = side;
  30. }
  31. /// <summary>
  32. /// Gets the View the position is anchored to.
  33. /// </summary>
  34. public View Target { get; }
  35. /// <summary>
  36. /// Gets the side of the View the position is anchored to.
  37. /// </summary>
  38. public Side Side { get; }
  39. /// <inheritdoc/>
  40. public override string ToString ()
  41. {
  42. string sideString = Side.ToString ();
  43. if (Target == null)
  44. {
  45. throw new NullReferenceException (nameof (Target));
  46. }
  47. return $"View(Side={sideString},Target={Target})";
  48. }
  49. internal override int GetAnchor (int size)
  50. {
  51. return Side switch
  52. {
  53. Side.Left => Target!.Frame.X,
  54. Side.Top => Target!.Frame.Y,
  55. Side.Right => Target!.Frame.Right,
  56. Side.Bottom => Target!.Frame.Bottom,
  57. _ => 0
  58. };
  59. }
  60. internal override bool ReferencesOtherViews () { return true; }
  61. }