PosView.cs 2.1 KB

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