Aligner.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using System.ComponentModel;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Aligns items within a container based on the specified <see cref="Gui.Alignment"/>. Both horizontal and vertical
  5. /// alignments are supported.
  6. /// </summary>
  7. public class Aligner : INotifyPropertyChanged
  8. {
  9. private Alignment _alignment;
  10. /// <summary>
  11. /// Gets or sets how the <see cref="Aligner"/> aligns items within a container.
  12. /// </summary>
  13. /// <remarks>
  14. /// <para>
  15. /// <see cref="AlignmentModes"/> provides additional options for aligning items in a container.
  16. /// </para>
  17. /// </remarks>
  18. public Alignment Alignment
  19. {
  20. get => _alignment;
  21. set
  22. {
  23. _alignment = value;
  24. PropertyChanged?.Invoke (this, new (nameof (Alignment)));
  25. }
  26. }
  27. private AlignmentModes _alignmentMode = AlignmentModes.StartToEnd;
  28. /// <summary>
  29. /// Gets or sets the modes controlling <see cref="Alignment"/>.
  30. /// </summary>
  31. public AlignmentModes AlignmentModes
  32. {
  33. get => _alignmentMode;
  34. set
  35. {
  36. _alignmentMode = value;
  37. PropertyChanged?.Invoke (this, new (nameof (AlignmentModes)));
  38. }
  39. }
  40. private int _containerSize;
  41. /// <summary>
  42. /// The size of the container.
  43. /// </summary>
  44. public int ContainerSize
  45. {
  46. get => _containerSize;
  47. set
  48. {
  49. _containerSize = value;
  50. PropertyChanged?.Invoke (this, new (nameof (ContainerSize)));
  51. }
  52. }
  53. /// <inheritdoc/>
  54. public event PropertyChangedEventHandler PropertyChanged;
  55. /// <summary>
  56. /// Takes a list of item sizes and returns a list of the positions of those items when aligned within
  57. /// <see name="ContainerSize"/>
  58. /// using the <see cref="Alignment"/> and <see cref="AlignmentModes"/> settings.
  59. /// </summary>
  60. /// <param name="sizes">The sizes of the items to align.</param>
  61. /// <returns>The locations of the items, from left/top to right/bottom.</returns>
  62. public int [] Align (int [] sizes) { return Align (Alignment, AlignmentModes, ContainerSize, sizes); }
  63. /// <summary>
  64. /// Takes a list of item sizes and returns a list of the positions of those items when aligned within
  65. /// <paramref name="containerSize"/>
  66. /// using specified parameters.
  67. /// </summary>
  68. /// <param name="alignment">Specifies how the items will be aligned.</param>
  69. /// <param name="alignmentMode"></param>
  70. /// <param name="containerSize">The size of the container.</param>
  71. /// <param name="sizes">The sizes of the items to align.</param>
  72. /// <returns>The positions of the items, from left/top to right/bottom.</returns>
  73. public static int [] Align (in Alignment alignment, in AlignmentModes alignmentMode, in int containerSize, in int [] sizes)
  74. {
  75. if (sizes.Length == 0)
  76. {
  77. return [];
  78. }
  79. var sizesCopy = sizes;
  80. if (alignmentMode.FastHasFlags (AlignmentModes.EndToStart))
  81. {
  82. sizesCopy = sizes.Reverse ().ToArray ();
  83. }
  84. int maxSpaceBetweenItems = alignmentMode.FastHasFlags (AlignmentModes.AddSpaceBetweenItems) ? 1 : 0;
  85. int totalItemsSize = sizes.Sum ();
  86. int totalGaps = sizes.Length - 1; // total gaps between items
  87. int totalItemsAndSpaces = totalItemsSize + totalGaps * maxSpaceBetweenItems; // total size of items and spacesToGive if we had enough room
  88. int spacesToGive = totalGaps * maxSpaceBetweenItems; // We'll decrement this below to place one space between each item until we run out
  89. if (totalItemsSize >= containerSize)
  90. {
  91. spacesToGive = 0;
  92. }
  93. else if (totalItemsAndSpaces > containerSize)
  94. {
  95. spacesToGive = containerSize - totalItemsSize;
  96. }
  97. switch (alignment)
  98. {
  99. case Alignment.Start:
  100. switch (alignmentMode & ~AlignmentModes.AddSpaceBetweenItems)
  101. {
  102. case AlignmentModes.StartToEnd:
  103. return Start (in sizesCopy, maxSpaceBetweenItems, spacesToGive);
  104. case AlignmentModes.StartToEnd | AlignmentModes.IgnoreFirstOrLast:
  105. return IgnoreLast (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive);
  106. case AlignmentModes.EndToStart:
  107. return End (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive).Reverse ().ToArray ();
  108. case AlignmentModes.EndToStart | AlignmentModes.IgnoreFirstOrLast:
  109. return IgnoreFirst (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive).Reverse ().ToArray (); ;
  110. }
  111. break;
  112. case Alignment.End:
  113. switch (alignmentMode & ~AlignmentModes.AddSpaceBetweenItems)
  114. {
  115. case AlignmentModes.StartToEnd:
  116. return End (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive);
  117. case AlignmentModes.StartToEnd | AlignmentModes.IgnoreFirstOrLast:
  118. return IgnoreFirst (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive);
  119. case AlignmentModes.EndToStart:
  120. return Start (in sizesCopy, maxSpaceBetweenItems, spacesToGive).Reverse ().ToArray ();
  121. case AlignmentModes.EndToStart | AlignmentModes.IgnoreFirstOrLast:
  122. return IgnoreLast (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive).Reverse ().ToArray (); ;
  123. }
  124. break;
  125. case Alignment.Center:
  126. switch (alignmentMode & ~AlignmentModes.AddSpaceBetweenItems)
  127. {
  128. case AlignmentModes.StartToEnd:
  129. return Center (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive);
  130. case AlignmentModes.EndToStart:
  131. return Center (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive).Reverse ().ToArray ();
  132. }
  133. break;
  134. case Alignment.Fill:
  135. switch (alignmentMode & ~AlignmentModes.AddSpaceBetweenItems)
  136. {
  137. case AlignmentModes.StartToEnd:
  138. return Fill (in sizesCopy, containerSize, totalItemsSize);
  139. case AlignmentModes.EndToStart:
  140. return Fill (in sizesCopy, containerSize, totalItemsSize).Reverse ().ToArray ();
  141. }
  142. break;
  143. default:
  144. throw new ArgumentOutOfRangeException (nameof (alignment), alignment, null);
  145. }
  146. return [];
  147. }
  148. internal static int [] Start (ref readonly int [] sizes, int maxSpaceBetweenItems, int spacesToGive)
  149. {
  150. var positions = new int [sizes.Length]; // positions of the items. the return value.
  151. for (var i = 0; i < sizes.Length; i++)
  152. {
  153. CheckSizeCannotBeNegative (i, in sizes);
  154. if (i == 0)
  155. {
  156. positions [0] = 0; // first item position
  157. continue;
  158. }
  159. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  160. // subsequent items are placed one space after the previous item
  161. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  162. }
  163. return positions;
  164. }
  165. internal static int [] IgnoreFirst (
  166. ref readonly int [] sizes,
  167. int containerSize,
  168. int totalItemsSize,
  169. int maxSpaceBetweenItems,
  170. int spacesToGive
  171. )
  172. {
  173. var positions = new int [sizes.Length]; // positions of the items. the return value.
  174. if (sizes.Length > 1)
  175. {
  176. var currentPosition = 0;
  177. positions [0] = currentPosition; // first item is flush left
  178. for (int i = sizes.Length - 1; i >= 0; i--)
  179. {
  180. CheckSizeCannotBeNegative (i, in sizes);
  181. if (i == sizes.Length - 1)
  182. {
  183. // start at right
  184. currentPosition = Math.Max (totalItemsSize, containerSize) - sizes [i];
  185. positions [i] = currentPosition;
  186. }
  187. if (i < sizes.Length - 1 && i > 0)
  188. {
  189. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  190. positions [i] = currentPosition - sizes [i] - spaceBefore;
  191. currentPosition = positions [i];
  192. }
  193. }
  194. }
  195. else if (sizes.Length == 1)
  196. {
  197. CheckSizeCannotBeNegative (0, in sizes);
  198. positions [0] = 0; // single item is flush left
  199. }
  200. return positions;
  201. }
  202. internal static int [] IgnoreLast (
  203. ref readonly int [] sizes,
  204. int containerSize,
  205. int totalItemsSize,
  206. int maxSpaceBetweenItems,
  207. int spacesToGive
  208. )
  209. {
  210. var positions = new int [sizes.Length]; // positions of the items. the return value.
  211. if (sizes.Length > 1)
  212. {
  213. var currentPosition = 0;
  214. if (totalItemsSize > containerSize)
  215. {
  216. currentPosition = containerSize - totalItemsSize - spacesToGive;
  217. }
  218. for (var i = 0; i < sizes.Length; i++)
  219. {
  220. CheckSizeCannotBeNegative (i, in sizes);
  221. if (i < sizes.Length - 1)
  222. {
  223. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  224. positions [i] = currentPosition;
  225. currentPosition += sizes [i] + spaceBefore;
  226. }
  227. }
  228. positions [sizes.Length - 1] = containerSize - sizes [^1];
  229. }
  230. else if (sizes.Length == 1)
  231. {
  232. CheckSizeCannotBeNegative (0, in sizes);
  233. positions [0] = containerSize - sizes [0]; // single item is flush right
  234. }
  235. return positions;
  236. }
  237. internal static int [] Fill (ref readonly int [] sizes, int containerSize, int totalItemsSize)
  238. {
  239. var positions = new int [sizes.Length]; // positions of the items. the return value.
  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. var currentPosition = 0;
  243. for (var i = 0; i < sizes.Length; i++)
  244. {
  245. CheckSizeCannotBeNegative (i, in sizes);
  246. positions [i] = currentPosition;
  247. int extraSpace = i < remainder ? 1 : 0;
  248. currentPosition += sizes [i] + spaceBetween + extraSpace;
  249. }
  250. return positions;
  251. }
  252. internal static int [] Center (ref readonly int [] sizes, int containerSize, int totalItemsSize, int maxSpaceBetweenItems, int spacesToGive)
  253. {
  254. var positions = new int [sizes.Length]; // positions of the items. the return value.
  255. if (sizes.Length > 1)
  256. {
  257. // remaining space to be distributed before first and after the items
  258. int remainingSpace = containerSize - totalItemsSize - spacesToGive;
  259. for (var i = 0; i < sizes.Length; i++)
  260. {
  261. CheckSizeCannotBeNegative (i, in sizes);
  262. if (i == 0)
  263. {
  264. positions [i] = remainingSpace / 2; // first item position
  265. continue;
  266. }
  267. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  268. // subsequent items are placed one space after the previous item
  269. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  270. }
  271. }
  272. else if (sizes.Length == 1)
  273. {
  274. CheckSizeCannotBeNegative (0, in sizes);
  275. positions [0] = (containerSize - sizes [0]) / 2; // single item is centered
  276. }
  277. return positions;
  278. }
  279. internal static int [] End (ref readonly int [] sizes, int containerSize, int totalItemsSize, int maxSpaceBetweenItems, int spacesToGive)
  280. {
  281. var positions = new int [sizes.Length]; // positions of the items. the return value.
  282. int currentPosition = containerSize - totalItemsSize - spacesToGive;
  283. for (var i = 0; i < sizes.Length; i++)
  284. {
  285. CheckSizeCannotBeNegative (i, in sizes);
  286. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  287. positions [i] = currentPosition;
  288. currentPosition += sizes [i] + spaceBefore;
  289. }
  290. return positions;
  291. }
  292. private static void CheckSizeCannotBeNegative (int i, ref readonly int [] sizes)
  293. {
  294. if (sizes [i] < 0)
  295. {
  296. throw new ArgumentException ("The size of an item cannot be negative.");
  297. }
  298. }
  299. }