Justification.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. private int _maxSpaceBetweenItems;
  82. /// <summary>
  83. /// 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
  84. /// placed between each item, which is useful for
  85. /// justifying text.
  86. /// </summary>
  87. public bool PutSpaceBetweenItems
  88. {
  89. get => _maxSpaceBetweenItems == 1;
  90. set => _maxSpaceBetweenItems = value ? 1 : 0;
  91. }
  92. /// <summary>
  93. /// Takes a list of items and returns their positions when justified within a container <paramref name="containerSize"/> wide based on the specified
  94. /// <see cref="Justification"/>.
  95. /// </summary>
  96. /// <param name="sizes">The sizes of the items to justify.</param>
  97. /// <param name="justification">The justification style.</param>
  98. /// <param name="containerSize">The width of the container.</param>
  99. /// <returns>The locations of the items, from left to right.</returns>
  100. public int [] Justify (int [] sizes, Justification justification, int containerSize)
  101. {
  102. if (sizes.Length == 0)
  103. {
  104. return new int [] { };
  105. }
  106. int totalItemsSize = sizes.Sum ();
  107. if (totalItemsSize > containerSize)
  108. {
  109. // throw new ArgumentException ("The sum of the sizes is greater than the total size.");
  110. }
  111. var positions = new int [sizes.Length];
  112. totalItemsSize = sizes.Sum (); // total size of items
  113. int totalGaps = sizes.Length - 1; // total gaps (MinimumSpaceBetweenItems)
  114. int totalItemsAndSpaces = totalItemsSize + totalGaps * _maxSpaceBetweenItems; // total size of items and spaces if we had enough room
  115. int spaces = totalGaps * _maxSpaceBetweenItems; // We'll decrement this below to place one space between each item until we run out
  116. if (totalItemsSize >= containerSize)
  117. {
  118. spaces = 0;
  119. }
  120. else if (totalItemsAndSpaces > containerSize)
  121. {
  122. spaces = containerSize - totalItemsSize;
  123. }
  124. switch (justification)
  125. {
  126. case Justification.Left:
  127. var currentPosition = 0;
  128. for (var i = 0; i < sizes.Length; i++)
  129. {
  130. if (sizes [i] < 0)
  131. {
  132. throw new ArgumentException ("The size of an item cannot be negative.");
  133. }
  134. if (i == 0)
  135. {
  136. positions [0] = 0; // first item position
  137. continue;
  138. }
  139. int spaceBefore = spaces-- > 0 ? _maxSpaceBetweenItems : 0;
  140. // subsequent items are placed one space after the previous item
  141. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  142. }
  143. break;
  144. case Justification.Right:
  145. currentPosition = Math.Max (0, containerSize - totalItemsSize - spaces);
  146. for (var i = 0; i < sizes.Length; i++)
  147. {
  148. if (sizes [i] < 0)
  149. {
  150. throw new ArgumentException ("The size of an item cannot be negative.");
  151. }
  152. int spaceBefore = spaces-- > 0 ? _maxSpaceBetweenItems : 0;
  153. positions [i] = currentPosition;
  154. currentPosition += sizes [i] + spaceBefore;
  155. }
  156. break;
  157. case Justification.Centered:
  158. if (sizes.Length > 1)
  159. {
  160. // remaining space to be distributed before first and after the items
  161. int remainingSpace = Math.Max (0, containerSize - totalItemsSize - spaces);
  162. for (var i = 0; i < sizes.Length; i++)
  163. {
  164. if (sizes [i] < 0)
  165. {
  166. throw new ArgumentException ("The size of an item cannot be negative.");
  167. }
  168. if (i == 0)
  169. {
  170. positions [i] = remainingSpace / 2; // first item position
  171. continue;
  172. }
  173. int spaceBefore = spaces-- > 0 ? _maxSpaceBetweenItems : 0;
  174. // subsequent items are placed one space after the previous item
  175. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  176. }
  177. }
  178. else if (sizes.Length == 1)
  179. {
  180. if (sizes [0] < 0)
  181. {
  182. throw new ArgumentException ("The size of an item cannot be negative.");
  183. }
  184. positions [0] = (containerSize - sizes [0]) / 2; // single item is centered
  185. }
  186. break;
  187. case Justification.Justified:
  188. int spaceBetween = sizes.Length > 1 ? (containerSize - totalItemsSize) / (sizes.Length - 1) : 0;
  189. int remainder = sizes.Length > 1 ? (containerSize - totalItemsSize) % (sizes.Length - 1) : 0;
  190. currentPosition = 0;
  191. for (var i = 0; i < sizes.Length; i++)
  192. {
  193. if (sizes [i] < 0)
  194. {
  195. throw new ArgumentException ("The size of an item cannot be negative.");
  196. }
  197. positions [i] = currentPosition;
  198. int extraSpace = i < remainder ? 1 : 0;
  199. currentPosition += sizes [i] + spaceBetween + extraSpace;
  200. }
  201. break;
  202. // 111 2222 33333
  203. case Justification.LastRightRestLeft:
  204. if (sizes.Length > 1)
  205. {
  206. currentPosition = 0;
  207. for (var i = 0; i < sizes.Length; i++)
  208. {
  209. if (sizes [i] < 0)
  210. {
  211. throw new ArgumentException ("The size of an item cannot be negative.");
  212. }
  213. if (i < sizes.Length - 1)
  214. {
  215. int spaceBefore = spaces-- > 0 ? _maxSpaceBetweenItems : 0;
  216. positions [i] = currentPosition;
  217. currentPosition += sizes [i] + spaceBefore;
  218. }
  219. }
  220. positions [sizes.Length - 1] = containerSize - sizes [sizes.Length - 1];
  221. }
  222. else if (sizes.Length == 1)
  223. {
  224. if (sizes [0] < 0)
  225. {
  226. throw new ArgumentException ("The size of an item cannot be negative.");
  227. }
  228. positions [0] = containerSize - sizes [0]; // single item is flush right
  229. }
  230. break;
  231. // 111 2222 33333
  232. case Justification.FirstLeftRestRight:
  233. if (sizes.Length > 1)
  234. {
  235. currentPosition = 0;
  236. positions [0] = currentPosition; // first item is flush left
  237. for (int i = sizes.Length - 1; i >= 0; i--)
  238. {
  239. if (sizes [i] < 0)
  240. {
  241. throw new ArgumentException ("The size of an item cannot be negative.");
  242. }
  243. if (i == sizes.Length - 1)
  244. {
  245. // start at right
  246. currentPosition = containerSize - sizes [i];
  247. positions [i] = currentPosition;
  248. }
  249. if (i < sizes.Length - 1 && i > 0)
  250. {
  251. int spaceBefore = spaces-- > 0 ? _maxSpaceBetweenItems : 0;
  252. positions [i] = currentPosition - sizes [i] - spaceBefore;
  253. currentPosition = positions [i];
  254. }
  255. }
  256. }
  257. else if (sizes.Length == 1)
  258. {
  259. if (sizes [0] < 0)
  260. {
  261. throw new ArgumentException ("The size of an item cannot be negative.");
  262. }
  263. positions [0] = 0; // single item is flush left
  264. }
  265. break;
  266. default:
  267. throw new ArgumentOutOfRangeException (nameof (justification), justification, null);
  268. }
  269. return positions;
  270. }
  271. }