Justification.cs 13 KB

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