DimCombine.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Represents a dimension that is a combination of two other dimensions.
  5. /// </summary>
  6. /// <param name="add">
  7. /// Indicates whether the two dimensions are added or subtracted.
  8. /// </param>
  9. /// <remarks>
  10. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  11. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  12. /// </remarks>
  13. /// <param name="left">The left dimension.</param>
  14. /// <param name="right">The right dimension.</param>
  15. public class DimCombine (AddOrSubtract add, Dim? left, Dim? right) : Dim
  16. {
  17. /// <summary>
  18. /// Gets whether the two dimensions are added or subtracted.
  19. /// </summary>
  20. public AddOrSubtract Add { get; } = add;
  21. /// <summary>
  22. /// Gets the left dimension.
  23. /// </summary>
  24. public Dim? Left { get; } = left;
  25. /// <summary>
  26. /// Gets the right dimension.
  27. /// </summary>
  28. public Dim? Right { get; } = right;
  29. /// <inheritdoc/>
  30. public override string ToString () { return $"Combine({Left}{(Add == AddOrSubtract.Add ? '+' : '-')}{Right})"; }
  31. internal override int GetAnchor (int size)
  32. {
  33. if (Add == AddOrSubtract.Add)
  34. {
  35. return Left!.GetAnchor (size) + Right!.GetAnchor (size);
  36. }
  37. return Left!.GetAnchor (size) - Right!.GetAnchor (size);
  38. }
  39. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  40. {
  41. int newDimension;
  42. if (Add == AddOrSubtract.Add)
  43. {
  44. newDimension = Left!.Calculate (location, superviewContentSize, us, dimension) + Right!.Calculate (location, superviewContentSize, us, dimension);
  45. }
  46. else
  47. {
  48. newDimension = Math.Max (
  49. 0,
  50. Left!.Calculate (location, superviewContentSize, us, dimension)
  51. - Right!.Calculate (location, superviewContentSize, us, dimension));
  52. }
  53. return newDimension;
  54. }
  55. /// <summary>
  56. /// Diagnostics API to determine if this Dim object references other views.
  57. /// </summary>
  58. /// <returns></returns>
  59. internal override bool ReferencesOtherViews ()
  60. {
  61. if (Left!.ReferencesOtherViews ())
  62. {
  63. return true;
  64. }
  65. if (Right!.ReferencesOtherViews ())
  66. {
  67. return true;
  68. }
  69. return false;
  70. }
  71. }