Justification.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Controls how the <see cref="Justifier"/> justifies items within a container.
  4. /// </summary>
  5. public enum Justification
  6. {
  7. /// <summary>
  8. /// The items will be aligned to the left.
  9. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  10. /// each item.
  11. /// </summary>
  12. /// <example>
  13. /// <c>
  14. /// 111 2222 33333
  15. /// </c>
  16. /// </example>
  17. Left,
  18. /// <summary>
  19. /// The items will be aligned to the right.
  20. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  21. /// each item.
  22. /// </summary>
  23. /// <example>
  24. /// <c>
  25. /// 111 2222 33333
  26. /// </c>
  27. /// </example>
  28. Right,
  29. /// <summary>
  30. /// The group will be centered in the container.
  31. /// If centering is not possible, the group will be left-justified.
  32. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  33. /// each item.
  34. /// </summary>
  35. /// <example>
  36. /// <c>
  37. /// 111 2222 33333
  38. /// </c>
  39. /// </example>
  40. Centered,
  41. /// <summary>
  42. /// The items will be justified. Space will be added between the items such that the first item
  43. /// is at the start and the right side of the last item against the end.
  44. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  45. /// each item.
  46. /// </summary>
  47. /// <example>
  48. /// <c>
  49. /// 111 2222 33333
  50. /// </c>
  51. /// </example>
  52. Justified,
  53. /// <summary>
  54. /// The first item will be aligned to the left and the remaining will aligned to the right.
  55. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  56. /// each item.
  57. /// </summary>
  58. /// <example>
  59. /// <c>
  60. /// 111 2222 33333
  61. /// </c>
  62. /// </example>
  63. FirstLeftRestRight,
  64. /// <summary>
  65. /// The last item will be aligned to the right and the remaining will aligned to the left.
  66. /// Set <see cref="Justifier.PutSpaceBetweenItems"/> to <see langword="true"/> to ensure at least one space between
  67. /// each item.
  68. /// </summary>
  69. /// <example>
  70. /// <c>
  71. /// 111 2222 33333
  72. /// </c>
  73. /// </example>
  74. LastRightRestLeft
  75. }
  76. /// <summary>
  77. /// Justifies items within a container based on the specified <see cref="Justification"/>.
  78. /// </summary>
  79. public class Justifier
  80. {
  81. /// <summary>
  82. /// Gets or sets how the <see cref="Justifier"/> justifies items within a container.
  83. /// </summary>
  84. public Justification Justification { get; set; }
  85. /// <summary>
  86. /// The size of the container.
  87. /// </summary>
  88. public int ContainerSize { get; set; }
  89. /// <summary>
  90. /// Gets or sets whether <see cref="Justify"/> puts a space is placed between items. Default is <see langword="false"/>. If <see langword="true"/>, a space will be
  91. /// placed between each item, which is useful for justifying text.
  92. /// </summary>
  93. public bool PutSpaceBetweenItems { get; set; }
  94. /// <summary>
  95. /// Takes a list of items and returns their positions when justified within a container <see name="ContainerSize"/> wide based on the specified
  96. /// <see cref="Justification"/>.
  97. /// </summary>
  98. /// <param name="sizes">The sizes of the items to justify.</param>
  99. /// <returns>The locations of the items, from left to right.</returns>
  100. public int [] Justify (int [] sizes)
  101. {
  102. return Justify (Justification, PutSpaceBetweenItems, ContainerSize, sizes);
  103. }
  104. /// <summary>
  105. /// Takes a list of items and returns their positions when justified within a container <paramref name="containerSize"/> wide based on the specified
  106. /// <see cref="Justification"/>.
  107. /// </summary>
  108. /// <param name="sizes">The sizes of the items to justify.</param>
  109. /// <param name="justification">The justification style.</param>
  110. /// <param name="containerSize">The size of the container.</param>
  111. /// <returns>The locations of the items, from left to right.</returns>
  112. public static int [] Justify (Justification justification, bool putSpaceBetweenItems, int containerSize, int [] sizes)
  113. {
  114. if (sizes.Length == 0)
  115. {
  116. return new int [] { };
  117. }
  118. int maxSpaceBetweenItems = putSpaceBetweenItems ? 1 : 0;
  119. var positions = new int [sizes.Length]; // positions of the items. the return value.
  120. int totalItemsSize = sizes.Sum ();
  121. int totalGaps = sizes.Length - 1; // total gaps between items
  122. int totalItemsAndSpaces = totalItemsSize + totalGaps * maxSpaceBetweenItems; // total size of items and spaces if we had enough room
  123. int spaces = totalGaps * maxSpaceBetweenItems; // We'll decrement this below to place one space between each item until we run out
  124. if (totalItemsSize >= containerSize)
  125. {
  126. spaces = 0;
  127. }
  128. else if (totalItemsAndSpaces > containerSize)
  129. {
  130. spaces = containerSize - totalItemsSize;
  131. }
  132. switch (justification)
  133. {
  134. case Justification.Left:
  135. var currentPosition = 0;
  136. for (var i = 0; i < sizes.Length; i++)
  137. {
  138. CheckSizeCannotBeNegative (i, sizes);
  139. if (i == 0)
  140. {
  141. positions [0] = 0; // first item position
  142. continue;
  143. }
  144. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  145. // subsequent items are placed one space after the previous item
  146. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  147. }
  148. break;
  149. case Justification.Right:
  150. currentPosition = Math.Max (0, containerSize - totalItemsSize - spaces);
  151. for (var i = 0; i < sizes.Length; i++)
  152. {
  153. CheckSizeCannotBeNegative (i, sizes);
  154. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  155. positions [i] = currentPosition;
  156. currentPosition += sizes [i] + spaceBefore;
  157. }
  158. break;
  159. case Justification.Centered:
  160. if (sizes.Length > 1)
  161. {
  162. // remaining space to be distributed before first and after the items
  163. int remainingSpace = Math.Max (0, containerSize - totalItemsSize - spaces);
  164. for (var i = 0; i < sizes.Length; i++)
  165. {
  166. CheckSizeCannotBeNegative (i, sizes);
  167. if (i == 0)
  168. {
  169. positions [i] = remainingSpace / 2; // first item position
  170. continue;
  171. }
  172. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  173. // subsequent items are placed one space after the previous item
  174. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  175. }
  176. }
  177. else if (sizes.Length == 1)
  178. {
  179. CheckSizeCannotBeNegative (0, sizes);
  180. positions [0] = (containerSize - sizes [0]) / 2; // single item is centered
  181. }
  182. break;
  183. case Justification.Justified:
  184. int spaceBetween = sizes.Length > 1 ? (containerSize - totalItemsSize) / (sizes.Length - 1) : 0;
  185. int remainder = sizes.Length > 1 ? (containerSize - totalItemsSize) % (sizes.Length - 1) : 0;
  186. currentPosition = 0;
  187. for (var i = 0; i < sizes.Length; i++)
  188. {
  189. CheckSizeCannotBeNegative (i, sizes);
  190. positions [i] = currentPosition;
  191. int extraSpace = i < remainder ? 1 : 0;
  192. currentPosition += sizes [i] + spaceBetween + extraSpace;
  193. }
  194. break;
  195. // 111 2222 33333
  196. case Justification.LastRightRestLeft:
  197. if (sizes.Length > 1)
  198. {
  199. currentPosition = 0;
  200. for (var i = 0; i < sizes.Length; i++)
  201. {
  202. CheckSizeCannotBeNegative (i,sizes);
  203. if (i < sizes.Length - 1)
  204. {
  205. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  206. positions [i] = currentPosition;
  207. currentPosition += sizes [i] + spaceBefore;
  208. }
  209. }
  210. positions [sizes.Length - 1] = containerSize - sizes [^1];
  211. }
  212. else if (sizes.Length == 1)
  213. {
  214. CheckSizeCannotBeNegative (0, sizes);
  215. positions [0] = containerSize - sizes [0]; // single item is flush right
  216. }
  217. break;
  218. // 111 2222 33333
  219. case Justification.FirstLeftRestRight:
  220. if (sizes.Length > 1)
  221. {
  222. currentPosition = 0;
  223. positions [0] = currentPosition; // first item is flush left
  224. for (int i = sizes.Length - 1; i >= 0; i--)
  225. {
  226. CheckSizeCannotBeNegative (i, sizes);
  227. if (i == sizes.Length - 1)
  228. {
  229. // start at right
  230. currentPosition = containerSize - sizes [i];
  231. positions [i] = currentPosition;
  232. }
  233. if (i < sizes.Length - 1 && i > 0)
  234. {
  235. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  236. positions [i] = currentPosition - sizes [i] - spaceBefore;
  237. currentPosition = positions [i];
  238. }
  239. }
  240. }
  241. else if (sizes.Length == 1)
  242. {
  243. CheckSizeCannotBeNegative (0, sizes);
  244. positions [0] = 0; // single item is flush left
  245. }
  246. break;
  247. default:
  248. throw new ArgumentOutOfRangeException (nameof (justification), justification, null);
  249. }
  250. return positions;
  251. }
  252. private static void CheckSizeCannotBeNegative (int i, int [] sizes)
  253. {
  254. if (sizes [i] < 0)
  255. {
  256. throw new ArgumentException ("The size of an item cannot be negative.");
  257. }
  258. }
  259. }