Justification.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. if (sizes [i] < 0)
  139. {
  140. throw new ArgumentException ("The size of an item cannot be negative.");
  141. }
  142. if (i == 0)
  143. {
  144. positions [0] = 0; // first item position
  145. continue;
  146. }
  147. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  148. // subsequent items are placed one space after the previous item
  149. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  150. }
  151. break;
  152. case Justification.Right:
  153. currentPosition = Math.Max (0, containerSize - totalItemsSize - spaces);
  154. for (var i = 0; i < sizes.Length; i++)
  155. {
  156. if (sizes [i] < 0)
  157. {
  158. throw new ArgumentException ("The size of an item cannot be negative.");
  159. }
  160. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  161. positions [i] = currentPosition;
  162. currentPosition += sizes [i] + spaceBefore;
  163. }
  164. break;
  165. case Justification.Centered:
  166. if (sizes.Length > 1)
  167. {
  168. // remaining space to be distributed before first and after the items
  169. int remainingSpace = Math.Max (0, containerSize - totalItemsSize - spaces);
  170. for (var i = 0; i < sizes.Length; i++)
  171. {
  172. if (sizes [i] < 0)
  173. {
  174. throw new ArgumentException ("The size of an item cannot be negative.");
  175. }
  176. if (i == 0)
  177. {
  178. positions [i] = remainingSpace / 2; // first item position
  179. continue;
  180. }
  181. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  182. // subsequent items are placed one space after the previous item
  183. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  184. }
  185. }
  186. else if (sizes.Length == 1)
  187. {
  188. if (sizes [0] < 0)
  189. {
  190. throw new ArgumentException ("The size of an item cannot be negative.");
  191. }
  192. positions [0] = (containerSize - sizes [0]) / 2; // single item is centered
  193. }
  194. break;
  195. case Justification.Justified:
  196. int spaceBetween = sizes.Length > 1 ? (containerSize - totalItemsSize) / (sizes.Length - 1) : 0;
  197. int remainder = sizes.Length > 1 ? (containerSize - totalItemsSize) % (sizes.Length - 1) : 0;
  198. currentPosition = 0;
  199. for (var i = 0; i < sizes.Length; i++)
  200. {
  201. if (sizes [i] < 0)
  202. {
  203. throw new ArgumentException ("The size of an item cannot be negative.");
  204. }
  205. positions [i] = currentPosition;
  206. int extraSpace = i < remainder ? 1 : 0;
  207. currentPosition += sizes [i] + spaceBetween + extraSpace;
  208. }
  209. break;
  210. // 111 2222 33333
  211. case Justification.LastRightRestLeft:
  212. if (sizes.Length > 1)
  213. {
  214. currentPosition = 0;
  215. for (var i = 0; i < sizes.Length; i++)
  216. {
  217. if (sizes [i] < 0)
  218. {
  219. throw new ArgumentException ("The size of an item cannot be negative.");
  220. }
  221. if (i < sizes.Length - 1)
  222. {
  223. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  224. positions [i] = currentPosition;
  225. currentPosition += sizes [i] + spaceBefore;
  226. }
  227. }
  228. positions [sizes.Length - 1] = containerSize - sizes [sizes.Length - 1];
  229. }
  230. else if (sizes.Length == 1)
  231. {
  232. if (sizes [0] < 0)
  233. {
  234. throw new ArgumentException ("The size of an item cannot be negative.");
  235. }
  236. positions [0] = containerSize - sizes [0]; // single item is flush right
  237. }
  238. break;
  239. // 111 2222 33333
  240. case Justification.FirstLeftRestRight:
  241. if (sizes.Length > 1)
  242. {
  243. currentPosition = 0;
  244. positions [0] = currentPosition; // first item is flush left
  245. for (int i = sizes.Length - 1; i >= 0; i--)
  246. {
  247. if (sizes [i] < 0)
  248. {
  249. throw new ArgumentException ("The size of an item cannot be negative.");
  250. }
  251. if (i == sizes.Length - 1)
  252. {
  253. // start at right
  254. currentPosition = containerSize - sizes [i];
  255. positions [i] = currentPosition;
  256. }
  257. if (i < sizes.Length - 1 && i > 0)
  258. {
  259. int spaceBefore = spaces-- > 0 ? maxSpaceBetweenItems : 0;
  260. positions [i] = currentPosition - sizes [i] - spaceBefore;
  261. currentPosition = positions [i];
  262. }
  263. }
  264. }
  265. else if (sizes.Length == 1)
  266. {
  267. if (sizes [0] < 0)
  268. {
  269. throw new ArgumentException ("The size of an item cannot be negative.");
  270. }
  271. positions [0] = 0; // single item is flush left
  272. }
  273. break;
  274. default:
  275. throw new ArgumentOutOfRangeException (nameof (justification), justification, null);
  276. }
  277. return positions;
  278. }
  279. }