DimCombine.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. namespace Terminal.Gui.ViewBase;
  2. /// <summary>
  3. /// Represents a dimension that is a combination of two other dimensions.
  4. /// </summary>
  5. /// <param name="Add">
  6. /// Indicates whether the two dimensions are added or subtracted.
  7. /// </param>
  8. /// <remarks>
  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="Dim"/> class to create <see cref="Dim"/> objects instead.
  11. /// </remarks>
  12. /// <param name="Left">The left dimension.</param>
  13. /// <param name="Right">The right dimension.</param>
  14. public record DimCombine (AddOrSubtract Add, Dim Left, Dim Right) : Dim
  15. {
  16. /// <summary>
  17. /// Gets whether the two dimensions are added or subtracted.
  18. /// </summary>
  19. public AddOrSubtract Add { get; } = Add;
  20. /// <summary>
  21. /// Gets the left dimension.
  22. /// </summary>
  23. public Dim Left { get; } = Left;
  24. /// <summary>
  25. /// Gets the right dimension.
  26. /// </summary>
  27. public Dim Right { get; } = Right;
  28. /// <inheritdoc/>
  29. public override string ToString () { return $"Combine({Left}{(Add == AddOrSubtract.Add ? '+' : '-')}{Right})"; }
  30. internal override int GetAnchor (int size)
  31. {
  32. if (Add == AddOrSubtract.Add)
  33. {
  34. return Left!.GetAnchor (size) + Right!.GetAnchor (size);
  35. }
  36. return Left!.GetAnchor (size) - Right!.GetAnchor (size);
  37. }
  38. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  39. {
  40. int newDimension;
  41. if (Add == AddOrSubtract.Add)
  42. {
  43. newDimension = Left!.Calculate (location, superviewContentSize, us, dimension) + Right!.Calculate (location, superviewContentSize, us, dimension);
  44. }
  45. else
  46. {
  47. newDimension = Math.Max (
  48. 0,
  49. Left!.Calculate (location, superviewContentSize, us, dimension)
  50. - Right!.Calculate (location, superviewContentSize, us, dimension));
  51. }
  52. return newDimension;
  53. }
  54. }