DimCombine.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. }