Aligner.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. }
  109. break;
  110. case Alignment.End:
  111. switch (alignmentMode & ~AlignmentModes.AddSpaceBetweenItems)
  112. {
  113. case AlignmentModes.StartToEnd:
  114. return End (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive);
  115. case AlignmentModes.StartToEnd | AlignmentModes.IgnoreFirstOrLast:
  116. return IgnoreFirst (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive);
  117. case AlignmentModes.EndToStart:
  118. return Start (in sizesCopy, maxSpaceBetweenItems, spacesToGive).Reverse ().ToArray ();
  119. }
  120. break;
  121. case Alignment.Center:
  122. switch (alignmentMode & ~AlignmentModes.AddSpaceBetweenItems)
  123. {
  124. case AlignmentModes.StartToEnd:
  125. return Center (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive);
  126. case AlignmentModes.EndToStart:
  127. return Center (in sizesCopy, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive).Reverse ().ToArray ();
  128. }
  129. break;
  130. case Alignment.Fill:
  131. switch (alignmentMode & ~AlignmentModes.AddSpaceBetweenItems)
  132. {
  133. case AlignmentModes.StartToEnd:
  134. return Fill (in sizesCopy, containerSize, totalItemsSize);
  135. case AlignmentModes.EndToStart:
  136. return Fill (in sizesCopy, containerSize, totalItemsSize).Reverse ().ToArray ();
  137. }
  138. break;
  139. default:
  140. throw new ArgumentOutOfRangeException (nameof (alignment), alignment, null);
  141. }
  142. return [];
  143. }
  144. internal static int [] Start (ref readonly int [] sizes, int maxSpaceBetweenItems, int spacesToGive)
  145. {
  146. var positions = new int [sizes.Length]; // positions of the items. the return value.
  147. for (var i = 0; i < sizes.Length; i++)
  148. {
  149. CheckSizeCannotBeNegative (i, in sizes);
  150. if (i == 0)
  151. {
  152. positions [0] = 0; // first item position
  153. continue;
  154. }
  155. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  156. // subsequent items are placed one space after the previous item
  157. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  158. }
  159. return positions;
  160. }
  161. internal static int [] IgnoreFirst (
  162. ref readonly int [] sizes,
  163. int containerSize,
  164. int totalItemsSize,
  165. int maxSpaceBetweenItems,
  166. int spacesToGive
  167. )
  168. {
  169. var positions = new int [sizes.Length]; // positions of the items. the return value.
  170. if (sizes.Length > 1)
  171. {
  172. var currentPosition = 0;
  173. positions [0] = currentPosition; // first item is flush left
  174. for (int i = sizes.Length - 1; i >= 0; i--)
  175. {
  176. CheckSizeCannotBeNegative (i, in sizes);
  177. if (i == sizes.Length - 1)
  178. {
  179. // start at right
  180. currentPosition = Math.Max (totalItemsSize, containerSize) - sizes [i];
  181. positions [i] = currentPosition;
  182. }
  183. if (i < sizes.Length - 1 && i > 0)
  184. {
  185. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  186. positions [i] = currentPosition - sizes [i] - spaceBefore;
  187. currentPosition = positions [i];
  188. }
  189. }
  190. }
  191. else if (sizes.Length == 1)
  192. {
  193. CheckSizeCannotBeNegative (0, in sizes);
  194. positions [0] = 0; // single item is flush left
  195. }
  196. return positions;
  197. }
  198. internal static int [] IgnoreLast (
  199. ref readonly int [] sizes,
  200. int containerSize,
  201. int totalItemsSize,
  202. int maxSpaceBetweenItems,
  203. int spacesToGive
  204. )
  205. {
  206. var positions = new int [sizes.Length]; // positions of the items. the return value.
  207. if (sizes.Length > 1)
  208. {
  209. var currentPosition = 0;
  210. if (totalItemsSize > containerSize)
  211. {
  212. currentPosition = containerSize - totalItemsSize - spacesToGive;
  213. }
  214. for (var i = 0; i < sizes.Length; i++)
  215. {
  216. CheckSizeCannotBeNegative (i, in sizes);
  217. if (i < sizes.Length - 1)
  218. {
  219. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  220. positions [i] = currentPosition;
  221. currentPosition += sizes [i] + spaceBefore;
  222. }
  223. }
  224. positions [sizes.Length - 1] = containerSize - sizes [^1];
  225. }
  226. else if (sizes.Length == 1)
  227. {
  228. CheckSizeCannotBeNegative (0, in sizes);
  229. positions [0] = containerSize - sizes [0]; // single item is flush right
  230. }
  231. return positions;
  232. }
  233. internal static int [] Fill (ref readonly int [] sizes, int containerSize, int totalItemsSize)
  234. {
  235. var positions = new int [sizes.Length]; // positions of the items. the return value.
  236. int spaceBetween = sizes.Length > 1 ? (containerSize - totalItemsSize) / (sizes.Length - 1) : 0;
  237. int remainder = sizes.Length > 1 ? (containerSize - totalItemsSize) % (sizes.Length - 1) : 0;
  238. var currentPosition = 0;
  239. for (var i = 0; i < sizes.Length; i++)
  240. {
  241. CheckSizeCannotBeNegative (i, in sizes);
  242. positions [i] = currentPosition;
  243. int extraSpace = i < remainder ? 1 : 0;
  244. currentPosition += sizes [i] + spaceBetween + extraSpace;
  245. }
  246. return positions;
  247. }
  248. internal static int [] Center (ref readonly int [] sizes, int containerSize, int totalItemsSize, int maxSpaceBetweenItems, int spacesToGive)
  249. {
  250. var positions = new int [sizes.Length]; // positions of the items. the return value.
  251. if (sizes.Length > 1)
  252. {
  253. // remaining space to be distributed before first and after the items
  254. int remainingSpace = containerSize - totalItemsSize - spacesToGive;
  255. for (var i = 0; i < sizes.Length; i++)
  256. {
  257. CheckSizeCannotBeNegative (i, in sizes);
  258. if (i == 0)
  259. {
  260. positions [i] = remainingSpace / 2; // first item position
  261. continue;
  262. }
  263. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  264. // subsequent items are placed one space after the previous item
  265. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  266. }
  267. }
  268. else if (sizes.Length == 1)
  269. {
  270. CheckSizeCannotBeNegative (0, in sizes);
  271. positions [0] = (containerSize - sizes [0]) / 2; // single item is centered
  272. }
  273. return positions;
  274. }
  275. internal static int [] End (ref readonly int [] sizes, int containerSize, int totalItemsSize, int maxSpaceBetweenItems, int spacesToGive)
  276. {
  277. var positions = new int [sizes.Length]; // positions of the items. the return value.
  278. int currentPosition = containerSize - totalItemsSize - spacesToGive;
  279. for (var i = 0; i < sizes.Length; i++)
  280. {
  281. CheckSizeCannotBeNegative (i, in sizes);
  282. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  283. positions [i] = currentPosition;
  284. currentPosition += sizes [i] + spaceBefore;
  285. }
  286. return positions;
  287. }
  288. private static void CheckSizeCannotBeNegative (int i, ref readonly int [] sizes)
  289. {
  290. if (sizes [i] < 0)
  291. {
  292. throw new ArgumentException ("The size of an item cannot be negative.");
  293. }
  294. }
  295. }