Aligner.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 | AlignmentModes.AddSpaceBetweenItems;
  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 (alignmentMode.HasFlag (AlignmentModes.EndToStart))
  76. {
  77. throw new NotImplementedException ("EndToStart is not implemented.");
  78. }
  79. if (sizes.Length == 0)
  80. {
  81. return [];
  82. }
  83. int maxSpaceBetweenItems = alignmentMode.HasFlag (AlignmentModes.AddSpaceBetweenItems) ? 1 : 0;
  84. int totalItemsSize = sizes.Sum ();
  85. int totalGaps = sizes.Length - 1; // total gaps between items
  86. int totalItemsAndSpaces = totalItemsSize + totalGaps * maxSpaceBetweenItems; // total size of items and spacesToGive if we had enough room
  87. int spacesToGive = totalGaps * maxSpaceBetweenItems; // We'll decrement this below to place one space between each item until we run out
  88. if (totalItemsSize >= containerSize)
  89. {
  90. spacesToGive = 0;
  91. }
  92. else if (totalItemsAndSpaces > containerSize)
  93. {
  94. spacesToGive = containerSize - totalItemsSize;
  95. }
  96. switch (alignment)
  97. {
  98. case Alignment.Start:
  99. switch (alignmentMode & ~AlignmentModes.AddSpaceBetweenItems)
  100. {
  101. case AlignmentModes.StartToEnd:
  102. return Start (in sizes, maxSpaceBetweenItems, spacesToGive);
  103. case AlignmentModes.StartToEnd | AlignmentModes.IgnoreFirstOrLast:
  104. return IgnoreLast (in sizes, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive);
  105. }
  106. break;
  107. case Alignment.End:
  108. switch (alignmentMode & ~AlignmentModes.AddSpaceBetweenItems)
  109. {
  110. case AlignmentModes.StartToEnd:
  111. return End (in sizes, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive);
  112. case AlignmentModes.StartToEnd | AlignmentModes.IgnoreFirstOrLast:
  113. return IgnoreFirst (in sizes, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive);
  114. }
  115. break;
  116. case Alignment.Center:
  117. return Center (in sizes, containerSize, totalItemsSize, maxSpaceBetweenItems, spacesToGive);
  118. case Alignment.Fill:
  119. return Fill (in sizes, containerSize, totalItemsSize);
  120. default:
  121. throw new ArgumentOutOfRangeException (nameof (alignment), alignment, null);
  122. }
  123. return [];
  124. }
  125. internal static int [] Start (ref readonly int [] sizes, int maxSpaceBetweenItems, int spacesToGive)
  126. {
  127. var positions = new int [sizes.Length]; // positions of the items. the return value.
  128. for (var i = 0; i < sizes.Length; i++)
  129. {
  130. CheckSizeCannotBeNegative (i, in sizes);
  131. if (i == 0)
  132. {
  133. positions [0] = 0; // first item position
  134. continue;
  135. }
  136. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  137. // subsequent items are placed one space after the previous item
  138. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  139. }
  140. return positions;
  141. }
  142. internal static int [] IgnoreFirst (
  143. ref readonly int [] sizes,
  144. int containerSize,
  145. int totalItemsSize,
  146. int maxSpaceBetweenItems,
  147. int spacesToGive
  148. )
  149. {
  150. var positions = new int [sizes.Length]; // positions of the items. the return value.
  151. if (sizes.Length > 1)
  152. {
  153. var currentPosition = 0;
  154. positions [0] = currentPosition; // first item is flush left
  155. for (int i = sizes.Length - 1; i >= 0; i--)
  156. {
  157. CheckSizeCannotBeNegative (i, in sizes);
  158. if (i == sizes.Length - 1)
  159. {
  160. // start at right
  161. currentPosition = Math.Max (totalItemsSize, containerSize) - sizes [i];
  162. positions [i] = currentPosition;
  163. }
  164. if (i < sizes.Length - 1 && i > 0)
  165. {
  166. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  167. positions [i] = currentPosition - sizes [i] - spaceBefore;
  168. currentPosition = positions [i];
  169. }
  170. }
  171. }
  172. else if (sizes.Length == 1)
  173. {
  174. CheckSizeCannotBeNegative (0, in sizes);
  175. positions [0] = 0; // single item is flush left
  176. }
  177. return positions;
  178. }
  179. internal static int [] IgnoreLast (
  180. ref readonly int [] sizes,
  181. int containerSize,
  182. int totalItemsSize,
  183. int maxSpaceBetweenItems,
  184. int spacesToGive
  185. )
  186. {
  187. var positions = new int [sizes.Length]; // positions of the items. the return value.
  188. if (sizes.Length > 1)
  189. {
  190. var currentPosition = 0;
  191. if (totalItemsSize > containerSize)
  192. {
  193. currentPosition = containerSize - totalItemsSize - spacesToGive;
  194. }
  195. for (var i = 0; i < sizes.Length; i++)
  196. {
  197. CheckSizeCannotBeNegative (i, in sizes);
  198. if (i < sizes.Length - 1)
  199. {
  200. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  201. positions [i] = currentPosition;
  202. currentPosition += sizes [i] + spaceBefore;
  203. }
  204. }
  205. positions [sizes.Length - 1] = containerSize - sizes [^1];
  206. }
  207. else if (sizes.Length == 1)
  208. {
  209. CheckSizeCannotBeNegative (0, in sizes);
  210. positions [0] = containerSize - sizes [0]; // single item is flush right
  211. }
  212. return positions;
  213. }
  214. internal static int [] Fill (ref readonly int [] sizes, int containerSize, int totalItemsSize)
  215. {
  216. var positions = new int [sizes.Length]; // positions of the items. the return value.
  217. int spaceBetween = sizes.Length > 1 ? (containerSize - totalItemsSize) / (sizes.Length - 1) : 0;
  218. int remainder = sizes.Length > 1 ? (containerSize - totalItemsSize) % (sizes.Length - 1) : 0;
  219. var currentPosition = 0;
  220. for (var i = 0; i < sizes.Length; i++)
  221. {
  222. CheckSizeCannotBeNegative (i, in sizes);
  223. positions [i] = currentPosition;
  224. int extraSpace = i < remainder ? 1 : 0;
  225. currentPosition += sizes [i] + spaceBetween + extraSpace;
  226. }
  227. return positions;
  228. }
  229. internal static int [] Center (ref readonly int [] sizes, int containerSize, int totalItemsSize, int maxSpaceBetweenItems, int spacesToGive)
  230. {
  231. var positions = new int [sizes.Length]; // positions of the items. the return value.
  232. if (sizes.Length > 1)
  233. {
  234. // remaining space to be distributed before first and after the items
  235. int remainingSpace = containerSize - totalItemsSize - spacesToGive;
  236. for (var i = 0; i < sizes.Length; i++)
  237. {
  238. CheckSizeCannotBeNegative (i, in sizes);
  239. if (i == 0)
  240. {
  241. positions [i] = remainingSpace / 2; // first item position
  242. continue;
  243. }
  244. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  245. // subsequent items are placed one space after the previous item
  246. positions [i] = positions [i - 1] + sizes [i - 1] + spaceBefore;
  247. }
  248. }
  249. else if (sizes.Length == 1)
  250. {
  251. CheckSizeCannotBeNegative (0, in sizes);
  252. positions [0] = (containerSize - sizes [0]) / 2; // single item is centered
  253. }
  254. return positions;
  255. }
  256. internal static int [] End (ref readonly int [] sizes, int containerSize, int totalItemsSize, int maxSpaceBetweenItems, int spacesToGive)
  257. {
  258. var positions = new int [sizes.Length]; // positions of the items. the return value.
  259. int currentPosition = containerSize - totalItemsSize - spacesToGive;
  260. for (var i = 0; i < sizes.Length; i++)
  261. {
  262. CheckSizeCannotBeNegative (i, in sizes);
  263. int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;
  264. positions [i] = currentPosition;
  265. currentPosition += sizes [i] + spaceBefore;
  266. }
  267. return positions;
  268. }
  269. private static void CheckSizeCannotBeNegative (int i, ref readonly int [] sizes)
  270. {
  271. if (sizes [i] < 0)
  272. {
  273. throw new ArgumentException ("The size of an item cannot be negative.");
  274. }
  275. }
  276. }