DimView.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Represents a dimension that tracks the Height or Width of the specified View.
  5. /// </summary>
  6. /// <remarks>
  7. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  8. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  9. /// </remarks>
  10. public class DimView : Dim
  11. {
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="DimView"/> class.
  14. /// </summary>
  15. /// <param name="view">The view the dimension is anchored to.</param>
  16. /// <param name="dimension">Indicates which dimension is tracked.</param>
  17. public DimView (View view, Dimension dimension)
  18. {
  19. Target = view;
  20. Dimension = dimension;
  21. }
  22. /// <summary>
  23. /// Gets the indicated dimension of the View.
  24. /// </summary>
  25. public Dimension Dimension { get; }
  26. /// <inheritdoc/>
  27. public override bool Equals (object? other) { return other is DimView abs && abs.Target == Target && abs.Dimension == Dimension; }
  28. /// <inheritdoc/>
  29. public override int GetHashCode () { return Target.GetHashCode (); }
  30. /// <summary>
  31. /// Gets the View the dimension is anchored to.
  32. /// </summary>
  33. public View Target { get; init; }
  34. /// <inheritdoc/>
  35. public override string ToString ()
  36. {
  37. if (Target == null)
  38. {
  39. throw new NullReferenceException ();
  40. }
  41. string dimString = Dimension switch
  42. {
  43. Dimension.Height => "Height",
  44. Dimension.Width => "Width",
  45. _ => "unknown"
  46. };
  47. return $"View({dimString},{Target})";
  48. }
  49. internal override int GetAnchor (int size)
  50. {
  51. return Dimension switch
  52. {
  53. Dimension.Height => Target.Frame.Height,
  54. Dimension.Width => Target.Frame.Width,
  55. _ => 0
  56. };
  57. }
  58. internal override bool ReferencesOtherViews () { return true; }
  59. }