DimCombine.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. int la = Left!.GetAnchor (size);
  34. int ra = Right!.GetAnchor (size);
  35. if (Add == AddOrSubtract.Add)
  36. {
  37. return la + ra;
  38. }
  39. return la - ra;
  40. }
  41. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  42. {
  43. int leftNewDim = Left!.Calculate (location, superviewContentSize, us, dimension);
  44. int rightNewDim = Right!.Calculate (location, superviewContentSize, us, dimension);
  45. int newDimension;
  46. if (Add == AddOrSubtract.Add)
  47. {
  48. newDimension = leftNewDim + rightNewDim;
  49. }
  50. else
  51. {
  52. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  53. }
  54. return newDimension;
  55. }
  56. /// <summary>
  57. /// Diagnostics API to determine if this Dim object references other views.
  58. /// </summary>
  59. /// <returns></returns>
  60. internal override bool ReferencesOtherViews ()
  61. {
  62. if (Left!.ReferencesOtherViews ())
  63. {
  64. return true;
  65. }
  66. if (Right!.ReferencesOtherViews ())
  67. {
  68. return true;
  69. }
  70. return false;
  71. }
  72. }